#!/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"p1 readonly ROOTDEV="$LOOPDEV"p2 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 kernel="linux-starfive-6.0.0-r0.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" -- mkpart primary fat32 1MiB 65MiB > /dev/null parted -s "$DISK" -- mkpart primary ext4 65MiB 100% > /dev/null parted -s "$DISK" -- set 1 esp on > /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 <<'EOF' > "$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 mmcpart=2 fdtfile=starfive/jh7100-starfive-visionfive-v1.dtb load_efi_dtb=load mmc 0:2 ${fdt_addr_r} /boot/dtbs-starfive/${fdtfile} bootcmd=devnum=0; run load_efi_dtb; run mmc_boot distro_bootcmd=run load_efi_dtb; 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 eudev \ iwd linux-firmware-brcm linux-firmware-cypress font-dejavu installkernel mkinitfs \ agetty openresolv tar wget apk --allow-untrusted --root "$TMPDIR" --arch riscv64 add $kernel 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 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"