Skip to content

Commit

Permalink
[sw] Simplify dbg_printf in immutable rom ext
Browse files Browse the repository at this point in the history
Since immutable rom_ext only prints string literals, this commit replaced the
`dbg_printf` by a simplified `dbg_puts` version. It saves about 480 Bytes.

Change-Id: Ic214aa8e2e69973f18302948311324d6556d8cf1
Signed-off-by: Yi-Hsuan Deng <[email protected]>
  • Loading branch information
sasdf committed Feb 11, 2025
1 parent f3b2425 commit 4b8ee5d
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 7 deletions.
2 changes: 1 addition & 1 deletion sw/device/silicon_creator/imm_rom_ext/imm_rom_ext.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ static rom_error_t imm_rom_ext_start(void) {
pinmux_init_uart0_tx();
uart_init(kUartNCOValue);

dbg_printf("IMM_ROM_EXT:0.1\r\n");
dbg_puts("IMM_ROM_EXT:0.1\r\n");

// Establish our identity.
const manifest_t *rom_ext = rom_ext_manifest();
Expand Down
9 changes: 4 additions & 5 deletions sw/device/silicon_creator/lib/cert/dice_chain.c
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ rom_error_t dice_chain_attestation_silicon(void) {
//
// In both cases, we do nothing, and boot normally, later attestation
// attempts will fail in a detectable manner.
dbg_printf("Warning: UDS certificate not valid.\r\n");
dbg_puts("Warning: UDS certificate not valid.\r\n");
} else {
// Cert is valid, move to the next one.
dice_chain_next_cert_obj();
Expand Down Expand Up @@ -334,7 +334,7 @@ rom_error_t dice_chain_attestation_creator(
// Check if the current CDI_0 cert is valid.
RETURN_IF_ERROR(dice_chain_load_cert_obj("CDI_0", /*name_size=*/6));
if (dice_chain.cert_valid == kHardenedBoolFalse) {
dbg_printf("CDI_0 certificate not valid. Updating it ...\r\n");
dbg_puts("CDI_0 certificate not valid. Updating it ...\r\n");
// Update the cert page buffer.
size_t updated_cert_size = kScratchCertSizeBytes;
HARDENED_RETURN_IF_ERROR(
Expand Down Expand Up @@ -398,7 +398,7 @@ rom_error_t dice_chain_attestation_owner(
// Check if the current CDI_0 cert is valid.
RETURN_IF_ERROR(dice_chain_load_cert_obj("CDI_1", /*name_size=*/6));
if (dice_chain.cert_valid == kHardenedBoolFalse) {
dbg_printf("CDI_1 certificate not valid. Updating it ...\r\n");
dbg_puts("CDI_1 certificate not valid. Updating it ...\r\n");
// Update the cert page buffer.
size_t updated_cert_size = kScratchCertSizeBytes;
// TODO(#19596): add owner configuration block measurement to CDI_1 cert.
Expand Down Expand Up @@ -438,8 +438,7 @@ rom_error_t dice_chain_flush_flash(void) {
/*offset=*/0,
/*word_count=*/FLASH_CTRL_PARAM_BYTES_PER_PAGE / sizeof(uint32_t),
dice_chain.data));
dbg_printf("Flushed dice cert page %d\r\n",
dice_chain.info_page->base_addr);
dbg_puts("Flushed dice cert page\r\n");
dice_chain.data_dirty = kHardenedBoolFalse;
}
return kErrorOk;
Expand Down
6 changes: 6 additions & 0 deletions sw/device/silicon_creator/lib/dbg_print.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ static void print_integer(unsigned value, bool is_signed) {
}
}

void dbg_puts(const char *str) {
while (*str) {
uart_putchar(*str++);
}
}

void dbg_printf(const char *format, ...) {
va_list args;
va_start(args, format);
Expand Down
8 changes: 8 additions & 0 deletions sw/device/silicon_creator/lib/dbg_print.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,14 @@ extern "C" {
*/
void dbg_printf(const char *format, ...) __attribute__((format(printf, 1, 2)));

/**
* Print the string to the console.
*
* Note: `dbg_puts` will NOT output an additional newline character '\n' after
* `str`, unlike the standard C puts.
*/
void dbg_puts(const char *str);

/**
* Print the ePMP configuration to the console.
*/
Expand Down
11 changes: 11 additions & 0 deletions sw/device/silicon_creator/lib/dbg_print_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,17 @@ TEST(LogTest, PrintfString) {
EXPECT_EQ(*uart_buf, "ABC");
}

TEST(LogTest, PutsString) {
uart_buf->clear();
dbg_puts("Hello, %s!");
// It should NOT be formated.
EXPECT_EQ(*uart_buf, "Hello, %s!");

uart_buf->clear();
dbg_puts("OpenTitan");
EXPECT_EQ(*uart_buf, "OpenTitan");
}

TEST(LogTest, PrintfMix) {
uart_buf->clear();
dbg_printf("%s%x", "OpenTitan", 0x0000000a);
Expand Down
2 changes: 1 addition & 1 deletion sw/device/silicon_creator/lib/otbn_boot_services.c
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ static rom_error_t load_attestation_keygen_seed(uint32_t additional_seed_idx,
// If we encountered a read error, this means the attestation seed page
// has not been provisioned yet. In this case, we erase the page and
// continue, which will simply result in generating an invalid identity.
dbg_printf(
dbg_puts(
"Warning: Attestation key seed flash info page not provisioned. "
"Erasing page to format.\r\n");
HARDENED_RETURN_IF_ERROR(flash_ctrl_info_erase(
Expand Down

0 comments on commit 4b8ee5d

Please sign in to comment.