Back to home page

LXR

 
 

    


0001 #!/usr/bin/perl -w
0002 #
0003 use strict;
0004 use Math::BigInt;
0005 use Fcntl "SEEK_SET";
0006 
0007 die "Format: $0 [-s <systemmap-file>] <vmlinux-file> <keyring-file>\n"
0008     if ($#ARGV != 1 && $#ARGV != 3 ||
0009     $#ARGV == 3 && $ARGV[0] ne "-s");
0010 
0011 my $sysmap = "";
0012 if ($#ARGV == 3) {
0013     shift;
0014     $sysmap = $ARGV[0];
0015     shift;
0016 }
0017 
0018 my $vmlinux = $ARGV[0];
0019 my $keyring = $ARGV[1];
0020 
0021 #
0022 # Parse the vmlinux section table
0023 #
0024 open FD, "objdump -h $vmlinux |" || die $vmlinux;
0025 my @lines = <FD>;
0026 close(FD) || die $vmlinux;
0027 
0028 my @sections = ();
0029 
0030 foreach my $line (@lines) {
0031     chomp($line);
0032     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]+)/
0033     ) {
0034     my $seg  = $1;
0035     my $name = $2;
0036     my $len  = Math::BigInt->new("0x" . $3);
0037     my $vma  = Math::BigInt->new("0x" . $4);
0038     my $lma  = Math::BigInt->new("0x" . $5);
0039     my $foff = Math::BigInt->new("0x" . $6);
0040     my $align = 2 ** $7;
0041 
0042     push @sections, { name => $name,
0043               vma => $vma,
0044               len => $len,
0045               foff => $foff };
0046     }
0047 }
0048 
0049 print "Have $#sections sections\n";
0050 
0051 #
0052 # Try and parse the vmlinux symbol table.  If the vmlinux file has been created
0053 # from a vmlinuz file with extract-vmlinux then the symbol table will be empty.
0054 #
0055 open FD, "nm $vmlinux 2>/dev/null |" || die $vmlinux;
0056 @lines = <FD>;
0057 close(FD) || die $vmlinux;
0058 
0059 my %symbols = ();
0060 my $nr_symbols = 0;
0061 
0062 sub parse_symbols(@) {
0063     foreach my $line (@_) {
0064     chomp($line);
0065     if ($line =~ /([0-9a-f]+)\s([a-zA-Z])\s(\S+)/
0066         ) {
0067         my $addr = "0x" . $1;
0068         my $type = $2;
0069         my $name = $3;
0070 
0071         $symbols{$name} = $addr;
0072         $nr_symbols++;
0073     }
0074     }
0075 }
0076 parse_symbols(@lines);
0077 
0078 if ($nr_symbols == 0 && $sysmap ne "") {
0079     print "No symbols in vmlinux, trying $sysmap\n";
0080 
0081     open FD, "<$sysmap" || die $sysmap;
0082     @lines = <FD>;
0083     close(FD) || die $sysmap;
0084     parse_symbols(@lines);
0085 }
0086 
0087 die "No symbols available\n"
0088     if ($nr_symbols == 0);
0089 
0090 print "Have $nr_symbols symbols\n";
0091 
0092 die "Can't find system certificate list"
0093     unless (exists($symbols{"__cert_list_start"}) &&
0094         exists($symbols{"system_certificate_list_size"}));
0095 
0096 my $start = Math::BigInt->new($symbols{"__cert_list_start"});
0097 my $end;
0098 my $size;
0099 my $size_sym = Math::BigInt->new($symbols{"system_certificate_list_size"});
0100 
0101 open FD, "<$vmlinux" || die $vmlinux;
0102 binmode(FD);
0103 
0104 my $s = undef;
0105 foreach my $sec (@sections) {
0106     my $s_name = $sec->{name};
0107     my $s_vma = $sec->{vma};
0108     my $s_len = $sec->{len};
0109     my $s_foff = $sec->{foff};
0110     my $s_vend = $s_vma + $s_len;
0111 
0112     next unless ($start >= $s_vma);
0113     next if ($start >= $s_vend);
0114 
0115     die "Certificate list size was not found on the same section\n"
0116     if ($size_sym < $s_vma || $size_sym > $s_vend);
0117 
0118     die "Cert object in multiple sections: ", $s_name, " and ", $s->{name}, "\n"
0119     if ($s);
0120 
0121     my $size_off = $size_sym -$s_vma + $s_foff;
0122     my $packed;
0123     die $vmlinux if (!defined(sysseek(FD, $size_off, SEEK_SET)));
0124     sysread(FD, $packed, 8);
0125     $size = unpack 'L!', $packed;
0126     $end = $start + $size;
0127 
0128     printf "Have %u bytes of certs at VMA 0x%x\n", $size, $start;
0129 
0130     die "Cert object partially overflows section $s_name\n"
0131     if ($end > $s_vend);
0132 
0133     $s = $sec;
0134 }
0135 
0136 die "Cert object not inside a section\n"
0137     unless ($s);
0138 
0139 print "Certificate list in section ", $s->{name}, "\n";
0140 
0141 my $foff = $start - $s->{vma} + $s->{foff};
0142 
0143 printf "Certificate list at file offset 0x%x\n", $foff;
0144 
0145 die $vmlinux if (!defined(sysseek(FD, $foff, SEEK_SET)));
0146 my $buf = "";
0147 my $len = sysread(FD, $buf, $size);
0148 die "$vmlinux" if (!defined($len));
0149 die "Short read on $vmlinux\n" if ($len != $size);
0150 close(FD) || die $vmlinux;
0151 
0152 open FD, ">$keyring" || die $keyring;
0153 binmode(FD);
0154 $len = syswrite(FD, $buf, $size);
0155 die "$keyring" if (!defined($len));
0156 die "Short write on $keyring\n" if ($len != $size);
0157 close(FD) || die $keyring;