Linux 4.4.89
[linux/fpc-iii.git] / scripts / extract-sys-certs.pl
blobd476e7d1fd88a4328a31a18bb62bcfcb66310062
1 #!/usr/bin/perl -w
3 use strict;
4 use Math::BigInt;
5 use Fcntl "SEEK_SET";
7 die "Format: $0 [-s <systemmap-file>] <vmlinux-file> <keyring-file>\n"
8 if ($#ARGV != 1 && $#ARGV != 3 ||
9 $#ARGV == 3 && $ARGV[0] ne "-s");
11 my $sysmap = "";
12 if ($#ARGV == 3) {
13 shift;
14 $sysmap = $ARGV[0];
15 shift;
18 my $vmlinux = $ARGV[0];
19 my $keyring = $ARGV[1];
22 # Parse the vmlinux section table
24 open FD, "objdump -h $vmlinux |" || die $vmlinux;
25 my @lines = <FD>;
26 close(FD) || die $vmlinux;
28 my @sections = ();
30 foreach my $line (@lines) {
31 chomp($line);
32 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]+)/
33 ) {
34 my $seg = $1;
35 my $name = $2;
36 my $len = Math::BigInt->new("0x" . $3);
37 my $vma = Math::BigInt->new("0x" . $4);
38 my $lma = Math::BigInt->new("0x" . $5);
39 my $foff = Math::BigInt->new("0x" . $6);
40 my $align = 2 ** $7;
42 push @sections, { name => $name,
43 vma => $vma,
44 len => $len,
45 foff => $foff };
49 print "Have $#sections sections\n";
52 # Try and parse the vmlinux symbol table. If the vmlinux file has been created
53 # from a vmlinuz file with extract-vmlinux then the symbol table will be empty.
55 open FD, "nm $vmlinux 2>/dev/null |" || die $vmlinux;
56 @lines = <FD>;
57 close(FD) || die $vmlinux;
59 my %symbols = ();
60 my $nr_symbols = 0;
62 sub parse_symbols(@) {
63 foreach my $line (@_) {
64 chomp($line);
65 if ($line =~ /([0-9a-f]+)\s([a-zA-Z])\s(\S+)/
66 ) {
67 my $addr = "0x" . $1;
68 my $type = $2;
69 my $name = $3;
71 $symbols{$name} = $addr;
72 $nr_symbols++;
76 parse_symbols(@lines);
78 if ($nr_symbols == 0 && $sysmap ne "") {
79 print "No symbols in vmlinux, trying $sysmap\n";
81 open FD, "<$sysmap" || die $sysmap;
82 @lines = <FD>;
83 close(FD) || die $sysmap;
84 parse_symbols(@lines);
87 die "No symbols available\n"
88 if ($nr_symbols == 0);
90 print "Have $nr_symbols symbols\n";
92 die "Can't find system certificate list"
93 unless (exists($symbols{"__cert_list_start"}) &&
94 exists($symbols{"__cert_list_end"}));
96 my $start = Math::BigInt->new($symbols{"__cert_list_start"});
97 my $end = Math::BigInt->new($symbols{"__cert_list_end"});
98 my $size = $end - $start;
100 printf "Have %u bytes of certs at VMA 0x%x\n", $size, $start;
102 my $s = undef;
103 foreach my $sec (@sections) {
104 my $s_name = $sec->{name};
105 my $s_vma = $sec->{vma};
106 my $s_len = $sec->{len};
107 my $s_foff = $sec->{foff};
108 my $s_vend = $s_vma + $s_len;
110 next unless ($start >= $s_vma);
111 next if ($start >= $s_vend);
113 die "Cert object partially overflows section $s_name\n"
114 if ($end > $s_vend);
116 die "Cert object in multiple sections: ", $s_name, " and ", $s->{name}, "\n"
117 if ($s);
118 $s = $sec;
121 die "Cert object not inside a section\n"
122 unless ($s);
124 print "Certificate list in section ", $s->{name}, "\n";
126 my $foff = $start - $s->{vma} + $s->{foff};
128 printf "Certificate list at file offset 0x%x\n", $foff;
130 open FD, "<$vmlinux" || die $vmlinux;
131 binmode(FD);
132 die $vmlinux if (!defined(sysseek(FD, $foff, SEEK_SET)));
133 my $buf = "";
134 my $len = sysread(FD, $buf, $size);
135 die "$vmlinux" if (!defined($len));
136 die "Short read on $vmlinux\n" if ($len != $size);
137 close(FD) || die $vmlinux;
139 open FD, ">$keyring" || die $keyring;
140 binmode(FD);
141 $len = syswrite(FD, $buf, $size);
142 die "$keyring" if (!defined($len));
143 die "Short write on $keyring\n" if ($len != $size);
144 close(FD) || die $keyring;