1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526
|
#!/bin/sh
# SPDX-License-Identifier: GPL-3.0+
# Copyright 2023-2025 Johannes Schauer Marin Rodrigues <josch@mister-muffin.de>
set -eu
usage() {
echo "Usage: " >&2
echo " reform-flash-bootloader [--offline] [--force] [DEVICE...]" >&2
echo >&2
echo "Download and flash the bootloader for the current platform to DEVICE. Unless" >&2
echo "the --offline option is given, download latest bootloader to /boot/flash.bin." >&2
echo "If one or more DEVICE is given, flash /boot/flash.bin with the correct" >&2
echo "offset to DEVICE. The short-hands 'sd' and 'emmc' can be used to flash" >&2
echo "the bootloader to the SD-card or eMMC, respectively." >&2
echo >&2
echo "Options:" >&2
echo " DEVICE One or more block device(s) to flash or short-hands 'sd' or 'emmc'" >&2
echo " -h, --help Display this help and exit." >&2
echo " -i IMAGE, --image=IMAGE" >&2
echo " Custom image (default: /boot/flash.bin). Implies --offline and" >&2
echo " disables checksum verification." >&2
echo " --offline Do not download latest bootloader to /boot/flash.bin." >&2
echo " -f, --force No user interaction and flash to devices marked as 'warn'" >&2
echo " --zero Fill the space where the bootloader would've been flashed to with zeroes" >&2
echo " --machine=MACHINE" >&2
echo " Force a different machine than the one this script is running on" >&2
echo " using the values from /proc/device-tree/model or the basenames of" >&2
echo " config files in /usr/share/reform-tools/machines/. This will change" >&2
echo " flashing offsets. Implies --offline. Requires a custom bootloader binary" >&2
echo " via --image. Device short-hands are disabled. Flashing to emmc is" >&2
echo " disabled." >&2
echo " --verbose Print with higher verbosity." >&2
echo " --dry-run Do not perform the actual flashing." >&2
echo "" >&2
}
nth_arg() {
shift "$1"
printf "%s" "$1"
}
OFFLINE=
FORCE=
IMAGE=
ZERO=
MACHINE=
VERBOSE=
DRYRUN=
while getopts :hfi:-: OPTCHAR; do
case "$OPTCHAR" in
h)
usage
exit 0
;;
f) FORCE=yes ;;
i) IMAGE="$OPTARG" ;;
-)
case "$OPTARG" in
help)
usage
exit 0
;;
force) FORCE=yes ;;
image)
if [ "$OPTIND" -gt "$#" ]; then
echo "E: missing argument for --image" >&2
exit 1
fi
IMAGE="$(nth_arg "$OPTIND" "$@")"
OPTIND=$((OPTIND + 1))
OFFLINE=yes
;;
image=*)
IMAGE="${OPTARG#*=}"
OFFLINE=yes
;;
machine)
if [ "$OPTIND" -gt "$#" ]; then
echo "E: missing argument for --machine" >&2
exit 1
fi
MACHINE="$(nth_arg "$OPTIND" "$@")"
OPTIND=$((OPTIND + 1))
OFFLINE=yes
;;
machine=*)
MACHINE="${OPTARG#*=}"
OFFLINE=yes
;;
offline) OFFLINE=yes ;;
zero) ZERO=yes ;;
verbose) VERBOSE=yes ;;
dry-run) DRYRUN=yes ;;
*)
echo "E: unrecognized option: --$OPTARG" >&2
exit 1
;;
esac
;;
':')
echo "E: missing argument for -$OPTARG" >&2
exit 1
;;
'?')
echo "E: unrecognized option -$OPTARG" >&2
exit 1
;;
*)
echo "E: error parsing options" >&2
exit 1
;;
esac
done
shift "$((OPTIND - 1))"
if [ "$(id -u)" -ne 0 ] && [ "$DRYRUN" != "yes" ]; then
echo "reform-flash-bootloader has to be run as root / using sudo."
exit 1
fi
if ! command -v parted >/dev/null && [ "$DRYRUN" = "yes" ]; then
# parted was not found in $PATH
# try adding /bin/sbin and /sbin to $PATH
PATH="$PATH:/usr/sbin:/sbin"
export PATH
fi
if ! command -v parted >/dev/null; then
echo "E: unable to find parted utility" >&2
exit 1
fi
# Even with --machine is used to override the machine config to load, we are
# loading the native machine config to be able to forbid flashing to eMMC.
# Since eMMC might be named differently in the config passed via --machine
# we need to load the native one for that information first.
# shellcheck source=/dev/null
if [ -e "./machines/$(cat /proc/device-tree/model).conf" ]; then
. "./machines/$(cat /proc/device-tree/model).conf"
elif [ -e "/usr/share/reform-tools/machines/$(cat /proc/device-tree/model).conf" ]; then
. "/usr/share/reform-tools/machines/$(cat /proc/device-tree/model).conf"
else
echo "E: unable to find config for $(cat /proc/device-tree/model)" >&2
exit 1
fi
if [ -n "$MACHINE" ]; then
if [ -z "$IMAGE" ]; then
echo "E: using the --machine option requires a custom image passed via the --image option" >&2
exit 1
fi
for dev in "$@"; do
case $dev in
"/dev/${DEV_MMC}"*)
echo "E: flashing to eMMC not supported with --machine" >&2
exit 1
;;
"sd" | "emmc")
echo "E: short-hands not supported with --machine. Supply the device path explicitly." >&2
exit 1
;;
esac
done
case $MACHINE in
*"/"*)
echo "E: invalid machine name (contains a slash)" >&2
exit 1
;;
*.conf)
echo "E: invalid machine name (ends with .conf)" >&2
exit 1
;;
esac
# shellcheck source=/dev/null
if [ -e "./machines/$MACHINE.conf" ]; then
. "./machines/$MACHINE.conf"
elif [ -e "/usr/share/reform-tools/machines/$MACHINE.conf" ]; then
. "/usr/share/reform-tools/machines/$MACHINE.conf"
else
echo "E: unable to find config for $MACHINE" >&2
exit 1
fi
# Make sure that the given device name can never be interpreted as meaning
# the eMMC. This is because the other machine config might indicate a device
# to be eMMC which it is not on this platform.
DEV_MMC="i-do-not-exist"
fi
for dev in "$@"; do
case $dev in
emmc | "/dev/${DEV_MMC}"*)
if [ "$EMMC_BOOT" = false ]; then
echo "E: writing bootloader to eMMC not supported on $(cat /proc/device-tree/model)" >&2
exit 1
fi
;;
sd | "/dev/${DEV_SD}"*)
if [ "$SD_BOOT" = false ]; then
printf "E: Refusing to write bootloader for this platform to SD-Card because this SoM is unable to load the bootloader from SD-Card: " >&2
if [ -z "$MACHINE" ]; then
printf "%s\n" "$(cat /proc/device-tree/model)" >&2
else
echo "$MACHINE" >&2
fi
exit 1
fi
;;
esac
done
if [ "$OFFLINE" != "yes" ]; then
if echo "$BOOTLOADER_SHA1 /boot/flash.bin" | sha1sum --strict --check >/dev/null 2>&1; then
echo "I: /boot/flash.bin is up-to-date and has expected sha1sum -- not downloading it again" >&2
else
echo "I: Downloading the bootloader to /boot/flash.bin and comparing checksum" >&2
bootloaderurl="https://source.mnt.re/reform/${BOOTLOADER_PROJECT}/-/jobs/artifacts/${BOOTLOADER_TAG}/raw/$(basename "$DTBPATH" .dtb)-flash.bin?job=build"
/usr/lib/apt/apt-helper -oAPT::Sandbox::User=root download-file "$bootloaderurl" "/boot/flash.bin" "SHA1:$BOOTLOADER_SHA1"
fi
# download mhdpfw.bin on ls1028a
case "$(cat /proc/device-tree/model)" in "MNT Reform 2 with LS1028A Module")
if echo "fa96b9aa59d7c1e9e6ee1c0375d0bcc8f8e5b78c /boot/ls1028a-mhdpfw.bin"; then
echo "I: /boot/ls1028a-mhdpfw.bin is up-to-date -- not downloading it again" >&2
else
echo "I: Downloading LS1028A MHDP firmware to /boot/ls1028a-mhdpfw.bin and comparing checksum" >&2
/usr/lib/apt/apt-helper -oAPT::Sandbox::User=root download-file \
"https://source.mnt.re/reform/reform-ls1028a-uboot/-/raw/main/ls1028a-mhdpfw.bin" \
"/boot/ls1028a-mhdpfw.bin" \
"SHA1:fa96b9aa59d7c1e9e6ee1c0375d0bcc8f8e5b78c"
fi
;;
esac
fi
if [ "$#" -eq 0 ]; then
echo "I: No device unto which to flash the bootloader provided. Exiting." >&2
exit 0
fi
if [ -z "$IMAGE" ]; then
if [ ! -e /boot/flash.bin ]; then
echo "E: /boot/flash.bin does not exist" >&2
exit 1
fi
if ! echo "$BOOTLOADER_SHA1 /boot/flash.bin" | sha1sum --strict --check >/dev/null 2>&1; then
echo "Incorrect checksum for /boot/flash.bin" >&2
echo "Either flash a custom image with --image, or run without --offline to download the latest bootloader version" >&2
exit 1
fi
IMAGE="/boot/flash.bin"
fi
if [ ! -e "$IMAGE" ]; then
echo "E: $IMAGE does not exist" >&2
exit 1
fi
bootloadersize=$(stat --format=%s "$IMAGE")
if [ "$((bootloadersize % 512))" -ne 0 ]; then
echo "E: the size of the bootloader must be a multiple of 512 bytes" >&2
fi
# check if there is enough free space at the beginning of the disk
for dev in "$@"; do
case $dev in
emmc | "/dev/${DEV_MMC}"*)
if [ "$DEV_MMC_BOOT0" = true ]; then
# there are no partitions on boot0, so no need to check here
continue
else
realdev=/dev/${DEV_MMC}
fi
;;
sd)
realdev=/dev/${DEV_SD}
;;
*) realdev="$dev" ;;
esac
if [ ! -e "$realdev" ]; then
if [ "$DRYRUN" = "yes" ]; then
echo "S: $realdev does not exist but that's okay because this is a simulation" >&2
continue
else
echo "E: $realdev does not exist" >&2
exit 1
fi
elif [ ! -b "$realdev" ]; then
if [ "$DRYRUN" = "yes" ]; then
echo "S: $realdev is not a block device but that's okay because this is a simulation" >&2
continue
else
echo "E: $realdev is not a block device" >&2
exit 1
fi
elif [ ! -r "$realdev" ]; then
if [ "$DRYRUN" = "yes" ]; then
echo "S: $realdev is not readable but that's okay because this is a simulation" >&2
continue
else
echo "E: $realdev is not readable" >&2
exit 1
fi
fi
disk_label=$(parted --json --script "$realdev" unit B print 2>/dev/null | jq --raw-output '.disk.label')
# no further tests for disks without a partition table
case $disk_label in
unknown)
echo "I: No partition table found on $realdev" >&2
continue
;;
msdos)
if [ "$BOOTLOADER_OFFSET" -lt 512 ]; then
echo "E: The bootloader would be flashed with an offset of $BOOTLOADER_OFFSET which would overwrite parts of the" >&2
echo "E: MBR partition table which requires 512 bytes of space at the beginning of $realdev" >&2
exit 1
fi
;;
gpt)
if [ "$BOOTLOADER_OFFSET" -lt 17408 ]; then
echo "E: The bootloader would be flashed with an offset of $BOOTLOADER_OFFSET which would overwrite parts of the" >&2
echo "E: GUID partition table which requires 17408 bytes of space at the beginning of $realdev" >&2
exit 1
fi
;;
esac
num_parts=$(parted --json --script "$realdev" unit B print | jq --raw-output '.disk.partitions | length')
if [ "$num_parts" -eq 0 ]; then
echo "I: No partition was found on $realdev" >&2
continue
fi
firstpartstart=$(parted --json --script "$realdev" unit B print | jq --raw-output '.disk.partitions[0].start')
# strip off trailing B
firstpartstart=${firstpartstart%B}
if [ "$((BOOTLOADER_OFFSET - FLASHBIN_OFFSET + bootloadersize))" -ge "$firstpartstart" ]; then
echo "E: The first partition on $realdev starts at $firstpartstart and would be overwritten by the bootloader" >&2
echo "E: make sure that the first $((BOOTLOADER_OFFSET - FLASHBIN_OFFSET + bootloadersize)) bytes are free on $realdev" >&2
exit 1
fi
done
# rk3588 and a311d u-boot version 2026-01-11 starts preferring NVMe over eMMC, so warn the
# user about it in case they have a bootflow on NVMe but want to use eMMC
# for their /boot instead
if [ "$ZERO" != "yes" ]; then
case $(findmnt --noheadings --evaluate --mountpoint /boot --output SOURCE) in "/dev/${DEV_MMC}"*)
case "$(cat /proc/device-tree/model)" in
"MNT Pocket Reform with BPI-CM4 Module" | \
"MNT Pocket Reform with RCORE RK3588 Module" | \
"MNT Reform 2 with BPI-CM4 Module" | \
"MNT Reform 2 with RCORE-DSI RK3588 Module" | \
"MNT Reform 2 with RCORE RK3588 Module" | \
"MNT Reform Next with RCORE RK3588 Module")
if dpkg --compare-versions "$BOOTLOADER_TAG" "ge" "2026-01-11"; then
echo "W: u-boot version 2026-01-11 for A311D and RK3588 moved the bootflow for NVMe to be preferred over eMMC." >&2
echo "W: Make sure that you do not have a boot.scr or extlinux.conf on your NVMe if you were" >&2
echo "W: using eMMC for your /boot before" >&2
if [ "$FORCE" = "yes" ]; then
echo "Proceeding without user interaction because of --force" >&2
response="y"
else
printf "Are you sure you want to proceed? [y/N] "
read -r response
fi
if [ "$response" != "y" ]; then
echo "Exiting."
exit
fi
fi
;;
esac
;;
esac
fi
if [ "$EMMC_BOOT" = warn ] && [ "$FORCE" != "yes" ]; then
for dev in "$@"; do
case $dev in
emmc | "/dev/${DEV_MMC}"*)
echo "W: Flashing the bootloader to eMMC on $(cat /proc/device-tree/model) is not without risk." >&2
echo "W: If you flash the wrong bootloader or if the flashing process goes wrong, it is" >&2
echo "W: possible to soft-brick your board. Restoring it might need some extra hardware." >&2
echo "W: Please only proceed if you are sure that the benefits outweigh the risks for you." >&2
printf "Are you sure you want to proceed? [y/N] "
read -r response
if [ "$response" != "y" ]; then
echo "Exiting."
exit
fi
break
;;
esac
done
fi
# do the flashing
for dev in "$@"; do
case $dev in
emmc | "/dev/${DEV_MMC}"*)
if [ "$DEV_MMC_BOOT0" = true ]; then
realdev=/dev/${DEV_MMC}boot0
else
realdev=/dev/${DEV_MMC}
fi
;;
sd)
realdev=/dev/${DEV_SD}
;;
*) realdev="$dev" ;;
esac
if [ "$DRYRUN" = "yes" ]; then
if [ "$ZERO" != "yes" ]; then
echo "I: Simulate writing $IMAGE to $realdev" >&2
else
echo "I: Simulate overwriting the bootloader on $realdev with zeroes" >&2
fi
else
if [ "$ZERO" != "yes" ]; then
echo "I: Writing $IMAGE to $realdev" >&2
else
echo "I: Overwriting the bootloader on $realdev with zeroes" >&2
fi
fi
case $dev in
emmc | "/dev/${DEV_MMC}"*)
if [ "$DEV_MMC_BOOT0" = true ]; then
echo 0 >"/sys/class/block/${DEV_MMC}boot0/force_ro"
fi
;;
esac
set -- of="$realdev" bs=512 seek="$((BOOTLOADER_OFFSET / 512))" skip="$((FLASHBIN_OFFSET / 512))" conv=fdatasync
if [ "$ZERO" != "yes" ]; then
# write bootloader binary
set -- if="$IMAGE" "$@"
else
# write zeroes instead of the bootloader image
set -- if="/dev/zero" "$@" count="$(((bootloadersize - FLASHBIN_OFFSET) / 512))"
fi
if [ "$DRYRUN" = "yes" ]; then
echo "S: Simulating: dd $*" >&2
else
if [ "$VERBOSE" = "yes" ]; then
echo "D: Running: dd $*" >&2
fi
dd "$@"
fi
case $dev in
emmc | "/dev/${DEV_MMC}"*)
if [ "$DEV_MMC_BOOT0" = true ]; then
echo 1 >"/sys/class/block/${DEV_MMC}boot0/force_ro"
fi
;;
esac
if [ "$DRYRUN" = "yes" ]; then
echo "S: Skipping verification because this is a simulation" >&2
continue
elif [ "$ZERO" != "yes" ]; then
echo "I: Reading bootloader from $realdev and comparing it to $IMAGE" >&2
else
echo "I: Reading bootloader from $realdev and making sure it's all zeroed out" >&2
fi
# For the extra paranoid, read back what got written and compare.
set -- --bytes="$((bootloadersize - FLASHBIN_OFFSET))"
if [ "$ZERO" != "yes" ]; then
set -- "$@" --ignore-initial="0:$FLASHBIN_OFFSET" - "$IMAGE"
else
set -- "$@" --bytes="$((bootloadersize - FLASHBIN_OFFSET))" - /dev/zero
fi
# We use "dd iflag=direct" (which uses O_DIRECT) in the hopes that we read
# from the device directly instead of re-using existing caches.
if dd if="$realdev" bs=512 skip="$((BOOTLOADER_OFFSET / 512))" count="$(((bootloadersize - FLASHBIN_OFFSET) / 512))" iflag=direct \
| cmp "$@"; then
echo "I: Successfully read the same bytes from $realdev as were written before" >&2
else
echo "E: Did not get the same bytes back which were written"
exit 1
fi
# compare checksum if no custom image was provided and if not just zeroes
# were written
if [ "$IMAGE" = "/boot/flash.bin" ] && [ "$ZERO" != "yes" ]; then
echo "I: Reading bootloader from $realdev and comparing sha1sum" >&2
trap 'rm -f $tmpbootloader' EXIT INT TERM
tmpbootloader="$(mktemp)"
dd if="$realdev" of="$tmpbootloader" bs=512 seek=$((FLASHBIN_OFFSET / 512)) skip="$((BOOTLOADER_OFFSET / 512))" count="$(((bootloadersize - FLASHBIN_OFFSET) / 512))"
if echo "$BOOTLOADER_SHA1 $tmpbootloader" | sha1sum --strict --check >/dev/null 2>&1; then
echo "I: sha1sum check successful" >&2
else
echo "E: sha1sum hash sum mismatch: bootloader on $realdev has an unexpected checksum" >&2
exit 1
fi
rm "$tmpbootloader"
trap - EXIT INT TERM
fi
done
# inform about the DIP switch position only on imx8mq
if [ -z "$MACHINE" ]; then
case "$(cat /proc/device-tree/model)" in "MNT Reform 2" | "MNT Reform 2 HDMI")
for dev in "$@"; do
case $dev in
emmc | "/dev/${DEV_MMC}"*)
echo "I: For the i.MX8MQ to load u-boot from MMC, make sure" >&2
echo "I: that your DIP switch is set to OFF." >&2
continue
;;
sd | "/dev/${DEV_SD}"*)
echo "I: For the i.MX8MQ to load u-boot from SD-Card, make sure" >&2
echo "I: that your DIP switch is set to ON." >&2
continue
;;
esac
done
;;
esac
fi
|