mirror of
https://github.com/brain-hackers/nkbin_maker
synced 2024-11-01 07:48:00 +09:00
64 lines
1.5 KiB
C
64 lines
1.5 KiB
C
#include <fcntl.h>
|
|
#include <gelf.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <sys/types.h>
|
|
#include <unistd.h>
|
|
|
|
uint8_t magic[7] = "B000FF\n";
|
|
|
|
#define READ(buf, len) \
|
|
if (!fread(buf, len, 1, f)) \
|
|
err(1, "read error");
|
|
|
|
int main(int argc, char *argv[]) {
|
|
FILE *f;
|
|
uint8_t marker[7];
|
|
uint32_t i32, addr, cksum, len, i;
|
|
int n, ch, c;
|
|
|
|
if (argc != 2) {
|
|
fprintf(stderr, "usage: nk-dump filename\n");
|
|
return 1;
|
|
}
|
|
|
|
if ((f = fopen(argv[1], "rb")) == NULL)
|
|
err(1, "can't open %s", argv[1]);
|
|
|
|
READ(marker, sizeof(marker));
|
|
if (memcmp(magic, marker, sizeof(magic)))
|
|
errx(1, "signature does not match");
|
|
READ(&i32, sizeof(i32));
|
|
printf("addr %08x\n", i32);
|
|
READ(&i32, sizeof(i32));
|
|
printf("length %08x\n", i32);
|
|
n = 0;
|
|
while (!feof(f)) {
|
|
READ(&addr, sizeof(addr));
|
|
READ(&len, sizeof(len));
|
|
READ(&cksum, sizeof(cksum));
|
|
|
|
printf("image %u\n", n);
|
|
c = 0;
|
|
for (i = 0; i < len; i++) {
|
|
ch = getc(f);
|
|
c += ch;
|
|
}
|
|
printf(" addr %08x\n", addr);
|
|
printf(" len %08x\n", len);
|
|
printf(" cksum %08x", cksum);
|
|
if (cksum == c) {
|
|
printf(" [ok]\n");
|
|
} else {
|
|
printf(" != calculated cksum %08x\n", c);
|
|
}
|
|
|
|
n++;
|
|
}
|
|
fclose(f);
|
|
|
|
return 0;
|
|
}
|
|
|
|
/* vim:set ts=2 sw=2: */
|