]> git.proxmox.com Git - mirror_edk2.git/blob - CryptoPkg/Library/OpensslLib/process_files.pl
545f2182842b90efe2bfa0e52e8fc3f8a6e1085e
[mirror_edk2.git] / CryptoPkg / Library / OpensslLib / process_files.pl
1 #!/usr/bin/perl -w
2 #
3 # This script runs the OpenSSL Configure script, then processes the
4 # resulting file list into our local OpensslLib[Crypto].inf and also
5 # takes copies of opensslconf.h and dso_conf.h.
6 #
7 # This only needs to be done once by a developer when updating to a
8 # new version of OpenSSL (or changing options, etc.). Normal users
9 # do not need to do this, since the results are stored in the EDK2
10 # git repository for them.
11 #
12 # Due to the script wrapping required to process the OpenSSL
13 # configuration data, each native architecture must be processed
14 # individually by the maintainer (in addition to the standard version):
15 # ./process_files.pl
16 # ./process_files.pl X64
17 # ./process_files.pl [Arch]
18
19 use strict;
20 use Cwd;
21 use File::Copy;
22 use File::Basename;
23 use File::Path qw(make_path remove_tree);
24 use Text::Tabs;
25
26 my $comment_character;
27
28 #
29 # OpenSSL perlasm generator script does not transfer the copyright header
30 #
31 sub copy_license_header
32 {
33 my @args = split / /, shift; #Separate args by spaces
34 my $source = $args[1]; #Source file is second (after "perl")
35 my $target = pop @args; #Target file is always last
36 chop ($target); #Remove newline char
37
38 my $temp_file_name = "license.tmp";
39 open (my $source_file, "<" . $source) || die $source;
40 open (my $target_file, "<" . $target) || die $target;
41 open (my $temp_file, ">" . $temp_file_name) || die $temp_file_name;
42
43 #Add "generated file" warning
44 $source =~ s/^..//; #Remove leading "./"
45 print ($temp_file "$comment_character WARNING: do not edit!\r\n");
46 print ($temp_file "$comment_character Generated from $source\r\n");
47 print ($temp_file "$comment_character\r\n");
48
49 #Copy source file header to temp file
50 while (my $line = <$source_file>) {
51 next if ($line =~ /#!/); #Ignore shebang line
52 $line =~ s/#/$comment_character/; #Fix comment character for assembly
53 $line =~ s/\s+$/\r\n/; #Trim trailing whitepsace, fixup line endings
54 print ($temp_file $line);
55 last if ($line =~ /http/); #Last line of copyright header contains a web link
56 }
57 print ($temp_file "\r\n");
58 #Retrieve generated assembly contents
59 while (my $line = <$target_file>) {
60 $line =~ s/\s+$/\r\n/; #Trim trailing whitepsace, fixup line endings
61 print ($temp_file expand ($line)); #expand() replaces tabs with spaces
62 }
63
64 close ($source_file);
65 close ($target_file);
66 close ($temp_file);
67
68 move ($temp_file_name, $target) ||
69 die "Cannot replace \"" . $target . "\"!";
70 }
71
72 #
73 # Find the openssl directory name for use lib. We have to do this
74 # inside of BEGIN. The variables we create here, however, don't seem
75 # to be available to the main script, so we have to repeat the
76 # exercise.
77 #
78 my $inf_file;
79 my $OPENSSL_PATH;
80 my $uefi_config;
81 my $extension;
82 my $arch;
83 my @inf;
84 #
85 # Use PCD to conditionally enable certain openssl features.
86 # $conditional_feature contains pcd_name:fetures_names pairs
87 # of conditional features.
88 # @conditional_feature_dir contains relative_path:pcd_name pairs
89 # of conditional features in openssl, MUST correspond to the content
90 # in $conditional_feature.
91 #
92 # Configure list [openssl_configuration : new_define_list : new_file_list : pcd]
93 # 1. no-ec : {NO_EC, NO_ECDH, NO_ECDSA, NO_TLS1_3, NO_SM2} : {/ec/, /sm2/} : PcdOpensslEcEnabled
94 #
95 my %conditional_feature = ("PcdOpensslEcEnabled"=>["EC", "ECDH", "ECDSA", "TLS1_3", "SM2"]);
96 my %conditional_feature_dir = ("/ec/"=>"PcdOpensslEcEnabled", "/sm2/"=>"PcdOpensslEcEnabled");
97
98 BEGIN {
99 $inf_file = "OpensslLib.inf";
100 $uefi_config = "UEFI";
101 $arch = shift;
102
103 if (defined $arch) {
104 if (uc ($arch) eq "X64") {
105 $arch = "X64";
106 $inf_file = "OpensslLibX64.inf";
107 $uefi_config = "UEFI-x86_64";
108 $extension = "nasm";
109 $comment_character = ";";
110 } elsif (uc ($arch) eq "X64GCC") {
111 $arch = "X64Gcc";
112 $inf_file = "OpensslLibX64Gcc.inf";
113 $uefi_config = "UEFI-x86_64-GCC";
114 $extension = "S";
115 $comment_character = "#";
116 } else {
117 die "Unsupported architecture \"" . $arch . "\"!";
118 }
119 if ($extension eq "nasm") {
120 if (`nasm -v 2>&1`) {
121 #Presence of nasm executable will trigger inclusion of AVX instructions
122 die "\nCannot run assembly generators with NASM in path!\n\n";
123 }
124 }
125
126 # Prepare assembly folder
127 if (-d $arch) {
128 opendir my $dir, $arch ||
129 die "Cannot open assembly folder \"" . $arch . "\"!";
130 while (defined (my $file = readdir $dir)) {
131 if (-d "$arch/$file") {
132 next if $file eq ".";
133 next if $file eq "..";
134 remove_tree ("$arch/$file", {safe => 1}) ||
135 die "Cannot clean assembly folder \"" . "$arch/$file" . "\"!";
136 }
137 }
138
139 } else {
140 mkdir $arch ||
141 die "Cannot create assembly folder \"" . $arch . "\"!";
142 }
143 }
144
145 # Read the contents of the inf file
146 open( FD, "<" . $inf_file ) ||
147 die "Cannot open \"" . $inf_file . "\"!";
148 @inf = (<FD>);
149 close(FD) ||
150 die "Cannot close \"" . $inf_file . "\"!";
151
152 foreach (@inf) {
153 if (/DEFINE\s+OPENSSL_PATH\s*=\s*([a-z]+)/) {
154
155 # We need to run Configure before we can include its result...
156 $OPENSSL_PATH = $1;
157
158 my $basedir = getcwd();
159
160 chdir($OPENSSL_PATH) ||
161 die "Cannot change to OpenSSL directory \"" . $OPENSSL_PATH . "\"";
162
163 # Configure UEFI
164 system(
165 "./Configure",
166 "--config=../UefiAsm.conf",
167 "$uefi_config",
168 "no-afalgeng",
169 "no-async",
170 "no-autoerrinit",
171 "no-autoload-config",
172 "no-bf",
173 "no-blake2",
174 "no-camellia",
175 "no-capieng",
176 "no-cast",
177 "no-chacha",
178 "no-cms",
179 "no-ct",
180 "no-deprecated",
181 "no-des",
182 "no-dgram",
183 "no-dsa",
184 "no-dynamic-engine",
185 "no-ec2m",
186 "no-engine",
187 "no-err",
188 "no-filenames",
189 "no-gost",
190 "no-hw",
191 "no-idea",
192 "no-md4",
193 "no-mdc2",
194 "no-pic",
195 "no-ocb",
196 "no-poly1305",
197 "no-posix-io",
198 "no-rc2",
199 "no-rc4",
200 "no-rfc3779",
201 "no-rmd160",
202 "no-scrypt",
203 "no-seed",
204 "no-sock",
205 "no-srp",
206 "no-ssl",
207 "no-stdio",
208 "no-threads",
209 "no-ts",
210 "no-ui",
211 "no-whirlpool",
212 # OpenSSL1_1_1b doesn't support default rand-seed-os for UEFI
213 # UEFI only support --with-rand-seed=none
214 "--with-rand-seed=none"
215 ) == 0 ||
216 die "OpenSSL Configure failed!\n";
217
218 # Generate opensslconf.h per config data
219 system(
220 "perl -I. -Mconfigdata util/dofile.pl " .
221 "include/openssl/opensslconf.h.in " .
222 "> include/openssl/opensslconf.h"
223 ) == 0 ||
224 die "Failed to generate opensslconf.h!\n";
225
226 # Generate dso_conf.h per config data
227 system(
228 "perl -I. -Mconfigdata util/dofile.pl " .
229 "include/crypto/dso_conf.h.in " .
230 "> include/crypto/dso_conf.h"
231 ) == 0 ||
232 die "Failed to generate dso_conf.h!\n";
233
234 chdir($basedir) ||
235 die "Cannot change to base directory \"" . $basedir . "\"";
236
237 push @INC, $1;
238 last;
239 }
240 }
241 }
242
243 #
244 # Retrieve file lists from OpenSSL configdata
245 #
246 use configdata qw/%unified_info/;
247 use configdata qw/%config/;
248 use configdata qw/%target/;
249
250 #
251 # Collect build flags from configdata
252 #
253 my $flags = "";
254 foreach my $f (@{$config{lib_defines}}) {
255 $flags .= " -D$f";
256 }
257
258 my @cryptofilelist = ();
259 my @sslfilelist = ();
260 my @asmfilelist = ();
261 my @asmbuild = ();
262 foreach my $product ((@{$unified_info{libraries}},
263 @{$unified_info{engines}})) {
264 foreach my $o (@{$unified_info{sources}->{$product}}) {
265 foreach my $s (@{$unified_info{sources}->{$o}}) {
266 # No need to add unused files in UEFI.
267 # So it can reduce porting time, compile time, library size.
268 next if $s =~ "crypto/bio/b_print.c";
269 next if $s =~ "crypto/rand/randfile.c";
270 next if $s =~ "crypto/store/";
271 next if $s =~ "crypto/err/err_all.c";
272 next if $s =~ "crypto/aes/aes_ecb.c";
273
274 if ($unified_info{generate}->{$s}) {
275 if (defined $arch) {
276 my $buildstring = "perl";
277 foreach my $arg (@{$unified_info{generate}->{$s}}) {
278 if ($arg =~ ".pl") {
279 $buildstring .= " ./openssl/$arg";
280 } elsif ($arg =~ "PERLASM_SCHEME") {
281 $buildstring .= " $target{perlasm_scheme}";
282 } elsif ($arg =~ "LIB_CFLAGS") {
283 $buildstring .= "$flags";
284 }
285 }
286 ($s, my $path, undef) = fileparse($s, qr/\.[^.]*/);
287 $buildstring .= " ./$arch/$path$s.$extension";
288 make_path ("./$arch/$path");
289 push @asmbuild, "$buildstring\n";
290 push @asmfilelist, " $arch/$path$s.$extension\r\n";
291 }
292 next;
293 }
294 if ($product =~ "libssl") {
295 push @sslfilelist, ' $(OPENSSL_PATH)/' . $s . "\r\n";
296 next;
297 }
298 push @cryptofilelist, ' $(OPENSSL_PATH)/' . $s;
299 foreach (keys(%conditional_feature_dir)) {
300 if ($s =~ $_) {
301 push @cryptofilelist, ' |*|*|*|gEfiCryptoPkgTokenSpaceGuid.' . $conditional_feature_dir{$_};
302 }
303 }
304 push @cryptofilelist, "\r\n";
305 }
306 }
307 }
308
309
310 #
311 # Update the perl script to generate the missing header files
312 #
313 my @dir_list = ();
314 for (sort keys %{$unified_info{dirinfo}}){
315 push @dir_list,$_;
316 }
317
318 my $dir = getcwd();
319 my @files = ();
320 my @headers = ();
321 chdir ("openssl");
322 foreach(@dir_list){
323 @files = glob($_."/*.h");
324 push @headers, @files;
325 }
326 chdir ($dir);
327
328 foreach (@headers){
329 if(/ssl/){
330 push @sslfilelist, ' $(OPENSSL_PATH)/' . $_ . "\r\n";
331 next;
332 }
333 push @cryptofilelist, ' $(OPENSSL_PATH)/' . $_;
334 foreach my $conditional_key (keys(%conditional_feature_dir)) {
335 if ($_ =~ $conditional_key) {
336 push @cryptofilelist, ' |*|*|*|gEfiCryptoPkgTokenSpaceGuid.' . $conditional_feature_dir{$conditional_key};
337 }
338 }
339 push @cryptofilelist, "\r\n";
340 }
341
342
343 #
344 # Generate assembly files
345 #
346 if (@asmbuild) {
347 print "\n--> Generating assembly files ... ";
348 foreach my $buildstring (@asmbuild) {
349 system ("$buildstring");
350 copy_license_header ($buildstring);
351 }
352 print "Done!";
353 }
354
355 #
356 # Update OpensslLib.inf with autogenerated file list
357 #
358 my @new_inf = ();
359 my $subbing = 0;
360 print "\n--> Updating $inf_file ... ";
361 foreach (@inf) {
362 if ($_ =~ "DEFINE OPENSSL_FLAGS_CONFIG") {
363 push @new_inf, " DEFINE OPENSSL_FLAGS_CONFIG =" . $flags . "\r\n";
364 next;
365 }
366 if ( $_ =~ "# Autogenerated files list starts here" ) {
367 push @new_inf, $_, @asmfilelist, @cryptofilelist, @sslfilelist;
368 $subbing = 1;
369 next;
370 }
371 if ( $_ =~ "# Autogenerated files list ends here" ) {
372 push @new_inf, $_;
373 $subbing = 0;
374 next;
375 }
376
377 push @new_inf, $_
378 unless ($subbing);
379 }
380
381 my $new_inf_file = $inf_file . ".new";
382 open( FD, ">" . $new_inf_file ) ||
383 die $new_inf_file;
384 print( FD @new_inf ) ||
385 die $new_inf_file;
386 close(FD) ||
387 die $new_inf_file;
388 rename( $new_inf_file, $inf_file ) ||
389 die "rename $inf_file";
390 print "Done!";
391
392 if (!defined $arch) {
393 #
394 # Update OpensslLibCrypto.inf with auto-generated file list (no libssl)
395 #
396 $inf_file = "OpensslLibCrypto.inf";
397
398 # Read the contents of the inf file
399 @inf = ();
400 @new_inf = ();
401 open( FD, "<" . $inf_file ) ||
402 die "Cannot open \"" . $inf_file . "\"!";
403 @inf = (<FD>);
404 close(FD) ||
405 die "Cannot close \"" . $inf_file . "\"!";
406
407 $subbing = 0;
408 print "\n--> Updating OpensslLibCrypto.inf ... ";
409 foreach (@inf) {
410 if ( $_ =~ "# Autogenerated files list starts here" ) {
411 push @new_inf, $_, @cryptofilelist;
412 $subbing = 1;
413 next;
414 }
415 if ( $_ =~ "# Autogenerated files list ends here" ) {
416 push @new_inf, $_;
417 $subbing = 0;
418 next;
419 }
420
421 push @new_inf, $_
422 unless ($subbing);
423 }
424
425 $new_inf_file = $inf_file . ".new";
426 open( FD, ">" . $new_inf_file ) ||
427 die $new_inf_file;
428 print( FD @new_inf ) ||
429 die $new_inf_file;
430 close(FD) ||
431 die $new_inf_file;
432 rename( $new_inf_file, $inf_file ) ||
433 die "rename $inf_file";
434 print "Done!";
435 }
436
437 #
438 # Copy opensslconf.h and dso_conf.h generated from OpenSSL Configuration
439 #
440 print "\n--> Duplicating opensslconf.h into Include/openssl ... ";
441 system(
442 "perl -pe 's/\\n/\\r\\n/' " .
443 "< " . $OPENSSL_PATH . "/include/openssl/opensslconf.h " .
444 "> " . $OPENSSL_PATH . "/../../Include/openssl/opensslconf_generated.h"
445 ) == 0 ||
446 die "Cannot copy opensslconf.h!";
447 print "Done!";
448
449 print "\n--> Duplicating dso_conf.h into Include/crypto ... ";
450 system(
451 "perl -pe 's/\\n/\\r\\n/' " .
452 "< " . $OPENSSL_PATH . "/include/crypto/dso_conf.h" .
453 "> " . $OPENSSL_PATH . "/../../Include/crypto/dso_conf.h"
454 ) == 0 ||
455 die "Cannot copy dso_conf.h!";
456 print "Done!";
457
458 #
459 # Add conditional feature to opensslconf.h
460 #
461 my $conf_file = "../Include/openssl/opensslconf.h";
462 my @conf_raw = ();
463 my @conditional_define = ();
464 print "\n--> Updating conditional feature in $conf_file ... ";
465
466 foreach my $pcd_name (keys(%conditional_feature)) {
467 push @conditional_define, "#if !FixedPcdGetBool ($pcd_name)\r\n";
468 foreach (@{$conditional_feature{$pcd_name}}) {
469 push @conditional_define, "# ifndef OPENSSL_NO_$_\r\n";
470 push @conditional_define, "# define OPENSSL_NO_$_\r\n";
471 push @conditional_define, "# endif\r\n";
472 }
473 push @conditional_define, "#endif\r\n";
474 }
475
476 open( FD, "<" . $conf_file ) ||
477 die $conf_file;
478 foreach (<FD>) {
479 # Insert conditional define to the begin of opensslconf.h
480 if ($_ =~ "Autogenerated conditional openssl feature list starts here") {
481 push @conf_raw, $_, @conditional_define;
482 $subbing = 1;
483 next;
484 }
485 if ($_ =~ "Autogenerated conditional openssl feature list ends here") {
486 push @conf_raw, $_;
487 $subbing = 0;
488 next;
489 }
490 push @conf_raw, $_
491 unless ($subbing);
492 }
493 close(FD) ||
494 die $conf_file;
495
496 open( FD, ">" . $conf_file ) ||
497 die $conf_file;
498 print( FD @conf_raw ) ||
499 die $conf_file;
500 close(FD) ||
501 die $conf_file;
502 print "Done!\n";
503
504 print "\nProcessing Files Done!\n";
505
506 exit(0);
507