imagetag.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  1. /*
  2. * This file is subject to the terms and conditions of the GNU General Public
  3. * License. See the file "COPYING" in the main directory of this archive
  4. * for more details.
  5. *
  6. * Copyright (C) 2008 Axel Gembe <ago@bastart.eu.org>
  7. * Copyright (C) 2009-2010 Daniel Dickinson <openwrt@cshore.neomailbox.net>
  8. */
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include <stdint.h>
  13. #include <time.h>
  14. #include <unistd.h>
  15. #include <sys/stat.h>
  16. #include <netinet/in.h>
  17. #include <inttypes.h>
  18. #include "bcm_tag.h"
  19. #include "imagetag_cmdline.h"
  20. #include "cyg_crc.h"
  21. #define DEADCODE 0xDEADC0DE
  22. /* Kernel header */
  23. struct kernelhdr {
  24. uint32_t loadaddr; /* Kernel load address */
  25. uint32_t entry; /* Kernel entry point address */
  26. uint32_t lzmalen; /* Compressed length of the LZMA data that follows */
  27. };
  28. static char pirellitab[NUM_PIRELLI][BOARDID_LEN] = PIRELLI_BOARDS;
  29. void int2tag(char *tag, uint32_t value) {
  30. uint32_t network = htonl(value);
  31. memcpy(tag, (char *)(&network), 4);
  32. }
  33. uint32_t compute_crc32(uint32_t crc, FILE *binfile, size_t compute_start, size_t compute_len)
  34. {
  35. uint8_t readbuf[1024];
  36. size_t read;
  37. fseek(binfile, compute_start, SEEK_SET);
  38. /* read block of 1024 bytes */
  39. while (binfile && !feof(binfile) && !ferror(binfile) && (compute_len >= sizeof(readbuf))) {
  40. read = fread(readbuf, sizeof(uint8_t), sizeof(readbuf), binfile);
  41. crc = cyg_crc32_accumulate(crc, readbuf, read);
  42. compute_len = compute_len - read;
  43. }
  44. /* Less than 1024 bytes remains, read compute_len bytes */
  45. if (binfile && !feof(binfile) && !ferror(binfile) && (compute_len > 0)) {
  46. read = fread(readbuf, sizeof(uint8_t), compute_len, binfile);
  47. crc = cyg_crc32_accumulate(crc, readbuf, read);
  48. }
  49. return crc;
  50. }
  51. size_t getlen(FILE *fp)
  52. {
  53. size_t retval, curpos;
  54. if (!fp)
  55. return 0;
  56. curpos = ftell(fp);
  57. fseek(fp, 0, SEEK_END);
  58. retval = ftell(fp);
  59. fseek(fp, curpos, SEEK_SET);
  60. return retval;
  61. }
  62. int tagfile(const char *kernel, const char *rootfs, const char *bin, \
  63. const struct gengetopt_args_info *args, \
  64. uint32_t flash_start, uint32_t image_offset, \
  65. uint32_t block_size, uint32_t load_address, uint32_t entry)
  66. {
  67. struct bcm_tag tag;
  68. struct kernelhdr khdr;
  69. FILE *kernelfile = NULL, *rootfsfile = NULL, *binfile = NULL, *cfefile = NULL;
  70. size_t cfeoff, cfelen, kerneloff, kernellen, rootfsoff, rootfslen, \
  71. read, imagelen, rootfsoffpadlen = 0, kernelfslen, kerneloffpadlen = 0, oldrootfslen, \
  72. rootfsend;
  73. uint8_t readbuf[1024];
  74. uint32_t imagecrc = IMAGETAG_CRC_START;
  75. uint32_t kernelcrc = IMAGETAG_CRC_START;
  76. uint32_t rootfscrc = IMAGETAG_CRC_START;
  77. uint32_t kernelfscrc = IMAGETAG_CRC_START;
  78. uint32_t fwaddr = 0;
  79. uint8_t crc_val;
  80. const uint32_t deadcode = htonl(DEADCODE);
  81. int i;
  82. int is_pirelli = 0;
  83. memset(&tag, 0, sizeof(struct bcm_tag));
  84. if (!kernel || !rootfs) {
  85. fprintf(stderr, "imagetag can't create an image without both kernel and rootfs\n");
  86. }
  87. if (kernel && !(kernelfile = fopen(kernel, "rb"))) {
  88. fprintf(stderr, "Unable to open kernel \"%s\"\n", kernel);
  89. return 1;
  90. }
  91. if (rootfs && !(rootfsfile = fopen(rootfs, "rb"))) {
  92. fprintf(stderr, "Unable to open rootfs \"%s\"\n", rootfs);
  93. return 1;
  94. }
  95. if (!bin || !(binfile = fopen(bin, "wb+"))) {
  96. fprintf(stderr, "Unable to open output file \"%s\"\n", bin);
  97. return 1;
  98. }
  99. if ((args->cfe_given) && (args->cfe_arg)) {
  100. if (!(cfefile = fopen(args->cfe_arg, "rb"))) {
  101. fprintf(stderr, "Unable to open CFE file \"%s\"\n", args->cfe_arg);
  102. }
  103. }
  104. fwaddr = flash_start + image_offset;
  105. if (cfefile) {
  106. cfeoff = flash_start;
  107. cfelen = getlen(cfefile);
  108. /* Seek to the start of the file after tag */
  109. fseek(binfile, sizeof(tag), SEEK_SET);
  110. /* Write the cfe */
  111. while (cfefile && !feof(cfefile) && !ferror(cfefile)) {
  112. read = fread(readbuf, sizeof(uint8_t), sizeof(readbuf), cfefile);
  113. fwrite(readbuf, sizeof(uint8_t), read, binfile);
  114. }
  115. } else {
  116. cfeoff = 0;
  117. cfelen = 0;
  118. }
  119. if (!args->root_first_flag) {
  120. /* Build the kernel address and length (doesn't need to be aligned, read only) */
  121. kerneloff = fwaddr + sizeof(tag);
  122. kernellen = getlen(kernelfile);
  123. if (!args->kernel_file_has_header_flag) {
  124. /* Build the kernel header */
  125. khdr.loadaddr = htonl(load_address);
  126. khdr.entry = htonl(entry);
  127. khdr.lzmalen = htonl(kernellen);
  128. /* Increase the kernel size by the header size */
  129. kernellen += sizeof(khdr);
  130. }
  131. /* Build the rootfs address and length */
  132. rootfsoff = kerneloff + kernellen;
  133. /* align the start if requested */
  134. if (args->align_rootfs_flag)
  135. rootfsoff = (rootfsoff % block_size) > 0 ? (((rootfsoff / block_size) + 1) * block_size) : rootfsoff;
  136. else
  137. rootfsoff = (rootfsoff % 4) > 0 ? (((rootfsoff / 4) + 1) * 4) : rootfsoff;
  138. /* align the end */
  139. rootfsend = rootfsoff + getlen(rootfsfile);
  140. if ((rootfsend % block_size) > 0)
  141. rootfsend = (((rootfsend / block_size) + 1) * block_size);
  142. rootfslen = rootfsend - rootfsoff;
  143. imagelen = rootfsoff + rootfslen - kerneloff + sizeof(deadcode);
  144. rootfsoffpadlen = rootfsoff - (kerneloff + kernellen);
  145. /* Seek to the start of the kernel */
  146. fseek(binfile, kerneloff - fwaddr + cfelen, SEEK_SET);
  147. /* Write the kernel header */
  148. fwrite(&khdr, sizeof(khdr), 1, binfile);
  149. /* Write the kernel */
  150. while (kernelfile && !feof(kernelfile) && !ferror(kernelfile)) {
  151. read = fread(readbuf, sizeof(uint8_t), sizeof(readbuf), kernelfile);
  152. fwrite(readbuf, sizeof(uint8_t), read, binfile);
  153. }
  154. /* Write the RootFS */
  155. fseek(binfile, rootfsoff - fwaddr + cfelen, SEEK_SET);
  156. while (rootfsfile && !feof(rootfsfile) && !ferror(rootfsfile)) {
  157. read = fread(readbuf, sizeof(uint8_t), sizeof(readbuf), rootfsfile);
  158. fwrite(readbuf, sizeof(uint8_t), read, binfile);
  159. }
  160. /* Align image to specified erase block size and append deadc0de */
  161. printf("Data alignment to %dk with 'deadc0de' appended\n", block_size/1024);
  162. fseek(binfile, rootfsoff + rootfslen - fwaddr + cfelen, SEEK_SET);
  163. fwrite(&deadcode, sizeof(uint32_t), 1, binfile);
  164. oldrootfslen = rootfslen;
  165. if (args->pad_given) {
  166. uint32_t allfs = 0xffffffff;
  167. uint32_t pad_size = args->pad_arg * 1024 * 1024;
  168. printf("Padding image to %d bytes ...\n", pad_size);
  169. while (imagelen < pad_size) {
  170. fwrite(&allfs, sizeof(uint32_t), 1, binfile);
  171. imagelen += 4;
  172. rootfslen += 4;
  173. }
  174. }
  175. /* Flush the binfile buffer so that when we read from file, it contains
  176. * everything in the buffer
  177. */
  178. fflush(binfile);
  179. /* Compute the crc32 of the entire image (deadC0de included) */
  180. imagecrc = compute_crc32(imagecrc, binfile, kerneloff - fwaddr + cfelen, imagelen);
  181. /* Compute the crc32 of the kernel and padding between kernel and rootfs) */
  182. kernelcrc = compute_crc32(kernelcrc, binfile, kerneloff - fwaddr + cfelen, kernellen + rootfsoffpadlen);
  183. /* Compute the crc32 of the kernel and padding between kernel and rootfs) */
  184. kernelfscrc = compute_crc32(kernelfscrc, binfile, kerneloff - fwaddr + cfelen, kernellen + rootfsoffpadlen + rootfslen + sizeof(deadcode));
  185. /* Compute the crc32 of the flashImageStart to rootLength.
  186. * The broadcom firmware assumes the rootfs starts the image,
  187. * therefore uses the rootfs start to determine where to flash
  188. * the image. Since we have the kernel first we have to give
  189. * it the kernel address, but the crc uses the length
  190. * associated with this address, which is added to the kernel
  191. * length to determine the length of image to flash and thus
  192. * needs to be rootfs + deadcode
  193. */
  194. rootfscrc = compute_crc32(rootfscrc, binfile, kerneloff - fwaddr + cfelen, rootfslen + sizeof(deadcode));
  195. } else {
  196. /* Build the kernel address and length (doesn't need to be aligned, read only) */
  197. rootfsoff = fwaddr + sizeof(tag);
  198. oldrootfslen = getlen(rootfsfile);
  199. rootfslen = oldrootfslen;
  200. rootfslen = ( (rootfslen % block_size) > 0 ? (((rootfslen / block_size) + 1) * block_size) : rootfslen );
  201. kerneloffpadlen = rootfslen - oldrootfslen;
  202. oldrootfslen = rootfslen;
  203. kerneloff = rootfsoff + rootfslen;
  204. kernellen = getlen(kernelfile);
  205. imagelen = cfelen + rootfslen + kernellen;
  206. /* Seek to the start of the kernel */
  207. fseek(binfile, kerneloff - fwaddr + cfelen, SEEK_SET);
  208. if (!args->kernel_file_has_header_flag) {
  209. /* Build the kernel header */
  210. khdr.loadaddr = htonl(load_address);
  211. khdr.entry = htonl(entry);
  212. khdr.lzmalen = htonl(kernellen);
  213. /* Write the kernel header */
  214. fwrite(&khdr, sizeof(khdr), 1, binfile);
  215. /* Increase the kernel size by the header size */
  216. kernellen += sizeof(khdr);
  217. }
  218. /* Write the kernel */
  219. while (kernelfile && !feof(kernelfile) && !ferror(kernelfile)) {
  220. read = fread(readbuf, sizeof(uint8_t), sizeof(readbuf), kernelfile);
  221. fwrite(readbuf, sizeof(uint8_t), read, binfile);
  222. }
  223. /* Write the RootFS */
  224. fseek(binfile, rootfsoff - fwaddr + cfelen, SEEK_SET);
  225. while (rootfsfile && !feof(rootfsfile) && !ferror(rootfsfile)) {
  226. read = fread(readbuf, sizeof(uint8_t), sizeof(readbuf), rootfsfile);
  227. fwrite(readbuf, sizeof(uint8_t), read, binfile);
  228. }
  229. /* Flush the binfile buffer so that when we read from file, it contains
  230. * everything in the buffer
  231. */
  232. fflush(binfile);
  233. /* Compute the crc32 of the entire image (deadC0de included) */
  234. imagecrc = compute_crc32(imagecrc, binfile, sizeof(tag), imagelen);
  235. /* Compute the crc32 of the kernel and padding between kernel and rootfs) */
  236. kernelcrc = compute_crc32(kernelcrc, binfile, kerneloff - fwaddr + cfelen, kernellen + rootfsoffpadlen);
  237. kernelfscrc = compute_crc32(kernelfscrc, binfile, rootfsoff - fwaddr + cfelen, kernellen + rootfslen);
  238. rootfscrc = compute_crc32(rootfscrc, binfile, rootfsoff - fwaddr + cfelen, rootfslen);
  239. }
  240. /* Close the files */
  241. if (cfefile) {
  242. fclose(cfefile);
  243. }
  244. fclose(kernelfile);
  245. fclose(rootfsfile);
  246. /* Build the tag */
  247. strncpy(tag.tagVersion, args->tag_version_arg, sizeof(tag.tagVersion) - 1);
  248. strncpy(tag.sig_1, args->signature_arg, sizeof(tag.sig_1) - 1);
  249. strncpy(tag.sig_2, args->signature2_arg, sizeof(tag.sig_2) - 1);
  250. strncpy(tag.chipid, args->chipid_arg, sizeof(tag.chipid) - 1);
  251. strncpy(tag.boardid, args->boardid_arg, sizeof(tag.boardid) - 1);
  252. strcpy(tag.big_endian, "1");
  253. sprintf(tag.totalLength, "%lu", imagelen);
  254. if (args->cfe_given) {
  255. sprintf(tag.cfeAddress, "%" PRIu32, flash_start);
  256. sprintf(tag.cfeLength, "%lu", cfelen);
  257. } else {
  258. /* We don't include CFE */
  259. strcpy(tag.cfeAddress, "0");
  260. strcpy(tag.cfeLength, "0");
  261. }
  262. sprintf(tag.kernelAddress, "%lu", kerneloff);
  263. sprintf(tag.kernelLength, "%lu", kernellen + rootfsoffpadlen);
  264. if (args->root_first_flag) {
  265. sprintf(tag.flashImageStart, "%lu", rootfsoff);
  266. sprintf(tag.flashRootLength, "%lu", rootfslen);
  267. } else {
  268. sprintf(tag.flashImageStart, "%lu", kerneloff);
  269. sprintf(tag.flashRootLength, "%lu", rootfslen + sizeof(deadcode));
  270. }
  271. int2tag(tag.rootLength, oldrootfslen + sizeof(deadcode));
  272. if (args->rsa_signature_given) {
  273. strncpy(tag.rsa_signature, args->rsa_signature_arg, RSASIG_LEN);
  274. }
  275. if (args->layoutver_given) {
  276. strncpy(tag.flashLayoutVer, args->layoutver_arg, TAGLAYOUT_LEN);
  277. }
  278. if (args->info1_given) {
  279. strncpy(tag.information1, args->info1_arg, TAGINFO1_LEN);
  280. }
  281. if (args->info2_given) {
  282. strncpy(tag.information2, args->info2_arg, TAGINFO2_LEN);
  283. }
  284. if (args->reserved2_given) {
  285. strncpy(tag.reserved2, args->reserved2_arg, 16);
  286. }
  287. if (args->altinfo_given) {
  288. strncpy(tag.information1, args->altinfo_arg, TAGINFO1_LEN);
  289. }
  290. if (args->second_image_flag_given) {
  291. if (strncmp(args->second_image_flag_arg, "2", DUALFLAG_LEN) != 0) {
  292. strncpy(tag.dualImage, args->second_image_flag_arg, DUALFLAG_LEN);
  293. }
  294. }
  295. if (args->inactive_given) {
  296. if (strncmp(args->inactive_arg, "2", INACTIVEFLAG_LEN) != 0) {
  297. strncpy(tag.inactiveFlag, args->second_image_flag_arg, INACTIVEFLAG_LEN);
  298. }
  299. }
  300. for (i = 0; i < NUM_PIRELLI; i++) {
  301. if (strncmp(args->boardid_arg, pirellitab[i], BOARDID_LEN) == 0) {
  302. is_pirelli = 1;
  303. break;
  304. }
  305. }
  306. if ( !is_pirelli ) {
  307. int2tag(tag.imageCRC, kernelfscrc);
  308. } else {
  309. int2tag(tag.imageCRC, kernelcrc);
  310. }
  311. int2tag(&(tag.rootfsCRC[0]), rootfscrc);
  312. int2tag(tag.kernelCRC, kernelcrc);
  313. int2tag(tag.fskernelCRC, kernelfscrc);
  314. int2tag(tag.headerCRC, cyg_crc32_accumulate(IMAGETAG_CRC_START, (uint8_t*)&tag, sizeof(tag) - 20));
  315. fseek(binfile, 0L, SEEK_SET);
  316. fwrite(&tag, sizeof(uint8_t), sizeof(tag), binfile);
  317. fflush(binfile);
  318. fclose(binfile);
  319. return 0;
  320. }
  321. int main(int argc, char **argv)
  322. {
  323. int c, i;
  324. char *kernel, *rootfs, *bin;
  325. uint32_t flash_start, image_offset, block_size, load_address, entry;
  326. flash_start = image_offset = block_size = load_address = entry = 0;
  327. struct gengetopt_args_info parsed_args;
  328. kernel = rootfs = bin = NULL;
  329. if (imagetag_cmdline(argc, argv, &parsed_args)) {
  330. exit(1);
  331. }
  332. printf("Broadcom 63xx image tagger - v2.0.0\n");
  333. printf("Copyright (C) 2008 Axel Gembe\n");
  334. printf("Copyright (C) 2009-2010 Daniel Dickinson\n");
  335. printf("Licensed under the terms of the Gnu General Public License\n");
  336. kernel = parsed_args.kernel_arg;
  337. rootfs = parsed_args.rootfs_arg;
  338. bin = parsed_args.output_arg;
  339. if (strlen(parsed_args.tag_version_arg) >= TAGVER_LEN) {
  340. fprintf(stderr, "Error: Tag Version (tag_version,v) too long.\n");
  341. exit(1);
  342. }
  343. if (strlen(parsed_args.boardid_arg) >= BOARDID_LEN) {
  344. fprintf(stderr, "Error: Board ID (boardid,b) too long.\n");
  345. exit(1);
  346. }
  347. if (strlen(parsed_args.chipid_arg) >= CHIPID_LEN) {
  348. fprintf(stderr, "Error: Chip ID (chipid,c) too long.\n");
  349. exit(1);
  350. }
  351. if (strlen(parsed_args.signature_arg) >= SIG1_LEN) {
  352. fprintf(stderr, "Error: Magic string (signature,a) too long.\n");
  353. exit(1);
  354. }
  355. if (strlen(parsed_args.signature2_arg) >= SIG2_LEN) {
  356. fprintf(stderr, "Error: Second magic string (signature2,m) too long.\n");
  357. exit(1);
  358. }
  359. if (parsed_args.layoutver_given) {
  360. if (strlen(parsed_args.layoutver_arg) > FLASHLAYOUTVER_LEN) {
  361. fprintf(stderr, "Error: Flash layout version (layoutver,y) too long.\n");
  362. exit(1);
  363. }
  364. }
  365. if (parsed_args.rsa_signature_given) {
  366. if (strlen(parsed_args.rsa_signature_arg) > RSASIG_LEN) {
  367. fprintf(stderr, "Error: RSA Signature (rsa_signature,r) too long.\n");
  368. exit(1);
  369. }
  370. }
  371. if (parsed_args.info1_given) {
  372. if (strlen(parsed_args.info1_arg) >= TAGINFO1_LEN) {
  373. fprintf(stderr, "Error: Vendor Information 1 (info1) too long.\n");
  374. exit(1);
  375. }
  376. }
  377. if (parsed_args.info2_given) {
  378. if (strlen(parsed_args.info2_arg) >= TAGINFO2_LEN) {
  379. fprintf(stderr, "Error: Vendor Information 2 (info2) too long.\n");
  380. exit(1);
  381. }
  382. }
  383. if (parsed_args.altinfo_given) {
  384. if (strlen(parsed_args.altinfo_arg) >= ALTTAGINFO_LEN) {
  385. fprintf(stderr, "Error: Vendor Information 1 (info1) too long.\n");
  386. exit(1);
  387. }
  388. }
  389. if (parsed_args.pad_given) {
  390. if (parsed_args.pad_arg < 0) {
  391. fprintf(stderr, "Error: pad size must be positive.\r");
  392. exit(1);
  393. }
  394. }
  395. flash_start = strtoul(parsed_args.flash_start_arg, NULL, 16);
  396. image_offset = strtoul(parsed_args.image_offset_arg, NULL, 16);
  397. block_size = strtoul(parsed_args.block_size_arg, NULL, 16);
  398. if (!parsed_args.kernel_file_has_header_flag) {
  399. load_address = strtoul(parsed_args.load_addr_arg, NULL, 16);
  400. entry = strtoul(parsed_args.entry_arg, NULL, 16);
  401. if (load_address == 0) {
  402. fprintf(stderr, "Error: Invalid value for load address\n");
  403. }
  404. if (entry == 0) {
  405. fprintf(stderr, "Error: Invalid value for entry\n");
  406. }
  407. }
  408. return tagfile(kernel, rootfs, bin, &parsed_args, flash_start, image_offset, block_size, load_address, entry);
  409. }