imagetag.c 15 KB

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