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