Skip to content

Commit

Permalink
Add readnor util
Browse files Browse the repository at this point in the history
  • Loading branch information
dixyes committed Mar 21, 2024
1 parent 46519e7 commit d5f0f42
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 0 deletions.
1 change: 1 addition & 0 deletions buildaa64.sh
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,5 @@ build_efi()
}

build_efi tablesfix tablesfix.c
build_efi readnor readnor.c

91 changes: 91 additions & 0 deletions readnor.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
#include <uefi.h>

#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;
}

0 comments on commit d5f0f42

Please sign in to comment.