#!/bin/sh # apk add 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"p2 readonly ROOTDEV="$LOOPDEV"p3 cleanup() { set +e mountpoint -q "$TMPDIR"/boot/efi && umount "$TMPDIR"/boot/efi mountpoint -q "$TMPDIR" && umount "$TMPDIR" losetup --detach-all rm -rf "$TMPDIR" } trap cleanup EXIT INT 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 200MB parted -s "$DISK" mktable gpt > /dev/null parted -s "$DISK" unit s -- mkpart primary ext4 64 1024 > /dev/null parted -s "$DISK" unit s -- mkpart primary fat32 2048 133119 > /dev/null parted -s "$DISK" unit s -- mkpart primary ext4 133120 542719 > /dev/null parted -s "$DISK" -- set 2 esp on > /dev/null parted -s "$DISK" -- rm 1 > /dev/null echo "Creating dirs and mounting" losetup -P "$LOOPDEV" "$DISK" mkfs.fat -F 32 -n ESP "$BOOTDEV" mkfs.ext4 -qL root "$ROOTDEV" mount -t ext4 "$ROOTDEV" "$TMPDIR" mkdir -p "$TMPDIR"/boot mkdir -p "$TMPDIR"/boot/efi "$TMPDIR"/etc/apk/keys mount -t vfat "$BOOTDEV" "$TMPDIR"/boot/efi 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 load_efi_dtb=load mmc 0:3 ${fdt_addr_r} /boot/dtbs-starfive/${fdtfile} bootcmd=devnum=0; run mmc_boot bootcmd_mmc0=devnum=0; run mmc_boot distro_bootcmd=run bootcmd_mmc0 EOF #fdtfile=starfive/jh7100-starfive-visionfive-v1.dtb 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 eudev \ iwd linux-firmware-cypress linux-firmware-brcm ttf-dejavu installkernel mkinitfs \ nvme-cli agetty terminus-font openresolv tar wget apk --allow-untrusted --root "$TMPDIR" --arch riscv64 add linux-starfive-5.18.0-r1.apk apk --allow-untrusted --root "$TMPDIR" --arch riscv64 --no-script add grub-efi grub-mkfont 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 "preparing scripts for setup" 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 cat > "$TMPDIR"/root/grub-setup.sh< /boot/grub/grub.cfg exit EOF echo 'GRUB_DISABLE_LINUX_PARTUUID=true' >> "$TMPDIR"/etc/default/grub echo 'GRUB_DISABLE_LINUX_UUID=false' >> "$TMPDIR"/etc/default/grub echo 'GRUB_CMDLINE_LINUX="rootwait console=tty0 console=ttyS0,115200 earlycon stmmaceth=chain_mode:1 "' >> "$TMPDIR"/etc/default/grub echo 'GRUB_TIMEOUT=5' >> "$TMPDIR"/etc/default/grub echo 'GRUB_FONT="/boot/grub/fonts/dejavu.pf2"' >> "$TMPDIR"/etc/default/grub chmod 0744 "$TMPDIR"/root/grub-setup.sh echo "Settting grub boot loader" mount -t proc none $TMPDIR/proc mount -o bind /sys $TMPDIR/sys mount -o bind /dev $TMPDIR/dev chroot $TMPDIR /root/grub-setup.sh umount $TMPDIR/dev umount $TMPDIR/sys umount $TMPDIR/proc # temporary hack of the DTB because its name differs in u-boot and kernel cp $TMPDIR/boot/dtbs-starfive/starfive/jh7100-starfive-visionfive-v1.dtb \ $TMPDIR/boot/dtbs-starfive/starfive/jh7100-visionfive.dtb # temporary disable i2svad sound driver till it is fixed echo "blacklist snd_soc_starfive_i2svad" \ > "$TMPDIR"/etc/modprobe.d/blacklist-i2svad.conf 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"