buildbrain/image/build_image.sh
Yusuke Nakamura 6fa865562d Use last loop device from loopdev -l
In my env, `make image/sd.img` failed by following error.

```
+ sudo kpartx -av /home/unasuke/src/github.com/brain-hackers/buildbrain/image/sd.img
add map loop6p1 (253:8): 0 131072 linear 7:6 2048
add map loop6p2 (253:9): 0 12154880 linear 7:6 133120
++ losetup -l
++ grep -o loop.
++ grep sd.img
+ LOOPDEV='loop6
loop4
loop5
loop3'
+ sudo mkfs.fat -F32 -v -I /dev/mapper/loop6 loop4 loop5 loop3p1
mkfs.fat 4.1 (2017-01-24)
Bad block count : loop4
Usage: mkfs.fat [-a][-A][-c][-C][-v][-I][-l bad-block-file][-b backup-boot-sector]
       [-m boot-msg-file][-n volume-name][-i volume-id]
       [-s sectors-per-cluster][-S logical-sector-size][-f number-of-FATs]
       [-h hidden-sectors][-F fat-size][-r root-dir-entries][-R reserved-sectors]
       [-M FAT-media-byte][-D drive_number]
       [--invariant]
       [--help]
       /dev/name [blocks]
make: *** [Makefile:126: image/sd.img] エラー 1
```
2021-05-05 22:05:49 +09:00

63 lines
1.4 KiB
Bash
Executable File

#!/bin/bash
set -uex -o pipefail
JOBS=$(nproc)
REPO=$(git rev-parse --show-toplevel)
WORK=${REPO}/image/work
LINUX=${REPO}/linux-brain
IMG=${REPO}/image/sd.img
mkdir -p ${WORK}
for i in $(seq 1 7); do
make -C ${REPO}/u-boot-brain pwsh${i}_defconfig
make -j${JOBS} -C ${REPO}/u-boot-brain u-boot.bin
${REPO}/nkbin_maker/bsd-ce ${REPO}/u-boot-brain/u-boot.bin
case $i in
1|2|3)
mv ${REPO}/nk.bin ${WORK}/edsa${i}exe.bin;;
4|5|6|7)
mv ${REPO}/nk.bin ${WORK}/edsh${i}exe.bin;;
*)
echo "WTF: $i"
exit 1;;
esac
done
dd if=/dev/zero of=${IMG} bs=1M count=3072
START1=2048
SECTORS1=$((1024 * 1024 * 64 / 512))
START2=$((2048 + ${SECTORS1}))
cat <<EOF > ${WORK}/part.sfdisk
${IMG}1 : start=${START1}, size=${SECTORS1}, type=b
${IMG}2 : start=${START2}, type=83
EOF
sfdisk ${IMG} < ${WORK}/part.sfdisk
sudo kpartx -av ${IMG}
LOOPDEV=$(losetup -l | grep sd.img | grep -o 'loop.' | tail -n 1)
sudo mkfs.fat -F32 -v -I /dev/mapper/${LOOPDEV}p1
sudo mkfs.ext4 /dev/mapper/${LOOPDEV}p2
mkdir -p ${WORK}/p1 ${WORK}/p2
sudo mount /dev/mapper/${LOOPDEV}p1 ${WORK}/p1
sudo mount /dev/mapper/${LOOPDEV}p2 ${WORK}/p2
sudo cp ${LINUX}/arch/arm/boot/zImage ${WORK}/p1/
sudo cp ${LINUX}/arch/arm/boot/dts/imx28-pwsh*.dtb ${WORK}/p1/
sudo cp ${WORK}/*.bin ${WORK}/p1/
sudo cp -ra ${REPO}/brainux/* ${WORK}/p2/
sudo umount ${WORK}/p1 ${WORK}/p2
sudo kpartx -d ${IMG}
rmdir ${WORK}/p1 ${WORK}/p2