|
| 1 | +#!/bin/sh |
| 2 | + |
| 3 | +set -e |
| 4 | + |
| 5 | +script_dir=$(cd "$(dirname "$0")" && pwd) |
| 6 | +TMP_DIR="" |
| 7 | + |
| 8 | +die() { |
| 9 | + echo "$@" >&2 |
| 10 | + exit 1 |
| 11 | +} |
| 12 | + |
| 13 | +cleanup() { |
| 14 | + if [ -d "${TMP_DIR}" ]; then |
| 15 | + rm -rf "${TMP_DIR}" |
| 16 | + fi |
| 17 | +} |
| 18 | + |
| 19 | +usage() { |
| 20 | +cat <<EOF |
| 21 | +Usage: |
| 22 | +sudo $(basename $0): <gpio_num> |
| 23 | +
|
| 24 | + Creates and SD card image which programs the OTP on a Pi 4B or Pi 400 |
| 25 | + to select a GPIO on the 40-pin header for use as the rpiboot GPIO. |
| 26 | + Once programmed, if this GPIO is pulled to ground at power on the SoC |
| 27 | + bootrom will boot into rpiboot provisioning mode. |
| 28 | +
|
| 29 | + This setting permenantely modifies the device confiuration and cannot |
| 30 | + be undone or changed. |
| 31 | +
|
| 32 | + The SD image will be written to images-2711/pi4-program-rpiboot-gpioN.zip |
| 33 | + and can be flashed using Raspberry Pi Imager to a spare SD card. |
| 34 | + As with programming the bootloader EEPROM insert the card in the |
| 35 | + Raspberry Pi, power on and wait for the green LED to flash. |
| 36 | + |
| 37 | + gpio_num: Select the rpiboot GPIO number from 2,4,5,6,7 or 8. |
| 38 | +EOF |
| 39 | + exit 1 |
| 40 | +} |
| 41 | + |
| 42 | +trap cleanup EXIT |
| 43 | + |
| 44 | +[ "$(id -u)" = "0" ] || die "$(basename $0) must be run as root" |
| 45 | +[ -n "${SUDO_UID}" ] || die "SUDO_UID not defined" |
| 46 | +[ -n "${SUDO_GID}" ] || die "SUDO_GID not defined" |
| 47 | + |
| 48 | +build_image() |
| 49 | +{ |
| 50 | + chip="${1}" |
| 51 | + gpio="${2}" |
| 52 | + img="pi4-program-rpiboot-gpio${gpio}" |
| 53 | + zip="${img}.zip" |
| 54 | + img="${img}.img" |
| 55 | + |
| 56 | + TMP_DIR="$(mktemp -d)" |
| 57 | + ( |
| 58 | + mkdir "${TMP_DIR}/files" |
| 59 | + cd "${TMP_DIR}/files" |
| 60 | + cp "${script_dir}/../firmware-${chip}/latest/recovery.bin" . |
| 61 | + cat <<EOF > config.txt |
| 62 | +uart_2ndstage=1 |
| 63 | +recovery_wait=1 |
| 64 | +program_rpiboot_gpio=${gpio} |
| 65 | +EOF |
| 66 | + echo "Generated config.txt file" |
| 67 | + cat config.txt |
| 68 | + cd "${TMP_DIR}" |
| 69 | + dd if=/dev/zero bs=1M count=258 of=temp.img > /dev/null 2>&1 |
| 70 | + /sbin/sfdisk temp.img <<EOF |
| 71 | +label: dos |
| 72 | +label-id: 0x0a7b5ac5 |
| 73 | +device: temp.img |
| 74 | +unit: sectors |
| 75 | +
|
| 76 | +./test.img1 : start= 2048, size= 524288, type=c |
| 77 | +EOF |
| 78 | + file temp.img |
| 79 | + LOOP="/dev/mapper/$(kpartx -lv temp.img | head -n1 | awk '{print $1}')" |
| 80 | + kpartx -a temp.img |
| 81 | + /sbin/mkfs.fat -F 32 -s 1 "${LOOP}" > /dev/null |
| 82 | + mkdir fs |
| 83 | + mount "${LOOP}" fs |
| 84 | + cp -v files/* fs |
| 85 | + sync |
| 86 | + sleep 5 |
| 87 | + umount fs |
| 88 | + # Delay before calling kpartx otherwise it's sometimes possible to get orphaned loopback devices |
| 89 | + sleep 5 |
| 90 | + kpartx -d temp.img |
| 91 | + ) |
| 92 | + image_dir="images-${chip}" |
| 93 | + mkdir -p "${image_dir}" |
| 94 | + chown "${SUDO_UID}:${SUDO_GID}" "${image_dir}" |
| 95 | + mv "${TMP_DIR}/temp.img" "${image_dir}/${img}" |
| 96 | + file "${image_dir}/${img}" |
| 97 | + cd "${image_dir}" |
| 98 | + zip "${zip}" "${img}" |
| 99 | + cd .. |
| 100 | + rm "${image_dir}/${img}" |
| 101 | + chown "${SUDO_UID}:${SUDO_GID}" "${image_dir}/${zip}" |
| 102 | + echo "Wrote $(pwd)/${image_dir}/${zip}" |
| 103 | +} |
| 104 | + |
| 105 | + |
| 106 | +if ! command -v kpartx > /dev/null; then |
| 107 | + die "kpartx not found: Try installing the kpartx package" |
| 108 | +fi |
| 109 | + |
| 110 | +[ -n "${1}" ] || usage |
| 111 | +gpio_num="$1" |
| 112 | + |
| 113 | +case "${gpio_num}" in |
| 114 | + 2) |
| 115 | + ;; |
| 116 | + 4) |
| 117 | + ;; |
| 118 | + 5) |
| 119 | + ;; |
| 120 | + 6) |
| 121 | + ;; |
| 122 | + 7) |
| 123 | + ;; |
| 124 | + 8) |
| 125 | + ;; |
| 126 | + *) |
| 127 | + echo "GPIO ${gpio_num} is not supported" |
| 128 | + echo |
| 129 | + usage |
| 130 | + ;; |
| 131 | +esac |
| 132 | + |
| 133 | +build_image 2711 "${gpio_num}" |
0 commit comments