2021-03-09 08:26:16 +00:00
|
|
|
#!/bin/sh
|
|
|
|
|
|
|
|
board=
|
|
|
|
device=
|
|
|
|
dryrun=
|
|
|
|
imagedir=
|
|
|
|
|
|
|
|
get_boot_blockdev() {
|
|
|
|
# Find the blockdevice where /boot is mounted from
|
2021-08-26 06:30:32 +00:00
|
|
|
mount | sed -n -E 's:.*(/dev/mmcblk[0-9])p[0-9] on /boot .*:\1:p'
|
2021-03-09 08:26:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
get_defaults() {
|
|
|
|
if [ -z "$board" ] && [ -e /sys/firmware/devicetree/base/compatible ]; then
|
|
|
|
case "$(cat /sys/firmware/devicetree/base/compatible 2>/dev/null)" in
|
|
|
|
purism,librem5r*) board=librem5;;
|
2022-01-13 07:45:40 +00:00
|
|
|
purism,librem5-devkit*) board=librem5-devkit;;
|
2021-03-09 08:26:16 +00:00
|
|
|
esac
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [ -z "$device" ]; then
|
|
|
|
case "$board" in
|
2022-01-13 07:45:40 +00:00
|
|
|
librem5*) device=/dev/mmcblk0 ;;
|
2021-03-09 08:26:16 +00:00
|
|
|
esac
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [ -z "$imagedir" ]; then
|
|
|
|
imagedir="/usr/share/u-boot"
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
die() {
|
|
|
|
echo "ERROR: $@"
|
|
|
|
exit 1
|
|
|
|
}
|
|
|
|
|
|
|
|
usage() {
|
|
|
|
get_defaults
|
|
|
|
|
|
|
|
cat <<EOF
|
2022-01-13 07:45:40 +00:00
|
|
|
usage: $0 [-n,--dry-run] [-i,--imagedir <imagedir>] [-b|--board <board-type>] [-d|--device <device>] [-s|--skip-delay]
|
2021-03-09 08:26:16 +00:00
|
|
|
|
|
|
|
options:
|
|
|
|
|
|
|
|
-b,--board <board> Specify the board type: librem5
|
|
|
|
(current default: ${board:-none})
|
|
|
|
|
|
|
|
-d,--device <device> Specify the device where to install u-boot
|
|
|
|
(current default: ${device:-none})
|
|
|
|
|
|
|
|
-i,--imagedir <imagedir> Specify u-boot image directory
|
|
|
|
(current default: ${imagedir:-none})
|
|
|
|
|
|
|
|
-n,--dry-run Print commands but don't execute them
|
|
|
|
|
2022-01-13 07:45:40 +00:00
|
|
|
-s,--skip-delay Skip delay and flash the image immediately
|
|
|
|
|
2021-03-09 08:26:16 +00:00
|
|
|
EOF
|
|
|
|
}
|
|
|
|
|
2022-01-13 07:45:40 +00:00
|
|
|
validate_checksum() {
|
|
|
|
file="$1"
|
|
|
|
file_sha512="$file.sha512"
|
|
|
|
file_size=$(stat -c%s $file)
|
|
|
|
device="$2"
|
|
|
|
bs="$3"
|
|
|
|
seek="$4"
|
|
|
|
|
|
|
|
checksum0=$(cat $file_sha512 | awk {'print $1'})
|
|
|
|
checksum1=$(dd if=$device bs=1 skip=$(($seek * $bs)) count=$file_size status=none | sha512sum | awk {'print $1'})
|
|
|
|
if [ "$checksum0" != "$checksum1" ]; then
|
|
|
|
echo "File: $checksum0"
|
|
|
|
echo "Part: $checksum1"
|
|
|
|
die "Checksum failed"
|
|
|
|
fi
|
|
|
|
echo "Successful U-Boot image checksum verification on $device :"
|
|
|
|
echo -e "\t$checksum1"
|
|
|
|
}
|
|
|
|
|
2021-03-09 08:26:16 +00:00
|
|
|
while [ $# -gt 0 ]; do
|
|
|
|
opt="$1"
|
|
|
|
shift
|
|
|
|
case "$opt" in
|
|
|
|
-b|--board)
|
|
|
|
case "$1" in
|
|
|
|
librem5) board="librem5" ;;
|
|
|
|
*) usage; exit 1;;
|
|
|
|
esac
|
|
|
|
shift
|
|
|
|
;;
|
|
|
|
-d|--device)
|
|
|
|
device="$1"
|
|
|
|
shift
|
|
|
|
;;
|
|
|
|
-i|--imagedir)
|
|
|
|
imagedir="$1"
|
|
|
|
shift
|
|
|
|
;;
|
|
|
|
-n|--dry-run)
|
|
|
|
dryrun="echo"
|
|
|
|
;;
|
2022-01-13 07:45:40 +00:00
|
|
|
-s|--skip-delay)
|
|
|
|
skip_delay="yes"
|
|
|
|
;;
|
|
|
|
--)
|
|
|
|
break
|
|
|
|
;;
|
|
|
|
-*)
|
|
|
|
usage
|
|
|
|
exit 1
|
|
|
|
;;
|
2021-03-09 08:26:16 +00:00
|
|
|
esac
|
|
|
|
done
|
|
|
|
|
|
|
|
get_defaults
|
|
|
|
if [ -z "$board" -o -z "$device" -o -z "$imagedir" -o ! -e "$imagedir" ]; then
|
|
|
|
usage
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [ -z "$dryrun" ]; then
|
2022-01-13 07:45:40 +00:00
|
|
|
if [ -z "$skip_delay" ]; then
|
|
|
|
echo "Updating $board u-boot in $device in 3 seconds..."
|
|
|
|
sleep 3
|
|
|
|
else
|
|
|
|
echo "Updating $board u-boot in $device"
|
|
|
|
fi
|
2021-03-09 08:26:16 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
(
|
|
|
|
set -e
|
|
|
|
case "$board" in
|
|
|
|
librem5)
|
2022-03-08 01:06:25 +00:00
|
|
|
seek=33
|
2021-03-09 08:26:16 +00:00
|
|
|
[ -e "$imagedir/librem5" ] || die "librem5 images not installed, apk add u-boot-librem5"
|
2022-03-08 01:06:25 +00:00
|
|
|
$dryrun dd if=$imagedir/librem5/phone-boot.img of=$device bs=1024 seek="$seek" oflag=direct status=none
|
|
|
|
[ -z "$dryrun" ] && validate_checksum $imagedir/librem5/phone-boot.img $device 1024 "$seek"
|
2022-01-13 07:45:40 +00:00
|
|
|
;;
|
|
|
|
librem5-devkit)
|
2022-03-08 01:06:25 +00:00
|
|
|
seek=33
|
2022-01-13 07:45:40 +00:00
|
|
|
[ -e "$imagedir/librem5" ] || die "librem5-devkit images not installed, apk add u-boot-librem5-devkit"
|
2022-03-08 01:06:25 +00:00
|
|
|
$dryrun dd if=$imagedir/librem5/devkit-boot.img of=$device bs=1024 seek="$seek" oflag=direct status=none
|
|
|
|
[ -z "$dryrun" ] && validate_checksum $imagedir/librem5/devkit-boot.img $device 1024 "$seek"
|
2021-03-09 08:26:16 +00:00
|
|
|
;;
|
|
|
|
esac
|
|
|
|
$dryrun sync
|
|
|
|
) || die "U-Boot installation in $device failed"
|
|
|
|
|
|
|
|
[ -z "$dryrun" ] && echo "Completed successfully."
|