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:
2026-05-04 18:25:41 +00:00
commit a911b9e4cb
19 changed files with 1497 additions and 0 deletions

View File

@@ -0,0 +1,51 @@
"""Select sub-platform for grub_boot_switch.
Exposes the parent's targets table as a Home Assistant select entity. The
options list is derived at code-gen time from the parent's `targets:` map, so
adding/removing entries there automatically updates the dropdown.
"""
import esphome.codegen as cg
import esphome.config_validation as cv
from esphome.components import select
from esphome.const import CONF_ID
from .. import (
CONF_GRUB_BOOT_SWITCH_ID,
CONF_TARGETS,
GrubBootSwitch,
grub_boot_switch_ns,
)
DEPENDENCIES = ["grub_boot_switch"]
BootTargetSelect = grub_boot_switch_ns.class_(
"BootTargetSelect", select.Select, cg.Component
)
CONFIG_SCHEMA = select.select_schema(BootTargetSelect).extend(
{
cv.GenerateID(CONF_GRUB_BOOT_SWITCH_ID): cv.use_id(GrubBootSwitch),
}
)
async def to_code(config):
parent = await cg.get_variable(config[CONF_GRUB_BOOT_SWITCH_ID])
# Reach back into the *full* validated config so we can pull the parent's
# targets map. CORE.config holds the post-validation tree.
from esphome.core import CORE
parent_cfg = CORE.config.get("grub_boot_switch", {})
targets = parent_cfg.get(CONF_TARGETS, {})
if not targets:
raise cv.Invalid(
"select platform requires the grub_boot_switch component to define `targets:`"
)
options = [label for _digit, label in sorted(targets.items())]
var = await select.new_select(config, options=options)
await cg.register_component(var, config)
cg.add(var.set_parent(parent))

View File

@@ -0,0 +1,44 @@
#include "boot_target_select.h"
#include "esphome/core/log.h"
namespace esphome {
namespace grub_boot_switch {
static const char *const TAG = "grub_boot_switch.select";
void BootTargetSelect::setup() {
// Publish the current state from the parent so HA reflects whatever was
// restored from NVS / set_initial_target.
if (parent_ != nullptr) {
auto label = parent_->label_for_digit(parent_->get_target());
if (!label.empty())
this->publish_state(label);
// Mirror future external set_target() calls (REST/automation/MQTT) into
// the select's reported state.
parent_->add_on_target_change([this](uint8_t digit) {
auto l = this->parent_->label_for_digit(digit);
if (!l.empty())
this->publish_state(l);
});
}
}
void BootTargetSelect::control(const std::string &value) {
if (parent_ == nullptr)
return;
uint8_t digit = 0;
if (!parent_->digit_for_label(value, &digit)) {
ESP_LOGW(TAG, "control: unknown option '%s'", value.c_str());
return;
}
parent_->set_target(digit);
// The parent will fire add_on_target_change → publish_state, but if the
// target was already at this digit no callback fires; publish anyway so
// HA's optimistic state stays in sync.
this->publish_state(value);
}
} // namespace grub_boot_switch
} // namespace esphome

View File

@@ -0,0 +1,24 @@
#pragma once
#include "esphome/core/component.h"
#include "esphome/components/select/select.h"
#include "../grub_boot_switch.h"
namespace esphome {
namespace grub_boot_switch {
class BootTargetSelect : public select::Select, public Component {
public:
void set_parent(GrubBootSwitch *parent) { parent_ = parent; }
void setup() override;
protected:
void control(const std::string &value) override;
GrubBootSwitch *parent_{nullptr};
};
} // namespace grub_boot_switch
} // namespace esphome