[mask_rom] Rename log.h -> rom_print.h
This file has nothing to do with the ordinary LOG macro; renaming it
makes this clearer.
This CL also simplifies its unit test to not use a mock.
Signed-off-by: Miguel Young de la Sota <mcyoung@google.com>
diff --git a/ci/scripts/mask-rom-tests.sh b/ci/scripts/mask-rom-tests.sh
index bc5598b..186e397 100755
--- a/ci/scripts/mask-rom-tests.sh
+++ b/ci/scripts/mask-rom-tests.sh
@@ -14,7 +14,7 @@
"//sw/device/silicon_creator/lib:error_unittest"
"//sw/device/silicon_creator/lib:epmp_unittest"
"//sw/device/silicon_creator/lib:manifest_unittest"
- "//sw/device/silicon_creator/lib:log_unittest"
+ "//sw/device/silicon_creator/lib:rom_print_unittest"
"//sw/device/silicon_creator/lib:shutdown_unittest"
"//sw/device/silicon_creator/lib/base:sec_mmio_unittest"
"//sw/device/silicon_creator/lib/drivers:uart_unittest"
diff --git a/sw/device/silicon_creator/lib/BUILD b/sw/device/silicon_creator/lib/BUILD
index ce1691a..496d4a0 100644
--- a/sw/device/silicon_creator/lib/BUILD
+++ b/sw/device/silicon_creator/lib/BUILD
@@ -154,9 +154,9 @@
)
cc_library(
- name = "log",
- srcs = ["log.c"],
- hdrs = ["log.h"],
+ name = "rom_print",
+ srcs = ["rom_print.c"],
+ hdrs = ["rom_print.h"],
deps = [
":error",
"//sw/device/silicon_creator/lib/drivers:uart",
@@ -164,14 +164,12 @@
)
cc_test(
- name = "log_unittest",
- srcs = ["log_unittest.cc"],
+ name = "rom_print_unittest",
+ srcs = ["rom_print_unittest.cc"],
deps = [
":error",
- ":log",
- "//sw/device/lib/base/testing:global_mock",
+ ":rom_print",
"//sw/device/silicon_creator/lib/drivers:uart",
- "//sw/device/silicon_creator/testing:mask_rom_test",
"@googletest//:gtest_main",
],
)
diff --git a/sw/device/silicon_creator/lib/log.c b/sw/device/silicon_creator/lib/log.c
deleted file mode 100644
index 8e46494..0000000
--- a/sw/device/silicon_creator/lib/log.c
+++ /dev/null
@@ -1,49 +0,0 @@
-// Copyright lowRISC contributors.
-// Licensed under the Apache License, Version 2.0, see LICENSE for details.
-// SPDX-License-Identifier: Apache-2.0
-
-#include "sw/device/silicon_creator/lib/log.h"
-
-#include <assert.h>
-#include <stdarg.h>
-#include <stdint.h>
-
-#include "sw/device/silicon_creator/lib/drivers/uart.h"
-
-rom_error_t log_printf(const char *format, ...) {
- va_list args;
- va_start(args, format);
-
- while (*format != '\0') {
- if (*format == '%') {
- ++format;
- switch (*format++) {
- case 's': {
- // Print a null-terminated string.
- const char *str = va_arg(args, const char *);
- while (*str != '\0') {
- uart_putchar(*str++);
- }
- continue;
- }
- case 'x': {
- // Print an `unsigned int` in hexadecimal.
- const char kHexTable[16] = "0123456789abcdef";
- unsigned int v = va_arg(args, unsigned int);
- static_assert(sizeof(unsigned int) == 4,
- "sizeof(unsigned int) is not 4");
- for (int32_t i = 28; i >= 0; i -= 4) {
- uart_putchar(kHexTable[(v >> i) & 0xf]);
- }
- continue;
- }
- default:
- return kErrorLogBadFormatSpecifier;
- }
- }
- uart_putchar(*format++);
- }
-
- va_end(args);
- return kErrorOk;
-}
diff --git a/sw/device/silicon_creator/lib/log_unittest.cc b/sw/device/silicon_creator/lib/log_unittest.cc
deleted file mode 100644
index 2ef30cb..0000000
--- a/sw/device/silicon_creator/lib/log_unittest.cc
+++ /dev/null
@@ -1,86 +0,0 @@
-// Copyright lowRISC contributors.
-// Licensed under the Apache License, Version 2.0, see LICENSE for details.
-// SPDX-License-Identifier: Apache-2.0
-
-#include "sw/device/silicon_creator/lib/log.h"
-
-#include "gtest/gtest.h"
-#include "sw/device/lib/base/testing/global_mock.h"
-#include "sw/device/silicon_creator/lib/drivers/uart.h"
-#include "sw/device/silicon_creator/lib/error.h"
-#include "sw/device/silicon_creator/testing/mask_rom_test.h"
-
-namespace log_unittest {
-
-namespace internal {
-// Create a mock for shutdown functions.
-// TODO: move mock UART into its own header file.
-class MockUart : public ::global_mock::GlobalMock<MockUart> {
- public:
- MOCK_METHOD(void, UartPutchar, (char));
-};
-} // namespace internal
-
-using MockUart = testing::StrictMock<internal::MockUart>;
-extern "C" {
-void uart_putchar(uint8_t c) { MockUart::Instance().UartPutchar(c); }
-} // extern "C"
-
-class LogTest : public mask_rom_test::MaskRomTest {
- protected:
- void ExpectPrint(const std::string &str) {
- for (char c : str) {
- EXPECT_CALL(uart_, UartPutchar(c));
- }
- }
- MockUart uart_;
-};
-
-TEST_F(LogTest, PrintfFormatOnly) {
- ExpectPrint("A");
- EXPECT_EQ(log_printf("A"), kErrorOk);
-
- ExpectPrint("1234567890\n");
- EXPECT_EQ(log_printf("1234567890\n"), kErrorOk);
-}
-
-TEST_F(LogTest, PrintfHex) {
- ExpectPrint("abcdef01");
- EXPECT_EQ(log_printf("%x", 0xabcdef01), kErrorOk);
-
- ExpectPrint("0x0102030405060708");
- EXPECT_EQ(log_printf("0x%x%x", 0x01020304, 0x05060708), kErrorOk);
-}
-
-TEST_F(LogTest, PrintfString) {
- ExpectPrint("Hello, World!");
- EXPECT_EQ(log_printf("Hello, %s!", "World"), kErrorOk);
-
- ExpectPrint("OpenTitan");
- EXPECT_EQ(log_printf("%s%s", "Open", "Titan"), kErrorOk);
-
- ExpectPrint("ABC");
- EXPECT_EQ(log_printf("%s%s%s%s%s", "A", "", "B", "", "C"), kErrorOk);
-}
-
-TEST_F(LogTest, PrintfMix) {
- ExpectPrint("OpenTitan0000000a");
- EXPECT_EQ(log_printf("%s%x", "OpenTitan", 0x0000000a), kErrorOk);
-}
-
-TEST_F(LogTest, BadFormatSpecifier) {
- // Disable compiler warnings about incorrect format strings so that we can
- // test them (works for both clang and GCC).
-#pragma GCC diagnostic push
-#pragma GCC diagnostic ignored "-Wformat"
- EXPECT_EQ(log_printf("%d", 0x0000000a), kErrorLogBadFormatSpecifier);
-
- ExpectPrint("abcd");
- EXPECT_EQ(log_printf("abcd%"), kErrorLogBadFormatSpecifier);
-
- ExpectPrint("abcd");
- EXPECT_EQ(log_printf("abcd%%"), kErrorLogBadFormatSpecifier);
-#pragma GCC diagnostic pop
-}
-
-} // namespace log_unittest
diff --git a/sw/device/silicon_creator/lib/meson.build b/sw/device/silicon_creator/lib/meson.build
index 57a9ce5..f06cdbf 100644
--- a/sw/device/silicon_creator/lib/meson.build
+++ b/sw/device/silicon_creator/lib/meson.build
@@ -9,7 +9,7 @@
link_with: static_library(
'sw_silicon_creator_lib_log',
sources: [
- 'log.c',
+ 'rom_print.c',
],
dependencies: [
sw_silicon_creator_lib_driver_uart,
@@ -219,8 +219,8 @@
test('sw_silicon_creator_lib_log_unittest', executable(
'sw_silicon_creator_lib_log_unittest',
sources: [
- 'log.c',
- 'log_unittest.cc',
+ 'rom_print.c',
+ 'rom_print_unittest.cc',
],
dependencies: [
sw_vendor_gtest,
diff --git a/sw/device/silicon_creator/lib/rom_print.c b/sw/device/silicon_creator/lib/rom_print.c
new file mode 100644
index 0000000..f7c7cf4
--- /dev/null
+++ b/sw/device/silicon_creator/lib/rom_print.c
@@ -0,0 +1,52 @@
+// Copyright lowRISC contributors.
+// Licensed under the Apache License, Version 2.0, see LICENSE for details.
+// SPDX-License-Identifier: Apache-2.0
+
+#include "sw/device/silicon_creator/lib/rom_print.h"
+
+#include <assert.h>
+#include <stdarg.h>
+#include <stdint.h>
+
+#include "sw/device/silicon_creator/lib/drivers/uart.h"
+
+rom_error_t rom_printf(const char *format, ...) {
+ va_list args;
+ va_start(args, format);
+
+ while (*format != '\0') {
+ if (*format != '%') {
+ uart_putchar(*format++);
+ continue;
+ }
+
+ ++format; // Skip over the '%'.
+ switch (*format++) {
+ case 's': {
+ // Print a null-terminated string.
+ const char *str = va_arg(args, const char *);
+ while (*str != '\0') {
+ uart_putchar(*str++);
+ }
+ break;
+ }
+ case 'x': {
+ // Print an `unsigned int` in hexadecimal.
+ static const char kHexTable[16] = "0123456789abcdef";
+ unsigned int v = va_arg(args, unsigned int);
+ for (int i = 0; i < sizeof(v) * 2; ++i) {
+ int shift = sizeof(v) * 8 - 4;
+ uart_putchar(kHexTable[v >> shift]);
+ v <<= 4;
+ }
+ break;
+ }
+ default:
+ va_end(args);
+ return kErrorLogBadFormatSpecifier;
+ }
+ }
+
+ va_end(args);
+ return kErrorOk;
+}
diff --git a/sw/device/silicon_creator/lib/log.h b/sw/device/silicon_creator/lib/rom_print.h
similarity index 77%
rename from sw/device/silicon_creator/lib/log.h
rename to sw/device/silicon_creator/lib/rom_print.h
index bffba32..9369d5d 100644
--- a/sw/device/silicon_creator/lib/log.h
+++ b/sw/device/silicon_creator/lib/rom_print.h
@@ -2,8 +2,8 @@
// Licensed under the Apache License, Version 2.0, see LICENSE for details.
// SPDX-License-Identifier: Apache-2.0
-#ifndef OPENTITAN_SW_DEVICE_SILICON_CREATOR_LIB_LOG_H_
-#define OPENTITAN_SW_DEVICE_SILICON_CREATOR_LIB_LOG_H_
+#ifndef OPENTITAN_SW_DEVICE_SILICON_CREATOR_LIB_ROM_PRINT_H_
+#define OPENTITAN_SW_DEVICE_SILICON_CREATOR_LIB_ROM_PRINT_H_
#include "sw/device/silicon_creator/lib/error.h"
@@ -12,8 +12,8 @@
#endif // __cplusplus
/**
- * Prints a status message to UART0, formatted according to the format
- * string `format`.
+ * An intentionally pared-down implementation of `printf()` that writes
+ * to UART0.
*
* This function only supports the format specifiers required by the
* mask ROM:
@@ -33,11 +33,11 @@
* @param ... The values to interpolate in the format.
* @return The result of the operation.
*/
-rom_error_t log_printf(const char *format, ...)
+rom_error_t rom_printf(const char *format, ...)
__attribute__((format(printf, 1, 2)));
#ifdef __cplusplus
} // extern "C"
#endif // __cplusplus
-#endif // OPENTITAN_SW_DEVICE_SILICON_CREATOR_LIB_LOG_H_
+#endif // OPENTITAN_SW_DEVICE_SILICON_CREATOR_LIB_ROM_PRINT_H_
diff --git a/sw/device/silicon_creator/lib/rom_print_unittest.cc b/sw/device/silicon_creator/lib/rom_print_unittest.cc
new file mode 100644
index 0000000..8ccfa29
--- /dev/null
+++ b/sw/device/silicon_creator/lib/rom_print_unittest.cc
@@ -0,0 +1,76 @@
+// Copyright lowRISC contributors.
+// Licensed under the Apache License, Version 2.0, see LICENSE for details.
+// SPDX-License-Identifier: Apache-2.0
+
+#include "sw/device/silicon_creator/lib/rom_print.h"
+
+#include "gtest/gtest.h"
+#include "sw/device/silicon_creator/lib/drivers/uart.h"
+#include "sw/device/silicon_creator/lib/error.h"
+
+namespace rom_printf_unittest {
+// We don't use a mock here since it'd be overkill; expectations are easier
+// to write on a global string, instead. This also produces a simpler error
+// message instead of a tower of failed expectations.
+static std::string *uart_buf = new std::string;
+extern "C" void uart_putchar(uint8_t c) { uart_buf->push_back(c); }
+
+TEST(LogTest, PrintfFormatOnly) {
+ uart_buf->clear();
+ EXPECT_EQ(rom_printf("A"), kErrorOk);
+ EXPECT_EQ(*uart_buf, "A");
+
+ uart_buf->clear();
+ EXPECT_EQ(rom_printf("1234567890\n"), kErrorOk);
+ EXPECT_EQ(*uart_buf, "1234567890\n");
+}
+
+TEST(LogTest, PrintfHex) {
+ uart_buf->clear();
+ EXPECT_EQ(rom_printf("%x", 0xabcdef01), kErrorOk);
+ EXPECT_EQ(*uart_buf, "abcdef01");
+
+ uart_buf->clear();
+ EXPECT_EQ(rom_printf("0x%x%x", 0x01020304, 0x05060708), kErrorOk);
+ EXPECT_EQ(*uart_buf, "0x0102030405060708");
+}
+
+TEST(LogTest, PrintfString) {
+ uart_buf->clear();
+ EXPECT_EQ(rom_printf("Hello, %s!", "World"), kErrorOk);
+ EXPECT_EQ(*uart_buf, "Hello, World!");
+
+ uart_buf->clear();
+ EXPECT_EQ(rom_printf("%s%s", "Open", "Titan"), kErrorOk);
+ EXPECT_EQ(*uart_buf, "OpenTitan");
+
+ uart_buf->clear();
+ EXPECT_EQ(rom_printf("%s%s%s%s%s", "A", "", "B", "", "C"), kErrorOk);
+ EXPECT_EQ(*uart_buf, "ABC");
+}
+
+TEST(LogTest, PrintfMix) {
+ uart_buf->clear();
+ EXPECT_EQ(rom_printf("%s%x", "OpenTitan", 0x0000000a), kErrorOk);
+ EXPECT_EQ(*uart_buf, "OpenTitan0000000a");
+}
+
+TEST(LogTest, BadFormatSpecifier) {
+ // Disable compiler warnings about incorrect format strings so that we can
+ // test them (works for both clang and GCC).
+#pragma GCC diagnostic push
+#pragma GCC diagnostic ignored "-Wformat"
+ uart_buf->clear();
+ EXPECT_EQ(rom_printf("%d", 0x0000000a), kErrorLogBadFormatSpecifier);
+
+ uart_buf->clear();
+ EXPECT_EQ(rom_printf("abcd%"), kErrorLogBadFormatSpecifier);
+ EXPECT_EQ(*uart_buf, "abcd");
+
+ uart_buf->clear();
+ EXPECT_EQ(rom_printf("abcd%%"), kErrorLogBadFormatSpecifier);
+ EXPECT_EQ(*uart_buf, "abcd");
+#pragma GCC diagnostic pop
+}
+
+} // namespace rom_printf_unittest
diff --git a/sw/device/silicon_creator/mask_rom/BUILD b/sw/device/silicon_creator/mask_rom/BUILD
index 5ae15ee..47b3f06 100644
--- a/sw/device/silicon_creator/mask_rom/BUILD
+++ b/sw/device/silicon_creator/mask_rom/BUILD
@@ -206,7 +206,7 @@
"//sw/device/lib/dif:spi_device",
"//sw/device/lib/testing/test_rom:bootstrap",
"//sw/device/silicon_creator/lib:error",
- "//sw/device/silicon_creator/lib:log",
+ "//sw/device/silicon_creator/lib:rom_print",
"//sw/device/silicon_creator/lib/base:sec_mmio",
"//sw/device/silicon_creator/lib/drivers:hmac",
"//sw/device/silicon_creator/lib/drivers:lifecycle",
diff --git a/sw/device/silicon_creator/mask_rom/primitive_bootstrap.c b/sw/device/silicon_creator/mask_rom/primitive_bootstrap.c
index 5c590f6..4d29d38 100644
--- a/sw/device/silicon_creator/mask_rom/primitive_bootstrap.c
+++ b/sw/device/silicon_creator/mask_rom/primitive_bootstrap.c
@@ -17,7 +17,7 @@
#include "sw/device/silicon_creator/lib/drivers/hmac.h"
#include "sw/device/silicon_creator/lib/drivers/watchdog.h"
#include "sw/device/silicon_creator/lib/error.h"
-#include "sw/device/silicon_creator/lib/log.h"
+#include "sw/device/silicon_creator/lib/rom_print.h"
#include "hw/top_earlgrey/sw/autogen/top_earlgrey.h"
@@ -149,7 +149,7 @@
if (frame_num == expected_frame_num) {
if (!check_frame_hash(&frame)) {
- log_printf("Detected hash mismatch on frame 0x%x\n\r",
+ rom_printf("Detected hash mismatch on frame 0x%x\n\r",
(unsigned int)frame_num);
RETURN_IF_ERROR(
spi_device_send((uint8_t *)&ack.digest, sizeof(ack.digest)));
@@ -173,7 +173,7 @@
++expected_frame_num;
if (SPIFLASH_FRAME_IS_EOF(frame.header.frame_num)) {
- log_printf("Bootstrap: DONE!\n\r");
+ rom_printf("Bootstrap: DONE!\n\r");
return kErrorOk;
}
} else {
@@ -187,7 +187,7 @@
}
static rom_error_t primitive_bootstrap_impl(void) {
- log_printf("Bootstrap: BEGIN\n\r");
+ rom_printf("Bootstrap: BEGIN\n\r");
flash_init_block();
RETURN_IF_ERROR(spi_device_init());
diff --git a/sw/device/silicon_creator/rom_ext/BUILD b/sw/device/silicon_creator/rom_ext/BUILD
index 4567c2f..803bf7a 100644
--- a/sw/device/silicon_creator/rom_ext/BUILD
+++ b/sw/device/silicon_creator/rom_ext/BUILD
@@ -104,7 +104,7 @@
"//sw/device/lib/base:macros",
"//sw/device/lib/base:stdasm",
"//sw/device/lib/runtime:hart",
- "//sw/device/silicon_creator/lib:log",
+ "//sw/device/silicon_creator/lib:rom_print",
"//sw/device/silicon_creator/lib:manifest",
"//sw/device/silicon_creator/lib:shutdown",
"//sw/device/silicon_creator/lib/base:sec_mmio",
diff --git a/sw/device/silicon_creator/rom_ext/rom_ext.c b/sw/device/silicon_creator/rom_ext/rom_ext.c
index d46dffb..c2b0abf 100644
--- a/sw/device/silicon_creator/rom_ext/rom_ext.c
+++ b/sw/device/silicon_creator/rom_ext/rom_ext.c
@@ -16,8 +16,8 @@
#include "sw/device/silicon_creator/lib/drivers/otp.h"
#include "sw/device/silicon_creator/lib/drivers/pinmux.h"
#include "sw/device/silicon_creator/lib/drivers/uart.h"
-#include "sw/device/silicon_creator/lib/log.h"
#include "sw/device/silicon_creator/lib/manifest.h"
+#include "sw/device/silicon_creator/lib/rom_print.h"
#include "sw/device/silicon_creator/lib/shutdown.h"
#include "sw/device/silicon_creator/lib/sigverify/sigverify.h"
#include "sw/device/silicon_creator/mask_rom/sigverify_keys.h"
@@ -97,7 +97,7 @@
// Jump to BL0 entry point.
uintptr_t entry_point = manifest_entry_point_get(manifest);
- log_printf("entry: 0x%x\r\n", entry_point);
+ rom_printf("entry: 0x%x\r\n", (unsigned int)entry_point);
((owner_stage_entry_point *)entry_point)();
return kErrorMaskRomBootFailed;
@@ -123,7 +123,7 @@
void rom_ext_main(void) {
rom_ext_init();
- log_printf("starting rom_ext\r\n");
+ rom_printf("starting rom_ext\r\n");
shutdown_finalize(rom_ext_try_boot());
}