From d5f0f42bfbfbbc8dae972da1297df3ccfeb079be Mon Sep 17 00:00:00 2001 From: dixyes Date: Thu, 21 Mar 2024 23:16:32 +0800 Subject: [PATCH] Add readnor util --- buildaa64.sh | 1 + readnor.c | 91 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 92 insertions(+) create mode 100644 readnor.c diff --git a/buildaa64.sh b/buildaa64.sh index 64dcf16..3e5fcdd 100755 --- a/buildaa64.sh +++ b/buildaa64.sh @@ -48,4 +48,5 @@ build_efi() } build_efi tablesfix tablesfix.c +build_efi readnor readnor.c diff --git a/readnor.c b/readnor.c new file mode 100644 index 0000000..f23ff7e --- /dev/null +++ b/readnor.c @@ -0,0 +1,91 @@ +#include + +#define IN +#define OUT +typedef uintn_t EFI_STATUS; +typedef uint32_t UINT32; +typedef uint8_t UINT8; + +#define HISI_SPI_FLASH_PROTOCOL_GUID {0x339132DC, 0xCED7, 0x4f84, {0xAA, 0xE7, 0x2E, 0xC4, 0xF9, 0x14, 0x38, 0x2F}} + +typedef struct _HISI_SPI_FLASH_PROTOCOL HISI_SPI_FLASH_PROTOCOL; + +typedef +EFI_STATUS +(EFIAPI *HISI_SPI_FLASH_ERASE_INTERFACE) ( + IN HISI_SPI_FLASH_PROTOCOL *This, + IN UINT32 Offset, + IN UINT32 ulLength + ); + +typedef +EFI_STATUS +(EFIAPI *HISI_SPI_FLASH_WRITE_INTERFACE) ( + IN HISI_SPI_FLASH_PROTOCOL *This, + IN UINT32 Offset, + IN UINT8 *Buffer, + IN UINT32 ulLength + ); + +typedef +EFI_STATUS +(EFIAPI *HISI_SPI_FLASH_READ_INTERFACE) ( + IN HISI_SPI_FLASH_PROTOCOL *This, + IN UINT32 Offset, + IN OUT UINT8 *Buffer, + IN UINT32 ulLength + ); + +typedef +EFI_STATUS +(EFIAPI *HISI_SPI_FLASH_ERASE_WRITE_INTERFACE) ( + IN HISI_SPI_FLASH_PROTOCOL *This, + IN UINT32 Offset, + IN UINT8 *Buffer, + IN UINT32 ulLength + ); + +struct _HISI_SPI_FLASH_PROTOCOL { + HISI_SPI_FLASH_ERASE_INTERFACE Erase; + HISI_SPI_FLASH_WRITE_INTERFACE Write; + HISI_SPI_FLASH_READ_INTERFACE Read; + HISI_SPI_FLASH_ERASE_WRITE_INTERFACE EraseWrite; +}; + +int main(int argc, const char **argv) { + int ret; + const efi_guid_t norGuid = HISI_SPI_FLASH_PROTOCOL_GUID; + const char *dst = "dump.bin"; + if (argc > 1) { + dst = argv[1]; + } + + HISI_SPI_FLASH_PROTOCOL *norp; + ret = BS->LocateProtocol((void*)&norGuid, NULL, (void**)&norp); + if (EFI_SUCCESS != ret) { + printf("failed LocateProtocol HISI_SPI_FLASH_PROTOCOL: %d\n", ret); + return 1; + } + printf("LocateProtocol HISI_SPI_FLASH_PROTOCOL success\n"); + + FILE *fp = fopen(dst, "wb"); + if (NULL == fp) { + printf("failed open file %s\n", dst); + return 1; + } + + unsigned char buf[4096]; + for (uint32_t offset = 0; offset < 16 * 1024 * 1024; offset += sizeof(buf)) { + ret = norp->Read(norp, offset, buf, sizeof(buf)); + if (EFI_SUCCESS != ret) { + printf("failed read nor: %d\n", ret); + return 1; + } + fwrite(buf, 1, sizeof(buf), fp); + } + + fflush(fp); + fclose(fp); + + return 0; +}