File uboot-image-a13olinuxino-install of Package JeOS-raspberrypi2
#!/bin/bash
#
# U-Boot injection script.
#
# This script installs U-Boot SPL, MLO, BL1, IMX, whatever images into
# the target image during setup time as well as on first boot.
#
# It also serves as our generic hook into things we need to do to fix
# up the build.
set -x
diskname=$1
flavor=a13olinuxino
is_firstboot=
if [ "$is_firstboot" ]; then
# We're not inside the image build, but in the first boot of an
# installed system
diskname=$(df / --output=source | tail -n1 | sed 's/[0-9]*$//;s/p$//')
is_firstboot=1
if [ -d /boot/efi ]; then
is_efi=1
fi
cd /
# During firstboot, gdisk is available at /usr/sbin.
export PATH="$PATH:/usr/sbin/"
else
# We don't have gdisk in the build system, so point to the rootfs binary
export PATH="$PATH:/usr/src/packages/KIWIROOT-oem/usr/sbin/"
fi
(
if [ -d EFI ]; then
# We're on an EFI build, so kiwi doesn't copy u-boot over.
# This is a hack to chdir into the obs-build dir to find
# our u-boot binaries nevertheless.
cd /usr/src/packages/KIWIROOT-oem/
is_efi=1
fi
#==========================================
# Convert GPT to MBR if necessary
#------------------------------------------
if [ "$is_efi" ]; then
# Some systems can not deal with GPT partition tables, so for
# those we convert the GPT to MBR
force_mbr=
if [ -f "boot/vc/bootcode.bin" ]; then
# The RPi firmware can only read MBR
force_mbr=1
fi
if [ -f "boot/cuboxi-spl.bin" -o -f "boot/u-boot.imx" ]; then
# SPL goes at sector 2, overlapping GPT
force_mbr=1
fi
if [ -f "boot/origen-spl.bin" ]; then
# SPL goes at sector 1, overlapping GPT
force_mbr=1
fi
if [ -f "boot/arndale-bl1.bin" -o \
-f "boot/bl1.bin.hardkernel" -o \
-f "boot/E4412_S.bl1.HardKernel.bin" ]; then
# BL1 goes at sector 1, overlapping GPT
force_mbr=1
fi
if [ "$force_mbr" ]; then
# The target system doesn't support GPT, so let's move it to
# MBR partition layout instead.
#
# Also make sure to set the ESP partition to type 0xc so that
# broken firmware (Rpi) detects it as FAT.
cat > gdisk.tmp <<-'EOF'
x
r
g
t
1
c
w
y
EOF
losetup /dev/loop3 $diskname
dd if=/dev/loop3 of=mbrid.bin bs=1 skip=440 count=4
gdisk /dev/loop3 < gdisk.tmp
dd of=/dev/loop3 if=mbrid.bin bs=1 seek=440 count=4
losetup -d /dev/loop3
rm -f mbrid.bin
rm -f gdisk.tmp
fi
fi
#==========================================
# adjust Raspberry Pi partition table
#------------------------------------------
if [ -f "boot/vc/bootcode.bin" -a ! "$is_efi" ]; then
echo -n > gdisk.tmp
if [ ! "$is_firstboot" ]; then
# Set the name of the first partition to "vcboot" and mark
# the 1st and 2nd partitions as bootable (checked by RPi loader and U-Boot)
cat >> gdisk.tmp <<-'EOF'
c
1
vcboot
x
a
1
2
64
a
2
2
64
m
EOF
else
# Mark the 1st partition as (legacy) bootable (checked by RPi loader), since repartion removed it
cat >> gdisk.tmp <<-'EOF'
x
a
1
2
64
m
EOF
fi
# Convert GPT to hybrid GPT
cat >> gdisk.tmp <<-'EOF'
x
r
h
1 2 3
n
c
n
82
y
83
n
w
y
EOF
if [ "$is_firstboot" ]; then
gdisk /dev/mmcblk0 < gdisk.tmp
else
gdisk /dev/loop0 < gdisk.tmp
fi
rm -f gdisk.tmp
fi
#==========================================
# set certain dirs as nocow
#------------------------------------------
function nocow() {
[ -d $1 ] && chattr +C $i
}
for i in var/lib/mariadb var/lib/mysql var/lib/pgsql var/lib/libvirt/images; do
nocow $i
done
#==========================================
# copy Raspberry Pi firmware to EFI partition
#------------------------------------------
if [ -f "boot/vc/bootcode.bin" -a "$is_efi" -a ! "$is_firstboot" ]; then
echo "RPi EFI system, installing firmware on ESP"
LINE=$(kpartx -asv $diskname | head -n1)
PART=$(echo "$LINE" | awk '{print $3}')
mkdir -p ./mnt-pi
mount /dev/mapper/$PART ./mnt-pi
( cd boot/vc; tar c . ) | ( cd ./mnt-pi/; tar x )
umount ./mnt-pi
rmdir ./mnt-pi
# "kpartx -dv $diskname" does not work if $diskname
# is longer than 64 characters
LOOPDEV=$(echo "/dev/$PART" | sed 's/p[0-9][0-9]*$//')
kpartx -dv $LOOPDEV
losetup -d $LOOPDEV
fi
#==========================================
# install MLO as raw
#------------------------------------------
if [ -f "boot/MLO" ];then
echo "Installing MLO (SPL)..."
opt="count=1 seek=1 conv=notrunc"
if ! dd if=boot/MLO of=$diskname bs=128k $opt; then
echo "Couldn't install MLO on $diskname"
exit 1
fi
echo "Installing U-Boot..."
opt="seek=1 conv=notrunc"
if ! dd if=boot/u-boot.img of=$diskname bs=384k $opt; then
echo "Couldn't install U-Boot on $diskname"
exit 1
fi
# /.../
# To avoid any issues when parted leaves x86 boot code
# in the MBR we better clear that part of the image
# ----
dd if=/dev/zero of=$diskname bs=440 count=1 conv=notrunc
fi
#==========================================
# install Origen SPL & u-boot as raw
#------------------------------------------
if [ -f "boot/origen-spl.bin" ];then
echo "Installing SPL..."
if ! dd if=boot/origen-spl.bin of=$diskname seek=1 conv=notrunc; then
echo "Couldn't install SPL on $diskname"
exit 1
fi
echo "Installing U-Boot..."
if ! dd if=boot/u-boot.bin of=$diskname seek=65 conv=notrunc; then
echo "Couldn't install u-boot on $diskname"
exit 1
fi
fi
#==========================================
# install Odroid (exynos4) BL* & u-boot as raw
#------------------------------------------
if [ -f "boot/E4412_S.bl1.HardKernel.bin" ];then
echo "Installing BL1..."
if ! dd if=boot/E4412_S.bl1.HardKernel.bin of=$diskname seek=1 conv=notrunc; then
echo "Couldn't install BL1 on $diskname"
exit 1
fi
echo "Installing BL2..."
if ! dd if=boot/bl2.signed.bin of=$diskname seek=31 conv=notrunc; then
echo "Couldn't install BL2 on $diskname"
exit 1
fi
echo "Installing U-Boot..."
if ! dd if=boot/u-boot.bin of=$diskname seek=63 conv=notrunc; then
echo "Couldn't install u-boot on $diskname"
exit 1
fi
if ! dd if=boot/E4412_S.tzsw.signed.bin of=$diskname seek=2111 conv=notrunc; then
echo "Couldn't install TrustZone S/W on $diskname"
exit 1
fi
fi
#==========================================
# install Odroid X3 (exynos5) BL* & u-boot as raw
#------------------------------------------
if [ -f "boot/bl1.bin.hardkernel" ];then
echo "Installing BL1..."
if ! dd if=boot/bl1.bin.hardkernel of=$diskname seek=1 conv=notrunc; then
echo "Couldn't install BL1 on $diskname"
exit 1
fi
echo "Installing BL2..."
if ! dd if=boot/bl2.bin.hardkernel of=$diskname seek=31 conv=notrunc; then
echo "Couldn't install BL2 on $diskname"
exit 1
fi
echo "Installing U-Boot..."
if ! dd if=boot/u-boot.bin of=$diskname seek=63 conv=notrunc; then
echo "Couldn't install u-boot on $diskname"
exit 1
fi
if ! dd if=boot/tzsw.bin.hardkernel of=$diskname seek=719 conv=notrunc; then
echo "Couldn't install TrustZone S/W on $diskname"
exit 1
fi
fi
#==========================================
# install Arndale SPL & u-boot as raw
#------------------------------------------
if [[ -f "boot/smdk5250-spl.bin" || -f "boot/arndale-spl.bin" ]];then
echo "Installing BL1..."
if ! dd if=boot/arndale-bl1.img of=$diskname seek=1 conv=notrunc; then
echo "Couldn't install BL1 on $diskname"
exit 1
fi
echo "Installing SPL..."
# Get SPL name (depends on u-boot version)
SPL_FILE=$(ls boot/*-spl.bin);
if ! dd if=$SPL_FILE of=$diskname seek=17 conv=notrunc; then
echo "Couldn't install SPL ($SPL_FILE) on $diskname"
exit 1
fi
echo "Installing U-Boot..."
if ! dd if=boot/u-boot.bin of=$diskname seek=49 conv=notrunc; then
echo "Couldn't install u-boot on $diskname"
exit 1
fi
fi
#==========================================
# install Cubieboard/Cubietruck SPL & u-boot as raw
#------------------------------------------
if [[ -f "boot/sunxi-spl.bin" || -f "boot/u-boot-sunxi-with-spl.bin" || -f "boot/pine64.img" ]];then
if [ "$is_efi" ]; then
# The GPT spans the first 33 sectors, but we need to write our
# at sector 16. Shrink the GPT to only span 5 sectors
# (16 partitions) to give us some space.
echo -e 'x\ns\n16\nw\ny' > gdisk.tmp
losetup /dev/loop3 $diskname
dd if=/dev/loop3 of=mbrid.bin bs=1 skip=440 count=4
gdisk /dev/loop3 < gdisk.tmp
dd of=/dev/loop3 if=mbrid.bin bs=1 seek=440 count=4
losetup -d /dev/loop3
rm -f mbrid.bin
rm -f gdisk.tmp
fi
if [ -f "boot/u-boot-sunxi-with-spl.bin" ]; then
echo "Installing All-in-one U-Boot/SPL..."
if ! dd if=boot/u-boot-sunxi-with-spl.bin of=$diskname bs=1024 seek=8 conv=notrunc; then
echo "Couldn't install SPL on $diskname"
exit 1
fi
elif [ -f "boot/pine64.img" ]; then
echo "Installing All-in-one U-Boot/SPL..."
if ! dd if=boot/pine64.img of=$diskname bs=1024 seek=8 skip=8 conv=notrunc; then
echo "Couldn't install SPL on $diskname"
exit 1
fi
else
echo "Installing SPL..."
if ! dd if=boot/sunxi-spl.bin of=$diskname bs=1024 seek=8 conv=notrunc; then
echo "Couldn't install SPL on $diskname"
exit 1
fi
echo "Installing U-Boot..."
if ! dd if=boot/u-boot.img of=$diskname bs=1024 seek=40 conv=notrunc; then
echo "Couldn't install u-boot on $diskname"
exit 1
fi
fi
fi
#==========================================
# install Chromebook u-boot as boot kernel
# And do the required magic!
#------------------------------------------
if [ "$flavor" = "chromebook" ]; then
if [ ! "$is_firstboot" ]; then
pushd /usr/src/packages/KIWIROOT-oem/
echo "console=tty1 debug verbose" > /tmp/config
./usr/bin/vbutil_kernel --pack /tmp/newkern \
--keyblock ./usr/share/vboot/devkeys/kernel.keyblock \
--version 1 \
--signprivate ./usr/share/vboot/devkeys/kernel_data_key.vbprivk \
--config=/tmp/config --vmlinuz boot/u-boot.img --arch arm
LINE=$(kpartx -asv $diskname | head -n1)
PART=$(echo "$LINE" | awk '{print $3}')
dd if=/tmp/newkern of=/dev/mapper/$PART
# "kpartx -dv $diskname" does not work if $diskname is longer than 64 characters
LOOPDEV=$(echo "/dev/$PART" | sed 's/p[0-9][0-9]*$//')
kpartx -dv $LOOPDEV
losetup -d $LOOPDEV
fi
# For build and after reaprtition occured on 1st boot:
# Enable bootflag on partition 2 (u-boot now looks for bootscripts on bootable partitions only)
parted $diskname set 2 boot on
# CGPT magic
./usr/bin/cgpt add -t kernel -i 1 -S 1 -T 5 -P 10 -l U-BOOT $diskname
popd
fi
#==========================================
# install i.MX u-boot as raw
#------------------------------------------
if [ -f "boot/u-boot.imx" ];then
if [ ! "$is_firstboot" ]; then
pushd /usr/src/packages/KIWIROOT-oem/
else
pushd /
fi
echo "Installing U-Boot..."
if ! dd if=boot/u-boot.imx of=$diskname bs=512 seek=2 conv=notrunc; then
echo "Couldn't install u-boot on $diskname"
exit 1
fi
popd
fi
#==========================================
# install Cubox-i SPL & u-boot as raw
#------------------------------------------
if [ -f "boot/cuboxi-spl.bin" ];then
echo "Installing SPL..."
if ! dd if=boot/cuboxi-spl.bin of=$diskname bs=1024 seek=1 conv=notrunc; then
echo "Couldn't install SPL on $diskname"
exit 1
fi
echo "Installing U-Boot..."
if ! dd if=boot/u-boot.img of=$diskname bs=1024 seek=69 conv=notrunc; then
echo "Couldn't install u-boot on $diskname"
exit 1
fi
fi
)
if [ "$is_firstboot" ]; then
for file in /etc/sysconfig/bootloader /etc/default/grub; do
# On second boot, the rootfs is no longer tmpfs and dracut would interpret the
# command line argument, remove it again from the config
[ -e "$file" ] && sed -i -e 's/rootflags=size=100%//' $file
# Make 2nd boot quieter since we already succeeded booting :-)
[ -e "$file" ] && sed -i -e 's/loglevel=3/quiet/' $file
done
# Fix up grub2 efi installation on 32bit arm (shouldn't hurt elsewhere)
if grep -q boot/efi /etc/fstab; then
mkdir -p /boot/efi
mount /boot/efi
/sbin/update-bootloader --reinit
fi
else
# Install a startup.nsh file so we boot automatically via the EFI shell
if [ -f boot/grub2/*efi/grub.efi -o -f ./EFI/BOOT/boot*.efi ];then
echo "EFI system, installing startup.nsh"
LINE=$(kpartx -asv $diskname | head -n1)
PART=$(echo "$LINE" | awk '{print $3}')
mkdir ./mnt
mount /dev/mapper/$PART ./mnt
if [ -f boot/grub2/arm-efi/grub.efi ]; then
echo "bootarm" > mnt/startup.nsh
else
echo "bootaa64" > mnt/startup.nsh
fi
umount ./mnt
rmdir ./mnt
# "kpartx -dv $diskname" does not work if $diskname
# is longer than 64 characters
LOOPDEV=$(echo "/dev/$PART" | sed 's/p[0-9][0-9]*$//')
kpartx -dv $LOOPDEV
losetup -d $LOOPDEV
fi
fi
#==========================================
# u-boot can not handle 64bit filesystems right now, so we convert
# back to 32bit
#------------------------------------------
if ! [ "$is_firstboot" ]; then
echo "APPLYING WORKAROUND FOR https://bugzilla.suse.com/show_bug.cgi?id=989284"
for part in $(kpartx -asv $diskname | awk '{print $3}'); do
fs=/dev/mapper/$part
if tune2fs -l $fs 2>/dev/null | grep -q 64bit; then
echo "partition $part seems to be affected"
tune2fs -O ^64bit $fs
e2fsck -f -y $fs
resize2fs -s $fs
fi
done
for loop in $(losetup -j $diskname | cut -d: -f1); do
kpartx -dv $loop
losetup -d $loop
done
fi