OpenWrt – Blame information for rev 1
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | office | 1 | /* J J Larworthy 27 September 2006 */ |
2 | |||
3 | /* file to read the boot sector of a dis and the loaded image and report |
||
4 | * if the boot rom would accept the data as intact and suitable for use |
||
5 | */ |
||
6 | |||
7 | #include <sys/types.h> |
||
8 | #include <sys/stat.h> |
||
9 | #include <sys/errno.h> |
||
10 | |||
11 | #include <fcntl.h> |
||
12 | #include <unistd.h> |
||
13 | #include <stdio.h> |
||
14 | #include <stdlib.h> |
||
15 | #include <string.h> |
||
16 | #include <assert.h> |
||
17 | |||
18 | extern uint32_t crc32(uint32_t, const unsigned char *, unsigned int); |
||
19 | |||
20 | #define NUMBER_VECTORS 12 |
||
21 | struct { |
||
22 | unsigned int start_vector[NUMBER_VECTORS]; |
||
23 | char code[4]; |
||
24 | unsigned int header_length; |
||
25 | unsigned int reserved[3]; |
||
26 | unsigned int length; |
||
27 | unsigned int img_CRC; |
||
28 | unsigned int CRC; |
||
29 | } img_header; |
||
30 | |||
31 | void print_usage(void) |
||
32 | { |
||
33 | printf("update_header file.bin\n"); |
||
34 | } |
||
35 | |||
36 | void print_header(void) |
||
37 | { |
||
38 | int i; |
||
39 | |||
40 | printf("vectors in header\n"); |
||
41 | for (i = 0; i < NUMBER_VECTORS; i++) { |
||
42 | printf("%d:0x%08x\n", i, img_header.start_vector[i]); |
||
43 | } |
||
44 | printf("length:%8x\nimg_CRC:0x%08x\nHeader CRC:0x%08x\n", |
||
45 | img_header.length, img_header.img_CRC, img_header.CRC); |
||
46 | } |
||
47 | |||
48 | int main(int argc, char **argv) |
||
49 | { |
||
50 | int in_file; |
||
51 | int status; |
||
52 | int unsigned crc; |
||
53 | int file_length; |
||
54 | int len; |
||
55 | |||
56 | struct stat file_stat; |
||
57 | |||
58 | void *executable; |
||
59 | |||
60 | in_file = open(argv[1], O_RDWR); |
||
61 | |||
62 | if (in_file < 0) { |
||
63 | printf("failed to open file:%s\n", argv[optind]); |
||
64 | return -ENOENT; |
||
65 | } |
||
66 | |||
67 | status = fstat(in_file, &file_stat); |
||
68 | |||
69 | /* read header and obtain size of image */ |
||
70 | status = read(in_file, &img_header, sizeof(img_header)); |
||
71 | |||
72 | file_length = file_stat.st_size - sizeof(img_header); |
||
73 | |||
74 | if (img_header.length != file_length) { |
||
75 | printf("size in header:%d, size of file: %d\n", |
||
76 | img_header.length, file_length); |
||
77 | } |
||
78 | img_header.length = file_length; |
||
79 | |||
80 | /* read working image and CRC */ |
||
81 | executable = malloc(file_length); |
||
82 | |||
83 | status = read(in_file, executable, file_length); |
||
84 | |||
85 | if (status != file_length) { |
||
86 | printf("Failed to load image\n"); |
||
87 | return -ENOENT; |
||
88 | } |
||
89 | |||
90 | /* verify image CRC */ |
||
91 | crc = crc32(0, (const unsigned char *) executable, img_header.length); |
||
92 | |||
93 | if (crc != img_header.img_CRC) { |
||
94 | printf("New Image CRC:0x%08x, hdr:0x%08x\n", crc, |
||
95 | img_header.img_CRC); |
||
96 | img_header.img_CRC = crc; |
||
97 | } |
||
98 | memcpy(img_header.code, "BOOT", 4); |
||
99 | img_header.header_length = sizeof(img_header); |
||
100 | |||
101 | /* check header CRC */ |
||
102 | crc = crc32(0, (const unsigned char *) &img_header, |
||
103 | sizeof(img_header) - sizeof(unsigned int)); |
||
104 | if (crc != img_header.CRC) { |
||
105 | printf("New header CRC - crc:0x%08x hdr:0x%08x\n", crc, |
||
106 | img_header.CRC); |
||
107 | img_header.CRC = crc; |
||
108 | } |
||
109 | |||
110 | /* re-write the file */ |
||
111 | status = lseek(in_file, 0, SEEK_SET); |
||
112 | if (status != 0) { |
||
113 | printf("failed to rewind\n"); |
||
114 | return 1; |
||
115 | } |
||
116 | len = write(in_file, &img_header, sizeof(img_header)); |
||
117 | assert(len == sizeof(img_header)); |
||
118 | len = write(in_file, executable, file_length); |
||
119 | assert(len == file_length); |
||
120 | close(in_file); |
||
121 | |||
122 | return 0; |
||
123 | } |