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.
60 lines
1.8 KiB
C++
60 lines
1.8 KiB
C++
// 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;
|
|
}
|