Skip to content

Commit 4264222

Browse files
committed
[rom_ext] Fix reporting of primary_bl0_slot
The `primary_bl0_slot` can be reported incorrectly when a `SetNextBl0` command is issued. 1. Initialize `boot_log->primary_bl0_slot` before boot services so that a temporary override doesn't get reported as the new primary. 2. Update the "set next bl0" test to verify that the primary_bl0_slot didn't change. Signed-off-by: Chris Frantz <[email protected]> (cherry picked from commit b0b385b) (cherry picked from commit cc64af9)
1 parent 5e9f15f commit 4264222

File tree

3 files changed

+21
-5
lines changed

3 files changed

+21
-5
lines changed

sw/device/silicon_creator/rom_ext/e2e/boot_svc/boot_svc_next_test.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ static status_t check_side_b(retention_sram_t *retram,
3131
TRY_CHECK(msg.header.type == kBootSvcNextBl0SlotResType);
3232
TRY_CHECK(msg.next_boot_bl0_slot_res.status == kErrorOk);
3333
TRY_CHECK(state->current_side == 'B');
34+
TRY_CHECK(state->primary_side == 'A');
35+
TRY_CHECK(msg.next_boot_bl0_slot_res.primary_bl0_slot == kBootSlotA);
3436
state->state = kBootSvcTestStateReturnSideA;
3537
rstmgr_reset();
3638
return INTERNAL();
@@ -39,6 +41,7 @@ static status_t check_side_b(retention_sram_t *retram,
3941
static status_t check_return_side_a(retention_sram_t *retram,
4042
boot_svc_retram_t *state) {
4143
TRY_CHECK(state->current_side == 'A');
44+
TRY_CHECK(state->primary_side == 'A');
4245
state->state = kBootSvcTestStateFinal;
4346
return OK_STATUS();
4447
}

sw/device/silicon_creator/rom_ext/e2e/boot_svc/boot_svc_primary_test.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ static status_t check_side_b(retention_sram_t *retram,
3232
TRY(boot_svc_header_check(&msg.header));
3333
TRY_CHECK(msg.header.type == kBootSvcNextBl0SlotResType);
3434
TRY_CHECK(msg.next_boot_bl0_slot_res.status == kErrorOk);
35+
TRY_CHECK(msg.next_boot_bl0_slot_res.primary_bl0_slot == kBootSlotB);
3536
TRY_CHECK(state->current_side == 'B');
3637
TRY_CHECK(state->primary_side == 'B');
3738
if (state->boots == 4) {
@@ -48,8 +49,12 @@ static status_t check_side_b(retention_sram_t *retram,
4849

4950
static status_t check_return_side_a(retention_sram_t *retram,
5051
boot_svc_retram_t *state) {
52+
boot_svc_msg_t msg = retram->creator.boot_svc_msg;
53+
TRY(boot_svc_header_check(&msg.header));
54+
TRY_CHECK(msg.header.type == kBootSvcNextBl0SlotResType);
5155
TRY_CHECK(state->current_side == 'A');
5256
TRY_CHECK(state->primary_side == 'A');
57+
TRY_CHECK(msg.next_boot_bl0_slot_res.primary_bl0_slot == kBootSlotA);
5358
state->state = kBootSvcTestStateFinal;
5459
return OK_STATUS();
5560
}

sw/device/silicon_creator/rom_ext/rom_ext.c

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,8 @@ static rom_error_t rom_ext_boot(boot_data_t *boot_data,
384384

385385
OT_WARN_UNUSED_RESULT
386386
static rom_error_t boot_svc_next_boot_bl0_slot_handler(
387-
boot_svc_msg_t *boot_svc_msg, boot_data_t *boot_data) {
387+
boot_svc_msg_t *boot_svc_msg, boot_data_t *boot_data,
388+
boot_log_t *boot_log) {
388389
uint32_t active_slot = boot_data->primary_bl0_slot;
389390
uint32_t primary_slot = boot_svc_msg->next_boot_bl0_slot_req.primary_bl0_slot;
390391
rom_error_t error = kErrorOk;
@@ -401,6 +402,8 @@ static rom_error_t boot_svc_next_boot_bl0_slot_handler(
401402
// Read the boot data back to ensure the correct slot is booted this
402403
// time.
403404
HARDENED_RETURN_IF_ERROR(boot_data_read(lc_state, boot_data));
405+
// Update the boot log.
406+
boot_log->primary_bl0_slot = boot_data->primary_bl0_slot;
404407
break;
405408
case kBootSlotUnspecified:
406409
// Do nothing.
@@ -487,7 +490,8 @@ static rom_error_t boot_svc_min_sec_ver_handler(boot_svc_msg_t *boot_svc_msg,
487490
}
488491

489492
OT_WARN_UNUSED_RESULT
490-
static rom_error_t handle_boot_svc(boot_data_t *boot_data) {
493+
static rom_error_t handle_boot_svc(boot_data_t *boot_data,
494+
boot_log_t *boot_log) {
491495
boot_svc_msg_t *boot_svc_msg = &retention_sram_get()->creator.boot_svc_msg;
492496
// TODO(lowRISC#22387): Examine the boot_svc code paths for boot loops.
493497
if (boot_svc_msg->header.identifier == kBootSvcIdentifier) {
@@ -500,7 +504,8 @@ static rom_error_t handle_boot_svc(boot_data_t *boot_data) {
500504
break;
501505
case kBootSvcNextBl0SlotReqType:
502506
HARDENED_CHECK_EQ(msg_type, kBootSvcNextBl0SlotReqType);
503-
return boot_svc_next_boot_bl0_slot_handler(boot_svc_msg, boot_data);
507+
return boot_svc_next_boot_bl0_slot_handler(boot_svc_msg, boot_data,
508+
boot_log);
504509
case kBootSvcMinBl0SecVerReqType:
505510
HARDENED_CHECK_EQ(msg_type, kBootSvcMinBl0SecVerReqType);
506511
return boot_svc_min_sec_ver_handler(boot_svc_msg, boot_data);
@@ -582,6 +587,10 @@ static rom_error_t rom_ext_start(boot_data_t *boot_data, boot_log_t *boot_log) {
582587
boot_log->rom_ext_major = self->version_major;
583588
boot_log->rom_ext_minor = self->version_minor;
584589
boot_log->rom_ext_size = CHIP_ROM_EXT_SIZE_MAX;
590+
// Even though `primary_bl0_slot` can be changed by boot svc, we initialize
591+
// it here so the "SetNextBl0" can do a one-time override of the RAM copy
592+
// of `boot_data`.
593+
boot_log->primary_bl0_slot = boot_data->primary_bl0_slot;
585594

586595
// Initialize the chip ownership state.
587596
rom_error_t error;
@@ -602,7 +611,7 @@ static rom_error_t rom_ext_start(boot_data_t *boot_data, boot_log_t *boot_log) {
602611
uint32_t reset_reasons = retention_sram_get()->creator.reset_reasons;
603612
uint32_t skip_boot_svc = reset_reasons & (1 << kRstmgrReasonLowPowerExit);
604613
if (skip_boot_svc == 0) {
605-
error = handle_boot_svc(boot_data);
614+
error = handle_boot_svc(boot_data, boot_log);
606615
if (error == kErrorWriteBootdataThenReboot) {
607616
// Boot services reports errors by writing a status code into the reply
608617
// messages. Regardless of whether a boot service request produced an
@@ -618,7 +627,6 @@ static rom_error_t rom_ext_start(boot_data_t *boot_data, boot_log_t *boot_log) {
618627
boot_log->ownership_transfers = boot_data->ownership_transfers;
619628
boot_log->rom_ext_min_sec_ver = boot_data->min_security_version_rom_ext;
620629
boot_log->bl0_min_sec_ver = boot_data->min_security_version_bl0;
621-
boot_log->primary_bl0_slot = boot_data->primary_bl0_slot;
622630
boot_log_digest_update(boot_log);
623631

624632
if (uart_break_detect(kRescueDetectTime) == kHardenedBoolTrue) {

0 commit comments

Comments
 (0)