From 82c90dad16a6a3e01688621c746114da7ddc0c8e Mon Sep 17 00:00:00 2001 From: Johan Gunnarsson Date: Sat, 16 Mar 2019 21:33:09 +0100 Subject: [PATCH] Code dump Support for Banana Pi M1 only for now. --- Dockerfile | 20 +++++ build-boot-allwinner | 22 +++++ build-debian | 83 +++++++++++++++++++ build-image | 14 ++++ build-u_boot | 40 +++++++++ post-2nd-stage-files/etc/fstab | 3 + post-2nd-stage-files/etc/hostname | 1 + .../etc/network/interfaces.d/eth0 | 2 + .../etc/network/interfaces.d/lo | 2 + post-2nd-stage-files/root/README.txt | 21 +++++ .../initramfs/post-update.d/zz-update-uimg | 21 +++++ .../etc/kernel/postrm.d/zz-remove-uimg | 8 ++ rebuild | 16 ++++ 13 files changed, 253 insertions(+) create mode 100644 Dockerfile create mode 100755 build-boot-allwinner create mode 100755 build-debian create mode 100755 build-image create mode 100755 build-u_boot create mode 100755 post-2nd-stage-files/etc/fstab create mode 100755 post-2nd-stage-files/etc/hostname create mode 100644 post-2nd-stage-files/etc/network/interfaces.d/eth0 create mode 100644 post-2nd-stage-files/etc/network/interfaces.d/lo create mode 100755 post-2nd-stage-files/root/README.txt create mode 100755 pre-2nd-stage-files/etc/initramfs/post-update.d/zz-update-uimg create mode 100755 pre-2nd-stage-files/etc/kernel/postrm.d/zz-remove-uimg create mode 100755 rebuild diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..2f899e8 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,20 @@ +FROM debian:latest +RUN apt-get update +ENV DEBIAN_FRONTEND=noninteractive +RUN apt-get -y install debootstrap \ + debian-archive-keyring \ + git \ + crossbuild-essential-arm64 \ + crossbuild-essential-armhf \ + qemu-user-static \ + device-tree-compiler \ + bison \ + flex \ + python-dev \ + swig \ + parted \ + e2fsprogs \ + pwgen +ENV PATH="/debimg:${PATH}" +COPY . /debimg +WORKDIR /debimg diff --git a/build-boot-allwinner b/build-boot-allwinner new file mode 100755 index 0000000..2f07682 --- /dev/null +++ b/build-boot-allwinner @@ -0,0 +1,22 @@ +#!/bin/sh +# Build SD card image + +IMAGE="${1}" # For example "boot-bananapi.bin" +DEFCONFIG="${2}" # For example "Bananapi_defconfig" +TUPLE="${3}" # For example "arm-linux-gnueabihf" + +set -e + +# Create fresh empty directory +TMP=$(mktemp -d tmp.XXXXXX) +cd "${TMP}" + +build-image tmp.img +build-u_boot u-boot "${DEFCONFIG}" "${TUPLE}" + +# Copy U-Boot to 8192 bytes from start +dd if=u-boot/u-boot-sunxi-with-spl.bin of=tmp.img bs=1K seek=8 + +truncate -s 32M tmp.img +gzip tmp.img +cp tmp.img.gz "${IMAGE}.bin.gz" diff --git a/build-debian b/build-debian new file mode 100755 index 0000000..c238976 --- /dev/null +++ b/build-debian @@ -0,0 +1,83 @@ +#!/bin/sh +# Build Debian root filesystem + +IMAGE2="$1" +ARCH="$2" +DIST="$3" + +set -e + +case "${ARCH}" in +armhf) + KERNEL=linux-image-armmp + ;; +arm64) + KERNEL=linux-image-arm64 + ;; +*) + echo "Can't decide kernel package for \"${ARCH}\"" + exit 1 + ;; +esac + +# Create fresh empty directory +TMP=$(mktemp -d tmp.XXXXXX) +cd "${TMP}" + +# Build a Debian root filesystem (first stage) +debootstrap \ + --arch="${ARCH}" \ + --verbose \ + --variant=minbase \ + --foreign \ + --include=\ +netbase,\ +ifupdown,\ +net-tools,\ +isc-dhcp-client,\ +${KERNEL},\ +systemd-sysv,\ +u-boot-tools,\ +openssh-server \ + "${DIST}" \ + debian \ + "http://ftp.se.debian.org/debian/" + +# Randomly generated root password +PASSWORD=$(pwgen -B -A 8 1) + +cp -rv --preserve=mode ../pre-2nd-stage-files/* debian + +# Copy ARM emulation stuff +cp /usr/bin/qemu-*-static debian/usr/bin + +# Build a Debian root filesystem (second stage) +chroot debian sh -e <<- EOF + /debootstrap/debootstrap --second-stage + /usr/bin/apt-get clean + /bin/rm -rf /var/lib/apt/lists/* + /bin/rm -f /var/log/*.log + /bin/echo root:${PASSWORD} | /usr/sbin/chpasswd + /bin/sed -i "s/#*\s*PermitRootLogin .*/PermitRootLogin yes/" /etc/ssh/sshd_config +EOF + +# Remove ARM emulation stuff again +rm debian/usr/bin/qemu-*-static + +cp -rv --preserve=mode ../post-2nd-stage-files/* debian + +# Write apt sources config +cat <<- EOF > debian/etc/apt/sources.list + deb http://deb.debian.org/debian ${DIST} main + deb-src http://deb.debian.org/debian ${DIST} main + deb http://security.debian.org/debian-security ${DIST}/updates main + deb-src http://security.debian.org/debian-security ${DIST}/updates main +EOF + +# Make a ext4 filesystem of this and put it into the image +rm -f ext4.img +fallocate -l 900MiB ext4.img +mkfs.ext4 -d debian ext4.img +gzip ext4.img + +cp ext4.img.gz "${IMAGE2}-${PASSWORD}.bin.gz" diff --git a/build-image b/build-image new file mode 100755 index 0000000..a7f6f01 --- /dev/null +++ b/build-image @@ -0,0 +1,14 @@ +#!/bin/sh +# Build empty image + +IMAGE="${1}" + +set -e + +rm -f "${IMAGE}" +fallocate -l 1GB "${IMAGE}" # 1,000,000,000 bytes + +parted -s "${IMAGE}" mklabel msdos +parted -s "${IMAGE}" mkpart primary fat32 -- 1MiB 32MiB # 31 MiB (32,505,856 bytes) +parted -s "${IMAGE}" mkpart primary ext2 -- 32MiB -1 +parted -s "${IMAGE}" set 2 boot on diff --git a/build-u_boot b/build-u_boot new file mode 100755 index 0000000..1b24ebf --- /dev/null +++ b/build-u_boot @@ -0,0 +1,40 @@ +#!/bin/sh +# Download and build latest version of U-Boot + +UBOOTDIR="${1}" +DEFCONFIG="${2}" +TUPLE="${3}" + +set -e + +# U-Boot version to build +VERSION=$(git ls-remote --tags --refs "git://git.denx.de/u-boot.git" "v????.??" | \ + cut -f 2 | \ + grep -o "v....\..." | \ + tail -n 1) + +# Download U-Boot +git clone -b "$VERSION" --depth 1 "git://git.denx.de/u-boot.git" "${UBOOTDIR}" + +# Step into U-Boot directory +cd "${UBOOTDIR}" + +# Setup ARCH +case "${TUPLE}" in +arm-*) + export ARCH="arm" + ;; +aarch64-*) + export ARCH="aarch64" + ;; +*) + echo "Can't decide \$ARCH for tuple \"${TUPLE}\"" + exit 1 + ;; +esac + +# CROSS_COMPILE must point to a valid compiler path prefix +export CROSS_COMPILE=$(dirname $(which "${TUPLE}-gcc"))/"${TUPLE}-" + +# Build U-Boot +make "${DEFCONFIG}" && make diff --git a/post-2nd-stage-files/etc/fstab b/post-2nd-stage-files/etc/fstab new file mode 100755 index 0000000..2f89eb6 --- /dev/null +++ b/post-2nd-stage-files/etc/fstab @@ -0,0 +1,3 @@ +# +tmpfs /tmp tmpfs nodev,nosuid 0 0 +tmpfs /var/tmp tmpfs nodev,nosuid 0 0 diff --git a/post-2nd-stage-files/etc/hostname b/post-2nd-stage-files/etc/hostname new file mode 100755 index 0000000..2dee175 --- /dev/null +++ b/post-2nd-stage-files/etc/hostname @@ -0,0 +1 @@ +debian diff --git a/post-2nd-stage-files/etc/network/interfaces.d/eth0 b/post-2nd-stage-files/etc/network/interfaces.d/eth0 new file mode 100644 index 0000000..81922ce --- /dev/null +++ b/post-2nd-stage-files/etc/network/interfaces.d/eth0 @@ -0,0 +1,2 @@ +auto eth0 +iface eth0 inet dhcp diff --git a/post-2nd-stage-files/etc/network/interfaces.d/lo b/post-2nd-stage-files/etc/network/interfaces.d/lo new file mode 100644 index 0000000..f1bd92e --- /dev/null +++ b/post-2nd-stage-files/etc/network/interfaces.d/lo @@ -0,0 +1,2 @@ +auto lo +iface lo inet loopback diff --git a/post-2nd-stage-files/root/README.txt b/post-2nd-stage-files/root/README.txt new file mode 100755 index 0000000..398131f --- /dev/null +++ b/post-2nd-stage-files/root/README.txt @@ -0,0 +1,21 @@ +HOW TO CHANGE ROOT PASSWORD +--------------------------- + +1. It is strongly recommended that you change the root password: + +# passwd root + +HOW TO RESIZE ROOT FILESYSTEM PARTITION +--------------------------------------- + +1. Maximize size of rootfs partition in: + +# parted -s /dev/mmcblk0 resizepart -- 2 -1 + +2. Reload partition table: + +# partprobe + +3. Expand ext4 filesystem: + +# resize2fs /dev/mmcblk0p2 diff --git a/pre-2nd-stage-files/etc/initramfs/post-update.d/zz-update-uimg b/pre-2nd-stage-files/etc/initramfs/post-update.d/zz-update-uimg new file mode 100755 index 0000000..d7208d9 --- /dev/null +++ b/pre-2nd-stage-files/etc/initramfs/post-update.d/zz-update-uimg @@ -0,0 +1,21 @@ +#!/bin/sh +# Generate initrd.uimg and update U-Boot boot.scr + +# Kernel ABI string +ABI="${1}" + +# Create U-Boot ramdisk image +mkimage -A arm -T ramdisk -C none -d "/boot/initrd.img-${ABI}" "/boot/initrd.uimg-${ABI}" + +# Create U-Boot boot script +cat << EOF > /boot/boot.cmd +mmc dev 0 +ext4load mmc 0:2 \${kernel_addr_r} /boot/vmlinuz-${ABI} +ext4load mmc 0:2 \${ramdisk_addr_r} /boot/initrd.uimg-${ABI} +ext4load mmc 0:2 \${fdt_addr_r} /usr/lib/linux-image-${ABI}/\${fdtfile} +setenv bootargs "root=/dev/mmcblk0p2 rw rootwait earlyprintk" +bootz \${kernel_addr_r} \${ramdisk_addr_r} \${fdt_addr_r} +EOF + +# Create U-Boot boot script +mkimage -A arm -T script -C none -d /boot/boot.cmd /boot/boot.scr diff --git a/pre-2nd-stage-files/etc/kernel/postrm.d/zz-remove-uimg b/pre-2nd-stage-files/etc/kernel/postrm.d/zz-remove-uimg new file mode 100755 index 0000000..432b096 --- /dev/null +++ b/pre-2nd-stage-files/etc/kernel/postrm.d/zz-remove-uimg @@ -0,0 +1,8 @@ +#!/bin/sh +# Generate initrd.uimg and update U-Boot boot.scr + +# Kernel ABI string +ABI="${1}" + +# Delete U-Boot ramdisk image +rm -f "/boot/initrd.uimg-${ABI}" diff --git a/rebuild b/rebuild new file mode 100755 index 0000000..e9a6af6 --- /dev/null +++ b/rebuild @@ -0,0 +1,16 @@ +#!/bin/sh +# Rebuild everything + +DOCKEROPTS1="--rm -v /tmp/artifacts:/artifacts" +DOCKEROPTS2="--privileged $DOCKEROPTS1" + +mkdir -p /tmp/artifacts || exit + +# Build Docker image +docker build -t debimg . + +# Build bootloaders +docker run $DOCKEROPTS1 debimg build-boot-allwinner "/artifacts/boot-bananapi" "Bananapi_defconfig" "arm-linux-gnueabihf" + +# Build root filesystems +docker run $DOCKEROPTS2 debimg build-debian "/artifacts/debian-buster-armhf" "armhf" "buster"