slugimage.pl 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217
  1. #!/usr/bin/env 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 File::Temp qw(tempfile);
  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. open FILE,$filename or die "Can't find firmware image \"$filename\": $!\n";
  319. read FILE,$firmware_buf,$flash_len or die "Can't read $flash_len bytes from \"$filename\": $!\n";
  320. close FILE or die "Can't close \"$filename\": $!\n";
  321. $debug and printf("Read 0x%08X bytes from \"%s\"\n", length($firmware_buf), $filename);
  322. spliceFirmwarePartitions($firmware_buf, $partitions_ref);
  323. # Read the parts of the firmware file into the partitions table.
  324. map {
  325. if (defined $_->{'offset'} and defined $_->{'size'}) {
  326. if (defined $_->{'data'}) {
  327. $debug and printf("Not overwriting data in <%s>\n", $_->{'name'});
  328. }
  329. else {
  330. # Slurp up the data, based on whether a header and/or data is present or not
  331. if ($_->{'header'}) {
  332. # Read the length, and grab the data based on the length.
  333. my($data_len) = unpack("N", substr($firmware_buf, $_->{'offset'}));
  334. # A length of 0xFFFFFFFF means that the area is not initialised
  335. if ($data_len != 0xFFFFFFFF) {
  336. $debug and printf("Found header size of 0x%08X bytes for <%s>\n", $data_len, $_->{'name'});
  337. $_->{'data'} = substr($firmware_buf, $_->{'offset'} + $_->{'header'}, $data_len);
  338. }
  339. }
  340. elsif ($_->{'pseudo'} and not defined $_->{'file'} and
  341. (substr($firmware_buf, $_->{'offset'}, $_->{'size'}) eq
  342. (pack("C", 0xff) x $_->{'size'}))) {
  343. $debug and printf("Skipping empty pseudo partition <%s>\n", $_->{'name'});
  344. }
  345. else {
  346. # Grab the whole partition, using the maximum size.
  347. $_->{'data'} = substr($firmware_buf, $_->{'offset'}, $_->{'size'});
  348. }
  349. # If skip regions are defined, remove them from the data.
  350. if (defined $_->{'skips'}) {
  351. my $removed = 0;
  352. foreach my $region (@{$_->{'skips'}}) {
  353. if (($region->{'offset'} > 0) or
  354. not ($_->{'header'} > 0)) {
  355. $debug and printf("Removing 0x%05X bytes from offset 0x%05X\n",
  356. $region->{'size'}, $region->{'offset'});
  357. $region->{'data'} = substr($_->{'data'}, $region->{'offset'} - $removed, $region->{'size'}, '');
  358. }
  359. $removed += $region->{'size'};
  360. }
  361. }
  362. $quiet or defined $_->{'data'} and printf("Read %s into <%s>\n",
  363. (length($_->{'data'}) >= $block_size ?
  364. sprintf("%d blocks", numBlocks(length($_->{'data'}))) :
  365. sprintf("0x%05X bytes", length($_->{'data'}))), $_->{'name'});
  366. }
  367. }
  368. } @$partitions_ref;
  369. }
  370. # Write the partition data stored in memory out into the files associated with each.
  371. sub writeOutFirmwareParts {
  372. my(@partitions) = @_;
  373. # Write out the parts of the firmware file.
  374. map {
  375. # We can only write if 'data' and 'file' are defined.
  376. if (defined $_->{'file'} and defined $_->{'data'} and length($_->{'data'})) {
  377. writeOut($_->{'data'}, $_->{'file'});
  378. $quiet or printf("Wrote 0x%08X bytes from <%s> into \"%s\"\n",
  379. length($_->{'data'}), $_->{'name'}, $_->{'file'});
  380. }
  381. else {
  382. $debug and printf("Skipping <%s> (%s)\n", $_->{'name'},
  383. (not defined $_->{'file'}) ?
  384. "no filename specified" :
  385. "no data to write");
  386. }
  387. } @partitions;
  388. return;
  389. }
  390. # Read in the partition data from the files associated with each and store in memory.
  391. sub readInFirmwareParts {
  392. my(@partitions) = (@_);
  393. undef $/; # we want to slurp
  394. map {
  395. my $file = $_->{'file'};
  396. if (defined $file) {
  397. open FILE,$file or die "Can't find firmware part \"$file\": $!\n";
  398. # Slurp in the data
  399. $_->{'data'} = <FILE>;
  400. # close the file
  401. close FILE or die "Can't close file \"$file\": $!\n";
  402. # Optionally byteswap the data
  403. if ($_->{'byteswap'}) {
  404. # Byte swap the data (which has to be padded to a multiple of 4 bytes).
  405. $_->{'data'} = pack("N*", unpack("V*", $_->{'data'}.pack("CCC", 0)));
  406. }
  407. # Keep track of the actual size.
  408. my $size;
  409. if ($_->{'header'}) {
  410. if ($_->{'pseudo'}) {
  411. $size = $_->{'header'} + length($_->{'data'});
  412. }
  413. else {
  414. $size = paddedSize($_->{'header'} + length($_->{'data'}));
  415. }
  416. }
  417. elsif (not $_->{'pseudo'}) {
  418. $size = paddedSize(length($_->{'data'}));
  419. }
  420. else {
  421. $size = length($_->{'data'});
  422. }
  423. # Check to make sure the file contents are not too large.
  424. if (defined $_->{'size'} and ($size > $_->{'size'})) {
  425. die sprintf("Ran out of flash space in <%s> - %s too large.\n", $_->{'name'},
  426. sprintf("0x%05X bytes", ($size - $_->{'size'})));
  427. }
  428. # If the partition does not have a fixed size, the calculate the size.
  429. if (not defined $_->{'size'}) {
  430. $_->{'size'} = $size;
  431. }
  432. # Keep the user appraised ...
  433. $quiet or printf("Read 0x%08X bytes from \"%s\" into <%s> (%s / %s)%s\n",
  434. length($_->{'data'}), $_->{'file'}, $_->{'name'},
  435. ($size >= $block_size ?
  436. sprintf("%d blocks", numBlocks($size)) :
  437. sprintf("0x%05X bytes", $size)),
  438. ($_->{'size'} >= $block_size ?
  439. sprintf("%d blocks", numBlocks($_->{'size'})) :
  440. sprintf("0x%05X bytes", $_->{'size'})),
  441. ($_->{'byteswap'} ? " (byte-swapped)" : ""));
  442. }
  443. } @partitions;
  444. return;
  445. }
  446. # layoutPartitions : this function must be ugly - it needs to verify RedBoot, SysConf, Kernel, Ramdisk, and
  447. # FIS directory partitions exist, are in the correct order, and do not have more data than can fit in
  448. # their lengths (fixed for all but Ramdisk, which has a minimum length of one block).
  449. # If Rootdisk and/or Userdisk exist, it must also verify that their block padded lengths are not
  450. # too great for the available space.
  451. # input : an array of hashes, some of which are populated with data
  452. # output: same reference with start and size (partition not data) also populated. this populated structure
  453. # can then be passed to buildPartitionTable() to generate the actual partition table data
  454. sub layoutPartitions {
  455. my(@partitions) = @_;
  456. # Find the kernel partition, and save a pointer to it for later use
  457. my $kernel;
  458. map { ($_->{'name'} eq "Kernel") && ($kernel = $_); } @partitions;
  459. $kernel or die "Couldn't find the kernel partition\n";
  460. # Find the last variable size partition, and save a pointer to it for later use
  461. my $lastdisk;
  462. my $directory_offset;
  463. my $curdisk = $partitions[0];
  464. map {
  465. if (not defined $lastdisk) {
  466. if ($_->{'name'} eq "FIS directory") {
  467. $lastdisk = $curdisk;
  468. $directory_offset = $_->{'offset'};
  469. }
  470. else {
  471. $curdisk = $_;
  472. }
  473. }
  474. } @partitions;
  475. $lastdisk or die "Couldn't find the last variable size partition\n";
  476. $debug and printf("Last variable size partition is <%s>\n", $lastdisk->{'name'});
  477. #
  478. # here we go through the $partitions array ref and fill in all the values
  479. #
  480. # This points to where the next partition should be placed.
  481. my $pointer = $flash_start;
  482. map {
  483. $debug and printf("Pointer is 0x%08X\n", $pointer);
  484. # Determine the start and offset of the current partition.
  485. if (defined $_->{'offset'}) {
  486. $_->{'start'} = $flash_start + $_->{'offset'};
  487. # Check for running past the defined start of the partition.
  488. if (($pointer > $_->{'start'}) and not $_->{'pseudo'}) {
  489. die sprintf("Ran out of flash space before <%s> - %s too large.\n", $_->{'name'},
  490. sprintf("0x%05X bytes", ($pointer - $_->{'start'})));
  491. }
  492. }
  493. # If offset is not defined, then calculate it.
  494. else {
  495. $_->{'start'} = $pointer;
  496. $_->{'offset'} = $_->{'start'} - $flash_start;
  497. }
  498. my $size = defined $_->{'data'} ? length($_->{'data'}) : 0;
  499. # Add skip regions for the partitions with headers.
  500. if ($_->{'header'} > 0) {
  501. # Define the skip region for the initial Sercomm header.
  502. push(@{$_->{'skips'}},
  503. { 'offset' => 0, 'size' => $_->{'header'}, 'data' => undef });
  504. # Allow for the Sercomm header to be prepended to the data.
  505. $size += $_->{'header'};
  506. # Determine if the partition overlaps the ramdisk boundary.
  507. if (($_->{'offset'} < $ramdisk_offset) and
  508. (($_->{'offset'} + $size) > $ramdisk_offset)) {
  509. # Define the skip region for the inline Sercomm header.
  510. push(@{$_->{'skips'}},
  511. { 'offset' => ($ramdisk_offset - $_->{'offset'}), 'size' => 16,
  512. 'data' => pack("N4", $block_size) });
  513. # Allow for the Sercomm header to be inserted in the data.
  514. $size += 16;
  515. }
  516. }
  517. # Partitions without headers cannot have skip regions.
  518. elsif (($_->{'offset'} <= $ramdisk_offset) and
  519. (($_->{'offset'} + $size) > $ramdisk_offset)) {
  520. # Pad the kernel until it extends past the ramdisk offset.
  521. push(@{$kernel->{'skips'}},
  522. { 'offset' => ($ramdisk_offset - $kernel->{'offset'}), 'size' => 16,
  523. 'data' => pack("N4", $block_size) });
  524. $kernel->{'size'} = $ramdisk_offset - $kernel->{'offset'} + $block_size;
  525. $kernel->{'data'} = padBytes($kernel->{'data'},
  526. $kernel->{'size'} - $kernel->{'header'} - 16);
  527. $_->{'offset'} = $ramdisk_offset + $block_size;
  528. $_->{'start'} = $flash_start + $_->{'offset'};
  529. $pointer = $_->{'start'};
  530. $debug and printf("Extending kernel partition past ramdisk offset.\n");
  531. }
  532. # If this is the last variable size partition, then fill the rest of the space.
  533. if ($_->{'name'} eq $lastdisk->{'name'}) {
  534. $_->{'size'} = paddedSize($directory_offset + $flash_start - $pointer);
  535. $debug and printf("Padding last variable partition <%s> to 0x%08X bytes\n", $_->{'name'}, $_->{'size'});
  536. }
  537. die sprintf("Partition size not defined in <%s>.\n", $_->{'name'})
  538. unless defined $_->{'size'};
  539. # Extend to another block if required.
  540. if ($size > $_->{'size'}) {
  541. if ($_->{'name'} eq $lastdisk->{'name'}) {
  542. die sprintf("Ran out of flash space in <%s> - %s too large.\n", $_->{'name'},
  543. sprintf("0x%05X bytes", ($size - $_->{'size'})));
  544. }
  545. $_->{'size'} = $size;
  546. printf("Extending partition <%s> to 0x%08X bytes\n", $_->{'name'}, $_->{'size'});
  547. }
  548. # Keep the user appraised ...
  549. $debug and printf("Allocated <%s> from 0x%08X to 0x%08X (%s / %s)\n",
  550. $_->{'name'}, $_->{'start'}, $_->{'start'} + $_->{'size'},
  551. ($size >= $block_size ?
  552. sprintf("%d blocks", numBlocks($size)) :
  553. sprintf("0x%05X bytes", $size)),
  554. ($_->{'size'} >= $block_size ?
  555. sprintf("%d blocks", numBlocks($_->{'size'})) :
  556. sprintf("0x%05X bytes", $_->{'size'})));
  557. # Check to make sure we have not run out of room.
  558. if (($_->{'start'} + $_->{'size'}) > ($flash_start + $flash_len)) {
  559. die "Ran out of flash space in <", $_->{'name'}, ">\n";
  560. }
  561. $debug and printf("Moving pointer from 0x%08X to 0x%08X (0x%08X + 0x%08X)\n",
  562. $pointer, paddedSize($_->{'start'} + $_->{'size'}),
  563. $_->{'start'}, $_->{'size'});
  564. # Move the pointer up, in preparation for the next partition.
  565. $pointer = paddedSize($_->{'start'} + $_->{'size'});
  566. } @partitions;
  567. return;
  568. }
  569. sub buildPartitionTable {
  570. my(@partitions) = @_;
  571. my($flash_start) = 0x50000000;
  572. my($partition_data) = '';
  573. map {
  574. # Collate the partition data for all known partitions.
  575. if (not $_->{'pseudo'} and defined $_->{'offset'} and defined $_->{'size'}) {
  576. # Pack and append the binary table entry for this partition.
  577. $partition_data .= createPartitionEntry($_->{'name'}, $_->{'offset'} + $flash_start,
  578. $_->{'size'}, $_->{'skips'});
  579. # If this is the FIS directory, then write the partition table data into it.
  580. if ($_->{'name'} eq "FIS directory") {
  581. # Explicitly terminate the partition data.
  582. $partition_data .= pack("C",0xff) x 0x100;
  583. $_->{'data'} = padBytes($partition_data, $_->{'size'});
  584. }
  585. my $size = length($_->{'data'});
  586. # Keep the user appraised ...
  587. $debug and printf("Table entry <%s> from 0x%08X to 0x%08X (%s / %s)%s\n",
  588. $_->{'name'}, $_->{'start'}, $_->{'start'} + $_->{'size'},
  589. ($size >= $block_size ?
  590. sprintf("%d blocks", numBlocks($size)) :
  591. sprintf("0x%05X bytes", $size)),
  592. ($_->{'size'} >= $block_size ?
  593. sprintf("%d blocks", numBlocks($_->{'size'})) :
  594. sprintf("0x%05X bytes", $_->{'size'})),
  595. (defined $_->{'skips'} ?
  596. sprintf("\nTable entry <%s> skip %s", $_->{'name'},
  597. join(", ",
  598. map { sprintf("0x%08X to 0x%08X", $_->{'offset'},
  599. $_->{'offset'} + $_->{'size'} - 1) }
  600. @{$_->{'skips'}})) :
  601. "")
  602. );
  603. }
  604. else {
  605. $debug and print "No table entry required for <", $_->{'name'}, ">\n";
  606. }
  607. } @partitions;
  608. return;
  609. }
  610. sub writeOutFirmware {
  611. my($filename, @partitions) = @_;
  612. # Clear the image to start.
  613. my $image_buf = "";
  614. map {
  615. # We can only write a partition if it has an offset, a size, and some data to write.
  616. if (defined $_->{'offset'} and defined $_->{'size'} and defined $_->{'data'}) {
  617. # Keep track of the end of the image.
  618. my $end_point = length($image_buf);
  619. # If the next partition is well past the end of the current image, then pad it.
  620. if ($_->{'offset'} > $end_point) {
  621. $image_buf .= padBytes("", $_->{'offset'} - $end_point);
  622. $quiet or printf("Padded %s before <%s> in \"%s\"\n",
  623. ((length($image_buf) - $end_point) >= $block_size ?
  624. sprintf("%d blocks", numBlocks(length($image_buf) - $end_point)) :
  625. sprintf("0x%05X bytes", length($image_buf) - $end_point)),
  626. $_->{'name'}, $filename);
  627. }
  628. # If the next partition is before the end of the current image, then rewind.
  629. elsif ($_->{'offset'} < $end_point) {
  630. $debug and printf("Rewound %s before <%s> in \"%s\"\n",
  631. (($end_point - $_->{'offset'}) >= $block_size ?
  632. sprintf("%d blocks", numBlocks($end_point - $_->{'offset'})) :
  633. sprintf("0x%05X bytes", $end_point - $_->{'offset'})),
  634. $_->{'name'}, $filename);
  635. # if (($end_point - $_->{'offset'}) >= $block_size) {
  636. # die "Allocation error: rewound a full block or more ...\n";
  637. # }
  638. }
  639. # If skip regions are defined, add them to the data.
  640. if (defined $_->{'skips'}) {
  641. my $added = 0;
  642. foreach my $region (@{$_->{'skips'}}) {
  643. if (($region->{'offset'} > 0) or
  644. not ($_->{'header'} > 0)) {
  645. $debug and printf("Inserted 0x%05X bytes (at offset 0x%05X) into <%s>\n",
  646. $region->{'size'}, $region->{'offset'}, $_->{'name'});
  647. substr($_->{'data'},
  648. $region->{'offset'} + $added - $_->{'header'},
  649. 0, $region->{'data'});
  650. $added += $region->{'size'};
  651. }
  652. }
  653. }
  654. # Splice the data into the image at the appropriate place, padding as required.
  655. substr($image_buf, $_->{'offset'}, $_->{'size'},
  656. $_->{'header'} ?
  657. padBytes(pack("N4",length($_->{'data'})).$_->{'data'}, $_->{'size'}) :
  658. padBytes($_->{'data'}, $_->{'size'}));
  659. # Keep the user appraised ...
  660. $quiet or printf("Wrote %s (0x%08X to 0x%08X) from <%s> into \"%s\"\n",
  661. ($_->{'size'} >= $block_size ?
  662. sprintf("%2d blocks", numBlocks($_->{'size'})) :
  663. sprintf("0x%05X bytes", $_->{'size'})),
  664. $_->{'offset'}, $_->{'offset'}+$_->{'size'}, $_->{'name'}, $filename);
  665. }
  666. # If we are not able to write a partition, then give debug information about why.
  667. else {
  668. $debug and printf("Skipping <%s> (%s)\n", $_->{'name'},
  669. (not defined $_->{'offset'}) ? "no offset defined" :
  670. ((not defined $_->{'size'}) ? "no size defined" :
  671. "no data available"));
  672. }
  673. } @partitions;
  674. # Write the image to the specified file.
  675. writeOut($image_buf, $filename);
  676. return;
  677. }
  678. # checkPartitionTable: sanity check partition table - for testing but might evolve into setting @partitions
  679. # so that we can write out jffs2 partitions from a read image
  680. # currently not nearly paranoid enough
  681. sub checkPartitionTable {
  682. my($data) = @_;
  683. my($pointer) = 0;
  684. my($entry);
  685. my($name, $flash_base, $size, $done, $dummy_long, $padding);
  686. do {
  687. $entry = substr($data, $pointer, 0x100);
  688. ($name,$flash_base,$dummy_long,$size,$dummy_long,$dummy_long,$padding,$dummy_long,$dummy_long) = unpack("a16N5x212N2",$entry);
  689. $name =~ s/\0//g;
  690. $debug and printf("pointer: %d\tname: %s%sflash_base: 0x%08X\tsize: 0x%08X\n",
  691. $pointer, $name, (" " x (16 - length($name))), $flash_base, $size);
  692. $pointer += 0x100;
  693. $debug and printf("terminator: 0x%08X\n", unpack("C", substr($data, $pointer, 1)));
  694. if (unpack("C", substr($data, $pointer, 1)) eq 0xff) {
  695. $done = 1;
  696. }
  697. } until $done;
  698. }
  699. sub printPartitions {
  700. my(@partitions) = @_;
  701. my($offset, $size, $skips);
  702. map {
  703. # defined $_->{'size'} ? $size = $_->{'size'} : $size = undef;
  704. if (defined $_->{'size'}) {
  705. $size = $_->{'size'};
  706. }
  707. else {
  708. $size = undef;
  709. }
  710. if (defined $_->{'offset'}) {
  711. $offset = $_->{'offset'};
  712. }
  713. else {
  714. $offset = undef;
  715. }
  716. if (defined $_->{'skips'}) {
  717. $skips = $_->{'skips'};
  718. }
  719. else {
  720. $skips = undef;
  721. }
  722. printf("%s%s", $_->{'name'}, (" " x (16 - length($_->{'name'}))));
  723. if (defined $offset) { printf("0x%08X\t", $offset); } else { printf("(undefined)\t"); };
  724. if (defined $size) { printf("0x%08X", $size); } else { printf("(undefined)"); };
  725. if (defined $skips) {
  726. printf("\t[%s]",
  727. join(", ",
  728. map { sprintf("0x%05X/0x%05X", $_->{'offset'}, $_->{'size'}); }
  729. @$skips));
  730. }
  731. printf("\n");
  732. } @partitions;
  733. }
  734. sub defaultPartitions {
  735. return ({'name'=>'RedBoot', 'file'=>'RedBoot',
  736. 'offset'=>0x00000000, 'size'=>0x00040000,
  737. 'variable'=>0, 'header'=>0, 'pseudo'=>0, 'data'=>undef, 'byteswap'=>0},
  738. {'name'=>'EthAddr', 'file'=>undef,
  739. 'offset'=>0x0003ffb0, 'size'=>0x00000006,
  740. 'variable'=>0, 'header'=>0, 'pseudo'=>1, 'data'=>undef, 'byteswap'=>0},
  741. {'name'=>'SysConf', 'file'=>'SysConf',
  742. 'offset'=>0x00040000, 'size'=>0x00020000,
  743. 'variable'=>0, 'header'=>0, 'pseudo'=>0, 'data'=>undef, 'byteswap'=>0},
  744. {'name'=>'Loader', 'file'=>'apex.bin',
  745. 'offset'=>undef, 'size'=>undef,
  746. 'variable'=>1, 'header'=>16, 'pseudo'=>0, 'data'=>undef, 'byteswap'=>0},
  747. {'name'=>'Kernel', 'file'=>'vmlinuz',
  748. 'offset'=>undef, 'size'=>undef,
  749. 'variable'=>1, 'header'=>16, 'pseudo'=>0, 'data'=>undef, 'byteswap'=>0},
  750. {'name'=>'Ramdisk', 'file'=>'ramdisk.gz',
  751. 'offset'=>undef, 'size'=>undef,
  752. 'variable'=>1, 'header'=>16, 'pseudo'=>0, 'data'=>undef, 'byteswap'=>0},
  753. {'name'=>'FIS directory', 'file'=>undef,
  754. 'offset'=>0x007e0000, 'size'=>0x00020000,
  755. 'variable'=>0, 'header'=>0, 'pseudo'=>0, 'data'=>undef, 'byteswap'=>0},
  756. {'name'=>'Loader config', 'file'=>undef,
  757. 'offset'=>0x007f8000, 'size'=>0x00004000,
  758. 'variable'=>0, 'header'=>0, 'pseudo'=>1, 'data'=>undef, 'byteswap'=>0},
  759. {'name'=>'Microcode', 'file'=>'NPE-B',
  760. 'offset'=>0x007fc000, 'size'=>0x00003fe0,
  761. 'variable'=>0, 'header'=>16, 'pseudo'=>1, 'data'=>undef, 'byteswap'=>0},
  762. {'name'=>'Trailer', 'file'=>'Trailer',
  763. 'offset'=>0x007ffff0, 'size'=>0x00000010,
  764. 'variable'=>0, 'header'=>0, 'pseudo'=>1, 'data'=>undef, 'byteswap'=>0});
  765. }
  766. # Main routine starts here ...
  767. my($unpack, $pack, $little, $fatflash, $input, $output, $redboot);
  768. my($kernel, $sysconf, $ramdisk, $fisdir);
  769. my($microcode, $trailer, $ethaddr, $loader);
  770. END {
  771. # Remove temporary files
  772. for my $file (@cleanup) {
  773. unlink $file;
  774. }
  775. }
  776. if (!GetOptions("d|debug" => \$debug,
  777. "q|quiet" => \$quiet,
  778. "u|unpack" => \$unpack,
  779. "p|pack" => \$pack,
  780. "l|little" => \$little,
  781. "F|fatflash" => \$fatflash,
  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 " [-F|--fatflash] Generate an image for 16MB flash\n";
  802. print " [-i|--input] <file> Input firmware image filename\n";
  803. print " [-o|--output] <file> Output firmware image filename\n";
  804. print " [-b|--redboot] <file> Input/Output RedBoot filename\n";
  805. print " [-s|--sysconf] <file> Input/Output SysConf filename\n";
  806. print " [-L|--loader] <file> Second stage boot loader filename\n";
  807. print " [-k|--kernel] <file> Input/Output Kernel filename\n";
  808. print " [-r|--ramdisk] <file> Input/Output Ramdisk filename(s)\n";
  809. print " [-f|--fisdir] <file> Input/Output FIS directory filename\n";
  810. print " [-m|--microcode] <file> Input/Output Microcode filename\n";
  811. print " [-t|--trailer] <file> Input/Output Trailer filename\n";
  812. print " [-e|--ethaddr] <AABBCCDDEEFF> Set the Ethernet address\n";
  813. # %%% TODO %%% Document --ramdisk syntax
  814. exit 1;
  815. }
  816. my(@partitions) = defaultPartitions();
  817. if ($pack) {
  818. die "Output filename must be specified\n" unless defined $output;
  819. # If we're creating an image and no RedBoot, SysConf partition is
  820. # explicitly specified, simply write an empty one as the upgrade tools
  821. # don't touch RedBoot and SysConf anyway. If no Trailer is specified,
  822. # put in one.
  823. if (not defined $redboot and not -e "RedBoot") {
  824. $redboot = tempfile();
  825. open TMP, ">$redboot" or die "Cannot open file $redboot: $!";
  826. push @cleanup, $redboot;
  827. # The RedBoot partition is 256 * 1024 = 262144; the trailer we add
  828. # is 70 bytes.
  829. print TMP "\0"x(262144-70);
  830. # Upgrade tools check for an appropriate Sercomm trailer.
  831. for my $i (@sercomm_redboot_trailer) {
  832. print TMP pack "S", $i;
  833. }
  834. close TMP;
  835. }
  836. if (not defined $sysconf and not -e "SysConf") {
  837. $sysconf = tempfile();
  838. open TMP, ">$sysconf" or die "Cannot open file $sysconf: $!";
  839. push @cleanup, $sysconf;
  840. # The SysConf partition is 128 * 1024 = 131072
  841. print TMP "\0"x131072;
  842. close TMP;
  843. }
  844. if (not defined $trailer and not -e "Trailer") {
  845. $trailer = tempfile();
  846. open TMP, ">$trailer" or die "Cannot open file $trailer: $!";
  847. push @cleanup, $trailer;
  848. for my $i (@sercomm_flash_trailer) {
  849. print TMP pack "S", $i;
  850. }
  851. close TMP;
  852. }
  853. # If the microcode was not specified, then don't complain that it's missing.
  854. if (not defined $microcode and not -e "NPE-B") {
  855. map { ($_->{'name'} eq 'Microcode') && ($_->{'file'} = undef); } @partitions;
  856. }
  857. }
  858. # Go through the partition options, and set the names and files in @partitions
  859. if (defined $redboot) { map { ($_->{'name'} eq 'RedBoot') && ($_->{'file'} = $redboot); } @partitions; }
  860. if (defined $sysconf) { map { ($_->{'name'} eq 'SysConf') && ($_->{'file'} = $sysconf); } @partitions; }
  861. if (defined $loader) { map { ($_->{'name'} eq 'Loader') && ($_->{'file'} = $loader); } @partitions; }
  862. if (defined $kernel) { map { ($_->{'name'} eq 'Kernel') && ($_->{'file'} = $kernel); } @partitions; }
  863. if (defined $fisdir) { map { ($_->{'name'} eq 'FIS directory') && ($_->{'file'} = $fisdir); } @partitions; }
  864. if (defined $microcode) { map { ($_->{'name'} eq 'Microcode') && ($_->{'file'} = $microcode); } @partitions; }
  865. if (defined $trailer) { map { ($_->{'name'} eq 'Trailer') && ($_->{'file'} = $trailer); } @partitions; }
  866. if (defined $little) {
  867. map {
  868. if (($_->{'name'} eq 'Loader') or
  869. ($_->{'name'} eq 'Kernel') or
  870. ($_->{'name'} eq 'Ramdisk')) {
  871. $_->{'byteswap'} = 1;
  872. }
  873. } @partitions;
  874. }
  875. if (defined $fatflash) {
  876. $flash_len = 0x01000000;
  877. map {
  878. if (($_->{'name'} eq 'FIS directory') or
  879. ($_->{'name'} eq 'Loader config') or
  880. ($_->{'name'} eq 'Microcode') or
  881. ($_->{'name'} eq 'Trailer')) {
  882. $_->{'offset'} += 0x00800000;
  883. }
  884. } @partitions;
  885. }
  886. if (defined $ethaddr) {
  887. map {
  888. if ($_->{'name'} eq 'EthAddr') {
  889. $ethaddr =~ s/://g;
  890. if (($ethaddr !~ m/^[0-9A-Fa-f]+$/) or (length($ethaddr) != 12)) {
  891. die "Invalid ethernet address specification: '".$ethaddr."'\n";
  892. }
  893. $_->{'data'} = pack("H12", $ethaddr);
  894. }
  895. } @partitions;
  896. }
  897. if (defined $ramdisk) {
  898. # A single filename is used for the ramdisk filename
  899. if ($ramdisk !~ m/[:,]/) {
  900. map { ($_->{'name'} eq 'Ramdisk') && ($_->{'file'} = $ramdisk); } @partitions;
  901. }
  902. # otherwise, it's a list of name:file mappings
  903. else {
  904. my @mappings = split(',', $ramdisk);
  905. # Find the index of the Ramdisk entry
  906. my $index;
  907. my $count = 0;
  908. map {
  909. if (not defined $index) {
  910. if ($_->{'name'} eq "Ramdisk") {
  911. $index = $count;
  912. }
  913. $count++;
  914. }
  915. } @partitions;
  916. defined $index or die "Cannot find the Ramdisk partition\n";
  917. # Replace the Ramdisk entry with the new mappings
  918. splice(@partitions, $index, 1, map {
  919. # Preserve the information from the ramdisk entry
  920. my %entry = %{$partitions[$index]};
  921. # Parse the mapping
  922. ($_ =~ m/^([^:]+):([^:]+)(:([^:]+))?$/) or die "Invalid syntax in --ramdisk\n";
  923. $entry{'name'} = $1; $entry{'file'} = $2; my $size = $4;
  924. # If the mapping is not for the ramdisk, then undefine its attributes
  925. if ($entry{'name'} ne 'Ramdisk') {
  926. $entry{'offset'} = undef;
  927. $entry{'size'} = undef;
  928. $entry{'variable'} = 1;
  929. $entry{'header'} = 0;
  930. $entry{'pseudo'} = 0;
  931. $entry{'data'} = undef;
  932. $entry{'byteswap'} = 0;
  933. }
  934. # Support specification of the number of blocks for empty jffs2
  935. if ($entry{'file'} =~ m/^[0-9]+$/) {
  936. $size = $entry{'file'};
  937. $entry{'file'} = undef;
  938. }
  939. # If the user has specified a size, then respect their wishes
  940. if (defined $size) {
  941. $entry{'size'} = $size * $block_size;
  942. # Create an empty partition of the requested size.
  943. $entry{'data'} = padBytes("", $entry{'size'} - $entry{'header'});
  944. }
  945. \%entry;
  946. } @mappings);
  947. }
  948. }
  949. # Read in the firmware image
  950. if ($input) {
  951. if ($debug) {
  952. print "Initial partition map:\n";
  953. printPartitions(@partitions);
  954. }
  955. my $result = readInFirmware($input, \@partitions);
  956. if ($debug) {
  957. print "After reading firmware:\n";
  958. printPartitions(@partitions);
  959. }
  960. }
  961. # Unpack the firmware if requested
  962. if ($unpack) {
  963. die "Input filename must be specified\n" unless defined $input;
  964. # map {
  965. # ($_->{'name'} eq 'FIS directory') and @partitions = checkPartitionTable($_->{'data'});
  966. # } @partitions;
  967. writeOutFirmwareParts(@partitions);
  968. }
  969. # Pack the firmware if requested
  970. if ($pack) {
  971. if (!defined $loader) {
  972. removeOptionalLoader(\@partitions);
  973. }
  974. if ($debug) {
  975. print "Initial partition map:\n";
  976. printPartitions(@partitions);
  977. }
  978. my $result = readInFirmwareParts(@partitions);
  979. if ($debug) {
  980. print "after readInFirmwareParts():\n";
  981. printPartitions(@partitions);
  982. # map {
  983. # ($_->{'name'} eq 'RedBoot') && (printRedbootTrailer($_->{'data'}));
  984. # } @partitions;
  985. }
  986. layoutPartitions(@partitions);
  987. if ($debug) {
  988. print "after layoutPartitions():\n";
  989. printPartitions(@partitions);
  990. }
  991. buildPartitionTable(@partitions);
  992. if ($debug) {
  993. print "after buildPartitionTable():\n";
  994. printPartitions(@partitions);
  995. # my($lastblock);
  996. # map {
  997. # if ($_->{'name'} eq 'FIS directory') {
  998. # $lastblock = $_->{'data'};
  999. # }
  1000. # } @partitions;
  1001. # print "checkPartitionTable():\n";
  1002. # checkPartitionTable($lastblock);
  1003. }
  1004. writeOutFirmware($output, @partitions);
  1005. }
  1006. exit 0;