#!/bin/sh # apk add qemu-img losetup blkid parted dosfstools e2fsprogs wget tar set -eu : "${DISK:=riscv-mmc.img}" : "${DISKSIZE:=300M}" readonly SCRIPT="${0##*/}" readonly TMPDIR="$(mktemp -dt "${SCRIPT%.*}".XXXXXX)" readonly APK_KEY="alpine-devel@lists.alpinelinux.org-60ac2099.rsa.pub" readonly MIRROR="http://dl-cdn.alpinelinux.org/alpine" readonly LOOPDEV="$(losetup --find)" readonly BOOTDEV="$LOOPDEV"p1 readonly ROOTDEV="$LOOPDEV"p2 cleanup() { set +e mountpoint -q "$TMPDIR"/boot && umount "$TMPDIR"/boot mountpoint -q "$TMPDIR" && umount "$TMPDIR" losetup --detach-all rm -rf "$TMPDIR" } trap cleanup EXIT INT kernel="linux-starfive-5.19.0-r5.apk" if [ ! -f $kernel ]; then wget https://dev.alpinelinux.org/~mps/riscv64/"$kernel" fi if [ ! -f "$DISK" ]; then echo "Creating qemu raw disk" qemu-img create -f raw "$DISK" "$DISKSIZE" chown "$USER":"$USER" "$DISK" else echo "Disk already exists exiting." exit 1 fi echo "Paritioning disk" # fat32 64MB, ext4 rest of device parted -s "$DISK" mktable gpt > /dev/null parted -s "$DISK" unit s -- mkpart primary ext4 2048 133119 > /dev/null parted -s "$DISK" -- mkpart primary ext4 65MiB 100% > /dev/null echo "Creating dirs and mounting" losetup -P "$LOOPDEV" "$DISK" mkfs.ext4 -qL boot "$BOOTDEV" mkfs.ext4 -qL root "$ROOTDEV" mount -t ext4 "$ROOTDEV" "$TMPDIR" mkdir -p "$TMPDIR"/boot mkdir -p "$TMPDIR"/etc/apk/keys mount -t ext4 "$BOOTDEV" "$TMPDIR"/boot cat < "$TMPDIR"/boot/uEnv.txt fdt_high=0xffffffffffffffff initrd_high=0xffffffffffffffff scriptaddr=0x88100000 script_offset_f=0x1fff000 script_size_f=0x1000 kernel_addr_r=0x84000000 kernel_comp_addr_r=0x90000000 kernel_comp_size=0x10000000 fdt_addr_r=0x88000000 ramdisk_addr_r=0x88300000 bootcmd=devnum=0; run mmc_boot bootcmd_mmc0=devnum=0; run mmc_boot distro_bootcmd=run bootcmd_mmc0 EOF echo "Setup apk keys and repositories" wget -qP "$TMPDIR"/etc/apk/keys https://alpinelinux.org/keys/"$APK_KEY" printf "$MIRROR/edge/%s\n" main community testing > "$TMPDIR"/etc/apk/repositories echo "installing base packages" apk --allow-untrusted --root "$TMPDIR" --arch riscv64 --initdb add \ alpine-base alpine-baselayout alpine-conf kmod openrc \ dbus util-linux blkid chrony \ sysfsutils ssl_client ca-certificates-bundle alpine-keys \ ethtool e2fsprogs e2fsprogs-extra libudev-zero libudev-zero-helper \ iwd linux-firmware-brcm linux-firmware-cypress installkernel mkinitfs \ agetty openresolv tar wget apk --allow-untrusted --root "$TMPDIR" --arch riscv64 add $kernel echo "Setting up services and inittab" for rc in boot/bootmisc boot/hostname boot/modules boot/sysctl boot/urandom boot/networking \ sysinit/devfs sysinit/hwdrivers sysinit/mdev sysinit/modules \ shutdown/mount-ro shutdown/killprocs \ default/dbus default/chronyd default/local; do ln -s /etc/init.d/"${rc##*/}" "$TMPDIR"/etc/runlevels/"$rc" done echo 'SUBSYSTEM=drm;.* root:video 660 */usr/libexec/libudev-zero-helper' >> $TMPDIR/etc/mdev.conf echo 'SUBSYSTEM=input;.* root:input 660 */usr/libexec/libudev-zero-helper' >> $TMPDIR/etc/mdev.conf echo "Settting boot loader" rootuuid=$(blkid -s UUID -o value $ROOTDEV) mkdir -p "$TMPDIR"/boot/extlinux cat < "$TMPDIR"/boot/extlinux/extlinux.conf menu title StarFive VisionFive timeout 50 default alpine label alpine menu label Alpine Linux kernel /vmlinuz-starfive initrd /initramfs-starfive fdt /dtbs-starfive/starfive/jh7100-starfive-visionfive-v1.dtb append earlycon=sbi rw root=UUID=$rootuuid rootfstype=ext4 rootwait console=ttyS0,115200 console=tty0 stmmaceth=chain_mode:1 EOF sed -i 's/^tty1.*/tty1::respawn:\/sbin\/agetty -L 115200 tty1 linux --login-pause --autologin root --noclear/' $TMPDIR/etc/inittab sed -i 's/^#ttyS0/ttyS0/' $TMPDIR/etc/inittab sed -i 's/^ttyS0.*/ttyS0::respawn:\/sbin\/agetty -L 115200 ttyS0 linux --login-pause --autologin root --noclear/' $TMPDIR/etc/inittab echo "Finished, cleaning up"