umanovskis / baremetal-arm

An ebook about bare-metal programming for ARM

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Note on ndb not being available

dueringa opened this issue · comments

Hi,

also in doc/03_bootloader.md, there's a note regarding the nbd module.
At least under Debian Stretch, I had some issues with section, and the SD card creation script, since nbd is not loaded by default. Also, simply executing modprobe nbd didn't solve the problem. Specifically, I had to execute modprobe nbd max_part=16, and after "connecting" the image with qemu-nbd -c /dev/nbd0 sdcard.img, I had to execute partprobe /dev/nbd0 so the /dev/nbd0p1 device gets created. (Otherwise I could not execute mkfs.ext2 on it).

I'm not sure if it's worth mentioning this in the note, since this might be fairly Debian-specific, and other Linux distros might behave differently. (Related bug: https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=824553)

Based on @dueringa's suggestion, this is the create-sd.sh script that worked for me.
In case you are facing problems with that script.

#!/usr/bin/env bash

SDNAME="$1"
UIMGNAME="$2"

if [ "$#" -ne 2 ]; then
    echo "Usage: "$0" sdimage uimage"
    exit 1
fi

command -v qemu-img > /dev/null || { echo "qemu-img not installed"; exit 1; }
command -v qemu-nbd > /dev/null || { echo "qemu-nbd not installed"; exit 1; }

sudo modprobe nbd max_part=16  # <-----  Added after @dueringa's suggestion
qemu-img create "$SDNAME" 64M
sudo qemu-nbd -c /dev/nbd0 "$SDNAME"
partprobe /dev/nbd0 # <-----  Added after @dueringa's suggestion

(echo o;
echo n; echo p;
echo 1
echo ; echo
echo w; echo p) | sudo fdisk /dev/nbd0
sudo mkfs.ext2 /dev/nbd0p1

mkdir tmp || true
sudo mount -o user /dev/nbd0p1 tmp/
sudo cp "$UIMGNAME" tmp/
sudo umount /dev/nbd0p1
rmdir tmp/ || true
sudo qemu-nbd -d /dev/nbd0

nbd seems not available on WSL, seems we should find a better way to handle this.

make_ext4fs can create a ext based filesystem and genimage can make a mbr image, and they doesn't need root permission. Next is a simple scripts can replace nbd.

#!/usr/bin/env bash

SDNAME="$1"
UIMGNAME="$2"

if [ "$#" -ne 2 ]; then
    echo "Usage: "$0" sdimage uimage"
    exit 1
fi

command -v genimage >/dev/null || { echo "genimage not installed"; exit 1; }

rm -rf tmp rootfs
mkdir rootfs

cp "$UIMGNAME" rootfs
make_ext4fs -l 32M rootfs.ext4 rootfs

cat << EOF > genimage.cfg
image genimage_output.img {
	hdimage {
	}

	partition rootfs {
		partition-type = 0x83
		image = "rootfs.ext4"
	}
}
EOF

genimage --inputpath . --outputpath . genimage.cfg
mv genimage_output.img "${SDNAME}"
qemu-img resize "${SDNAME}" 64M

rm rootfs.ext4