fscrypt: clarify what is meant by a per-file key
[linux/fpc-iii.git] / scripts / extract-sys-certs.pl
blobfa8ab15118ccb75e7affa90a04279ecb17f63181
1 #!/usr/bin/env perl
2 # SPDX-License-Identifier: GPL-2.0
4 use warnings;
5 use strict;
6 use Math::BigInt;
7 use Fcntl "SEEK_SET";
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");
13 my $sysmap = "";
14 if ($#ARGV == 3) {
15 shift;
16 $sysmap = $ARGV[0];
17 shift;
20 my $vmlinux = $ARGV[0];
21 my $keyring = $ARGV[1];
24 # Parse the vmlinux section table
26 open FD, "objdump -h $vmlinux |" || die $vmlinux;
27 my @lines = <FD>;
28 close(FD) || die $vmlinux;
30 my @sections = ();
32 foreach my $line (@lines) {
33 chomp($line);
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]+)/
35 ) {
36 my $seg = $1;
37 my $name = $2;
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);
42 my $align = 2 ** $7;
44 push @sections, { name => $name,
45 vma => $vma,
46 len => $len,
47 foff => $foff };
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;
58 @lines = <FD>;
59 close(FD) || die $vmlinux;
61 my %symbols = ();
62 my $nr_symbols = 0;
64 sub parse_symbols(@) {
65 foreach my $line (@_) {
66 chomp($line);
67 if ($line =~ /([0-9a-f]+)\s([a-zA-Z])\s(\S+)/
68 ) {
69 my $addr = "0x" . $1;
70 my $type = $2;
71 my $name = $3;
73 $symbols{$name} = $addr;
74 $nr_symbols++;
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;
84 @lines = <FD>;
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"});
99 my $end;
100 my $size;
101 my $size_sym = Math::BigInt->new($symbols{"system_certificate_list_size"});
103 open FD, "<$vmlinux" || die $vmlinux;
104 binmode(FD);
106 my $s = undef;
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"
121 if ($s);
123 my $size_off = $size_sym -$s_vma + $s_foff;
124 my $packed;
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"
133 if ($end > $s_vend);
135 $s = $sec;
138 die "Cert object not inside a section\n"
139 unless ($s);
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)));
148 my $buf = "";
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;
155 binmode(FD);
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;