Files
esp32-grub-switch/components/grub_boot_switch/fat12.h
adamksmith a911b9e4cb 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.
2026-05-04 18:25:41 +00:00

53 lines
1.2 KiB
C++

#pragma once
#include <stdint.h>
#include <stddef.h>
namespace esphome {
namespace grub_boot_switch {
#define WBVAL(x) ((x) & 0xFF), (((x) >> 8) & 0xFF)
#define QBVAL(x) ((x) & 0xFF), (((x) >> 8) & 0xFF), (((x) >> 16) & 0xFF), (((x) >> 24) & 0xFF)
constexpr uint8_t FAT_MEDIA_FIXED_DISK = 0xF8;
constexpr uint8_t FAT_NAME_LEN = 8;
constexpr uint8_t FAT_EXT_LEN = 3;
enum FatFileAttributes : uint8_t {
kFatAttr_ReadOnly = 0x01,
kFatAttr_Hidden = 0x02,
kFatAttr_System = 0x04,
kFatAttr_VolumeLabel = 0x08,
kFatAttr_Subdirectory = 0x10,
kFatAttr_Archive = 0x20,
kFatAttr_LongFileName = 0x0F,
};
enum FatOrdinalFlags : uint8_t {
kFat_LastLFN = 0x40,
kFat_DeletedLFN = 0x80,
};
struct FatDirEntry {
char name[FAT_NAME_LEN];
char ext[FAT_EXT_LEN];
uint8_t attrs;
uint8_t type;
uint8_t ctime_ms;
uint16_t ctime;
uint16_t cdate;
uint16_t adate;
uint16_t ea_index;
uint16_t mtime;
uint16_t mdate;
uint16_t start;
uint32_t size;
} __attribute__((packed));
uint8_t fat_write_lfn(const char *name, const FatDirEntry *direntry, uint8_t *output);
uint8_t fat_write_dir(const FatDirEntry *direntry, uint8_t *output);
uint16_t fat_date(uint16_t year, uint8_t month, uint8_t day);
} // namespace grub_boot_switch
} // namespace esphome