2 # SPDX-License-Identifier: GPL-2.0
9 die "Format: $0 [-s <systemmap-file>] <vmlinux-file> <keyring-file>\n"
10 if ($#ARGV != 1 && $#ARGV != 3 ||
11 $#ARGV == 3 && $ARGV[0] ne "-s");
20 my $vmlinux = $ARGV[0];
21 my $keyring = $ARGV[1];
24 # Parse the vmlinux section table
26 open FD
, "objdump -h $vmlinux |" || die $vmlinux;
28 close(FD
) || die $vmlinux;
32 foreach my $line (@lines) {
34 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]+)/
38 my $len = Math
::BigInt
->new("0x" . $3);
39 my $vma = Math
::BigInt
->new("0x" . $4);
40 my $lma = Math
::BigInt
->new("0x" . $5);
41 my $foff = Math
::BigInt
->new("0x" . $6);
44 push @sections, { name
=> $name,
51 print "Have $#sections sections\n";
54 # Try and parse the vmlinux symbol table. If the vmlinux file has been created
55 # from a vmlinuz file with extract-vmlinux then the symbol table will be empty.
57 open FD
, "nm $vmlinux 2>/dev/null |" || die $vmlinux;
59 close(FD
) || die $vmlinux;
64 sub parse_symbols
(@
) {
65 foreach my $line (@_) {
67 if ($line =~ /([0-9a-f]+)\s([a-zA-Z])\s(\S+)/
73 $symbols{$name} = $addr;
78 parse_symbols
(@lines);
80 if ($nr_symbols == 0 && $sysmap ne "") {
81 print "No symbols in vmlinux, trying $sysmap\n";
83 open FD
, "<$sysmap" || die $sysmap;
85 close(FD
) || die $sysmap;
86 parse_symbols
(@lines);
89 die "No symbols available\n"
90 if ($nr_symbols == 0);
92 print "Have $nr_symbols symbols\n";
94 die "Can't find system certificate list"
95 unless (exists($symbols{"__cert_list_start"}) &&
96 exists($symbols{"system_certificate_list_size"}));
98 my $start = Math
::BigInt
->new($symbols{"__cert_list_start"});
101 my $size_sym = Math
::BigInt
->new($symbols{"system_certificate_list_size"});
103 open FD
, "<$vmlinux" || die $vmlinux;
107 foreach my $sec (@sections) {
108 my $s_name = $sec->{name
};
109 my $s_vma = $sec->{vma
};
110 my $s_len = $sec->{len
};
111 my $s_foff = $sec->{foff
};
112 my $s_vend = $s_vma + $s_len;
114 next unless ($start >= $s_vma);
115 next if ($start >= $s_vend);
117 die "Certificate list size was not found on the same section\n"
118 if ($size_sym < $s_vma || $size_sym > $s_vend);
120 die "Cert object in multiple sections: ", $s_name, " and ", $s->{name
}, "\n"
123 my $size_off = $size_sym -$s_vma + $s_foff;
125 die $vmlinux if (!defined(sysseek(FD
, $size_off, SEEK_SET
)));
126 sysread(FD
, $packed, 8);
127 $size = unpack 'L!', $packed;
128 $end = $start + $size;
130 printf "Have %u bytes of certs at VMA 0x%x\n", $size, $start;
132 die "Cert object partially overflows section $s_name\n"
138 die "Cert object not inside a section\n"
141 print "Certificate list in section ", $s->{name
}, "\n";
143 my $foff = $start - $s->{vma
} + $s->{foff
};
145 printf "Certificate list at file offset 0x%x\n", $foff;
147 die $vmlinux if (!defined(sysseek(FD
, $foff, SEEK_SET
)));
149 my $len = sysread(FD
, $buf, $size);
150 die "$vmlinux" if (!defined($len));
151 die "Short read on $vmlinux\n" if ($len != $size);
152 close(FD
) || die $vmlinux;
154 open FD
, ">$keyring" || die $keyring;
156 $len = syswrite(FD
, $buf, $size);
157 die "$keyring" if (!defined($len));
158 die "Short write on $keyring\n" if ($len != $size);
159 close(FD
) || die $keyring;