os/kernelhwsrv/kernel/eka/rombuild/rom.pl
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
     1 #!perl
     2 #
     3 # Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
     4 # All rights reserved.
     5 # This component and the accompanying materials are made available
     6 # under the terms of the License "Eclipse Public License v1.0"
     7 # which accompanies this distribution, and is available
     8 # at the URL "http://www.eclipse.org/legal/epl-v10.html".
     9 #
    10 # Initial Contributors:
    11 # Nokia Corporation - initial contribution.
    12 #
    13 # Contributors:
    14 #
    15 # Description:
    16 #
    17 
    18 # rom.pl - Build a rom
    19 #
    20 # Pre-processes the .oby/iby files then invokes rombuild.exe
    21 # (or other specified builder)
    22 
    23 # First, read our config file
    24 
    25 use strict;
    26 use Getopt::Long;
    27 use Cwd;
    28 
    29 #Getopt::Long::Configure("ignore_case");
    30 my %opts=();
    31 my $param_count = scalar(@ARGV);
    32 my $result = GetOptions (\%opts, "assp=s",
    33 						 "inst=s",
    34 						 "type=s",
    35 						 "variant=s",
    36 						 "build=s", 
    37 						 "conf=s",
    38 						 "name=s",
    39 						 "modules=s",
    40 						 "xabi=s",
    41 						 "clean",
    42 						 "quiet",
    43 						 "help",
    44 						 "_debug",
    45 						 "zip",
    46 						 "symbol",
    47 						 "noheader",
    48 						 "kerneltrace=s",
    49 						 "rombuilder=s",
    50 						 "define=s@",
    51 						 "rofsbuilder=s",
    52 						 "compress"
    53 						 );
    54 
    55 my (@assps, @builds, %variants, @templates, %flags, %insts, %zip, %builder);
    56 my $main;
    57 my $kmain;
    58 my $toroot;
    59 my $e32path;
    60 my $rombuildpath;
    61 my $euserdir;
    62 my $elocldir;
    63 my $kbdir;
    64 my $romname;
    65 my $single;
    66 my $smain;
    67 my $pagedCode;
    68 my $debug = $opts{_debug};
    69 my $quiet;
    70 $quiet = $opts{quiet} unless $debug;
    71 
    72 my $drive;
    73 
    74 # discover where this script is running from to set the $toroot and $e32path variables
    75 
    76 my $toolpath;
    77 my $Epoc32Path;
    78 my $EpocRoot;
    79 my $drive;
    80 my $BasePath;
    81 
    82 BEGIN {
    83 
    84 	$EpocRoot = $ENV{EPOCROOT};
    85 	die "ERROR: Must set the EPOCROOT environment variable.\n" if (!defined($EpocRoot));
    86 	$EpocRoot =~ s-/-\\-go;	# for those working with UNIX shells
    87 	die "ERROR: EPOCROOT must be an absolute path, " .
    88 		"not containing a drive letter.\n" if ($EpocRoot !~ /^\\/);
    89 	die "ERROR: EPOCROOT must not be a UNC path.\n" if ($EpocRoot =~ /^\\\\/);
    90 	die "ERROR: EPOCROOT must end with a backslash.\n" if ($EpocRoot !~ /\\$/);
    91 	die "ERROR: EPOCROOT must specify an existing directory.\n" if (!-d $EpocRoot);
    92 
    93 	# The cpp needed a drive apparently
    94 	my $cwd = Cwd::cwd();
    95 	$cwd =~ /^(.)/;
    96 	$drive = "$1:";
    97 
    98 	my $fp0 = $0;
    99 	$cwd =~ s/\//\\/g;
   100 	$fp0 =~ s/\//\\/g;
   101 	unless ($fp0 =~ /^([A-Za-z]:)?\\/) {
   102 		if ($cwd =~ /\\$/) {
   103 			$fp0 = "$cwd$fp0";
   104 		} else {
   105 			$fp0 = "$cwd\\$fp0";
   106 		}
   107 	}
   108 
   109 	my @path = split(/\\/, $cwd);
   110 	shift(@path);
   111 	$toroot = ('..\\') x @path;
   112 	$e32path = $fp0;
   113 	$e32path =~ s/\\kernelhwsrv\\kernel\\eka\\rombuild\\rom\.pl$//i;
   114 	$e32path =~ s/^[A-Za-z]://;
   115 	$rombuildpath = $toroot."os\\kernelhwsrv\\kernel\\eka\\rombuild";
   116 	$Epoc32Path = $toroot;
   117 	$Epoc32Path =~ s/\\$//;
   118 	$Epoc32Path .= $EpocRoot . "epoc32";
   119 	$toolpath = "$Epoc32Path\\tools\\";
   120 	push @INC, $toolpath;
   121 	$BasePath = $toroot;
   122 	$BasePath =~ s/\\$/$e32path\\/;
   123 }
   124 
   125 use E32Plat;
   126 {
   127         Plat_Init($toolpath);
   128 }
   129 
   130 if ($debug) {
   131 	print "EpocRoot = $EpocRoot\n";
   132 	print "Epoc32Path = $Epoc32Path\n";
   133 	print "drive = $drive\n";
   134 	print "toolpath = $toolpath\n";
   135 	print "toroot = $toroot\n";
   136 	print "e32path = $e32path\n";
   137 	print "rombuildpath = $rombuildpath\n";
   138 	print "BasePath = $BasePath\n";
   139 }
   140 
   141 my $cppflags="-P -undef -traditional -lang-c++ -nostdinc -iwithprefixbefore $rombuildpath -I $rombuildpath -I $Epoc32Path ";
   142 
   143 # Include variant hrh file defines when processing oby and ibys with cpp
   144 # (Code copied from \\EPOC\master\cedar\generic\tools\romkit\tools\buildrom.pm -
   145 # it used relative path to the working dir but we are using absolute path seems neater)
   146 use E32Variant;
   147 use Pathutl;
   148 my $variantMacroHRHFile = Variant_GetMacroHRHFile();
   149 if($variantMacroHRHFile){
   150 	# Using absolute paths so must include drive letter otherwise cpp will fail
   151 	# Also adding the directory containing the HRH file to main includes paths in
   152 	# case it includes files
   153 	my $variantFilePath = Path_Split('Path',$variantMacroHRHFile);
   154 	$cppflags .= " -I $drive$variantFilePath -include $drive$variantMacroHRHFile";
   155 }
   156 
   157 if($param_count == 0 || $opts{'help'} || !$result) {
   158 	usage();
   159 	exit 0;
   160 }
   161 
   162 # Now check that the options we have make sense
   163 
   164 checkopts();
   165 
   166 if (!$quiet) {
   167 	print "Starting directory: ", Cwd::cwd(), "\n";
   168 	print <<EOF;
   169 OPTIONS:
   170 \tTYPE: $opts{'type'}
   171 \tVARIANT: $opts{'variant'}
   172 \tINSTRUCTION SET: $opts{'inst'}
   173 \tBUILD: $opts{'build'}
   174 \tMODULES: $opts{'modules'}
   175 EOF
   176 }
   177 
   178 #Pick out the type file
   179 my $skel;
   180 
   181 if (-e "$opts{'type'}.oby") {
   182 	$skel="$opts{'type'}.oby";
   183 } elsif (-e "$rombuildpath\\$opts{'type'}.oby") {
   184 	$skel="$rombuildpath\\$opts{'type'}.oby";
   185 } else {
   186 	die "Can't find type file for type $opts{'type'}, $!";
   187 }
   188 
   189 print "Using type file $skel\n" if !$quiet;
   190 
   191 # If clean is specified, zap all the image and .oby files
   192 
   193 if($opts{'clean'}) {
   194 	unlink glob("*.img");
   195 	unlink "rom.oby";
   196 	unlink "rombuild.log";
   197 	unlink glob("*.rofs");
   198 	unlink "rofsbuild.log";
   199 }
   200 
   201 # Now pre-pre-process this file to point to the right places for .ibys
   202 # Unfortunately cpp won't do macro replacement in #include strings, so
   203 # we have to do it by hand
   204 
   205 my $k = $opts{kerneltrace};
   206 
   207 if ($opts{assp}=~/^m(\S+)/i) {
   208 	$kbdir="kb$1";
   209 	$kbdir="kbarm" if (lc $1 eq 'eig');
   210 } else {
   211 	$kbdir="kb$opts{assp}";
   212 }
   213 $single=1 if ($opts{assp}=~/^s(\S+)/i);
   214 
   215 if ($single) {
   216 	# Hackery to cope with old compiler
   217 	if ($main eq 'MARM') {
   218 		$smain='SARM';
   219 	} else {
   220 		$smain="S$main";
   221 	}
   222 } else {
   223 	$smain=$main;
   224 }
   225 
   226 open(X, "$skel") || die "Can't open type file $skel, $!";
   227 open(OUT, "> rom1.tmp") || die "Can't open output file, $!";
   228 
   229 # First output the ROM name
   230 print OUT "\nromname=$romname\n";
   231 while(<X>) {
   232 	s/\#\#ASSP\#\#/$opts{'assp'}/;
   233 	s/\#\#VARIANT\#\#/$opts{'variant'}/;
   234 	s/\#\#BUILD\#\#/$opts{'build'}/;
   235 	s/\#\#MAIN\#\#/$main/;
   236 	s/\#\#KMAIN\#\#/$kmain/;
   237 	s/\#\#E32PATH\#\#/$e32path/;
   238 	s/\#\#BASEPATH\#\#/$BasePath/;
   239 	s/\#\#EUSERDIR\#\#/$euserdir/;
   240 	s/\#\#ELOCLDIR\#\#/$elocldir/;
   241 	s/\#\#KBDIR\#\#/$kbdir/;
   242 	print OUT;
   243 }
   244 
   245 close X;
   246 close OUT;
   247 
   248 # Use cpp to pull in include chains and replace defines
   249 
   250 my $defines = "";
   251 $defines .= "-D MAIN=$main ";
   252 $defines .= "-D KMAIN=$kmain ";
   253 $defines .= "-D EUSERDIR=$euserdir ";
   254 $defines .= "-D ELOCLDIR=$elocldir ";
   255 $defines .= "-D E32PATH=$e32path ";
   256 $defines .= "-D BASEPATH=$BasePath ";
   257 $defines .= "-D EPOCROOT=$EpocRoot ";
   258 $defines .= "-D SMAIN=$smain " if $smain;
   259 
   260 foreach (@{$opts{'define'}}) {
   261 	my @array=split(/,/,$_);
   262 	foreach (@array) {
   263 		$defines.="-D ".uc $_." ";
   264 		$pagedCode = 1 if $_ eq 'PAGED_CODE';
   265 		}
   266 	}
   267 
   268 if ($opts{'modules'}) {
   269 	my @array=split(/,/,$opts{'modules'});
   270 	foreach (@array) {
   271 		$defines.="-D ".uc $_." ";
   272 		}
   273 	}
   274 
   275 foreach (keys %opts) {
   276 	next if ($_ eq 'name');
   277 	next if ($_ eq 'modules');
   278 	next if ($_ eq 'zip');
   279 	next if ($_ eq 'symbol');
   280 	next if ($_ eq 'kerneltrace');
   281 	next if ($_ eq 'define');
   282 	$defines.="-D ".uc $_."=".$opts{$_}." ";
   283 	$defines.="-D ".uc $_."_".$opts{$_}." ";
   284 }
   285 
   286 $defines.="-D SINGLE " if ($single);
   287 
   288 sub IsRVCTBuild($) {
   289     my ($build)=@_;
   290     return 1 if ($build =~ /^ARMV/i);
   291 	my @customizations = Plat_Customizations('ARMV5');
   292 	return 1 if (grep /$build/, @customizations);
   293 	return 0;
   294 }
   295 
   296 $defines.="-D RVCT " if (IsRVCTBuild($main));
   297 
   298 print "Using defines $defines\n" if !$quiet;
   299 
   300 my $ret=1;
   301 my $cppcmd;
   302 if($opts{'build'}=~/^u/i) {
   303 	# Unicode build
   304 	$cppcmd = "cpp $cppflags -D UNICODE $defines rom1.tmp rom2.tmp";
   305 } else {
   306 	$cppcmd = "cpp $cppflags $defines rom1.tmp rom2.tmp";
   307 }
   308 print "Executing CPP:\n\t$cppcmd\n" if $debug;
   309 $ret = system($cppcmd);
   310 die "ERROR EXECUTING CPP\n" if $ret;
   311 
   312 # Zap any ## marks REMS or blank lines
   313 
   314 cleanup("rom2.tmp", "rom3.tmp", $k);
   315 
   316 # scan tmp file and generate auxiliary files, if required
   317 open TMP, "rom3.tmp" or die("Can't open rom3.tmp\n");
   318 my $line;
   319 while ($line=<TMP>)
   320 	{
   321 	if ($line=~/\s*gentestpaged/i) {
   322 		genfile("paged");	}
   323 	if ($line=~/\s*gentestnonpaged/i) {
   324 		genfile("nonpaged");	}
   325 	}
   326 
   327 parsePatchData("rom3.tmp", "rom4.tmp");
   328 
   329 # break down the oby file into rom, rofs, extensions and smr oby files
   330 
   331 my $oby_index =0;
   332 my $dumpfile="rom.oby";
   333 my $rofs=0;
   334 my $smr=0;
   335 my $extension=0;
   336 my $corerofsname="";
   337 my $smrname="";
   338 open DUMPFILE, ">$dumpfile" or die("Can't create $dumpfile\n");
   339 my $line;
   340 open TMP, "rom4.tmp" or die("Can't open rom4.tmp\n");
   341 while ($line=<TMP>)
   342 	{
   343 	if ($line=~/^\s*rofsname/i)
   344 		{
   345 		close DUMPFILE;							# close rom.oby or previous rofs#/extension#.oby
   346 		$oby_index=1;
   347 		$corerofsname=$line;
   348 		$corerofsname =~ s/rofsname\s*=\s*//i;		# save core rofs name
   349 		$corerofsname =~ s/\s*$//g; 			# remove trailing \n
   350 		unlink $corerofsname || print "unable to delete $corerofsname";
   351 		my $dumpfile="rofs".$rofs.".oby";
   352 		$rofs++;
   353 		open DUMPFILE, ">$dumpfile" or (close TMP and die("Can't create $dumpfile\n"));
   354 		}
   355 
   356 	if ($line=~/^\s*imagename/i)
   357 		{
   358 		close DUMPFILE;							# close rom.oby or previous rofs#/extension#.oby
   359 		$smrname=$line;
   360 		$smrname =~ s/imagename\s*=\s*//i;		# save smr name
   361 		$smrname =~ s/\s*$//g; 			# remove trailing \n
   362 		unlink $smrname || print "unable to delete $smrname";
   363 		my $dumpfile="smr".$smr.".oby";
   364 		$smr++;
   365 		open DUMPFILE, ">$dumpfile" or (close TMP and die("Can't create $dumpfile\n"));
   366 		}
   367 
   368 	if ($line=~/^\s*coreimage/i)
   369 		{
   370 		close DUMPFILE;							# close rofs.oby
   371 		if ($oby_index ne 1) {
   372 			close TMP;
   373 			die "Must specify ROFS image before ROFS extension\n";
   374 		}
   375 		my $name=$line;
   376 		$name =~ s/coreimage\s*=\s*//i;		# read core rofs name
   377 		$name =~ s/\s*$//g; 			# remove trailing \n
   378 		if ($name ne $corerofsname) {
   379 			close TMP;
   380 			die "This extension does not relate to previous ROFS\n";
   381 		}
   382 		$oby_index=33;						# open window
   383 		my $dumpfile="extension".$extension.".oby";
   384 		$extension++;
   385 		open DUMPFILE, ">$dumpfile" or (close TMP and die("Can't create $dumpfile\n"));
   386 		}
   387 
   388 	if ($line=~/^\s*extensionrofs/i)
   389 		{
   390 		$oby_index=3 if ($oby_index eq 2);
   391 		}
   392 
   393 	if (($oby_index eq 2) && !($line=~/^\s*$/)) {
   394 		close TMP;
   395 		die "Bad ROFS extension specification\n";
   396 	}
   397 	print DUMPFILE $line;
   398 	$oby_index=2 if ($oby_index eq 33);		# close window
   399 	}
   400 close DUMPFILE;
   401 close TMP;
   402 
   403 # For paged roms that use rofs, move all data= lines in rom which are not 'paging_unmovable' to rofs, so that paged ram-loaded code
   404 # is automatically put into rofs
   405 rename('rom.oby', 'rom4.tmp') || die;
   406 
   407 open(IN, 'rom4.tmp') || die "Can't read rom4.tmp";
   408 open(ROM, '>rom.oby') || die "Can't write to rom.oby";
   409 
   410 if ($oby_index >= 1 && $pagedCode)	{
   411 	open(ROFS, '>>rofs0.oby') || die "Can't append to rofs0.oby";
   412 }
   413 
   414 while ($line=<IN>)
   415 {
   416 	if(($oby_index >= 1) && ($pagedCode) && ($line=~/^\s*data\s*=/) && !($line=~/\.*paging_unmovable\s*/)) {
   417 		print ROFS $line;
   418 	}
   419 	else {
   420 		$line=~s/paging_unmovable//;
   421 		print ROM $line;
   422 	}
   423 }
   424 
   425 close IN;
   426 close ROM;
   427 
   428 if ($oby_index >= 1 && $pagedCode)	{
   429 	close ROFS;
   430 }
   431 	unlink 'rom4.tmp';
   432 
   433 my $flags;
   434 
   435 foreach (@{$flags{$opts{'assp'}}}) {
   436 	$flags.=" -$_";
   437 }
   438 
   439 if($opts{'noheader'}) {
   440 	$flags.=" -no-header";
   441 }
   442 
   443 if($opts{'compress'}) {
   444 	$flags.=" -compress";
   445 }
   446 
   447 my $builder = $opts{'rombuilder'};
   448 $builder = "rombuild" unless ($builder);
   449 
   450 
   451 
   452 print "$builder $flags -type-safe-link -S rom.oby 2>&1\n\n";
   453 
   454 open(Y, "$builder $flags -type-safe-link -S rom.oby 2>&1 |") || 
   455 	die "Can't start $builder command, $!";
   456 
   457 my $nerrors=0;
   458 my $nwarnings=0;
   459 
   460 while(<Y>) {
   461 	my $error=(/^error:/i);
   462 	my $warning=(/^warning:/i);
   463 	print if ($error or $warning or !$quiet);
   464 	$nerrors++ if ($error);
   465 	$nwarnings++ if ($warning);
   466 }
   467 
   468 print "\nGenerated .oby file is rom.oby\n" if !$quiet;
   469 print "\nGenerated image file is $romname\n" if (!$nerrors);
   470 
   471 my$rerrors;
   472 my $rofsbuilder;
   473 if ($rofs) {
   474 	$rofsbuilder = $opts{'rofsbuilder'};
   475 	$rofsbuilder = "rofsbuild" unless ($rofsbuilder);
   476 	for(my $i=0;$i<$rofs;++$i) {
   477 		print "Executing $rofsbuilder on main rofs\n" if !$quiet;
   478 		my $image="rofs".$i.".oby";
   479 		system("$rofsbuilder $image");
   480 		if ($? != 0)
   481 			{
   482 			print "$rofsbuilder $image returned $?\n";
   483 			$rerrors++;
   484 			}
   485 		rename "rofsbuild.log", "rofs$i.log"
   486 		}
   487 }
   488 
   489 if ($rofs and $extension) {
   490 	for(my $i=0;$i<$extension;++$i) {
   491 		print "Executing $rofsbuilder on extension rofs\n" if !$quiet;
   492 		my $image="extension".$i.".oby";
   493 		system("$rofsbuilder $image");
   494 		if ($? != 0)
   495 			{
   496 			print "$rofsbuilder $image returned $?\n";
   497 			$rerrors++;
   498 			}
   499 		rename "rofsbuild.log", "extension$i.log"
   500 		}
   501 }
   502 
   503 if ($smr) {
   504 	$rofsbuilder = $opts{'rofsbuilder'};
   505 	$rofsbuilder = "rofsbuild" unless ($rofsbuilder);
   506 	for(my $i=0;$i<$smr;++$i) {
   507 		print "Executing $rofsbuilder on smr partition\n" if !$quiet;
   508 		my $image="smr".$i.".oby";
   509 		system("$rofsbuilder -smr=$image");
   510 		if ($? != 0)
   511 			{
   512 			print "$rofsbuilder -smr=$image returned $?\n";
   513 			$rerrors++;
   514 			}
   515 		rename "rofsbuild.log", "smr$i.log"
   516 		}
   517 }
   518 
   519 if ($nerrors) {
   520 	print "\n\n Errors found during $builder!!\n\nLeaving tmp files\n";
   521 } elsif ($nwarnings) {
   522 	print "\n\n Warnings during $builder!!\n\nLeaving tmp files\n";
   523 } elsif ($rerrors) {
   524 	print "\n\n Errors during $rofsbuilder!!\n\nLeaving tmp files\n";
   525 } else {
   526 	unlink glob("*.tmp") if !$debug;
   527 }
   528 if ($opts{zip} or $zip{$opts{assp}}) {
   529 	my $zipname=$romname;
   530 	$zipname =~ s/\.(\w+)$/\.zip/i;
   531 	unlink $zipname;
   532 	system("zip $zipname $romname");
   533 }
   534 if ($opts{symbol}) {
   535 	my $logname=$romname;
   536 	$logname =~ s/\.(\w+)$/\.log/i;
   537 	my $obyname=$romname;
   538 	$obyname =~ s/\.(\w+)$/\.oby/i;
   539 	unlink $logname;
   540 	unlink $obyname;
   541 	system("rename rombuild.log $logname");
   542 	system("rename rom.oby $obyname");
   543 	system("maksym $logname");
   544 }
   545 
   546 if ($nerrors || $nwarnings || $rerrors) {
   547 	exit 4;
   548 }	
   549 	
   550 exit 0;
   551 
   552 
   553 ################################ Subroutines  ##################################
   554 
   555 sub usage {
   556 	print <<EOT;
   557 
   558 rom <options>
   559 
   560 Generate a rom image for the specified target, along with a rom.oby file
   561 that can be fed to (a) rombuild to regenerate the image.
   562 
   563 The following options are required:
   564   --variant=<variant>         e.g. --variant=assabet
   565   --inst=<instruction set>    e.g. --inst=arm4
   566   --build=<build>             e.g. --build=udeb
   567   --type=<type of rom>  
   568          tshell for a text shell rom
   569          e32tests for a rom with e32tests
   570          f32tests for rom with f32tests
   571          alltests for all the tests
   572 
   573 The following are optional:
   574   --name=<image name>               Give image file specified name
   575   --noheader                        Pass -no-header option on to rombuild
   576   --help                            This help message.
   577   --clean                           Remove existing generated files first
   578   --quiet                           Be less verbose
   579   --modules=<comma separated list>  List of additional modules for this ROM
   580   --define=<comma separated list>   List of CPP macros to define
   581 
   582 Options may be specified as a short abbreviation 
   583 e.g. -b udeb instead of --build udeb
   584 
   585 EOT
   586 }
   587 
   588 sub cleanup($$$) {
   589 	my ($in, $out, $k) = @_;
   590 	my ($line, $lastblank);
   591 
   592 	open(OUTPUT_FILE, "> $out") or die "Cannot open $out for output";
   593 	open(INPUT_FILE, "< $in") or die "Cannot open for $in input";
   594   
   595 	while ($line=<INPUT_FILE>) {
   596 		$line=~s/##//g;
   597 
   598 		# file=\epoc32\...  ==> file=%EPOCROOT%\epoc32\...
   599 		$line =~ s/(=\s*)\\epoc32/\1${EpocRoot}Epoc32/i;
   600 
   601 		# Now compress blank lines down to one
   602 	
   603 		if($line=~/^\s*$/) {
   604 			if($lastblank) {
   605 				# Do nothing
   606 			} else {
   607 				# This is the first blank line
   608 				$lastblank=1;
   609 				print OUTPUT_FILE $line;
   610 			}
   611 		} else {
   612 			# Not blank
   613 			$lastblank=0;
   614 			if ($k and $line=~/^\s*kerneltrace/i) {
   615 				$line = "kerneltrace $k\n";
   616 			}
   617 			print OUTPUT_FILE $line if !($line=~/^\s*REM\s+/i);
   618 		}
   619 	}
   620 	close(INPUT_FILE);
   621 	close(OUTPUT_FILE);
   622 }
   623 
   624 sub IsSmp($) {
   625 	my %SmpKernelDirs=(
   626 		'x86smp' => 1,
   627 		'x86gmp' => 1,
   628 		'arm4smp' => 1,
   629 		'armv4smp' => 1,
   630 		'armv5smp' => 1
   631 	);
   632 
   633 	my ($kdir) = @_;
   634 	return $SmpKernelDirs{lc $kdir};
   635 }
   636 
   637 sub checkopts {
   638 	unless($opts{variant}) { die "No Variant specified"; }
   639 	$opts{'build'}="UDEB" unless($opts{'build'});
   640 	$opts{'type'}="TSHELL" unless($opts{'type'});
   641 	$opts{'inst'}="ARM4" unless($opts{'inst'});
   642 
   643 	my $additional;
   644 	if ($opts{'modules'}) {
   645 		$additional="_".$opts{modules};
   646 		$additional=~ s/,/_/ig;
   647 	}
   648 	my $build=lc $opts{build};
   649 	my $inst=uc $opts{'inst'};
   650 	if ($inst eq "MARM") {
   651 		# Hackery to cope with old compiler
   652 		$main="MARM";
   653 		$euserdir="MARM";
   654 		$elocldir="MARM";
   655 	}
   656 	else {
   657 		$main=$inst;
   658 		if ($main eq "THUMB") {
   659 			$euserdir="ARMI";
   660 		} else {
   661 			$euserdir=$main;
   662 		}
   663 		if ($main eq "ARMI" or $main eq "THUMB") {
   664 			$elocldir="ARM4";
   665 		} else {
   666 			$elocldir=$main;
   667 		}
   668 	}
   669 	$kmain = $opts{'xabi'};
   670 	$kmain = $main unless ($kmain);
   671 	if (IsSmp($kmain)) {
   672 		$euserdir = $kmain;
   673 	}
   674 	if ($opts{name}) {
   675 		$romname=$opts{name};
   676 	} else {
   677 		$romname=uc($opts{variant}.$additional.$main);
   678 		if ($build=~/^\w*DEB$/i) {
   679 			$romname.='D';
   680 		}
   681 		$romname.='.IMG';
   682 	}
   683 }
   684 
   685 sub lookupFileInfo($$)
   686 {
   687 	my ($infile, $fullname) = @_;
   688 
   689 	my ($name, $ext) = $fullname =~ /^(.+)\.(\w+)$/ ? ($1, $2) : ($fullname, undef);
   690 
   691 	open TMP, $infile or die("Can't open $infile\n");
   692 	while(<TMP>)
   693 	{
   694 		$_ = lc;
   695 		if(/^\s*(\S+)\s*=\s*(\S+)\s+(\S+)/i)
   696 		{
   697 			my ($src, $dest) = ($2, $3);
   698 
   699 			my $destFullname = $dest =~ /^.*\\(.+)$/ ? $1 : $dest;
   700 			my ($destName, $destExt) = $destFullname =~ /^(.+?)\.(\w+)$/ ? ($1, $2) : ($destFullname, undef);
   701 
   702 			if ($destName eq $name && (!$ext || $ext eq $destExt))
   703 			{
   704 				close TMP;
   705 				return ($src, $dest);
   706 			}
   707 		}
   708 	}
   709 
   710 	die "patchdata: Can't find file $fullname\n";
   711 }
   712 
   713 sub lookupSymbolInfo($$)
   714 {
   715 	my ($file, $name) = @_;
   716 
   717 	open TMP, $file or die "Can't read $file\n";
   718 
   719 	# ignore local symbols.
   720 	while (<TMP> !~ /Global Symbols/) { }
   721 
   722 	while (<TMP>)
   723 	{
   724 		if (/^\s*(\S+)\s+(\S+)\s+data\s+(\S+)/i)
   725 		{
   726 			my ($symbol, $addr, $size) = ($1, $2, $3);
   727 			if ($symbol eq $name)
   728 			{
   729 				close TMP;
   730 				return ($addr, $size);
   731 			}
   732 		}
   733 
   734 		# This is a quick fix for RVCT 3.1, which uses the text "(EXPORTED)"
   735 		# in the map file. Here is an example:
   736 		#
   737 		# KHeapMinCellSize (EXPORTED) 0x0003d81c Data 4 mem.o(.constdata)
   738 		#
   739 		elsif (/^\s*(\S+)\s+\(exported\)\s+(\S+)\s+data\s+(\S+)/i)
   740 		{
   741 			my ($symbol, $addr, $size) = ($1, $2, $3);
   742 			if ($symbol eq $name)
   743 			{
   744 				close TMP;
   745 				return ($addr, $size);
   746 			}
   747 		}
   748 	}
   749 
   750 	die "patchdata: Can't find symbol $name\n";
   751 }
   752 
   753 sub parsePatchData($$)
   754 {
   755 	my ($infile, $outfile) = @_;
   756 
   757 	open IN, $infile or die("Can't read $infile\n");
   758 	open OUT, ">$outfile" or die("Can't write $outfile\n");
   759 
   760 	my $line;
   761 	while($line = <IN>)
   762 	{
   763 		if ($line =~ /^\s*patchdata\s+(.+?)\s*$/i)
   764 		{
   765 			if ($1 !~ /(\S+)\s*@\s*(\S+)\s+(\S+)\s*$/)
   766 			{
   767 				die "Bad patchdata command: $line\n";
   768 			}
   769 
   770 			my ($file, $symbol, $value) = (lc $1, $2, $3);
   771 			my ($srcFile, $destFile) = lookupFileInfo($infile, $file);
   772 			my ($index, $elementSize) = (undef, undef);
   773 			if ($symbol =~ s/:(\d+)\[(\d+)\]$//)
   774 			{
   775 				($index, $elementSize) = ($2, $1);
   776 				$index = hex($index) if $index =~ /^0x/i;
   777 			}
   778 
   779 			if ($srcFile =~ /\\armv5(smp)?\\/i)
   780 			{
   781 				my ($symbolAddr, $symbolSize) = lookupSymbolInfo("$srcFile.map", $symbol);
   782 
   783 				my $max;
   784 				if (defined($index))
   785 				{
   786 					my $bytes;
   787 					$bytes = 1, $max = 0xff       if $elementSize ==  8;
   788 					$bytes = 2, $max = 0xffff     if $elementSize == 16;
   789 					$bytes = 4, $max = 0xffffffff if $elementSize == 32;
   790 					die("patchdata: invalid element size $elementSize: $line\n") unless defined($bytes);
   791 
   792 					if ($bytes > 1 && (($symbolSize & ($bytes-1)) != 0))
   793 					{
   794 						die("patchdata: unexpected symbol size $symbolSize for array $symbol ($elementSize-bit elements)\n");
   795 					}
   796 
   797 					if ($index >= int($symbolSize / $bytes))
   798 					{
   799 						die("patchdata: index $index out of bounds for $symbol of $symbolSize bytes ($elementSize-bit elements)\n");
   800 					}
   801 
   802 					$symbolAddr = hex($symbolAddr) if $symbolAddr =~ /^0x/i;
   803 					$symbolAddr += $index * $bytes;
   804 					$symbolAddr = sprintf("0x%x", $symbolAddr);
   805 
   806 					$symbolSize = $bytes;
   807 				}
   808 				elsif ($symbolSize == 1) { $max = 0xff; }
   809 				elsif ($symbolSize == 2) { $max = 0xffff; }
   810 				elsif ($symbolSize == 4) { $max = 0xffffffff; }
   811 				else { die "patchdata: Unexpected symbol size $symbolSize for $symbol\n"; }
   812 
   813 				$value = hex($value) if $value =~ /^0x/i;
   814 				if ($value > $max)
   815 				{
   816 					print("Warning:  Value overflow of $symbol\n");
   817 					$value &= $max;
   818 				}					
   819 				$value = sprintf("0x%08x", $value);
   820 
   821 				$line = "patchdata $destFile addr $symbolAddr $symbolSize $value\n";
   822 			}
   823 			else
   824 			{
   825 				$line = "";
   826 			}
   827 
   828 		}
   829 
   830 		print OUT $line;
   831 	}
   832 
   833 	close IN;
   834 	close OUT;
   835 }
   836 
   837 sub genfile {
   838 	my $count=0;
   839 	if($_[0] eq 'paged') {
   840 		my $file='gentestpaged.txt';
   841 		unlink $file;
   842 		open(OUTFILE, ">$file") or die "Can't open output file, $!";
   843 		for(my $i=0;$i<50000;++$i) {
   844 			if(($i >5) && ($i % 40 ==0)) {
   845 			print OUTFILE "\n";
   846 			$count++;
   847 			} 
   848 			if(($i+$count) % 5 ==0) {
   849 			print OUTFILE "SATOR ";
   850 			}
   851 			if(($i+$count) % 5 ==1) {
   852 			print OUTFILE "AREPO ";
   853 			}
   854 			if(($i+$count) % 5 ==2) {
   855 			print OUTFILE "TENET ";
   856 			}
   857 			if(($i+$count) % 5 ==3) {
   858 			print OUTFILE "OPERA ";
   859 			}
   860 			if(($i+$count) % 5 ==4) {
   861 			print OUTFILE "ROTAS ";
   862 			}
   863 		}
   864 	} else {
   865 		my $file='gentestnonpaged.txt';
   866 		unlink $file;
   867 		open(OUTFILE, ">$file") or die "Can't open output file, $!";
   868 		for(my $i=0;$i<20000;++$i) {
   869 			if(($i >5) && ($i % 40 ==0)) {
   870 			print OUTFILE "\n";
   871 			$count++;
   872 			} 
   873 			if(($i+$count) % 4 ==0) {
   874 			print OUTFILE "STEP ";
   875 			}
   876 			if(($i+$count) % 4 ==1) {
   877 			print OUTFILE "TIME ";
   878 			}
   879 			if(($i+$count) % 4 ==2) {
   880 			print OUTFILE "EMIT ";
   881 			}
   882 			if(($i+$count) % 4 ==3) {
   883 			print OUTFILE "PETS ";
   884 			}
   885 		}
   886 	}
   887 }
   888 
   889 __END__
   890 
   891 # Tell emacs that this is a perl script even 'though it has a .bat extension
   892 # Local Variables:
   893 # mode:perl
   894 # tab-width:4
   895 # End:
   896