1 /* J J Larworthy 27 September 2006 */
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
18 extern uint32_t crc32(uint32_t, const unsigned char *, unsigned int);
20 #define NUMBER_VECTORS 12
22 unsigned int start_vector
[NUMBER_VECTORS
];
24 unsigned int header_length
;
25 unsigned int reserved
[3];
31 void print_usage(void)
33 printf("update_header file.bin\n");
36 void print_header(void)
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
]);
44 printf("length:%8x\nimg_CRC:0x%08x\nHeader CRC:0x%08x\n",
45 img_header
.length
, img_header
.img_CRC
, img_header
.CRC
);
48 int main(int argc
, char **argv
)
56 struct stat file_stat
;
60 in_file
= open(argv
[1], O_RDWR
);
63 printf("failed to open file:%s\n", argv
[optind
]);
67 status
= fstat(in_file
, &file_stat
);
69 /* read header and obtain size of image */
70 status
= read(in_file
, &img_header
, sizeof(img_header
));
72 file_length
= file_stat
.st_size
- sizeof(img_header
);
74 if (img_header
.length
!= file_length
) {
75 printf("size in header:%d, size of file: %d\n",
76 img_header
.length
, file_length
);
78 img_header
.length
= file_length
;
80 /* read working image and CRC */
81 executable
= malloc(file_length
);
83 status
= read(in_file
, executable
, file_length
);
85 if (status
!= file_length
) {
86 printf("Failed to load image\n");
90 /* verify image CRC */
91 crc
= crc32(0, (const unsigned char *) executable
, img_header
.length
);
93 if (crc
!= img_header
.img_CRC
) {
94 printf("New Image CRC:0x%08x, hdr:0x%08x\n", crc
,
96 img_header
.img_CRC
= crc
;
98 memcpy(img_header
.code
, "BOOT", 4);
99 img_header
.header_length
= sizeof(img_header
);
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
,
107 img_header
.CRC
= crc
;
110 /* re-write the file */
111 status
= lseek(in_file
, 0, SEEK_SET
);
113 printf("failed to rewind\n");
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
);