add_header.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /*
  2. * add_header.c - partially based on OpenWrt's motorola-bin.c
  3. *
  4. * Copyright (C) 2008 Imre Kaloz <kaloz@openwrt.org>
  5. * Gabor Juhos <juhosg@openwrt.org>
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License,
  9. * version 2 as published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  19. */
  20. /*
  21. * The add_header utility used by various vendors preprends the buf
  22. * image with a header containing a CRC32 value which is generated for the
  23. * model id + reserved space for CRC32 + buf, then replaces the reserved
  24. * area with the actual CRC32. This replacement tool mimics this behavior.
  25. */
  26. #include <stdio.h>
  27. #include <stdlib.h>
  28. #include <stddef.h>
  29. #include <unistd.h>
  30. #include <errno.h>
  31. #include <fcntl.h>
  32. #include <sys/mman.h>
  33. #include <string.h>
  34. #include <netinet/in.h>
  35. #include <inttypes.h>
  36. #define BPB 8 /* bits/byte */
  37. static uint32_t crc32[1<<BPB];
  38. static void init_crc32()
  39. {
  40. const uint32_t poly = ntohl(0x2083b8ed);
  41. int n;
  42. for (n = 0; n < 1<<BPB; n++) {
  43. uint32_t crc = n;
  44. int bit;
  45. for (bit = 0; bit < BPB; bit++)
  46. crc = (crc & 1) ? (poly ^ (crc >> 1)) : (crc >> 1);
  47. crc32[n] = crc;
  48. }
  49. }
  50. static uint32_t crc32buf(unsigned char *buf, size_t len)
  51. {
  52. uint32_t crc = 0xFFFFFFFF;
  53. for (; len; len--, buf++)
  54. crc = crc32[(uint8_t)crc ^ *buf] ^ (crc >> BPB);
  55. return ~crc;
  56. }
  57. struct header {
  58. unsigned char model[16];
  59. uint32_t crc;
  60. };
  61. static void usage(const char *) __attribute__ (( __noreturn__ ));
  62. static void usage(const char *mess)
  63. {
  64. fprintf(stderr, "Error: %s\n", mess);
  65. fprintf(stderr, "Usage: add_header model_id input_file output_file\n");
  66. fprintf(stderr, "\n");
  67. exit(1);
  68. }
  69. int main(int argc, char **argv)
  70. {
  71. off_t len; // of original buf
  72. off_t buflen; // of the output file
  73. int fd;
  74. void *input_file; // pointer to the input file (mmmapped)
  75. struct header header;
  76. unsigned char *buf; // pointer to prefix + copy of original buf
  77. // verify parameters
  78. if (argc != 4)
  79. usage("wrong number of arguments");
  80. // mmap input_file
  81. if ((fd = open(argv[2], O_RDONLY)) < 0
  82. || (len = lseek(fd, 0, SEEK_END)) < 0
  83. || (input_file = mmap(0, len, PROT_READ, MAP_SHARED, fd, 0)) == (void *) (-1)
  84. || close(fd) < 0)
  85. {
  86. fprintf(stderr, "Error loading file %s: %s\n", argv[2], strerror(errno));
  87. exit(1);
  88. }
  89. buflen = len + sizeof(header);
  90. init_crc32();
  91. // copy model name into header
  92. strncpy(header.model, argv[1], sizeof(header.model));
  93. header.crc = 0;
  94. // create a firmware image in memory and copy the input_file to it
  95. buf = malloc(buflen);
  96. memcpy(buf, &header, sizeof(header));
  97. memcpy(&buf[sizeof(header)], input_file, len);
  98. // CRC of temporary header + buf
  99. header.crc = htonl(crc32buf(buf, buflen));
  100. memcpy(buf, &header, sizeof(header));
  101. // write the buf
  102. if ((fd = open(argv[3], O_CREAT|O_WRONLY|O_TRUNC,0644)) < 0
  103. || write(fd, buf, buflen) != buflen
  104. || close(fd) < 0)
  105. {
  106. fprintf(stderr, "Error storing file %s: %s\n", argv[3], strerror(errno));
  107. exit(2);
  108. }
  109. free(buf);
  110. munmap(input_file,len);
  111. return 0;
  112. }