imagetag.c 15 KB

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