From b1099d2a6406fcf2ede65e92036aebb6e6e0f6cf Mon Sep 17 00:00:00 2001 From: Steve Langasek Date: Thu, 11 Feb 2016 23:56:10 -0800 Subject: [PATCH] Import live-build/ubuntu-cpc/hooks/raspi2/mkknlimg from https://github.com/raspberrypi/linux/blob/rpi-4.1.y/scripts/mkknlimg and use it to install a bootable uboot.bin. --- debian/changelog | 3 + debian/copyright | 6 +- .../ubuntu-cpc/hooks/032-disk-image.binary | 6 + live-build/ubuntu-cpc/hooks/raspi2/mkknlimg | 244 ++++++++++++++++++ 4 files changed, 258 insertions(+), 1 deletion(-) create mode 100755 live-build/ubuntu-cpc/hooks/raspi2/mkknlimg diff --git a/debian/changelog b/debian/changelog index b1702c4b..383d7e3e 100644 --- a/debian/changelog +++ b/debian/changelog @@ -40,6 +40,9 @@ livecd-rootfs (2.376) UNRELEASED; urgency=medium live-build and reuse this for the tarball, instead of lb_binary_rootfs creating some artifact that we ignore / throw away. * Initial support for raspi2 subarch. + * Import live-build/ubuntu-cpc/hooks/raspi2/mkknlimg from + https://github.com/raspberrypi/linux/blob/rpi-4.1.y/scripts/mkknlimg + and use it to install a bootable uboot.bin. -- Ben Howard Sun, 07 Feb 2016 16:59:41 -0700 diff --git a/debian/copyright b/debian/copyright index 0a2a7189..3d267162 100644 --- a/debian/copyright +++ b/debian/copyright @@ -1,4 +1,4 @@ -live-build/* is copyright (c) 2004-2013 by Canonical Ltd. +live-build/* is copyright (c) 2004-2016 by Canonical Ltd. These programs are free software; you can redistribute and/or modify them under the terms of the GNU General Public License as published by @@ -16,3 +16,7 @@ livecd-rootfs source package as the file COPYING. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +Files: live-build/ubuntu-cpc/hooks/raspi2/mkknlimg +Copyright: 2009,2010 Dick Streefland + 2014,2015 Raspberry Pi (Trading) Limited +License: GPL-2+ diff --git a/live-build/ubuntu-cpc/hooks/032-disk-image.binary b/live-build/ubuntu-cpc/hooks/032-disk-image.binary index 7120364b..e3dc79dd 100755 --- a/live-build/ubuntu-cpc/hooks/032-disk-image.binary +++ b/live-build/ubuntu-cpc/hooks/032-disk-image.binary @@ -84,6 +84,12 @@ case $ARCH:$SUBARCH in armhf:raspi2) chroot mountpoint flash-kernel \ --machine "Raspberry Pi 2 Model B" + # not the best place for this, but neither flash-kernel nor + # u-boot have provisions for installing u-boot via maintainer + # script + /build/config/hooks/raspi2/mkknlimg --dtok \ + mountpoint/usr/lib/u-boot/rpi_2/u-boot.bin \ + mountpoint/boot/firmware/uboot.bin ;; *) ;; esac diff --git a/live-build/ubuntu-cpc/hooks/raspi2/mkknlimg b/live-build/ubuntu-cpc/hooks/raspi2/mkknlimg new file mode 100755 index 00000000..3998d43b --- /dev/null +++ b/live-build/ubuntu-cpc/hooks/raspi2/mkknlimg @@ -0,0 +1,244 @@ +#!/usr/bin/env perl +# ---------------------------------------------------------------------- +# mkknlimg by Phil Elwell for Raspberry Pi +# based on extract-ikconfig by Dick Streefland +# +# (c) 2009,2010 Dick Streefland +# (c) 2014,2015 Raspberry Pi (Trading) Limited +# +# Licensed under the terms of the GNU General Public License. +# ---------------------------------------------------------------------- + +use strict; +use warnings; +use integer; + +my $trailer_magic = 'RPTL'; + +my $tmpfile1 = "/tmp/mkknlimg_$$.1"; +my $tmpfile2 = "/tmp/mkknlimg_$$.2"; + +my $dtok = 0; +my $is_283x = 0; + +while (@ARGV && ($ARGV[0] =~ /^-/)) +{ + my $arg = shift(@ARGV); + if ($arg eq '--dtok') + { + $dtok = 1; + } + elsif ($arg eq '--283x') + { + $is_283x = 1; + } + else + { + print ("* Unknown option '$arg'\n"); + usage(); + } +} + +usage() if (@ARGV != 2); + +my $kernel_file = $ARGV[0]; +my $out_file = $ARGV[1]; + +if (! -r $kernel_file) +{ + print ("* File '$kernel_file' not found\n"); + usage(); +} + +my @wanted_strings = +( + 'bcm2708_fb', + 'brcm,bcm2835-mmc', + 'brcm,bcm2835-sdhost', + 'brcm,bcm2708-pinctrl', + 'brcm,bcm2835-gpio', + 'brcm,bcm2835', + 'brcm,bcm2836' +); + +my $res = try_extract($kernel_file, $tmpfile1); +$res = try_decompress('\037\213\010', 'xy', 'gunzip', 0, + $kernel_file, $tmpfile1, $tmpfile2) if (!$res); +$res = try_decompress('\3757zXZ\000', 'abcde', 'unxz --single-stream', -1, + $kernel_file, $tmpfile1, $tmpfile2) if (!$res); +$res = try_decompress('BZh', 'xy', 'bunzip2', 0, + $kernel_file, $tmpfile1, $tmpfile2) if (!$res); +$res = try_decompress('\135\0\0\0', 'xxx', 'unlzma', 0, + $kernel_file, $tmpfile1, $tmpfile2) if (!$res); +$res = try_decompress('\211\114\132', 'xy', 'lzop -d', 0, + $kernel_file, $tmpfile1, $tmpfile2) if (!$res); +$res = try_decompress('\002\041\114\030', 'xy', 'lz4 -d', 1, + $kernel_file, $tmpfile1, $tmpfile2) if (!$res); + +my $append_trailer; +my $trailer; +my $kver = '?'; + +$append_trailer = $dtok; + +if ($res) +{ + $kver = $res->{''} || '?'; + print("Version: $kver\n"); + + $append_trailer = $dtok; + if (!$dtok) + { + if (config_bool($res, 'bcm2708_fb') || + config_bool($res, 'brcm,bcm2835-mmc') || + config_bool($res, 'brcm,bcm2835-sdhost')) + { + $dtok ||= config_bool($res, 'brcm,bcm2708-pinctrl'); + $dtok ||= config_bool($res, 'brcm,bcm2835-gpio'); + $is_283x ||= config_bool($res, 'brcm,bcm2835'); + $is_283x ||= config_bool($res, 'brcm,bcm2836'); + $dtok ||= $is_283x; + $append_trailer = 1; + } + else + { + print ("* This doesn't look like a Raspberry Pi kernel. In pass-through mode.\n"); + } + } +} +elsif (!$dtok) +{ + print ("* Is this a valid kernel? In pass-through mode.\n"); +} + +if ($append_trailer) +{ + printf("DT: %s\n", $dtok ? "y" : "n"); + printf("283x: %s\n", $is_283x ? "y" : "n"); + + my @atoms; + + push @atoms, [ $trailer_magic, pack('V', 0) ]; + push @atoms, [ 'KVer', $kver ]; + push @atoms, [ 'DTOK', pack('V', $dtok) ]; + push @atoms, [ '283x', pack('V', $is_283x) ]; + + $trailer = pack_trailer(\@atoms); + $atoms[0]->[1] = pack('V', length($trailer)); + + $trailer = pack_trailer(\@atoms); +} + +my $ofh; +my $total_len = 0; + +if ($out_file eq $kernel_file) +{ + die "* Failed to open '$out_file' for append\n" + if (!open($ofh, '>>', $out_file)); + $total_len = tell($ofh); +} +else +{ + die "* Failed to open '$kernel_file'\n" + if (!open(my $ifh, '<', $kernel_file)); + die "* Failed to create '$out_file'\n" + if (!open($ofh, '>', $out_file)); + + my $copybuf; + while (1) + { + my $bytes = sysread($ifh, $copybuf, 64*1024); + last if (!$bytes); + syswrite($ofh, $copybuf, $bytes); + $total_len += $bytes; + } + close($ifh); +} + +if ($trailer) +{ + # Pad to word-alignment + syswrite($ofh, "\x000\x000\x000", (-$total_len & 0x3)); + syswrite($ofh, $trailer); +} + +close($ofh); + +exit($trailer ? 0 : 1); + +END { + unlink($tmpfile1) if ($tmpfile1); + unlink($tmpfile2) if ($tmpfile2); +} + + +sub usage +{ + print ("Usage: mkknlimg [--dtok] [--283x] \n"); + exit(1); +} + +sub try_extract +{ + my ($knl, $tmp) = @_; + + my $ver = `strings "$knl" | grep -a -E "^Linux version [1-9]"`; + + return undef if (!$ver); + + chomp($ver); + + my $res = { ''=>$ver }; + my $string_pattern = '^('.join('|', @wanted_strings).')$'; + + my @matches = `strings \"$knl\" | grep -E \"$string_pattern\"`; + foreach my $match (@matches) + { + chomp($match); + $res->{$match} = 1; + } + + return $res; +} + + +sub try_decompress +{ + my ($magic, $subst, $zcat, $idx, $knl, $tmp1, $tmp2) = @_; + + my $pos = `tr "$magic\n$subst" "\n$subst=" < "$knl" | grep -abo "^$subst"`; + if ($pos) + { + chomp($pos); + $pos = (split(/[\r\n]+/, $pos))[$idx]; + return undef if (!defined($pos)); + $pos =~ s/:.*[\r\n]*$//s; + my $cmd = "tail -c+$pos \"$knl\" | $zcat > $tmp2 2> /dev/null"; + my $err = (system($cmd) >> 8); + return undef if (($err != 0) && ($err != 2)); + + return try_extract($tmp2, $tmp1); + } + + return undef; +} + +sub pack_trailer +{ + my ($atoms) = @_; + my $trailer = pack('VV', 0, 0); + for (my $i = $#$atoms; $i>=0; $i--) + { + my $atom = $atoms->[$i]; + $trailer .= pack('a*x!4Va4', $atom->[1], length($atom->[1]), $atom->[0]); + } + return $trailer; +} + +sub config_bool +{ + my ($configs, $wanted) = @_; + my $val = $configs->{$wanted} || 'n'; + return (($val eq 'y') || ($val eq '1')); +}