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.c # 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.c` 505 lines ยท 18745 bytes Raw Blame History 1 /* fat_boot.c -- the AIENOS boot section (FAT32); see fat_boot.h. Field names 2 * and offsets are those of the Microsoft FAT32 File System Specification 3 * 1.03 ("Boot Sector and BPB Structure", "FAT32 FSInfo Sector Structure", 4 * "FAT 32 Byte Directory Entry Structure"). */ 5 #include "fat_boot.h" 6 7 const uint8_t ck_esp_part_type[16] = {0x28, 0x73, 0x2a, 0xc1, 0x1f, 0xf8, 0xd2, 0x11, 8 0xba, 0x4b, 0x00, 0xa0, 0xc9, 0x3e, 0xc9, 0x3b}; 9 10 #define SEC 512u 11 #define CHUNK 128u /* sectors per buffered transfer */ 12 #define PER_FAT_SEC (SEC / 4u) /* FAT32 entries in one FAT sector */ 13 #define EOC 0x0FFFFFFFu 14 #define MAN_CLUS (CK_FATB_MAN_BYTES / SEC) 15 #define ROOT 2u 16 #define EFI_DIR 3u 17 #define BOOT_DIR 4u 18 #define FMT_DATE 0x0021u /* 1980-01-01, the directories' stamp */ 19 20 static uint8_t g_buf[CHUNK * SEC]; 21 static uint8_t g_sec[SEC], g_want[SEC]; 22 23 typedef struct { 24 uint32_t total, rsv, fatsz, data0, clusters, slotc; 25 } geo; 26 27 static void put16(uint8_t *p, uint32_t v) 28 { 29 p[0] = (uint8_t)v; 30 p[1] = (uint8_t)(v >> 8); 31 } 32 static void put32(uint8_t *p, uint32_t v) 33 { 34 put16(p, v); 35 put16(p + 2, v >> 16); 36 } 37 static uint32_t get16(const uint8_t *p) { return (uint32_t)p[0] | (uint32_t)p[1] << 8; } 38 static uint32_t get32(const uint8_t *p) { return get16(p) | get16(p + 2) << 16; } 39 static void fill(uint8_t *p, uint8_t v, uint32_t n) 40 { 41 for (uint32_t i = 0; i < n; i++) 42 p[i] = v; 43 } 44 static void copy(uint8_t *d, const uint8_t *s, uint32_t n) 45 { 46 for (uint32_t i = 0; i < n; i++) 47 d[i] = s[i]; 48 } 49 static int same(const uint8_t *a, const uint8_t *b, uint32_t n) 50 { 51 uint8_t x = 0; 52 for (uint32_t i = 0; i < n; i++) 53 x |= a[i] ^ b[i]; 54 return x == 0; 55 } 56 57 /* The spec's FAT32 sizing ("FAT Type Determination", BPB_FATSz32 formula 58 * with TmpVal2 = (256 * BPB_SecPerClus + BPB_NumFATs) / 2), one sector per 59 * cluster as its table gives up to 260 MB and fine above; the reserved 60 * region padded to a 4 KiB boundary for the data region. */ 61 static int geometry(const disk_dev *p, geo *g) 62 { 63 if (p->block_size != SEC || p->block_count > 0xffffffffull || 64 p->block_count < (uint64_t)CK_FATB_MIN_MIB * (1024u * 1024u / SEC)) 65 return CK_FATB_EGEOM; 66 g->total = (uint32_t)p->block_count; 67 uint32_t t1 = g->total - 32u, t2 = (256u * 1u + 2u) / 2u; 68 g->fatsz = (t1 + t2 - 1u) / t2; 69 g->rsv = 32u + (8u - (32u + 2u * g->fatsz) % 8u) % 8u; 70 g->data0 = g->rsv + 2u * g->fatsz; 71 g->clusters = g->total - g->data0; 72 if (g->clusters < 65525u || (uint64_t)g->fatsz * PER_FAT_SEC < (uint64_t)g->clusters + 2u) 73 return CK_FATB_EGEOM; 74 g->slotc = ((g->clusters + 2u - 128u) / 2u) & ~127u; 75 return g->slotc >= 256u ? CK_FATB_OK : CK_FATB_EGEOM; 76 } 77 78 static uint32_t slot_base(const geo *g, int k) { return 128u + (uint32_t)k * g->slotc; } 79 static uint32_t clus_sec(const geo *g, uint32_t c) { return g->data0 + (c - 2u); } 80 static uint32_t slot_max(const geo *g) { return (g->slotc - 128u) * SEC; } 81 82 static int rd(const disk_dev *p, uint32_t lba, uint32_t n, uint8_t *b) 83 { 84 return disk_read(p, lba, n, b) == DISK_OK ? CK_FATB_OK : CK_FATB_EIO; 85 } 86 static int wr(const disk_dev *p, uint32_t lba, uint32_t n, const uint8_t *b, uint32_t *count) 87 { 88 if (disk_write(p, lba, n, b) != DISK_OK) 89 return CK_FATB_EIO; 90 if (count) 91 *count += n; 92 return CK_FATB_OK; 93 } 94 static int flush(const disk_dev *p) { return disk_flush(p) == DISK_OK ? CK_FATB_OK : CK_FATB_EIO; } 95 96 /* ---- the fixed sectors ---------------------------------------------------- */ 97 static void boot_sector(const geo *g, uint32_t serial, uint8_t *b) 98 { 99 fill(b, 0, SEC); 100 b[0] = 0xeb, b[1] = 0x58, b[2] = 0x90; 101 copy(b + 3, (const uint8_t *)"AIENOS ", 8); 102 put16(b + 11, SEC); 103 b[13] = 1; /* BPB_SecPerClus */ 104 put16(b + 14, g->rsv); /* BPB_RsvdSecCnt */ 105 b[16] = 2; /* BPB_NumFATs; RootEntCnt, TotSec16, FATSz16 stay 0 */ 106 b[21] = 0xf8; /* BPB_Media: fixed */ 107 put16(b + 24, 63); 108 put16(b + 26, 255); 109 put32(b + 32, g->total); 110 put32(b + 36, g->fatsz); 111 put32(b + 44, ROOT); /* BPB_RootClus; ExtFlags 0 = mirrored, FSVer 0:0 */ 112 put16(b + 48, 1); /* BPB_FSInfo */ 113 put16(b + 50, 6); /* BPB_BkBootSec */ 114 b[64] = 0x80; 115 b[66] = 0x29; 116 put32(b + 67, serial); 117 copy(b + 71, (const uint8_t *)"AIENOS ", 11); 118 copy(b + 82, (const uint8_t *)"FAT32 ", 8); 119 b[90] = 0xcd, b[91] = 0x18; /* not a BIOS boot disk: int 18h */ 120 b[510] = 0x55, b[511] = 0xaa; 121 } 122 123 static void fsinfo(uint8_t *b) 124 { 125 fill(b, 0, SEC); 126 put32(b, 0x41615252u); 127 put32(b + 484, 0x61417272u); 128 put32(b + 488, 0xffffffffu); /* free count unknown */ 129 put32(b + 492, 0xffffffffu); /* no hint */ 130 put32(b + 508, 0xaa550000u); 131 } 132 133 static void dirent(uint8_t *e, const char *name11, uint8_t attr, uint32_t clus, uint32_t size, uint16_t date, 134 uint16_t time) 135 { 136 fill(e, 0, 32); 137 copy(e, (const uint8_t *)name11, 11); 138 e[11] = attr; 139 put16(e + 14, time); 140 put16(e + 16, date); 141 put16(e + 18, date); 142 put16(e + 20, clus >> 16); 143 put16(e + 22, time); 144 put16(e + 24, date); 145 put16(e + 26, clus & 0xffffu); 146 put32(e + 28, size); 147 } 148 149 /* Directory sector for cluster 2, 3 or 4 as ck_fatb_format writes it. */ 150 static void dir_sector(uint32_t c, uint8_t *b) 151 { 152 fill(b, 0, SEC); 153 if (c == ROOT) { 154 dirent(b, "AIENOS ", 0x08, 0, 0, FMT_DATE, 0); 155 dirent(b + 32, "EFI ", 0x10, EFI_DIR, 0, FMT_DATE, 0); 156 } else if (c == EFI_DIR) { 157 dirent(b, ". ", 0x10, EFI_DIR, 0, FMT_DATE, 0); 158 dirent(b + 32, ".. ", 0x10, 0, 0, FMT_DATE, 0); /* parent is the root: 0 */ 159 dirent(b + 64, "BOOT ", 0x10, BOOT_DIR, 0, FMT_DATE, 0); 160 } else { 161 dirent(b, ". ", 0x10, BOOT_DIR, 0, FMT_DATE, 0); 162 dirent(b + 32, ".. ", 0x10, EFI_DIR, 0, FMT_DATE, 0); 163 } 164 } 165 166 /* FAT sector 0: FAT[0] media, FAT[1] end of chain (clean, no error), and 167 * the three one-cluster directories. */ 168 static void fat_sector0(uint8_t *b) 169 { 170 fill(b, 0, SEC); 171 put32(b, 0x0FFFFFF8u); 172 for (uint32_t c = 1; c <= BOOT_DIR; c++) 173 put32(b + 4 * c, EOC); 174 } 175 176 /* ---- format --------------------------------------------------------------- */ 177 int ck_fatb_format(const disk_dev *p, uint32_t serial) 178 { 179 geo g; 180 int rc = geometry(p, &g); 181 if (rc) 182 return rc; 183 /* the old boot sector goes first, so a cut format is never taken for ours */ 184 fill(g_sec, 0, SEC); 185 if ((rc = wr(p, 0, 1, g_sec, 0)) || (rc = flush(p))) 186 return rc; 187 fill(g_buf, 0, sizeof g_buf); 188 for (uint32_t s = g.rsv; s < g.data0; s += CHUNK) { 189 uint32_t n = g.data0 - s < CHUNK ? g.data0 - s : CHUNK; 190 if ((rc = wr(p, s, n, g_buf, 0))) 191 return rc; 192 } 193 fat_sector0(g_sec); 194 if ((rc = wr(p, g.rsv, 1, g_sec, 0)) || (rc = wr(p, g.rsv + g.fatsz, 1, g_sec, 0))) 195 return rc; 196 for (uint32_t c = ROOT; c <= BOOT_DIR; c++) { 197 dir_sector(c, g_sec); 198 if ((rc = wr(p, clus_sec(&g, c), 1, g_sec, 0))) 199 return rc; 200 } 201 if ((rc = flush(p))) 202 return rc; 203 for (uint32_t s = 1; s < g.rsv; s++) { 204 if (s == 1 || s == 7) 205 fsinfo(g_sec); 206 else if (s == 6) 207 boot_sector(&g, serial, g_sec); 208 else 209 fill(g_sec, 0, SEC); 210 if ((rc = wr(p, s, 1, g_sec, 0))) 211 return rc; 212 } 213 if ((rc = flush(p))) 214 return rc; 215 boot_sector(&g, serial, g_sec); 216 if ((rc = wr(p, 0, 1, g_sec, 0))) 217 return rc; 218 return flush(p); 219 } 220 221 /* ---- reading -------------------------------------------------------------- */ 222 /* 1 when FAT `fat` (0 or 1) holds the chain first, first+1, .., first+n-1, 223 * end of chain; 0 when not; or CK_FATB_EIO. */ 224 static int chain_ok(const disk_dev *p, const geo *g, int fat, uint32_t first, uint32_t n) 225 { 226 uint32_t loaded = 0xffffffffu, base = g->rsv + (uint32_t)fat * g->fatsz; 227 for (uint32_t c = first; c < first + n; c++) { 228 uint32_t s = c / PER_FAT_SEC; 229 if (s != loaded) { 230 if (rd(p, base + s, 1, g_sec)) 231 return CK_FATB_EIO; 232 loaded = s; 233 } 234 uint32_t v = get32(g_sec + 4u * (c % PER_FAT_SEC)) & 0x0FFFFFFFu; 235 if (c + 1u < first + n ? v != c + 1u : v < 0x0FFFFFF8u) 236 return 0; 237 } 238 return 1; 239 } 240 241 static int find(const uint8_t *dir, const char *name11, uint32_t *clus, uint32_t *size) 242 { 243 for (uint32_t i = 2; i < SEC / 32u; i++) { 244 const uint8_t *e = dir + 32u * i; 245 if (e[0] == 0) 246 break; 247 if (same(e, (const uint8_t *)name11, 11) && !(e[11] & 0x18)) { 248 *clus = get16(e + 20) << 16 | get16(e + 26); 249 *size = get32(e + 28); 250 return 1; 251 } 252 } 253 return 0; 254 } 255 256 /* Compares n bytes read (got) with what ck_fatb_format wrote (want), after 257 * taking over from want the fields FAT drivers change in normal use (see 258 * fat_boot.h). Records the first difference in st; 1 when none. */ 259 enum { BOOT_SEC, FAT_SEC, DIR_SEC }; 260 static int as_written(uint8_t *got, const uint8_t *want, uint32_t n, int kind, const char *where, ck_fatb_state *st) 261 { 262 if (kind == BOOT_SEC) { 263 got[65] = want[65]; /* BS_Reserved1 */ 264 } else if (kind == FAT_SEC) { 265 got[7] = (uint8_t)((got[7] & ~0x0cu) | (want[7] & 0x0cu)); /* FAT[1] bits 27, 26 */ 266 } else { 267 for (uint32_t e = 0; e + 32u <= n; e += 32u) { 268 got[e + 18] = want[e + 18], got[e + 19] = want[e + 19]; /* DIR_LstAccDate */ 269 /* Firmware may mark an existing directory for backup and stamp 270 * it as written (Apple's does both to \EFI when it starts the 271 * kernel). Only normalize ARCHIVE, DIR_WrtTime and DIR_WrtDate 272 * on expected directories, never their type, volume labels, or 273 * unused entries (FAT32 spec DIR_Attr). */ 274 if (want[e] && (want[e + 11] & 0x10)) { 275 got[e + 11] = (uint8_t)((got[e + 11] & ~0x20u) | (want[e + 11] & 0x20u)); 276 copy(got + e + 22, want + e + 22, 4); 277 } 278 } 279 } 280 for (uint32_t i = 0; i < n; i++) 281 if (got[i] != want[i]) { 282 st->where = where, st->at = i, st->got = got[i], st->want = want[i]; 283 return 0; 284 } 285 return 1; 286 } 287 288 int ck_fatb_state_read(const disk_dev *p, uint32_t serial, ck_fatb_state *st) 289 { 290 static const char *const dir_name[] = {"root", "EFI", "BOOT"}; 291 geo g; 292 int rc = geometry(p, &g); 293 st->ours = 0; 294 st->slot = -1; 295 st->image_len = 0; 296 st->slot_max = 0; 297 st->where = 0; 298 if (rc) 299 return rc; 300 st->slot_max = slot_max(&g); 301 boot_sector(&g, serial, g_want); 302 if (rd(p, 0, 1, g_sec)) 303 return CK_FATB_EIO; 304 if (!as_written(g_sec, g_want, SEC, BOOT_SEC, "boot sector", st)) 305 return CK_FATB_OK; 306 fat_sector0(g_want); 307 for (int f = 0; f < 2; f++) { 308 if (rd(p, g.rsv + (uint32_t)f * g.fatsz, 1, g_sec)) 309 return CK_FATB_EIO; 310 if (!as_written(g_sec, g_want, 4u * (BOOT_DIR + 1u), FAT_SEC, f ? "FAT 2" : "FAT 1", st)) 311 return CK_FATB_OK; 312 } 313 for (uint32_t c = ROOT; c <= BOOT_DIR; c++) { 314 dir_sector(c, g_want); 315 if (rd(p, clus_sec(&g, c), 1, g_sec)) 316 return CK_FATB_EIO; 317 if (!as_written(g_sec, g_want, c == BOOT_DIR ? 64u : SEC, DIR_SEC, dir_name[c - ROOT], st)) 318 return CK_FATB_OK; 319 } 320 st->ours = 1; 321 uint32_t ic, is, mc, ms; 322 if (!find(g_sec, "BOOTX64 EFI", &ic, &is) || !find(g_sec, "BOOTX64 MAN", &mc, &ms)) 323 return CK_FATB_OK; 324 for (int k = 0; k < 2; k++) { 325 uint32_t base = slot_base(&g, k); 326 if (mc != base || ms != CK_FATB_MAN_BYTES || ic != base + 128u || is == 0 || is > st->slot_max) 327 continue; 328 for (int f = 0; f < 2; f++) { 329 int a = chain_ok(p, &g, f, mc, MAN_CLUS), b = a == 1 ? chain_ok(p, &g, f, ic, (is + SEC - 1u) / SEC) : a; 330 if (a < 0 || b < 0) 331 return CK_FATB_EIO; 332 if (!a || !b) 333 return CK_FATB_OK; 334 } 335 st->slot = k; 336 st->image_len = is; 337 } 338 return CK_FATB_OK; 339 } 340 341 /* 1 when the n bytes at sector lba equal want; 0 when not; or CK_FATB_EIO. */ 342 static int same_on_disk(const disk_dev *p, uint32_t lba, const uint8_t *want, uint32_t n) 343 { 344 for (uint32_t done = 0; done < n;) { 345 uint32_t secs = (n - done + SEC - 1u) / SEC; 346 if (secs > CHUNK) 347 secs = CHUNK; 348 if (rd(p, lba + done / SEC, secs, g_buf)) 349 return CK_FATB_EIO; 350 uint32_t m = secs * SEC < n - done ? secs * SEC : n - done; 351 if (!same(g_buf, want + done, m)) 352 return 0; 353 for (uint32_t i = m; i < secs * SEC; i++) /* the tail of the last sector: zero */ 354 if (g_buf[i]) 355 return 0; 356 done += m; 357 } 358 return 1; 359 } 360 361 static int slot_holds(const disk_dev *p, const geo *g, int k, const uint8_t *man, const uint8_t *img, uint32_t len) 362 { 363 uint32_t base = slot_base(g, k); 364 int a = same_on_disk(p, clus_sec(g, base), man, CK_FATB_MAN_BYTES); 365 return a == 1 ? same_on_disk(p, clus_sec(g, base + 128u), img, len) : a; 366 } 367 368 int ck_fatb_manifest_read(const disk_dev *p, uint32_t serial, uint8_t man[CK_FATB_MAN_BYTES]) 369 { 370 geo g; 371 ck_fatb_state st; 372 int rc = geometry(p, &g); 373 if (rc || (rc = ck_fatb_state_read(p, serial, &st))) 374 return rc; 375 if (!st.ours || st.slot < 0) 376 return CK_FATB_ENOTOURS; 377 return rd(p, clus_sec(&g, slot_base(&g, st.slot)), CK_FATB_MAN_BYTES / SEC, man); 378 } 379 380 /* Writes slot k's FAT sectors in both FATs: the manifest and image chains 381 * when len > 0, all free when len == 0. */ 382 static int slot_fat(const disk_dev *p, const geo *g, int k, uint32_t len, uint32_t *count) 383 { 384 uint32_t base = slot_base(g, k), img0 = base + 128u, n = (len + SEC - 1u) / SEC; 385 uint32_t s0 = base / PER_FAT_SEC, s1 = (base + g->slotc) / PER_FAT_SEC; 386 for (uint32_t s = s0; s < s1; s += CHUNK) { 387 uint32_t secs = s1 - s < CHUNK ? s1 - s : CHUNK; 388 for (uint32_t i = 0; i < secs * PER_FAT_SEC; i++) { 389 uint32_t c = s * PER_FAT_SEC + i, v = 0; 390 if (len && c >= base && c < base + MAN_CLUS) 391 v = c + 1u < base + MAN_CLUS ? c + 1u : EOC; 392 else if (len && c >= img0 && c < img0 + n) 393 v = c + 1u < img0 + n ? c + 1u : EOC; 394 put32(g_buf + 4u * i, v); 395 } 396 int rc; 397 if ((rc = wr(p, g->rsv + s, secs, g_buf, count)) || (rc = wr(p, g->rsv + g->fatsz + s, secs, g_buf, count))) 398 return rc; 399 } 400 return CK_FATB_OK; 401 } 402 403 static int write_bytes(const disk_dev *p, uint32_t lba, const uint8_t *b, uint32_t n, uint32_t *count) 404 { 405 int rc; 406 if (n / SEC && (rc = wr(p, lba, n / SEC, b, count))) 407 return rc; 408 if (n % SEC) { 409 fill(g_sec, 0, SEC); 410 copy(g_sec, b + n / SEC * SEC, n % SEC); 411 if ((rc = wr(p, lba + n / SEC, 1, g_sec, count))) 412 return rc; 413 } 414 return CK_FATB_OK; 415 } 416 417 int ck_fatb_install(const disk_dev *p, uint32_t serial, int format_ok, const uint8_t *man, const uint8_t *img, 418 uint32_t img_len, uint16_t dos_date, uint16_t dos_time, ck_fatb_result *out) 419 { 420 geo g; 421 ck_fatb_state st; 422 out->formatted = out->written = 0; 423 out->slot = -1; 424 out->sectors_written = 0; 425 int rc = geometry(p, &g); 426 if (rc) 427 return rc; 428 if (!man || !img || img_len == 0 || img_len > slot_max(&g)) 429 return CK_FATB_EBIG; 430 if ((rc = ck_fatb_state_read(p, serial, &st))) 431 return rc; 432 if (!st.ours) { 433 if (!format_ok) 434 return CK_FATB_ENOTOURS; 435 if ((rc = ck_fatb_format(p, serial)) || (rc = ck_fatb_state_read(p, serial, &st))) 436 return rc; 437 if (!st.ours) 438 return CK_FATB_EVERIFY; 439 out->formatted = 1; 440 out->sectors_written = g.data0 + 4u; 441 } 442 if (st.slot >= 0 && st.image_len == img_len) { 443 int h = slot_holds(p, &g, st.slot, man, img, img_len); 444 if (h < 0) 445 return h; 446 if (h) { 447 out->slot = st.slot; 448 return CK_FATB_OK; 449 } 450 } 451 int k = st.slot == 0 ? 1 : 0; 452 uint32_t base = slot_base(&g, k); 453 if ((rc = slot_fat(p, &g, k, img_len, &out->sectors_written)) || 454 (rc = write_bytes(p, clus_sec(&g, base), man, CK_FATB_MAN_BYTES, &out->sectors_written)) || 455 (rc = write_bytes(p, clus_sec(&g, base + 128u), img, img_len, &out->sectors_written)) || (rc = flush(p))) 456 return rc; 457 dir_sector(BOOT_DIR, g_sec); 458 dirent(g_sec + 64, "BOOTX64 EFI", 0x20, base + 128u, img_len, dos_date, dos_time); 459 dirent(g_sec + 96, "BOOTX64 MAN", 0x20, base, CK_FATB_MAN_BYTES, dos_date, dos_time); 460 if ((rc = wr(p, clus_sec(&g, BOOT_DIR), 1, g_sec, &out->sectors_written)) || (rc = flush(p))) 461 return rc; 462 if (st.slot >= 0 && ((rc = slot_fat(p, &g, st.slot, 0, &out->sectors_written)) || (rc = flush(p)))) 463 return rc; 464 if ((rc = ck_fatb_state_read(p, serial, &st))) 465 return rc; 466 int h = st.slot == k && st.image_len == img_len ? slot_holds(p, &g, k, man, img, img_len) : 0; 467 if (h < 0) 468 return h; 469 if (!h) 470 return CK_FATB_EVERIFY; 471 out->written = 1; 472 out->slot = k; 473 return CK_FATB_OK; 474 } 475 476 void ck_fatb_dos_time(uint64_t unix_s, uint16_t *date, uint16_t *time) 477 { 478 *date = FMT_DATE; 479 *time = 0; 480 if (unix_s < 315532800ull) /* 1980-01-01 */ 481 return; 482 uint64_t days = unix_s / 86400u, rem = unix_s % 86400u; 483 /* civil from days (proleptic Gregorian), era-based */ 484 uint64_t z = days + 719468u, era = z / 146097u, doe = z - era * 146097u; 485 uint64_t yoe = (doe - doe / 1460u + doe / 36524u - doe / 146096u) / 365u; 486 uint64_t doy = doe - (365u * yoe + yoe / 4u - yoe / 100u), mp = (5u * doy + 2u) / 153u; 487 uint64_t d = doy - (153u * mp + 2u) / 5u + 1u, m = mp < 10u ? mp + 3u : mp - 9u, y = yoe + era * 400u + (m <= 2u); 488 if (y > 2107u) 489 return; 490 *date = (uint16_t)((y - 1980u) << 9 | m << 5 | d); 491 *time = (uint16_t)((rem / 3600u) << 11 | (rem / 60u % 60u) << 5 | (rem % 60u) / 2u); 492 } 493 494 const char *ck_fatb_strerror(int rc) 495 { 496 switch (rc) { 497 case CK_FATB_OK: return "ok"; 498 case CK_FATB_EGEOM: return "the section is not 512-byte sectors or too small"; 499 case CK_FATB_EIO: return "a read, write or flush failed"; 500 case CK_FATB_ENOTOURS: return "the section holds a volume AIENOS did not write"; 501 case CK_FATB_EBIG: return "the image is larger than a slot"; 502 case CK_FATB_EVERIFY: return "read back, the files are not what was written"; 503 default: return "unknown"; 504 } 505 }