Merge branch 'for-next' of git://git.kernel.org/pub/scm/linux/kernel/git/gerg/m68knommu
[linux/fpc-iii.git] / scripts / extract-sys-certs.pl
blob2aa873b944e0a5666868c91760b4d9829006e759
1 #!/usr/bin/env perl
3 use warnings;
4 use strict;
5 use Math::BigInt;
6 use Fcntl "SEEK_SET";
8 die "Format: $0 [-s <systemmap-file>] <vmlinux-file> <keyring-file>\n"
9 if ($#ARGV != 1 && $#ARGV != 3 ||
10 $#ARGV == 3 && $ARGV[0] ne "-s");
12 my $sysmap = "";
13 if ($#ARGV == 3) {
14 shift;
15 $sysmap = $ARGV[0];
16 shift;
19 my $vmlinux = $ARGV[0];
20 my $keyring = $ARGV[1];
23 # Parse the vmlinux section table
25 open FD, "objdump -h $vmlinux |" || die $vmlinux;
26 my @lines = <FD>;
27 close(FD) || die $vmlinux;
29 my @sections = ();
31 foreach my $line (@lines) {
32 chomp($line);
33 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]+)/
34 ) {
35 my $seg = $1;
36 my $name = $2;
37 my $len = Math::BigInt->new("0x" . $3);
38 my $vma = Math::BigInt->new("0x" . $4);
39 my $lma = Math::BigInt->new("0x" . $5);
40 my $foff = Math::BigInt->new("0x" . $6);
41 my $align = 2 ** $7;
43 push @sections, { name => $name,
44 vma => $vma,
45 len => $len,
46 foff => $foff };
50 print "Have $#sections sections\n";
53 # Try and parse the vmlinux symbol table. If the vmlinux file has been created
54 # from a vmlinuz file with extract-vmlinux then the symbol table will be empty.
56 open FD, "nm $vmlinux 2>/dev/null |" || die $vmlinux;
57 @lines = <FD>;
58 close(FD) || die $vmlinux;
60 my %symbols = ();
61 my $nr_symbols = 0;
63 sub parse_symbols(@) {
64 foreach my $line (@_) {
65 chomp($line);
66 if ($line =~ /([0-9a-f]+)\s([a-zA-Z])\s(\S+)/
67 ) {
68 my $addr = "0x" . $1;
69 my $type = $2;
70 my $name = $3;
72 $symbols{$name} = $addr;
73 $nr_symbols++;
77 parse_symbols(@lines);
79 if ($nr_symbols == 0 && $sysmap ne "") {
80 print "No symbols in vmlinux, trying $sysmap\n";
82 open FD, "<$sysmap" || die $sysmap;
83 @lines = <FD>;
84 close(FD) || die $sysmap;
85 parse_symbols(@lines);
88 die "No symbols available\n"
89 if ($nr_symbols == 0);
91 print "Have $nr_symbols symbols\n";
93 die "Can't find system certificate list"
94 unless (exists($symbols{"__cert_list_start"}) &&
95 exists($symbols{"system_certificate_list_size"}));
97 my $start = Math::BigInt->new($symbols{"__cert_list_start"});
98 my $end;
99 my $size;
100 my $size_sym = Math::BigInt->new($symbols{"system_certificate_list_size"});
102 open FD, "<$vmlinux" || die $vmlinux;
103 binmode(FD);
105 my $s = undef;
106 foreach my $sec (@sections) {
107 my $s_name = $sec->{name};
108 my $s_vma = $sec->{vma};
109 my $s_len = $sec->{len};
110 my $s_foff = $sec->{foff};
111 my $s_vend = $s_vma + $s_len;
113 next unless ($start >= $s_vma);
114 next if ($start >= $s_vend);
116 die "Certificate list size was not found on the same section\n"
117 if ($size_sym < $s_vma || $size_sym > $s_vend);
119 die "Cert object in multiple sections: ", $s_name, " and ", $s->{name}, "\n"
120 if ($s);
122 my $size_off = $size_sym -$s_vma + $s_foff;
123 my $packed;
124 die $vmlinux if (!defined(sysseek(FD, $size_off, SEEK_SET)));
125 sysread(FD, $packed, 8);
126 $size = unpack 'L!', $packed;
127 $end = $start + $size;
129 printf "Have %u bytes of certs at VMA 0x%x\n", $size, $start;
131 die "Cert object partially overflows section $s_name\n"
132 if ($end > $s_vend);
134 $s = $sec;
137 die "Cert object not inside a section\n"
138 unless ($s);
140 print "Certificate list in section ", $s->{name}, "\n";
142 my $foff = $start - $s->{vma} + $s->{foff};
144 printf "Certificate list at file offset 0x%x\n", $foff;
146 die $vmlinux if (!defined(sysseek(FD, $foff, SEEK_SET)));
147 my $buf = "";
148 my $len = sysread(FD, $buf, $size);
149 die "$vmlinux" if (!defined($len));
150 die "Short read on $vmlinux\n" if ($len != $size);
151 close(FD) || die $vmlinux;
153 open FD, ">$keyring" || die $keyring;
154 binmode(FD);
155 $len = syswrite(FD, $buf, $size);
156 die "$keyring" if (!defined($len));
157 die "Short write on $keyring\n" if ($len != $size);
158 close(FD) || die $keyring;