18c2ecf20Sopenharmony_ci#!/usr/bin/env perl 28c2ecf20Sopenharmony_ci# SPDX-License-Identifier: GPL-2.0 38c2ecf20Sopenharmony_ci# 48c2ecf20Sopenharmony_ciuse warnings; 58c2ecf20Sopenharmony_ciuse strict; 68c2ecf20Sopenharmony_ciuse Math::BigInt; 78c2ecf20Sopenharmony_ciuse Fcntl "SEEK_SET"; 88c2ecf20Sopenharmony_ci 98c2ecf20Sopenharmony_cidie "Format: $0 [-s <systemmap-file>] <vmlinux-file> <keyring-file>\n" 108c2ecf20Sopenharmony_ci if ($#ARGV != 1 && $#ARGV != 3 || 118c2ecf20Sopenharmony_ci $#ARGV == 3 && $ARGV[0] ne "-s"); 128c2ecf20Sopenharmony_ci 138c2ecf20Sopenharmony_cimy $sysmap = ""; 148c2ecf20Sopenharmony_ciif ($#ARGV == 3) { 158c2ecf20Sopenharmony_ci shift; 168c2ecf20Sopenharmony_ci $sysmap = $ARGV[0]; 178c2ecf20Sopenharmony_ci shift; 188c2ecf20Sopenharmony_ci} 198c2ecf20Sopenharmony_ci 208c2ecf20Sopenharmony_cimy $vmlinux = $ARGV[0]; 218c2ecf20Sopenharmony_cimy $keyring = $ARGV[1]; 228c2ecf20Sopenharmony_ci 238c2ecf20Sopenharmony_ci# 248c2ecf20Sopenharmony_ci# Parse the vmlinux section table 258c2ecf20Sopenharmony_ci# 268c2ecf20Sopenharmony_ciopen FD, "objdump -h $vmlinux |" || die $vmlinux; 278c2ecf20Sopenharmony_cimy @lines = <FD>; 288c2ecf20Sopenharmony_ciclose(FD) || die $vmlinux; 298c2ecf20Sopenharmony_ci 308c2ecf20Sopenharmony_cimy @sections = (); 318c2ecf20Sopenharmony_ci 328c2ecf20Sopenharmony_ciforeach my $line (@lines) { 338c2ecf20Sopenharmony_ci chomp($line); 348c2ecf20Sopenharmony_ci if ($line =~ /\s*([0-9]+)\s+(\S+)\s+([0-9a-f]+)\s+([0-9a-f]+)\s+([0-9a-f]+)\s+([0-9a-f]+)\s+2[*][*]([0-9]+)/ 358c2ecf20Sopenharmony_ci ) { 368c2ecf20Sopenharmony_ci my $seg = $1; 378c2ecf20Sopenharmony_ci my $name = $2; 388c2ecf20Sopenharmony_ci my $len = Math::BigInt->new("0x" . $3); 398c2ecf20Sopenharmony_ci my $vma = Math::BigInt->new("0x" . $4); 408c2ecf20Sopenharmony_ci my $lma = Math::BigInt->new("0x" . $5); 418c2ecf20Sopenharmony_ci my $foff = Math::BigInt->new("0x" . $6); 428c2ecf20Sopenharmony_ci my $align = 2 ** $7; 438c2ecf20Sopenharmony_ci 448c2ecf20Sopenharmony_ci push @sections, { name => $name, 458c2ecf20Sopenharmony_ci vma => $vma, 468c2ecf20Sopenharmony_ci len => $len, 478c2ecf20Sopenharmony_ci foff => $foff }; 488c2ecf20Sopenharmony_ci } 498c2ecf20Sopenharmony_ci} 508c2ecf20Sopenharmony_ci 518c2ecf20Sopenharmony_ciprint "Have $#sections sections\n"; 528c2ecf20Sopenharmony_ci 538c2ecf20Sopenharmony_ci# 548c2ecf20Sopenharmony_ci# Try and parse the vmlinux symbol table. If the vmlinux file has been created 558c2ecf20Sopenharmony_ci# from a vmlinuz file with extract-vmlinux then the symbol table will be empty. 568c2ecf20Sopenharmony_ci# 578c2ecf20Sopenharmony_ciopen FD, "nm $vmlinux 2>/dev/null |" || die $vmlinux; 588c2ecf20Sopenharmony_ci@lines = <FD>; 598c2ecf20Sopenharmony_ciclose(FD) || die $vmlinux; 608c2ecf20Sopenharmony_ci 618c2ecf20Sopenharmony_cimy %symbols = (); 628c2ecf20Sopenharmony_cimy $nr_symbols = 0; 638c2ecf20Sopenharmony_ci 648c2ecf20Sopenharmony_cisub parse_symbols(@) { 658c2ecf20Sopenharmony_ci foreach my $line (@_) { 668c2ecf20Sopenharmony_ci chomp($line); 678c2ecf20Sopenharmony_ci if ($line =~ /([0-9a-f]+)\s([a-zA-Z])\s(\S+)/ 688c2ecf20Sopenharmony_ci ) { 698c2ecf20Sopenharmony_ci my $addr = "0x" . $1; 708c2ecf20Sopenharmony_ci my $type = $2; 718c2ecf20Sopenharmony_ci my $name = $3; 728c2ecf20Sopenharmony_ci 738c2ecf20Sopenharmony_ci $symbols{$name} = $addr; 748c2ecf20Sopenharmony_ci $nr_symbols++; 758c2ecf20Sopenharmony_ci } 768c2ecf20Sopenharmony_ci } 778c2ecf20Sopenharmony_ci} 788c2ecf20Sopenharmony_ciparse_symbols(@lines); 798c2ecf20Sopenharmony_ci 808c2ecf20Sopenharmony_ciif ($nr_symbols == 0 && $sysmap ne "") { 818c2ecf20Sopenharmony_ci print "No symbols in vmlinux, trying $sysmap\n"; 828c2ecf20Sopenharmony_ci 838c2ecf20Sopenharmony_ci open FD, "<$sysmap" || die $sysmap; 848c2ecf20Sopenharmony_ci @lines = <FD>; 858c2ecf20Sopenharmony_ci close(FD) || die $sysmap; 868c2ecf20Sopenharmony_ci parse_symbols(@lines); 878c2ecf20Sopenharmony_ci} 888c2ecf20Sopenharmony_ci 898c2ecf20Sopenharmony_cidie "No symbols available\n" 908c2ecf20Sopenharmony_ci if ($nr_symbols == 0); 918c2ecf20Sopenharmony_ci 928c2ecf20Sopenharmony_ciprint "Have $nr_symbols symbols\n"; 938c2ecf20Sopenharmony_ci 948c2ecf20Sopenharmony_cidie "Can't find system certificate list" 958c2ecf20Sopenharmony_ci unless (exists($symbols{"__cert_list_start"}) && 968c2ecf20Sopenharmony_ci exists($symbols{"system_certificate_list_size"})); 978c2ecf20Sopenharmony_ci 988c2ecf20Sopenharmony_cimy $start = Math::BigInt->new($symbols{"__cert_list_start"}); 998c2ecf20Sopenharmony_cimy $end; 1008c2ecf20Sopenharmony_cimy $size; 1018c2ecf20Sopenharmony_cimy $size_sym = Math::BigInt->new($symbols{"system_certificate_list_size"}); 1028c2ecf20Sopenharmony_ci 1038c2ecf20Sopenharmony_ciopen FD, "<$vmlinux" || die $vmlinux; 1048c2ecf20Sopenharmony_cibinmode(FD); 1058c2ecf20Sopenharmony_ci 1068c2ecf20Sopenharmony_cimy $s = undef; 1078c2ecf20Sopenharmony_ciforeach my $sec (@sections) { 1088c2ecf20Sopenharmony_ci my $s_name = $sec->{name}; 1098c2ecf20Sopenharmony_ci my $s_vma = $sec->{vma}; 1108c2ecf20Sopenharmony_ci my $s_len = $sec->{len}; 1118c2ecf20Sopenharmony_ci my $s_foff = $sec->{foff}; 1128c2ecf20Sopenharmony_ci my $s_vend = $s_vma + $s_len; 1138c2ecf20Sopenharmony_ci 1148c2ecf20Sopenharmony_ci next unless ($start >= $s_vma); 1158c2ecf20Sopenharmony_ci next if ($start >= $s_vend); 1168c2ecf20Sopenharmony_ci 1178c2ecf20Sopenharmony_ci die "Certificate list size was not found on the same section\n" 1188c2ecf20Sopenharmony_ci if ($size_sym < $s_vma || $size_sym > $s_vend); 1198c2ecf20Sopenharmony_ci 1208c2ecf20Sopenharmony_ci die "Cert object in multiple sections: ", $s_name, " and ", $s->{name}, "\n" 1218c2ecf20Sopenharmony_ci if ($s); 1228c2ecf20Sopenharmony_ci 1238c2ecf20Sopenharmony_ci my $size_off = $size_sym -$s_vma + $s_foff; 1248c2ecf20Sopenharmony_ci my $packed; 1258c2ecf20Sopenharmony_ci die $vmlinux if (!defined(sysseek(FD, $size_off, SEEK_SET))); 1268c2ecf20Sopenharmony_ci sysread(FD, $packed, 8); 1278c2ecf20Sopenharmony_ci $size = unpack 'L!', $packed; 1288c2ecf20Sopenharmony_ci $end = $start + $size; 1298c2ecf20Sopenharmony_ci 1308c2ecf20Sopenharmony_ci printf "Have %u bytes of certs at VMA 0x%x\n", $size, $start; 1318c2ecf20Sopenharmony_ci 1328c2ecf20Sopenharmony_ci die "Cert object partially overflows section $s_name\n" 1338c2ecf20Sopenharmony_ci if ($end > $s_vend); 1348c2ecf20Sopenharmony_ci 1358c2ecf20Sopenharmony_ci $s = $sec; 1368c2ecf20Sopenharmony_ci} 1378c2ecf20Sopenharmony_ci 1388c2ecf20Sopenharmony_cidie "Cert object not inside a section\n" 1398c2ecf20Sopenharmony_ci unless ($s); 1408c2ecf20Sopenharmony_ci 1418c2ecf20Sopenharmony_ciprint "Certificate list in section ", $s->{name}, "\n"; 1428c2ecf20Sopenharmony_ci 1438c2ecf20Sopenharmony_cimy $foff = $start - $s->{vma} + $s->{foff}; 1448c2ecf20Sopenharmony_ci 1458c2ecf20Sopenharmony_ciprintf "Certificate list at file offset 0x%x\n", $foff; 1468c2ecf20Sopenharmony_ci 1478c2ecf20Sopenharmony_cidie $vmlinux if (!defined(sysseek(FD, $foff, SEEK_SET))); 1488c2ecf20Sopenharmony_cimy $buf = ""; 1498c2ecf20Sopenharmony_cimy $len = sysread(FD, $buf, $size); 1508c2ecf20Sopenharmony_cidie "$vmlinux" if (!defined($len)); 1518c2ecf20Sopenharmony_cidie "Short read on $vmlinux\n" if ($len != $size); 1528c2ecf20Sopenharmony_ciclose(FD) || die $vmlinux; 1538c2ecf20Sopenharmony_ci 1548c2ecf20Sopenharmony_ciopen FD, ">$keyring" || die $keyring; 1558c2ecf20Sopenharmony_cibinmode(FD); 1568c2ecf20Sopenharmony_ci$len = syswrite(FD, $buf, $size); 1578c2ecf20Sopenharmony_cidie "$keyring" if (!defined($len)); 1588c2ecf20Sopenharmony_cidie "Short write on $keyring\n" if ($len != $size); 1598c2ecf20Sopenharmony_ciclose(FD) || die $keyring; 160