imgtools/romtools/rofsbuild/dumpdirs.pl
changeset 0 044383f39525
equal deleted inserted replaced
-1:000000000000 0:044383f39525
       
     1 #
       
     2 # Copyright (c) 2004-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 # All rights reserved.
       
     4 # This component and the accompanying materials are made available
       
     5 # under the terms of the License "Eclipse Public License v1.0"
       
     6 # which accompanies this distribution, and is available
       
     7 # at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 #
       
     9 # Initial Contributors:
       
    10 # Nokia Corporation - initial contribution.
       
    11 #
       
    12 # Contributors:
       
    13 #
       
    14 # Description: 
       
    15 # Perl script to dump the ROM root directory
       
    16 #
       
    17 
       
    18 my $imagefile="ba_001.engbuild.img";
       
    19 my $imageoffset=0;
       
    20 my $readsortedarrays = 1;
       
    21 my $imagetype="none";
       
    22 
       
    23 sub usage_and_die
       
    24 {
       
    25 die "Usage: $0 [-no-sorted-arrays] [imagefile]\n";
       
    26 }
       
    27 
       
    28 usage_and_die() if (@ARGV>2);
       
    29 
       
    30 foreach(@ARGV)
       
    31 	{
       
    32 	if (uc($_) eq '-NO-SORTED-ARRAYS')
       
    33 		{
       
    34 		$readsortedarrays = 0;
       
    35 		}
       
    36 	else
       
    37 		{
       
    38 		$imagefile = $_;
       
    39 		}
       
    40 	}
       
    41 
       
    42 #
       
    43 #
       
    44 #
       
    45 
       
    46 open IMAGE, "<$imagefile" or die "Cannot open $imagefile";
       
    47 binmode IMAGE;
       
    48 
       
    49 my $imagedata;
       
    50 my $errors=0;
       
    51 my $romrootdirectory;
       
    52 
       
    53 read IMAGE, $imagedata, 20;
       
    54 if ($imagedata =~ /^EPOC....ROM/)
       
    55 	{
       
    56 	$imageoffset -= 256;	# compensate for REPRO header
       
    57 	}
       
    58 else
       
    59 	{
       
    60 	# No REPRO header, rewind back to the start
       
    61 	seek(IMAGE,0, 0)
       
    62 	}
       
    63 
       
    64 #
       
    65 # identify the image type
       
    66 #
       
    67 
       
    68 my $coresize=0;
       
    69 my $id=readword(0);
       
    70 if ($id == 0x53464f52)	# ROFS
       
    71 	{
       
    72 	$coresize=readword(0x2c);	
       
    73 	printf "Core ROFS ROM image: %s\n", $imagefile;
       
    74 	$imagetype="ROFS";
       
    75 	$romrootdirectory = readword(8);
       
    76 	}
       
    77 elsif ($id == 0x78464f52) # ROFx
       
    78 	{
       
    79 	printf "Extension ROFS ROM image: %s\n", $imagefile;
       
    80 	$imagetype="ROFx";
       
    81 	exit (1);
       
    82 	}
       
    83 else
       
    84     {
       
    85     printf "Unknown ROM image type: %s\n", $imagefile;
       
    86 	exit (1);
       
    87     }
       
    88 
       
    89 #
       
    90 #
       
    91 #
       
    92 
       
    93 my @directories;
       
    94 push @directories, $romrootdirectory;
       
    95 if (seek(IMAGE, $coresize+8, 0))
       
    96 	{
       
    97 	my $extdir=0;
       
    98 	$extdir=readword($coresize+8);
       
    99 	if ($extdir) {
       
   100 		push @directories, $extdir;
       
   101 		printf "Extended ROFS @ %08x\n", $coresize;
       
   102 		}
       
   103 	}
       
   104 
       
   105 my $root;
       
   106 foreach $root (@directories)
       
   107     {
       
   108     printf "\n\nDirectory @ 0x%08x:\n", $root;
       
   109     print_directory($root, "");
       
   110     }
       
   111 
       
   112 my %seen_before;
       
   113 
       
   114 sub print_sortedarrays
       
   115 {
       
   116 	return if (!$readsortedarrays);
       
   117 	my $entry = shift ;
       
   118 	my $prefix = shift;
       
   119 	my $sortedarrayptr=$entry+4;
       
   120 	my $dircount=readshort($entry);
       
   121 	my $filecount=readshort($entry+2);
       
   122 
       
   123 	printf "%s dirs[%d]=[",$prefix, $dircount;
       
   124 	while ($dircount--)
       
   125 		{
       
   126 		printf "%04x",readshort($sortedarrayptr);
       
   127 		printf "," if $dircount;
       
   128 		$sortedarrayptr+=2;
       
   129 		}
       
   130 	print "]\n";
       
   131 
       
   132 	printf "%s files[%d]=[",$prefix,$filecount;
       
   133 	while ($filecount--)
       
   134 		{
       
   135 		printf "%04x",readshort($sortedarrayptr);
       
   136 		printf ","  if $filecount;
       
   137 		$sortedarrayptr+=2;
       
   138 		}
       
   139 	print "]\n";
       
   140 
       
   141 }
       
   142 
       
   143 sub print_entry
       
   144 	{
       
   145  	my ($entry, $prefix) = @_;
       
   146 	
       
   147 	my $size = readshort($entry);
       
   148 	my $nameoffset = readbyte(-1);
       
   149 	my $attributes = readbyte(-1);
       
   150 	my $filesize = readword(-1);
       
   151 
       
   152 	my $fileoffset = readword(-1); # fileaddr
       
   153 	my $attributesextra = readbyte(-1);
       
   154 
       
   155 	my $namelen = readbyte(-1);
       
   156 	my $name = readimage($namelen*2, -1);
       
   157 	$name =~ s/(.)./$1/g;	# drop high byte of Unicode characters
       
   158 
       
   159 	printf "%s %s\t%08x %02x %3d %08x\n", $prefix,$name, $entry, 
       
   160 				$attributes, $filesize, $fileoffset;
       
   161 
       
   162 	}
       
   163 
       
   164 sub print_directory
       
   165     {
       
   166     my ($dir, $prefix) = @_;
       
   167     if ($seen_before{$dir} == 1)
       
   168 		{
       
   169 		printf "%s ...\n", $prefix;
       
   170 		return;
       
   171 		}
       
   172     $seen_before{$dir} = 1;
       
   173 	my $dirstructsize=readshort($dir);
       
   174 	my $firstentryoffset=readbyte($dir+3);
       
   175 	my $entry = $dir+$firstentryoffset;
       
   176 	my $end = $dir+$dirstructsize;
       
   177 
       
   178 	# print the files in this dir
       
   179 	#
       
   180 	my $fileblockoffset = readword($dir+4);
       
   181 	my $fileblocksize	= readword($dir+8);
       
   182 	while ($fileblocksize>0)
       
   183 		{
       
   184 		my $fileentrysize=readshort($fileblockoffset);
       
   185 		print_entry($fileblockoffset, "$prefix");
       
   186 		$fileblocksize-=$fileentrysize;
       
   187 		$fileblockoffset+=$fileentrysize;
       
   188 		}
       
   189 
       
   190 	# print the sub dirs
       
   191 	my $firstentry = $entry;
       
   192     while ($entry < $end)
       
   193 		{
       
   194 		my $size = readshort($entry);
       
   195 		my $nameoffset = readbyte(-1);
       
   196 		my $attributes = readbyte(-1);
       
   197 		my $filesize = readword(-1);
       
   198 
       
   199 		my $fileoffset = readword(-1); # fileaddr
       
   200 		my $attributesextra = readbyte(-1);
       
   201 
       
   202 		my $namelen = readbyte(-1);
       
   203 		my $name = readimage($namelen*2, -1);
       
   204 		$name =~ s/(.)./$1/g;	# drop high byte of Unicode characters
       
   205 
       
   206 		printf "%s %s\\\t%08x \n", $prefix, $name, $entry;
       
   207 		print_directory($fileoffset, "$prefix |");
       
   208 
       
   209 		$entry += $size;
       
   210 		$entry = ($entry+3)&~3;
       
   211 		}
       
   212 
       
   213     }
       
   214 
       
   215 
       
   216 
       
   217 #
       
   218 # read image subroutines
       
   219 #
       
   220 
       
   221 sub readimage
       
   222     {
       
   223     my ($length, $linaddr) = @_;
       
   224     my $imagedata;
       
   225 
       
   226 	if ($linaddr>=0)
       
   227 		{
       
   228 		if (!seek(IMAGE, $linaddr-$imageoffset, 0))
       
   229 	    	{
       
   230 		   	 printf "Can't seek to address 0x%x\n", $linaddr;
       
   231 	    	$errors++;
       
   232 	    	return "";
       
   233 	    	}
       
   234 		}
       
   235     read IMAGE, $imagedata, $length;
       
   236     return $imagedata;
       
   237     }
       
   238 
       
   239 sub readbyte
       
   240 	{
       
   241 	my ($linaddr) = @_;
       
   242     return unpack "C", readimage(1, $linaddr);
       
   243     }
       
   244 
       
   245 sub readword
       
   246     {
       
   247     my ($linaddr) = @_;
       
   248     return unpack "V", readimage(4, $linaddr);
       
   249     }
       
   250 
       
   251 sub readshort
       
   252 	{
       
   253 	my ($linaddr) = @_;
       
   254 	return unpack "v", readimage(2, $linaddr); # unsigned short in little-endian
       
   255 	}