Skip to content

Commit

Permalink
[boot_svc] Handle empty messages like other boot_svc messages
Browse files Browse the repository at this point in the history
1. Add a `BootSvcEmptyResType` to represent empty message responses.
2. Create an empty response message that is the request message with
   only the `type` changed to the response type.
3. Handle the empty request in the ROM_EXT in a similar way to all other
   boot service messages.
4. Update the `empty_test` to confirm that the request message results
   in a response message.

Signed-off-by: Chris Frantz <[email protected]>
(cherry picked from commit c8a493f)
  • Loading branch information
cfrantz committed Aug 16, 2024
1 parent 5324701 commit bfd4ce6
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 16 deletions.
15 changes: 11 additions & 4 deletions sw/device/silicon_creator/lib/boot_svc/boot_svc_empty.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,22 @@

#include "sw/device/silicon_creator/lib/boot_svc/boot_svc_empty.h"

void boot_svc_empty_init(boot_svc_empty_t *msg) {
size_t i = 0, j = kBootSvcEmptyPayloadWordCount - 1;
void boot_svc_empty_req_init(boot_svc_empty_t *msg) {
// We use `uint32_t` instead of `size_t` so that end-of-loop check passes both
// on- and off-target tests.
uint32_t i = 0, j = kBootSvcEmptyPayloadWordCount - 1;
for (; launder32(i) < kBootSvcEmptyPayloadWordCount &&
launder32(j) < kBootSvcEmptyPayloadWordCount;
++i, --j) {
msg->payload[i] = 0;
}
HARDENED_CHECK_EQ(i, kBootSvcEmptyPayloadWordCount);
HARDENED_CHECK_EQ(j, SIZE_MAX);
boot_svc_header_finalize(kBootSvcEmptyType, sizeof(boot_svc_empty_t),
HARDENED_CHECK_EQ(j, UINT32_MAX);
boot_svc_header_finalize(kBootSvcEmptyReqType, sizeof(boot_svc_empty_t),
&msg->header);
}

void boot_svc_empty_res_init(boot_svc_empty_t *msg) {
boot_svc_header_finalize(kBootSvcEmptyResType, sizeof(boot_svc_empty_t),
&msg->header);
}
15 changes: 12 additions & 3 deletions sw/device/silicon_creator/lib/boot_svc/boot_svc_empty.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ extern "C" {

enum {
/** Empty boot services request: `EMPT`. */
kBootSvcEmptyType = 0x54504d45,
kBootSvcEmptyReqType = 0x54504d45,
/** Empty boot services response: `TPME`. */
kBootSvcEmptyResType = 0x454d5054,
kBootSvcEmptyPayloadWordCount =
CHIP_BOOT_SVC_MSG_PAYLOAD_SIZE_MAX / sizeof(uint32_t),
};
Expand Down Expand Up @@ -48,11 +50,18 @@ OT_ASSERT_MEMBER_OFFSET(boot_svc_empty_t, payload,
OT_ASSERT_SIZE(boot_svc_empty_t, CHIP_BOOT_SVC_MSG_SIZE_MAX);

/**
* Initialize an empty boot services message.
* Initialize an empty boot services request.
*
* @param[out] msg Output buffer for the message.
*/
void boot_svc_empty_init(boot_svc_empty_t *msg);
void boot_svc_empty_req_init(boot_svc_empty_t *msg);

/**
* Initialize an empty boot services response.
*
* @param[inout] msg Buffer for the message.
*/
void boot_svc_empty_res_init(boot_svc_empty_t *msg);

#ifdef __cplusplus
} // extern "C"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ TEST_F(BootSvcEmptyTest, Init) {

boot_svc_empty_t msg{};
EXPECT_CALL(boot_svc_header_,
Finalize(kBootSvcEmptyType, sizeof(msg), &msg.header));
Finalize(kBootSvcEmptyReqType, sizeof(msg), &msg.header));

boot_svc_empty_init(&msg);
boot_svc_empty_req_init(&msg);

EXPECT_THAT(msg.payload, ElementsAreArray(payload));
}
Expand Down
4 changes: 2 additions & 2 deletions sw/device/silicon_creator/rom_ext/e2e/boot_svc/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -124,12 +124,12 @@ opentitan_test(
--exec="fpga clear-bitstream"
--exec="fpga load-bitstream {bitstream}"
--exec="bootstrap --clear-uart=true {firmware}"
--exec="console --non-interactive --exit-success='ownership_state = LockedNone\r\n' --exit-failure='{exit_failure}'"
--exec="console --non-interactive --exit-success='ownership_state = .x00.x00.x00.x00\r\n' --exit-failure='{exit_failure}'"
--exec="rescue boot-svc ownership-unlock \
--mode Any \
--nonce 0 \
--sign $(location //sw/device/silicon_creator/lib/ownership/keys/fake:no_owner_recovery_key)"
--exec="console --non-interactive --exit-success='ownership_state = UnlockedAny\r\n' --exit-failure='{exit_failure}'"
--exec="console --non-interactive --exit-success='ownership_state = UANY\r\n' --exit-failure='{exit_failure}'"
no-op
""",
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ OTTF_DEFINE_TEST_CONFIG();

static status_t initialize(retention_sram_t *retram, boot_svc_retram_t *state) {
boot_svc_msg_t msg = {0};
boot_svc_empty_init(&msg.empty);
boot_svc_empty_req_init(&msg.empty);
retram->creator.boot_svc_msg = msg;
state->state = kBootSvcTestStateCheckEmpty;
rstmgr_reset();
Expand All @@ -27,7 +27,7 @@ static status_t check_empty(retention_sram_t *retram,
boot_svc_retram_t *state) {
boot_svc_msg_t msg = retram->creator.boot_svc_msg;
TRY(boot_svc_header_check(&msg.header));
TRY_CHECK(msg.header.type == kBootSvcEmptyType);
TRY_CHECK(msg.header.type == kBootSvcEmptyResType);
state->state = kBootSvcTestStateFinal;
return OK_STATUS();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ OTTF_DEFINE_TEST_CONFIG();

static status_t initialize(retention_sram_t *retram, boot_svc_retram_t *state) {
boot_svc_msg_t msg = {0};
boot_svc_empty_init(&msg.empty);
boot_svc_min_bl0_sec_ver_req_init(2, &msg.min_bl0_sec_ver_req);
retram->creator.boot_svc_msg = msg;
state->state = kBootSvcTestStateMinSecAdvance;
Expand Down
6 changes: 4 additions & 2 deletions sw/device/silicon_creator/rom_ext/rom_ext.c
Original file line number Diff line number Diff line change
Expand Up @@ -757,8 +757,9 @@ static rom_error_t handle_boot_svc(boot_data_t *boot_data) {
HARDENED_RETURN_IF_ERROR(boot_svc_header_check(&boot_svc_msg->header));
uint32_t msg_type = boot_svc_msg->header.type;
switch (launder32(msg_type)) {
case kBootSvcEmptyType:
HARDENED_CHECK_EQ(msg_type, kBootSvcEmptyType);
case kBootSvcEmptyReqType:
HARDENED_CHECK_EQ(msg_type, kBootSvcEmptyReqType);
boot_svc_empty_res_init(&boot_svc_msg->empty);
break;
case kBootSvcNextBl0SlotReqType:
HARDENED_CHECK_EQ(msg_type, kBootSvcNextBl0SlotReqType);
Expand All @@ -772,6 +773,7 @@ static rom_error_t handle_boot_svc(boot_data_t *boot_data) {
case kBootSvcOwnershipUnlockReqType:
HARDENED_CHECK_EQ(msg_type, kBootSvcOwnershipUnlockReqType);
return ownership_unlock_handler(boot_svc_msg, boot_data);
case kBootSvcEmptyResType:
case kBootSvcNextBl0SlotResType:
case kBootSvcPrimaryBl0SlotResType:
case kBootSvcMinBl0SecVerResType:
Expand Down

0 comments on commit bfd4ce6

Please sign in to comment.