mkox820crc.c 2.7 KB

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