Initial port of stecman/hw-boot-selection to ESP32-S3
ESPHome external_component that presents a synthetic 64 KB FAT12 disk over the ESP32-S3's native USB-OTG, containing a one-byte switch_position file and a switch_position_grub.cfg snippet. The disk's volume serial is fixed at 0x55AA6922 to stay drop-in compatible with the upstream /etc/grub.d hook. Components: - fat12 / msc_disk: portable FAT12 sector synthesiser ported from reference/src/fat.c and the boot-sector / FAT / dir-entry generation in reference/src/usb.c. - usb_descriptors / tusb_glue: TinyUSB MSC class with our own callbacks delegating to MscDisk::read_lba (linker --allow-multiple-definition picks ours over esp_tinyusb's storage-backed defaults). - grub_boot_switch: top-level Component, NVS persistence on target changes, exposes targets table to sub-platforms. - select: HA-facing select platform reading parent's targets map. Validated end-to-end on an ESP32-S3-DevKitC-1: USB enumerates as 26ba:8003, /dev/sdb mounts FAT12, mtype reads back the live target, and POST /select/boot_target/set?option=Windows updates both files on the next host read.
This commit is contained in:
102
components/grub_boot_switch/__init__.py
Normal file
102
components/grub_boot_switch/__init__.py
Normal file
@@ -0,0 +1,102 @@
|
||||
"""ESPHome external_component: GRUB boot switch over USB MSC.
|
||||
|
||||
Presents a synthetic 64 KB FAT12 disk to the attached host containing two
|
||||
virtual files:
|
||||
|
||||
switch_position - one ASCII digit (0-9) reflecting the current
|
||||
boot target.
|
||||
switch_position_grub.cfg - GRUB snippet `set os_hw_switch=N\\n` consumed
|
||||
by the user's /etc/grub.d/01_bootswitch hook.
|
||||
|
||||
The volume serial defaults to 0x55AA6922 to stay drop-in compatible with
|
||||
the upstream stecman/hw-boot-selection firmware.
|
||||
"""
|
||||
|
||||
from esphome import automation
|
||||
import esphome.codegen as cg
|
||||
import esphome.config_validation as cv
|
||||
from esphome.const import CONF_ID
|
||||
from esphome.components import esp32
|
||||
|
||||
DEPENDENCIES = ["esp32"]
|
||||
CODEOWNERS = ["@adamksmith"]
|
||||
MULTI_CONF = False
|
||||
|
||||
grub_boot_switch_ns = cg.esphome_ns.namespace("grub_boot_switch")
|
||||
GrubBootSwitch = grub_boot_switch_ns.class_("GrubBootSwitch", cg.Component)
|
||||
|
||||
CONF_VOLUME_SERIAL = "volume_serial"
|
||||
CONF_VOLUME_LABEL = "volume_label"
|
||||
CONF_USB_VID = "usb_vid"
|
||||
CONF_USB_PID = "usb_pid"
|
||||
CONF_USB_MANUFACTURER = "usb_manufacturer"
|
||||
CONF_USB_PRODUCT = "usb_product"
|
||||
CONF_INITIAL_TARGET = "initial_target"
|
||||
CONF_TARGETS = "targets"
|
||||
CONF_GRUB_BOOT_SWITCH_ID = "grub_boot_switch_id"
|
||||
|
||||
|
||||
def _validate_label(value):
|
||||
value = cv.string_strict(value)
|
||||
if len(value) > 11:
|
||||
raise cv.Invalid("volume_label must be 11 characters or fewer")
|
||||
return value
|
||||
|
||||
|
||||
# targets: maps a digit (0-9) the firmware writes into switch_position to a
|
||||
# human-readable label. GRUB scripts react to the digit; the labels are only
|
||||
# used by the select sub-platform exposed to Home Assistant.
|
||||
TARGETS_SCHEMA = cv.Schema({cv.int_range(min=0, max=9): cv.string_strict})
|
||||
|
||||
|
||||
CONFIG_SCHEMA = cv.All(
|
||||
cv.Schema(
|
||||
{
|
||||
cv.GenerateID(): cv.declare_id(GrubBootSwitch),
|
||||
cv.Optional(CONF_VOLUME_SERIAL, default=0x55AA6922): cv.hex_uint32_t,
|
||||
cv.Optional(CONF_VOLUME_LABEL, default="SWITCH"): _validate_label,
|
||||
cv.Optional(CONF_USB_VID, default=0x26BA): cv.hex_uint16_t,
|
||||
cv.Optional(CONF_USB_PID, default=0x8003): cv.hex_uint16_t,
|
||||
cv.Optional(CONF_USB_MANUFACTURER, default="Stecman"): cv.string,
|
||||
cv.Optional(CONF_USB_PRODUCT, default="Boot Switch"): cv.string,
|
||||
cv.Optional(CONF_INITIAL_TARGET, default=0): cv.int_range(min=0, max=9),
|
||||
cv.Optional(CONF_TARGETS, default={}): TARGETS_SCHEMA,
|
||||
}
|
||||
).extend(cv.COMPONENT_SCHEMA),
|
||||
cv.only_with_esp_idf,
|
||||
cv.only_on_esp32,
|
||||
)
|
||||
|
||||
|
||||
async def to_code(config):
|
||||
var = cg.new_Pvariable(config[CONF_ID])
|
||||
await cg.register_component(var, config)
|
||||
|
||||
cg.add(var.set_volume_serial(config[CONF_VOLUME_SERIAL]))
|
||||
cg.add(var.set_volume_label(config[CONF_VOLUME_LABEL]))
|
||||
cg.add(var.set_usb_ids(config[CONF_USB_VID], config[CONF_USB_PID]))
|
||||
cg.add(var.set_usb_strings(config[CONF_USB_MANUFACTURER], config[CONF_USB_PRODUCT]))
|
||||
cg.add(var.set_initial_target(config[CONF_INITIAL_TARGET]))
|
||||
|
||||
for digit, label in sorted(config[CONF_TARGETS].items()):
|
||||
cg.add(var.add_target(digit, label))
|
||||
|
||||
# esp_tinyusb is the supported MSC stack on ESP32-S3 USB-OTG.
|
||||
esp32.add_idf_component(
|
||||
name="espressif/esp_tinyusb",
|
||||
ref="~1.4.0",
|
||||
)
|
||||
|
||||
# Move IDF console off USB-Serial-JTAG so TinyUSB can claim the peripheral.
|
||||
esp32.add_idf_sdkconfig_option("CONFIG_ESP_CONSOLE_UART_DEFAULT", True)
|
||||
esp32.add_idf_sdkconfig_option("CONFIG_ESP_CONSOLE_USB_SERIAL_JTAG", False)
|
||||
|
||||
# esp_tinyusb's tusb_msc_storage.c provides default tud_msc_* callbacks as
|
||||
# strong symbols. We provide our own callback-driven implementations, so
|
||||
# we tell the linker to allow multiple definitions and rely on link order
|
||||
# (main component first → our symbols win) to pick ours.
|
||||
esp32.add_idf_sdkconfig_option("CONFIG_TINYUSB_MSC_ENABLED", True)
|
||||
esp32.add_idf_sdkconfig_option("CONFIG_TINYUSB_CDC_ENABLED", False)
|
||||
|
||||
cg.add_build_flag("-DCFG_TUSB_MCU=OPT_MCU_ESP32S3")
|
||||
cg.add_build_flag("-Wl,--allow-multiple-definition")
|
||||
Reference in New Issue
Block a user