Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/aienux/blob/main/native/kernel/dev/fat_boot.h # aienux Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `aienux / native / kernel / dev / fat_boot.h` 109 lines ยท 5758 bytes Raw Blame History 1 /* fat_boot.h -- the AIENOS boot section: a FAT32 volume (Microsoft FAT32 2 * File System Specification 1.03; UEFI 2.10 section 13.3: FAT32 for a system 3 * partition) on an EFI System Partition that AIENOS added itself, holding 4 * 5 * \EFI\BOOT\BOOTX64.EFI the kernel image, started by the firmware 6 * \EFI\BOOT\BOOTX64.MAN its signed slot manifest (boot/slot_format.h) 7 * 8 * so a machine starts AIENOS from its own disk with no stick and no network. 9 * 10 * Layout, all fixed by the partition size (512-byte sectors only): 11 * sector 0 boot sector, 1 FSInfo, 6 and 7 their backups; the reserved 12 * region is padded so the data region starts on a 4 KiB boundary 13 * (UEFI: "aligned to the physical block boundary"); two FATs; one sector 14 * per cluster. Cluster 2 is the root (volume label AIENOS, directory EFI), 15 * 3 is \EFI, 4 is \EFI\BOOT. The rest holds two image slots, A and B, each 16 * starting on a 128-cluster boundary so their FAT sectors never overlap: 17 * the manifest at the slot's first cluster, the image 128 clusters later, 18 * each one contiguous chain. 19 * 20 * Installing a new image never touches the files the firmware starts now: 21 * the new manifest and image go into the other slot (its FAT sectors, then 22 * its data), flush; then the one sector of \EFI\BOOT is rewritten to name 23 * the new slot, flush; then the old slot's FAT sectors are freed, flush; 24 * then everything is read back. A power cut leaves the old kernel or the new 25 * one, never a mix (at worst the old slot stays allocated with no name, 26 * "lost clusters", freed by the next install). 27 * 28 * A volume is "ours" only when its boot sector is byte for byte the one 29 * ck_fatb_format writes for that size and volume serial; anything else is 30 * formatted only if the caller says so (the partition itself must already 31 * be AIENOS's, gpt_add.h ck_gpt_is_ours). Freestanding: no libc, no heap, 32 * I/O through disk_read/disk_write/disk_flush on the partition view only. 33 * Host tested in dev/tests/fat_boot_test.c (with fsck.fat and mtools as 34 * outside readers). */ 35 #ifndef AIENOS_CK_FAT_BOOT_H 36 #define AIENOS_CK_FAT_BOOT_H 37 #include 38 #include "disk.h" 39 40 /* EFI System Partition type C12A7328-F81F-11D2-BA4B-00A0C93EC93B (UEFI 2.10 41 * table 5-7), on-disk byte order. */ 42 extern const uint8_t ck_esp_part_type[16]; 43 44 #define CK_FATB_MAN_BYTES 4096u /* a slot manifest */ 45 #define CK_FATB_MIN_MIB 64u /* smallest partition handled */ 46 47 enum { 48 CK_FATB_OK = 0, 49 CK_FATB_EGEOM = -440, /* not 512-byte sectors, or too small for FAT32 with two slots */ 50 CK_FATB_EIO = -441, /* a read, write or flush failed */ 51 CK_FATB_ENOTOURS = -442, /* the volume is not one ck_fatb_format wrote */ 52 CK_FATB_EBIG = -443, /* the image is larger than a slot */ 53 CK_FATB_EVERIFY = -444, /* read back, the files are not what was written */ 54 }; 55 56 typedef struct { 57 int ours; /* the boot sector is ck_fatb_format's */ 58 int slot; /* 0 or 1: the slot \EFI\BOOT names; -1: none (or not valid) */ 59 uint32_t image_len; /* BOOTX64.EFI's size when slot >= 0 */ 60 uint32_t slot_max; /* the largest image a slot holds, in bytes */ 61 /* when not ours: the first byte that differs from what ck_fatb_format 62 * wrote (where = "boot sector", "FAT 1", "FAT 2", "root", "EFI" or 63 * "BOOT"; 0 when the geometry or a read failed) */ 64 const char *where; 65 uint32_t at; 66 uint8_t got, want; 67 } ck_fatb_state; 68 69 /* Reads the volume: is it ours, and which slot holds a complete, valid pair 70 * of files (directory entries, contiguous FAT chains ending in the 71 * end-of-chain mark). Reads only. Fields that FAT drivers change in normal use 72 * are not compared: the boot sector's BS_Reserved1 (the NT dirty flag), 73 * FAT[1]'s ClnShutBitMask and HrdErrBitMask, each directory entry's 74 * DIR_LstAccDate, and ATTR_ARCHIVE, DIR_WrtTime and DIR_WrtDate on the 75 * expected directory entries (Apple's firmware sets them on the EFI directory 76 * when it starts the kernel). 77 * CK_FATB_OK (also for "not ours") or CK_FATB_EGEOM / CK_FATB_EIO. */ 78 int ck_fatb_state_read(const disk_dev *p, uint32_t serial, ck_fatb_state *st); 79 80 /* Copies BOOTX64.MAN of the slot \EFI\BOOT names (CK_FATB_MAN_BYTES bytes) into 81 * man. Reads only. CK_FATB_OK; CK_FATB_ENOTOURS when the volume is not ours or 82 * holds no complete slot; or CK_FATB_EGEOM / CK_FATB_EIO. */ 83 int ck_fatb_manifest_read(const disk_dev *p, uint32_t serial, uint8_t man[CK_FATB_MAN_BYTES]); 84 85 /* Writes an empty AIENOS boot volume over the whole partition view p: both 86 * FATs, the three directories, then (last, after a flush) the FSInfo and 87 * boot sectors and their backups. */ 88 int ck_fatb_format(const disk_dev *p, uint32_t serial); 89 90 typedef struct { 91 int formatted; /* 1: the volume was not ours and was formatted first */ 92 int written; /* 1: a new image was installed; 0: the same one was already there */ 93 int slot; /* the slot the firmware now starts */ 94 uint32_t sectors_written; 95 } ck_fatb_result; 96 97 /* Makes BOOTX64.EFI = img and BOOTX64.MAN = man (CK_FATB_MAN_BYTES bytes), 98 * formatting first when the volume is not ours and format_ok is set. When 99 * the current slot already holds exactly these bytes nothing is written. 100 * `dos_date`/`dos_time`: the FAT time stamps for the new entries. */ 101 int ck_fatb_install(const disk_dev *p, uint32_t serial, int format_ok, const uint8_t *man, const uint8_t *img, 102 uint32_t img_len, uint16_t dos_date, uint16_t dos_time, ck_fatb_result *out); 103 104 /* FAT date and time (spec "Date and Time Formats") of a Unix time, UTC; 105 * times before 1980 give 1980-01-01 00:00. */ 106 void ck_fatb_dos_time(uint64_t unix_s, uint16_t *date, uint16_t *time); 107 108 const char *ck_fatb_strerror(int rc); 109 #endif