Create a gist now

Instantly share code, notes, and snippets.

Script for building a Gentoo image for the Allwinner H3, use at your own risk
#! /bin/bash
# This script builds a Gentoo image for the Allwinner H3
# Based on http://linux-sunxi.org/H3_Manual_build_howto
# Tested on Orange Pi PC
set -e
function green {
echo -e '\033[0;32m'$@'\033[0m'
}
SDK="lichee"
SIZE="7500"
OUT=$(realpath out)
GENTOO=http://distfiles.gentoo.org/releases/arm/autobuilds/20150730/stage3-armv7a-20151011.tar.bz2
#GENTOO=http://distfiles.gentoo.org/releases/arm/autobuilds/20150606/stage3-armv7a-20150606.tar.bz2
GENTOO_TAR=$(echo "$GENTOO" | rev | cut -d "/" -f-1 | rev)
if ! [ -e "$OUT" ]; then
mkdir "$OUT"
fi
cd "$OUT"
green "-- Downloading files"
if ! [ -e "$SDK" ]; then
green "--- SDK not found, downloading..."
wget 'http://www.mediafire.com/download/fdk5kgqff3so2qg/Draco_H3_SDK_20150601_lichee.zip' \
-O sdk.zip
unzip sdk.zip
mv "Draco H3_SDK_20150601_lichee" lichee
chmod +x -R "$SDK"
fi
if ! [ -e "$GENTOO_TAR" ]; then
green "--- Gentoo stage 3 tar not found, downloading..."
wget "http://distfiles.gentoo.org/releases/arm/autobuilds/20150730/stage3-armv7a-20150730.tar.bz2" -O "$GENTOO_TAR"
fi
green "-- Building"
green "--- Patching boot0"
wget 'https://raw.githubusercontent.com/loboris/OrangePI-Kernel/master/build/orange_pi_pc.fex' -O sys_config.fex
cp -f "$SDK/tools/pack/chips/sun8iw7p1/bin/boot0_sdcard_sun8iw7p1.bin" boot0.bin
unix2dos sys_config.fex
MOD_UPDATE="$SDK/tools/pack/pctools/linux/mod_update"
"$MOD_UPDATE/script" sys_config.fex
"$MOD_UPDATE/update_boot0" boot0.bin sys_config.bin SDMMC_CARD
green "--- Building u-boot"
pushd "$SDK/brandy/u-boot-2011.09"
make clean
make sun8iw7p1_config
sed -e 's|bootcmd=.*|bootcmd=fatload mmc 0 0x43000000 script.bin; fatload mmc 0 0x48000000 uImage; bootm 0x48000000\\0""bootargs=console=ttyS0,115200 root=/dev/mmcblk0p2 rootwait\\0" \\|g' -i include/configs/sun8iw7p1.h
cat include/configs/sun8iw7p1.h
make -j
UBOOTBIN="$(pwd)/u-boot.bin"
popd
cp "$UBOOTBIN" u-boot.bin
"$MOD_UPDATE/update_uboot" u-boot.bin sys_config.bin
green "--- Building the kernel"
mkdir -p modules
MODULES_DIR="$(realpath modules)"
pushd "$SDK/linux-3.4"
export PATH="$PATH:$(realpath ../brandy/gcc-linaro/bin)"
make ARCH=arm CROSS_COMPILE=arm-linux-gnueabi- clean
make ARCH=arm CROSS_COMPILE=arm-linux-gnueabi- sun8iw7p1smp_defconfig
# These options break the build for some reason
sed -i 's/^\(CONFIG_\(CMDLINE\|INITRAMFS_SOURCE\)\)=.*/\1=""/g' .config
sed -i 's/^\(CONFIG_\(NETFILTER\|IP_NF\|BT\|ANDROID\).*\)=.*/# \1 is not set/g' .config
#make ARCH=arm CROSS_COMPILE=arm-linux-gnueabi- menuconfig
make ARCH=arm CROSS_COMPILE=arm-linux-gnueabi- -j 2 uImage modules
make ARCH=arm CROSS_COMPILE=arm-linux-gnueabi- -j 2 modules
make ARCH=arm CROSS_COMPILE=arm-linux-gnueabi- INSTALL_MOD_PATH="$MODULES_DIR" modules_install
cp -v arch/arm/boot/uImage "$OUT"
popd
green "-- Creating the image"
dd if=/dev/zero of=image.img bs=1M count="$SIZE"
LOOP="$(sudo losetup -f)"
sudo losetup "$LOOP" image.img
green "--- Partitioning"
sudo fdisk "$LOOP" <<EOF || true
o
n
p
1
40960
+50M
n
p
2
w
EOF
sudo partprobe "$LOOP"
sudo mkfs.vfat "${LOOP}p1"
sudo mkfs.ext4 "${LOOP}p2"
green "--- Installing the bootloader"
sudo dd if=boot0.bin of=$LOOP bs=1k seek=8
sudo dd if=u-boot.bin of=$LOOP bs=1k seek=16400
MNT=$(mktemp -d)
green "--- Installing kernel"
sudo mount "${LOOP}p1" "$MNT"
sudo cp uImage "$MNT"
sudo cp sys_config.bin "$MNT/script.bin"
sudo umount "$MNT"
green "--- Installing Gentoo"
sudo mount "${LOOP}p2" "$MNT"
sudo cp "$GENTOO_TAR" "$MNT"
sudo cp -r modules/lib "$MNT"
pushd "$MNT"
sudo tar -xf "$GENTOO_TAR"
# Set baud rate to something more reasonable and set autologin to root
sudo sed -i 's/9600/115200 -a root/g' etc/inittab
echo vm.mmap_min_addr = 32768 | sudo tee -a etc/sysctl.conf
popd
sudo umount "$MNT"
rm -r "$MNT"
sudo losetup -d "$LOOP"
green "All done, image in $OUT/image.img"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment