#!/usr/bin/perl # # SlugImage : Manipulate NSLU2 firmware images # Dwayne Fontenot (jacques) # Rod Whitby (rwhitby) # www.nslu2-linux.org # # Copyright (c) 2004, Dwayne Fontenot & Rod Whitby # All rights reserved. # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions # are met: # # Redistributions of source code must retain the above copyright # notice, this list of conditions and the following disclaimer. # Redistributions in binary form must reproduce the above copyright # notice, this list of conditions and the following disclaimer in the # documentation and/or other materials provided with the distribution. # Neither the name of the NSLU2-Linux Development Team nor the names # of its contributors may be used to endorse or promote products # derived from this software without specific prior written # permission. # # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE # COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE # POSSIBILITY OF SUCH DAMAGE. # use strict; use warnings; use Getopt::Long; my($debug) = 0; my($quiet) = 0; my($block_size) = 0x00020000; # Take $data, and pad it out to $total_len bytes, appending 0xff's. sub padBytes { my($data,$total_len) = @_; # 0xFF is used to pad, as it's the erase value of the flash. my($pad_char) = pack("C",0xff); my($pad_len) = $total_len - length($data); # A request for negative padding is indicative of a logic error ... if (length($data) > $total_len) { die sprintf("padBytes error: data (%d) is longer than total_len (%d)", length($data), $total_len); } return $data . ($pad_char x $pad_len); } # Return the next multiple of block_size larger than or equal to $data_len. sub paddedSize { my($data_len) = @_; use integer; return (($data_len - 1) / $block_size) * $block_size + $block_size; } # Return the number of block_size blocks required to hold $data_len. sub numBlocks { my($data_len) = @_; use integer; return (($data_len - 1) / $block_size) + 1; } # Pack the name, address, and size of a partition entry into binary form. sub createPartitionEntry { my($name, $flash_base, $size) = @_; my($zero_long) = 0x0000; # Pack the partition entry according to the format that RedBoot (and the MTD partition parsing code) requires. return pack("a16N5x212N2",$name,$flash_base,$zero_long,$size,$zero_long,$zero_long,$zero_long,$zero_long); } # Parse partition entry and return anon array ref [$name, $offset, $size] or return 0 on partition terminator. sub parsePartitionEntry { my($partition_entry) = @_; my($flash_start) = 0x50000000; my($entry_len) = 0x100; length($partition_entry) eq $entry_len or die "parsePartitionEntry: partition entry length is not $entry_len!\n"; # Unpack the partition table entry, saving those values in which we are interested. my($name, $flash_base, $size, $zero_long, $padding); ($name, $flash_base, $zero_long, $size, $zero_long, $zero_long, $padding, $zero_long, $zero_long) = unpack("a16N5x212N2",$partition_entry); # A partition entry starting with 0xFF terminates the table. if (unpack("C", $name) eq 0xff) { $debug and print "Found terminator for \n"; return 0; } # Remove trailing nulls from the partition name. $name =~ s/\000+//; return [$name, $flash_base - $flash_start, $size]; } # Return partition table from data is one exists, otherwise return 0. sub findPartitionTable { my($data_buf) = @_; unpack("a7", $data_buf) eq 'RedBoot' or return 0; return substr($data_buf, 0, 0x1000) } # Parse partition table and return array of anonymous array references ([$name, $offset, $size], ...). sub parsePartitionTable { my($partition_table) = @_; my(@partitions, $fields_ref); my($entry_len) = 0x100; my($partition_count) = 0; # Loop through the fixed size partition table entries, and store the entries in @partitions. while ($fields_ref = parsePartitionEntry(substr($partition_table, $partition_count * $entry_len, $entry_len))) { $debug and printf("Found <%s> at 0x%08X (%s)\n", $fields_ref->[0], $fields_ref->[1], ($fields_ref->[2] >= $block_size ? sprintf("%d blocks", numBlocks($fields_ref->[2])) : sprintf("0x%05X bytes", $fields_ref->[2]))); $partitions[$partition_count++] = $fields_ref; } return(@partitions); } # Create an empty jffs2 block. sub jffs2Block { return padBytes(pack("N3", 0x19852003, 0x0000000c, 0xf060dc98), 0x00020000); } # Write out $data to $filename, sub writeOut { my($data, $filename) = @_; open FILE,">$filename" or die "Can't open file \"$filename\": $!\n"; if (defined($data)) { print FILE $data;} close FILE or die "Can't close file \"$filename\": $!\n"; } # Not used at the moment. sub trailerData { my($product_id) = 0x0001; my($protocol_id) = 0x0000; my($firmware_version) = 0x2325; my($unknown1) = 0x90f7; my($magic_number) = 'eRcOmM'; my($unknown2) = 0x00b9; return pack("n4a6n",$product_id,$protocol_id,$firmware_version,$unknown1,$magic_number,$unknown2); } # Print the contents of the Sercomm RedBoot trailer. sub printRedbootTrailer { my($redboot_data) = @_; my($correct_redboot_len) = 0x40000; my($redboot_data_len) = length($redboot_data); if ($redboot_data_len != $correct_redboot_len) { printf("Redboot length (0x%08X) is not 0x%08X\n", $redboot_data_len, $correct_redboot_len); return; } # The trailer is the last 80 bytes of the redboot partition. my($redboot_trailer) = substr($redboot_data, -80); writeOut($redboot_trailer, 'RedbootTrailer'); my($mac_addr0, $mac_addr1, $mac_addr2, $unknown, $prefix, $ver_ctrl, $down_ctrl, $hid, $hver, $prodid, $prodidmask, $protid, $protidmask, $funcid, $funcidmask, $fver, $cseg, $csize, $postfix) = unpack("n3Na7n2a32n10a7",$redboot_trailer); printf("MAC address is %04X%04X%04X\n", $mac_addr0, $mac_addr1, $mac_addr2); printf("unknown: %08X\n", $unknown); printf("%s:%04X:%04X:%s\n", $prefix, $ver_ctrl, $down_ctrl, $postfix); printf("VerControl: %04X\nDownControl: %04X\n", $ver_ctrl, $down_ctrl); printf("hid: %04X %04X %04X %04X %04X %04X %04X %04X %04X %04X %04X %04X %04X %04X %04X %04X\n", unpack("n16", $hid)); printf("Hver: %04X\nProdID: %04X\nProtID: %04X\nFuncID: %04X\nFver: %04X\nCseg: %04X\nCsize: %04X\n", $hver, $prodid, $protid, $funcid, $fver, $cseg, $csize); } # populate @partitions based on the firmware's partition table sub spliceFirmwarePartitions { my($firmware_buf, $partitions_ref) = @_; # we know that partition table, if it exists, begins at start of 'FIS directory' and has max length 0x1000 my($partition_table); map { $_->{'name'} eq 'FIS directory' and $partition_table = findPartitionTable(substr($firmware_buf, $_->{'offset'}, $_->{'size'})); } @$partitions_ref; # return 0 here if no partition table in FIS directory return if not $partition_table; my @new_partitions = parsePartitionTable($partition_table); my($partition_count) = 0; my($splice) = 0; map { # Skip pseudo partitions. while (($partition_count < scalar(@$partitions_ref)) and $partitions_ref->[$partition_count]->{'pseudo'}) { $debug and printf("Skipped <%s> (pseudo partition)\n", $partitions_ref->[$partition_count]->{'name'}); $partition_count++; } # If we are in a variable area, and we haven't reached the end of it, # then splice in another partition for use by the later code. if ($splice and ($partitions_ref->[$partition_count]->{'name'} ne $_->[0])) { $debug and printf("Splicing new partition <%s> before <%s>\n", $_->[0], $partitions_ref->[$partition_count]->{'name'}); splice(@{$partitions_ref}, $partition_count, 0, ({'name' => "",'variable'=>1,'header'=>0})); } my $partition = $partitions_ref->[$partition_count]; # Variable partitions can be overridden by the real FIS directory if ($partition->{'variable'}) { # Only override the filename if the partition name is not set or doesn't match if ($partition->{'name'} ne $_->[0]) { if (length($partition->{'name'})) { $debug and printf("Overwriting <%s> with <%s>\n", $partitions_ref->[$partition_count]->{'name'}); } $partition->{'name'} = $_->[0]; $partition->{'file'} = $_->[0]; } # Set the offset and size based on the real partition table $partition->{'offset'} = $_->[1]; $partition->{'size'} = $_->[2]; $debug and printf("Locating <%s> at 0x%08X (%s)\n", $partition->{'name'}, $partition->{'offset'}, ($partition->{'size'} >= $block_size ? sprintf("%d blocks", numBlocks($partition->{'size'})) : sprintf("0x%05X bytes", $partition->{'size'}))); $splice = 1; } # Fixed partitions cannot be overridden else { ($partition->{'name'} eq $_->[0]) or die "Unexpected partition <",$_->[0],"> (expecting <",$partition->{'name'},">)\n"; $splice = 0; } $partition_count++; } @new_partitions; return; } # Read in an 8MB firmware file, and store the data into @partitions. # Note that the data is only stored in a partition if 'offset' and 'size' are defined, # and it does not already have data stored in it. sub readInFirmware { my($filename, $partitions_ref) = @_; my($firmware_buf); my($total_length) = 0x800000; open FILE,$filename or die "Can't find firmware image \"$filename\": $!\n"; read FILE,$firmware_buf,$total_length or die "Can't read $total_length bytes from \"$filename\": $!\n"; close FILE or die "Can't close \"$filename\": $!\n"; $debug and printf("Read 0x%08X bytes from \"%s\"\n", length($firmware_buf), $filename); spliceFirmwarePartitions($firmware_buf, $partitions_ref); # Read the parts of the firmware file into the partitions table. map { if (defined $_->{'offset'} and defined $_->{'size'}) { if (defined $_->{'data'}) { $debug and printf("Not overwriting data in <%s>\n", $_->{'name'}); } else { # Slurp up the data, based on whether a header is present or not if ($_->{'header'}) { # Read the length, and grab the data based on the length. my($data_len) = unpack("N", substr($firmware_buf, $_->{'offset'})); $debug and printf("Found header size of 0x%08X bytes for <%s>\n", $data_len, $_->{'name'}); # A length of 0xFFFFFFFF means that the area is not initialised if ($data_len != 0xFFFFFFFF) { $_->{'data'} = substr($firmware_buf, $_->{'offset'} + $_->{'header'}, $data_len); } } else { # Grab the whole partition, using the maximum size. $_->{'data'} = substr($firmware_buf, $_->{'offset'}, $_->{'size'}); } $quiet or defined $_->{'data'} and printf("Read %s into <%s>\n", (length($_->{'data'}) >= $block_size ? sprintf("%d blocks", numBlocks(length($_->{'data'}))) : sprintf("0x%05X bytes", length($_->{'data'}))), $_->{'name'}); } } } @$partitions_ref; } # Write the partition data stored in memory out into the files associated with each. sub writeOutFirmwareParts { my(@partitions) = @_; # Write out the parts of the firmware file. map { # We can only write if 'data' and 'file' are defined. if (defined $_->{'file'} and defined $_->{'data'} and length($_->{'data'})) { writeOut($_->{'data'}, $_->{'file'}); $quiet or printf("Wrote 0x%08X bytes from <%s> into \"%s\"\n", length($_->{'data'}), $_->{'name'}, $_->{'file'}); } else { $debug and printf("Skipping <%s> (%s)\n", $_->{'name'}, (not defined $_->{'file'}) ? "no filename specified" : "no data to write"); } } @partitions; return; } # Read in the partition data from the files associated with each and store in memory. sub readInFirmwareParts { my(@partitions) = (@_); undef $/; # we want to slurp map { my $file = $_->{'file'}; if (defined $file) { open FILE,$file or die "Can't find firmware part \"$file\": $!\n"; # Slurp in the data $_->{'data'} = ; # close the file close FILE or die "Can't close file \"$file\": $!\n"; # Keep track of the actual size. my $size; # Account for the length header if required. if ($_->{'header'}) { $size = paddedSize($_->{'header'} + length($_->{'data'})); } elsif (not $_->{'pseudo'}) { $size = paddedSize(length($_->{'data'})); } else { $size = length($_->{'data'}); } # If the partition does not have a fixed size, the calculate the size. if (not defined $_->{'size'}) { $_->{'size'} = $size; } # Keep the user appraised ... $quiet or printf("Read 0x%08X bytes from \"%s\" into <%s> (%s / %s)\n", length($_->{'data'}), $_->{'file'}, $_->{'name'}, ($size >= $block_size ? sprintf("%d blocks", numBlocks($size)) : sprintf("0x%05X bytes", $size)), ($_->{'size'} >= $block_size ? sprintf("%d blocks", numBlocks($_->{'size'})) : sprintf("0x%05X bytes", $_->{'size'}))); } } @partitions; return; } # layoutPartitions : this function must be ugly - it needs to verify RedBoot, SysConf, Kernel, Ramdisk, and # FIS directory partitions exist, are in the correct order, and do not have more data than can fit in # their lengths (fixed for all but Ramdisk, which has a minimum length of one block). # If Rootdisk and/or Userdisk exist, it must also verify that their block padded lengths are not # too great for the available space. # input : an array of hashes, some of which are populated with data # output: same reference with start and size (partition not data) also populated. this populated structure # can then be passed to buildPartitionTable() to generate the actual partition table data sub layoutPartitions { my(@partitions) = @_; my($flash_start) = 0x50000000; my($flash_len) = 0x00800000; # Find the last variable size partition, and save a pointer to it for later use my $lastdisk; my $directory_offset; my $curdisk = $partitions[0]; map { if (not defined $lastdisk) { if ($_->{'name'} eq "FIS directory") { $lastdisk = $curdisk; $directory_offset = $_->{'offset'}; } else { $curdisk = $_; } } } @partitions; $lastdisk or die "Couldn't find the last variable size partition\n"; $debug and printf("Last variable size partition is <%s>\n", $lastdisk->{'name'}); # # here we go through the $partitions array ref and fill in all the values # # This points to where the next partition should be placed. my $pointer = $flash_start; map { # If this is the last variable size partition, then fill the rest of the space. if ($_->{'name'} eq $lastdisk->{'name'}) { $_->{'size'} = paddedSize($directory_offset + $flash_start - $pointer); } # Handle requests for partition creation first. if (defined $_->{'size'} and not defined $_->{'data'} and ($_->{'name'} ne "FIS directory")) { # A zero size is a request to fill all available space. if ($_->{'size'} == 0) { # Grab the start of the FIS directory, and use all the space up to there. $_->{'size'} = paddedSize($directory_offset + $flash_start - $pointer); # Create an empty partition of the requested size. $_->{'data'} = padBytes("", $_->{'size'}); } if (not defined $_->{'offset'}) { # Check to make sure that the requested size is not too large. if (($pointer + $_->{'size'}) > ($flash_start + $directory_offset)) { die "Ran out of flash space in <", $_->{'name'}, ">\n"; } } else { # Check to make sure that the requested size is not too large. if (($_->{'offset'} + $_->{'size'}) > ($flash_start + $directory_offset)) { die "Ran out of flash space in <", $_->{'name'}, ">\n"; } } } # Then handle known partitions, and allocate them. if (defined $_->{'size'}) { # Determine the start and offset of the current partition. if (defined $_->{'offset'}) { $_->{'start'} = $flash_start + $_->{'offset'}; my $size = defined $_->{'data'} ? length($_->{'data'}) : 0; # Keep the user appraised ... $debug and printf("Allocated <%s> from 0x%08X to 0x%08X (%s / %s)\n", $_->{'name'}, $_->{'start'}, $_->{'start'} + $_->{'size'}, ($size >= $block_size ? sprintf("%d blocks", numBlocks($size)) : sprintf("0x%05X bytes", $size)), ($_->{'size'} >= $block_size ? sprintf("%d blocks", numBlocks($_->{'size'})) : sprintf("0x%05X bytes", $_->{'size'}))); } # If offset is not defined, then calculate it. else { $_->{'start'} = $pointer; $_->{'offset'} = $_->{'start'} - $flash_start; my $size = defined $_->{'data'} ? length($_->{'data'}) : 0; # Keep the user appraised ... $debug and printf("Allocated <%s> from 0x%08X to 0x%08X (%s / %s)\n", $_->{'name'}, $_->{'start'}, $_->{'start'} + $_->{'size'}, ($size >= $block_size ? sprintf("%d blocks", numBlocks($size)) : sprintf("0x%05X bytes", $size)), ($_->{'size'} >= $block_size ? sprintf("%d blocks", numBlocks($_->{'size'})) : sprintf("0x%05X bytes", $_->{'size'}))); } # Check to make sure we have not run out of room. if (($_->{'start'} + $_->{'size'}) > ($flash_start + $flash_len)) { die "Ran out of flash space in <", $_->{'name'}, ">\n"; } # Move the pointer up, in preparation for the next partition. if ($_->{'variable'} and defined $_->{'data'}) { $pointer = $_->{'start'} + paddedSize(length($_->{'data'})); } else { $pointer = $_->{'start'} + paddedSize($_->{'size'}); } } } @partitions; return; } sub buildPartitionTable { my(@partitions) = @_; my($flash_start) = 0x50000000; my($partition_data) = ''; map { # Collate the partition data for all known partitions. if (not $_->{'pseudo'} and defined $_->{'offset'} and defined $_->{'size'}) { # Pack and append the binary table entry for this partition. $partition_data .= createPartitionEntry($_->{'name'}, $_->{'offset'} + $flash_start, $_->{'size'}); # If this is the FIS directory, then write the partition table data into it. if ($_->{'name'} eq "FIS directory") { # Explicitly terminate the partition data. $partition_data .= pack("C",0xff) x 0x100; $_->{'data'} = padBytes($partition_data, $_->{'size'}); } my $size = length($_->{'data'}); # Keep the user appraised ... $debug and printf("Table entry <%s> from 0x%08X to 0x%08X (%s / %s)\n", $_->{'name'}, $_->{'start'}, $_->{'start'} + $_->{'size'}, ($size >= $block_size ? sprintf("%d blocks", numBlocks($size)) : sprintf("0x%05X bytes", $size)), ($_->{'size'} >= $block_size ? sprintf("%d blocks", numBlocks($_->{'size'})) : sprintf("0x%05X bytes", $_->{'size'}))); } else { $debug and print "No table entry required for <", $_->{'name'}, ">\n"; } } @partitions; return; } sub writeOutFirmware { my($filename, @partitions) = @_; # Clear the image to start. my $image_buf = ""; map { # We can only write a partition if it has an offset, a size, and some data to write. if (defined $_->{'offset'} and defined $_->{'size'} and defined $_->{'data'}) { # Keep track of the end of the image. my $end_point = length($image_buf); # If the next partition is well past the end of the current image, then pad it. if ($_->{'offset'} > $end_point) { $image_buf .= padBytes("", $_->{'offset'} - $end_point); $quiet or printf("Padded %s before <%s> in \"%s\"\n", ((length($image_buf) - $end_point) >= $block_size ? sprintf("%d blocks", numBlocks(length($image_buf) - $end_point)) : sprintf("0x%05X bytes", length($image_buf) - $end_point)), $_->{'name'}, $filename); } # If the next parition is before the end of the current image, then rewind. elsif ($_->{'offset'} < $end_point) { $debug and printf("Rewound %s before <%s> in \"%s\"\n", (($end_point - $_->{'offset'}) >= $block_size ? sprintf("%d blocks", numBlocks($end_point - $_->{'offset'})) : sprintf("0x%05X bytes", length($end_point - $_->{'offset'}))), $_->{'name'}, $filename); # if (($end_point - $_->{'offset'}) >= $block_size) { # die "Allocation error: rewound a full block or more ...\n"; # } } # Splice the data into the image at the appropriate place, padding as required. substr($image_buf, $_->{'offset'}, $_->{'size'}, $_->{'header'} ? padBytes(pack("N4",length($_->{'data'})).$_->{'data'}, $_->{'size'}) : padBytes($_->{'data'}, $_->{'size'})); # Keep the user appraised ... $quiet or printf("Wrote %s (0x%08X to 0x%08X) from <%s> into \"%s\"\n", ($_->{'size'} >= $block_size ? sprintf("%d blocks", numBlocks($_->{'size'})) : sprintf("0x%05X bytes", $_->{'size'})), $_->{'offset'}, $_->{'offset'}+$_->{'size'}, $_->{'name'}, $filename); } # If we are not able to write a partition, then give debug information about why. else { $debug and printf("Skipping <%s> (%s)\n", $_->{'name'}, (not defined $_->{'offset'}) ? "no offset defined" : ((not defined $_->{'size'}) ? "no size defined" : "no data available")); } } @partitions; # Write the image to the specified file. writeOut($image_buf, $filename); return; } # checkPartitionTable: sanity check partition table - for testing but might evolve into setting @partitions # so that we can write out jffs2 partitions from a read image # currently not nearly paranoid enough sub checkPartitionTable { my($data) = @_; my($zero_long) = 0x0000; my($pointer) = 0; my($entry); my($name, $flash_base, $size, $done, $padding); do { $entry = substr($data, $pointer, 0x100); ($name,$flash_base,$zero_long,$size,$zero_long,$zero_long,$padding,$zero_long,$zero_long) = unpack("a16N5x212N2",$entry); $debug and printf("pointer: %d\tname: %s\tflash_base: 0x%08X\tsize: 0x%08X\n", $pointer, $name, $flash_base, $size); $pointer += 0x100; $debug and printf("terminator: 0x%08X\n", unpack("C", substr($data, $pointer, 1))); if (unpack("C", substr($data, $pointer, 1)) eq 0xff) { $done = 1; } } until $done; } sub printPartitions { my(@partitions) = @_; my($offset, $size); map { # defined $_->{'size'} ? $size = $_->{'size'} : $size = 'undef'; if (defined $_->{'size'}) { $size = $_->{'size'}; } else { $size = 'undef'; } if (defined $_->{'offset'}) { $offset = $_->{'offset'}; } else { $offset = 'undef'; } printf("%s\t0x%08X\t0x%08X\n", $_->{'name'}, $offset, $size); } @partitions; } sub defaultPartitions { return ({'name'=>'RedBoot', 'file'=>'RedBoot', 'offset'=>0x00000000, 'size'=>0x00040000, 'variable'=>0, 'header'=>0, 'pseudo'=>0, 'data'=>undef}, {'name'=>'EthAddr', 'file'=>undef, 'offset'=>0x0003ffb0, 'size'=>0x00000006, 'variable'=>0, 'header'=>0, 'pseudo'=>1, 'data'=>undef}, {'name'=>'SysConf', 'file'=>'SysConf', 'offset'=>0x00040000, 'size'=>0x00020000, 'variable'=>0, 'header'=>0, 'pseudo'=>0, 'data'=>undef}, {'name'=>'Kernel', 'file'=>'vmlinuz', 'offset'=>0x00060000, 'size'=>0x00100000, 'variable'=>0, 'header'=>16, 'pseudo'=>0, 'data'=>undef}, {'name'=>'Ramdisk', 'file'=>'ramdisk.gz', 'offset'=>0x00160000, 'size'=>0x006a0000, 'variable'=>1, 'header'=>16, 'pseudo'=>0, 'data'=>undef}, {'name'=>'FIS directory', 'file'=>undef, 'offset'=>0x007e0000, 'size'=>0x00020000, 'variable'=>0, 'header'=>0, 'pseudo'=>0, 'data'=>undef}, {'name'=>'SlugMark', 'file'=>undef, 'offset'=>0x007e0f02, 'size'=>0x00000006, 'variable'=>0, 'header'=>0, 'pseudo'=>1, 'data'=>'sLuGmK'}, {'name'=>'SwitchArgs', 'file'=>undef, 'offset'=>0x007e0f08, 'size'=>0x000000f8, 'variable'=>0, 'header'=>0, 'pseudo'=>1, 'data'=>undef}, {'name'=>'Payload', 'file'=>undef, 'offset'=>0x007e1000, 'size'=>0x0001eff0, 'variable'=>1, 'header'=>16, 'pseudo'=>1, 'data'=>undef}, {'name'=>'Trailer', 'file'=>'Trailer', 'offset'=>0x007ffff0, 'size'=>0x00000010, 'variable'=>0, 'header'=>0, 'pseudo'=>1, 'data'=>undef}, ); } # Main routine starts here ... my(@partitions) = defaultPartitions(); my($unpack, $pack, $input, $output, $redboot, $kernel, $sysconf, $ramdisk, $fisdir, $payload, $trailer, $ethaddr); if (!GetOptions("d|debug" => \$debug, "q|quiet" => \$quiet, "u|unpack" => \$unpack, "p|pack" => \$pack, "i|input=s" => \$input, "o|output=s" => \$output, "b|redboot=s" => \$redboot, "k|kernel=s" => \$kernel, "s|sysconf=s" => \$sysconf, "r|ramdisk=s" => \$ramdisk, "f|fisdir=s" => \$fisdir, "l|payload=s" => \$payload, "t|trailer=s" => \$trailer, "e|ethaddr=s" => \$ethaddr, ) or (not defined $pack and not defined $unpack)) { print "Usage: slugimage \n"; print "\n"; print " [-d|--debug] Turn on debugging output\n"; print " [-q|--quiet] Turn off status messages\n"; print " [-u|--unpack] Unpack a firmware image\n"; print " [-p|--pack] Pack a firmware image\n"; print " [-i|--input] Input firmware image filename\n"; print " [-o|--output] Output firmware image filename\n"; print " [-b|--redboot] Input/Output RedBoot filename\n"; print " [-k|--kernel] Input/Ouptut Kernel filename\n"; print " [-s|--sysconf] Input/Output SysConf filename\n"; print " [-r|--ramdisk] Input/Output Ramdisk filename(s)\n"; print " [-f|--fisdir] Input/Output FIS directory filename\n"; print " [-l|--payload] Input/Output Payload filename\n"; print " [-t|--trailer] Input/Output Trailer filename\n"; print " [-e|--ethaddr] Set the Ethernet address\n"; # TODO: document --ramdisk syntax exit 1; } # Go through the partition options, and set the names and files in @partitions if (defined $redboot) { map { ($_->{'name'} eq 'RedBoot') && ($_->{'file'} = $redboot); } @partitions; } if (defined $kernel) { map { ($_->{'name'} eq 'Kernel') && ($_->{'file'} = $kernel); } @partitions; } if (defined $sysconf) { map { ($_->{'name'} eq 'SysConf') && ($_->{'file'} = $sysconf); } @partitions; } if (defined $fisdir) { map { ($_->{'name'} eq 'FIS directory') && ($_->{'file'} = $fisdir); } @partitions; } if (defined $payload) { map { ($_->{'name'} eq 'Payload') && ($_->{'file'} = $payload); } @partitions; } if (defined $trailer) { map { ($_->{'name'} eq 'Trailer') && ($_->{'file'} = $trailer); } @partitions; } if (defined $ethaddr) { map { if ($_->{'name'} eq 'EthAddr') { $ethaddr =~ s/://g; if (($ethaddr !~ m/^[0-9A-Fa-f]+$/) or (length($ethaddr) != 12)) { die "Invalid ethernet address specification: '".$ethaddr."'\n"; } $_->{'data'} = pack("H12", $ethaddr); } } @partitions; } if (defined $ramdisk) { # A single filename is used for the ramdisk filename if ($ramdisk !~ m/[:,]/) { map { ($_->{'name'} eq 'Ramdisk') && ($_->{'file'} = $ramdisk); } @partitions; } # otherwise, it's a list of name:file mappings else { my @mappings = split(',', $ramdisk); # Find the index of the Ramdisk entry my $index; my $count = 0; map { if (not defined $index) { if ($_->{'name'} eq "Ramdisk") { $index = $count; } $count++; } } @partitions; defined $index or die "Cannot find the Ramdisk partition\n"; # Replace the Ramdisk entry with the new mappings splice(@partitions, $index, 1, map { # Preserve the information from the ramdisk entry my %entry = %{$partitions[$index]}; # Parse the mapping ($_ =~ m/^([^:]+):([^:]+)(:([^:]+))?$/) or die "Invalid syntax in --ramdisk\n"; $entry{'name'} = $1; $entry{'file'} = $2; my $size = $4; # If the mapping is not for the ramdisk, then undefine its attributes if ($entry{'name'} ne 'Ramdisk') { $entry{'offset'} = undef; $entry{'size'} = undef; $entry{'variable'} = 1; $entry{'header'} = 0; $entry{'pseudo'} = 0; $entry{'data'} = undef; } # Support specification of the number of blocks for empty jffs2 if ($entry{'file'} =~ m/^[0-9]+$/) { $size = $entry{'file'}; $entry{'file'} = undef; } # If the user has specified a size, then respect their wishes if (defined $size) { $entry{'size'} = $size * $block_size; # Create an empty partition of the requested size. $entry{'data'} = padBytes("", $entry{'size'}); if ($entry{'header'}) { $entry{'data'} = padBytes("", $entry{'size'} - $entry{'header'}); } } \%entry; } @mappings); } } # Read in the firmware image if ($input) { my $result = readInFirmware($input, \@partitions); } # Unpack the firmware if requested if ($unpack) { # map { # ($_->{'name'} eq 'FIS directory') and @partitions = checkPartitionTable($_->{'data'}); # } @partitions; writeOutFirmwareParts(@partitions); } # Undefine the ramdisk size - it must be calculated for packing # map { # ($_->{'name'} eq "Ramdisk") && ($_->{'size'} = undef); # } @partitions; # Pack the firmware if requested if ($pack) { my $result = readInFirmwareParts(@partitions); # if ($debug) { # print "after readInFirmwareParts():\n"; # printPartitions(@partitions); # map { # ($_->{'name'} eq 'RedBoot') && (printRedbootTrailer($_->{'data'})); # } @partitions; # } layoutPartitions(@partitions); # if ($debug) { # print "after layoutPartitions():\n"; # printPartitions(@partitions); # } buildPartitionTable(@partitions); # if ($debug) { # print "after buildPartitionTable():\n"; # printPartitions(@partitions); # my($lastblock); # map { # if ($_->{'name'} eq 'FIS directory') { # $lastblock = $_->{'data'}; # } # } @partitions; # print "checkPartitionTable():\n"; # checkPartitionTable($lastblock); # } die "Output filename must be specified\n" unless defined $output; writeOutFirmware($output, @partitions); } exit 0;