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