]> git.zerfleddert.de Git - rsbs2/blob - firmware.c
f8584c163caa3ef4aa9aec3250dccc8e37d6cfe8
[rsbs2] / firmware.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <sys/types.h>
4 #include <sys/stat.h>
5 #include <fcntl.h>
6 #include <unistd.h>
7 #include <errno.h>
8 #include <strings.h>
9 #include "rsb-crc.h"
10
11 struct fw_header {
12 unsigned int unknown[8];
13 unsigned int len;
14 char ident[4];
15 unsigned int offset;
16 char desc[128];
17 };
18
19 void parse_fw(unsigned char *fw, unsigned int off) {
20 struct fw_header *header = (struct fw_header*)(fw + off);
21 static unsigned int last_off;
22 int i;
23
24 printf("Address in file: 0x%08x, Difference to last: %u\n", off, off-last_off);
25 printf("Unknown: ");
26 for (i = 0; i < 8; i++)
27 printf("0x%08x ", header->unknown[i]);
28
29 printf("\n");
30
31 printf("Length: %u, possible next entry at: 0x%08x\n", header->len, off + header->len + 22);
32
33 printf("Identifier: %.4s\n", header->ident);
34
35 printf("Offset: 0x%08x\n", header->offset);
36
37 printf("Descriptiom: %s\n", header->desc);
38
39 printf("\n");
40 last_off = off;
41 }
42
43 int main(int argc, char **argv)
44 {
45 struct stat statbuf;
46 unsigned char *fw;
47 int fd;
48 int remaining;
49 int ret;
50 unsigned int crc;
51
52 if (argc != 2) {
53 fprintf(stderr,"Syntax: %s firmware.bin\n", argv[0]);
54 exit(1);
55 }
56
57 if (stat(argv[1], &statbuf) == -1) {
58 perror("stat");
59 exit(1);
60 }
61
62 if ((fd = open(argv[1], O_RDONLY)) == -1) {
63 perror("open");
64 exit(1);
65 }
66
67 if ((fw = malloc(statbuf.st_size)) == NULL) {
68 perror("malloc");
69 exit(1);
70 }
71
72 bzero(fw, statbuf.st_size);
73
74 remaining = statbuf.st_size;
75
76 while(remaining) {
77 if ((ret = read(fd, fw + (statbuf.st_size - remaining), remaining)) == -1) {
78 perror("read");
79 exit(1);
80 }
81 remaining -= ret;
82 }
83
84 #if 0
85 parse_fw(fw, 0x0);
86 parse_fw(fw, 0x555c0);
87 parse_fw(fw, 0x5940e);
88 #endif
89
90 crc = rsb_crc(0, fw, statbuf.st_size-4);
91
92 printf("Length: %ld, Checksum: 0x%08x\n", statbuf.st_size-4, crc);
93
94 exit(0);
95 }
Impressum, Datenschutz