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:
80
components/grub_boot_switch/fat12.cpp
Normal file
80
components/grub_boot_switch/fat12.cpp
Normal file
@@ -0,0 +1,80 @@
|
||||
#include "fat12.h"
|
||||
|
||||
#include <string.h>
|
||||
|
||||
namespace esphome {
|
||||
namespace grub_boot_switch {
|
||||
|
||||
static inline uint8_t fat_checksum(const char *name) {
|
||||
uint8_t s = name[0];
|
||||
s = (s << 7) + (s >> 1) + name[1];
|
||||
s = (s << 7) + (s >> 1) + name[2];
|
||||
s = (s << 7) + (s >> 1) + name[3];
|
||||
s = (s << 7) + (s >> 1) + name[4];
|
||||
s = (s << 7) + (s >> 1) + name[5];
|
||||
s = (s << 7) + (s >> 1) + name[6];
|
||||
s = (s << 7) + (s >> 1) + name[7];
|
||||
s = (s << 7) + (s >> 1) + name[8];
|
||||
s = (s << 7) + (s >> 1) + name[9];
|
||||
s = (s << 7) + (s >> 1) + name[10];
|
||||
return s;
|
||||
}
|
||||
|
||||
static uint8_t write_utf16_chars(const char *name, uint8_t *index, uint8_t length, uint8_t write_count,
|
||||
uint8_t *output) {
|
||||
for (uint8_t i = write_count; i != 0; --i) {
|
||||
if (*index <= length) {
|
||||
*output++ = name[*index];
|
||||
*output++ = 0x00;
|
||||
++(*index);
|
||||
} else {
|
||||
*output++ = 0xFF;
|
||||
*output++ = 0xFF;
|
||||
}
|
||||
}
|
||||
return write_count * 2;
|
||||
}
|
||||
|
||||
uint8_t fat_write_lfn(const char *name, const FatDirEntry *direntry, uint8_t *output) {
|
||||
constexpr uint8_t kCharsPerEntry = 13;
|
||||
|
||||
const size_t length = strlen(name);
|
||||
const uint8_t num_entries = (length + (kCharsPerEntry - 1)) / kCharsPerEntry;
|
||||
const uint8_t checksum = fat_checksum(reinterpret_cast<const char *>(direntry));
|
||||
|
||||
for (uint8_t ordinal = num_entries; ordinal != 0; --ordinal) {
|
||||
uint8_t index = (ordinal - 1) * kCharsPerEntry;
|
||||
|
||||
*output = ordinal | (kFat_LastLFN * (ordinal == num_entries));
|
||||
++output;
|
||||
|
||||
output += write_utf16_chars(name, &index, length, 5, output);
|
||||
|
||||
output[0] = kFatAttr_LongFileName;
|
||||
output[1] = 0x00;
|
||||
output[2] = checksum;
|
||||
output += 3;
|
||||
|
||||
output += write_utf16_chars(name, &index, length, 6, output);
|
||||
|
||||
output[0] = 0x00;
|
||||
output[1] = 0x00;
|
||||
output += 2;
|
||||
|
||||
output += write_utf16_chars(name, &index, length, 2, output);
|
||||
}
|
||||
|
||||
return sizeof(FatDirEntry) * num_entries;
|
||||
}
|
||||
|
||||
uint8_t fat_write_dir(const FatDirEntry *direntry, uint8_t *output) {
|
||||
memcpy(output, direntry, sizeof(FatDirEntry));
|
||||
return sizeof(FatDirEntry);
|
||||
}
|
||||
|
||||
uint16_t fat_date(uint16_t year, uint8_t month, uint8_t day) {
|
||||
return ((year - 1980) << 9) | (month << 5) | day;
|
||||
}
|
||||
|
||||
} // namespace grub_boot_switch
|
||||
} // namespace esphome
|
||||
Reference in New Issue
Block a user