diff --git a/CMakeLists.txt b/CMakeLists.txt index bbafa46..006532e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -17,6 +17,7 @@ add_subdirectory(pinctrl) add_subdirectory(piolib) add_subdirectory(raspinfo) add_subdirectory(rpieepromab) +add_subdirectory(rpierrorlog) add_subdirectory(rpi-gpu-usage) add_subdirectory(rpi-manufacture-info) add_subdirectory(rpi-pmic) diff --git a/README.md b/README.md index ee077fa..ae4fe7e 100644 --- a/README.md +++ b/README.md @@ -24,6 +24,8 @@ A collection of scripts and simple applications the submission of bug reports. * [rpieepromab](rpieepromab/) - A tool to update and manage the AB EEPROM partitions on a device in the Pi 5 family. +* [rpierrorlog](rpierrorlog/) - A tool to read and clear the fatal boot errors + that the bootloader logs in the SPI EEPROM on a device in the Pi 5 family. * [rpifwcrypto](rpifwcrypto/) - A command line application and shared library for the firmware cryptography service. Intended for use with Raspberry Pi Connect and secure-boot provisioner. diff --git a/rpierrorlog/CMakeLists.txt b/rpierrorlog/CMakeLists.txt new file mode 100644 index 0000000..ef3e1d7 --- /dev/null +++ b/rpierrorlog/CMakeLists.txt @@ -0,0 +1,25 @@ +cmake_minimum_required(VERSION 3.10...3.27) +include(GNUInstallDirs) + +set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra") +option(ENABLE_WERROR "Treat compiler warnings as errors" OFF) +if(ENABLE_WERROR) + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Werror") +endif() + +project(rpierrorlog) + +option(BUILD_SHARED_LIBS "Build using shared libraries" ON) + +add_library(rpierrorlog rpierrorlog.c) +target_sources(rpierrorlog PUBLIC rpierrorlog.h) +set_target_properties(rpierrorlog PROPERTIES PUBLIC_HEADER rpierrorlog.h) +set_target_properties(rpierrorlog PROPERTIES SOVERSION 0) + +add_executable(rpi-error-log main.c) +target_link_libraries(rpi-error-log rpierrorlog) + +install(TARGETS rpi-error-log RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}) +install(TARGETS rpierrorlog + ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} + PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}) diff --git a/rpierrorlog/README.md b/rpierrorlog/README.md new file mode 100644 index 0000000..962af61 --- /dev/null +++ b/rpierrorlog/README.md @@ -0,0 +1,26 @@ + +# rpierrorlog + +Tool to read and clear the fatal boot errors that the bootloader logs in the +SPI EEPROM on Raspberry Pi 5 family devices. + +The log keeps the error code of each fatal boot error, so the reason for a +failed boot can still be read after the device restarts. Installing a +bootloader update clears the log, because the errors in it came from the +previous bootloader version. The log only exists on AB-capable firmware. + +**Build Instructions** + +Install prerequisites with "sudo apt install cmake" - you need at least version 3.10. + + - *mkdir build* + - *cd build* + - *cmake ..* + - *make* + - *sudo make install* + +**Usage** + +* rpi-error-log (Shows usage information) +* rpi-error-log get (Read and print all EEPROM error log entries - Pi 5 family boards only) +* rpi-error-log clear (Clear the EEPROM error log and verify it is empty - Pi 5 family boards only) diff --git a/rpierrorlog/main.c b/rpierrorlog/main.c new file mode 100644 index 0000000..13bd5e5 --- /dev/null +++ b/rpierrorlog/main.c @@ -0,0 +1,91 @@ +#include +#include +#include "rpierrorlog.h" + +static void usage(const char *progname) +{ + fprintf(stderr, + "Usage: %s \n" + "\n" + "Commands:\n" + " get Read and print all EEPROM error log entries\n" + " (Pi 5 family boards only)\n" + " clear Clear the EEPROM error log and verify it is empty\n" + " (Pi 5 family boards only)\n", + progname); +} + +static int cmd_get(void) +{ + rpi_error_log_t entries[RPI_ERROR_LOG_MAX_SIZE / RPI_ERROR_LOG_ENTRY_SIZE]; + size_t count; + + if (rpi_error_log_get(entries, sizeof(entries) / sizeof(entries[0]), &count) < 0) { + fprintf(stderr, "Failed to read error log\n"); + return -1; + } + + if (count == 0) { + printf("No error log entries\n"); + return 0; + } + + for (size_t i = 0; i < count; i++) + printf("Entry %zu: 0x%08x - %s\n", i, entries[i].error_code, rpi_error_log_strerror(entries[i].error_code)); + + return 0; +} + +static int cmd_clear(void) +{ + rpi_error_log_t entries[RPI_ERROR_LOG_MAX_SIZE / RPI_ERROR_LOG_ENTRY_SIZE]; + size_t count; + + if (rpi_error_log_clear() < 0) { + fprintf(stderr, "Failed to clear error log\n"); + return -1; + } + printf("Error log cleared\n"); + + if (rpi_error_log_get(entries, sizeof(entries) / sizeof(entries[0]), &count) < 0) { + fprintf(stderr, "Verification read failed\n"); + return -1; + } + if (count != 0) { + fprintf(stderr, "Verification failed: %zu entries still present after clear\n", count); + return -1; + } + printf("Verification OK: error log is empty\n"); + return 0; +} + +int main(int argc, char *argv[]) +{ + int rc = -1; + + if (argc != 2) { + usage(argv[0]); + return 1; + } + + if (strcmp(argv[1], "get") == 0) { + rc = cmd_get(); + if (rc < 0) + goto error; + return 0; + } + + if (strcmp(argv[1], "clear") == 0) { + rc = cmd_clear(); + if (rc < 0) + goto error; + return 0; + } + + usage(argv[0]); + return 1; + +error: + fprintf(stderr, "Command '%s' failed\n", argv[1]); + return rc; +} diff --git a/rpierrorlog/rpierrorlog.c b/rpierrorlog/rpierrorlog.c new file mode 100644 index 0000000..df415d6 --- /dev/null +++ b/rpierrorlog/rpierrorlog.c @@ -0,0 +1,269 @@ +#include +#include +#include +#include +#include +#include +#include +#include "rpierrorlog.h" + +#define DEVICE_FILE_NAME "/dev/vcio" +#define MAJOR_NUM 100 +#define IOCTL_MBOX_PROPERTY _IOWR(MAJOR_NUM, 0, char *) + +#if 0 +#define LOG_DEBUG(...) do { \ + fprintf(stderr, __VA_ARGS__); \ +} while (0) +#else +#define LOG_DEBUG(...) do { } while (0) +#endif + +/* VideoCore mailbox error flag, by convention set on status 0 of property */ +#define VC_MAILBOX_ERROR 0x80000000u + +/* VideoCore sets most significant bit to indicate that the code/len has been set by the firmware */ +#define VC_MAILBOX_COMPLETE 0x80000000u + +/* Error log mailbox tags */ +#define TAG_GET_EEPROM_ERROR_LOGS 0x0003009au /* Read error log entries from EEPROM */ +#define TAG_SET_EEPROM_ERROR_LOGS 0x0003809au /* Clear error log in EEPROM */ + +/* Firmware status codes returned with VC_MAILBOX_ERROR by the error log tags */ +#define EEPROM_AB_ERROR_SPI_GPIO 12u +#define EEPROM_AB_ERROR_NO_PARTITIONING 13u +#define EEPROM_AB_ERROR_WRITE_PROTECTED 14u + +/* Big endian header at the start of the error log sector */ +#define RPI_ERROR_LOG_MAGIC 0x55aaf66fu +#define RPI_ERROR_LOG_VERSION 1 +#define RPI_ERROR_LOG_HEADER_SIZE 16 + +/* Common header for all firmware mailbox messages */ +struct firmware_msg_header { + uint32_t buf_size; + uint32_t code; + uint32_t tag; + uint32_t tag_buf_size; + uint32_t tag_req_resp_size; +}; + +/* Mailbox message for TAG_GET_EEPROM_ERROR_LOGS. + * + * Request: reserved (ignored, 0) + * Response: status (VC_MAILBOX_ERROR on failure) + raw error log sector + */ +struct firmware_error_log_msg { + struct firmware_msg_header hdr; + union { + struct { + uint32_t reserved; + } req; + struct { + uint32_t status; + uint8_t data[RPI_ERROR_LOG_MAX_SIZE]; + } resp; + }; + uint32_t end_tag; +}; + +/* Mailbox message for TAG_SET_EEPROM_ERROR_LOGS. + * + * Request: reserved (ignored, 0) + * Response: status (VC_MAILBOX_ERROR on failure) + */ +struct firmware_error_log_clear_msg { + struct firmware_msg_header hdr; + union { + struct { + uint32_t reserved; + } req; + struct { + uint32_t status; + } resp; + }; + uint32_t end_tag; +}; + +static int mbox_open(void) +{ + int fd = open(DEVICE_FILE_NAME, O_RDWR); + if (fd < 0) + fprintf(stderr, "Failed to open %s: %s\n", DEVICE_FILE_NAME, strerror(errno)); + return fd; +} + +static void mbox_close(int fd) +{ + close(fd); +} + +static int mbox_property(int fd, void *msg) +{ + struct firmware_msg_header *hdr = (struct firmware_msg_header *)msg; + int rc = ioctl(fd, IOCTL_MBOX_PROPERTY, msg); + if (rc < 0) { + fprintf(stderr, "ioctl_mbox_property failed: %d\n", rc); + return -1; + } + + LOG_DEBUG("hdr.code=0x%08x tag=0x%08x tag_buf_size=%u tag_req_resp_size=0x%08x\n", + hdr->code, hdr->tag, hdr->tag_buf_size, hdr->tag_req_resp_size); + + /* The firmware sets bit 0 of the code when it rejects the request */ + if (hdr->code != VC_MAILBOX_COMPLETE || + !(hdr->tag_req_resp_size & VC_MAILBOX_COMPLETE)) + return -1; + + return 0; +} + +static uint32_t be32(const uint8_t *p) +{ + return ((uint32_t)p[0] << 24) | ((uint32_t)p[1] << 16) | ((uint32_t)p[2] << 8) | p[3]; +} + +static const char *firmware_strerror(uint32_t status) +{ + switch (status & ~VC_MAILBOX_ERROR) { + case EEPROM_AB_ERROR_SPI_GPIO: return "SPI pins are claimed by the OS"; + case EEPROM_AB_ERROR_NO_PARTITIONING: return "this EEPROM image has no error log"; + case EEPROM_AB_ERROR_WRITE_PROTECTED: return "EEPROM is write protected"; + default: return "firmware error"; + } +} + +const char *rpi_error_log_strerror(uint32_t error_code) +{ + switch (error_code) { + case 0x03: return "Generic boot failure"; + case 0x04: return "Firmware (start*.elf) not found"; + case 0x05: return "Memory test failed"; + case 0x07: return "Kernel or device-tree not found or is not compatible"; + case 0x08: return "SDRAM failure"; + case 0x09: return "SDRAM mismatch"; + case 0x0a: return "Halting"; + case 0x11: return "Operation requires USB high current limit"; + case 0x12: return "SD card overcurrent"; + case 0x21: return "Partition is not FAT"; + case 0x22: return "Failed to read from partition"; + case 0x23: return "Extended partition not FAT"; + case 0x24: return "File signature or hash mismatch"; + case 0x25: return "Anti-rollback check failed"; + case 0x31: return "SPI EEPROM error"; + case 0x32: return "EEPROM is write protected"; + case 0x33: return "I2C error"; + case 0x34: return "Configuration error"; + case 0x43: return "RP1 not found"; + case 0x44: return "Unsupported board type"; + case 0x45: return "Fatal firmware error"; + case 0x46: return "Power failure type A"; + case 0x47: return "Power failure type B"; + default: return "Unknown"; + } +} + +int rpi_error_log_get(rpi_error_log_t *entries, size_t max_entries, size_t *out_count) +{ + struct firmware_error_log_msg msg = {0}; + size_t count = 0; + size_t offset; + uint32_t resp_len; + int mb, rc; + + if (!entries || !out_count || max_entries == 0) + return -1; + + mb = mbox_open(); + if (mb < 0) + return -1; + + msg.hdr.buf_size = sizeof(msg); + msg.hdr.tag = TAG_GET_EEPROM_ERROR_LOGS; + msg.hdr.tag_buf_size = sizeof(msg.resp); + msg.req.reserved = 0; + + rc = mbox_property(mb, &msg); + mbox_close(mb); + + if (rc < 0) + return -1; + + resp_len = msg.hdr.tag_req_resp_size & ~VC_MAILBOX_COMPLETE; + if (resp_len != sizeof(msg.resp)) { + fprintf(stderr, "Unexpected response length %u\n", resp_len); + return -1; + } + + if (msg.resp.status & VC_MAILBOX_ERROR) { + fprintf(stderr, "Firmware returned error 0x%08x (%s) for GET_EEPROM_ERROR_LOGS\n", + msg.resp.status, firmware_strerror(msg.resp.status)); + return -1; + } + + if (be32(msg.resp.data) != RPI_ERROR_LOG_MAGIC) { + /* No error has been logged on this EEPROM image yet, or it has no log sector */ + *out_count = 0; + return 0; + } + + if (be32(msg.resp.data + 4) != RPI_ERROR_LOG_MAX_SIZE - 8 || + be32(msg.resp.data + 8) != RPI_ERROR_LOG_VERSION) { + fprintf(stderr, "Unsupported error log format\n"); + return -1; + } + + for (offset = RPI_ERROR_LOG_HEADER_SIZE; + offset < RPI_ERROR_LOG_MAX_SIZE && count < max_entries; + offset += RPI_ERROR_LOG_ENTRY_SIZE) { + uint32_t ec, ec_inv; + + memcpy(&ec, msg.resp.data + offset, sizeof(ec)); + memcpy(&ec_inv, msg.resp.data + offset + 4, sizeof(ec_inv)); + + LOG_DEBUG("offset %zu ec=0x%08x ec_inv=0x%08x\n", offset, ec, ec_inv); + + /* Entries are only appended, so the first free entry ends the log */ + if (ec == 0xffffffffu && ec_inv == 0xffffffffu) + break; + + if (ec_inv != (~ec & 0xffffffffu)) { + /* Power was lost while this entry was being written */ + fprintf(stderr, "Skipping damaged entry at offset %zu\n", offset); + continue; + } + + entries[count++].error_code = ec; + } + + *out_count = count; + return 0; +} + +int rpi_error_log_clear(void) +{ + struct firmware_error_log_clear_msg msg = {0}; + int mb, rc; + + mb = mbox_open(); + if (mb < 0) + return -1; + + msg.hdr.buf_size = sizeof(msg); + msg.hdr.tag = TAG_SET_EEPROM_ERROR_LOGS; + msg.hdr.tag_buf_size = sizeof(msg.resp); + + rc = mbox_property(mb, &msg); + mbox_close(mb); + + if (rc < 0) + return -1; + + if (msg.resp.status & VC_MAILBOX_ERROR) { + fprintf(stderr, "Firmware returned error 0x%08x (%s) for SET_EEPROM_ERROR_LOGS\n", + msg.resp.status, firmware_strerror(msg.resp.status)); + return -1; + } + + return 0; +} diff --git a/rpierrorlog/rpierrorlog.h b/rpierrorlog/rpierrorlog.h new file mode 100644 index 0000000..f2747a3 --- /dev/null +++ b/rpierrorlog/rpierrorlog.h @@ -0,0 +1,50 @@ +#ifndef RPI_ERROR_LOG_H +#define RPI_ERROR_LOG_H + +#ifdef __cplusplus +extern "C" { +#endif + +#include +#include + +#define RPI_ERROR_LOG_ENTRY_SIZE 8 /* error_code (u32 LE) + ~error_code (u32 LE) */ +#define RPI_ERROR_LOG_MAX_SIZE 4096 + +typedef struct { + uint32_t error_code; +} rpi_error_log_t; + +/** + * Read the error log sector from the EEPROM via the firmware mailbox and + * return its entries, oldest first. Damaged entries are skipped. + * + * @param entries Output buffer for parsed entries. + * @param max_entries Size of the entries buffer. + * @param out_count Output: number of entries stored in entries, 0 if + * nothing has been logged since the log was cleared. + * @return 0 on success, -1 on firmware/ioctl error or an + * unsupported log format. + */ +int rpi_error_log_get(rpi_error_log_t *entries, size_t max_entries, size_t *out_count); + +/** + * Clear the EEPROM error log via the firmware mailbox (SET_EEPROM_ERROR_LOGS). + * + * @return 0 on success, -1 on firmware/ioctl error. + */ +int rpi_error_log_clear(void); + +/** + * Translate a bootrom error code to a human-readable string. + * + * @param error_code The error_code field from rpi_error_log_t. + * @return A constant string describing the error. + */ +const char *rpi_error_log_strerror(uint32_t error_code); + +#ifdef __cplusplus +} +#endif + +#endif /* RPI_ERROR_LOG_H */