secureswitools/swisistools/test/tinterpretsis/preparesis.pl
changeset 0 ba25891c3a9e
equal deleted inserted replaced
-1:000000000000 0:ba25891c3a9e
       
     1 #
       
     2 # Copyright (c) 2006-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 #
       
    16 
       
    17 use Cwd;
       
    18 use strict;
       
    19 use Getopt::Std;
       
    20 use File::Basename;
       
    21 use File::Copy;     # for future portability
       
    22 
       
    23 # Note that although Win32 internally will quite happily accept
       
    24 # forward slashes as directory separators, both cmd.exe and
       
    25 # makesis.exe/signsis.exe interpret these as option separators,
       
    26 # so use backslashes when calling these programs.
       
    27 
       
    28 die "EPOCROOT not defined" if !defined ($ENV{EPOCROOT});
       
    29 my $makesis = "$ENV{EPOCROOT}epoc32\\tools\\makesis.exe";
       
    30 my $signsis = "$ENV{EPOCROOT}epoc32\\tools\\signsis.exe";
       
    31 if ( ! -x $makesis || ! -x $signsis ) {
       
    32    die "$makesis and $signsis are not executable";
       
    33 }
       
    34 
       
    35 my %opt;
       
    36 getopts( "vch:", \%opt ) or usage();
       
    37 
       
    38 my $platform = lc($ARGV[0]);
       
    39 my $configuration = lc($ARGV[1]);
       
    40 my $packagedir = "$ENV{EPOCROOT}epoc32\\winscw\\c\\tswi\\tinterpretsis\\pkg";
       
    41 my $builddir = "$ENV{EPOCROOT}epoc32\\winscw\\c\\tswi\\tinterpretsis\\sis_${platform}_${configuration}";
       
    42 my $log = "$ENV{EPOCROOT}epoc32\\winscw\\c\\tswi\\tinterpretsis\\logfile.txt";
       
    43 
       
    44 usage() if $opt{h};
       
    45 clean() if $opt{c};
       
    46 usage() if !defined($ARGV[0]) || !defined($ARGV[1]);
       
    47 
       
    48 # --------------------------------------------------------------------------
       
    49 
       
    50 sub usage() {
       
    51     print STDERR << "EOF";
       
    52     usage: $0 [-hcv] <platform> <udeb|urel>
       
    53      -h        : this (help) message
       
    54      -v        : verbose output
       
    55      -c        : clean up signed .sis files
       
    56 EOF
       
    57    exit;
       
    58 }
       
    59 
       
    60 # --------------------------------------------------------------------------
       
    61 
       
    62 sub clean() {
       
    63    foreach my $file (getFiles($builddir, "\.pkg\$")) { unlink("$builddir/$file"); }
       
    64    foreach my $file (getFiles($builddir, "\.sis\$")) { unlink("$builddir/$file"); }
       
    65    exit;
       
    66 }
       
    67 
       
    68 # --------------------------------------------------------------------------
       
    69 
       
    70 sub mtime($) {
       
    71     my ($file) = shift;
       
    72     my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,
       
    73       $size,$atime,$mtime,$ctime,$blksize,$blocks) = stat($file);
       
    74 
       
    75     $mtime = 0 if ! defined( $mtime );
       
    76     return $mtime;
       
    77 }
       
    78 
       
    79 # --------------------------------------------------------------------------
       
    80 
       
    81 # Return an array of files matching a regexp in a directory
       
    82 sub getFiles($$) {
       
    83     my $dir = shift;
       
    84     my $regfiles = shift;
       
    85     my @files;
       
    86 
       
    87     if ( opendir DIR, $dir ) {
       
    88        @files = grep (/$regfiles/, readdir(DIR));
       
    89        closedir DIR;
       
    90     }
       
    91     return @files;
       
    92 }
       
    93 
       
    94 # --------------------------------------------------------------------------
       
    95 
       
    96 # Edit a file
       
    97 sub EditFile ($$) {
       
    98 	my ($file, $coderef) = @_;
       
    99 	local $_ = ReadFile($file);
       
   100 	&$coderef();
       
   101 	WriteFile($file, $_);
       
   102 }
       
   103 
       
   104 # --------------------------------------------------------------------------
       
   105 
       
   106 # Read the contents of a file into a string and return it
       
   107 sub ReadFile ($) {
       
   108 	my ($file) = @_;
       
   109 	open FILE, "<$file" or die "Can't read from $file: $!";
       
   110 	local $/ = undef;
       
   111 	my $data = <FILE>;
       
   112 	close FILE;
       
   113 	return $data;
       
   114 }
       
   115 
       
   116 # --------------------------------------------------------------------------
       
   117 
       
   118 # Replace a file with a string
       
   119 sub WriteFile ($$) {
       
   120 	my ($file, $data) = @_;
       
   121 
       
   122 	# Ensure directory exists
       
   123 	if ($file =~ basename($1) && ! -e $1) {
       
   124 		mkdir($1);
       
   125 	}
       
   126 
       
   127 	system("attrib -r $file");
       
   128 	open FILE, ">$file" or die "Can't write to $file: $!";
       
   129 	print FILE $data;
       
   130 	close FILE;
       
   131 }
       
   132 
       
   133 # --------------------------------------------------------------------------
       
   134 
       
   135 print "\nBuilding test executables for $platform $configuration configuration\n\n";
       
   136 
       
   137 if ($platform =~ /armv5/i)
       
   138 {
       
   139 	mkdir("/epoc32/winscw/c/tswi/tinterpretsis/data/armv5/");
       
   140 }
       
   141 
       
   142 mkdir($builddir);
       
   143 
       
   144 
       
   145 print "Processing files in directory: $builddir\n\n";
       
   146 
       
   147 foreach my $file ( getFiles( $packagedir, "\.pkg\$" )) {
       
   148 	
       
   149 	# Copy package files to builddir if not present or package more recent.
       
   150 	# Some package files are re-written with platform, config and builddir.
       
   151 	#
       
   152 	if ( mtime("$packagedir/$file") > mtime("$builddir/$file") )
       
   153 	{
       
   154 		print "Copying $packagedir/$file to $builddir/$file and re-writing\n" if $opt{v};
       
   155 		copy("$packagedir/$file", "$builddir/$file");
       
   156 
       
   157 		# replace <PLATFORM> with the platform and <CFG> with the configuration
       
   158 		EditFile("$builddir/$file" , sub { s/<PLATFORM>/$platform/gm;
       
   159 						   s/<CONFIGURATION>/$configuration/gm;
       
   160 						   s/<BUILD>/$builddir/gm; });
       
   161 	}
       
   162 	# Run makesis/signsis if needed on package files
       
   163 	#
       
   164 	$file =~ s/\.pkg//;
       
   165 	if (    mtime("$builddir/$file.sis") < mtime("$builddir/$file.pkg")
       
   166 	     || mtime("$builddir/$file.sis") < mtime("$ENV{EPOCROOT}epoc32\\winscw\\c\\tswi\\tinterpretsis\\certs\\default.cer")
       
   167 	     || mtime("$builddir/$file.sis") < mtime("$ENV{EPOCROOT}epoc32\\winscw\\c\\tswi\\tinterpretsis\\certs\\default.key")
       
   168 	     || mtime("$builddir/$file.sis") < mtime($makesis)
       
   169 	     || mtime("$builddir/$file.sis") < mtime($signsis))
       
   170 	{
       
   171 		# build sis file
       
   172 		system ("$makesis $builddir\\$file.pkg $builddir\\$file-tmp.sis");
       
   173 		print ("$makesis $builddir\\$file.pkg $builddir\\$file-tmp.sis\n") if $opt{v};
       
   174 		system("$signsis -s $builddir\\$file-tmp.sis $builddir\\$file.sis $ENV{EPOCROOT}epoc32\\winscw\\c\\tswi\\tinterpretsis\\certs\\default.cer $ENV{EPOCROOT}epoc32\\winscw\\c\\tswi\\tinterpretsis\\certs\\default.key");
       
   175 		print ("$signsis -s $builddir\\$file-tmp.sis $builddir\\$file.sis $ENV{EPOCROOT}epoc32\\winscw\\c\\tswi\\tinterpretsis\\certs\\default.cer $ENV{EPOCROOT}epoc32\\winscw\\c\\tswi\\tinterpretsis\\certs\\default.key\n") if $opt{v};
       
   176 		print "\n" if $opt{v};
       
   177 	} else {
       
   178 		print ("$builddir/$file.sis is up-to-date\n");
       
   179 	}
       
   180 
       
   181 	# If the package file contains the string "CREATE_UNSIGNED_SIS"
       
   182 	# copy the unsigned sis file too. Note that these SIS files are
       
   183 	# not currently added to the ROM, since we can't change the
       
   184 	# swipolicy file on the fly on the ROM.
       
   185 
       
   186 	if (grep(/CREATE_UNSIGNED_SIS/, ReadFile("$builddir/$file.pkg")))
       
   187 	{
       
   188 		rename("$builddir/$file-tmp.sis", "$builddir/$file-unsigned.sis");
       
   189 	} else {
       
   190 		unlink("$builddir/$file-tmp.sis");
       
   191 	}
       
   192 
       
   193 	
       
   194 }
       
   195 
       
   196 
       
   197 # Keep original method names for ease of merging/compatibility
       
   198 sub SignFile($$$$)  { SignFileLen(1, @_); }
       
   199 sub SignFile2($$$$) { SignFileLen(2, @_); }
       
   200 
       
   201 sub SignFileLen($$$$$)
       
   202 {
       
   203 	my ($len, $infile, $outfile, $signwith, $iby) = @_;
       
   204 	my $certpath="$ENV{'SECURITYSOURCEDIR'}\\installationservices\\switestfw\\testcertificates\\swi\\test\\tsisfile\\data\\signedsis";
       
   205 	my $keypath = "$ENV{'SECURITYSOURCEDIR'}\\installationservices\\swi\\test\\tsisfile\\data\\signedsis";
       
   206 	my $sispath=$builddir;
       
   207 
       
   208 
       
   209 	my $type="rsa";
       
   210 	if ($signwith =~ /DSA/i) {
       
   211 		$type="dsa";
       
   212 	}
       
   213 
       
   214 	my $pemfile = "$certpath\\$signwith\\cert_chain_$type\_len$len\\chain_$type\_len$len.cert.pem ";
       
   215 	my $keyfile = "$keypath\\$signwith\\cert_chain_$type\_len$len\\" . $type .  "$len.key";
       
   216 
       
   217 	# If infile, keyfile, pemfile or signsis.exe is more recent than
       
   218 	# outfile, or outfile is non-existent then sign the sis file.
       
   219 	#
       
   220 	if (   mtime("$sispath/$outfile") < mtime("$sispath/$infile")
       
   221 	    || mtime("$sispath/$outfile") < mtime("$signsis")
       
   222 	    || mtime("$sispath/$outfile") < mtime("$pemfile")
       
   223 	    || mtime("$sispath/$outfile") < mtime("$keyfile")) {
       
   224 		printf("\t$infile with $signwith to $outfile\n");
       
   225 		my $cmd = "$signsis -s $sispath\\$infile $sispath\\$outfile $pemfile $keyfile";
       
   226 		system($cmd);
       
   227 		print "$cmd\n\n" if $opt{v};
       
   228 	} else {
       
   229 		print("$sispath/$outfile is up-to-date\n");
       
   230 	}
       
   231 }
       
   232 
       
   233 
       
   234 sub SignFileWithSpecifiedCert($$$$$)
       
   235 {
       
   236 
       
   237 	my ($infile, $outfile, $signcert, $signkey, $iby) = @_;
       
   238 	my $certpath="$ENV{'SECURITYSOURCEDIR'}\\installationservices\\switestfw\\testcertificates\\swi\\test\\tsisfile\\data\\signedsis";
       
   239  	my $keypath = "$ENV{'SECURITYSOURCEDIR'}\\installationservices\\swi\\test\\tsisfile\\data\\signedsis";
       
   240 
       
   241 	if ($signkey =~ /\.pem$/)
       
   242 	{
       
   243 		$keypath = "$ENV{'SECURITYSOURCEDIR'}\\installationservices\\switestfw\\testcertificates\\swi\\test\\tsisfile\\data\\signedsis";
       
   244 	}
       
   245 	 
       
   246  	my $sispath=$builddir;
       
   247 
       
   248 	if (   mtime("$sispath/$outfile") < mtime("$sispath/$infile")
       
   249 	    || mtime("$sispath/$outfile") < mtime("$signsis")
       
   250 	    || mtime("$sispath/$outfile") < mtime("$keypath/$signkey")
       
   251 	    || mtime("$sispath/$outfile") < mtime("$certpath/$signcert")) {
       
   252 		printf("\t$infile with $signcert and $signkey to $outfile\n");
       
   253 		my $cmd = "$signsis -s $sispath\\$infile $sispath\\$outfile $certpath\\$signcert $keypath\\$signkey";
       
   254 		system($cmd);
       
   255 		print "$cmd\n\n" if $opt{v};
       
   256 	} else {
       
   257 		print("$sispath/$outfile is up-to-date\n");
       
   258 	}
       
   259 }
       
   260 
       
   261 
       
   262 #
       
   263 # Sign the files as necessary for the following tests
       
   264 #
       
   265 
       
   266 printf("Signing files...\n");
       
   267 SignFile("exe.sis", "exe_s.sis", "Root5CA",\*iby);
       
   268 SignFile("dll_exe_dup_sid.sis", "dll_exe_dup_sid_s.sis", "Root5CA",\*iby);
       
   269 SignFile("dll_dll_dup_sid.sis", "dll_dll_dup_sid_s.sis", "Root5CA",\*iby);
       
   270 SignFile("eclipse.sis", "eclipse_s.sis", "Root5CA",\*iby);
       
   271 SignFile("partial.sis", "partial_s.sis", "Root5CA",\*iby);
       
   272 SignFile("patch.sis", "patch_s.sis", "Root5CA",\*iby);
       
   273 SignFile("overwrite.sis", "overwrite_s.sis", "Root5CA",\*iby);
       
   274 SignFile("private_fail.sis", "private_fail_s.sis", "Root5CA",\*iby);
       
   275 SignFile("duplicate_sid.sis", "duplicate_sid_s.sis", "Root5CA",\*iby);
       
   276 SignFile("depend1.sis", "depend1_s.sis", "Root5CA",\*iby);
       
   277 SignFile("depend2.sis", "depend2_s.sis", "Root5CA",\*iby);
       
   278 SignFile("depend3.sis", "depend3_s.sis", "Root5CA",\*iby);
       
   279 SignFile("depend4.sis", "depend4_s.sis", "Root5CA",\*iby);
       
   280 SignFile("depend5.sis", "depend5_s.sis", "Root5CA",\*iby);
       
   281 SignFile("depend6.sis", "depend6_s.sis", "Root5CA",\*iby);
       
   282 SignFile("functions_base.sis", "functions_base_s.sis", "Root5CA",\*iby);
       
   283 SignFile("functions.sis", "functions_s.sis", "Root5CA",\*iby);
       
   284 SignFile("stub_file_depend.sis", "stub_file_depend_s.sis", "Root5CA",\*iby);
       
   285 SignFile("fn_flag.sis", "fn_flag_s.sis", "Root5CA",\*iby);
       
   286 SignFile("testconfig.sis", "testconfig_s.sis", "Root5CA",\*iby);
       
   287 
       
   288 # DEF070572 - SIS files signed by multiple chains to test OCSP result dialog
       
   289 #SignFileWithSpecifiedCert("tswinocapability_Root5.sis", "testocspresultdialog_a.sis", "SymbianTestRootExpiredCARSA\\cacert.pem", "SymbianTestRootExpiredCARSA\\cakey.pem", \*iby);
       
   290 #SignFileWithSpecifiedCert("testocspresultdialog_a.sis", "testocspresultdialog_b.sis", "..\\..\\..\\tdevcerts\\SymbianTestRootCARSA_OCSP\\SymbianTestRootCARSA\\certs\\revoked_user_caps.cert.pem", "..\\..\\..\\tdevcerts\\SymbianTestRootCARSA_OCSP\\SymbianTestRootCARSA\\certs\\revoked_user_caps.key.pem", \*iby);
       
   291 
       
   292 printf("\n");
       
   293