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

52
test/Makefile Normal file
View File

@@ -0,0 +1,52 @@
CXX ?= g++
CXXFLAGS ?= -std=c++17 -O0 -g -Wall -Wextra -Wno-unused-parameter
INCLUDES = -I../components/grub_boot_switch
SRCS = test_disk.cpp \
../components/grub_boot_switch/fat12.cpp \
../components/grub_boot_switch/msc_disk.cpp
BIN = test_disk
IMG = disk.img
.PHONY: all build run verify clean
all: verify
build: $(BIN)
$(BIN): $(SRCS)
$(CXX) $(CXXFLAGS) $(INCLUDES) -o $@ $(SRCS)
# Generate a disk image (target=0 by default; pass T=N to override)
$(IMG): $(BIN)
./$(BIN) $(IMG) $(or $(T),0)
# Run the verifier suite. Requires mtools and `file`.
verify: $(IMG)
@echo "=== file(1) on disk image ==="
@file $(IMG)
@echo
@echo "=== mdir listing ==="
@MTOOLS_SKIP_CHECK=1 mdir -i $(IMG) ::
@echo
@echo "=== switch_position contents ==="
@MTOOLS_SKIP_CHECK=1 mtype -i $(IMG) ::switch_position
@echo
@echo "=== switch_position_grub.cfg contents ==="
@MTOOLS_SKIP_CHECK=1 mtype -i $(IMG) ::switch_position_grub.cfg
@echo
@echo "=== volume serial ==="
@MTOOLS_SKIP_CHECK=1 minfo -i $(IMG) :: | grep -i serial
# Cycle through several target values to prove the file content tracks state
sweep: $(BIN)
@for t in 0 1 2 3 9 ; do \
./$(BIN) sweep_$$t.img $$t >/dev/null ; \
echo "target=$$t:" ; \
MTOOLS_SKIP_CHECK=1 mtype -i sweep_$$t.img ::switch_position_grub.cfg ; \
done ; \
rm -f sweep_*.img
clean:
rm -f $(BIN) $(IMG) sweep_*.img

59
test/test_disk.cpp Normal file
View File

@@ -0,0 +1,59 @@
// Host-side test harness for the FAT12 emulator.
//
// Builds a 64 KB disk image by walking LBAs 0..127 through MscDisk::read_lba,
// then verifies it with mtools. We mimic the two virtual files the production
// component installs in install_virtual_files_().
//
// Build with: make -C test
#include "msc_disk.h"
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <string>
using esphome::grub_boot_switch::MscDisk;
int main(int argc, char **argv) {
const char *out_path = (argc > 1) ? argv[1] : "disk.img";
const uint8_t target = (argc > 2) ? static_cast<uint8_t>(atoi(argv[2])) : 0;
MscDisk disk;
disk.set_volume_serial(0x55AA6922);
disk.set_volume_label("SWITCH");
disk.set_file_date(2026, 5, 4);
static const char kPrefix[] = "set os_hw_switch=";
static const size_t kPrefixLen = sizeof(kPrefix) - 1;
static const size_t kFileLen = kPrefixLen + 2;
disk.add_file("switch_position", "SWITCH~1", " ", 1,
[target](uint8_t *out) { out[0] = '0' + (target % 10); });
disk.add_file("switch_position_grub.cfg", "SWITCH~1", "CFG", kFileLen,
[target](uint8_t *out) {
memcpy(out, kPrefix, kPrefixLen);
out[kPrefixLen] = '0' + (target % 10);
out[kPrefixLen + 1] = '\n';
});
std::ofstream f(out_path, std::ios::binary);
if (!f) {
fprintf(stderr, "could not open %s\n", out_path);
return 1;
}
uint8_t sector[512];
for (uint32_t lba = 0; lba < disk.sector_count(); ++lba) {
disk.read_lba(lba, sector);
f.write(reinterpret_cast<const char *>(sector), sizeof(sector));
}
f.close();
fprintf(stderr, "wrote %u sectors (%u bytes) to %s with target=%u\n",
disk.sector_count(), disk.sector_count() * disk.sector_size(),
out_path, target);
return 0;
}