Skip to content

Commit 6e81e34

Browse files
committed
boot/nxboot: add flush barriers and CRC-validate primary before boot
Two hardening fixes for nxboot power-loss resilience: 1. Add flash_partition_flush() calls between critical partition operations in perform_update(). Without explicit flush barriers, writes may remain buffered in RAM (e.g. via FTL rwbuffer) when nxboot proceeds to the next phase. A power loss between phases can leave the recovery image uncommitted while the staging partition has already been consumed. Flush points added: - After copy_partition(primary, recovery) completes - After copy_partition(update, primary) completes, before erasing the staging first sector 2. Replace validate_image_header() with validate_image() in the final primary validation path of nxboot_perform_update(). The header-only check validates magic and platform identifier but does not CRC-check the image body. After an interrupted update, a corrupt primary with an intact header would pass this check and be booted, resulting in a persistent boot failure. Signed-off-by: Neil Berkman <neil@xuku.com>
1 parent 8cd9e9a commit 6e81e34

3 files changed

Lines changed: 40 additions & 2 deletions

File tree

boot/nxboot/loader/boot.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -417,6 +417,7 @@ static int perform_update(struct nxboot_state *state, bool check_only)
417417
syslog(LOG_INFO, "Creating recovery image.\n");
418418
nxboot_progress(nxboot_progress_start, recovery_create);
419419
copy_partition(primary, recovery, state, false);
420+
flash_partition_flush(recovery);
420421
nxboot_progress(nxboot_progress_end);
421422
nxboot_progress(nxboot_progress_start, validate_recovery);
422423
successful = validate_image(recovery);
@@ -444,6 +445,8 @@ static int perform_update(struct nxboot_state *state, bool check_only)
444445
nxboot_progress(nxboot_progress_start, update_from_update);
445446
if (copy_partition(update, primary, state, true) >= 0)
446447
{
448+
flash_partition_flush(primary);
449+
447450
/* Erase the first sector of update partition. This marks the
448451
* partition as updated so we don't end up in an update loop.
449452
* The sector is written back again during the image
@@ -919,8 +922,7 @@ int nxboot_perform_update(bool check_only)
919922
return ERROR;
920923
}
921924

922-
get_image_header(primary, &header);
923-
if (!validate_image_header(&header))
925+
if (!validate_image(primary, &header))
924926
{
925927
ret = ERROR;
926928
}

boot/nxboot/loader/flash.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,26 @@ int flash_partition_open(const char *path)
7676
return fd;
7777
}
7878

79+
/****************************************************************************
80+
* Name: flash_partition_flush
81+
*
82+
* Description:
83+
* Flushes any buffered writes to the underlying storage. This ensures
84+
* data is physically committed to flash before the caller proceeds.
85+
*
86+
* Input parameters:
87+
* fd: Valid file descriptor.
88+
*
89+
* Returned Value:
90+
* 0 on success, -1 on failure.
91+
*
92+
****************************************************************************/
93+
94+
int flash_partition_flush(int fd)
95+
{
96+
return fsync(fd);
97+
}
98+
7999
/****************************************************************************
80100
* Name: flash_partition_close
81101
*

boot/nxboot/loader/flash.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,22 @@ int flash_partition_open(const char *path);
7979

8080
int flash_partition_close(int fd);
8181

82+
/****************************************************************************
83+
* Name: flash_partition_flush
84+
*
85+
* Description:
86+
* Flushes any buffered writes to the underlying storage.
87+
*
88+
* Input parameters:
89+
* fd: Valid file descriptor.
90+
*
91+
* Returned Value:
92+
* 0 on success, -1 on failure.
93+
*
94+
****************************************************************************/
95+
96+
int flash_partition_flush(int fd);
97+
8298
/****************************************************************************
8399
* Name: flash_partition_write
84100
*

0 commit comments

Comments
 (0)