// 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 #include #include #include #include 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(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(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; }