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