From 7cba1ed5347a82b72a17dc9dbd9f020b07ef14a7 Mon Sep 17 00:00:00 2001 From: David Garske Date: Thu, 3 Dec 2026 12:12:04 -0800 Subject: [PATCH] Record why the disk boot path refuses a slot and pass boot stage times to the OS --- docs/API.md | 11 +- docs/compile.md | 4 + include/wolfboot/wolfboot.h | 6 + src/update_disk.c | 242 ++++++++++++++- tools/unit-tests/Makefile | 28 +- tools/unit-tests/unit-update-disk-confirm.c | 59 ++++ tools/unit-tests/unit-update-disk-golden.c | 75 +++++ tools/unit-tests/unit-update-disk.c | 322 ++++++++++++++++++++ 8 files changed, 744 insertions(+), 3 deletions(-) diff --git a/docs/API.md b/docs/API.md index cc863f8282..7304c010b4 100644 --- a/docs/API.md +++ b/docs/API.md @@ -97,6 +97,9 @@ This feature is disabled by default. To enable it, set smaller than, the flash write granularity. Raise it on platforms whose flash write word is wider than 16 bytes (for example 32 for the 256-bit words on STM32H7). +- On the disk boot path (`WOLFBOOT_UPDATE_DISK`) use SPI flash: `EXT_FLASH=1`, + `WOLFBOOT_DIAGNOSTICS_EXT=1` and `WOLFBOOT_SECTOR_SIZE` set to its erase + block (`0x20000` on the ZCU102 QSPI). The region is managed as a circular store over its sectors. With two or more sectors, older records are retained until the log wraps all the way around, so @@ -117,11 +120,17 @@ The following events are recorded: - A rollback was caused by a new image that never confirmed via `wolfBoot_success()` (`WOLFBOOT_FAILURE_PHASE_ROLLBACK`). +On the disk boot path each refused slot is recorded, with `partition` set to +the slot (0 A, 1 B, 2 golden), followed by a `WOLFBOOT_FAILURE_PHASE_RECOVERY` +record when no slot boots. The newest record is printed at the next boot. + For verification failures, the cause distinguishes a bad header (`WOLFBOOT_FAILURE_CAUSE_HEADER`), a failed integrity/hash check (`WOLFBOOT_FAILURE_CAUSE_HASH`) and a failed signature/authenticity check (`WOLFBOOT_FAILURE_CAUSE_SIGNATURE`). A rollback uses -`WOLFBOOT_FAILURE_CAUSE_NOT_CONFIRMED`. +`WOLFBOOT_FAILURE_CAUSE_NOT_CONFIRMED`. The disk boot path adds +`WOLFBOOT_FAILURE_CAUSE_DECRYPT`, `WOLFBOOT_FAILURE_CAUSE_VERSION` and +`WOLFBOOT_FAILURE_CAUSE_READ`. The application can read the records, ordered newest-first, through this API: diff --git a/docs/compile.md b/docs/compile.md index 86bb23bbb2..cdddc20d10 100644 --- a/docs/compile.md +++ b/docs/compile.md @@ -525,6 +525,10 @@ A dual-boot target that loads the image to RAM (two fixed slots, no swap, `src/u `DUALBOOT_ROLLBACK_UNCONFIRMED=1` lets that fallback go to a lower version: the other slot boots, and the unconfirmed slot is erased once that image has passed verification. If the other slot fails verification, the unconfirmed image is kept and booted again. The version check for confirmed images is unchanged. The option is not available with the hardware-assisted swap loader. The trade-off is that someone able to reset the board during the probation window can keep it on the older image, which `ALLOW_DOWNGRADE=0` otherwise prevents once the newer one has been written. +### Boot stage timing + +`BOOT_BENCHMARK=1` prints the time of each step. On the disk boot path it also passes `/chosen/wolfboot,boot-times` to the OS: six big-endian 32-bit stamps in microseconds of `hal_get_timer_us()`, taken at wolfBoot entry and at the end of load, decrypt, hash, verify and handoff (0 for a stage that did not run). On ZynqMP the counter starts at `psu_init`, so the entry stamp is the time spent before wolfBoot. + ### Linux kernel command line (bootargs) On the FIT Linux-boot targets whose DTB comes from the signed FIT (Versal, ZynqMP, PolarFire SoC), a DTB that already carries a non-empty `/chosen/bootargs` keeps it by default - the image boots with the arguments its kernel was validated with, and wolfBoot logs `FDT: using DTB bootargs: ...`. Three macros control this: diff --git a/include/wolfboot/wolfboot.h b/include/wolfboot/wolfboot.h index 0d554bd88c..f94b80ae43 100644 --- a/include/wolfboot/wolfboot.h +++ b/include/wolfboot/wolfboot.h @@ -847,6 +847,12 @@ int wolfBoot_get_partition_state(uint8_t part, uint8_t *st); #define WOLFBOOT_FAILURE_CAUSE_SIGNATURE 3 /* signature/auth check failed */ #define WOLFBOOT_FAILURE_CAUSE_NOT_CONFIRMED 4 /* image never confirmed via * wolfBoot_success() */ +#define WOLFBOOT_FAILURE_CAUSE_DECRYPT 5 /* image could not be + * decrypted */ +#define WOLFBOOT_FAILURE_CAUSE_VERSION 6 /* below the version floor or + * a refused downgrade */ +#define WOLFBOOT_FAILURE_CAUSE_READ 8 /* image could not be read + * from storage */ /* Persisted failure record. */ struct wolfBoot_failure_record { diff --git a/src/update_disk.c b/src/update_disk.c index 684f609271..404a3f1f3b 100644 --- a/src/update_disk.c +++ b/src/update_disk.c @@ -419,6 +419,111 @@ struct boot_slot { * WOLFBOOT_SMALL_STACK=1. */ static struct boot_slot boot_slots[DISK_SLOTS]; +#ifdef BOOT_BENCHMARK +/* Stage stamps in microseconds of hal_get_timer_us(), passed to the OS */ +#define BOOT_TIME_ENTRY 0 +#define BOOT_TIME_LOAD 1 +#define BOOT_TIME_DECRYPT 2 +#define BOOT_TIME_HASH 3 +#define BOOT_TIME_VERIFY 4 +#define BOOT_TIME_HANDOFF 5 +#define BOOT_TIME_STAGES 6 +static uint32_t wolfBoot_boot_times[BOOT_TIME_STAGES]; +#define BOOT_TIME_MARK(stage) \ + (wolfBoot_boot_times[(stage)] = (uint32_t)hal_get_timer_us()) +/* A new load attempt replaces whatever an earlier slot recorded */ +#define BOOT_TIME_RESTART() \ + XMEMSET(&wolfBoot_boot_times[BOOT_TIME_LOAD], 0, \ + (BOOT_TIME_STAGES - BOOT_TIME_LOAD) * sizeof(uint32_t)) +#else +#define BOOT_TIME_MARK(stage) do {} while (0) +#define BOOT_TIME_RESTART() do {} while (0) +#endif + +#ifdef WOLFBOOT_PERSIST_FAILURE_STATUS +/* Cause and slot of the last refusal, reported again if no slot boots */ +static uint8_t disk_last_cause = 0; +static int disk_last_slot = 0; +/* Why the first header read of slot A or B failed, 0 if it did not */ +static uint8_t disk_probe_cause[2]; +/* Set when the first read of A, B or golden found an empty slot, which is + * never recorded */ +static uint8_t disk_probe_blank[3]; +/* Set once those are recorded: a retry of the same slot is not recorded */ +static int disk_probe_recorded = 0; + +/* Persist why a slot was refused. The partition field holds the slot (0 A, + * 1 B, 2 golden), the version the one its header declared. */ +static void disk_record_failure(uint8_t phase, uint8_t cause, int sel, + uint32_t ver) +{ + if ((phase != WOLFBOOT_FAILURE_PHASE_RECOVERY) && (sel >= 0) && + (sel < 3) && disk_probe_blank[sel]) { + return; + } + disk_last_cause = cause; + disk_last_slot = sel; + if (disk_probe_recorded && (phase == WOLFBOOT_FAILURE_PHASE_BOOT) && + (sel < 2) && (disk_probe_cause[sel] != 0)) { + return; + } + if (wolfBoot_record_failure(phase, cause, (uint8_t)sel, ver) != 0) { + wolfBoot_printf("Could not record the failure reason\r\n"); + } +} +/* An all-0x00 or all-0xFF header is an empty slot, not a failure */ +static int disk_hdr_blank(const uint8_t *hdr) +{ + int i; + + if ((hdr[0] != 0x00U) && (hdr[0] != 0xFFU)) { + return 0; + } + for (i = 1; i < IMAGE_HEADER_SIZE; i++) { + if (hdr[i] != hdr[0]) { + return 0; + } + } + return 1; +} + +/* Note why the first read of a slot's header gave no version */ +static void disk_probe_note(int sel, uint8_t cause, uint32_t ver, + const uint8_t *hdr) +{ + if ((disk_probe_cause[sel] != 0U) || disk_probe_blank[sel]) { + return; + } + if (cause == WOLFBOOT_FAILURE_CAUSE_READ) { + disk_probe_cause[sel] = cause; + } + else if ((ver == 0U) && disk_hdr_blank(hdr)) { + disk_probe_blank[sel] = 1; + } + else if (ver == 0U) { + disk_probe_cause[sel] = (cause != 0U) ? cause : + WOLFBOOT_FAILURE_CAUSE_HEADER; + } +} + +static void disk_record_probes(void) +{ + int i; + + for (i = 0; i < 2; i++) { + if (disk_probe_cause[i] != 0U) { + disk_record_failure(WOLFBOOT_FAILURE_PHASE_BOOT, + disk_probe_cause[i], i, 0U); + } + } + disk_probe_recorded = 1; +} +#define DISK_PROBE_NOTE(sel, cause, ver, hdr) \ + disk_probe_note((sel), (cause), (ver), (hdr)) +#else +#define DISK_PROBE_NOTE(sel, cause, ver, hdr) do {} while (0) +#endif + #ifdef DISK_BOOT_CONFIRM /* Boot confirmation for the disk path. include/disk_trailer.h owns the * format: the offset, the magic and the four state values are defined there @@ -566,6 +671,11 @@ static void disk_boot_state_reap(uint32_t *pA_ver, uint32_t *pB_ver) } wolfBoot_printf("Slot %c was not confirmed; skipping it\r\n", 'A' + i); +#ifdef WOLFBOOT_PERSIST_FAILURE_STATUS + disk_record_failure(WOLFBOOT_FAILURE_PHASE_ROLLBACK, + WOLFBOOT_FAILURE_CAUSE_NOT_CONFIRMED, i, + (i == 0) ? *pA_ver : *pB_ver); +#endif slot_reaped[i] = 1; SET_FAILOVER(DISK_FAILOVER_UNCONFIRMED); if (i == 0) { @@ -797,6 +907,28 @@ static const char *disk_boot_file = NULL; * writing the staged DTB here: a HAL may edit a copy of it, and may replace * bootargs wholesale, so a /chosen property set in that pass is what * survives. Nothing is written before a slot has been verified. */ +#ifdef BOOT_BENCHMARK +/* /chosen/wolfboot,boot-times: the BOOT_TIME_* stamps in stage order, in + * microseconds, so the OS can report what the boot chain cost. */ +static int disk_dts_boot_times(fdt_ctx* ctx) +{ + uint32_t be[BOOT_TIME_STAGES]; + int i, off; + + off = fdt_subnode_offset(ctx, 0, "chosen"); + if (off == -FDT_ERR_NOTFOUND) { + off = fdt_add_subnode(ctx, 0, "chosen"); + } + if (off < 0) { + return off; + } + for (i = 0; i < BOOT_TIME_STAGES; i++) { + be[i] = cpu_to_fdt32(wolfBoot_boot_times[i]); + } + return fdt_setprop(ctx, off, "wolfboot,boot-times", be, (int)sizeof(be)); +} +#endif + int wolfBoot_disk_dts_fixup(fdt_ctx* ctx) { int ret; @@ -807,6 +939,13 @@ int wolfBoot_disk_dts_fixup(fdt_ctx* ctx) if (disk_boot_part < 0) { return 0; } +#ifdef BOOT_BENCHMARK + ret = disk_dts_boot_times(ctx); + if (ret != 0) { + wolfBoot_printf("FDT: Failed to set wolfboot,boot-times (%d)\r\n", + ret); + } +#endif ret = fdt_fixup_chosen_val(ctx, "wolfboot,boot-part", (uint32_t)disk_boot_part); if (ret != 0) { @@ -876,6 +1015,37 @@ static const char *disk_signed_cmdline(const uint8_t *hdr) } #endif +#ifdef WOLFBOOT_PERSIST_FAILURE_STATUS +static void disk_report_last_failure(void) +{ + struct wolfBoot_failure_record rec; + + if (wolfBoot_get_failure(0, &rec) == 0) { + wolfBoot_printf("Last recorded failure: #%u phase %u cause %u " + "slot %u version %u\r\n", (unsigned int)rec.seq, + (unsigned int)rec.phase, (unsigned int)rec.cause, + (unsigned int)rec.partition, (unsigned int)rec.fw_version); + } +} +/* Neither A nor B has a usable header. Without a golden slot nothing can + * boot. */ +static void disk_record_no_image(void) +{ +#ifndef DISK_GOLDEN_SLOT + disk_record_failure(WOLFBOOT_FAILURE_PHASE_RECOVERY, + (disk_last_cause != 0) ? disk_last_cause : + WOLFBOOT_FAILURE_CAUSE_HEADER, + disk_last_slot, 0U); +#endif +} + +#define DISK_RECORD_FAILURE(phase, cause) \ + disk_record_failure((phase), (cause), selected, \ + (selected == 0) ? pA_ver_u : ((selected == 1) ? pB_ver_u : 0U)) +#else +#define DISK_RECORD_FAILURE(phase, cause) do {} while (0) +#endif + void RAMFUNCTION wolfBoot_start(void) { uint8_t p_hdr[IMAGE_HEADER_SIZE] XALIGNED_STACK(16); @@ -935,6 +1105,7 @@ void RAMFUNCTION wolfBoot_start(void) #endif BENCHMARK_DECLARE(); + BOOT_TIME_MARK(BOOT_TIME_ENTRY); #ifdef DISK_ENCRYPT /* Initialize encryption - this sets up the cipher with key from storage */ if (wolfBoot_initialize_encryption() != 0) { @@ -990,7 +1161,6 @@ void RAMFUNCTION wolfBoot_start(void) wolfBoot_printf("Error opening disk %d\r\n", BOOT_DISK); wolfBoot_panic(); } - #ifdef WOLFBOOT_FSP stage2_params = stage2_get_parameters(); #endif @@ -1032,6 +1202,9 @@ void RAMFUNCTION wolfBoot_start(void) BOOT_FILE_A, slot_max); (void)slot_prepare(&boot_slots[1], BOOT_PART_B, BOOT_LABEL_B, BOOT_FILE_B, slot_max); +#ifdef WOLFBOOT_PERSIST_FAILURE_STATUS + disk_report_last_failure(); +#endif wolfBoot_printf("Checking primary OS image in %d,%d...\r\n", BOOT_DISK, boot_slots[0].part); @@ -1044,10 +1217,17 @@ void RAMFUNCTION wolfBoot_start(void) pA_ver = get_decrypted_blob_version(dec_hdr); slot_note_image(&boot_slots[0], dec_hdr); } + else { + DISK_PROBE_NOTE(0, WOLFBOOT_FAILURE_CAUSE_DECRYPT, 0U, p_hdr); + } #else pA_ver = wolfBoot_get_blob_version((uint8_t*)p_hdr); slot_note_image(&boot_slots[0], p_hdr); #endif + DISK_PROBE_NOTE(0, 0U, pA_ver, p_hdr); + } + else { + DISK_PROBE_NOTE(0, WOLFBOOT_FAILURE_CAUSE_READ, 0U, p_hdr); } wolfBoot_printf("Checking secondary OS image in %d,%d...\r\n", BOOT_DISK, @@ -1061,12 +1241,22 @@ void RAMFUNCTION wolfBoot_start(void) pB_ver = get_decrypted_blob_version(dec_hdr); slot_note_image(&boot_slots[1], dec_hdr); } + else { + DISK_PROBE_NOTE(1, WOLFBOOT_FAILURE_CAUSE_DECRYPT, 0U, p_hdr); + } #else pB_ver = wolfBoot_get_blob_version((uint8_t*)p_hdr); slot_note_image(&boot_slots[1], p_hdr); #endif + DISK_PROBE_NOTE(1, 0U, pB_ver, p_hdr); + } + else { + DISK_PROBE_NOTE(1, WOLFBOOT_FAILURE_CAUSE_READ, 0U, p_hdr); } +#ifdef WOLFBOOT_PERSIST_FAILURE_STATUS + disk_record_probes(); +#endif #ifdef DISK_BOOT_CONFIRM disk_boot_state_reap(&pA_ver, &pB_ver); #endif @@ -1074,6 +1264,9 @@ void RAMFUNCTION wolfBoot_start(void) if ((pB_ver == 0) && (pA_ver == 0)) { wolfBoot_printf("No valid OS image found in either partition %d or %d\r\n", boot_slots[0].part, boot_slots[1].part); +#ifdef WOLFBOOT_PERSIST_FAILURE_STATUS + disk_record_no_image(); +#endif #ifdef DISK_GOLDEN_SLOT /* Straight to the golden slot. A version-0 header is not a bootable * A/B image here any more than it was before this option existed. */ @@ -1154,6 +1347,8 @@ void RAMFUNCTION wolfBoot_start(void) if (!SLOT_IS_GOLDEN(selected) && slot_reaped[selected]) { wolfBoot_printf("Slot %c was not confirmed; not booting it\r\n", 'A' + selected); + DISK_RECORD_FAILURE(WOLFBOOT_FAILURE_PHASE_ROLLBACK, + WOLFBOOT_FAILURE_CAUSE_NOT_CONFIRMED); SET_FAILOVER(DISK_FAILOVER_UNCONFIRMED); selected ^= 1; continue; @@ -1165,6 +1360,8 @@ void RAMFUNCTION wolfBoot_start(void) if (!SLOT_IS_GOLDEN(selected) && (max_ver > 0U) && (cur_ver < max_ver)) { wolfBoot_printf("Rollback to lower version not allowed\r\n"); + DISK_RECORD_FAILURE(WOLFBOOT_FAILURE_PHASE_BOOT, + WOLFBOOT_FAILURE_CAUSE_VERSION); #ifdef DISK_GOLDEN_SLOT /* Nothing newer can boot: only the golden slot is left. */ SET_FAILOVER(DISK_FAILOVER_ROLLBACK); @@ -1175,6 +1372,10 @@ void RAMFUNCTION wolfBoot_start(void) disk_decrypted_header_clear(dec_hdr); disk_crypto_clear(); #endif +#ifdef WOLFBOOT_PERSIST_FAILURE_STATUS + disk_record_failure(WOLFBOOT_FAILURE_PHASE_RECOVERY, + disk_last_cause, disk_last_slot, 0U); +#endif #if defined(WOLFBOOT_ANTI_ROLLBACK) && defined(WOLFBOOT_FSP) boot_state_on_failure(BOOT_FAIL_ROLLBACK); #endif @@ -1197,10 +1398,17 @@ void RAMFUNCTION wolfBoot_start(void) != IMAGE_HEADER_SIZE) { wolfBoot_printf("Error reading image header from disk: p%d\r\n", cur_part); + DISK_RECORD_FAILURE(WOLFBOOT_FAILURE_PHASE_BOOT, + WOLFBOOT_FAILURE_CAUSE_READ); SET_FAILOVER(DISK_FAILOVER_NO_IMAGE); selected ^= 1; continue; } +#if defined(WOLFBOOT_PERSIST_FAILURE_STATUS) && defined(DISK_GOLDEN_SLOT) + if (SLOT_IS_GOLDEN(selected) && disk_hdr_blank(p_hdr)) { + disk_probe_blank[SLOT_GOLDEN] = 1; + } +#endif hdr_ptr = p_hdr; #ifdef DISK_ENCRYPT @@ -1208,6 +1416,8 @@ void RAMFUNCTION wolfBoot_start(void) /* Each slot carries its own key, so re-key before reading one. */ if (disk_crypto_rekey(selected) != 0) { wolfBoot_printf("Error selecting the key for %s\r\n", part_name); + DISK_RECORD_FAILURE(WOLFBOOT_FAILURE_PHASE_BOOT, + WOLFBOOT_FAILURE_CAUSE_DECRYPT); SET_FAILOVER_IF_UNSET(DISK_FAILOVER_NO_KEY); selected ^= 1; continue; @@ -1221,6 +1431,8 @@ void RAMFUNCTION wolfBoot_start(void) } else if (decrypt_header(p_hdr, dec_hdr) != 0) { wolfBoot_printf("Error decrypting header for %s\r\n", part_name); + DISK_RECORD_FAILURE(WOLFBOOT_FAILURE_PHASE_BOOT, + WOLFBOOT_FAILURE_CAUSE_DECRYPT); SET_FAILOVER(DISK_FAILOVER_NO_IMAGE); selected ^= 1; continue; @@ -1233,6 +1445,8 @@ void RAMFUNCTION wolfBoot_start(void) ret = wolfBoot_open_image_address(&os_image, (void*)hdr_ptr); if (ret < 0) { wolfBoot_printf("Error parsing loaded image\r\n"); + DISK_RECORD_FAILURE(WOLFBOOT_FAILURE_PHASE_BOOT, + WOLFBOOT_FAILURE_CAUSE_HEADER); SET_FAILOVER(DISK_FAILOVER_NO_IMAGE); selected ^= 1; continue; @@ -1246,6 +1460,8 @@ void RAMFUNCTION wolfBoot_start(void) if (wolfBoot_image_part_matches(&os_image) < 0) { wolfBoot_printf("Image partition mismatch for %s\r\n", part_name); + DISK_RECORD_FAILURE(WOLFBOOT_FAILURE_PHASE_BOOT, + WOLFBOOT_FAILURE_CAUSE_HEADER); selected ^= 1; continue; } @@ -1264,6 +1480,8 @@ void RAMFUNCTION wolfBoot_start(void) (((uint64_t)os_image.fw_size + (uint64_t)IMAGE_HEADER_SIZE) > fs_size(&slot->f))) { wolfBoot_printf("Image larger than file for %s\r\n", part_name); + DISK_RECORD_FAILURE(WOLFBOOT_FAILURE_PHASE_BOOT, + WOLFBOOT_FAILURE_CAUSE_HEADER); SET_FAILOVER(DISK_FAILOVER_NO_IMAGE); selected ^= 1; continue; @@ -1274,6 +1492,8 @@ void RAMFUNCTION wolfBoot_start(void) if (os_image.fw_size > WOLFBOOT_RAMBOOT_MAX_SIZE) { wolfBoot_printf("Image size %u exceeds max RAM load size\r\n", os_image.fw_size); + DISK_RECORD_FAILURE(WOLFBOOT_FAILURE_PHASE_BOOT, + WOLFBOOT_FAILURE_CAUSE_HEADER); SET_FAILOVER(DISK_FAILOVER_NO_IMAGE); selected ^= 1; continue; @@ -1287,6 +1507,8 @@ void RAMFUNCTION wolfBoot_start(void) if (os_image.fw_size > slot_max) { wolfBoot_printf("Image size %u doesn't fit in low memory\r\n", os_image.fw_size); + DISK_RECORD_FAILURE(WOLFBOOT_FAILURE_PHASE_BOOT, + WOLFBOOT_FAILURE_CAUSE_HEADER); SET_FAILOVER(DISK_FAILOVER_NO_IMAGE); selected ^= 1; continue; @@ -1299,6 +1521,7 @@ void RAMFUNCTION wolfBoot_start(void) /* Read the payload into RAM (skip header) */ wolfBoot_printf("Loading image from disk..."); + BOOT_TIME_RESTART(); BENCHMARK_START(); load_off = 0; do { @@ -1322,11 +1545,14 @@ void RAMFUNCTION wolfBoot_start(void) wolfBoot_printf("Error reading image from disk: p%d " "(%u of %u bytes)\r\n", cur_part, (unsigned int)load_off, (unsigned int)os_image.fw_size); + DISK_RECORD_FAILURE(WOLFBOOT_FAILURE_PHASE_BOOT, + WOLFBOOT_FAILURE_CAUSE_READ); SET_FAILOVER(DISK_FAILOVER_NO_IMAGE); selected ^= 1; continue; } BENCHMARK_END("done"); + BOOT_TIME_MARK(BOOT_TIME_LOAD); #ifdef DISK_ENCRYPT /* Decrypt the payload in RAM */ @@ -1343,6 +1569,7 @@ void RAMFUNCTION wolfBoot_start(void) crypto_decrypt((uint8_t*)load_address, (uint8_t*)load_address, os_image.fw_size); BENCHMARK_END("done"); + BOOT_TIME_MARK(BOOT_TIME_DECRYPT); } #endif @@ -1350,6 +1577,8 @@ void RAMFUNCTION wolfBoot_start(void) ret = wolfBoot_open_image_address(&os_image, (void*)hdr_ptr); if (ret < 0) { wolfBoot_printf("Error parsing loaded image\r\n"); + DISK_RECORD_FAILURE(WOLFBOOT_FAILURE_PHASE_BOOT, + WOLFBOOT_FAILURE_CAUSE_HEADER); SET_FAILOVER(DISK_FAILOVER_NO_IMAGE); selected ^= 1; continue; @@ -1364,22 +1593,28 @@ void RAMFUNCTION wolfBoot_start(void) BENCHMARK_START(); if (wolfBoot_verify_integrity(&os_image) != 0) { wolfBoot_printf("Error validating integrity for %s\r\n", part_name); + DISK_RECORD_FAILURE(WOLFBOOT_FAILURE_PHASE_BOOT, + WOLFBOOT_FAILURE_CAUSE_HASH); SET_FAILOVER(DISK_FAILOVER_VERIFY); selected ^= 1; continue; } BENCHMARK_END("done"); + BOOT_TIME_MARK(BOOT_TIME_HASH); wolfBoot_printf("Verifying image signature..."); BENCHMARK_START(); if (wolfBoot_verify_authenticity(&os_image) != 0) { wolfBoot_printf("Error validating authenticity for %s\r\n", part_name); + DISK_RECORD_FAILURE(WOLFBOOT_FAILURE_PHASE_BOOT, + WOLFBOOT_FAILURE_CAUSE_SIGNATURE); SET_FAILOVER(DISK_FAILOVER_VERIFY); selected ^= 1; continue; } else { BENCHMARK_END("done"); + BOOT_TIME_MARK(BOOT_TIME_VERIFY); failures = 0; break; /* Success case */ } @@ -1395,6 +1630,10 @@ void RAMFUNCTION wolfBoot_start(void) disk_crypto_clear(); #endif wolfBoot_printf("Unable to find a valid partition!\r\n"); +#ifdef WOLFBOOT_PERSIST_FAILURE_STATUS + disk_record_failure(WOLFBOOT_FAILURE_PHASE_RECOVERY, disk_last_cause, + disk_last_slot, 0U); +#endif #if defined(WOLFBOOT_ANTI_ROLLBACK) && defined(WOLFBOOT_FSP) boot_state_on_failure(BOOT_FAIL_VERIFY_BOTH); #endif @@ -1682,6 +1921,7 @@ void RAMFUNCTION wolfBoot_start(void) * wolfBoot set them up. */ wolfBoot_hook_preboot(&os_image); #endif + BOOT_TIME_MARK(BOOT_TIME_HANDOFF); hal_prepare_boot(); #ifdef WOLFBOOT_HOOK_BOOT diff --git a/tools/unit-tests/Makefile b/tools/unit-tests/Makefile index 2b443e2754..97c01a6fa8 100644 --- a/tools/unit-tests/Makefile +++ b/tools/unit-tests/Makefile @@ -93,7 +93,7 @@ TESTS:=unit-parser unit-parser-large-header unit-fdt unit-extflash unit-string \ unit-update-flash-self-update-monolithic \ unit-nsc-update \ unit-update-flash-enc unit-update-flash-enc-cache unit-update-flash-enc-full unit-update-flash-enc-intswap unit-update-ram unit-update-ram-fit unit-update-ram-uboot unit-update-ram-enc unit-update-ram-enc-start unit-update-ram-enc-nopart unit-update-ram-nofixed unit-update-ram-nofixed-noramboot unit-update-ram-noramboot unit-update-ram-custom-trailer unit-update-ram-rollback-unconfirmed unit-compressed-tlv unit-custom-trailer-nopart unit-update-flash-hwswap unit-update-flash-custom-trailer unit-pkcs11_store unit-psa_store unit-wolfhsm_flash_hal unit-disk \ - unit-update-disk unit-update-disk-confirm unit-update-disk-golden unit-update-disk-failover unit-update-disk-golden-enckey unit-update-disk-fsp unit-update-disk-oob unit-update-disk-fit unit-update-disk-fit-ramdisk unit-multiboot unit-boot-x86-fsp unit-loader-tpm-init unit-qspi-flash unit-fwtpm-stub unit-tpm-rsa-exp \ + unit-update-disk unit-update-disk-confirm unit-update-disk-confirm-diag unit-update-disk-golden unit-update-disk-failover unit-update-disk-golden-enckey unit-update-disk-fsp unit-update-disk-oob unit-update-disk-fit unit-update-disk-fit-ramdisk unit-multiboot unit-boot-x86-fsp unit-loader-tpm-init unit-qspi-flash unit-fwtpm-stub unit-tpm-rsa-exp \ unit-image-nopart unit-image-sha384 unit-image-sha3-384 unit-image-dts \ unit-image-dts-sha384 unit-image-dts-sha3-384 unit-store-sbrk \ unit-tpm-blob unit-policy-create unit-policy-sign unit-rot-auth unit-sdhci-response-bits \ @@ -118,6 +118,7 @@ TESTS+=unit-psoc-c3-divider TESTS+=unit-psoc-e8-tz TESTS+=unit-psoc-e8-flash TESTS+=unit-diagnostics-256 +TESTS+=unit-update-disk-diag unit-update-disk-diag-plain unit-update-disk-golden-diag TESTS+=unit-fit-gzip unit-fit-nogzip TESTS+=unit-gzip-small unit-gzip-bits32 unit-gzip-root7 unit-fit-gzip-small ifeq ($(HAVE_ZLIB),1) @@ -659,12 +660,24 @@ unit-custom-trailer-nopart:CFLAGS+=-DMOCK_PARTITIONS -DWOLFBOOT_NO_SIGN \ # the cap update_disk.c now requires; all images here are exactly that size. unit-update-disk:CFLAGS+=-DMOCK_PARTITIONS -DPRINTF_ENABLED -DWOLFBOOT_RAMBOOT_MAX_SIZE=0x40 \ -DWOLFBOOT_ORIGIN=MOCK_ADDRESS_BOOT -DBOOTLOADER_PARTITION_SIZE=WOLFBOOT_PARTITION_SIZE +unit-update-disk-diag:CFLAGS+=-DMOCK_PARTITIONS -DPRINTF_ENABLED -DWOLFBOOT_RAMBOOT_MAX_SIZE=0x40 \ + -DWOLFBOOT_ORIGIN=MOCK_ADDRESS_BOOT -DBOOTLOADER_PARTITION_SIZE=WOLFBOOT_PARTITION_SIZE \ + -DWOLFBOOT_PERSIST_FAILURE_STATUS +unit-update-disk-diag-plain:CFLAGS+=-DMOCK_PARTITIONS -DPRINTF_ENABLED -DWOLFBOOT_RAMBOOT_MAX_SIZE=0x40 \ + -DWOLFBOOT_ORIGIN=MOCK_ADDRESS_BOOT -DBOOTLOADER_PARTITION_SIZE=WOLFBOOT_PARTITION_SIZE \ + -DWOLFBOOT_PERSIST_FAILURE_STATUS -DUNIT_DISK_PLAIN unit-update-disk-confirm:CFLAGS+=-DMOCK_PARTITIONS -DPRINTF_ENABLED -DWOLFBOOT_RAMBOOT_MAX_SIZE=0x400 \ -DWOLFBOOT_ORIGIN=MOCK_ADDRESS_BOOT -DBOOTLOADER_PARTITION_SIZE=WOLFBOOT_PARTITION_SIZE +unit-update-disk-confirm-diag:CFLAGS+=-DMOCK_PARTITIONS -DPRINTF_ENABLED -DWOLFBOOT_RAMBOOT_MAX_SIZE=0x400 \ + -DWOLFBOOT_ORIGIN=MOCK_ADDRESS_BOOT -DBOOTLOADER_PARTITION_SIZE=WOLFBOOT_PARTITION_SIZE \ + -DWOLFBOOT_PERSIST_FAILURE_STATUS unit-update-disk-golden:CFLAGS+=-DMOCK_PARTITIONS -DPRINTF_ENABLED -DWOLFBOOT_RAMBOOT_MAX_SIZE=0x400 \ -DWOLFBOOT_ORIGIN=MOCK_ADDRESS_BOOT -DBOOTLOADER_PARTITION_SIZE=WOLFBOOT_PARTITION_SIZE unit-update-disk-failover:CFLAGS+=-DMOCK_PARTITIONS -DPRINTF_ENABLED -DWOLFBOOT_RAMBOOT_MAX_SIZE=0x400 \ -DWOLFBOOT_ORIGIN=MOCK_ADDRESS_BOOT -DBOOTLOADER_PARTITION_SIZE=WOLFBOOT_PARTITION_SIZE +unit-update-disk-golden-diag:CFLAGS+=-DMOCK_PARTITIONS -DPRINTF_ENABLED -DWOLFBOOT_RAMBOOT_MAX_SIZE=0x400 \ + -DWOLFBOOT_ORIGIN=MOCK_ADDRESS_BOOT -DBOOTLOADER_PARTITION_SIZE=WOLFBOOT_PARTITION_SIZE \ + -DWOLFBOOT_PERSIST_FAILURE_STATUS unit-update-disk-golden-enckey:CFLAGS+=-DMOCK_PARTITIONS -DPRINTF_ENABLED -DWOLFBOOT_RAMBOOT_MAX_SIZE=0x400 \ -DWOLFBOOT_ORIGIN=MOCK_ADDRESS_BOOT -DBOOTLOADER_PARTITION_SIZE=WOLFBOOT_PARTITION_SIZE # Non-FSP disk-boot OOB regression (CRIT-03). WOLFBOOT_RAMBOOT_MAX_SIZE is the @@ -1625,15 +1638,28 @@ unit-update-flash-hwswap: ../../include/target.h unit-update-flash-hwswap.c unit-update-disk: ../../include/target.h unit-update-disk.c gcc -o $@ unit-update-disk.c $(CFLAGS) $(LDFLAGS) +# unit-update-disk with failure diagnostics: which refusals are recorded +unit-update-disk-diag: ../../include/target.h unit-update-disk.c + gcc -o $@ unit-update-disk.c $(CFLAGS) $(LDFLAGS) + +unit-update-disk-diag-plain: ../../include/target.h unit-update-disk.c + gcc -o $@ unit-update-disk.c $(CFLAGS) $(LDFLAGS) + unit-update-disk-confirm: ../../include/target.h unit-update-disk-confirm.c gcc -o $@ unit-update-disk-confirm.c $(CFLAGS) $(LDFLAGS) +unit-update-disk-confirm-diag: ../../include/target.h unit-update-disk-confirm.c + gcc -o $@ unit-update-disk-confirm.c $(CFLAGS) $(LDFLAGS) + unit-update-disk-golden: ../../include/target.h unit-update-disk-golden.c gcc -o $@ unit-update-disk-golden.c $(CFLAGS) $(LDFLAGS) unit-update-disk-failover: ../../include/target.h unit-update-disk-failover.c gcc -o $@ unit-update-disk-failover.c $(CFLAGS) $(LDFLAGS) +unit-update-disk-golden-diag: ../../include/target.h unit-update-disk-golden.c + gcc -o $@ unit-update-disk-golden.c $(CFLAGS) $(LDFLAGS) + unit-update-disk-golden-enckey: ../../include/target.h unit-update-disk-golden-enckey.c gcc -o $@ unit-update-disk-golden-enckey.c $(CFLAGS) $(LDFLAGS) diff --git a/tools/unit-tests/unit-update-disk-confirm.c b/tools/unit-tests/unit-update-disk-confirm.c index ab71446099..724300fdf9 100644 --- a/tools/unit-tests/unit-update-disk-confirm.c +++ b/tools/unit-tests/unit-update-disk-confirm.c @@ -288,6 +288,28 @@ int hal_flash_protect(haladdr_t address, int len) return 0; } +#ifdef WOLFBOOT_PERSIST_FAILURE_STATUS +static int mock_records; + +int wolfBoot_record_failure(uint8_t phase, uint8_t cause, uint8_t partition, + uint32_t fw_version) +{ + (void)phase; + (void)cause; + (void)partition; + (void)fw_version; + mock_records++; + return 0; +} + +int wolfBoot_get_failure(int index, struct wolfBoot_failure_record *out) +{ + (void)index; + (void)out; + return -1; +} +#endif + #include "update_disk.c" @@ -592,6 +614,39 @@ START_TEST(test_failover_does_not_land_on_a_testing_slot) } END_TEST +#ifdef WOLFBOOT_PERSIST_FAILURE_STATUS +START_TEST(test_diag_refused_slot_is_recorded) +{ + reset_mocks(); + mock_records = 0; + build_image(part_a_image, 4, 0xA1); + build_image(part_b_image, 5, 0xB2); + mock_verify_authenticity_ret = -1; + + wolfBoot_start(); + + ck_assert_int_eq(wolfBoot_panicked, 1); + ck_assert_int_gt(mock_records, 0); +} +END_TEST + +START_TEST(test_diag_unconfirmed_slot_is_recorded) +{ + reset_mocks(); + mock_records = 0; + build_image(part_a_image, 5, 0xA1); + build_image(part_b_image, 4, 0xB2); + set_state_at(part_a_image, (size_t)mock_part_size, ST_TESTING); + set_state_at(part_b_image, (size_t)mock_part_size, ST_SUCCESS); + + wolfBoot_start(); + + ck_assert_int_eq(mock_do_boot_called, 1); + ck_assert_int_eq(mock_records, 1); +} +END_TEST +#endif + Suite *wolfboot_suite(void) { Suite *s = suite_create("wolfBoot"); @@ -608,6 +663,10 @@ Suite *wolfboot_suite(void) tcase_add_test(tc, test_partition_too_small_for_a_trailer_still_boots); tcase_add_test(tc, test_two_unconfirmed_slots_boot_neither); tcase_add_test(tc, test_failover_does_not_land_on_a_testing_slot); +#ifdef WOLFBOOT_PERSIST_FAILURE_STATUS + tcase_add_test(tc, test_diag_refused_slot_is_recorded); + tcase_add_test(tc, test_diag_unconfirmed_slot_is_recorded); +#endif suite_add_tcase(s, tc); return s; diff --git a/tools/unit-tests/unit-update-disk-golden.c b/tools/unit-tests/unit-update-disk-golden.c index 321d99d76f..21605d3c6f 100644 --- a/tools/unit-tests/unit-update-disk-golden.c +++ b/tools/unit-tests/unit-update-disk-golden.c @@ -271,6 +271,46 @@ int hal_flash_protect(haladdr_t address, int len) return 0; } +#ifdef WOLFBOOT_PERSIST_FAILURE_STATUS +/* Slot of each failure record, oldest first */ +#define MOCK_MAX_RECORDS 16 +static uint8_t mock_record_part[MOCK_MAX_RECORDS]; +static uint8_t mock_record_phase[MOCK_MAX_RECORDS]; +static int mock_record_count; + +int wolfBoot_record_failure(uint8_t phase, uint8_t cause, uint8_t partition, + uint32_t fw_version) +{ + (void)cause; + (void)fw_version; + if (mock_record_count < MOCK_MAX_RECORDS) { + mock_record_part[mock_record_count] = partition; + mock_record_phase[mock_record_count] = phase; + } + mock_record_count++; + return 0; +} + +int wolfBoot_get_failure(int index, struct wolfBoot_failure_record *out) +{ + (void)index; + (void)out; + return -1; +} + +/* Records naming 'part', other than the final RECOVERY record */ +static int records_for(int part) +{ + int i, n = 0; + for (i = 0; (i < mock_record_count) && (i < MOCK_MAX_RECORDS); i++) { + if ((mock_record_part[i] == part) && + (mock_record_phase[i] != WOLFBOOT_FAILURE_PHASE_RECOVERY)) + n++; + } + return n; +} +#endif + #include "update_disk.c" /* Trailer in the partition tail, as include/disk_trailer.h lays it out. */ @@ -377,6 +417,37 @@ START_TEST(test_golden_boots_when_a_is_unconfirmed_and_b_bad) } END_TEST +#ifdef WOLFBOOT_PERSIST_FAILURE_STATUS +/* An empty golden slot is not a failure, even when A and B fail */ +START_TEST(test_diag_blank_golden_is_not_recorded) +{ + reset_mocks(); + mock_record_count = 0; + mark_bad(FILL_A); + mark_bad(FILL_B); + memset(part_image[BOOT_PART_GOLDEN], 0xFF, IMAGE_HEADER_SIZE); + wolfBoot_start(); + ck_assert_int_gt(wolfBoot_panicked, 0); + ck_assert_int_gt(mock_record_count, 0); + ck_assert_int_eq(records_for(BOOT_PART_GOLDEN), 0); +} +END_TEST + +/* Nor is an empty slot whose trailer still says TESTING */ +START_TEST(test_diag_blank_testing_slot_is_not_recorded) +{ + reset_mocks(); + mock_record_count = 0; + memset(part_image[BOOT_PART_B], 0xFF, IMAGE_HEADER_SIZE); + set_state(BOOT_PART_B, DISK_STATE_TESTING); + wolfBoot_start(); + ck_assert_int_eq(wolfBoot_panicked, 0); + ck_assert_int_eq(mock_booted_fill, FILL_A); + ck_assert_int_eq(records_for(BOOT_PART_B), 0); +} +END_TEST +#endif + Suite *wolfboot_suite(void) { Suite *s = suite_create("wolfBoot"); @@ -389,6 +460,10 @@ Suite *wolfboot_suite(void) tcase_add_test(tc, test_golden_is_verified_and_a_bad_golden_panics); tcase_add_test(tc, test_golden_is_never_written_and_skips_confirmation); tcase_add_test(tc, test_golden_boots_when_a_is_unconfirmed_and_b_bad); +#ifdef WOLFBOOT_PERSIST_FAILURE_STATUS + tcase_add_test(tc, test_diag_blank_golden_is_not_recorded); + tcase_add_test(tc, test_diag_blank_testing_slot_is_not_recorded); +#endif suite_add_tcase(s, tc); return s; diff --git a/tools/unit-tests/unit-update-disk.c b/tools/unit-tests/unit-update-disk.c index 8bc993cf33..d45fddbaa3 100644 --- a/tools/unit-tests/unit-update-disk.c +++ b/tools/unit-tests/unit-update-disk.c @@ -2,9 +2,11 @@ #define WOLFBOOT_SELF_UPDATE_MONOLITHIC #define RAM_CODE #define WOLFBOOT_SELF_HEADER +#ifndef UNIT_DISK_PLAIN #define EXT_ENCRYPTED #define ENCRYPT_WITH_CHACHA #define HAVE_CHACHA +#endif #define IMAGE_HEADER_SIZE 256 #define BOOT_PART_A 0 #define BOOT_PART_B 1 @@ -20,7 +22,9 @@ #include "wolfboot/wolfboot.h" #include "image.h" #include "loader.h" +#ifndef UNIT_DISK_PLAIN #include +#endif #define TEST_PAYLOAD_SIZE 64 @@ -34,6 +38,8 @@ static int mock_disk_close_called; static int mock_do_boot_called; static const uint32_t *mock_boot_address; static int mock_fail_payload_part; +static int mock_fail_all_reads; +static int mock_fail_header_part; static int mock_verify_integrity_ret; /* not_ext as the verify calls saw it: the staged image lives in RAM, so an * EXT_FLASH build must not route these reads back through external flash. */ @@ -43,7 +49,9 @@ static int mock_verify_authenticity_ret; static int mock_part_mismatch_part; static int mock_last_read_part; +#ifndef UNIT_DISK_PLAIN ChaCha chacha; +#endif static void set_u16_le(uint8_t *dst, uint16_t value) { @@ -86,6 +94,8 @@ static void reset_mocks(void) mock_do_boot_called = 0; mock_boot_address = NULL; mock_fail_payload_part = -1; + mock_fail_all_reads = 0; + mock_fail_header_part = -1; mock_verify_integrity_ret = 0; mock_verify_authenticity_ret = 0; mock_part_mismatch_part = -2; @@ -98,6 +108,7 @@ static void reset_mocks(void) wolfBoot_panicked = 0; } +#ifndef UNIT_DISK_PLAIN int chacha_init(void) { return 0; @@ -117,6 +128,7 @@ int wc_Chacha_Process(ChaCha* ctx, byte* output, const byte* input, word32 msgle memmove(output, input, msglen); return 0; } +#endif void wc_ForceZero(void* mem, size_t len) { @@ -126,6 +138,7 @@ void wc_ForceZero(void* mem, size_t len) } } +#ifndef UNIT_DISK_PLAIN int wolfBoot_initialize_encryption(void) { return 0; @@ -137,6 +150,22 @@ int wolfBoot_get_encrypt_key(uint8_t *key, uint8_t *nonce) memset(nonce, 0xC3, ENCRYPT_NONCE_SIZE); return 0; } +#endif + +#ifdef UNIT_DISK_PLAIN +/* The layout build_image() writes: magic, then the version TLV first */ +uint32_t wolfBoot_get_blob_version(uint8_t *blob) +{ + uint32_t magic, ver; + + memcpy(&magic, blob, sizeof(magic)); + if (magic != WOLFBOOT_MAGIC) + return 0; + memcpy(&ver, blob + IMAGE_HEADER_OFFSET + 2 * sizeof(uint16_t), + sizeof(ver)); + return ver; +} +#endif int disk_init(int drv) { @@ -163,6 +192,8 @@ int disk_part_read(int drv, int part, uint64_t off, uint64_t sz, uint8_t *buf) (void)drv; mock_last_read_part = part; + if (mock_fail_all_reads || ((mock_fail_header_part == part) && (off == 0))) + return -1; image = (part == BOOT_PART_B) ? part_b_image : part_a_image; if ((mock_fail_payload_part == part) && (off >= IMAGE_HEADER_SIZE)) return -1; @@ -241,8 +272,42 @@ int hal_flash_protect(haladdr_t address, int len) return 0; } +#ifdef WOLFBOOT_PERSIST_FAILURE_STATUS +/* Failure records as update_disk.c reported them, oldest first */ +#define MOCK_MAX_RECORDS 16 +static struct wolfBoot_failure_record mock_records[MOCK_MAX_RECORDS]; +static int mock_record_count; + +int wolfBoot_record_failure(uint8_t phase, uint8_t cause, uint8_t partition, + uint32_t fw_version) +{ + if (mock_record_count < MOCK_MAX_RECORDS) { + mock_records[mock_record_count].phase = phase; + mock_records[mock_record_count].cause = cause; + mock_records[mock_record_count].partition = partition; + mock_records[mock_record_count].fw_version = fw_version; + } + mock_record_count++; + return 0; +} + +int wolfBoot_get_failure(int index, struct wolfBoot_failure_record *out) +{ + (void)index; + (void)out; + return -1; +} + +static void reset_records(void) +{ + memset(mock_records, 0, sizeof(mock_records)); + mock_record_count = 0; +} +#endif + #include "update_disk.c" +#ifndef UNIT_DISK_PLAIN START_TEST(test_update_disk_zeroizes_key_material_on_panic) { size_t i; @@ -288,6 +353,7 @@ START_TEST(test_update_disk_zeroizes_key_material_before_boot) } } END_TEST +#endif START_TEST(test_update_disk_prefers_primary_partition_when_versions_equal) { @@ -357,6 +423,7 @@ START_TEST(test_update_disk_marks_staged_image_not_external) } END_TEST +#ifndef UNIT_DISK_PLAIN START_TEST(test_get_decrypted_blob_version_rejects_truncated_version_tlv) { uint8_t hdr[IMAGE_HEADER_SIZE + 2]; @@ -385,6 +452,7 @@ START_TEST(test_get_decrypted_blob_version_rejects_truncated_version_tlv) ck_assert_uint_eq(get_decrypted_blob_version(hdr), 0); } END_TEST +#endif START_TEST(test_update_disk_highbit_version_selects_higher_partition) { @@ -403,7 +471,9 @@ END_TEST START_TEST(test_update_disk_rejects_rollback_after_higher_image_failure) { +#ifndef UNIT_DISK_PLAIN size_t i; +#endif reset_mocks(); build_image(part_a_image, 7, 0xA1); @@ -414,12 +484,14 @@ START_TEST(test_update_disk_rejects_rollback_after_higher_image_failure) ck_assert_int_gt(wolfBoot_panicked, 0); ck_assert_int_eq(mock_do_boot_called, 0); +#ifndef UNIT_DISK_PLAIN for (i = 0; i < ENCRYPT_KEY_SIZE; i++) { ck_assert_uint_eq(disk_encrypt_key[i], 0); } for (i = 0; i < ENCRYPT_NONCE_SIZE; i++) { ck_assert_uint_eq(disk_encrypt_nonce[i], 0); } +#endif } END_TEST @@ -481,24 +553,274 @@ START_TEST(test_update_disk_rejects_partition_mismatch) } END_TEST +#ifdef WOLFBOOT_PERSIST_FAILURE_STATUS +START_TEST(test_update_disk_records_unreadable_headers) +{ + reset_mocks(); + reset_records(); + build_image(part_a_image, 7, 0xA1); + build_image(part_b_image, 7, 0xB2); + mock_fail_all_reads = 1; + + wolfBoot_start(); + + ck_assert_int_gt(wolfBoot_panicked, 0); + /* The mock panic returns, so the retry loop adds records after these */ + ck_assert_int_ge(mock_record_count, 3); + ck_assert_uint_eq(mock_records[0].cause, WOLFBOOT_FAILURE_CAUSE_READ); + ck_assert_uint_eq(mock_records[0].partition, 0); + ck_assert_uint_eq(mock_records[1].cause, WOLFBOOT_FAILURE_CAUSE_READ); + ck_assert_uint_eq(mock_records[1].partition, 1); + ck_assert_uint_eq(mock_records[2].phase, WOLFBOOT_FAILURE_PHASE_RECOVERY); + ck_assert_uint_eq(mock_records[2].cause, WOLFBOOT_FAILURE_CAUSE_READ); +} +END_TEST + +START_TEST(test_update_disk_records_blank_headers) +{ + reset_mocks(); + reset_records(); + memset(part_a_image, 0, sizeof(part_a_image)); + memset(part_b_image, 0, sizeof(part_b_image)); + + wolfBoot_start(); + + ck_assert_int_gt(wolfBoot_panicked, 0); + /* Empty slots are not failures; only the final record is written */ + ck_assert_int_ge(mock_record_count, 1); + ck_assert_uint_eq(mock_records[0].phase, WOLFBOOT_FAILURE_PHASE_RECOVERY); + ck_assert_uint_eq(mock_records[0].cause, WOLFBOOT_FAILURE_CAUSE_HEADER); +} +END_TEST + +START_TEST(test_update_disk_records_unreadable_slot_when_other_boots) +{ + reset_mocks(); + reset_records(); + build_image(part_a_image, 7, 0xA1); + build_image(part_b_image, 5, 0xB2); + mock_fail_header_part = BOOT_PART_A; + + wolfBoot_start(); + + ck_assert_int_eq(mock_do_boot_called, 1); + ck_assert_int_eq(mock_record_count, 1); + ck_assert_uint_eq(mock_records[0].phase, WOLFBOOT_FAILURE_PHASE_BOOT); + ck_assert_uint_eq(mock_records[0].cause, WOLFBOOT_FAILURE_CAUSE_READ); + ck_assert_uint_eq(mock_records[0].partition, 0); +} +END_TEST + +START_TEST(test_update_disk_records_corrupt_header_when_other_boots) +{ + reset_mocks(); + reset_records(); + memset(part_a_image, 0x5A, IMAGE_HEADER_SIZE); + build_image(part_b_image, 5, 0xB2); + + wolfBoot_start(); + + ck_assert_int_eq(mock_do_boot_called, 1); + ck_assert_int_eq(mock_record_count, 1); + ck_assert_uint_eq(mock_records[0].partition, 0); +#ifdef DISK_ENCRYPT + ck_assert_uint_eq(mock_records[0].cause, WOLFBOOT_FAILURE_CAUSE_DECRYPT); +#else + ck_assert_uint_eq(mock_records[0].cause, WOLFBOOT_FAILURE_CAUSE_HEADER); +#endif +} +END_TEST + +START_TEST(test_update_disk_records_blank_slot_nothing) +{ + reset_mocks(); + reset_records(); + memset(part_a_image, 0xFF, IMAGE_HEADER_SIZE); + build_image(part_b_image, 5, 0xB2); + + wolfBoot_start(); + + ck_assert_int_eq(mock_do_boot_called, 1); + ck_assert_int_eq(mock_record_count, 0); +} +END_TEST + +/* A slot already recorded by the first header read is not recorded again + * when the retry loop falls back to it */ +START_TEST(test_update_disk_records_probe_failure_once) +{ + int i, a_boot = 0; + + reset_mocks(); + reset_records(); + build_image(part_a_image, 7, 0xA1); + build_image(part_b_image, 5, 0xB2); + mock_fail_header_part = BOOT_PART_A; + mock_verify_authenticity_ret = -1; + + wolfBoot_start(); + + for (i = 0; (i < mock_record_count) && (i < MOCK_MAX_RECORDS); i++) { + if ((mock_records[i].phase == WOLFBOOT_FAILURE_PHASE_BOOT) && + (mock_records[i].partition == 0)) + a_boot++; + } + ck_assert_int_eq(a_boot, 1); +} +END_TEST + +START_TEST(test_update_disk_records_refused_slot) +{ + reset_mocks(); + reset_records(); + build_image(part_a_image, 7, 0xA1); + build_image(part_b_image, 7, 0xB2); + mock_part_mismatch_part = BOOT_PART_A; + + wolfBoot_start(); + + ck_assert_int_eq(mock_do_boot_called, 1); + ck_assert_int_eq(mock_record_count, 1); + ck_assert_uint_eq(mock_records[0].phase, WOLFBOOT_FAILURE_PHASE_BOOT); + ck_assert_uint_eq(mock_records[0].cause, WOLFBOOT_FAILURE_CAUSE_HEADER); + ck_assert_uint_eq(mock_records[0].partition, 0); + ck_assert_uint_eq(mock_records[0].fw_version, 7); +} +END_TEST + +START_TEST(test_update_disk_records_signature_and_recovery) +{ + int last; + + reset_mocks(); + reset_records(); + build_image(part_a_image, 7, 0xA1); + build_image(part_b_image, 7, 0xB2); + mock_verify_authenticity_ret = -1; + + wolfBoot_start(); + + ck_assert_int_gt(wolfBoot_panicked, 0); + ck_assert_int_ge(mock_record_count, 3); + ck_assert_int_le(mock_record_count, MOCK_MAX_RECORDS); + ck_assert_uint_eq(mock_records[0].cause, WOLFBOOT_FAILURE_CAUSE_SIGNATURE); + ck_assert_uint_eq(mock_records[0].partition, 0); + ck_assert_uint_eq(mock_records[1].cause, WOLFBOOT_FAILURE_CAUSE_SIGNATURE); + ck_assert_uint_eq(mock_records[1].partition, 1); + last = mock_record_count - 1; + ck_assert_uint_eq(mock_records[last].phase, + WOLFBOOT_FAILURE_PHASE_RECOVERY); + ck_assert_uint_eq(mock_records[last].cause, + WOLFBOOT_FAILURE_CAUSE_SIGNATURE); + /* The slot whose refusal is repeated, not the one tried next */ + ck_assert_uint_eq(mock_records[last].partition, + mock_records[last - 1].partition); +} +END_TEST + +START_TEST(test_update_disk_records_hash_failure) +{ + reset_mocks(); + reset_records(); + build_image(part_a_image, 7, 0xA1); + build_image(part_b_image, 7, 0xB2); + mock_verify_integrity_ret = -1; + + wolfBoot_start(); + + ck_assert_int_gt(wolfBoot_panicked, 0); + ck_assert_int_ge(mock_record_count, 1); + ck_assert_uint_eq(mock_records[0].cause, WOLFBOOT_FAILURE_CAUSE_HASH); +} +END_TEST + +START_TEST(test_update_disk_records_refused_rollback) +{ + reset_mocks(); + reset_records(); + build_image(part_a_image, 7, 0xA1); + build_image(part_b_image, 5, 0xB2); + mock_fail_payload_part = BOOT_PART_A; + + wolfBoot_start(); + + ck_assert_int_gt(wolfBoot_panicked, 0); + ck_assert_int_eq(mock_record_count, 3); + ck_assert_uint_eq(mock_records[0].cause, WOLFBOOT_FAILURE_CAUSE_READ); + ck_assert_uint_eq(mock_records[0].partition, 0); + ck_assert_uint_eq(mock_records[0].fw_version, 7); + ck_assert_uint_eq(mock_records[1].cause, WOLFBOOT_FAILURE_CAUSE_VERSION); + ck_assert_uint_eq(mock_records[1].partition, 1); + ck_assert_uint_eq(mock_records[1].fw_version, 5); + ck_assert_uint_eq(mock_records[2].phase, WOLFBOOT_FAILURE_PHASE_RECOVERY); + ck_assert_uint_eq(mock_records[2].cause, WOLFBOOT_FAILURE_CAUSE_VERSION); + ck_assert_uint_eq(mock_records[2].partition, 1); +} +END_TEST + +/* An empty slot tried after the other fails is still not a failure */ +START_TEST(test_update_disk_records_blank_fallback_nothing) +{ + int i; + + reset_mocks(); + reset_records(); + build_image(part_a_image, 7, 0xA1); + memset(part_b_image, 0xFF, sizeof(part_b_image)); + mock_fail_payload_part = BOOT_PART_A; + + wolfBoot_start(); + + ck_assert_int_gt(wolfBoot_panicked, 0); + ck_assert_int_ge(mock_record_count, 2); + for (i = 0; (i < mock_record_count) && (i < MOCK_MAX_RECORDS); i++) { + ck_assert_uint_ne(mock_records[i].partition, 1); + } + ck_assert_uint_eq(mock_records[0].cause, WOLFBOOT_FAILURE_CAUSE_READ); + i = mock_record_count - 1; + ck_assert_uint_eq(mock_records[i].phase, WOLFBOOT_FAILURE_PHASE_RECOVERY); + ck_assert_uint_eq(mock_records[i].cause, WOLFBOOT_FAILURE_CAUSE_READ); +} +END_TEST +#endif + Suite *wolfboot_suite(void) { Suite *s = suite_create("wolfBoot"); TCase *tc = tcase_create("update-disk"); +#ifndef UNIT_DISK_PLAIN tcase_add_test(tc, test_update_disk_zeroizes_key_material_on_panic); +#endif +#ifndef UNIT_DISK_PLAIN tcase_add_test(tc, test_update_disk_zeroizes_key_material_before_boot); +#endif tcase_add_test(tc, test_update_disk_prefers_primary_partition_when_versions_equal); tcase_add_test(tc, test_update_disk_boots_from_A_when_B_is_blank); tcase_add_test(tc, test_update_disk_boots_from_B_when_A_is_blank); tcase_add_test(tc, test_update_disk_marks_staged_image_not_external); +#ifndef UNIT_DISK_PLAIN tcase_add_test(tc, test_get_decrypted_blob_version_rejects_truncated_version_tlv); +#endif tcase_add_test(tc, test_update_disk_highbit_version_selects_higher_partition); tcase_add_test(tc, test_update_disk_rejects_rollback_after_higher_image_failure); tcase_add_test(tc, test_update_disk_rejects_invalid_integrity); tcase_add_test(tc, test_update_disk_rejects_invalid_signature); tcase_add_test(tc, test_update_disk_skips_partition_mismatch); tcase_add_test(tc, test_update_disk_rejects_partition_mismatch); +#ifdef WOLFBOOT_PERSIST_FAILURE_STATUS + tcase_add_test(tc, test_update_disk_records_refused_slot); + tcase_add_test(tc, test_update_disk_records_signature_and_recovery); + tcase_add_test(tc, test_update_disk_records_hash_failure); + tcase_add_test(tc, test_update_disk_records_refused_rollback); + tcase_add_test(tc, test_update_disk_records_blank_fallback_nothing); + tcase_add_test(tc, test_update_disk_records_unreadable_headers); + tcase_add_test(tc, test_update_disk_records_blank_headers); + tcase_add_test(tc, test_update_disk_records_unreadable_slot_when_other_boots); + tcase_add_test(tc, test_update_disk_records_corrupt_header_when_other_boots); + tcase_add_test(tc, test_update_disk_records_blank_slot_nothing); + tcase_add_test(tc, test_update_disk_records_probe_failure_once); +#endif suite_add_tcase(s, tc); return s;