#!/bin/sh # Raspberry Pi4 boot EEPROM updater. set -e script_dir=$(cd "$(dirname "$0")" && pwd) if [ -f /etc/default/rpi-eeprom-update ]; then . /etc/default/rpi-eeprom-update fi FIRMWARE_ROOT=${FIRMWARE_ROOT:-/lib/firmware/raspberrypi/bootloader} # May be used to select beta releases instead of the default critical # updates. FIRMWARE_RELEASE_STATUS=${FIRMWARE_RELEASE_STATUS:-critical} FIRMWARE_IMAGE_DIR=${FIRMWARE_IMAGE_DIR:-${FIRMWARE_ROOT}/${FIRMWARE_RELEASE_STATUS}} FIRMWARE_BACKUP_DIR=${FIRMWARE_BACKUP_DIR:-/var/lib/raspberrypi/bootloader/backup} ENABLE_VL805_UPDATES=${ENABLE_VL805_UPDATES:-1} USE_FLASHROM=${USE_FLASHROM:-0} RECOVERY_BIN=${RECOVERY_BIN:-${FIRMWARE_ROOT}/${FIRMWARE_RELEASE_STATUS}/recovery.bin} BOOTFS=${BOOTFS:-/boot} EXIT_SUCCESS=0 EXIT_UPDATE_REQUIRED=1 EXIT_FAILED=2 EXIT_EEPROM_FROZEN=3 # Reserved # EXIT_PREVIOUS_UPDATE_FAILED=4 OVERWRITE_CONFIG=0 # Maximum safe SPI speed for EEPROM access 16000, slower is ok. SPI_SPEED=16000 # Timestamp for first release which doesn't have a timestamp field BOOTLOADER_FIRST_VERSION=1557513636 EEPROM_SIZE=524288 # Simple bootloader which is able to load start.elf in the event of a power # cut. This runs SDRAM at low speed and may have reduced functionality but # should be enough to run flashrom again. TMP_EEPROM_IMAGE="" TMP_BOOTFS_MNT="" VL805_CURRENT_VERSION= VL805_UPDATE_VERSION= # The update actions selected by the version check ACTION_UPDATE_BOOTLOADER=0 ACTION_UPDATE_VL805=0 cleanup() { if [ -f "${TMP_EEPROM_IMAGE}" ]; then rm -f "${TMP_EEPROM_IMAGE}" fi if [ -f "${TMP_EEPROM_CONFIG}" ]; then rm -f "${TMP_EEPROM_CONFIG}" fi if [ -d "${TMP_BOOTFS_MNT}" ]; then umount "${TMP_BOOTFS_MNT}" rmdir "${TMP_BOOTFS_MNT}" fi TMP_BOOTFS_MNT= TMP_EEPROM_IMAGE= TMP_EEPROM_CONFIG= } trap cleanup EXIT die() { echo "$@" >&2 exit ${EXIT_FAILED} } prepareImage() { [ -f "${BOOTLOADER_UPDATE_IMAGE}" ] || die "EEPROM image \'${BOOTLOADER_UPDATE_IMAGE}\' not found" TMP_EEPROM_IMAGE="$(mktemp)" TMP_EEPROM_CONFIG="$(mktemp)" mkdir -p "${FIRMWARE_BACKUP_DIR}" # Backup the configuration of the currently loaded bootloader vcgencmd bootloader_config > "${TMP_EEPROM_CONFIG}" backup="${FIRMWARE_BACKUP_DIR}/pieeprom-backup-$(date +%Y%m%d-%H%M%S).conf" cp -f "${TMP_EEPROM_CONFIG}" "${backup}" if [ "$(wc -l "${TMP_EEPROM_CONFIG}" | awk '{print $1}')" -lt 3 ]; then # Don't propagate empty EEPROM config files and also prevent the initial # bootloader config with WAKE_ON_GPIO=0 propgating to newer versions by # accident. OVERWRITE_CONFIG=1 fi cp -f "${BOOTLOADER_UPDATE_IMAGE}" "${TMP_EEPROM_IMAGE}" if [ "${OVERWRITE_CONFIG}" = 0 ]; then "${script_dir}/rpi-eeprom-config" \ --out "${TMP_EEPROM_IMAGE}" \ --config "${TMP_EEPROM_CONFIG}" "${BOOTLOADER_UPDATE_IMAGE}" fi } applyRecoveryUpdate() { [ -n "${BOOTLOADER_UPDATE_IMAGE}" ] || [ -n "${VL805_UPDATE_IMAGE}" ] || die "No update images specified" findBootFS # A '.sig' file is created so that recovery.bin can check that the # EEPROM image has not been created (e.g. SD card corruption). # The .sig file format is currently just a SHA256 in ASCII hex. In future, # if an actual public key signature is required then that plus any other # data would be appended after the SHA256 signature. if [ -n "${BOOTLOADER_UPDATE_IMAGE}" ]; then [ -f "${BOOTLOADER_UPDATE_IMAGE}" ] || die "${BOOTLOADER_UPDATE_IMAGE} not found" TMP_EEPROM_IMAGE="$(mktemp)" prepareImage # If recovery.bin encounters pieeprom.upd then it will select it in # preference to pieeprom.bin. The .upd file also causes recovery.bin # to rename itself to recovery.000 and reboot if the update is successful. # The rename causes the ROM to ignore this file and use the newly flashed # EEPROM image instead. sha256sum "${TMP_EEPROM_IMAGE}" | awk '{print $1}' > "${BOOTFS}/pieeprom.sig" \ || die "Failed to create ${BOOTFS}/pieeprom.sig" cp -f "${TMP_EEPROM_IMAGE}" "${BOOTFS}/pieeprom.upd" \ || die "Failed to copy ${TMP_EEPROM_IMAGE} to ${BOOTFS}" fi if [ -n "${VL805_UPDATE_IMAGE}" ]; then sha256sum "${VL805_UPDATE_IMAGE}" | awk '{print $1}' > "${BOOTFS}/vl805.sig" \ || die "Failed to create ${BOOTFS}/vl805.sig" cp -f "${VL805_UPDATE_IMAGE}" "${BOOTFS}/vl805.bin" fi cp -f "${RECOVERY_BIN}" "${BOOTFS}/recovery.bin" \ || die "Failed to copy ${RECOVERY_BIN} to ${BOOTFS}" } applyUpdate() { checksums_file="/var/lib/dpkg/info/rpi-eeprom-images.md5sums" [ "$(id -u)" = "0" ] || die "* Must be run as root - try 'sudo rpi-eeprom-update'" if [ "${IGNORE_DPKG_CHECKSUMS}" = 0 ] && [ -f "${checksums_file}" ]; then ( cd / if ! md5sum -c "${checksums_file}" > /dev/null 2>&1; then md5sum -c "${checksums_file}" die "rpi-eeprom-images checksums failed - try reinstalling this package" fi ) fi if [ "${USE_FLASHROM}" = 0 ]; then applyRecoveryUpdate return fi if [ -f "${BOOTLOADER_UPDATE_IMAGE}" ]; then # Bootloader EEPROM chip-select is muxed with audio pin so disable audio # LDO first to avoid sending noise to analog audio. /opt/vc/bin/vcmailbox 0x00030056 4 4 0 > /dev/null || true dtparam audio=off # Switch the SPI pins to boot EEPROM dtoverlay spi-gpio40-45 modprobe spidev modprobe spi-bcm2835 prepareImage "${BOOTLOADER_UPDATE_IMAGE}" echo "Applying bootloaer update ${BOOTLOADER_UPDATE_IMAGE}" flashrom -p "linux_spi:dev=/dev/spidev0.0,spispeed=${SPI_SPEED}" -w "${TMP_EEPROM_IMAGE}" || die "flashrom EEPROM update failed" dtparam -R spi-gpio40-45 dtparam audio=on /opt/vc/bin/vcmailbox 0x00030056 4 4 1 > /dev/null || true fi if [ -f "${VL805_UPDATE_IMAGE}" ]; then echo "Applying VL805 image ${VL805_UPDATE_IMAGE}" vl805 -w "${VL805_UPDATE_IMAGE}" fi echo "Applying bootloader update ${BOOTLOADER_UPDATE_IMAGE}" } # Use the version reported by the loaded EEPROM instead of attempting to retrieve # this via flashrom to avoid unnecessary audio glitches. BOOTLOADER_CURRENT_VERSION= getBootloaderCurrentVersion() { if vcgencmd bootloader_version | grep -q timestamp; then BOOTLOADER_CURRENT_VERSION=$(vcgencmd bootloader_version | grep timestamp | awk '{print $2}') if [ "${BOOTLOADER_CURRENT_VERSION}" = "0" ]; then # If a timestamp of zero is returned then it's new firmware but an # old bootloader. Assume bootloader v0 BOOTLOADER_CURRENT_VERSION="${BOOTLOADER_FIRST_VERSION}" fi else # New bootloader / old firmware ? Try to parse the date BOOTLOADER_CURRENT_VERSION=$(date -u +%s --date "$(vcgencmd bootloader_version | head -n1)" 2>/dev/null || true) fi # Failed to parse the version. Default to the initial production release. if [ -z "${BOOTLOADER_CURRENT_VERSION}" ]; then BOOTLOADER_CURRENT_VERSION="${BOOTLOADER_FIRST_VERSION}" fi } # Find latest applicable update version BOOTLOADER_UPDATE_IMAGE="" BOOTLOADER_UPDATE_VERSION=0 getBootloaderUpdateVersion() { BOOTLOADER_UPDATE_VERSION=0 match=".*/pieeprom-[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9].bin" latest="$(find "${FIRMWARE_IMAGE_DIR}" -maxdepth 1 -type f -size "${EEPROM_SIZE}c" -regex "${match}" | sort -r | head -n1)" if [ -f "${latest}" ]; then BOOTLOADER_UPDATE_VERSION=$(strings "${latest}" | grep BUILD_TIMESTAMP | sed 's/.*=//g') BOOTLOADER_UPDATE_IMAGE="${latest}" fi } checkDependencies() { CPU_VER="$(vcgencmd otp_dump | grep 30: | cut -c8)" if [ "${CPU_VER}" != "3" ]; then # Not a BCM2711, no EEPROMs to update. exit ${EXIT_SUCCESS} fi if [ ! -d "${FIRMWARE_IMAGE_DIR}" ]; then die "Bootloader updates directory ${FIRMWARE_IMAGE_DIR} not found." fi if ! command -v vl805 -h > /dev/null 2>&1; then die "vl805 command not found" fi if vcgencmd bootloader_config | grep -qi "Command not registered"; then die "vcgencmd: 'bootloader_config' command not supported. Please update VC firmware and reboot." fi if ! command -v sha256sum > /dev/null 2>&1; then die "sha256sum not found. On Debian, try installing the coreutilities package" fi if ! command -v flashrom > /dev/null 2>&1; then [ "${USE_FLASHROM}" = 0 ] || die "flashrom not found. On Debian, try installing the flashrom package." fi if [ "${USE_FLASHROM}" = 0 ]; then [ -f "${RECOVERY_BIN}" ] || die "${RECOVERY_BIN} not found." fi } usage() { cat < "${MACHINE_OUTPUT}" < "${MACHINE_OUTPUT}" <