|
19 | 19 | */ |
20 | 20 |
|
21 | 21 | #define CFG_FILENAME "/.KFF2.cfg" |
22 | | - |
| 22 | +#define UI_COLOR_TAIL_BYTES 5 /* bytes at the end of dir->name reserved for color IDs */ |
23 | 23 | #define CRT_C64_SIGNATURE "C64 CARTRIDGE " |
24 | 24 | #define CRT_C128_SIGNATURE "C128 CARTRIDGE " |
25 | 25 | #define CRT_CHIP_SIGNATURE "CHIP" |
|
29 | 29 | #define EAPI_OFFSET 0x3800 |
30 | 30 | #define EAPI_SIZE 0x300 |
31 | 31 |
|
| 32 | + |
32 | 33 | static u16 prg_load_file(FIL *file) |
33 | 34 | { |
34 | 35 | u16 len = file_read(file, dat_buf, sizeof(dat_buf)); |
@@ -412,24 +413,73 @@ static bool mount_sd_card(void) |
412 | 413 | return dir_change("/"); |
413 | 414 | } |
414 | 415 |
|
| 416 | + |
415 | 417 | static bool load_cfg(void) |
416 | 418 | { |
417 | | - bool result = true; |
| 419 | + bool ok = true; |
418 | 420 | FIL file; |
419 | | - if (!file_open(&file, CFG_FILENAME, FA_READ) || |
420 | | - file_read(&file, &cfg_file, sizeof(cfg_file)) != sizeof(cfg_file) || |
421 | | - memcmp(CFG_SIGNATURE, cfg_file.signature, sizeof(cfg_file.signature)) != 0) |
422 | | - { |
423 | | - wrn("%s file not found or invalid", CFG_FILENAME); |
| 421 | + FILINFO info; |
| 422 | + |
| 423 | + /* Start clean + signature + DEFAULTS for new fields */ |
| 424 | + memset(&cfg_file, 0, sizeof(cfg_file)); |
| 425 | + memcpy(cfg_file.signature, CFG_SIGNATURE, sizeof(cfg_file.signature)); |
| 426 | + |
| 427 | + /* Defaults for first boot / older cfgs (IDs, not conio colors) */ |
| 428 | + cfg_file.ui_color1_id = UI_COL_WHITE; |
| 429 | + cfg_file.ui_color2_id = UI_COL_WHITE; |
| 430 | + cfg_file.ui_color3_id = UI_COL_PURPLE; |
| 431 | + cfg_file.ui_color4_id = UI_COL_BLACK; |
| 432 | + cfg_file.ui_color5_id = UI_COL_BLACK; |
| 433 | + |
| 434 | + /* If no file, keep defaults */ |
| 435 | + if (!file_stat(CFG_FILENAME, &info) || !file_open(&file, CFG_FILENAME, FA_READ)) { |
| 436 | + wrn("%s file not found", CFG_FILENAME); |
| 437 | + return false; |
| 438 | + } |
| 439 | + |
| 440 | + /* Read at most what's present in the file (older cfgs are shorter) */ |
| 441 | + UINT want = (UINT)info.fsize; |
| 442 | + if (want > sizeof(cfg_file)) want = (UINT)sizeof(cfg_file); |
| 443 | + |
| 444 | + UINT got = file_read(&file, &cfg_file, want); |
| 445 | + file_close(&file); |
| 446 | + if (got != want) { |
| 447 | + wrn("Failed to read %s", CFG_FILENAME); |
| 448 | + return false; |
| 449 | + } |
| 450 | + |
| 451 | + /* Validate signature */ |
| 452 | + if (memcmp(CFG_SIGNATURE, cfg_file.signature, sizeof(cfg_file.signature)) != 0) { |
| 453 | + wrn("%s invalid signature", CFG_FILENAME); |
424 | 454 | memset(&cfg_file, 0, sizeof(cfg_file)); |
425 | 455 | memcpy(cfg_file.signature, CFG_SIGNATURE, sizeof(cfg_file.signature)); |
426 | | - result = false; |
| 456 | + cfg_file.ui_color1_id = UI_COL_WHITE; |
| 457 | + cfg_file.ui_color2_id = UI_COL_WHITE; |
| 458 | + cfg_file.ui_color3_id = UI_COL_PURPLE; |
| 459 | + cfg_file.ui_color4_id = UI_COL_BLACK; |
| 460 | + cfg_file.ui_color5_id = UI_COL_BLACK; |
| 461 | + ok = false; |
427 | 462 | } |
428 | 463 |
|
429 | | - file_close(&file); |
430 | | - return result; |
| 464 | + /* Clamp invalid IDs (in case of very old/garbage values) */ |
| 465 | + if (cfg_file.ui_color1_id >= UI_COL_MAX) cfg_file.ui_color1_id = UI_COL_WHITE; |
| 466 | + if (cfg_file.ui_color2_id >= UI_COL_MAX) cfg_file.ui_color2_id = UI_COL_WHITE; |
| 467 | + if (cfg_file.ui_color3_id >= UI_COL_MAX) cfg_file.ui_color3_id = UI_COL_PURPLE; |
| 468 | + if (cfg_file.ui_color4_id >= UI_COL_MAX) cfg_file.ui_color4_id = UI_COL_BLACK; |
| 469 | + if (cfg_file.ui_color5_id >= UI_COL_MAX) cfg_file.ui_color5_id = UI_COL_BLACK; |
| 470 | + |
| 471 | + /* --- Safety correction --- */ |
| 472 | + /* If both Color2 and Color3 equal Color4, restore to readable defaults */ |
| 473 | + if (cfg_file.ui_color2_id == cfg_file.ui_color4_id && cfg_file.ui_color3_id == cfg_file.ui_color4_id) { |
| 474 | + cfg_file.ui_color2_id = UI_COL_WHITE; |
| 475 | + cfg_file.ui_color3_id = UI_COL_PURPLE; |
| 476 | + |
| 477 | + } |
| 478 | + |
| 479 | + return ok; |
431 | 480 | } |
432 | 481 |
|
| 482 | + |
433 | 483 | static bool auto_boot(void) |
434 | 484 | { |
435 | 485 | bool result = false; |
|
0 commit comments