slugimage.pl 40 KB

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