slugimage.pl 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204
  1. #!/usr/bin/perl
  2. #
  3. # SlugImage : Manipulate NSLU2 firmware images
  4. # Dwayne Fontenot (jacques)
  5. # Rod Whitby (rwhitby)
  6. # www.nslu2-linux.org
  7. #
  8. # Copyright (c) 2004, 2006, Dwayne Fontenot & Rod Whitby
  9. # All rights reserved.
  10. #
  11. # Redistribution and use in source and binary forms, with or without
  12. # modification, are permitted provided that the following conditions
  13. # are met:
  14. #
  15. # Redistributions of source code must retain the above copyright
  16. # notice, this list of conditions and the following disclaimer.
  17. # Redistributions in binary form must reproduce the above copyright
  18. # notice, this list of conditions and the following disclaimer in the
  19. # documentation and/or other materials provided with the distribution.
  20. # Neither the name of the NSLU2-Linux Development Team nor the names
  21. # of its contributors may be used to endorse or promote products
  22. # derived from this software without specific prior written
  23. # permission.
  24. #
  25. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  26. # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  27. # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  28. # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  29. # COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  30. # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  31. # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  32. # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  33. # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  34. # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  35. # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  36. # POSSIBILITY OF SUCH DAMAGE.
  37. #
  38. use strict;
  39. use warnings;
  40. use Getopt::Long qw(:config no_ignore_case);
  41. use POSIX qw(tmpnam);
  42. my($debug) = 0;
  43. my($quiet) = 0;
  44. my($flash_start) = 0x50000000;
  45. my($flash_len) = 0x00800000;
  46. my($block_size) = 0x00020000;
  47. my($kernel_offset) = 0x00060000;
  48. my($kernel_size) = 0x00100000;
  49. my($ramdisk_offset) = 0x00160000;
  50. my(@cleanup);
  51. # The last 70 bytes of the SercommRedBootTrailer (i.e. excluding MAC
  52. # address). Needed to create an image with an empty RedBoot partition
  53. # since the Sercomm upgrade tool checks for this trailer.
  54. # http://www.nslu2-linux.org/wiki/Info/SercommRedBootTrailer
  55. my @sercomm_redboot_trailer = (0x4573, 0x4372, 0x4d6f, 0x006d, 0x0001,
  56. 0x0400, 0x3170, 0x5895, 0x0010, 0x0000, 0x0000, 0x0000, 0x0000,
  57. 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
  58. 0x0000, 0x0000, 0x0001, 0x0000, 0x0000, 0x0000, 0x0003, 0x2300,
  59. 0x0063, 0x0000, 0x7320, 0x7245, 0x6f43, 0x6d4d);
  60. # There's a 16 byte Sercomm trailer at the end of the flash. It is used
  61. # by RedBoot to detect a Sercomm flash layout and to configure the
  62. # Sercomm upgrade system.
  63. # http://www.nslu2-linux.org/wiki/Info/SercommFlashTrailer
  64. my @sercomm_flash_trailer = (0x0100, 0x0000, 0x6323, 0xf790, 0x5265,
  65. 0x4f63, 0x4d6d, 0xb400);
  66. # Take $data, and pad it out to $total_len bytes, appending 0xff's.
  67. sub padBytes {
  68. my($data,$total_len) = @_;
  69. # 0xFF is used to pad, as it's the erase value of the flash.
  70. my($pad_char) = pack("C",0xff);
  71. my($pad_len) = $total_len - length($data);
  72. # A request for negative padding is indicative of a logic error ...
  73. if (length($data) > $total_len) {
  74. die sprintf("padBytes error: data (%d) is longer than total_len (%d)", length($data), $total_len);
  75. }
  76. return $data . ($pad_char x $pad_len);
  77. }
  78. # Return the next multiple of block_size larger than or equal to $data_len.
  79. sub paddedSize {
  80. my($data_len) = @_;
  81. use integer;
  82. return (($data_len - 1) / $block_size) * $block_size + $block_size;
  83. }
  84. # Return the number of block_size blocks required to hold $data_len.
  85. sub numBlocks {
  86. my($data_len) = @_;
  87. use integer;
  88. return (($data_len - 1) / $block_size) + 1;
  89. }
  90. # Pack the name, address, size and optional skip regions of a partition entry into binary form.
  91. sub createPartitionEntry {
  92. my($name, $flash_base, $size, $skips) = @_;
  93. my $entry;
  94. my($zero_long) = 0x0000;
  95. # Pack the partition entry according to the format that RedBoot (and the MTD partition parsing code) requires.
  96. $entry = pack("a16N5x212N2",$name,$flash_base,$zero_long,$size,$zero_long,$zero_long,$zero_long,$zero_long);
  97. # Optionally put a skip header into the padding area.
  98. if (defined $skips) {
  99. my $i = scalar(@$skips);
  100. foreach my $region (@$skips) {
  101. substr($entry, -8 - 12*$i, 12) =
  102. pack("a4N2", "skip", $region->{'offset'}, $region->{'size'});
  103. $i--;
  104. }
  105. }
  106. return $entry;
  107. }
  108. # Parse partition entry and return anon array ref [$name, $offset, $size, $skip] or return 0 on partition terminator.
  109. sub parsePartitionEntry {
  110. my($partition_entry) = @_;
  111. my($entry_len) = 0x100;
  112. length($partition_entry) eq $entry_len or die "parsePartitionEntry: partition entry length is not $entry_len!\n";
  113. # Unpack the partition table entry, saving those values in which we are interested.
  114. my($name, $flash_base, $size, $dummy_long, $padding, $skips);
  115. ($name, $flash_base, $dummy_long, $size, $dummy_long, $dummy_long, $padding, $dummy_long, $dummy_long) =
  116. unpack("a16N5a212N2",$partition_entry);
  117. # A partition entry starting with 0xFF terminates the table.
  118. if (unpack("C", $name) eq 0xff) {
  119. # %%% FIXME: This should only skip, not terminate. %%%
  120. $debug and print "Found terminator for <FIS directory>\n";
  121. return 0;
  122. }
  123. # Remove trailing nulls from the partition name.
  124. $name =~ s/\000+//;
  125. # Extract the skip regions out of the padding area.
  126. $padding =~ s/^\000+//;
  127. $padding =~ s/\000*skip(........)\000*/$1/g;
  128. $padding =~ s/\000+$//;
  129. # Store the skip regions in an array for later use.
  130. while (length($padding)) {
  131. my $region = {};
  132. ($region->{'offset'}, $region->{'size'}) =
  133. unpack("N2", $padding);
  134. $debug and printf("Found skip region at 0x%05X, size 0x%05X\n",
  135. $region->{'offset'}, $region->{'size'});
  136. push(@$skips, $region);
  137. $padding = substr($padding,8);
  138. }
  139. return [$name, $flash_base - $flash_start, $size, $skips];
  140. }
  141. # Return partition table from data is one exists, otherwise return 0.
  142. sub findPartitionTable {
  143. my($data_buf) = @_;
  144. unpack("a7", $data_buf) eq 'RedBoot' or return 0;
  145. return substr($data_buf, 0, 0x1000)
  146. }
  147. # Parse partition table and return array of anonymous array references ([$name, $offset, $size, $skips], ...).
  148. sub parsePartitionTable {
  149. my($partition_table) = @_;
  150. my(@partitions, $fields_ref);
  151. my($entry_len) = 0x100;
  152. my($partition_count) = 0;
  153. # Loop through the fixed size partition table entries, and store the entries in @partitions.
  154. # %%% FIXME: This doesn't handle the case of a completely full partition table. %%%
  155. while ($fields_ref = parsePartitionEntry(substr($partition_table, $partition_count * $entry_len, $entry_len))) {
  156. $debug and printf("Found <%s> at 0x%08X (%s)%s\n", $fields_ref->[0], $fields_ref->[1],
  157. ($fields_ref->[2] >= $block_size ?
  158. sprintf("%d blocks", numBlocks($fields_ref->[2])) :
  159. sprintf("0x%05X bytes", $fields_ref->[2])),
  160. (defined $fields_ref->[3] ?
  161. sprintf(" [%s]",
  162. join(", ",
  163. map { sprintf("0x%05X/0x%05X", $_->{'offset'},$_->{'size'}) }
  164. @{$fields_ref->[3]})) :
  165. ""));
  166. $partitions[$partition_count++] = $fields_ref;
  167. }
  168. return(@partitions);
  169. }
  170. # Create an empty jffs2 block.
  171. sub jffs2Block {
  172. return padBytes(pack("N3", 0x19852003, 0x0000000c, 0xf060dc98), $block_size);
  173. }
  174. # Write out $data to $filename,
  175. sub writeOut {
  176. my($data, $filename) = @_;
  177. open FILE,">$filename" or die "Can't open file \"$filename\": $!\n";
  178. if (defined($data)) { print FILE $data;}
  179. close FILE or die "Can't close file \"$filename\": $!\n";
  180. }
  181. # Not used at the moment.
  182. sub trailerData {
  183. my($product_id) = 0x0001;
  184. my($protocol_id) = 0x0000;
  185. my($firmware_version) = 0x2325;
  186. my($unknown1) = 0x90f7;
  187. my($magic_number) = 'eRcOmM';
  188. my($unknown2) = 0x00b9;
  189. return pack("n4a6n",$product_id,$protocol_id,$firmware_version,$unknown1,$magic_number,$unknown2);
  190. }
  191. # Print the contents of the Sercomm RedBoot trailer.
  192. sub printRedbootTrailer {
  193. my($redboot_data) = @_;
  194. my($correct_redboot_len) = 0x40000;
  195. my($redboot_data_len) = length($redboot_data);
  196. if ($redboot_data_len != $correct_redboot_len) {
  197. printf("Redboot length (0x%08X) is not 0x%08X\n", $redboot_data_len, $correct_redboot_len);
  198. return;
  199. }
  200. # The trailer is the last 80 bytes of the redboot partition.
  201. my($redboot_trailer) = substr($redboot_data, -80);
  202. writeOut($redboot_trailer, 'RedbootTrailer');
  203. my($mac_addr0, $mac_addr1, $mac_addr2, $unknown, $prefix, $ver_ctrl, $down_ctrl, $hid, $hver, $prodid, $prodidmask,
  204. $protid, $protidmask, $funcid, $funcidmask, $fver, $cseg, $csize, $postfix) =
  205. unpack("n3Na7n2a32n10a7",$redboot_trailer);
  206. printf("MAC address is %04X%04X%04X\n", $mac_addr0, $mac_addr1, $mac_addr2);
  207. printf("unknown: %08X\n", $unknown);
  208. printf("%s:%04X:%04X:%s\n", $prefix, $ver_ctrl, $down_ctrl, $postfix);
  209. printf("VerControl: %04X\nDownControl: %04X\n", $ver_ctrl, $down_ctrl);
  210. printf("hid: %04X %04X %04X %04X %04X %04X %04X %04X %04X %04X %04X %04X %04X %04X %04X %04X\n", unpack("n16", $hid));
  211. printf("Hver: %04X\nProdID: %04X\nProtID: %04X\nFuncID: %04X\nFver: %04X\nCseg: %04X\nCsize: %04X\n",
  212. $hver, $prodid, $protid, $funcid, $fver, $cseg, $csize);
  213. }
  214. # remove the optional Loader partition
  215. sub removeOptionalLoader {
  216. my($partitions_ref) = @_;
  217. my $index;
  218. my $count = 0;
  219. map {
  220. if (not defined $index) {
  221. if ($_->{'name'} eq "Loader") {
  222. $index = $count;
  223. }
  224. $count++;
  225. }
  226. } @$partitions_ref;
  227. defined $index or die "Cannot find the Loader partition\n";
  228. splice(@$partitions_ref, $index, 1);
  229. # Set fixed offsets and sizes for Kernel and Ramdisk
  230. map {
  231. if ($_->{'name'} eq 'Kernel') {
  232. $_->{'offset'} = $kernel_offset;
  233. $_->{'size'} = $kernel_size;
  234. $_->{'variable'} = 0;
  235. }
  236. if ($_->{'name'} eq 'Ramdisk') {
  237. $_->{'offset'} = $ramdisk_offset;
  238. }
  239. } @$partitions_ref;
  240. return;
  241. }
  242. # populate @partitions based on the firmware's partition table
  243. sub spliceFirmwarePartitions {
  244. my($firmware_buf, $partitions_ref) = @_;
  245. # we know that partition table, if it exists, begins at start of 'FIS directory' and has max length 0x1000
  246. my($partition_table);
  247. map {
  248. $_->{'name'} eq 'FIS directory' and
  249. $partition_table = findPartitionTable(substr($firmware_buf, $_->{'offset'}, $_->{'size'}));
  250. } @$partitions_ref;
  251. # return 0 here if no partition table in FIS directory
  252. return if not $partition_table;
  253. my @new_partitions = parsePartitionTable($partition_table);
  254. # Remove the optional second stage bootloader if it is not found in the FIS directory.
  255. if (not grep { $_->[0] eq 'Loader' } @new_partitions) {
  256. removeOptionalLoader($partitions_ref);
  257. }
  258. my($partition_count) = 0;
  259. my($splice) = 0;
  260. map {
  261. # Skip pseudo partitions.
  262. while (($partition_count < scalar(@$partitions_ref)) and
  263. $partitions_ref->[$partition_count]->{'pseudo'}) {
  264. $debug and printf("Skipped <%s> (pseudo partition)\n", $partitions_ref->[$partition_count]->{'name'});
  265. $partition_count++;
  266. }
  267. # If we are in a variable area, and we haven't reached the end of it,
  268. # then splice in another partition for use by the later code.
  269. if ($splice and ($partitions_ref->[$partition_count]->{'name'} ne $_->[0])) {
  270. $debug and printf("Splicing new partition <%s> before <%s>\n",
  271. $_->[0], $partitions_ref->[$partition_count]->{'name'});
  272. splice(@{$partitions_ref}, $partition_count, 0, ({'name' => "",'variable'=>1,'header'=>0}));
  273. }
  274. my $partition = $partitions_ref->[$partition_count];
  275. # Variable partitions can be overridden by the real FIS directory
  276. if ($partition->{'variable'}) {
  277. # Only override the filename if the partition name is not set or doesn't match
  278. if ($partition->{'name'} ne $_->[0]) {
  279. if (length($partition->{'name'})) {
  280. $debug and printf("Overwriting <%s> with <%s>\n",
  281. $partitions_ref->[$partition_count]->{'name'}, $_->[0]);
  282. }
  283. $partition->{'name'} = $_->[0];
  284. $partition->{'file'} = $_->[0];
  285. }
  286. # Set the offset, size and skips based on the real partition table
  287. $partition->{'offset'} = $_->[1];
  288. $partition->{'size'} = $_->[2];
  289. $partition->{'skips'} = $_->[3];
  290. $debug and printf("Locating <%s> at 0x%08X (%s)\n",
  291. $partition->{'name'}, $partition->{'offset'},
  292. ($partition->{'size'} >= $block_size ?
  293. sprintf("%d blocks", numBlocks($partition->{'size'})) :
  294. sprintf("0x%05X bytes", $partition->{'size'})));
  295. $splice = 1;
  296. }
  297. # Fixed partitions cannot be overridden
  298. else {
  299. ($partition->{'name'} eq $_->[0]) or
  300. die "Unexpected partition <",$_->[0],"> (expecting <",$partition->{'name'},">)\n";
  301. $debug and printf("Locating <%s> at 0x%08X (%s)\n",
  302. $partition->{'name'}, $partition->{'offset'},
  303. ($partition->{'size'} >= $block_size ?
  304. sprintf("%d blocks", numBlocks($partition->{'size'})) :
  305. sprintf("0x%05X bytes", $partition->{'size'})));
  306. $splice = 0;
  307. }
  308. $partition_count++;
  309. } @new_partitions;
  310. return;
  311. }
  312. # Read in an 8MB firmware file, and store the data into @partitions.
  313. # Note that the data is only stored in a partition if 'offset' and 'size' are defined,
  314. # and it does not already have data stored in it.
  315. sub readInFirmware {
  316. my($filename, $partitions_ref) = @_;
  317. my($firmware_buf);
  318. my($total_length) = 0x800000;
  319. open FILE,$filename or die "Can't find firmware image \"$filename\": $!\n";
  320. read FILE,$firmware_buf,$total_length or die "Can't read $total_length bytes from \"$filename\": $!\n";
  321. close FILE or die "Can't close \"$filename\": $!\n";
  322. $debug and printf("Read 0x%08X bytes from \"%s\"\n", length($firmware_buf), $filename);
  323. spliceFirmwarePartitions($firmware_buf, $partitions_ref);
  324. # Read the parts of the firmware file into the partitions table.
  325. map {
  326. if (defined $_->{'offset'} and defined $_->{'size'}) {
  327. if (defined $_->{'data'}) {
  328. $debug and printf("Not overwriting data in <%s>\n", $_->{'name'});
  329. }
  330. else {
  331. # Slurp up the data, based on whether a header and/or data is present or not
  332. if ($_->{'header'}) {
  333. # Read the length, and grab the data based on the length.
  334. my($data_len) = unpack("N", substr($firmware_buf, $_->{'offset'}));
  335. # A length of 0xFFFFFFFF means that the area is not initialised
  336. if ($data_len != 0xFFFFFFFF) {
  337. $debug and printf("Found header size of 0x%08X bytes for <%s>\n", $data_len, $_->{'name'});
  338. $_->{'data'} = substr($firmware_buf, $_->{'offset'} + $_->{'header'}, $data_len);
  339. }
  340. }
  341. elsif ($_->{'pseudo'} and not defined $_->{'file'} and
  342. (substr($firmware_buf, $_->{'offset'}, $_->{'size'}) eq
  343. (pack("C", 0xff) x $_->{'size'}))) {
  344. $debug and printf("Skipping empty pseudo partition <%s>\n", $_->{'name'});
  345. }
  346. else {
  347. # Grab the whole partition, using the maximum size.
  348. $_->{'data'} = substr($firmware_buf, $_->{'offset'}, $_->{'size'});
  349. }
  350. # If skip regions are defined, remove them from the data.
  351. if (defined $_->{'skips'}) {
  352. my $removed = 0;
  353. foreach my $region (@{$_->{'skips'}}) {
  354. if (($region->{'offset'} > 0) or
  355. not ($_->{'header'} > 0)) {
  356. $debug and printf("Removing 0x%05X bytes from offset 0x%05X\n",
  357. $region->{'size'}, $region->{'offset'});
  358. $region->{'data'} = substr($_->{'data'}, $region->{'offset'} - $removed, $region->{'size'}, '');
  359. }
  360. $removed += $region->{'size'};
  361. }
  362. }
  363. $quiet or defined $_->{'data'} and printf("Read %s into <%s>\n",
  364. (length($_->{'data'}) >= $block_size ?
  365. sprintf("%d blocks", numBlocks(length($_->{'data'}))) :
  366. sprintf("0x%05X bytes", length($_->{'data'}))), $_->{'name'});
  367. }
  368. }
  369. } @$partitions_ref;
  370. }
  371. # Write the partition data stored in memory out into the files associated with each.
  372. sub writeOutFirmwareParts {
  373. my(@partitions) = @_;
  374. # Write out the parts of the firmware file.
  375. map {
  376. # We can only write if 'data' and 'file' are defined.
  377. if (defined $_->{'file'} and defined $_->{'data'} and length($_->{'data'})) {
  378. writeOut($_->{'data'}, $_->{'file'});
  379. $quiet or printf("Wrote 0x%08X bytes from <%s> into \"%s\"\n",
  380. length($_->{'data'}), $_->{'name'}, $_->{'file'});
  381. }
  382. else {
  383. $debug and printf("Skipping <%s> (%s)\n", $_->{'name'},
  384. (not defined $_->{'file'}) ?
  385. "no filename specified" :
  386. "no data to write");
  387. }
  388. } @partitions;
  389. return;
  390. }
  391. # Read in the partition data from the files associated with each and store in memory.
  392. sub readInFirmwareParts {
  393. my(@partitions) = (@_);
  394. undef $/; # we want to slurp
  395. map {
  396. my $file = $_->{'file'};
  397. if (defined $file) {
  398. open FILE,$file or die "Can't find firmware part \"$file\": $!\n";
  399. # Slurp in the data
  400. $_->{'data'} = <FILE>;
  401. # close the file
  402. close FILE or die "Can't close file \"$file\": $!\n";
  403. # Optionally byteswap the data
  404. if ($_->{'byteswap'}) {
  405. # Byte swap the data (which has to be padded to a multiple of 4 bytes).
  406. $_->{'data'} = pack("N*", unpack("V*", $_->{'data'}.pack("CCC", 0)));
  407. }
  408. # Keep track of the actual size.
  409. my $size;
  410. if ($_->{'header'}) {
  411. if ($_->{'pseudo'}) {
  412. $size = $_->{'header'} + length($_->{'data'});
  413. }
  414. else {
  415. $size = paddedSize($_->{'header'} + length($_->{'data'}));
  416. }
  417. }
  418. elsif (not $_->{'pseudo'}) {
  419. $size = paddedSize(length($_->{'data'}));
  420. }
  421. else {
  422. $size = length($_->{'data'});
  423. }
  424. # Check to make sure the file contents are not too large.
  425. if (defined $_->{'size'} and ($size > $_->{'size'})) {
  426. die sprintf("Ran out of flash space in <%s> - %s too large.\n", $_->{'name'},
  427. sprintf("0x%05X bytes", ($size - $_->{'size'})));
  428. }
  429. # If the partition does not have a fixed size, the calculate the size.
  430. if (not defined $_->{'size'}) {
  431. $_->{'size'} = $size;
  432. }
  433. # Keep the user appraised ...
  434. $quiet or printf("Read 0x%08X bytes from \"%s\" into <%s> (%s / %s)%s\n",
  435. length($_->{'data'}), $_->{'file'}, $_->{'name'},
  436. ($size >= $block_size ?
  437. sprintf("%d blocks", numBlocks($size)) :
  438. sprintf("0x%05X bytes", $size)),
  439. ($_->{'size'} >= $block_size ?
  440. sprintf("%d blocks", numBlocks($_->{'size'})) :
  441. sprintf("0x%05X bytes", $_->{'size'})),
  442. ($_->{'byteswap'} ? " (byte-swapped)" : ""));
  443. }
  444. } @partitions;
  445. return;
  446. }
  447. # layoutPartitions : this function must be ugly - it needs to verify RedBoot, SysConf, Kernel, Ramdisk, and
  448. # FIS directory partitions exist, are in the correct order, and do not have more data than can fit in
  449. # their lengths (fixed for all but Ramdisk, which has a minimum length of one block).
  450. # If Rootdisk and/or Userdisk exist, it must also verify that their block padded lengths are not
  451. # too great for the available space.
  452. # input : an array of hashes, some of which are populated with data
  453. # output: same reference with start and size (partition not data) also populated. this populated structure
  454. # can then be passed to buildPartitionTable() to generate the actual partition table data
  455. sub layoutPartitions {
  456. my(@partitions) = @_;
  457. # Find the kernel partition, and save a pointer to it for later use
  458. my $kernel;
  459. map { ($_->{'name'} eq "Kernel") && ($kernel = $_); } @partitions;
  460. $kernel or die "Couldn't find the kernel partition\n";
  461. # Find the last variable size partition, and save a pointer to it for later use
  462. my $lastdisk;
  463. my $directory_offset;
  464. my $curdisk = $partitions[0];
  465. map {
  466. if (not defined $lastdisk) {
  467. if ($_->{'name'} eq "FIS directory") {
  468. $lastdisk = $curdisk;
  469. $directory_offset = $_->{'offset'};
  470. }
  471. else {
  472. $curdisk = $_;
  473. }
  474. }
  475. } @partitions;
  476. $lastdisk or die "Couldn't find the last variable size partition\n";
  477. $debug and printf("Last variable size partition is <%s>\n", $lastdisk->{'name'});
  478. #
  479. # here we go through the $partitions array ref and fill in all the values
  480. #
  481. # This points to where the next partition should be placed.
  482. my $pointer = $flash_start;
  483. map {
  484. $debug and printf("Pointer is 0x%08X\n", $pointer);
  485. # Determine the start and offset of the current partition.
  486. if (defined $_->{'offset'}) {
  487. $_->{'start'} = $flash_start + $_->{'offset'};
  488. # Check for running past the defined start of the partition.
  489. if (($pointer > $_->{'start'}) and not $_->{'pseudo'}) {
  490. die sprintf("Ran out of flash space before <%s> - %s too large.\n", $_->{'name'},
  491. sprintf("0x%05X bytes", ($pointer - $_->{'start'})));
  492. }
  493. }
  494. # If offset is not defined, then calculate it.
  495. else {
  496. $_->{'start'} = $pointer;
  497. $_->{'offset'} = $_->{'start'} - $flash_start;
  498. }
  499. my $size = defined $_->{'data'} ? length($_->{'data'}) : 0;
  500. # Add skip regions for the partitions with headers.
  501. if ($_->{'header'} > 0) {
  502. # Define the skip region for the initial Sercomm header.
  503. push(@{$_->{'skips'}},
  504. { 'offset' => 0, 'size' => $_->{'header'}, 'data' => undef });
  505. # Allow for the Sercomm header to be prepended to the data.
  506. $size += $_->{'header'};
  507. # Determine if the partition overlaps the ramdisk boundary.
  508. if (($_->{'offset'} < $ramdisk_offset) and
  509. (($_->{'offset'} + $size) > $ramdisk_offset)) {
  510. # Define the skip region for the inline Sercomm header.
  511. push(@{$_->{'skips'}},
  512. { 'offset' => ($ramdisk_offset - $_->{'offset'}), 'size' => 16,
  513. 'data' => pack("N4", $block_size) });
  514. # Allow for the Sercomm header to be inserted in the data.
  515. $size += 16;
  516. }
  517. }
  518. # Partitions without headers cannot have skip regions.
  519. elsif (($_->{'offset'} <= $ramdisk_offset) and
  520. (($_->{'offset'} + $size) > $ramdisk_offset)) {
  521. # Pad the kernel until it extends past the ramdisk offset.
  522. push(@{$kernel->{'skips'}},
  523. { 'offset' => ($ramdisk_offset - $kernel->{'offset'}), 'size' => 16,
  524. 'data' => pack("N4", $block_size) });
  525. $kernel->{'size'} = $ramdisk_offset - $kernel->{'offset'} + $block_size;
  526. $kernel->{'data'} = padBytes($kernel->{'data'},
  527. $kernel->{'size'} - $kernel->{'header'} - 16);
  528. $_->{'offset'} = $ramdisk_offset + $block_size;
  529. $_->{'start'} = $flash_start + $_->{'offset'};
  530. $pointer = $_->{'start'};
  531. $debug and printf("Extending kernel partition past ramdisk offset.\n");
  532. }
  533. # If this is the last variable size partition, then fill the rest of the space.
  534. if ($_->{'name'} eq $lastdisk->{'name'}) {
  535. $_->{'size'} = paddedSize($directory_offset + $flash_start - $pointer);
  536. $debug and printf("Padding last variable partition <%s> to 0x%08X bytes\n", $_->{'name'}, $_->{'size'});
  537. }
  538. die sprintf("Partition size not defined in <%s>.\n", $_->{'name'})
  539. unless defined $_->{'size'};
  540. # Extend to another block if required.
  541. if ($size > $_->{'size'}) {
  542. if ($_->{'name'} eq $lastdisk->{'name'}) {
  543. die sprintf("Ran out of flash space in <%s> - %s too large.\n", $_->{'name'},
  544. sprintf("0x%05X bytes", ($size - $_->{'size'})));
  545. }
  546. $_->{'size'} = $size;
  547. printf("Extending partition <%s> to 0x%08X bytes\n", $_->{'name'}, $_->{'size'});
  548. }
  549. # Keep the user appraised ...
  550. $debug and printf("Allocated <%s> from 0x%08X to 0x%08X (%s / %s)\n",
  551. $_->{'name'}, $_->{'start'}, $_->{'start'} + $_->{'size'},
  552. ($size >= $block_size ?
  553. sprintf("%d blocks", numBlocks($size)) :
  554. sprintf("0x%05X bytes", $size)),
  555. ($_->{'size'} >= $block_size ?
  556. sprintf("%d blocks", numBlocks($_->{'size'})) :
  557. sprintf("0x%05X bytes", $_->{'size'})));
  558. # Check to make sure we have not run out of room.
  559. if (($_->{'start'} + $_->{'size'}) > ($flash_start + $flash_len)) {
  560. die "Ran out of flash space in <", $_->{'name'}, ">\n";
  561. }
  562. $debug and printf("Moving pointer from 0x%08X to 0x%08X (0x%08X + 0x%08X)\n",
  563. $pointer, paddedSize($_->{'start'} + $_->{'size'}),
  564. $_->{'start'}, $_->{'size'});
  565. # Move the pointer up, in preparation for the next partition.
  566. $pointer = paddedSize($_->{'start'} + $_->{'size'});
  567. } @partitions;
  568. return;
  569. }
  570. sub buildPartitionTable {
  571. my(@partitions) = @_;
  572. my($flash_start) = 0x50000000;
  573. my($partition_data) = '';
  574. map {
  575. # Collate the partition data for all known partitions.
  576. if (not $_->{'pseudo'} and defined $_->{'offset'} and defined $_->{'size'}) {
  577. # Pack and append the binary table entry for this partition.
  578. $partition_data .= createPartitionEntry($_->{'name'}, $_->{'offset'} + $flash_start,
  579. $_->{'size'}, $_->{'skips'});
  580. # If this is the FIS directory, then write the partition table data into it.
  581. if ($_->{'name'} eq "FIS directory") {
  582. # Explicitly terminate the partition data.
  583. $partition_data .= pack("C",0xff) x 0x100;
  584. $_->{'data'} = padBytes($partition_data, $_->{'size'});
  585. }
  586. my $size = length($_->{'data'});
  587. # Keep the user appraised ...
  588. $debug and printf("Table entry <%s> from 0x%08X to 0x%08X (%s / %s)%s\n",
  589. $_->{'name'}, $_->{'start'}, $_->{'start'} + $_->{'size'},
  590. ($size >= $block_size ?
  591. sprintf("%d blocks", numBlocks($size)) :
  592. sprintf("0x%05X bytes", $size)),
  593. ($_->{'size'} >= $block_size ?
  594. sprintf("%d blocks", numBlocks($_->{'size'})) :
  595. sprintf("0x%05X bytes", $_->{'size'})),
  596. (defined $_->{'skips'} ?
  597. sprintf("\nTable entry <%s> skip %s", $_->{'name'},
  598. join(", ",
  599. map { sprintf("0x%08X to 0x%08X", $_->{'offset'},
  600. $_->{'offset'} + $_->{'size'} - 1) }
  601. @{$_->{'skips'}})) :
  602. "")
  603. );
  604. }
  605. else {
  606. $debug and print "No table entry required for <", $_->{'name'}, ">\n";
  607. }
  608. } @partitions;
  609. return;
  610. }
  611. sub writeOutFirmware {
  612. my($filename, @partitions) = @_;
  613. # Clear the image to start.
  614. my $image_buf = "";
  615. map {
  616. # We can only write a partition if it has an offset, a size, and some data to write.
  617. if (defined $_->{'offset'} and defined $_->{'size'} and defined $_->{'data'}) {
  618. # Keep track of the end of the image.
  619. my $end_point = length($image_buf);
  620. # If the next partition is well past the end of the current image, then pad it.
  621. if ($_->{'offset'} > $end_point) {
  622. $image_buf .= padBytes("", $_->{'offset'} - $end_point);
  623. $quiet or printf("Padded %s before <%s> in \"%s\"\n",
  624. ((length($image_buf) - $end_point) >= $block_size ?
  625. sprintf("%d blocks", numBlocks(length($image_buf) - $end_point)) :
  626. sprintf("0x%05X bytes", length($image_buf) - $end_point)),
  627. $_->{'name'}, $filename);
  628. }
  629. # If the next parition is before the end of the current image, then rewind.
  630. elsif ($_->{'offset'} < $end_point) {
  631. $debug and printf("Rewound %s before <%s> in \"%s\"\n",
  632. (($end_point - $_->{'offset'}) >= $block_size ?
  633. sprintf("%d blocks", numBlocks($end_point - $_->{'offset'})) :
  634. sprintf("0x%05X bytes", $end_point - $_->{'offset'})),
  635. $_->{'name'}, $filename);
  636. # if (($end_point - $_->{'offset'}) >= $block_size) {
  637. # die "Allocation error: rewound a full block or more ...\n";
  638. # }
  639. }
  640. # If skip regions are defined, add them to the data.
  641. if (defined $_->{'skips'}) {
  642. my $added = 0;
  643. foreach my $region (@{$_->{'skips'}}) {
  644. if (($region->{'offset'} > 0) or
  645. not ($_->{'header'} > 0)) {
  646. $debug and printf("Inserted 0x%05X bytes (at offset 0x%05X) into <%s>\n",
  647. $region->{'size'}, $region->{'offset'}, $_->{'name'});
  648. substr($_->{'data'},
  649. $region->{'offset'} + $added - $_->{'header'},
  650. 0, $region->{'data'});
  651. $added += $region->{'size'};
  652. }
  653. }
  654. }
  655. # Splice the data into the image at the appropriate place, padding as required.
  656. substr($image_buf, $_->{'offset'}, $_->{'size'},
  657. $_->{'header'} ?
  658. padBytes(pack("N4",length($_->{'data'})).$_->{'data'}, $_->{'size'}) :
  659. padBytes($_->{'data'}, $_->{'size'}));
  660. # Keep the user appraised ...
  661. $quiet or printf("Wrote %s (0x%08X to 0x%08X) from <%s> into \"%s\"\n",
  662. ($_->{'size'} >= $block_size ?
  663. sprintf("%2d blocks", numBlocks($_->{'size'})) :
  664. sprintf("0x%05X bytes", $_->{'size'})),
  665. $_->{'offset'}, $_->{'offset'}+$_->{'size'}, $_->{'name'}, $filename);
  666. }
  667. # If we are not able to write a partition, then give debug information about why.
  668. else {
  669. $debug and printf("Skipping <%s> (%s)\n", $_->{'name'},
  670. (not defined $_->{'offset'}) ? "no offset defined" :
  671. ((not defined $_->{'size'}) ? "no size defined" :
  672. "no data available"));
  673. }
  674. } @partitions;
  675. # Write the image to the specified file.
  676. writeOut($image_buf, $filename);
  677. return;
  678. }
  679. # checkPartitionTable: sanity check partition table - for testing but might evolve into setting @partitions
  680. # so that we can write out jffs2 partitions from a read image
  681. # currently not nearly paranoid enough
  682. sub checkPartitionTable {
  683. my($data) = @_;
  684. my($pointer) = 0;
  685. my($entry);
  686. my($name, $flash_base, $size, $done, $dummy_long, $padding);
  687. do {
  688. $entry = substr($data, $pointer, 0x100);
  689. ($name,$flash_base,$dummy_long,$size,$dummy_long,$dummy_long,$padding,$dummy_long,$dummy_long) = unpack("a16N5x212N2",$entry);
  690. $name =~ s/\0//g;
  691. $debug and printf("pointer: %d\tname: %s%sflash_base: 0x%08X\tsize: 0x%08X\n",
  692. $pointer, $name, (" " x (16 - length($name))), $flash_base, $size);
  693. $pointer += 0x100;
  694. $debug and printf("terminator: 0x%08X\n", unpack("C", substr($data, $pointer, 1)));
  695. if (unpack("C", substr($data, $pointer, 1)) eq 0xff) {
  696. $done = 1;
  697. }
  698. } until $done;
  699. }
  700. sub printPartitions {
  701. my(@partitions) = @_;
  702. my($offset, $size, $skips);
  703. map {
  704. # defined $_->{'size'} ? $size = $_->{'size'} : $size = undef;
  705. if (defined $_->{'size'}) {
  706. $size = $_->{'size'};
  707. }
  708. else {
  709. $size = undef;
  710. }
  711. if (defined $_->{'offset'}) {
  712. $offset = $_->{'offset'};
  713. }
  714. else {
  715. $offset = undef;
  716. }
  717. if (defined $_->{'skips'}) {
  718. $skips = $_->{'skips'};
  719. }
  720. else {
  721. $skips = undef;
  722. }
  723. printf("%s%s", $_->{'name'}, (" " x (16 - length($_->{'name'}))));
  724. if (defined $offset) { printf("0x%08X\t", $offset); } else { printf("(undefined)\t"); };
  725. if (defined $size) { printf("0x%08X", $size); } else { printf("(undefined)"); };
  726. if (defined $skips) {
  727. printf("\t[%s]",
  728. join(", ",
  729. map { sprintf("0x%05X/0x%05X", $_->{'offset'}, $_->{'size'}); }
  730. @$skips));
  731. }
  732. printf("\n");
  733. } @partitions;
  734. }
  735. sub defaultPartitions {
  736. return ({'name'=>'RedBoot', 'file'=>'RedBoot',
  737. 'offset'=>0x00000000, 'size'=>0x00040000,
  738. 'variable'=>0, 'header'=>0, 'pseudo'=>0, 'data'=>undef, 'byteswap'=>0},
  739. {'name'=>'EthAddr', 'file'=>undef,
  740. 'offset'=>0x0003ffb0, 'size'=>0x00000006,
  741. 'variable'=>0, 'header'=>0, 'pseudo'=>1, 'data'=>undef, 'byteswap'=>0},
  742. {'name'=>'SysConf', 'file'=>'SysConf',
  743. 'offset'=>0x00040000, 'size'=>0x00020000,
  744. 'variable'=>0, 'header'=>0, 'pseudo'=>0, 'data'=>undef, 'byteswap'=>0},
  745. {'name'=>'Loader', 'file'=>'apex.bin',
  746. 'offset'=>undef, 'size'=>undef,
  747. 'variable'=>1, 'header'=>16, 'pseudo'=>0, 'data'=>undef, 'byteswap'=>0},
  748. {'name'=>'Kernel', 'file'=>'vmlinuz',
  749. 'offset'=>undef, 'size'=>undef,
  750. 'variable'=>1, 'header'=>16, 'pseudo'=>0, 'data'=>undef, 'byteswap'=>0},
  751. {'name'=>'Ramdisk', 'file'=>'ramdisk.gz',
  752. 'offset'=>undef, 'size'=>undef,
  753. 'variable'=>1, 'header'=>16, 'pseudo'=>0, 'data'=>undef, 'byteswap'=>0},
  754. {'name'=>'FIS directory', 'file'=>undef,
  755. 'offset'=>0x007e0000, 'size'=>0x00020000,
  756. 'variable'=>0, 'header'=>0, 'pseudo'=>0, 'data'=>undef, 'byteswap'=>0},
  757. {'name'=>'Loader config', 'file'=>undef,
  758. 'offset'=>0x007f8000, 'size'=>0x00004000,
  759. 'variable'=>0, 'header'=>0, 'pseudo'=>1, 'data'=>undef, 'byteswap'=>0},
  760. {'name'=>'Microcode', 'file'=>'NPE-B',
  761. 'offset'=>0x007fc000, 'size'=>0x00003000,
  762. 'variable'=>0, 'header'=>16, 'pseudo'=>1, 'data'=>undef, 'byteswap'=>0},
  763. {'name'=>'Trailer', 'file'=>'Trailer',
  764. 'offset'=>0x007ffff0, 'size'=>0x00000010,
  765. 'variable'=>0, 'header'=>0, 'pseudo'=>1, 'data'=>undef, 'byteswap'=>0});
  766. }
  767. # Main routine starts here ...
  768. my($unpack, $pack, $little, $input, $output, $redboot);
  769. my($kernel, $sysconf, $ramdisk, $fisdir);
  770. my($microcode, $trailer, $ethaddr, $loader);
  771. END {
  772. # Remove temporary files
  773. for my $file (@cleanup) {
  774. unlink $file;
  775. }
  776. }
  777. if (!GetOptions("d|debug" => \$debug,
  778. "q|quiet" => \$quiet,
  779. "u|unpack" => \$unpack,
  780. "p|pack" => \$pack,
  781. "l|little" => \$little,
  782. "i|input=s" => \$input,
  783. "o|output=s" => \$output,
  784. "b|redboot=s" => \$redboot,
  785. "k|kernel=s" => \$kernel,
  786. "s|sysconf=s" => \$sysconf,
  787. "r|ramdisk=s" => \$ramdisk,
  788. "f|fisdir=s" => \$fisdir,
  789. "m|microcode=s" => \$microcode,
  790. "t|trailer=s" => \$trailer,
  791. "e|ethaddr=s" => \$ethaddr,
  792. "L|loader=s" => \$loader,
  793. ) or (not defined $pack and not defined $unpack)) {
  794. print "Usage: slugimage <options>\n";
  795. print "\n";
  796. print " [-d|--debug] Turn on debugging output\n";
  797. print " [-q|--quiet] Turn off status messages\n";
  798. print " [-u|--unpack] Unpack a firmware image\n";
  799. print " [-p|--pack] Pack a firmware image\n";
  800. print " [-l|--little] Convert Kernel and Ramdisk to little-endian\n";
  801. print " [-i|--input] <file> Input firmware image filename\n";
  802. print " [-o|--output] <file> Output firmware image filename\n";
  803. print " [-b|--redboot] <file> Input/Output RedBoot filename\n";
  804. print " [-s|--sysconf] <file> Input/Output SysConf filename\n";
  805. print " [-L|--loader] <file> Second stage boot loader filename\n";
  806. print " [-k|--kernel] <file> Input/Ouptut Kernel filename\n";
  807. print " [-r|--ramdisk] <file> Input/Output Ramdisk filename(s)\n";
  808. print " [-f|--fisdir] <file> Input/Output FIS directory filename\n";
  809. print " [-m|--microcode] <file> Input/Output Microcode filename\n";
  810. print " [-t|--trailer] <file> Input/Output Trailer filename\n";
  811. print " [-e|--ethaddr] <AABBCCDDEEFF> Set the Ethernet address\n";
  812. # %%% TODO %%% Document --ramdisk syntax
  813. exit 1;
  814. }
  815. my(@partitions) = defaultPartitions();
  816. if ($pack) {
  817. die "Output filename must be specified\n" unless defined $output;
  818. # If we're creating an image and no RedBoot, SysConf partition is
  819. # explicitly specified, simply write an empty one as the upgrade tools
  820. # don't touch RedBoot and SysConf anyway. If no Trailer is specified,
  821. # put in one.
  822. if (not defined $redboot and not -e "RedBoot") {
  823. $redboot = tmpnam();
  824. open TMP, ">$redboot" or die "Cannot open file $redboot: $!";
  825. push @cleanup, $redboot;
  826. # The RedBoot partition is 256 * 1024 = 262144; the trailer we add
  827. # is 70 bytes.
  828. print TMP "\0"x(262144-70);
  829. # Upgrade tools check for an appropriate Sercomm trailer.
  830. for my $i (@sercomm_redboot_trailer) {
  831. print TMP pack "S", $i;
  832. }
  833. close TMP;
  834. }
  835. if (not defined $sysconf and not -e "SysConf") {
  836. $sysconf = tmpnam();
  837. open TMP, ">$sysconf" or die "Cannot open file $sysconf: $!";
  838. push @cleanup, $sysconf;
  839. # The SysConf partition is 128 * 1024 = 131072
  840. print TMP "\0"x131072;
  841. close TMP;
  842. }
  843. if (not defined $trailer and not -e "Trailer") {
  844. $trailer = tmpnam();
  845. open TMP, ">$trailer" or die "Cannot open file $trailer: $!";
  846. push @cleanup, $trailer;
  847. for my $i (@sercomm_flash_trailer) {
  848. print TMP pack "S", $i;
  849. }
  850. close TMP;
  851. }
  852. # If the microcode was not specified, then don't complain that it's missing.
  853. if (not defined $microcode and not -e "NPE-B") {
  854. map { ($_->{'name'} eq 'Microcode') && ($_->{'file'} = undef); } @partitions;
  855. }
  856. }
  857. # Go through the partition options, and set the names and files in @partitions
  858. if (defined $redboot) { map { ($_->{'name'} eq 'RedBoot') && ($_->{'file'} = $redboot); } @partitions; }
  859. if (defined $sysconf) { map { ($_->{'name'} eq 'SysConf') && ($_->{'file'} = $sysconf); } @partitions; }
  860. if (defined $loader) { map { ($_->{'name'} eq 'Loader') && ($_->{'file'} = $loader); } @partitions; }
  861. if (defined $kernel) { map { ($_->{'name'} eq 'Kernel') && ($_->{'file'} = $kernel); } @partitions; }
  862. if (defined $fisdir) { map { ($_->{'name'} eq 'FIS directory') && ($_->{'file'} = $fisdir); } @partitions; }
  863. if (defined $microcode) { map { ($_->{'name'} eq 'Microcode') && ($_->{'file'} = $microcode); } @partitions; }
  864. if (defined $trailer) { map { ($_->{'name'} eq 'Trailer') && ($_->{'file'} = $trailer); } @partitions; }
  865. if (defined $little) {
  866. map {
  867. if (($_->{'name'} eq 'Loader') or
  868. ($_->{'name'} eq 'Kernel') or
  869. ($_->{'name'} eq 'Ramdisk')) {
  870. $_->{'byteswap'} = 1;
  871. }
  872. } @partitions;
  873. }
  874. if (defined $ethaddr) {
  875. map {
  876. if ($_->{'name'} eq 'EthAddr') {
  877. $ethaddr =~ s/://g;
  878. if (($ethaddr !~ m/^[0-9A-Fa-f]+$/) or (length($ethaddr) != 12)) {
  879. die "Invalid ethernet address specification: '".$ethaddr."'\n";
  880. }
  881. $_->{'data'} = pack("H12", $ethaddr);
  882. }
  883. } @partitions;
  884. }
  885. if (defined $ramdisk) {
  886. # A single filename is used for the ramdisk filename
  887. if ($ramdisk !~ m/[:,]/) {
  888. map { ($_->{'name'} eq 'Ramdisk') && ($_->{'file'} = $ramdisk); } @partitions;
  889. }
  890. # otherwise, it's a list of name:file mappings
  891. else {
  892. my @mappings = split(',', $ramdisk);
  893. # Find the index of the Ramdisk entry
  894. my $index;
  895. my $count = 0;
  896. map {
  897. if (not defined $index) {
  898. if ($_->{'name'} eq "Ramdisk") {
  899. $index = $count;
  900. }
  901. $count++;
  902. }
  903. } @partitions;
  904. defined $index or die "Cannot find the Ramdisk partition\n";
  905. # Replace the Ramdisk entry with the new mappings
  906. splice(@partitions, $index, 1, map {
  907. # Preserve the information from the ramdisk entry
  908. my %entry = %{$partitions[$index]};
  909. # Parse the mapping
  910. ($_ =~ m/^([^:]+):([^:]+)(:([^:]+))?$/) or die "Invalid syntax in --ramdisk\n";
  911. $entry{'name'} = $1; $entry{'file'} = $2; my $size = $4;
  912. # If the mapping is not for the ramdisk, then undefine its attributes
  913. if ($entry{'name'} ne 'Ramdisk') {
  914. $entry{'offset'} = undef;
  915. $entry{'size'} = undef;
  916. $entry{'variable'} = 1;
  917. $entry{'header'} = 0;
  918. $entry{'pseudo'} = 0;
  919. $entry{'data'} = undef;
  920. $entry{'byteswap'} = 0;
  921. }
  922. # Support specification of the number of blocks for empty jffs2
  923. if ($entry{'file'} =~ m/^[0-9]+$/) {
  924. $size = $entry{'file'};
  925. $entry{'file'} = undef;
  926. }
  927. # If the user has specified a size, then respect their wishes
  928. if (defined $size) {
  929. $entry{'size'} = $size * $block_size;
  930. # Create an empty partition of the requested size.
  931. $entry{'data'} = padBytes("", $entry{'size'} - $entry{'header'});
  932. }
  933. \%entry;
  934. } @mappings);
  935. }
  936. }
  937. # Read in the firmware image
  938. if ($input) {
  939. if ($debug) {
  940. print "Initial partition map:\n";
  941. printPartitions(@partitions);
  942. }
  943. my $result = readInFirmware($input, \@partitions);
  944. if ($debug) {
  945. print "After reading firmware:\n";
  946. printPartitions(@partitions);
  947. }
  948. }
  949. # Unpack the firmware if requested
  950. if ($unpack) {
  951. die "Input filename must be specified\n" unless defined $input;
  952. # map {
  953. # ($_->{'name'} eq 'FIS directory') and @partitions = checkPartitionTable($_->{'data'});
  954. # } @partitions;
  955. writeOutFirmwareParts(@partitions);
  956. }
  957. # Pack the firmware if requested
  958. if ($pack) {
  959. if (!defined $loader) {
  960. removeOptionalLoader(\@partitions);
  961. }
  962. if ($debug) {
  963. print "Initial partition map:\n";
  964. printPartitions(@partitions);
  965. }
  966. my $result = readInFirmwareParts(@partitions);
  967. if ($debug) {
  968. print "after readInFirmwareParts():\n";
  969. printPartitions(@partitions);
  970. # map {
  971. # ($_->{'name'} eq 'RedBoot') && (printRedbootTrailer($_->{'data'}));
  972. # } @partitions;
  973. }
  974. layoutPartitions(@partitions);
  975. if ($debug) {
  976. print "after layoutPartitions():\n";
  977. printPartitions(@partitions);
  978. }
  979. buildPartitionTable(@partitions);
  980. if ($debug) {
  981. print "after buildPartitionTable():\n";
  982. printPartitions(@partitions);
  983. # my($lastblock);
  984. # map {
  985. # if ($_->{'name'} eq 'FIS directory') {
  986. # $lastblock = $_->{'data'};
  987. # }
  988. # } @partitions;
  989. # print "checkPartitionTable():\n";
  990. # checkPartitionTable($lastblock);
  991. }
  992. writeOutFirmware($output, @partitions);
  993. }
  994. exit 0;