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");
18 my $vmlinux = $ARGV[0];
19 my $keyring = $ARGV[1];
22 # Parse the vmlinux section table
24 open FD, "objdump -h $vmlinux |" || die $vmlinux;
26 close(FD) || die $vmlinux;
30 foreach my $line (@lines) {
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]+)/
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);
42 push @sections, { name => $name,
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;
57 close(FD) || die $vmlinux;
62 sub parse_symbols(@) {
63 foreach my $line (@_) {
65 if ($line =~ /([0-9a-f]+)\s([a-zA-Z])\s(\S+)/
71 $symbols{$name} = $addr;
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;
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{"system_certificate_list_size"}));
96 my $start = Math::BigInt->new($symbols{"__cert_list_start"});
99 my $size_sym = Math::BigInt->new($symbols{"system_certificate_list_size"});
101 open FD, "<$vmlinux" || die $vmlinux;
105 foreach my $sec (@sections) {
106 my $s_name = $sec->{name};
107 my $s_vma = $sec->{vma};
108 my $s_len = $sec->{len};
109 my $s_foff = $sec->{foff};
110 my $s_vend = $s_vma + $s_len;
112 next unless ($start >= $s_vma);
113 next if ($start >= $s_vend);
115 die "Certificate list size was not found on the same section\n"
116 if ($size_sym < $s_vma || $size_sym > $s_vend);
118 die "Cert object in multiple sections: ", $s_name, " and ", $s->{name}, "\n"
121 my $size_off = $size_sym -$s_vma + $s_foff;
123 die $vmlinux if (!defined(sysseek(FD, $size_off, SEEK_SET)));
124 sysread(FD, $packed, 8);
125 $size = unpack 'L!', $packed;
126 $end = $start + $size;
128 printf "Have %u bytes of certs at VMA 0x%x\n", $size, $start;
130 die "Cert object partially overflows section $s_name\n"
136 die "Cert object not inside a section\n"
139 print "Certificate list in section ", $s->{name}, "\n";
141 my $foff = $start - $s->{vma} + $s->{foff};
143 printf "Certificate list at file offset 0x%x\n", $foff;
145 die $vmlinux if (!defined(sysseek(FD, $foff, SEEK_SET)));
147 my $len = sysread(FD, $buf, $size);
148 die "$vmlinux" if (!defined($len));
149 die "Short read on $vmlinux\n" if ($len != $size);
150 close(FD) || die $vmlinux;
152 open FD, ">$keyring" || die $keyring;
154 $len = syswrite(FD, $buf, $size);
155 die "$keyring" if (!defined($len));
156 die "Short write on $keyring\n" if ($len != $size);
157 close(FD) || die $keyring;