#!/bin/sh # OpenIPC.org | 2025 scr_version=1.0.85 args="$@" LOCK_FILE=/tmp/sysupgrade.lock # Cross-repo contract with majestic. While this exists, majestic is in charge of # putting itself back after a failed upgrade and must not be restarted from # underneath -- see restore_resources. Written by its enter_upgrade_mode and # removed by leave_upgrade_mode; the other side is src/main.c in majestic. MAJESTIC_OWNER=/tmp/majestic-upgrade-owner # Where the flash phase is moved to before it starts writing. Deliberately not # under /tmp: /tmp is moved into it, and a mount cannot be relocated under # itself. See enter_ramfs. RAM_ROOT=${RAM_ROOT:-/ram} # Absolute, always. Two things downstream assume it: /proc/mounts records mount # points absolutely, so the "is this tmpfs ours" check would never match a # relative path; and the second phase reaches this same directory through the # old root as "/mnt$RAM_ROOT", which a missing leading slash turns into # "/mntram". Both would fail quietly, so normalise an override rather than # trusting whoever set it. case "$RAM_ROOT" in /*) ;; *) RAM_ROOT="/$RAM_ROOT" ;; esac # Where elf_libs looks a bare DT_NEEDED name up, in the loader's own order. # Overridable for the same reason RAM_ROOT is: the test harness's host keeps # its libraries elsewhere. ELF_LIB_DIRS=${ELF_LIB_DIRS:-/lib /usr/lib} # A writable descriptor on the real /dev/null, opened once while /dev is still # certainly the devtmpfs. # # enter_ramfs moves /dev out of the old root and then keeps running there for a # few more commands. A shell redirection CREATES its target, so a plain # `2>/dev/null` in that window discards nothing — it writes a regular file named # `null` into the old root's now-empty /dev. That root is an overlay, so the file # lands in the upper layer and outlives the upgrade: cameras came back from a # perfectly good flash carrying a 0600 /overlay/root/dev/null which shadows the # character device on every boot until /init moves devtmpfs back over it # (OpenIPC/majestic-webui#120). Reported on four SoCs at once, and absent from # every camera old enough to predate the ramfs pivot. # # A descriptor names the inode, not the path, so it keeps working across the # mount move, the pivot and the exec. Guarded and given a fallback because `exec` # is a special builtin: a redirection it cannot satisfy takes the script down. if [ -c /dev/null ]; then exec 3>/dev/null; else exec 3>&2; fi # The hardware watchdog, and where to look for the process that owns it. # # majestic opens /dev/watchdog and is the only thing on the image that pets it # (measured on gk7205v200, hi3516av300 and t31 -- three vendors, one pattern). # Nothing here opens it unless that owner dies mid-flash; see watchdog_keep. # Overridable so the test harness can point both at its sandbox. WDOG=${WDOG:-/dev/watchdog} WDOG_PROC=${WDOG_PROC:-/proc} # Written to tell a running watchdog_keep to stop petting and let go. Inside the # pivot /tmp is the tmpfs enter_ramfs moved across, the same one set_progress # smuggles its status through. WDOG_RELEASE=/tmp/.sysupgrade-wdog-release # Where UBI publishes its devices and volumes, and the inittab PID 1 parsed at # boot. Overridable so the test harness can point both at its sandbox. UBI_SYS=${UBI_SYS:-/sys/class/ubi} INITTAB=${INITTAB:-/etc/inittab} # Where MTD publishes its partitions, and the block devices the split NOR # layout is written through. Overridable for the same reason. MTD_SYS=${MTD_SYS:-/sys/class/mtd} # The NOR layout this run found before its first write (nor_split_devs), never # one handed in from outside: a stale value would decide where the image goes. nor_split_layout= MTDBLOCK=${MTDBLOCK:-/dev/mtdblock} MTDCHR=${MTDCHR:-/dev/mtd} # Where a handed-off flash phase writes its log, and the port that serves it. # The hand-off kills dropbear and majestic with everything else, so this is the # only way to watch a NAND flash without a serial cable -- see follow_start. # Set but empty turns it off; the test harness does that. FOLLOW_LOG=${FOLLOW_LOG-/tmp/upgrade.log} FOLLOW_PORT=${FOLLOW_PORT:-80} # Seconds the rootfs verify-mount may take before it is treated as unmountable. mount_wait=${mount_wait:-45} # Seconds the operator gets to Ctrl-C out of a run that will force a reboot. abort_wait=${abort_wait:-5} # Every upgrade this camera has been through, one line each, on the overlay so # that it outlives the flash and goes with `-n` like the rest of the settings. # Read back by --history; see record_transition. HISTORY_FILE=${HISTORY_FILE:-/etc/sysupgrade.history} # Route to firmware or builder manifest. A stock OpenIPC/firmware build # stamps BUILD_PLATFORM=${soc}_${variant} with variant in {lite,ultimate, # neo}. Anything else — a per-device override (${soc}_${variant}_${device}, # two or more underscores) or a non-stock variant (${soc}_wfbng etc.) — is # built by OpenIPC/builder. Pre-1.0.51 cameras lack BUILD_PLATFORM; fall # back to BUILD_OPTION (variant) alone. _plat=$(grep '^BUILD_PLATFORM=' /etc/os-release 2>/dev/null | cut -d= -f2) case "$_plat" in *_*_*) _manifest_repo=builder ;; *_lite|*_ultimate|*_neo) _manifest_repo=firmware ;; ?*) _manifest_repo=builder ;; *) _osr=$(grep '^BUILD_OPTION=' /etc/os-release 2>/dev/null | cut -d= -f2) case "${_osr:-lite}" in lite|ultimate|neo) _manifest_repo=firmware ;; *) _manifest_repo=builder ;; esac ;; esac MANIFEST_URL=${MANIFEST_URL:-https://openipc.github.io/${_manifest_repo}/manifest.flat} # One per run, and gone with it: the WebUI's update notice runs --list-builds on # page loads, with no lock, so it can overlap an upgrade, and 1.7 MB left in /tmp # after a listing is RAM the next flash does not have. MANIFEST_CACHE=/tmp/manifest.$$.flat trap 'rm -f "$MANIFEST_CACHE"' EXIT # What this run unpacked into /tmp, one name per line as tar -v lists it, so a # refusal can take back exactly that and nothing else (discard_unpacked). Made # by mktemp when the unpack starts, never a fixed name: a fixed one could be a # link planted to redirect the write, and a second sysupgrade that fails -- # before the lock, even -- would find the first run's list and delete that # run's images. Empty until this run has unpacked something. unpacked_list= # Where the image is unpacked: /tmp, or the idle media zone (stage_in_mmz). # The second phase is a fresh shell -- PID 1 after a hand-off, or the re-exec in # the RAM root without one -- that knows only what was exported, so the first # passes the zone on. Taken only where that path really is the stage's jffs2 # mount, so a stray variable in the environment cannot point a run elsewhere. stage_dir=/tmp [ -n "$handoff_stage_dir" ] && grep -q " $handoff_stage_dir jffs2 " /proc/mounts 2>/dev/null && stage_dir=$handoff_stage_dir mmz_stage_mounted= mmz_phram_ours= unset _osr _plat _manifest_repo # Every online fetch verifies the server's certificate against the bundle the # image ships at /etc/ssl/certs/ca-certificates.crt (libcurl's built-in default # here). --insecure puts -k back for one run, for a private mirror with a # self-signed certificate; nothing else ever sets it. GHSA-fjf7-9x3v-6mj6. curl_insecure= echo_c() { # 31 red, 32 green, 33 yellow, 34 blue, 35 magenta, 36 cyan, 37 white, 38 grey [ -z "$HASERLVER" ] && t="\e[1;$1m$2\e[0m" || t="$2" echo -e "$t" } die() { echo_c 31 "$1 Aborting." # Only reboot once flash has actually been touched. Past that point the # partition is already erased and rebooting into whatever landed is the # least-bad option; before it, nothing on the camera has changed, so a reboot # fixes nothing and costs plenty: # # - it destroys /tmp, and with it the log that says what went wrong; # - to anything watching the camera come back it is indistinguishable from # a successful upgrade. The WebUI drives this over /ws/upgrade and had no # other signal, so a download that timed out was reported to the user as # an update that worked (majestic-webui issue #120). # # The caller sees a non-zero exit either way; what changes is that a failure # before the first write now leaves the camera exactly as it found it. # # _ramfs_phase is the other way in. Once enter_ramfs has pivoted there is no # sane system left to hand back — the services, /etc and the rootfs an # operator would return to are all behind /mnt — so a failure there reboots # whatever it was doing, written or not. if [ "1" = "$flash_touched" ] || [ "1" = "$_ramfs_phase" ]; then reboot_system else # Nothing was written, so the camera is unchanged. The DRAM boot counter # is cleared only on the flash path below (and clearing it is harmless # even if no flash follows), so there is nothing to undo here. discard_unpacked # Before majestic is back: its media buffers come from the zone the # stage maps, and the zone's allocator does not know the stage is there. release_mmz_stage restore_resources # Only ever remove a lock this process owns. die() is reachable before # create_lock has run (get_system_info, called from print_sysinfo, can # raise one), and an unconditional rm there would unlink a CONCURRENT # sysupgrade's lock and let two upgrades run at once. # And only while it still names this process: a stale lock can be # taken over, so "it was mine" is not the same as "it is mine". [ "1" = "$lock_owned" ] && [ "$(cat $LOCK_FILE 2>/dev/null)" = "$$" ] && rm -f $LOCK_FILE restore_majestic # A WebUI that lost this log when majestic was stopped for its memory # never saw the line above; the camera's own log is where the page can # still find it, and syslogd is back by now. [ "1" = "$majestic_stopped" ] && [ "1" = "$web_update" ] && logger -p user.err -t sysupgrade "upgrade refused, nothing was written: $1" fi exit 1 } check_soc() { # Validate the downloaded image's stamped SoC ($1 = kernel-header ksoc or # rootfs hostname) against the BUILD identity $model, NOT the chip's # self-report. One published image (e.g. hi3516cv6xx) legitimately serves # dies that report a different id (hi3516cv610/608) via fw_printenv/ipcinfo. [ "1" = "$skip_soc" ] && echo "Skip SoC validation" && return 0 [ "$1" = "$model" ] && echo "SoC OK" && return 0 # The SoC this camera's own build was made for, before resolve_model_alias # renamed it: a builder device profile on an aliased SoC is built, named and # stamped for that one (see image_soc). [ -n "$1" ] && [ "$1" = "$platform_soc" ] && echo "SoC OK ($1, aliased to $model)" && return 0 die "Wrong SoC! (image '$1' != build '$model')" } compare_versions() { [ "1" = "$skip_ver" ] && echo "Skip version checking" && return 1 [ "$1" = "$2" ] && echo_c 32 "Same version, nothing to update" && return 0 echo_c 32 "New version, going to update" && return 1 } # The images a run writes, with the defaults do_update_kernel and # do_update_rootfs apply. Factored out because verify_rootfs and the size # pre-checks have to name the same files before those two run, and four copies # of the defaults would drift. rootfs_image() { [ -n "$rootfs_file" ] && { echo "$rootfs_file"; return 0; } [ "$ubi_layout" = "ubifs" ] && echo "$stage_dir/rootfs.ubifs.$image_soc" || echo "$stage_dir/rootfs.squashfs.$image_soc" } kernel_image() { [ -n "$kernel_file" ] && { echo "$kernel_file"; return 0; } [ "$ubi_layout" = "ubifs" ] && { echo "/tmp/fitImage.$image_soc"; return 0; } [ "$vendor" = "rockchip" ] && echo "$stage_dir/zboot.img.$image_soc" || echo "$stage_dir/uImage.$image_soc" } # Where the rootfs is written: the `rootfs` UBI volume on a UBI layout, the # partition of that name otherwise. rootfs_device() { [ -n "$ubi_layout" ] && { echo "$ubi_rootfs_dev"; return 0; } get_device "rootfs" } # The ubifs layout sizes its volumes to the image on every upgrade, so what a # rootfs has to fit is not its volume but the UBI space it shares with the # overlay: both volumes' blocks, less what the overlay needs afterwards -- a # floor UBIFS cannot mount below, plus the settings that are carried across # (none under -n). Bytes, or nothing when sysfs cannot say. UBIFS_OVERLAY_MIN_LEBS=24 ubifs_rootfs_room() { local r="$UBI_SYS/${ubi_rootfs_dev#/dev/}" d="$UBI_SYS/${ubi_data_dev#/dev/}" usable ebs debs=0 keep=0 usable=$(cat "$r/usable_eb_size" 2>&3) && ebs=$(cat "$r/reserved_ebs" 2>&3) || return 0 [ -n "$ubi_data_dev" ] && debs=$(cat "$d/reserved_ebs" 2>&3 || echo 0) [ "1" = "$clear_overlay" ] || keep=$((${overlay_kb:-0} * 1024)) # $1 replaces the settings estimate, for the caller that has measured them. [ -n "$1" ] && keep=$1 echo $(((ebs + debs - UBIFS_OVERLAY_MIN_LEBS) * usable - keep)) } # Refuse a rootfs that cannot fit where it is going, before anything is written. check_rootfs_fits() { local x=$1 room if [ "$ubi_layout" = "ubifs" ]; then [ -f "$x" ] || return 0 room=$(ubifs_rootfs_room) [ -n "$room" ] || return 0 [ "$(stat -c %s "$x" 2>/dev/null || echo 0)" -le "$room" ] && return 0 die "The rootfs image does not fit: $x is $(($(stat -c %s "$x") / 1024)) KB, the UBI device has $((room / 1024)) KB for it beside the settings. Nothing was written." fi check_image_fits "$x" "$(rootfs_device)" rootfs } # Write a ubifs-layout rootfs: the kernel is inside it, and the volumes are # sized to it. As PID 1 in the RAM root, with the old root released, so # nothing holds either volume: # 1. copy the overlay (the camera's settings) into RAM, unless -n # 2. remove rootfs_data, which frees every block it held # 3. resize rootfs to the image -- bigger or smaller, the UBI device decides # 4. write the image # 5. recreate rootfs_data on everything that is left # 6. put the settings back; UBIFS formats the empty volume as it mounts # A power cut between 2 and 6 costs the settings, not the camera: init mounts a # tmpfs overlay when rootfs_data is missing or empty. One between 3 and 4 # leaves no kernel to boot, as any rootfs write would. rewrite_ubifs() { local x=$1 ubi=${ubi_rootfs_dev%_*} rid=${ubi_rootfs_dev##*_} local name=${ubi#/dev/} bak=/tmp/overlay.tar mnt=/tmp/overlay.old local size=$(stat -c %s "$x" 2>/dev/null) [ -n "$size" ] || die "Cannot size $x." mark_live_flash_dirty rm -f "$bak" if [ -n "$ubi_data_dev" ]; then # Keeping the settings means having them in RAM before the volume # goes. Any failure here stops the run while nothing is written -- # die reboots into the system as it was; -n is the way to upgrade # without them. The archive, not df, is what has to fit back: it # counts sparse files at their full length, as extracting them will. if [ "1" != "$clear_overlay" ]; then mkdir -p "$mnt" mount -t ubifs -o ro "$name:rootfs_data" "$mnt" 2>&3 || die "Could not read the settings to carry them over. Nothing was written; -n upgrades without them." if ! tar -C "$mnt" -cf "$bak" . 2>&3; then busybox umount "$mnt" 2>&3 rm -f "$bak" die "Could not copy the settings into RAM. Nothing was written; -n upgrades without them." fi busybox umount "$mnt" 2>&3 local room=$(ubifs_rootfs_room 0) local need=$(( size + $(stat -c %s "$bak" 2>/dev/null || echo 0) )) [ -z "$room" ] || [ "$need" -le "$room" ] || { rm -f "$bak" die "The new rootfs and the settings ($(( need / 1024 )) KB together) do not fit the UBI device ($(( room / 1024 )) KB). Nothing was written; -n upgrades without the settings." } fi set_progress ubirmvol "$ubi" -N rootfs_data || die "Removing the settings volume failed." fi set_progress ubirsvol "$ubi" -n "$rid" -s "$size" || die "Resizing the rootfs volume to $size bytes failed." ubi_update "$ubi_rootfs_dev" "$x" || die "Writing $x to $ubi_rootfs_dev failed." set_progress ubimkvol "$ubi" -N rootfs_data -m || die "Recreating the settings volume failed; the camera boots with its settings in RAM until it is reset." overlay_recreated=1 if [ -f "$bak" ]; then mkdir -p "$mnt" if mount -t ubifs "$name:rootfs_data" "$mnt" 2>&3 && tar -C "$mnt" -xf "$bak" 2>&3; then sync echo "Settings carried over" else echo_c 33 "Warning: could not restore the settings; the camera starts from defaults." fi busybox umount "$mnt" 2>&3 rm -f "$bak" fi } # Refuse an image that cannot fit the partition it is bound for, before the # write rather than during it. # # flashcp makes this check itself -- and refuses without erasing anything -- but # its refusal went unread: the split path never looked at the status, so a run # that wrote nothing at all printed "RootFS updated to " and rebooted onto the old image (#2426). Reported against a camera # whose rootfs partition had been resized in the bootloader environment without # /proc/cmdline following (#2238), and reproduced on a lab gk7205v200. # # Checking here as well as honouring the status there buys the two things a # refusal at write time cannot: on a split layout the kernel is not committed # before the rootfs is found not to fit, and nothing has been erased, so die() # takes its no-reboot branch and leaves the camera exactly as it found it # instead of rebooting into what looks to a watcher like a successful upgrade. # # Fails OPEN. A size it cannot read -- no stat applet, a device that is not an # MTD partition, a layout /proc/mtd does not describe -- must never block a # flash that would have worked. The `|| die` on the write itself is the gate; # this is the early warning in front of it. # $1 bytes, $2 device, $3 what it is, $4 how to name the source in the message. # Taken as a size rather than a file because a combined image is measured as two # slices that do not exist as files until flash time. check_size_fits() { local fsz=$1 dev=$2 what=$3 src=$4 local psz=$(partition_size "$dev") [ -n "$fsz" ] && [ -n "$psz" ] || return 0 [ "$fsz" -le "$psz" ] && return 0 die "The $what image does not fit its partition: $src is $((fsz / 1024)) KB, $dev holds $((psz / 1024)) KB. Nothing was written." } check_image_fits() { local f=$1 [ -f "$f" ] || return 0 check_size_fits "$(stat -c %s "$f" 2>/dev/null)" "$2" "$3" "$f" } # firmware.bin is the split layout's one image (a uImage, then the squashfs), # measured here, before the pivot: everything do_update_firmware_split refuses, # refused while the camera is still whole. check_combined_fits() { local x=$1 ovl end blob [ -f "$x" ] || return 0 nor_split_image "$x" || die "$x is not a split-layout firmware.bin for this camera (a uImage, then a squashfs), or the flash is laid out neither as the split layout nor as fixed slots. Nothing was written." ovl=$(nor_split_overlay_at "$x") end=$(nor_split_end) nor_split_soc_ok "$x" || die "$x is built for $(nor_split_soc "$x"), this camera is a $model. Nothing was written." [ $(($(nor_split_base) + $(stat -c %s "$x"))) -le "$ovl" ] || die "$x runs past the end of its squashfs ($(printf 0x%x "$ovl")): not a firmware.bin as the build packs it. Nothing was written." blob=$(nor_split_blob_size) # Whether there are settings to carry: the hand-off takes the overlay's # mount away before the write (release_old_root), so the RAM phase cannot # tell from /proc/mounts any more. See do_update_firmware_split. [ -n "$(nor_split_overlay_mp)" ] && split_overlay_mounted=1 [ $((ovl + blob)) -le "$end" ] || die "$x and the settings do not fit the flash: the image ends at $(printf 0x%x "$ovl"), the settings need $((blob / 1024)) KB, the flash ends at $(printf 0x%x "$end"). Nothing was written; -n upgrades without the settings." } # Every image this run will write, judged against its partition while the camera # is still whole -- before the ramfs pivot, not after it. # # Inside the pivot a die() has to reboot: the shell, /etc and the services an # operator would come back to are all behind /mnt (#2416). A reboot with nothing # written is exactly the outcome die() goes out of its way to avoid, because to # anything watching the camera come back it is indistinguishable from a # successful upgrade (majestic-webui #120). Refusing here costs nothing: no # pivot yet, no write yet, services restored, and the log still in /tmp. # # This deliberately does not ask whether the write would have been SKIPPED for # being the same version. An image that cannot fit the partition it is bound for # means the layout and the build disagree, and the next build that is not the # same version would fail with flash half-written; saying so now, while the # camera is intact, is the more useful answer than "nothing to update". preflight_image_sizes() { if [ "1" = "$image_combined" ]; then check_combined_fits "$stage_dir/firmware.bin.$image_soc" free_staged_archive return 0 fi # A camera on the split layout has no kernel or rootfs slot to write: its # "kernel" and "rootfs" are what the running kernel carved out of the # current image, and a uImage or squashfs written into them would sit where # no boot looks for it. if { [ "1" = "$update_kernel" ] || [ "1" = "$update_rootfs" ]; } && nor_split_running; then die "This camera runs the split NOR layout and upgrades from firmware.bin (openipc.$model-nor-$(get_system_build)-split.tgz), not a separate kernel and rootfs. Nothing was written." fi # Present at all, first. An archive built for another camera unpacks # fine and holds nothing named for this $model; the image checks found # that only after the pivot, where every failure reboots -- a camera # restarted for an upgrade that never wrote a byte. Here die() still # restores it in place. [ "1" = "$update_kernel" ] && [ ! -f "$(kernel_image)" ] && die "No kernel image for this camera ($model): $(kernel_image) not found. Is the firmware built for $model?" [ "1" = "$update_rootfs" ] && [ ! -f "$(rootfs_image)" ] && die "No rootfs image for this camera ($model): $(rootfs_image) not found. Is the firmware built for $model?" [ "1" = "$update_kernel" ] && check_image_fits "$(kernel_image)" "$kernel_device" kernel [ "1" = "$update_rootfs" ] && check_rootfs_fits "$(rootfs_image)" [ "1" = "$update_rootfs" ] && check_rootfs_complete "$(rootfs_image)" # The UBIFS checks read the target volume out of sysfs, which the flash # phase may not have; the camera is also still whole here. if [ "1" = "$update_rootfs" ] && [ -n "$ubi_layout" ]; then check_rootfs_format "$(rootfs_image)" # The whole UBIFS verdict, SoC included: a UBIFS rootfs is written from # the handed-off PID 1, where any refusal is a reboot. is_ubifs "$(rootfs_image)" && verify_ubifs_rootfs "$(rootfs_image)" fi free_staged_archive need_handoff && check_handoff return 0 } do_update_kernel() { local x=$1 [ -z "$x" ] && x=$(kernel_image) echo_c 33 "\nKernel" echo "Update kernel from $x" [ ! -f "$x" ] && die "File $x not found" if [ "1" != "$skip_soc" ]; then # A FIT image (DTB magic d00dfeed, e.g. a NAND /boot/fitImage) carries # neither a uImage SoC name at offset 32 nor a uImage timestamp at # offset 8: the legacy probes would misfire (ksoc garbage -> false # "Wrong SoC!"; identical bogus values -> false "Same version", kernel # silently skipped). For a FIT, rely on the rootfs hostname SoC check # and always reflash. if [ "$(xxd -p -l 4 "$x" 2>/dev/null)" = "d00dfeed" ]; then echo "FIT kernel detected, skipping uImage SoC/version probe" local fsoc=$(fit_soc "$x") [ -n "$fsoc" ] && check_soc "$fsoc" else local ksoc=$(nor_split_soc "$x") if [ "$vendor" != "ingenic" ] && [ "$vendor" != "rockchip" ]; then check_soc "$ksoc" fi compare_versions "$kernel_version" "$(get_kernel_version "$x")" && return 0 fi fi check_image_fits "$x" "$kernel_device" kernel # A board with no separate "kernel" partition has kernel_device pointing at # the combined "firmware" one (get_system_info's fallback), which overlaps # the rootfs the camera is running from. A kernel write there is every bit # as live as a rootfs write, so -x cannot be honoured after it either. # mark_live_flash_dirty is itself a no-op on a camera not running from # flash, and marks the run touched in both cases. # A UBI kernel volume is never mounted, gluebi or not. if [ -z "$ubi_layout" ] && [ "/dev/" = "$(get_device "kernel")" ]; then mark_live_flash_dirty else mark_flash_touched fi # The success line below reads the version back off the DEVICE, so an # unchecked failure here does not merely claim success, it claims it while # printing the timestamp of the kernel that is still there. write_image "$x" "$kernel_device" || die "Writing $x to $kernel_device failed." echo_c 32 "Kernel updated to $(get_kernel_version "$kernel_device")" } # mount, bounded, read-only. The verify-mount does not always fail on a rootfs the # running kernel cannot read — it can block. Nothing is printed between the mount # and the flash write, so an unbounded mount that wedges is indistinguishable from # a dead tool: the upgrade stops with no error, taking any GUI driving it down too. # # KILL, not timeout's default TERM: this runs after `trap '' HUP INT TERM PIPE` # (#2025), and SIG_IGN survives exec, so mount ignores a TERM. busybox timeout # execs the program in its own place and only signals it, so with TERM the bound # never fired on a camera, and the run waited on the mount for as long as it took. # GNU timeout hid that on CI hosts: it catches TERM itself, so its child execs # with TERM at default. Ubuntu 26.04's uutils timeout does not, which is how it # surfaced. # # Best-effort, not a guarantee: a mount stuck in uninterruptible sleep outlives # even KILL. It bounds the realistic failures — a slow loop/probe, or a # decompressor the kernel lacks — which is what strands upgrades in practice. # # -t squashfs skips kernel fs autoprobing of what may be a garbage file; the error # text is returned rather than discarded, so the caller can print it. mount_rootfs() { if command -v timeout >/dev/null 2>&1; then timeout -s KILL "$mount_wait" mount -t squashfs -o ro "$1" "$2" 2>&1 else mount -t squashfs -o ro "$1" "$2" 2>&1 fi } # Whether a kernel flashed in this same run validates the SoC with no mount, and # so can stand in for the rootfs hostname check. do_update_kernel probes the SoC # out of a legacy uImage header and hands it to check_soc — but a FIT image (magic # d00dfeed) carries no such field, and check_soc is skipped outright for ingenic # and rockchip. In those cases a kernel in the run proves nothing about the SoC. # $1 = "kernel_follows" when the caller flashes a kernel unconditionally right # after verifying (do_update_firmware's split path, where update_kernel is unset # on a plain `sysupgrade -r`). soc_guarded_by_kernel() { local k= if [ "kernel_follows" = "$1" ]; then k="$stage_dir/uImage.$image_soc" elif [ "1" = "$update_kernel" ]; then k=$(kernel_image) else return 1 fi [ -f "$k" ] || return 1 # Mirrors do_update_kernel's own skips. [ "$vendor" = "ingenic" ] && return 1 [ "$vendor" = "rockchip" ] && return 1 [ "$(xxd -p -l 4 "$k" 2>/dev/null)" = "d00dfeed" ] && return 1 return 0 } # Is this rootfs shorter than it says it is? # # A squashfs records its own length at offset 0x28, so a truncated image is # provable here with no companion checksum, no mount and no decompressor. That # matters because the two guards either side of this one can both be absent at # once. An unpack that runs out of room in /tmp drops the image's .md5sum -- it # is packed after the file it describes, so it is the member that does not land # -- which makes `md5sum -c *.md5sum` verify only the kernel and exit 0. And the # unmountable branch in verify_rootfs is deliberately non-fatal, because a # running kernel may lack the new image's decompressor. flashcp then writes the # short file and verifies it happily: it compares the file against the flash, # not the filesystem. Reported as a camera that flashed 4.19 MB of a 4.70 MB # rootfs, called it a success, and rebooted into a filesystem the kernel could # not mount. # # Padding makes the test "shorter than", never "not equal to": repack pads the # artifact to a 4 KB boundary (4700598 -> 4702208 on the image measured). # # Refuses only what it can prove. No squashfs magic, an unreadable header, a # length that will not parse -- all mean no opinion, so a non-squashfs artifact # keeps today's behaviour, and so does a big-endian host, where the magic reads # back byte-swapped and simply fails to match. `[ -f ]` because `stat -c %s` # reports 0 for a block device, which would otherwise read as short. squashfs_short() { local f=$1 lo hi have [ -f "$f" ] || return 1 [ "$(od -An -tx4 -j0 -N4 "$f" 2>&3 | awk '{print $1; exit}')" = "73717368" ] || return 1 lo=$(od -An -tu4 -j40 -N4 "$f" 2>&3 | awk '{print $1; exit}') hi=$(od -An -tu4 -j44 -N4 "$f" 2>&3 | awk '{print $1; exit}') [ "$hi" = "0" ] || return 1 [ -n "$lo" ] && [ "$lo" -gt 0 ] 2>&3 || return 1 have=$(stat -c %s "$f" 2>&3) || return 1 [ -n "$have" ] || return 1 [ "$have" -lt "$lo" ] } # Refuse a rootfs that is provably incomplete, before anything is written. # # Called from preflight_image_sizes, so the normal path refuses on the # still-normal system -- services handed back, no reboot, message on the # operator's terminal -- and again from verify_rootfs, which is the one place # every rootfs candidate passes through, including the slice dd'd out of a # combined image inside the pivot. check_rootfs_complete() { local f=$1 have lo squashfs_short "$f" || return 0 have=$(stat -c %s "$f" 2>&3) lo=$(od -An -tu4 -j40 -N4 "$f" 2>&3 | awk '{print $1; exit}') die "Incomplete rootfs: $f is $((have / 1024)) KB but its superblock says $((lo / 1024)) KB. Nothing was written. Download it again -- a truncated image loses its .md5sum with it." } # The SoC a FIT kernel was built for, or nothing. # # A legacy uImage carries it in its name field (do_update_kernel reads it at # offset 32); a FIT has none of its own, so the NAND FIT is described as # "OpenIPC " (board//nand-fit.its, stamped at build time). The # description is in the FIT's first few hundred bytes. A FIT without one # answers nothing, and no conclusion is drawn from that. fit_soc() { [ -f "$1" ] || return 0 [ "$(xxd -p -l 4 "$1" 2>&3)" = "d00dfeed" ] || return 0 dd if="$1" bs=4096 count=1 2>&3 | tr -c 'a-zA-Z0-9_ ' '\n' | awk '$1 == "OpenIPC" && NF == 2 { print $2; exit }' } # The BUILD_PLATFORM a NAND FIT was built with (its root openipc,platform, # "BUILD_PLATFORM="), or nothing for a FIT from before it carried one. # The same witness as fit_soc, for the device profile: the UBIFS rootfs beside # it cannot say which profile it is without being mounted. fit_platform() { [ -f "$1" ] || return 0 [ "$(xxd -p -l 4 "$1" 2>&3)" = "d00dfeed" ] || return 0 # Not anchored: the FDT property header (length, name offset) sits right # before the value, and the name offset's last byte can be printable. dd if="$1" bs=4096 count=1 2>&3 | tr -c 'A-Za-z0-9_=-' '\n' | sed -n 's/.*BUILD_PLATFORM=\([A-Za-z0-9_-][A-Za-z0-9_-]*\)$/\1/p' | head -1 } # A UBIFS image: the superblock node's magic (0x06101831, stored little-endian) # at offset 0. is_ubifs() { [ "$(xxd -p -l 4 "$1" 2>&3)" = "31181006" ] } # The rootfs format has to be the one this camera's kernel command line mounts: # a squashfs written into the volume of a root=ubi0:rootfs camera, or a UBIFS # into a ubiblock one, flashes cleanly and never mounts again. The kernel's # rootfstype is fixed by the bootloader environment, which no upgrade rewrites. check_rootfs_format() { [ -n "$ubi_layout" ] || return 0 if [ "$ubi_layout" = "ubifs" ] && ! is_ubifs "$1"; then die "This camera boots a UBIFS rootfs, and $1 is not one. Nothing was written." fi if [ "$ubi_layout" = "ubiblock" ] && is_ubifs "$1"; then die "This camera boots a squashfs rootfs through ubiblock, and $1 is a UBIFS image. Nothing was written." fi return 0 } # What can be known about a UBIFS rootfs without mounting it -- which needs UBI # under it, so a loop device will not do. # # Its superblock pins the LEB size the image was made for, and UBIFS refuses to # mount on a volume whose LEBs are any other size: the classic way to get this # wrong is a 2 KiB-page image on a 4 KiB-page chip, and it fails only after the # reboot. So that is checked against the volume it is bound for. # # What it cannot give is the SoC stamp or the version: both live in files the # image stores compressed. So the SoC rests on the same evidence an unmountable # squashfs does (see verify_rootfs), and an image is never skipped as the same # version -- the kernel FIT carries no version either. check_ubifs_image() { local x=$1 dev want leb ntype ntype=$(od -An -tu1 -j20 -N1 "$x" 2>&3 | awk '{print $1; exit}') [ "$ntype" = "6" ] || die "$x is not a UBIFS image: its first node is not a superblock. Nothing was written." leb=$(od -An -tu4 -j36 -N4 "$x" 2>&3 | awk '{print $1; exit}') dev=$(rootfs_device) want=$(cat "$UBI_SYS/${dev#/dev/}/usable_eb_size" 2>&3) [ -n "$leb" ] && [ -n "$want" ] || return 0 [ "$leb" = "$want" ] && return 0 die "$x was made for $leb-byte LEBs; $dev has $want. It would not mount. Nothing was written." } verify_ubifs_rootfs() { local x=$1 rootfs_version= check_ubifs_image "$x" echo "UBIFS rootfs; its SoC and version cannot be read without mounting it." # The FIT that ships beside it says which SoC the pair was built for -- # the one witness a UBIFS rootfs has. Taken whether or not this run # writes the kernel: the package carries it either way. local fsoc=$(fit_soc "$(kernel_image)") if [ "1" != "$skip_soc" ] && is_device_platform "$system_platform" && [ "1" != "$device_pinned" ]; then # Named for this device by its download, or by the FIT beside it -- # without either, every local archive for a profile camera needed # --force_soc, which also gives up the SoC check below. Compared past # the SoC token, as check_platform does. local fplat=$(fit_platform "$(kernel_image)") [ -n "$fplat" ] || die "Cannot verify that a UBIFS rootfs is built for this camera's device profile ('$system_platform'): the FIT kernel beside it names no platform. Nothing was written. Pass --force_soc if you are sure it is." [ "${fplat#*_}" = "${system_platform#*_}" ] || die "Wrong platform! The FIT kernel beside this UBIFS rootfs was built for '$fplat'; this camera runs '$system_platform', and the image would drop its device profile. Nothing was written. Pass --force_soc to flash it anyway." echo "Device profile from the FIT kernel beside it: $fplat" fi if [ "1" = "$skip_soc" ]; then echo_c 33 "Skipping the rootfs SoC pre-check (SoC validation disabled)." elif [ -n "$fsoc" ]; then echo "SoC from the FIT kernel beside it: $fsoc" check_soc "$fsoc" elif [ "1" = "$remote_update" ] && [ -z "$archive" ]; then # A download, named for this camera's model. A local archive is # named by whoever made it, so it does not count. echo_c 33 "The image is pinned to '$model' by its artifact name." else die "Cannot verify the SoC of a UBIFS rootfs: no FIT kernel beside it names one. Nothing was written. Pass --force_soc if you are sure it is built for '$model'." fi } # Read the candidate rootfs's SoC stamp and version by loop-mounting it. # # This is a PRE-FLIGHT CHECK ONLY: the image is written with flashcp, which writes # the partition raw and never needs the mount. So it must run BEFORE anything is # written to flash, and it must not reject an image that would have flashed fine. verify_rootfs() { local x=$1 [ -z "$x" ] && x=$(rootfs_image) echo_c 33 "\nVerify" echo "Verifying rootfs from $x" [ ! -f "$x" ] && die "File $x not found" check_rootfs_format "$x" if is_ubifs "$x"; then verify_ubifs_rootfs "$x" "$2" return 0 fi check_rootfs_complete "$x" local y=/tmp/rootfs local loop= local err= local verdict= rootfs_version= # read_candidate_build already tried this mount before the pivot; one that # failed there fails here too, and is not worth waiting out twice. [ "1" = "$rootfs_unmountable" ] && err=$rootfs_mount_err if [ "1" != "$rootfs_unmountable" ] && mkdir -p "$y" && loop=$(losetup -f) && err=$(losetup "$loop" "$x" 2>&1) && err=$(mount_rootfs "$loop" "$y"); then rootfs_version=$(get_system_version "$y") check_soc "$(head -1 $y/etc/hostname | cut -d- -f2)" if ! verdict=$(check_platform "$y"); then umount "$y" losetup -d "$loop" 2>/dev/null rm -rf "$y" die "$verdict" fi [ -n "$verdict" ] && echo "$verdict" compare_versions "$system_version" "$rootfs_version" && exit_update=1 umount "$y" else # Unmountable is not a defect in the image. It usually means the RUNNING # kernel lacks the squashfs decompressor the NEW image uses (commonly XZ) — # a property of the kernel being replaced, and irrelevant the moment the new # one boots. flashcp writes the partition raw and never needs the mount, so # aborting here rejects an image that would have flashed fine. # A KILL from mount_rootfs can land after the kernel finished the mount # but before mount exited, so a failed mount may still be attached; it # has to go before any path below detaches the loop under it. umount "$y" 2>/dev/null echo_c 33 "\nCannot mount $x to verify it." [ -n "$err" ] && echo_c 33 " $err" echo_c 33 "The running kernel may lack its squashfs decompressor; the image itself may be fine." # The rootfs hostname is one of two SoC witnesses. The other is the uImage # header of a kernel flashed in this same run, which check_soc reads with no # mount — but that witness does not always exist (FIT kernels carry no SoC # field; check_soc is skipped for ingenic/rockchip), so it cannot simply be # assumed. With no witness at all, a downloaded artifact is still pinned to # $model by the name it must have to be found (rootfs.squashfs.$model, md5 # verified), while a hand-picked --rootfs= file is pinned by nothing — and # writing an unverified rootfs for a foreign SoC bricks the device. That is # the case worth refusing, and refusing costs nothing here: no flash yet. if [ "1" = "$skip_soc" ]; then echo_c 33 "Skipping the rootfs version/SoC pre-check (SoC validation disabled)." elif is_device_platform "$system_platform" && [ "1" != "$device_pinned" ]; then losetup -d "$loop" 2>/dev/null rm -rf "$y" die "Cannot verify that an unmountable rootfs is built for this camera's device profile ('$system_platform'). Nothing was written. Pass --force_soc if you are sure it is." elif soc_guarded_by_kernel "$2"; then echo_c 33 "Skipping the rootfs version/SoC pre-check; the SoC is validated from the kernel image header instead." elif [ "1" = "$remote_update" ]; then echo_c 33 "Skipping the rootfs version/SoC pre-check; the image is pinned to '$model' by its artifact name." else losetup -d "$loop" 2>/dev/null rm -rf "$y" die "Cannot verify the SoC of an unmountable rootfs, and no kernel in this run carries a SoC to check it against. Flash the matching kernel in the same run, or pass --force_soc." fi fi [ -n "$loop" ] && losetup -d "$loop" 2>/dev/null rm -rf "$y" return 0 } do_update_rootfs() { local x=$1 [ -z "$x" ] && x=$(rootfs_image) echo_c 33 "\nRootFS" echo "Update rootfs from $x" [ ! -f "$x" ] && die "File $x not found" [ "1" = "$exit_update" ] && return 0 local dev=$(rootfs_device) if [ "$ubi_layout" = "ubifs" ]; then check_rootfs_fits "$x" rewrite_ubifs "$x" echo_c 32 "RootFS updated to ${rootfs_version:-unknown}" return 0 fi check_image_fits "$x" "$dev" rootfs mark_live_flash_dirty # ${rootfs_version} is what verify_rootfs read out of the CANDIDATE FILE, so # an unchecked failure here prints the version the operator asked for -- # indistinguishable from the upgrade that did not happen. write_image "$x" "$dev" || die "Writing $x to $dev failed." echo_c 32 "RootFS updated to ${rootfs_version:-unknown}" } # --- Split NOR layout ------------------------------------------------------- # # firmware.bin -- the uImage, then the squashfs at the next 64 KiB boundary -- # is written whole where the board's "firmware" partition starts (0x50000, or # after a larger boot area), where every U-Boot loads the kernel from. The # kernel's device tree has one "firmware" partition from there to the end of # the chip, which it divides at boot (mtdsplit), whatever the bootloader # passes: "kernel" is the uImage, "rootfs" the squashfs, "rootfs_data" the rest # from the squashfs end rounded up to 64 KiB. Nothing has a slot of its own, so # an upgrade that changes either image moves where the overlay starts. # # The settings cross that boundary as a blob written at the new overlay start: # "OPENIPC-OVL1", the length in 12 decimal digits, then a gzipped tar of the # overlay. /init finds it before it mounts rootfs_data, restores it into a # freshly erased overlay, and only then does the camera have its settings back. # A power cut before that leaves the blob where it is, and the next boot does it # again. # The kernel image of a split firmware.bin, in whole 64 KiB blocks: the uImage # header (64 bytes, magic 0x27051956) plus its data size (bytes 12..15, # big-endian). The squashfs starts right after it. nor_split_blocks() { [ "$(xxd -p -l 4 "$1" 2>&3)" = "27051956" ] || return 1 local size=$((0x$(xxd -p -l 4 -s 12 "$1" 2>&3))) [ "$size" -gt 0 ] || return 1 echo $(((64 + size + 65535) / 65536)) } # The SoC a uImage was built for: the last word of its name field, # "Linux--", or "Linux-3.4.43-Goke-gk7102" as the gk710x kernel # names itself -- whose third word, "Goke", no camera is. nor_split_soc() { od -j 32 -N 32 -S 1 -A n "$1" 2>&3 | tr -d ' ' | sed 's/.*-//' } # "mtdN offset size name" for every partition, in device order. Kernels from # 4.1 on give each partition's offset in sysfs. The older ones -- 3.18 on the # Hi3516CV300 and Hi3519V101 families and on Hi3536CV100, 3.0 and 3.4 on the # oldest SoCs -- give only names and sizes, and then the offsets follow from # the order every OpenIPC NOR layout has: each partition starts where the one # before it ends, except "firmware". That one starts where the kernel does -- # the fixed slots' alias over kernel and rootfs, listed after them -- or, on # the split layout, where the next partition would, holding kernel, rootfs and # rootfs_data after it; either way it moves nothing on. mtd_geometry() { local n d off size name next=0 kern= for n in $(ls "$MTD_SYS" 2>&3 | sed -n 's/^mtd\([0-9][0-9]*\)$/\1/p' | sort -n); do d=$MTD_SYS/mtd$n size=$(cat "$d/size" 2>&3) && name=$(cat "$d/name" 2>&3) || continue # Not the flash: the media zone mapped by stage_in_mmz is an MTD too, # with no offset, and laid out after rootfs_data it made the chip 24 MB # longer and every image "not a split-layout firmware.bin". [ "$(cat "$d/type" 2>&3)" = ram ] && continue if [ -r "$d/offset" ]; then off=$(cat "$d/offset") elif [ "$name" = firmware ]; then off=${kern:-$next} else off=$next fi [ "$name" = kernel ] && kern=$off [ "$name" = firmware ] || next=$((off + size)) echo "mtd$n $off $size $name" done } # "mtdN offset size" for the partitions that together cover the firmware area # to the end of the chip without overlapping: the "firmware" partition alone # when it reaches the end (the split layout), else kernel, rootfs and # rootfs_data back to back (fixed slots, on the way to the split layout). The # area starts where the board puts it; nothing here assumes where. From sysfs, which the # ramfs pivot carries along; nothing printed means neither shape is there. # # Offsets worked out from the order (mtd_geometry) are only taken when the # partitions hold what they are named for: a uImage at the start of "kernel" # and a squashfs at the start of "rootfs". nor_split_devs() { local geo dev o z name chip=0 derived= # Once the write has started, the partitions no longer hold what they did # (the old rootfs slot is in the middle of the new image): the upgrade # takes the layout once, before it, and every write and read-back goes # by that (do_update_firmware_split). if [ -n "$nor_split_layout" ]; then echo "$nor_split_layout" return 0 fi geo=$(mtd_geometry) [ -n "$geo" ] || return 1 firmware_dev= kernel_dev= rootfs_dev= rootfs_data_dev= while read -r dev o z name; do [ $((o + z)) -gt "$chip" ] && chip=$((o + z)) # one offset not from sysfs is enough to want the images checked [ -r "$MTD_SYS/$dev/offset" ] || derived=1 case "$name" in firmware) [ -n "$firmware_dev" ] || firmware_dev=$dev firmware_off=$o firmware_size=$z ;; kernel) [ -n "$kernel_dev" ] || kernel_dev=$dev kernel_off=$o kernel_size=$z ;; rootfs) [ -n "$rootfs_dev" ] || rootfs_dev=$dev rootfs_off=$o rootfs_size=$z ;; rootfs_data) [ -n "$rootfs_data_dev" ] || rootfs_data_dev=$dev rootfs_data_off=$o rootfs_data_size=$z ;; esac done <&3)" = "27051956" ] && [ "$(xxd -p -l 4 "$MTDCHR${rootfs_dev#mtd}" 2>&3)" = "68737173" ] || return 1 fi if [ -n "$firmware_dev" ] && [ "$firmware_off" -gt 0 ] && [ $((firmware_off + firmware_size)) -eq "$chip" ]; then echo "$firmware_dev $firmware_off $firmware_size" return 0 fi [ -n "$kernel_dev" ] && [ -n "$rootfs_dev" ] && [ -n "$rootfs_data_dev" ] || return 1 [ "$kernel_off" -gt 0 ] && [ "$rootfs_off" -eq $((kernel_off + kernel_size)) ] && [ "$rootfs_data_off" -eq $((rootfs_off + rootfs_size)) ] && [ $((rootfs_data_off + rootfs_data_size)) -eq "$chip" ] || return 1 echo "$kernel_dev $kernel_off $kernel_size" echo "$rootfs_dev $rootfs_off $rootfs_size" echo "$rootfs_data_dev $rootfs_data_off $rootfs_data_size" } # Where the kernel will start rootfs_data for image $1, as an absolute flash # offset: the squashfs at the uImage rounded up to 64 KiB, plus its bytes_used # (superblock offset 40, little-endian) rounded up to 64 KiB. nor_split_overlay_at() { local blocks le used blocks=$(nor_split_blocks "$1") || return 1 [ "$(xxd -p -l 4 -s $((blocks * 65536)) "$1" 2>&3)" = "68737173" ] || return 1 le=$(xxd -p -l 4 -s $((blocks * 65536 + 40)) "$1" 2>&3 | sed 's/\(..\)\(..\)\(..\)\(..\)/\4\3\2\1/') used=$((0x${le:-0})) [ "$used" -gt 0 ] || return 1 echo $(($(nor_split_base) + blocks * 65536 + (used + 65535) / 65536 * 65536)) } # Where the area nor_split_devs covers starts: where firmware.bin goes. nor_split_base() { nor_split_devs | awk 'NR == 1 { print $2 }' } # The end of the area nor_split_devs covers. nor_split_end() { nor_split_devs | awk 'END { if (NR) print $2 + $3 }' } # Write $1 at absolute offset $2 through the partitions nor_split_devs lists, # 64 KiB blocks through mtdblock (which erases each block it writes). # # mtdblock writes into a cache of one erase block and puts it on the chip when a # write lands in another block, on a flush, or on the device's last close. On # the way from fixed slots the old root squashfs still holds "rootfs" open, so # dd's close is not the last, and a plain sync does not flush it: the last block # written -- the settings blob -- stayed in RAM and was gone at the reboot # (GK7205V300). dd's fsync sends the flush. The result is then read back past # mtdblock, through the MTD character device, so a write that did not reach the # chip fails here instead of being reported done. nor_write_at() { local f=$1 at=$2 len dev off size from to len=$(stat -c %s "$f") || return 1 nor_split_devs | while read -r dev off size; do from=$((at > off ? at : off)) to=$((at + len < off + size ? at + len : off + size)) [ "$from" -lt "$to" ] || continue dd if="$f" of="$MTDBLOCK${dev#mtd}" bs=65536 \ skip=$(((from - at) / 65536)) seek=$(((from - off) / 65536)) \ count=$(((to - from + 65535) / 65536)) conv=notrunc,fsync 2>&3 || exit 1 done || return 1 sync [ "$(nor_read_at "$at" "$len" | md5sum | cut -d' ' -f1)" = "$(md5sum < "$f" | cut -d' ' -f1)" ] } # $2 bytes from absolute offset $1, read through the MTD character devices: # what is on the chip, past any cache mtdblock keeps. nor_read_at() { local at=$1 left=$2 dev off size n nor_split_devs | while read -r dev off size; do [ "$left" -gt 0 ] || break [ "$at" -ge "$off" ] && [ "$at" -lt $((off + size)) ] || continue n=$((off + size - at)); [ "$n" -gt "$left" ] && n=$left dd if="$MTDCHR${dev#mtd}" bs=65536 skip=$(((at - off) / 65536)) \ count=$((((at - off) % 65536 + n + 65535) / 65536)) 2>&3 | tail -c +$(((at - off) % 65536 + 1)) | head -c "$n" at=$((at + n)); left=$((left - n)) done } # Running from the split layout: one "firmware" partition from the firmware # area to the end of the chip, the kernel's own partitions inside it. nor_split_running() { [ "$(nor_split_devs | wc -l)" -eq 1 ] } # Whether $1 answers with a 2xx, redirects followed. url_exists() { curl --connect-timeout 30 -m 60 -sIL -o /dev/null -w '%{http_code}' \ $(tls_opts "$1") "$1" 2>&3 | grep -q '^2' } # The -split package's URL for a plain firmware package's, when the release # carries one for this camera, else the URL as it is: # openipc.-nor-.tgz becomes ...--split.tgz, and a # builder device's __-nor.tgz ...-nor-split.tgz. A board # built for the split layout publishes it, whatever its SoC; any bootloader # boots it, as the kernel sits where the fixed slots put it and finds its own # partitions. nor_split_url() { local split split=$(echo "$1" | sed -e 's|\(/openipc\.[^/]*-nor-[a-z0-9]*\)\.tgz$|\1-split.tgz|' \ -e 's|\(/[a-z0-9]*_[a-z0-9]*_[^/]*-nor\)\.tgz$|\1-split.tgz|') if [ "$(pkg_flash)" = nor ] && [ "$split" != "$1" ] && url_exists "$split"; then echo "$split" else echo "$1" fi } # Where the jffs2 overlay on rootfs_data is mounted, if it is. nor_split_overlay_mp() { awk -v d="$(get_device rootfs_data | sed 's|/dev/mtd|/dev/mtdblock|')" '$1 == d { print $2; exit }' /proc/mounts } # The settings blob this run will write, in bytes: the header, the gzipped tar # of the overlay, whole 64 KiB blocks; one block for -n. nor_split_blob_size() { local mp n=0 mp=$(nor_split_overlay_mp) if [ "1" != "$clear_overlay" ] && [ -n "$mp" ]; then n=$(tar -C "$mp" -cf - . 2>&3 | gzip | wc -c) fi echo $(((24 + n + 65535) / 65536 * 65536)) } # The image is one for the split layout, for a camera whose flash is laid out # as either layout. # check_soc's question, asked of a firmware.bin's uImage. nor_split_soc_ok() { local s=$(nor_split_soc "$1") [ "$s" = "$model" ] || { [ -n "$s" ] && [ "$s" = "$platform_soc" ]; } && return 0 [ "1" = "$skip_soc" ] && echo "Skip SoC validation (image '$s', build '$model')" && return 0 return 1 } nor_split_image() { [ -n "$(nor_split_devs)" ] && nor_split_overlay_at "$1" >/dev/null } do_update_firmware_split() { local x=$1 ovl end base blob=/tmp/overlay.blob tgz=/tmp/overlay.tgz mp nor_split_layout=$(nor_split_devs) [ -n "$nor_split_layout" ] || die "The flash layout is neither the split one nor fixed slots this upgrade can turn into it. Nothing was written." base=$(nor_split_base) echo "Split NOR layout: firmware.bin at $(printf 0x%x "$base"), the kernel divides it at boot" nor_split_soc_ok "$x" || die "$x is built for $(nor_split_soc "$x"), this camera is a $model. Nothing was written." ovl=$(nor_split_overlay_at "$x") || die "$x is not a uImage followed by a squashfs. Nothing was written." [ $((base + $(stat -c %s "$x"))) -le "$ovl" ] || die "$x runs past the end of its squashfs. Nothing was written." end=$(nor_split_end) [ -n "$end" ] || die "The flash layout is neither the split one nor fixed slots this upgrade can turn into it. Nothing was written." rm -f "$blob" "$tgz" quiesce_overlay mp=$(nor_split_overlay_mp) # Handed off, the overlay is no longer mounted: release_old_root let go of # everything behind /mnt, the overlay with it. Reading /proc/mounts alone # then took the camera for one without settings and staged an empty blob, # which the first boot honours by starting from defaults -- every NOR # upgrade since the hand-off (#2648). Nothing holds rootfs_data any more, # so mount it read-only here and copy the settings from that, as # rewrite_ubifs does on UBI. Refusing costs a reboot with nothing written; # going on would cost the settings. if [ "1" != "$clear_overlay" ] && [ -z "$mp" ] && [ "1" = "$split_overlay_mounted" ]; then mp=/tmp/overlay.old mkdir -p "$mp" && mount -t jffs2 -o ro "$(get_device rootfs_data | sed 's|/dev/mtd|/dev/mtdblock|')" "$mp" 2>&3 || die "Could not read the settings to carry them over. Nothing was written; -n upgrades without them." overlay_mounted_here=1 fi if [ "1" != "$clear_overlay" ] && [ -n "$mp" ]; then tar -C "$mp" -cf - . 2>&3 | gzip > "$tgz" || die "Could not copy the settings into RAM. Nothing was written; -n upgrades without them." # Read-only, but still a filesystem on the flash about to be # rewritten: let go of it before the write, or do not write. if [ "1" = "$overlay_mounted_here" ]; then busybox umount "$mp" 2>&3 || die "Could not release the settings partition after copying it. Nothing was written." rmdir "$mp" 2>&3 fi else # Length 0: the first boot erases the overlay and restores nothing. : > "$tgz" fi printf 'OPENIPC-OVL1%012d' "$(stat -c %s "$tgz")" > "$blob" cat "$tgz" >> "$blob" && rm -f "$tgz" # To a whole 64 KiB block (busybox truncate has no %SIZE). local bsz=$(stat -c %s "$blob") head -c $(((65536 - bsz % 65536) % 65536)) /dev/zero >> "$blob" [ $((ovl + $(stat -c %s "$blob"))) -le "$end" ] || die "The new firmware and the settings do not fit the flash ($(( (ovl + $(stat -c %s "$blob") - base) / 1024 )) KB of $(( (end - base) / 1024 )) KB). Nothing was written; -n upgrades without the settings." mark_live_flash_dirty echo "Writing $x at $(printf 0x%x "$base")" # Once more before giving up: the image is still in RAM, and a failure here # ends in a reboot onto whatever part of it reached the flash. nor_write_at "$x" "$base" || { echo_c 33 "The flash does not read back as the image; writing it again." nor_write_at "$x" "$base" } || die "Writing $x failed: the flash does not read back as the image." nor_write_at "$blob" "$ovl" || die "Writing the settings at $(printf 0x%x "$ovl") failed; the camera starts from defaults." rm -f "$blob" overlay_recreated=1 echo "Settings staged at $(printf 0x%x "$ovl") for the first boot" echo_c 32 "Firmware updated" } do_update_firmware() { local x=$stage_dir/firmware.bin.$image_soc echo_c 33 "\nFirmware (split NOR image)" echo "Update firmware from $x" [ ! -f "$x" ] && die "File $x not found" nor_split_image "$x" || die "$x is not a split-layout firmware.bin for this camera. Nothing was written." do_update_firmware_split "$x" } # Stop the kernel writing to the overlay before erasing the medium under it. # # do_wipe_overlay hands flash_eraseall a partition that is still a mounted, RW # jffs2 with its garbage-collect thread running, so two agents end up erasing and # writing the same medium at once. Both notice. The GC thread walks a filesystem # that is being deleted under it -- "Node header CRC failed ... {ffff,ffff,...}", # "Node totlen on flash (0xffffffff) != totlen from node ref", and inodes lost # outright to "no data nodes found for ino #20" -- while jffs2's own erase path # reads back a block it has just erased and finds someone else's cleanmarker in # it: "Newly-erased block contained word 0x20031985" (magic 0x1985, nodetype # 0x2003 -- the marker `flash_eraseall -j` writes). # # The same applies to a ubifs overlay on a NAND camera -- a background thread # and a live medium being erased under it -- so both spellings are handled below. # # The overlay cannot be unmounted. It is the upperdir AND workdir of the # overlayfs that is still root for init(1), majestic, dropbear and getty, so that # superblock holds a reference to this mount for as long as they exist: umount is # EBUSY by construction, and umount -l only detaches it from our namespace while # the superblock and the GC thread live on. pivot_root does not help and is not # meant to -- it moved THIS script's root off the doomed partition, nobody # else's, which is why enter_ramfs leaves the old root mounted at /mnt. # # Remounting it read-only is enough, and does work: jffs2_remount_fs() stops the # garbage-collect thread on the way to ro, which leaves flash_eraseall as the # only thing touching the medium. Anything still writing through the overlayfs # gets EROFS instead of reaching flash -- the outcome we want a second before a # reboot that is going to wipe it anyway. # # Measured on an hi3516ev300 with an overlay carrying real write history: 3 of 4 # runs logged jffs2 errors before this, 0 of 5 after. An empty overlay is silent # either way, which is why a bench that has just been reset never shows it. # # The mount point is looked up rather than assumed: after the pivot the overlay # is at /mnt/overlay, and on the in-place fallback it is still at /overlay. quiesce_overlay() { local blk=$(get_device "rootfs_data" | sed 's|/dev/mtd|/dev/mtdblock|') # Two spellings for the same partition, because init mounts it two ways: a # jffs2 camera gets /dev/mtdblockN, a ubifs one gets ubi0:rootfs_data (see # general/overlay/init). Matching only the first would quietly no-op on every # UBI board, where the race is the same and the cure is the same -- # ubifs_remount_ro() stops the background thread and commits, just as # jffs2_remount_fs() stops the GC thread. local mp=$(awk -v d="$blk" ' $1 == d || $1 ~ /^ubi[0-9]+:rootfs_data$/ { print $2; exit }' /proc/mounts) # Nothing mounted on it -- an overlay this camera never brought up, or a root # that put its upper layer in tmpfs instead. The erase is unopposed already. [ -n "$mp" ] || return 0 sync # A refusal is a warning, not a die: erasing it live is exactly what this # did before, so falling back to that is never worse than the status quo. if mount -o remount,ro "$mp" 2>&3; then echo "Overlay $mp remounted read-only" else echo_c 33 "Could not remount $mp read-only; erasing it live." fi } do_wipe_overlay() { echo_c 33 "\nOverlayFS" echo "Erase overlay partition" [ "$flash_type" = "nand" ] || jffs2="-j" quiesce_overlay mark_live_flash_dirty # Same reason the flashcp writes are checked: an erase that failed and was # not noticed is a `firstboot` that reports the camera reset and leaves the # old overlay -- including the shadow entry that decides whether the camera # is claimed -- exactly where it was. local dev=$(get_device "rootfs_data") # A UBI rootfs_data that no MTD (gluebi) name reaches: truncate the volume. # An empty volume is what ubinize ships, and UBIFS formats it on the next # mount. Reaching here with it mounted means PID 1 was handed off and the # old root released first (need_handoff), so the volume is free. if [ -n "$ubi_layout" ] && [ "$dev" = "/dev/" ]; then [ -n "$ubi_data_dev" ] || die "No rootfs_data volume to wipe." set_progress ubiupdatevol -t "$ubi_data_dev" || die "Truncating the overlay volume $ubi_data_dev failed." return 0 fi set_progress flash_eraseall $jffs2 "$dev" || die "Erasing the overlay partition $dev failed." } # curl options for one URL. An https URL may redirect (a GitHub release asset # does), but never down to plain http: a verified connection that hands the # transfer to an unverified one has verified nothing. tls_opts() { case "$1" in https://*) echo "--proto-redir =https" ;; esac echo "$curl_insecure" } # The clock is part of certificate verification: a certificate is valid between # two dates, and a camera with no RTC boots at its build stamp (or fake-hwclock's # last checkpoint), so behind a network that blocks NTP the fetch fails with # BADCERT_FUTURE. That failure is what made `-k` look necessary (majestic-webui # #44, #121). Take the Date header of a plain-HTTP HEAD instead, as htpdate # does, and step the clock FORWARD only: a spoofed header can move the clock # ahead, which validates no certificate the bundle does not already trust. # Returns 0 only when the clock moved, so the caller knows a retry is worth it. clock_from_http() { local hdr now web hdr=$(curl -s -m 15 -I --proto =http "http://${MANIFEST_URL#*://}" 2>/dev/null \ | sed -n 's/^[Dd]ate: //p' | tr -d '\r') web=$(TZ=UTC0 date -D '%a, %d %b %Y %H:%M:%S' -d "${hdr% GMT}" +%s 2>/dev/null) now=$(date +%s) [ -n "$web" ] && [ "$web" -gt "$now" ] || return 1 date -s "@$web" >/dev/null 2>&1 || return 1 echo_c 33 "Clock was behind; set from the HTTP Date header: $(date)" } # Reachability and trust, before anything is downloaded. Kept apart from the # download, which streams into tar where curl's exit status is lost in the # pipe, and because a certificate failure must not read as "Check your # network!" -- the two have different fixes. probe_url() { local rc curl --connect-timeout 30 -m 60 -s -o /dev/null $(tls_opts "$1") "$1" rc=$? if [ "$rc" -eq 60 ] && clock_from_http; then curl --connect-timeout 30 -m 60 -s -o /dev/null $(tls_opts "$1") "$1" rc=$? fi case "$rc" in 0) ;; 60) die "Certificate verification failed for $1 The clock reads $(date). If that is wrong, fix it first (ntpd -Nnq, or date -s). For a private server with a self-signed certificate, add --insecure." ;; 77) die "Cannot read the CA bundle /etc/ssl/certs/ca-certificates.crt. Add --insecure to proceed without verification." ;; *) die "Check your network!" ;; esac } fetch_manifest() { probe_url "$MANIFEST_URL" curl --connect-timeout 30 -s -m 60 -L $(tls_opts "$MANIFEST_URL") "$MANIFEST_URL" -o "$MANIFEST_CACHE" \ || die "Cannot fetch manifest $MANIFEST_URL" manifest_fetched=1 } # resolve_channel -> echoes a build_id resolve_channel() { awk -v c="$1" '$1=="@channel" && $2==c { print $3; exit }' "$MANIFEST_CACHE" } # resolve_url -> echoes a URL # The plain package for flash $3; for nor, a board that publishes only the # split NOR package (hi3516cv6xx, hi3519dv500) is found under nor_split. resolve_url() { awk -v b="$1" -v p="$2" -v f="$3" ' $1==b && $2==p && $3==f { print $NF; found = 1; exit } $1==b && $2==p && f=="nor" && $3=="nor_split" && split_url=="" { split_url = $NF } END { if (!found && split_url != "") print split_url }' "$MANIFEST_CACHE" } # Rewrite $model from a retired/shared SoC id to the canonical model an # OpenIPC image is actually published under, so the download name, manifest # key, extracted artifact names and SoC check all reference an image that # exists. Two sources, in order: (1) the manifest's @alias records (chip -> # model) when a manifest has been fetched — authoritative, extended by # defconfig BR2_OPENIPC_SOC_ALIASES; (2) a tiny static net for the known # retired SoCs so offline / SD-card / --archive migrations resolve with no # network at all. Idempotent and a no-op for canonical models. resolve_model_alias() { local m="" [ -s "$MANIFEST_CACHE" ] && m=$(awk -v c="$model" \ '$1=="@alias" && $2==c { print $3; exit }' "$MANIFEST_CACHE") [ -z "$m" ] && case "$model" in xm550) m=xm530 ;; gk7205v210) m=gk7205v200 ;; esac [ -n "$m" ] && [ "$m" != "$model" ] && { echo_c 36 "SoC alias: $model -> $m (shared platform)" model="$m" } return 0 } # The images THIS run intends to write, one per line. # # Not "everything the archive brought": a plain `-k` downloads both halves and # writes one, and whether the other arrived intact is not this run's business. images_this_run() { [ -f "$stage_dir/firmware.bin.$image_soc" ] && { echo "$stage_dir/firmware.bin.$image_soc"; return 0; } [ "1" = "$update_kernel" ] && kernel_image [ "1" = "$update_rootfs" ] && rootfs_image return 0 } # Every name the manifests actually cover. # Normalised, because `md5sum -c` accepts spellings this comparison must not # reject: a `*` prefix is its binary-mode marker, and a manifest written from a # staging directory names its members `./x`, which verifies fine from /tmp. # Refusing those would block a perfectly good archive from installing at all -- # a worse failure than the one this guard exists to catch. md5_names() { awk '{ n = $2; sub(/^[*]/, "", n); while (sub(/^[.]\//, "", n)) ; print n }' "$stage_dir"/*.md5sum 2>&3 } # An image no manifest mentions has not been checksummed, and must not pass for # one that has. # # `md5sum -c *.md5sum` verifies the files the manifests list and says nothing # about a file no manifest names -- so when an unpack drops # rootfs.squashfs.$model.md5sum, the check silently narrows to the kernel and # still exits 0. Measured: a 4190208-byte truncated rootfs whose companion never # landed passes that gate untouched, which is how a short image reached flashcp. # # Matched by name INSIDE the manifests rather than by a $f.md5sum filename, # because both layouts are valid: the released artifacts carry one companion per # image, and a single manifest listing several images is just as correct. check_md5_covered() { local f for f in $(images_this_run); do [ -f "$f" ] || continue md5_names | grep -qxF "${f##*/}" && continue die "Nothing checksums ${f##*/}: its .md5sum never arrived, so the check above could not see this file. The download is incomplete. Nothing was written." done } # What unpacking archive $1 puts in /tmp, in KB: the part of it this run keeps, # sized from the gzip trailer. Fails when the trailer will not read. archive_unpack_kb() { local sz need sz=$(stat -c %s "$1" 2>&3) || return 1 [ -n "$sz" ] && [ "$sz" -gt 4 ] 2>&3 || return 1 # The last four bytes of a gzip stream are the uncompressed size. need=$(od -An -tu4 -j $((sz - 4)) -N4 "$1" 2>&3 | awk '{print $1; exit}') [ -n "$need" ] && [ "$need" -gt 0 ] 2>&3 || return 1 unpacked_part_kb $((need / 1024)) unpacked } # Will the unpack fit in /tmp? # # Asked only on an archive route, where the .tgz and everything inside it have # to sit on the same tmpfs at once -- the WebUI's "install from a file" hands # majestic's upload straight to --archive. Measured on the camera this was # reported from: a 6395 KB archive plus 6413 KB of unpacked images against a # 13564 KB /tmp, so 756 KB of headroom before majestic's own RAM. Past that edge # tar dies mid-member and leaves a truncated image behind, which is where the # missing-checksum hole starts. The streamed route needs half as much and stages # no second copy, so it is not asked. # # #2425 frees the archive once it is unpacked, which cannot help an unpack that # is itself what runs out of room. # # Refuses only on arithmetic it actually has: a gzip trailer that will not read # or a df that will not parse means proceed. check_unpack_room() { local a=$1 need free need=$(archive_unpack_kb "$a") || return 0 need=$((need * 1024)) # The same archive has to fit twice over: once in the tmpfs, and once in the # RAM that tmpfs is made of. They are different numbers -- see # check_unpack_ram -- and either can be the one that binds. check_unpack_ram "$((need / 1024))" "$a" # Staged in the media zone, the unpack does not touch /tmp: what has to fit # is the stage, and only the archive stays behind in the tmpfs. if [ "$stage_dir" != /tmp ]; then free=$(df -k "$stage_dir" 2>&3 | awk 'END { print $(NF - 2) }') [ -n "$free" ] && [ "$free" -gt 0 ] 2>&3 || return 0 [ "$((need / 1024))" -lt "$free" ] && return 0 die "No room to unpack $a in the media zone: needs $((need / 1024)) KB, $free KB free. Nothing was written." fi # Anchored on the Use% column rather than a field number, so a long device # name that wraps the row does not shift the answer. free=$(df -k /tmp 2>&3 | awk 'END { print $(NF - 2) }') [ -n "$free" ] && [ "$free" -gt 0 ] 2>&3 || return 0 [ "$((need / 1024))" -lt "$free" ] && return 0 grow_tmp "$((need / 1024))" "$free" && return 0 die "No room to unpack $a in /tmp: needs $((need / 1024)) KB, $free KB free. Use 'sysupgrade --channel nightly' instead -- it streams and stages no second copy." } # Raise /tmp's own cap when that cap is all that is short. # # tmpfs defaults to half of RAM, and a cap is not memory: it is a ceiling on # pages taken as they are written. On an XM510 at its stock mem=18M that ceiling # is 7804 KB, and an archive plus its images is 8341 KB, so every archive # upgrade was refused (#2618) while the RAM behind /tmp could hold it. Raised by # what is missing plus the tools' reserve, and only when RAM was measured to # hold that much -- check_unpack_ram has already counted it and stopped majestic # if that was needed. A ramfs has no cap, and a RAM figure nobody could read is # no grounds to raise one. Put back by restore_resources on a run that stays up. grow_tmp() { local need=$1 free=$2 size tmpfs_is_ramfs && return 1 usable_ram || return 1 [ "$((need + UNPACK_RESERVE_KB))" -lt "$ram_kb" ] || return 1 size=$(df -k /tmp 2>&3 | awk 'END { print $(NF - 4) }') [ -n "$size" ] && [ "$size" -gt 0 ] 2>&3 || return 1 mount -o remount,size=$((size + need - free + UNPACK_RESERVE_KB))k /tmp 2>&3 || return 1 tmp_cap_kb=$size echo "Raised the /tmp cap from $size KB to $((size + need - free + UNPACK_RESERVE_KB)) KB for the unpack: $ram_kb KB of RAM is there to back it." } # What the run still needs for itself while that tmpfs is full, in KB. # # The measurement is not free of the measurer: curl, gzip and tar are forked # AFTER MemAvailable is read, and they live alongside the pages they are # writing. The kernel's own OOM dump in the #2457 reproduction put them at 57, # 28 and 11 pages -- 384 KB together -- with the shell driving them at 11 more. # Rounded up to cover that and the page rounding on 8 MB of tmpfs writes. # # Deliberately does NOT reserve for the ramfs pivot, which stages around 1.5 MB # of busybox and libraries later in the run. A pivot that cannot be built is not # a failure -- enter_ramfs returns 1 and the run flashes in place instead -- so # reserving for it would refuse upgrades that would have completed. The one # exception is a run that must hand PID 1 over, which has no in-place path to # fall back to; check_unpack_ram reserves the pivot for that one. # # Overridable so the test harness can pin the boundary. UNPACK_RESERVE_KB=${UNPACK_RESERVE_KB:-512} # What MemAvailable would say, on a kernel too old to say it: 3.14 added the # line, so XM510 (3.0), XM530 (3.10) and hi3516cv100 (3.0.8) never had it, and # every RAM guard here stood down on them -- an XM510 then lost its rootfs # mid-erase (#2647) and could not install an archive at all (#2618). The same # arithmetic si_mem_available() does: free pages above the low watermark, plus # the file page cache and reclaimable slab, each less the part reclaim cannot # have (half, capped at the watermark). The low watermark is min_free_kbytes # plus a quarter, as the kernel derives it. Fails when a field is missing. mem_available_estimate() { local minfree minfree=$(cat /proc/sys/vm/min_free_kbytes 2>&3) [ -n "$minfree" ] && [ "$minfree" -ge 0 ] 2>&3 || return 1 awk -v minfree="$minfree" ' /^MemFree:/ { free = $2; n++ } /^Active\(file\):/ { pc += $2; n++ } /^Inactive\(file\):/ { pc += $2; n++ } /^SReclaimable:/ { sl = $2; n++ } END { if (n < 4) exit 1 wl = int(minfree * 5 / 4) a = free - wl h = int(pc / 2); a += pc - (h < wl ? h : wl) h = int(sl / 2); a += sl - (h < wl ? h : wl) print (a > 0 ? a : 0) }' /proc/meminfo 2>&3 } # What /tmp can still be given, in KB, into $ram_kb: MemAvailable (estimated # where the kernel does not report it), less the free CMA where tmpfs is ramfs # (see check_unpack_ram), whose amount is then left in $ram_cma_kb. Fails when # neither can be read, which both callers take as no opinion. usable_ram() { local cma ram_kb=$(awk '/^MemAvailable:/ { print $2; exit }' /proc/meminfo 2>&3) [ -n "$ram_kb" ] || ram_kb=$(mem_available_estimate) ram_cma_kb= [ -n "$ram_kb" ] && [ "$ram_kb" -gt 0 ] 2>&3 || return 1 tmpfs_is_ramfs || return 0 cma=$(awk '/^CmaFree:/ { print $2; exit }' /proc/meminfo 2>&3) [ -n "$cma" ] && [ "$cma" -gt 0 ] 2>&3 || return 0 ram_kb=$((ram_kb > cma ? ram_kb - cma : 0)) ram_cma_kb=$cma } # Whether the archive this run unpacks will not fit in RAM beside majestic, # by $majestic_short_kb. Only an archive already staged in /tmp counts -- one # behind the old root or still to be downloaded is measured where it lands -- # and a RAM figure nobody can read is no reason to stop anything. majestic_short_for_archive() { local need majestic_short_kb= case "$archive" in /tmp/*) ;; *) return 1 ;; esac pidof majestic >/dev/null 2>&1 || return 1 need=$(archive_unpack_kb "$archive") || return 1 usable_ram || return 1 need=$((need + UNPACK_RESERVE_KB)) [ "$need" -lt "$ram_kb" ] && return 1 majestic_short_kb=$((need - ram_kb)) } # Stop majestic to give the upgrade the memory it holds, when the upgrade is # short of it. $1 is the shortfall in KB, for the log. # # majestic in upgrade mode -- the WebUI's, and the one free_resources asks for # on a console run -- has stopped its video, but what is left of it is locked # in RAM (VmLck in /proc//status), and that is the memory a small camera # is missing. Measured on a gk7201v200 (28 MB): MemAvailable 15664 KB with # majestic running, 11476 KB in upgrade mode with 7072 KB of it locked, 18888 # KB with it stopped. # An hi3516ev200 that had hung in the RAM phase through the WebUI was then # refused over ssh, 607 KB short of the unpack, and went through only after a # reboot. # # Only on a shortfall: majestic is what streams this log to the WebUI, so a # camera with room keeps its progress bar. Never when majestic holds the # watchdog -- the RAM-phase keeper takes over only a dog whose owner is gone, # and nothing pets it between here and the pivot. Once per run. stop_majestic_for_memory() { local pid [ "1" = "$majestic_stopped" ] && return 1 pid=$(pidof majestic 2>/dev/null) || return 1 # "releases": free_resources' early stop, before upgrade mode. majestic's # own stop usually gives its watchdog back with the magic close, so # stopping the owner there leaves nothing armed -- but not when majestic # ignored SIGTERM and S95majestic had to SIGKILL it, which closes the # device without the magic close. Either way its ownership is already # recorded, so where the dog stays armed (that, or a driver that ignores # the magic close) the RAM-phase keeper takes the device over from the # pivot -- the unpack before it is seconds against majestic's 300 s. Later stops come after upgrade mode let go of the # watchdog, and keep the guard. if [ -n "$wdog_owner_pid" ] && [ "releases" != "$2" ]; then case " $pid " in *" $wdog_owner_pid "*) return 1 ;; esac fi echo_c 33 "\nShort of memory by $1 KB with majestic running: stopping it, which gives back what it holds in RAM." # A second for majestic to forward the line above before it goes: it is # the last the WebUI sees of this run. if [ "1" = "$web_update" ]; then echo_c 33 "This page loses the log here. If the upgrade goes ahead, the camera reboots on its own when it is done; if it is still short, nothing is written, majestic comes back, and the reason is in the camera's log." sleep 2 fi # From here the camera has no majestic and this run holds the lock its # supervisor waits on, so every way out has to go through die(), which # puts both back: an interrupt included, until the flash phase takes the # signals away for good. Not once it has: the floor is asked from inside # enter_ramfs, past that point, and an interrupt that reached die() from # there on could reboot the camera halfway through the erase (#2024). And # a reader of this output that went with majestic must not take the run # down with a SIGPIPE. trap '' PIPE [ "1" = "$signals_ignored" ] || trap 'die "Interrupted, with majestic stopped for the upgrade."' INT TERM HUP if ! /etc/init.d/S95majestic stop >/dev/null 2>&1; then # Still running, in upgrade mode, and its supervisor gone: nothing # was freed, and only a restart brings its video back. majestic_stop_failed=1 echo_c 33 "majestic did not stop; carrying on without the memory it holds." return 1 fi majestic_stopped=1 sync echo 3 > /proc/sys/vm/drop_caches return 0 } # Unpack into the media zone instead of RAM, where RAM is what is short. # # A 64 MB HiSilicon part with hisi_mem's carve-out ("ot") gives the OS 32-36 MB # and the media zone the rest, out of the kernel's sight. Once majestic has # stopped, that zone is idle -- the kernel modules keep a few hundred KB at its # start -- and is far larger than an image. phram maps its free part as an MTD, # jffs2 on that is a filesystem that costs the OS nothing, and every step after # the unpack -- checksums, the rootfs peek, the write -- reads files from it as # it would from /tmp. It sits under /tmp, so the pivot to the RAM root carries # it along. # # The start of the zone is left alone (MMZ_STAGE_SKIP_KB), and nothing is mapped # unless every block the modules hold ends below it: the zone's own allocator # does not know about the mapping. MMZ_STAGE_SKIP_KB=${MMZ_STAGE_SKIP_KB:-4096} # Whether stage_in_mmz could run at all here: a carve-out zone and phram. mmz_stage_possible() { [ "ot" = "$(cat /sys/module/hisi_mem/parameters/allocator 2>&3)" ] || return 1 # A phram someone already loaded is theirs, mapping who knows what. grep -q '^phram ' /proc/modules 2>&3 && return 1 find "/lib/modules/$(uname -r)" -name phram.ko 2>&3 | grep -q . } stage_in_mmz() { local need=$1 p=/sys/module/hisi_mem/parameters base size_kb top start len_kb n [ -z "$(pidof majestic 2>&3)" ] || return 1 [ "ot" = "$(cat $p/allocator 2>&3)" ] || return 1 base=$(cut -d, -f3 $p/mmz 2>&3) size_kb=$(cut -d, -f4 $p/mmz 2>&3 | tr -d M) [ -n "$base" ] && [ -n "$size_kb" ] || return 1 size_kb=$((size_kb * 1024)) start=$((base + MMZ_STAGE_SKIP_KB * 1024)) # Every block the modules hold, by where it ENDS (the second address of # "phys(start, end)"), so one that starts below the mapping and runs into it # is refused too. A zone whose occupancy cannot be read is not mapped at all. grep -q "total size=" /proc/umap/media-mem 2>&3 || return 1 top=$(awk -F'[(), ]+' '/MMB: phys/ { for (i = 1; i < NF; i++) if ($i == "phys") print $(i + 2) }' /proc/umap/media-mem 2>&3 | while read -r e; do echo $((e)); done | sort -n | tail -1) [ -n "$top" ] && [ "$top" -ge "$start" ] && return 1 len_kb=$((size_kb - MMZ_STAGE_SKIP_KB)) [ "$len_kb" -ge $((need + 1024)) ] || return 1 # Ours only if this run loads it: modprobe of a loaded module ignores the # parameters, and one already loaded may map something else. grep -q '^phram ' /proc/modules 2>&3 && return 1 # In bytes: the 5.10 parser takes no "Ki". modprobe phram phram=sysupgrade,$(printf 0x%x "$start"),$((len_kb * 1024)) 2>&3 || return 1 mmz_phram_ours=1 n=$(awk -F: '/"sysupgrade"/ { sub("mtd", "", $1); print $1 }' /proc/mtd) if [ -z "$n" ] || ! flash_eraseall -q "/dev/mtd$n" 2>&3 || ! mkdir -p /tmp/stage || ! mount -t jffs2 "/dev/mtdblock$n" /tmp/stage 2>&3; then release_mmz_stage return 1 fi mmz_stage_mounted=1 stage_dir=/tmp/stage export handoff_stage_dir=$stage_dir echo_c 33 "Unpacking into the idle media zone ($((len_kb / 1024)) MB at $(printf 0x%x "$start")) instead of RAM." } release_mmz_stage() { if [ "1" = "$mmz_stage_mounted" ]; then umount /tmp/stage 2>&3 mmz_stage_mounted= fi rmdir /tmp/stage 2>&3 if [ "1" = "$mmz_phram_ours" ]; then rmmod phram 2>&3 mmz_phram_ours= fi stage_dir=/tmp return 0 } # Will the unpack fit in the RAM this camera has left? $1 KB, $2 what to call # the source in the message. # # /tmp is a tmpfs, so every byte unpacked into it is a page of RAM that nothing # can reclaim -- and, being shared memory, it is charged to no process. So when # the machine runs out, the OOM killer cannot choose the unpack: it chooses the # largest RSS on the box, which on a camera is majestic. Reproduced on a lab # gk7205v300 held at the reporter's ~8 MB of MemAvailable while it unpacked the # 8510 KB image from #2457: # # [ 968] ... rss 928 ... majestic <- every other task held 11 to 57 pages # Out of memory: Kill process 968 (majestic) score 29 # # curl, gzip and tar were never candidates. The unpack then FINISHED, all four # members at their published sizes, so no exit status downstream was wrong and # nothing printed a word. What died was the process streaming the WebUI's # progress, which is why the run ends mid-log, the camera keeps the old image, # and RTSP and the WebUI are gone until somebody power-cycles it. That is also # why this has to be asked BEFORE the unpack: afterwards there is nobody left to # ask it to. # # majestic is most of the shortfall, on every path: the WebUI's leaves it # streaming this log, and a console run asks it into the same upgrade mode, # which keeps it locked in RAM. The same 8510 KB unpack with majestic stopped and # 9544 KB free finished rc=0 -- so a shortfall stops it before refusing # (stop_majestic_for_memory). # # df is the wrong question and was the only one being asked. tmpfs is sized at # half of RAM and charges nothing until it is written, so that same camera # reported 51836 KB free in /tmp while it had 9544 KB left to give. # # Fails OPEN, like every other measurement here: a /proc/meminfo that will not # parse, or a size the caller could not learn, is not grounds to refuse an # upgrade that would have worked. # # Two things MemAvailable does not say, both from a gk7205v500 that passed this # check with 96 MB to spare and was then OOM-killed at the hand-off (#2536): # # On a kernel without CONFIG_SHMEM, tmpfs is ramfs, and ramfs pages are not # movable, so they can never be placed in a CMA area. MemAvailable counts free # CMA all the same -- on that camera, 98 of the 99 MB it reported once the image # was unpacked. There, CmaFree is not memory the unpack can have. # # And where the run must hand PID 1 to a RAM root (need_handoff) that root is # not optional: it is staged after the unpack, from the same memory, and a # staging that runs out is not a clean fallback but an OOM kill in the middle # of the hand-off. So for that run it is reserved, at its real size. check_unpack_ram() { local need=$1 src=$2 avail want stage=0 why [ -n "$need" ] && [ "$need" -gt 0 ] 2>&3 || return 0 usable_ram || return 0 avail=$ram_kb [ -n "$ram_cma_kb" ] && why=" On this kernel that tmpfs is ramfs, which cannot use the $ram_cma_kb KB free in the CMA (MMZ) area, so that is not counted." need_handoff && stage=$(ramfs_stage_kb) want=$((need + UNPACK_RESERVE_KB + stage)) # Where the media zone is a carve-out, it is the first choice and not a # fallback. Passing this check is not the end of what the run needs: the # rootfs peek mounts the new image, and its squashfs caches come out of the # same RAM -- 4 MB more for a 1 MiB-block image, where a 64 MB hi3516cv608 # that had passed here ran out and thrashed until power was cut. In the # zone, the image costs the OS nothing. if mmz_stage_possible; then [ -z "$(pidof majestic 2>&3)" ] || stop_majestic_for_memory "$need" stage_in_mmz "$need" && return 0 fi [ "$want" -lt "$avail" ] && return 0 if stop_majestic_for_memory "$((want - avail))"; then # A second reading that will not parse is no opinion, as the first is. usable_ram || return 0 avail=$ram_kb [ "$want" -lt "$avail" ] && return 0 fi # The image itself can go to the media zone; the tools unpacking it and the # RAM root still need the OS's own memory. [ $((UNPACK_RESERVE_KB + stage)) -lt "$avail" ] 2>&3 && stage_in_mmz "$need" && return 0 local out="Not enough memory to unpack $src: it needs $want KB -- $need KB of image plus $UNPACK_RESERVE_KB KB for the tools unpacking it" [ "$stage" -gt 0 ] && out="$out and $stage KB for the RAM root the flash phase runs from" out="$out -- and this camera has $avail KB free${majestic_stopped:+ with majestic stopped}. /tmp is a tmpfs, so the unpacked image is RAM.$why Nothing was written." die "$out" } # Is there RAM left to flash from, once the RAM root is staged? Called by # enter_ramfs before it moves anything; on a refusal the reason is left in # $flash_ram_refusal for it to raise once the stage is torn down. # # check_unpack_ram above leaves the stage out on purpose, and the unpack can # pass it with only its 512 KB to spare. The stage then takes ~1.3 MB of what # is left, and what goes short is not the flash phase itself -- it is in RAM -- # but everything still running from the old root: PID 1, dropbear, the majestic # supervisor. Their pages are reclaimed, and once the rootfs erase starts they # fault back in from a squashfs that is no longer there. Reproduced on a # gk7201v200 through the WebUI with ~500 KB left at the pivot: kernel written, # then the console filled with "SQUASHFS error: Unable to read data cache # entry" for one block of the old root and the rootfs erase never got past # block 17 of 77. No watchdog rebooted it; the camera came back only through # U-Boot. The same run went through with 2108 KB left here, and with ~4 MB. # --web is the path that gets here, because majestic stays resident. # # Counted the way check_unpack_ram counts it: where tmpfs is ramfs, free CMA # is not RAM this stage, the kernel's own allocations or the verify's loop # mount can have. And fails open like it: no MemAvailable is no opinion. # # Overridable so the test harness can pin the boundary. FLASH_RAM_FLOOR_KB=${FLASH_RAM_FLOOR_KB:-2048} check_flash_ram() { local avail flash_ram_refusal= usable_ram || return 0 avail=$ram_kb echo "RAM left to flash from: $avail KB${ram_cma_kb:+ (free CMA not counted: $ram_cma_kb KB)}" [ "$avail" -ge "$FLASH_RAM_FLOOR_KB" ] && return 0 if stop_majestic_for_memory "$((FLASH_RAM_FLOOR_KB - avail))"; then usable_ram || return 0 avail=$ram_kb echo "RAM left to flash from: $avail KB with majestic stopped" [ "$avail" -ge "$FLASH_RAM_FLOOR_KB" ] && return 0 fi flash_ram_refusal="Not enough memory left to flash safely: $avail KB once the flash phase is staged in RAM${majestic_stopped:+ and majestic stopped}, and it needs $FLASH_RAM_FLOOR_KB KB so the processes still running from the old rootfs are not paged out while it is erased. Nothing was written." return 1 } # Whether /tmp is ramfs underneath: what "tmpfs" is on a kernel built without # CONFIG_SHMEM, and what statfs then reports (RAMFS_MAGIC). Anything else, # including a stat that will not answer, is a real tmpfs. tmpfs_is_ramfs() { [ "$(stat -f -c %t /tmp 2>&3)" = "858458f6" ] } # The libraries busybox loads, as absolute paths: what enter_ramfs stages # beside it and ramfs_stage_kb counts. ldd where it answers. A uClibc image has # none at all -- not a busybox applet, not in the toolchain sysroot -- so the # RAM root went without a loader and the re-exec into it died "not found" # (#2605). There the names come out of the ELF itself, read twice so the # libraries' own needs come too: see elf_libs. busybox_libs() { local libs f libs=$(ldd /bin/busybox 2>&3 | grep -oE '/[^ ]*\.so[^ ]*') if [ -z "$libs" ]; then libs=$(elf_libs /bin/busybox) for f in $libs; do libs="$libs $(elf_libs "$f")" done fi [ -n "$libs" ] && printf '%s\n' $libs | sort -u return 0 } # What one ELF asks the loader for, without a loader to ask. PT_INTERP is a # plain path string, and every DT_NEEDED a bare "libX.so.N" in .dynstr, so # `strings` sees both. Anything that merely looks like one is dropped by the # existence check: it costs a lookup, never a wrong copy. elf_libs() { local s d for s in $(strings "$1" 2>&3 | grep -E '^/[^ ]*/ld[^/ ]*\.so[.0-9]*$'); do [ -f "$s" ] && echo "$s" done for s in $(strings "$1" 2>&3 | grep -E '^lib[^/ ]*\.so(\.[0-9]+)*$'); do for d in $ELF_LIB_DIRS; do [ -f "$d/$s" ] && { echo "$d/$s"; break; } done done } # What enter_ramfs stages into the RAM root, in KB, counted the way the RAM is # taken: busybox, busybox_libs (the same list enter_ramfs copies) and this # script, each rounded up to whole pages; on ramfs a page for # every applet link as well, because a ramfs symlink keeps its target in a page # of its own (227 links, 908 KB, on a gk7205v510) where a real tmpfs keeps a # short one inline; and 16 KB for the directories, sysupgrade.env and the # /sbin/init the hand-off writes. ramfs_stage_kb() { local f sz kb=16 links for f in /bin/busybox $(busybox_libs) "$0"; do sz=$(stat -L -c %s "$f" 2>&3) && [ -n "$sz" ] && kb=$((kb + (sz + 4095) / 4096 * 4)) done if tmpfs_is_ramfs; then links=$(/bin/busybox --list 2>&3 | wc -l) kb=$((kb + ${links:-0} * 4)) fi echo "$kb" } # Members of the archive this run does not unpack, as tar options. # # A UBIFS NAND package carries rootfs.ubi -- the whole UBI image, for a fresh # install from U-Boot or defib -- beside the two volume images sysupgrade # writes. It is the largest member and it is never written here, so it is not # unpacked either: /tmp is RAM. The pattern takes its .md5sum with it, so the # checksum pass does not go looking for it. tar_excludes() { [ "$ubi_layout" = "ubifs" ] && echo "--exclude=rootfs.ubi.*" return 0 } # Of an unpack measured as $1 KB, the part tar_excludes leaves -- $2 says what # was measured: "unpacked" (the gzip trailer) or "packed" (Content-Length). # # A UBIFS NAND package holds each volume image twice -- alone, and again inside # rootfs.ubi -- so the two this run keeps are about half of the unpacked total # (48% on the gk7205v500 image). Against the PACKED size they are more: a UBIFS # image pads every LEB with 0xFF, which gzip takes almost nothing to store, so # Content-Length understates what lands in /tmp (65% measured, 22035917 bytes # packed against 14640708 kept). Both rounded up, so the guess errs towards # asking for more. Every other package unpacks whole, and its Content-Length # already stands for the unpacked size (see unpack_size_kb). unpacked_part_kb() { [ -n "$1" ] || return 0 if [ "$ubi_layout" != "ubifs" ]; then echo "$1" elif [ "$2" = "packed" ]; then echo $(($1 * 75 / 100)) else echo $(($1 * 55 / 100)) fi } # How big will the unpack be, in KB? Nothing when it cannot be told. # # Asked two ways, because neither works everywhere. # # The exact answer is the gzip trailer -- the last four bytes of the stream are # the uncompressed size -- and a Range request fetches it without downloading # the archive. Plenty of servers serve that; GitHub's does not. Measured against # release-assets.githubusercontent.com, which advertises `accept-ranges: bytes` # in its own headers and then answers a ranged GET with 501, at the redirecting # github.com URL and at the resolved one alike. # # So fall back to the artifact's Content-Length, which for OpenIPC's tarballs is # the unpacked size within a fraction of a percent: the payload is a squashfs # and a compressed kernel, both already compressed, so gzip takes almost nothing # off (measured on the image from #2457, 8691055 packed against 8714240 # unpacked -- 0.3%). For anything else that number is a floor and not a bound -- # a hand-rolled --url archive of something compressible expands far past it -- # which is why the trailer is asked for first and this is only the fallback. unpack_size_kb() { local n if n=$(gzip_isize_kb "$1"); then unpacked_part_kb "$n" unpacked elif n=$(remote_length_kb "$1"); then unpacked_part_kb "$n" packed fi } # The gzip trailer, via Range. Trusted only on a 206 with exactly four bytes # back: a server that does not implement Range answers 200 with the whole file, # and reading "the first four bytes of what arrived" would parse the gzip MAGIC # as a length. # # --max-filesize is not belt and braces, it is the whole safety of this probe. # That 200 is a full-artifact download, and -o points into the tmpfs this guard # exists to protect, so without a cap the check funds the very failure it is # meant to refuse. Measured on a lab gk7205v300 while writing this: a ranged GET # for four bytes of a 200 MB artifact, against a server with no Range support, # filled the camera's 60 MB /tmp and reset the board. 64 bytes is room for the # four wanted and nothing else; curl gives up on the declared length before it # transfers a body, and mid-stream when the length is not declared. gzip_isize_kb() { local t=/tmp/.sysupgrade-isize code n rm -f "$t" code=$(curl --connect-timeout 30 -m 60 -s -r -4 --max-filesize 64 \ $(tls_opts "$1") "$1" -o "$t" -w '%{http_code}' 2>&3) if [ "206" = "$code" ] && [ "4" = "$(stat -c %s "$t" 2>&3)" ]; then n=$(od -An -tu4 -j0 -N4 "$t" 2>&3 | awk '{print $1; exit}') fi rm -f "$t" [ -n "$n" ] && [ "$n" -gt 0 ] 2>&3 && echo $(((n + 1023) / 1024)) } # Content-Length, read from the FINAL response only. # # `curl -IL` prints the headers of every hop, so taking the last length in the # stream measures a redirect whenever the final response declares none -- a # chunked artifact behind a redirect that did declare one. Track the status line # of each block instead and answer only for a 2xx that carried a length itself. remote_length_kb() { local n n=$(curl --connect-timeout 30 -m 60 -sIL $(tls_opts "$1") "$1" 2>&3 | awk ' /^HTTP\// { ok = ($2 >= 200 && $2 < 300); len = ""; next } /^[Cc]ontent-[Ll]ength:/ { gsub(/[^0-9]/, "", $2); len = $2 } END { if (ok && len != "") print len }') [ -n "$n" ] && [ "$n" -gt 0 ] 2>&3 && echo $((n / 1024)) } # An archive staged in /tmp is a second full-size copy of an image that is also # unpacked beside it, on the tmpfs the rest of the run still needs: # do_update_firmware dd's two more copies out of a combined image, and # verify_rootfs loop-mounts one. The WebUI's "install from a file" route puts it # there -- majestic writes the upload to /tmp and hands us the path -- so on a # camera where /tmp is half of 32 MB the archive is pure cost once the image is # known good. The online route never has one: it pipes curl straight into tar. # # Only under /tmp, and never a path the caller chose: --archive can name a file # on an SD card or a share, and that one is theirs. Freed once # preflight_image_sizes has passed the image, not after the checksum: a refusal # in between -- the wrong device profile, say -- left nothing to retry from, and # the unpacked images it did leave were no use to anybody. Nothing between the # two allocates in /tmp, so the peak is the unpack's, as before. # (OpenIPC/majestic-webui#474) # # Classified by where the file RESOLVES, not by how it was spelt. # "/tmp/../mnt/fw.tgz" matches the pattern and is a file on the SD card, so the # raw string would have deleted exactly the archive this guard exists to # protect. A path that cannot be resolved is left alone: not knowing where it # is, is not a reason to remove it. free_staged_archive() { [ -n "$archive" ] || return 0 local resolved=$(readlink -f "$archive" 2>/dev/null) case "$resolved" in /tmp/*) rm -f "$resolved" ;; esac } # Take back what download_firmware unpacked, for a run that ends without # writing: a refused image is no use to a retry, which unpacks it again, and on # a 60 MB tmpfs it is RAM -- 17 MB of it for a NAND package. Only the names tar # reported, and only plain names in /tmp itself, so a file the caller staged # there, or one some other process keeps there, is never touched. discard_unpacked() { [ -n "$unpacked_list" ] && [ -f "$unpacked_list" ] || return 0 local f while read -r f; do f=${f#./} case "$f" in "" | . | .. | */*) continue ;; esac rm -f "$stage_dir/$f" done < "$unpacked_list" rm -f "$unpacked_list" unpacked_list= } download_firmware() { # A UBIFS NAND layout installs from the -nand- package; every other camera, # NAND ones included (a ubiblock rootfs holds the NOR artifacts verbatim), # from the NOR package. [ "$flash_type" = "nand" ] && [ "$(pkg_flash)" = "nor" ] && echo_c 31 "\nNote: the updater uses the NOR package for updating NAND" echo_c 33 "\nFirmware" osr=$(get_system_build) unpacked_list=$(mktemp /tmp/sysupgrade.unpacked.XXXXXX) || die "Cannot create a file in /tmp to record the unpack in." if [ -n "$archive" ]; then [ ! -f "$archive" ] && die "File $archive not found" check_unpack_room "$archive" gzip -d "$archive" -c | tar xvf - -C "$stage_dir" $(tar_excludes) >"$unpacked_list" && echo_c 32 "Local archive unpacked" || die "Cannot extract $archive" else if [ -n "$channel" ] || [ -n "$build_id" ]; then fetch_manifest resolve_model_alias [ -n "$channel" ] && build_id=$(resolve_channel "$channel") [ -z "$build_id" ] && die "Unknown channel/build" platform=$(manifest_platform) url=$(resolve_url "$build_id" "$platform" "$(pkg_flash)") [ -z "$url" ] && die "No artifact for $platform in $build_id" # The manifest lists the plain package, which older cameras # install from; the -split one sits beside it under its own name. url=$(nor_split_url "$url") echo "Resolved $build_id for $platform" device_pinned=1 fi # A builder device writes its plain package's URL into `upgrade` on # first boot; its -split one, where published, is beside it. # default_upgrade_url has looked for it already. if [ -z "$url" ]; then if url=$(fw_printenv -n upgrade); then url=$(nor_split_url "$url") else url=$(default_upgrade_url) fi fi # Named for this device, so verify_rootfs can trust it when it cannot mount it. case "${url##*/}" in "$system_platform-$(pkg_flash).tgz"|"$system_platform-$(pkg_flash)-split.tgz") device_pinned=1 ;; esac echo "Download from $url" probe_url "$url" # The streamed route stages no second copy, so it never had a room # question asked of it. It still has to land the unpacked image in a # tmpfs, and that is the half that runs out first on a 32 MB camera. check_unpack_ram "$(unpack_size_kb "$url")" "$url" # Show a transfer bar so the (silent, up-to-2-min) download doesn't look # stuck — over /ws/upgrade this also gives the WebUI something to stream # (issue #120). `-s` (silent_update) opts back out for a quiet console run. [ "1" = "$silent_update" ] && dl_meter=-s || dl_meter=-# # Bound the download on THROUGHPUT, not on total time. `-m 120` capped the # whole transfer at two minutes, which is not a timeout but a speed # requirement: a camera on a link slower than ~60 KB/s could never finish # a ~7 MB image, and curl cut it off mid-stream with no hint that slowness # was the reason (majestic-webui issue #120 — the WebUI log froze partway # through the progress bar). --speed-limit/--speed-time instead give up # only once the transfer has genuinely stalled, so a slow-but-progressing # link completes. -m stays as a far outer backstop against a trickle that # never trips the stall check. curl --connect-timeout 30 $dl_meter --speed-limit 1024 --speed-time 60 -m 1800 \ -L $(tls_opts "$url") "$url" -o - | gzip -d | tar xvf - -C "$stage_dir" $(tar_excludes) >"$unpacked_list" && echo_c 32 "Received and unpacked" || die "Cannot retrieve $url" fi pick_image_soc if [ "1" != "$skip_md5" ]; then (cd "$stage_dir" && md5sum -s -c *.md5sum) || die "Wrong checksum!" check_md5_covered fi # The split NOR image (a uImage and the rootfs in one firmware.bin): # flashed as a unit by do_update_firmware. [ -f "$stage_dir/firmware.bin.$image_soc" ] && image_combined=1 [ "1" = "$image_soc_forced" ] && check_images_unpacked return 0 } # A forced run's images came out of this run's archive. # # The images are found by name in $stage_dir, and an interrupted run can leave # some there. The name a forced run takes is one the camera would never match # otherwise, and -f skips the checksums that tie an image to its archive, so # a partial archive could pass with an older run's kernel, rootfs or # firmware.bin beside it. Explicit --kernel=/--rootfs= files are the # operator's own and are not this check's business. check_images_unpacked() { local f for f in $(images_this_run); do [ "$f" = "$kernel_file" ] || [ "$f" = "$rootfs_file" ] && continue awk '{ n = $0; while (sub(/^[.]\//, "", n)) ; print n }' "$unpacked_list" 2>&3 | grep -qxF "${f##*/}" && continue die "${f##*/} did not come from this archive (it holds images for $image_soc, but not this one). Nothing was written." done } # The SoC the images this run unpacked are named for (#2688). # # An image is named for $model, the canonical SoC resolve_model_alias makes of # a retired or shared one -- except a builder device profile on an aliased SoC. # builder builds it for the chip it was made for and names and stamps it so: # a gk7205v210 profile's archive holds uImage.gk7205v210 and a rootfs whose # hostname says gk7205v210, while $model is gk7205v200. Every upgrade of such # a camera was refused with "No kernel image for this camera (gk7205v200)". # # So when nothing this run unpacked is named for $model, and something is # named for the SoC the camera's own build was made for, the images are that # one's. # check_soc accepts the same SoC: it is the one the running image was built # for, so it says no more about a foreign image than $model does. # # And -f (skip_soc) is the operator saying the images are for this camera # whatever they are named: an archive whose images all carry one other SoC's # name is taken under that name. Without it such an archive matched nothing # here, the run fell through to the kernel/rootfs path and was refused with a # message that never named the SoC -- seen when the GK7206 family image was # renamed from gk7206v12a to gk7206v11a and a gk7206v12a camera was handed # the new archive with -f, offline, where no manifest @alias could apply. pick_image_soc() { local other image_soc=$model image_soc_forced= # What this run unpacked, as tar listed it -- not whatever /tmp holds. grep -q "\.$model\$" "$unpacked_list" 2>&3 && return 0 if [ -n "$platform_soc" ] && [ "$platform_soc" != "$model" ] && grep -q "\.$platform_soc\$" "$unpacked_list" 2>&3; then image_soc=$platform_soc echo "Images named for $platform_soc, the SoC this camera's build was made for (aliased to $model)" return 0 fi [ "1" = "$skip_soc" ] || return 0 other=$(image_socs_unpacked) [ -n "$other" ] && [ "$(echo "$other" | wc -l)" -eq 1 ] || return 0 image_soc=$other image_soc_forced=1 echo_c 33 "Forced: images named for $other, this camera is $model" } # The SoC suffixes of the images this run unpacked, one per line. image_socs_unpacked() { awk '{ n = $0; while (sub(/^[.]\//, "", n)) ; if (match(n, /^(uImage|zboot[.]img|fitImage|firmware[.]bin|rootfs[.]squashfs|rootfs[.]ubifs)[.][a-z0-9]+$/)) { sub(/^.*[.]/, "", n); print n } }' "$unpacked_list" 2>&3 | sort -u } # Who is petting the hardware watchdog, if anyone. # # Asked once, on the still-normal system, because the answer decides whether the # flash phase may touch the device at all -- and after the pivot there is no # majestic left to ask about. # # On gk7205v200/v300 the kernel carries no CONFIG_WATCHDOG at all; the device # comes from open_wdt.ko, which load_goke inserts with no parameters, so # nodeamon defaults to 0 and a KERNEL daemon pets the dog while no userspace # process holds it. Opening the device takes that duty away from the kernel. So # on a camera whose owner turned the watchdog off, or one where majestic was # stopped before this ran, nobody holds it and we must leave it alone: claiming # it there would manufacture exactly the unfed fuse this guards against. # `-ef` rather than readlink: it is a test builtin in both busybox ash and dash, # so the whole scan costs no forks. Per-fd readlink cost one each, which is # thousands on anything bigger than a camera. watchdog_owner() { local p f [ -c "$WDOG" ] || return 1 for p in "$WDOG_PROC"/[0-9]*; do for f in "$p"/fd/*; do [ -e "$f" ] || continue [ "$f" -ef "$WDOG" ] && { echo "${p##*/}"; return 0; } done done return 1 } # Keep petting a watchdog whose owner has died, for as long as the flash lasts. # # majestic is demand-paged from the rootfs partition and free_resources has # already dropped the page cache, so its first fault after the erase is SIGBUS: # the pivot protects this script, not majestic. When it dies the driver says # "Unexpected close, watchdog left running!" and means it -- measured on # gk7205v200, the SoC hard-resets 297-307 s later (majestic asks for a 300 s # margin; the driver's own default is 60). A reset inside the write leaves the # rootfs partition part-written, which is how a camera comes back with a # corrupted filesystem from an upgrade whose log looked fine. It is also why # such a log stops mid-erase: majestic is what streams it. # # Whether majestic dies in that window is decided by which of its pages happen # to still be resident, which nothing here can reason about -- hence taking the # device over rather than estimating the odds. # # While majestic is alive the open is simply refused (measured: EPERM), so this # idles and the WebUI keeps streaming. It only ever takes over what someone else # abandoned, and only where a userspace owner was seen to begin with. # # No magic close on the way out. A plain close leaves the dog armed, so if the # reboot cannot exec -- the #2231 failure, where the binary lives on the # partition just overwritten -- it bites and recovers the camera instead of # leaving it wedged forever. watchdog_keep() { while :; do [ -f "$WDOG_RELEASE" ] && return 0 # The open cannot be the test. `exec` is a special builtin, so a # redirection it cannot satisfy takes this shell down -- and while the # owner is alive the open IS refused (measured: EPERM), so probing with # it killed this keeper on its first attempt, silently, before the owner # it was waiting for had even died. Ask who holds the device instead, and # only reach for it once nobody does. If something claims it in the # moment between the two, this keeper dies and the run is no worse off # than it was before there was one. if ! wd_holder=$(watchdog_owner); then exec 9>"$WDOG" echo_c 33 "\nWatchdog: its owner is gone; petting $WDOG until the reboot." while :; do [ -f "$WDOG_RELEASE" ] && return 0 printf 1 >&9 sleep 3 done fi sleep 1 done } # Ask majestic, over its API on this camera, to enter or leave upgrade mode # (prepare | abort). Answers true on a 200 and false on anything else: an # older majestic without the endpoint (404), one that is not running, or no # answer in time. Loopback needs no credential, so this works on a camera # nobody has claimed yet; majestic accepts it only from the camera itself. majestic_api() { local port port=$(cli -g .system.webPort 2>/dev/null) [ -n "$port" ] || port=80 [ "200" = "$(curl -s -o /dev/null -w '%{http_code}' -m 60 -X POST \ "http://127.0.0.1:$port/api/v1/upgrade/$1" 2>/dev/null)" ] } # Two kernel defaults that leave a 32 MB camera nothing to flash from, both # measured on a hi3516cv608 (26 MB for Linux, ~10 MB of it the kernel's own # slab and vmalloc) with the 9 MB image in /tmp, where check_flash_ram had # seen 3 MB available. Each time, the dd writing the image was OOM-killed in # the mtdblock page cache within its first blocks -- "Killed", then "the # flash does not read back as the image", then a reboot onto a half-written # kernel that never started; the SPI-NOR driver's writeback hit the same wall # on its per-operation ioremap. # # - Watermark boosting (watermark_boost_factor, 15000 by default since 5.0) # raises all three zone watermarks by a whole pageblock, 4 MB with 4 KiB # pages, whenever an allocation falls back to another migratetype: the min # watermark stood at 4736 KB instead of 640 KB. The vendor's own firmware # for that board sets the factor to 0 at boot. # - With boosting off, dirty pages took the rest: dd fills the page cache far # faster than NOR erases and programs, and dirty_ratio lets 20% of what is # available go dirty before the writer is throttled -- 3.6 MB of it at the # kill. A fixed 256 KB keeps dd at the flash's pace instead. # # Held from here until the camera reboots; restore_resources gives the values # back to a camera that stays up. Each knob is skipped where the kernel has # none (watermark_boost_factor is 5.0+). VM=/proc/sys/vm hold_vm_for_flash() { vm_saved= local k v kv for k in watermark_boost_factor dirty_ratio dirty_background_ratio dirty_bytes dirty_background_bytes; do [ -w "$VM/$k" ] && v=$(cat "$VM/$k" 2>&3) && vm_saved="$vm_saved $k=$v" done # Not a reason to refuse the upgrade: most cameras have the memory to # spare, and these are values the kernel always takes from root. But a # camera that does run short should be able to say why, so say it. for kv in watermark_boost_factor=0 dirty_bytes=262144 dirty_background_bytes=65536; do k=${kv%%=*} v=${kv#*=} [ -w "$VM/$k" ] || continue echo "$v" > "$VM/$k" 2>&3 || echo_c 33 "Warning: could not set vm.$k=$v; the flash runs without that memory guard." done return 0 } # The bytes and ratio knobs each zero the other when written, so only the one # that was in force (the non-zero one) is written back. release_vm_for_flash() { [ -n "$vm_saved" ] || return 0 local kv k v for kv in $vm_saved; do k=${kv%%=*} v=${kv#*=} case "$k" in dirty_bytes | dirty_background_bytes) [ "$v" = 0 ] && continue ;; esac case "$k" in dirty_ratio | dirty_background_ratio) [ "$v" = 0 ] && continue ;; esac echo "$v" > "$VM/$k" 2>&3 done vm_saved= } free_resources() { echo_c 37 "\nStop services, sync files, free up memory" # From here until the flash takes the signals away, an interrupt is a # refusal with nothing written: die() then restarts what this stops and # gives back the VM settings held below. trap 'die "Interrupted before the first write; nothing was written."' INT TERM HUP # First, before anything is downloaded or unpacked: see hold_vm_for_flash. hold_vm_for_flash # --web: majestic freed its own video pipeline and is streaming this log # over /ws/upgrade; keep it alive until just before the flash (see below). # # Otherwise ask it to do the same: prepare stops video and the SDK and # closes the clip the recorder is writing, so the image is downloaded into # RAM the video pipeline held and the SD card is free to unmount, while # majestic keeps serving and can put video back if we give up # (restore_resources). A majestic too old to have the endpoint gets the # SIGQUIT it always got. if [ "1" != "$web_update" ]; then # An archive already in /tmp that cannot be unpacked beside majestic # gets majestic stopped by check_unpack_ram anyway -- and asking it # into upgrade mode first is what this costs on a camera that tight. # On an XM510 at mem=18M, a majestic started a minute earlier, with # the 4 MB archive in a tmpfs nothing can reclaim, faulted its own # pages back after drop_caches during prepare and was OOM-killed; # the run died with it, leaving the lock and no majestic (#2618). # # The watchdog owner is recorded first, so the RAM-phase keeper still # arms if majestic was it. A stop that is refused or fails falls back # to prepare, as if nothing had been measured. wdog_owner_pid=$(watchdog_owner) && wdog_userspace_owned=1 if majestic_short_for_archive && stop_majestic_for_memory "$majestic_short_kb" releases; then : elif majestic_api prepare; then majestic_prepared=1 echo "majestic stopped its video for the upgrade" else killall -q -3 majestic sleep 1 fi fi # vigil (the NVR application) records to the card check_sdcard unmounts and # holds the media memory; it has no prepare endpoint, so it is stopped, and # restore_resources() brings it back if nothing is flashed -- only if it was # running: one stopped on purpose, or given up on after a crash loop, stays # down. Running means vigild, or its supervisor between two of them (in a # crash back-off, or restarting it for a card that turned up): that one # would see the lock, give up, and leave the NVR dark. A stop that fails has already ended its supervisor, so it gets a # restart; if it still holds the card, check_sdcard's umount refuses and the # run stops there, naming it, with nothing written. # The pid is checked for being there first: busybox ash's `kill -0 ""` # succeeds, so a missing pidfile would read as a live supervisor. vigil_sup=$(cat /var/run/vigil-supervise.pid 2>/dev/null) if [ -x /etc/init.d/S95vigil ] && { pidof vigild >/dev/null 2>&1 || { [ -n "$vigil_sup" ] && kill -0 "$vigil_sup" 2>/dev/null; }; }; then if /etc/init.d/S95vigil stop >/dev/null 2>&1; then vigil_stopped=1 else vigil_stop_failed=1 echo_c 33 "vigil did not stop; carrying on, it is restarted if nothing is written." fi fi /etc/init.d/S99rc.local stop /etc/init.d/S60crond stop /etc/init.d/S49ntpd stop /etc/init.d/S02klogd stop # fake-hwclock writes /etc periodically; left running it would be a live # overlay writer during the erase that quiesce_overlay() exists to fence # off. Its stop also takes a final checkpoint, so the camera comes back # from the post-flash reboot with the freshest time it can have. /etc/init.d/S02fakehwclock stop # The last line a collector sees if this camera never comes back. # # A forwarded log otherwise just stops here, and a clean stop reads exactly # like a death -- on the one operation most likely to produce the latter. # busybox sends no shutdown marker of its own on the remote path (it # forwards the line unmodified and logs "syslogd exiting" only locally), so # this has to be said out loud, before syslogd goes. #2415. # # "Preparing to", not "flashing": free_resources runs before check_sdcard, # before the download and before any write, so several paths end here with # nothing flashed at all -- and restore_resources() below logs the # retraction when they do. It cannot name the image for the same reason. # warning, not the default notice, because a collector that filters at # >= warning should not lose the one line explaining the silence. syslog_remote_set && { logger -s -p user.warning -t sysupgrade \ "preparing to flash (kernel=${update_kernel:-0}" \ "rootfs=${update_rootfs:-0} overlay-wipe=${clear_overlay:-0})" \ "on $(get_build_id); logging stops here until this camera returns" # Only for the forwarded case, and only when it is on: UDP has no # retry, so the datagram needs a moment to leave before syslogd goes. # Every camera used to pay this second for a feature most have off. sleep 1 } /etc/init.d/S01syslogd stop services_stopped=1 sleep 1 sync echo 3 > /proc/sys/vm/drop_caches echo_c 34 "\nUptime:" uptime echo_c 34 "\nMemory:" free -h echo_c 34 "\nProcesses:" ps | grep -v '\[' # Said out loud either way: a camera that comes back from a flash with a # corrupted rootfs needs this line to tell an unfed-dog reset apart from a # bad image, and it costs one scan of /proc on the normal system. # Asked again, now that the services are down; an owner recorded before an # early majestic stop above keeps the keeper armed. wdog_owner_pid=$(watchdog_owner) && wdog_userspace_owned=1 echo_c 34 "\nWatchdog:" if [ "1" = "$wdog_userspace_owned" ] && [ -n "$wdog_owner_pid" ]; then echo "$WDOG: pid $wdog_owner_pid feeds it; taken over if that dies" elif [ "1" = "$wdog_userspace_owned" ]; then echo "$WDOG: majestic fed it and was stopped for memory; taken over at the flash" else [ -c "$WDOG" ] && echo "$WDOG: nobody feeds it; left alone" || echo "none" fi } # Undo free_resources for a run that ends without rebooting. # # free_resources runs before the download, because dropping the page cache is # how a small camera finds the RAM to unpack the image -- so by the time a # download or checksum fails, syslog, klogd, ntpd, fake-hwclock and cron are # already stopped. # The reboot used to hide that: everything came back on the way up. Now that a # pre-flash failure leaves the camera running, it has to be undone explicitly, # or a refused upgrade silently costs the user their logging and their cron jobs # until somebody power-cycles the camera. # # Started low-number-first, the reverse of the order they were stopped in. restore_resources() { release_vm_for_flash # Best effort: a /tmp still holding more than the old cap keeps the new one. [ -n "$tmp_cap_kb" ] && mount -o remount,size=${tmp_cap_kb}k /tmp 2>&3 [ "1" = "$services_stopped" ] || return 0 echo_c 37 "\nRestarting the services stopped for the upgrade" local s for s in S01syslogd S02fakehwclock S02klogd S49ntpd S60crond S99rc.local; do [ -x /etc/init.d/$s ] && /etc/init.d/$s start >/dev/null 2>&1 done # Retract the warning free_resources sent. Reaching here means the camera # is staying up -- an abort, or a deliberate --no_reboot -- so a collector # holding "logging stops here until this camera returns" is now holding a # death notice for a camera that never died. Without this the log simply # resumes minutes later with no explanation, and the two cases a reader has # to tell apart look identical. #2415. syslog_remote_set && logger -s -p user.warning -t sysupgrade \ "still here on $(get_build_id): this camera is not rebooting after all" # Give back the cards check_sdcard took, mounted exactly as they were, so a # camera that stays up records again instead of writing to a card nobody # remounts until the next reboot. Not through the hotplug handler: that # would run the card's autostart.sh a second time. local dev dir type opts rest printf '%s\n' "$sd_unmounted" | while read -r dev dir type opts rest; do [ -n "$dev" ] || continue mkdir -p "$dir" if mount -t "$type" -o "$opts" "$dev" "$dir" 2>/dev/null; then echo "Remounted $dev on $dir" else echo_c 31 "Could not remount $dev on $dir; it stays unmounted until the card is reinserted or the camera reboots" fi done # majestic needs restarting, not starting: it is still running, just gutted. # # `killall -3 majestic` above is not a terminate. SIGQUIT is majestic's # "release the SDK and keep serving" signal -- signal_quit_cb calls exit_sdk # and returns, unlike SIGINT/SIGTERM which also break the event loop. So the # process survives with the same pid, no video pipeline, and sdk_state NULL. # Nothing brings that back in place: the SIGHUP reload early-returns on a # NULL sdk_state. # # Unless majestic says it will do it itself. A majestic new enough to leave # upgrade mode drops $MAJESTIC_OWNER while it holds one: its /ws/upgrade # watcher calls leave_upgrade_mode when we exit without rebooting, and an # orphan watcher covers a browser that closed early. Restarting that one is # not merely redundant, it destroys the end of the transcript -- the SIGTERM # lands between "Aborting." reaching the log and majestic's next read of it, # so the browser is left with a log that stops just short of the reason. # Measured on an hi3516av300: 1285 bytes of 1431 arrived, and the missing 146 # were the "File ... not found Aborting." and this very message # (majestic-webui #120). # # Keyed on the file rather than on a version test or a new option, because # self_update pulls this script onto cameras whose majestic predates that # self-restore (2026-08-10) and which do still need the restart. A new # command-line flag could not carry it either: arguments are parsed by the # INSTALLED script before self_update re-execs, so an older one would reject # it outright. # The marker states an INTENTION, so confirm the process is still there to # act on it. majestic removes the file in leave_upgrade_mode, but a crash # leaves it behind -- and a dead majestic restores nothing, so trusting the # marker alone would turn "we did not need to restart it" into "nobody # restarted it", and the camera stays video-less until a power cycle. That is # strictly worse than the redundant restart this replaces. # # A majestic we prepared over the API is in upgrade mode with no watcher of # its own -- nobody else will bring its video back -- so ask it to leave. # Only if that fails is it restarted. # # A start or a restart is only decided here and carried out by # restore_majestic(), once the caller has released the lock: the majestic # supervisor (S95majestic) will not start majestic while an upgrade holds # /tmp/sysupgrade.lock, so a start from here leaves the camera up with no # majestic and no WebUI. majestic_pending= if [ "1" = "$majestic_stopped" ]; then # Stopped for its memory (stop_majestic_for_memory): nothing is left # to leave upgrade mode, so it is started afresh, supervisor and all. majestic_pending=start elif [ "1" = "$majestic_stop_failed" ]; then # A stop that timed out took the supervisor and left majestic in # upgrade mode, where a start is a no-op. majestic_pending=restart elif [ "1" = "$majestic_prepared" ] && majestic_api abort; then echo "majestic restarted its video" elif [ "1" != "$majestic_prepared" ] && [ -f "$MAJESTIC_OWNER" ] && pidof majestic >/dev/null 2>&1; then echo "majestic is restoring itself; leaving it running" elif [ -x /etc/init.d/S95majestic ]; then majestic_pending=restart fi vigil_pending= [ "1" = "$vigil_stopped" ] && vigil_pending=start [ "1" = "$vigil_stop_failed" ] && vigil_pending=restart services_stopped= return 0 } # What restore_resources() decided for majestic, after the lock is gone. restore_majestic() { [ -n "$majestic_pending" ] && /etc/init.d/S95majestic "$majestic_pending" >/dev/null 2>&1 majestic_pending= # vigil's supervisor waits on the same lock (S95vigil), so its start is # carried out here too, once the lock is gone. [ -n "$vigil_pending" ] && /etc/init.d/S95vigil "$vigil_pending" >/dev/null 2>&1 vigil_pending= return 0 } # Give the old root back its mount point. # # The shipped rootfs has no /ram, so the `mkdir -p "$RAM_ROOT"` in enter_ramfs # writes a new directory into the overlay's upper layer — and nothing ever took # it out again, so every upgrade that used the pivot left an empty # /overlay/root/ram behind for good (OpenIPC/majestic-webui#120). # # rmdir, never `rm -rf`: if the tmpfs is somehow still attached — a lazy umount # that has not completed, a staged root the flash is still executing from — # rmdir fails harmlessly where a recursive delete would pull the floor out from # under the flash. An empty directory is all there is to remove. ramfs_discard() { # Only ever detach a tmpfs of our own. RAM_ROOT is overridable from the # environment (the test harness relies on that), so an unconditional umount # would happily detach real storage if it were pointed at some — the same # care the stale-mount check in enter_ramfs takes, for the same reason. if grep -q "^tmpfs $RAM_ROOT tmpfs" /proc/mounts 2>&3; then umount "$RAM_ROOT" 2>&3 fi # Only a mount point we created ourselves; enter_ramfs records which. # # A rootfs that ships /ram has it in the read-only lower layer, and rmdir # does not delete one of those -- overlayfs cannot, so it records a WHITEOUT # in the upper layer instead: a 0:0 character device that HIDES the shipped # directory from every later boot. Verified on an overlay mount: # # mkdir -p ram (already in lower) -> upper untouched, 0 entries # rmdir ram -> upper gains c--------- 0,0 ram # and /ram is gone from the merge # # That is the same class of debris as the /overlay/root/dev/null this script # already goes to lengths to avoid, and strictly worse than the empty # directory the rmdir was added to clean up. [ "1" = "$ram_root_shipped" ] || rmdir "$RAM_ROOT" 2>&3 } # Everything from the first flash write onwards runs on borrowed time. # # flashcp rewrites the MTD partition that backs the mounted squashfs rootfs, and # the page cache is not invalidated by that: the write goes to the raw /dev/mtdN # character device, behind the block layer's back. So the running system keeps # working for exactly as long as the pages it needs stay RESIDENT. Anything # faulted back in afterwards reads the NEW image at the OLD offset -- garbage, or # EIO. free_resources() has already dropped the page cache to make room for the # download, so on a small camera that eviction is likely rather than theoretical. # # It is what kills the reboot: `busybox reboot` is a fork+exec of a binary living # on the partition just overwritten, so it fails with EIO and the camera never # reboots -- it sits there with a corrupt live filesystem until somebody # power-cycles it (OpenIPC/majestic-webui#120). The same mechanism was described # for the -x path in #2231, where forcing a reboot cured that symptom without # removing the hazard. # # Reproduced deterministically without touching MTD: copy the rootfs squashfs to # tmpfs, loop-mount it, exec a binary from it (fine), overwrite the backing file # (fine -- pages still cached), then drop caches and exec again: I/O error. # # Put back everything enter_ramfs moved, for a run that did not make it into the # new root. # # The fallback is supposed to be "upgrade the old way", and the old way needs # /dev for the MTD nodes, /proc for get_device, and /tmp for the unpacked image. # Returning without these does not degrade to the previous behaviour, it strands # the RUNNING camera with no device nodes at all -- worse than the bug the pivot # fixes. Moves that never happened fail harmlessly, so this is safe to call from # any failure point after the first one. # # Every redirection from here to the end of the function goes to fd 3 rather # than to /dev/null by name: this runs precisely when /dev may already have been # moved away, which is when writing to that name creates a file instead of # discarding output. See the note beside the `exec 3>` at the top. ramfs_unwind() { cd / 2>&3 mount -o move "$RAM_ROOT/tmp" /tmp 2>&3 mount -o move "$RAM_ROOT/proc" /proc 2>&3 mount -o move "$RAM_ROOT/dev" /dev 2>&3 mount -o move "$RAM_ROOT/sys" /sys 2>&3 # Lazy as a fallback: /sys carries submounts (debugfs, configfs) and a # move-back that does not take leaves the tmpfs busy. Observed on an ssc30kq, # where the plain umount left /ram mounted -- harmless on its own, but a # retry would stack a second tmpfs on top of it. umount "$RAM_ROOT" 2>&3 || umount -l "$RAM_ROOT" 2>&3 # The mount point itself is left for the caller to reclaim: every path that # runs this one ends in `return 1` out of enter_ramfs, and that is where # ramfs_discard lives, so removing it here as well would just be a second # owner for the same directory. # Restoring is attempted, not guaranteed, so check it rather than assume it. # Handing the in-place path an environment without /dev or /proc is the very # thing this function exists to prevent: get_device reads /proc/mtd and # flashcp needs the nodes under /dev. Nothing has been written at this point, # so a reboot puts the camera back exactly as it was -- a far better outcome # than flashing blind. # # Deliberately not reboot_system(): -x must not be honoured here, and there # is no state left worth tidying. if [ ! -c /dev/null ] || [ ! -r /proc/mounts ] || ! grep -q " /tmp " /proc/mounts 2>&3; then echo_c 31 "\nCould not restore /dev, /proc or /tmp after a failed pivot." echo_c 31 "Rebooting instead of flashing in a broken environment." echo_c 31 "Nothing was written; the camera comes back on its current firmware." sync busybox reboot -d 1 -f exit 1 fi echo_c 33 "Could not move into RAM; flashing in place instead." return 1 } # Does this run have to get PID 1 off the flash before it writes? # # ubiupdatevol (UBI_IOCVOLUP) takes the volume exclusively -- get_exclusive() # in drivers/mtd/ubi/cdev.c -- and fails with EBUSY while anything else has it # open, readers included. Every volume init mounted is open: a UBIFS rootfs or # overlay (UBIFS opens its volume even for a read-only mount), and a squashfs # rootfs through ubiblock alike. The ramfs pivot does not unmount them: init's # overlay root is the old root, and PID 1 runs from it. OpenWrt meets the same # wall the same way (procd execs upgraded as PID 1 before stage2 unmounts # anything); here busybox init does the exec, through inittab's ::restart:. # Measured on a gk7205v510 with a ubiblock root: "ubiupdatevol: UBI_IOCVOLUP: # Resource busy" from inside the ramfs pivot. # # The kernel volume is never mounted, so a kernel-only run needs none of this. need_handoff() { [ -n "$ubi_layout" ] || return 1 [ "1" = "$update_rootfs" ] && return 0 [ "1" = "$clear_overlay" ] && grep -qE '^ubi[0-9]*:rootfs_data ' /proc/mounts 2>&3 && return 0 return 1 } # Should this run hand PID 1 off even though it could write in place? Any run # that rewrites the rootfs, where it can. # # On NOR a squashfs can be rewritten while it is mounted, so the hand-off was # kept for UBI, where it is the only way. But everything still running from the # old root then goes on running from it through the erase, and free_resources # has dropped the page cache: whatever it runs next faults in from flash that is # being wiped. Most of those processes die harmlessly. PID 1 does not -- busybox # init woke to reap one, faulted on its own text and took SIGBUS, and the # kernel's "Attempted to kill init!" rebooted an XM510 with its rootfs half # written, in three runs of four (#2647). The free-RAM floor does not cover it: # what it counts as reclaimable is exactly those pages. # # Unlike need_handoff this is a preference, not a requirement: an image without # the ::restart: entry, a run without a RAM root, or an image only reachable # behind the old root flashes as it did before. want_handoff() { need_handoff && return 0 [ "1" = "$update_rootfs" ] || return 1 [ "1" = "$skip_ramfs" ] && return 1 command -v pivot_root >/dev/null 2>&1 || return 1 inittab_hands_off || return 1 local f for f in "$([ "1" = "$update_kernel" ] && echo "$kernel_file")" "$rootfs_file"; do case "$(path_after_pivot "$f")" in /mnt/*) return 1 ;; esac done return 0 } # Can PID 1 be handed off at all? Asked before anything is written, where a # refusal still leaves the camera exactly as it was. # # The ::restart: entry is read by init at boot, so the file on flash is the # best evidence there is of what PID 1 will do with SIGQUIT. Without one, # busybox init ignores the signal, and the run could only reboot unwritten. check_handoff() { [ "1" = "$skip_ramfs" ] && die "--no_ramfs: this run must rewrite a mounted UBI volume, which cannot be done from the running system. Nothing was written." command -v pivot_root >/dev/null 2>&1 || die "No pivot_root: this run must rewrite a mounted UBI volume, which needs it. Nothing was written." inittab_hands_off || die "This run must rewrite a mounted UBI volume, and only PID 1 can let go of it: $INITTAB has no '::restart:/sbin/init' entry to hand it off with. Nothing was written." # release_old_root lets go of everything behind /mnt before the first # write, an SD card included, so only an image in /tmp is still there to # flash from (#2605). Only an image this run will write: on the ubifs # layout a --kernel given beside --rootfs is dropped, not flashed. local f for f in "$([ "1" = "$update_kernel" ] && echo "$kernel_file")" \ "$([ "1" = "$update_rootfs" ] && echo "$rootfs_file")"; do case "$(path_after_pivot "$f")" in /mnt/*) die "$f will not be reachable once the old root is released; copy it into /tmp and give that path instead. Nothing was written." ;; esac done return 0 } # Whether PID 1 can be handed off at all: its inittab, read at boot, has the # ::restart: entry that execs /sbin/init. Images before the UBI layouts had none. inittab_hands_off() { grep -qE '^[^#]*::restart:/sbin/init([[:space:]]|$)' "$INITTAB" 2>&3 } # Lay out what busybox init needs to exec into this ramfs as PID 1. # # init runs its ::shutdown: actions before it execs the ::restart: target, and # after the pivot it resolves them here. rcK is not here, which is right. But # `/bin/umount -a -f` would be -- the applet symlinks above put an umount in # /bin -- and it would take /tmp, holding the images, down with everything # else. The action names that absolute path, so the link moves to /sbin: init # no longer finds it, and the flash phase (verify_rootfs's loop mount, for # one) still does, through PATH. # # /sbin/init is the restart target init names, resolved in this root too. The # state the second phase needs cannot ride in the environment: init execs it # with init's environment, not ours. So it goes in a file, written after the # exports it records -- see hand_off_pid1. stage_handoff() { mkdir -p "$RAM_ROOT/sbin" 2>&3 || return 1 rm -f "$RAM_ROOT/bin/umount" 2>&3 ln -sf ../bin/busybox "$RAM_ROOT/sbin/umount" 2>&3 printf '#!/bin/sh\n. /sysupgrade.env\nexec /bin/busybox ash /sysupgrade\n' \ > "$RAM_ROOT/sbin/init" 2>&3 || return 1 chmod +x "$RAM_ROOT/sbin/init" 2>&3 [ -x "$RAM_ROOT/sbin/init" ] } # Let init get past a boot that never finished. # # busybox init looks at its signals only between inittab's stages, and the # ::sysinit: stage lasts until rcS exits. A foreground loop in /etc/rc.local # (S99 runs it from rcS) therefore holds SIGQUIT unread for as long as the # camera is up: the hand-off waits out handoff_wait, reboots with nothing # written, and every later attempt does the same. init is waiting on rcS's pid # alone, so ending rcS lets it reach the signal; what rcS left running is killed # by the hand-off along with everything else. release_boot_scripts() { local p cmd for p in "$WDOG_PROC"/[0-9]*; do cmd=$(tr '\0' ' ' < "$p/cmdline" 2>&3) || continue case "$cmd" in *'/etc/init.d/rcS'*) ;; *) continue ;; esac [ "1" = "$(awk '/^PPid:/ { print $2; exit }' "$p/status" 2>&3)" ] || continue echo_c 33 "The boot scripts have not finished (often a command in /etc/rc.local running in the foreground), and PID 1 cannot take over until they do: ending them." busybox kill -KILL "${p##*/}" 2>&3 done return 0 } # Hand PID 1 to the ramfs, and never come back. # # pivot_root has already moved PID 1's root and cwd here along with ours. What # still ties it to the flash is its own text -- busybox mapped from the old # root -- and the processes it supervises. SIGQUIT makes busybox init run its # shutdown actions, kill every other process (this one included), and exec # /sbin/init: ours. The second phase then starts as PID 1 with the old root # referenced by nothing, which is what lets its UBI volumes go. # # If PID 1 does not take it -- an inittab that changed under a running init -- # this survives the wait and reboots: nothing has been written. hand_off_pid1() { # Relative: the cwd is the new root (enter_ramfs cd'd into it), so this is # the /sysupgrade.env that /sbin/init sources. export -p > ./sysupgrade.env 2>&3 echo "export _handoff=1" >> ./sysupgrade.env echo_c 37 "\nHanding PID 1 over to the RAM root." # "This console goes quiet" was all it used to say, and over SSH it never # came back: the hand-off kills dropbear, so a flash that went fine looked # exactly like one that crashed. Say so, and where to watch instead. local urls=$(follow_urls) echo_c 33 "SSH, the WebUI and every other login end here; the flash carries on without them." if [ -n "$urls" ]; then echo_c 33 "Follow it from another machine (curl -sN, or a browser):" echo_c 36 "$urls" fi echo_c 33 "Progress also goes to the serial console. The camera reboots by itself when it is done." sync release_boot_scripts busybox kill -QUIT 1 sleep "${handoff_wait:-30}" echo_c 31 "PID 1 did not hand over; rebooting (nothing was flashed)" ./bin/busybox reboot -d 1 -f exit 1 } # Where follow_start will serve the log, one URL per line: every global IPv4 # address the camera has, except Wi-Fi. The hand-off kills wpa_supplicant, which # takes the association with it, so a wlan address would only be a promise of a # connection that times out. A wired address outlives it (measured on # hi3516ev300 with udhcpc -R): the deconfig script udhcpc runs on its way out # lives in the old root, which pivot_root has already taken from it. Empty when # nothing will listen. follow_urls() { [ -n "$FOLLOW_LOG" ] && [ -e ./bin/nc ] || return 0 local port= [ "80" = "$FOLLOW_PORT" ] || port=":$FOLLOW_PORT" ip -4 addr show scope global 2>&3 | sed -n 's/.*inet \([0-9.]*\)\/.* \([^ ]*\)$/\1 \2/p' | while read -r addr dev; do case "$dev" in wl*) continue ;; esac echo " http://$addr$port/" done } # Make a handed-off flash phase watchable. Called first thing as PID 1. # # From here every write goes to $FOLLOW_LOG, a file in the RAM /tmp: a file # cannot block and cannot raise SIGPIPE, so nothing that happens to a reader # can stall or kill PID 1 mid-write -- which a pipe through tee could. Readers # are separate processes that may die without consequence: # - `tail -f` puts it back on the serial console, where it went before; # - nc serves it on $FOLLOW_PORT, free now that majestic is gone, as a plain # HTTP response that never ends, so curl, a browser and plain nc all show # it as it grows. Colour codes and flashcp's \r updates are turned into # plain lines on the way out, for the browser -- by tr, not by a regex RS: # with one, busybox awk never passed on the last line before the reboot # (measured on hi3516ev300). # Best effort throughout: when any piece is missing the flash runs as before. follow_start() { [ -n "$FOLLOW_LOG" ] && : > "$FOLLOW_LOG" 2>&3 || return 0 busybox tail -n +1 -f "$FOLLOW_LOG" 2>&3 & exec >> "$FOLLOW_LOG" 2>&1 follow_tail=1 # nc -e runs one per connection; it finds the log beside itself. cat > "$FOLLOW_LOG.http" 2>&3 <<'EOF' #!/bin/sh printf 'HTTP/1.0 200 OK\r\nContent-Type: text/plain; charset=utf-8\r\nCache-Control: no-store\r\n\r\n' busybox tail -n +1 -f "${0%.http}" | busybox tr '\r' '\n' | busybox awk '{ gsub(/\033\[[0-9;]*m/, ""); print; fflush() }' EOF chmod +x "$FOLLOW_LOG.http" 2>&3 && busybox nc -ll -p "$FOLLOW_PORT" -e "$FOLLOW_LOG.http" 2>&3 & echo_c 33 "sysupgrade $scr_version on $(hostname 2>&3): flashing from RAM" } # Give the readers time to pass the last lines on before the reboot cuts them # off: busybox `tail -f` looks for new data once a second. follow_flush() { [ "1" = "$follow_tail" ] && sleep 2 return 0 } # Second phase, as PID 1: let go of the old root so its UBI volumes close. # # Lazy, because the old root holds the moved-out mount points' parents and # whatever else init had mounted under it; with every process that could hold # a reference gone, the lazy detach is final at once: UBIFS's put_super closes # its volumes, and the squashfs on a ubiblock device is the block device's last # opener, so ubiblock closes its reader too. release_old_root() { # Nothing is written before this answers. If the old root is still # attached, every volume under it is still open, and the kernel would go # down and the rootfs then fail -- a half upgrade. Refusing here reboots # onto the image that is still there, untouched. busybox umount -l /mnt 2>&3 || die "Could not let go of the old root; nothing was written." sync echo 3 > /proc/sys/vm/drop_caches 2>&3 } # A local image path as the RAM phase will see it. pivot_root parks the old # root at /mnt, so an image on the SD card at /mnt/mmcblk0p1 is reached at # /mnt/mnt/mmcblk0p1 from there, and a relative one not at all once enter_ramfs # has cd'd away (#2605). /tmp is the exception: enter_ramfs moves that mount # across whole. Empty in, empty out. path_after_pivot() { [ -n "$1" ] || return 0 local p p=$(realpath "$1" 2>&3) || p=$1 case "$p" in /tmp/*) echo "$p" ;; /*) echo "/mnt$p" ;; *) echo "/mnt$(pwd)/$p" ;; esac } # Move the whole flashing phase into a RAM filesystem and re-exec there, so that # nothing it needs afterwards lives on the partition it is about to overwrite. # # pivot_root on its own is not enough: it changes what paths resolve to, but the # already-running shell keeps its text mapped from the old squashfs inode. The # re-exec is what actually gets us off the doomed partition, which is why this # ends in `exec` and why the caller treats a return as failure. # # On success this never returns. On any failure it returns and the caller # flashes in place, exactly as before — a camera that cannot build a ramfs is # still better off upgrading than not. enter_ramfs() { # Before touching it: is the mount point already part of the image? Recorded # here rather than beside the mkdir below because every early return leads to # ramfs_discard too, and it needs the answer on all of them. [ -d "$RAM_ROOT" ] && ram_root_shipped=1 || ram_root_shipped= [ "1" = "$skip_ramfs" ] && return 1 command -v pivot_root >/dev/null 2>&1 || return 1 echo_c 37 "\nMoving the flash phase into RAM" # Not under /tmp: /tmp is moved into the new root below, and a mount point # cannot be moved underneath itself. mkdir -p "$RAM_ROOT" 2>/dev/null || return 1 # A previous attempt that could not fully unwind may have left one here; # mounting over it would stack rather than replace. Only ever clear a tmpfs # of our own though: RAM_ROOT is overridable from the environment (the test # harness relies on that), and an unconditional umount would happily detach # real storage if it were pointed at some. if grep -q "^tmpfs $RAM_ROOT tmpfs" /proc/mounts 2>/dev/null; then umount "$RAM_ROOT" 2>/dev/null fi # A cap, not a reservation: the staged root is busybox plus its libraries and # the script, around 1.5 MB. Kept small because the cameras that need this # most have 34 MB of RAM in total, and /tmp (holding the unpacked image) is # a separate mount that does not draw on this budget. mount -t tmpfs -o size=8m tmpfs "$RAM_ROOT" 2>/dev/null || return 1 mkdir -p "$RAM_ROOT/bin" "$RAM_ROOT/lib" "$RAM_ROOT/dev" "$RAM_ROOT/proc" \ "$RAM_ROOT/tmp" "$RAM_ROOT/sys" "$RAM_ROOT/mnt" 2>/dev/null || return 1 # busybox is dynamically linked here, so the loader and every library it # names have to come too, or the re-exec dies with the rootfs still mapped. cp /bin/busybox "$RAM_ROOT/bin/" 2>/dev/null || return 1 # Plain cp follows a link, so a chain like ld-uClibc.so.0 -> .so.1 -> # -1.0.45.so lands as one file under the name the ELF asks for. local lib for lib in $(busybox_libs); do cp "$lib" "$RAM_ROOT/lib/" 2>/dev/null done # busybox dispatches on argv[0], so without symlinks every tool the flash # phase reaches for -- od, xxd, cut, grep, losetup, flashcp -- is simply "not # found" once we are in the new root. The first hardware run pivoted # perfectly and then could not find a single one of them. local applet for applet in $("$RAM_ROOT/bin/busybox" --list 2>/dev/null); do ln -sf busybox "$RAM_ROOT/bin/$applet" 2>/dev/null done # --list is a build option; fall back to what the flash phase actually uses. for applet in sh ash awk basename cat cut dd flash_eraseall flashcp grep \ head ln losetup ls mkdir mount od printf reboot rm rmdir sed sleep \ stat sync tail timeout umount xxd; do [ -e "$RAM_ROOT/bin/$applet" ] || ln -sf busybox "$RAM_ROOT/bin/$applet" 2>/dev/null done # If the essentials are not there, the ramfs is worse than useless: better to # hand back to the in-place path than to pivot into a root with no tools. for applet in sh flashcp reboot; do [ -e "$RAM_ROOT/bin/$applet" ] || return 1 done cp "$0" "$RAM_ROOT/sysupgrade" 2>/dev/null || return 1 chmod +x "$RAM_ROOT/sysupgrade" 2>/dev/null # Can the staged root run anything at all? Asked now, while nothing has # moved and a refusal still falls back: past the pivot the same missing # loader only shows as an exec that fails with no root to return to (#2605). chroot "$RAM_ROOT" /bin/busybox true 2>/dev/null || return 1 if [ "handoff" = "$1" ]; then stage_handoff || return 1 fi # The last moment to refuse: the stage is built, nothing has moved and # nothing is written. Not `return 1` -- the in-place fallback that would # take runs the shell itself off the flash being erased, which is the same # failure with less in RAM. if ! check_flash_ram; then ramfs_discard die "$flash_ram_refusal" fi # Where --kernel / --rootfs will be once the old root sits at /mnt, worked # out while /tmp is still where the caller named it. Not kernel_file and # rootfs_file themselves: a pivot that fails below falls back to flashing # from here, where the original paths are still right. ramfs_kernel_file=$(path_after_pivot "$kernel_file") ramfs_rootfs_file=$(path_after_pivot "$rootfs_file") # Carry over the mounts the flash still needs: /dev for the MTD nodes, /proc # for get_device and sysrq, /tmp for the unpacked images. Moving rather than # binding leaves nothing pointing back into the old root. # # /dev goes first and takes the devtmpfs with it, so from here until the pivot # the name /dev/null resolves inside the old root, where there is no such node # — every redirection below therefore goes to fd 3 instead. See the `exec 3>` # at the top of the script. mount -o move /dev "$RAM_ROOT/dev" 2>&3 || { ramfs_unwind; return 1; } mount -o move /proc "$RAM_ROOT/proc" 2>&3 || { ramfs_unwind; return 1; } mount -o move /tmp "$RAM_ROOT/tmp" 2>&3 || { ramfs_unwind; return 1; } mount -o move /sys "$RAM_ROOT/sys" 2>&3 # State the second phase cannot recompute once the old root is behind it. # NOT _ramfs_phase: that one is set only after the pivot has actually # happened. Setting it here leaks into the fallback, where it would tell # die() to reboot on a failure that never touched flash -- the exact # false-success this script was fixed to stop doing. export model kernel_file rootfs_file kernel_device fw_dev flash_type vendor export update_kernel update_rootfs clear_overlay image_combined exit_update export skip_soc skip_ver skip_reboot skip_md5 web_update silent_update export live_flash_dirty root_on_flash flash_touched services_stopped # remote_update: verify_rootfs branches on it for an unmountable rootfs. # lock_owned: die() needs it to know the lock is ours to remove. export remote_update lock_owned split_overlay_mounted # wdog_userspace_owned: watchdog_keep must not claim a device nobody was # petting, and the scan behind that answer needs a /proc this pivot is about # to move and a majestic it is about to leave behind /mnt. export wdog_userspace_owned # ram_root_shipped: the second phase reclaims the old root's mount point and # needs the same answer ramfs_discard does. It cannot recompute it -- by then # $RAM_ROOT names a directory in the NEW root, not the one being judged. export ram_root_shipped export kernel_version system_version rootfs_version mount_wait abort_wait # rootfs_unmountable/rootfs_mount_err: read_candidate_build's mount of the # same image failed; verify_rootfs takes its word rather than retrying. export rootfs_unmountable rootfs_mount_err # system_platform: check_platform runs in the ramfs, which has no # /etc/os-release to read it from (#2484). # device_pinned: the image was fetched under this device's own name. export system_platform device_pinned # platform_soc/image_soc: the pre-alias SoC check_soc also accepts, and the # one the unpacked images are named for -- found from /tmp before the pivot. export platform_soc image_soc # The UBI layout and its volumes, resolved on the whole system: the flash # phase may have no /sys to resolve them from. export ubi_layout ubi_rootfs_dev ubi_data_dev UBI_SYS INITTAB overlay_kb # archive: verify_ubifs_rootfs does not count a local archive as pinned. export archive # ramfs_kernel_file/ramfs_rootfs_file: the image paths as the pivot will # leave them, worked out above before the mounts moved. export ramfs_kernel_file ramfs_rootfs_file cd "$RAM_ROOT" || { ramfs_unwind; return 1; } # After this the old root is at ./mnt and still mounted — deliberately: the # MTD partitions are addressed through /dev, not through it. if ! pivot_root . mnt 2>&3; then ramfs_unwind return 1 fi # Past the pivot there is no way back, so this is where the second phase is # declared -- see the note above the other exports. export _ramfs_phase=1 [ "handoff" = "$1" ] && hand_off_pid1 # `chroot .` is what actually re-points this process's root; pivot_root only # moved the mounts. Then exec so the shell interpreting the rest is the copy # in RAM, not the one on the flash we are about to erase. exec ./bin/busybox chroot . /bin/busybox ash /sysupgrade # exec only returns on failure, and the old root is behind ./mnt by now, so # there is no system left to hand back to the caller -- returning would send # the fallback off to flash against a root it can no longer see. Nothing has # been written yet, so a reboot simply comes back on the current firmware. echo_c 31 "Cannot exec inside the ramfs; rebooting (nothing was flashed)" ./bin/busybox reboot -d 1 -f exit 1 } # Run a flash tool, reducing its progress output to the line-count form the # WebUI polls when it is driving us (-s). # # The status has to survive that pipe. A pipeline reports the status of its LAST # command, so `busybox flashcp | awk` returns awk's 0 no matter how the write # went -- which made `|| die` on a caller dead code in exactly the mode the # WebUI drives, and turned a flashcp that refused to write anything into a # reported-successful upgrade (#2426). busybox ash has `set -o pipefail`; dash, # which .github/scripts/test_sysupgrade.sh runs this script under, does not. So # carry the status out of the pipeline by hand. # # An unreadable status file means failure, not success: this is the guard that # stops a silent flash lying about itself, so it must not fail open. /tmp is # writable on every path that reaches here -- create_lock has already touched # $LOCK_FILE in it, and inside the ramfs it is the tmpfs the images came from. set_progress() { if [ "1" = "$silent_update" ]; then local rcf=/tmp/.sysupgrade-write-rc rm -f "$rcf" { busybox "$@"; echo $? > "$rcf"; } | awk '{print NR, $1}' local st=$(cat "$rcf" 2>/dev/null) rm -f "$rcf" return ${st:-1} fi busybox "$@" } sync_time() { echo_c 37 "\nSynchronizing time" # Bound the sync: with no reachable NTP server `ntpd -q` can block for # minutes and strand the upgrade at this step (WebUI issue #120). An # unsynced clock is still not fatal: every fetch after this verifies the # server certificate, and when that fails on the date probe_url falls back # to the HTTP Date header (clock_from_http) rather than to skipping the # verification. if command -v timeout >/dev/null 2>&1; then timeout 30 ntpd -Nnq else ntpd -Nnq fi echo_c 33 "$(date)" } self_update() { if echo "$args" | grep -E "\-(k|r|w|url|channel|build)" >/dev/null 2>&1; then sync_time # The WebUI flow must not re-exec a freshly downloaded sysupgrade: a # pre-patch master copy would reject --web. Time is synced; carry on. [ "1" = "$web_update" ] && return echo -e "\nChecking for sysupgrade update..." # What lands in /tmp/sysupgrade is exec'd as root a few lines down, so: # verified TLS, https only (no redirect may step down to http), -f so an # error page is not kept, and staged through a rename so a transfer that # died half-way cannot leave a truncated script that still carries the # shebang and a foreign scr_version. And only the copy THIS run fetched is # ever looked at: whatever an earlier run left behind (one made with # --insecure, say) is removed first, so a failed fetch cannot fall # through to it. Removing the file the running shell may itself have # been exec'd from is fine -- it keeps reading the unlinked inode. rm -f /tmp/sysupgrade /tmp/sysupgrade.part if curl -s -f -L --proto =https --proto-redir =https $curl_insecure -o /tmp/sysupgrade.part \ "https://raw.githubusercontent.com/OpenIPC/firmware/master/general/overlay/usr/sbin/sysupgrade" \ && mv /tmp/sysupgrade.part /tmp/sysupgrade \ && grep -q "#!/bin/sh" /tmp/sysupgrade; then dstv=$(grep scr_version /tmp/sysupgrade | head -1 | cut -f 2 -d '=') if ! [ "$scr_version" = "$dstv" ]; then echo "A new version is available, trying to activate updated script..." chmod +x /tmp/sysupgrade echo -e "Done. Restarting...\n" exec /tmp/sysupgrade $args exit 1 else echo "Same version. No update required." fi else rm -f /tmp/sysupgrade.part echo -e "\nVersion checking failed, proceeding with the installed version." fi else echo -e "\nOffline operations." fi } check_sdcard() { echo_c 33 "\nUnmounting SD card" stoplist="autoupdate-kernel.img autoupdate-rootfs.img autoupdate-uboot.img" local busy=0 while [ -n "$(mount | grep /mnt/mmc)" ]; do local d=$(mount | grep /mnt/mmc | tail -1 | awk '{print $3}') echo_c 34 "$d" local f for f in $stoplist; do echo "- checking for $d/$f" if [ -f "$d/$f" ]; then # die(), not a bare exit. check_sdcard runs after create_lock # and free_resources, so leaving straight from here left the # camera holding everything they had just taken: the lock file, # so every later run answered "Another sysupgrade process is # already running!" until a reboot cleared /tmp; syslogd, klogd, # ntpd, crond and fake-hwclock stopped; majestic gutted by the # SIGQUIT free_resources sends, so no video until someone # rebooted; and, since #2417, a collector left holding "logging # stops here until this camera returns" for a camera that was # never touched. Nothing has been written at this point, so # die() takes its no-reboot branch and puts all of it back. die "Cannot upgrade! Recovery file $d/$f found on the mounted SD card. Remove the card from the slot and restart sysupgrade." fi done # Checked, and bounded. A busy card -- majestic recording to it, a # shell sitting in it -- refuses the umount, and an unchecked loop then # found the same mount again and spun forever, holding the lock. local entry=$(grep " $d " /proc/mounts | tail -1) if umount $d 2>/dev/null; then [ -n "$entry" ] && sd_unmounted="$sd_unmounted $entry" else busy=$((busy + 1)) if [ "$busy" -ge 5 ]; then die "Cannot unmount $d: it is in use$(sd_holders $d). Stop whatever is using the card, or remove it, and restart sysupgrade." fi sleep 1 fi done } # Who has files open under a mount point, as " (by name[pid] ...)", or nothing # when that cannot be told. For the message only. sd_holders() { local p f who= for p in /proc/[0-9]*; do for f in "$p"/fd/*; do case "$(readlink "$f" 2>/dev/null)" in "$1"/*) who="$who $(cat "$p/comm" 2>/dev/null)[${p#/proc/}]" break ;; esac done done [ -n "$who" ] && echo " (by$who)" } create_lock() { if [ -f $LOCK_FILE ]; then # The lock names its owner, so a run that was killed -- an ssh session # dropped mid-upgrade -- no longer refuses every later one until a # reboot clears /tmp. A lock whose owner is gone is taken over; one that # names no owner (an older sysupgrade's) or a live one is respected. local owner owner=$(cat $LOCK_FILE 2>/dev/null) if [ -n "$owner" ] && ! kill -0 "$owner" 2>/dev/null; then echo "Taking over a stale lock left by sysupgrade process $owner" rm -f $LOCK_FILE else echo_c 31 "\nAnother sysupgrade process is already running!" [ -z "$owner" ] && echo "If none is, remove $LOCK_FILE and try again." exit 1 fi fi # Created with noclobber, so of two runs taking over the same stale lock # only one gets it; the other finds it again and is refused. if ! (set -C; echo $$ >$LOCK_FILE) 2>/dev/null; then echo_c 31 "\nAnother sysupgrade process is already running!" exit 1 fi # Ownership, so die() can tell "my lock" from "somebody else's". lock_owned=1 } get_device() { echo -n "/dev/$(grep "\"$1\"" /proc/mtd | cut -d: -f1)" } # Bytes in the MTD partition behind $1 (/dev/mtdN), or nothing when it cannot be # told -- a name that is not an mtd device, or a /proc/mtd that does not list it. # # /proc/mtd rather than /sys/class/mtd/mtdN/size: get_device already depends on # /proc being there, while /sys is carried into the ramfs pivot best-effort # (enter_ramfs moves it without checking), so sysfs can simply be absent exactly # where this is needed. partition_size() { case "$1" in # A UBI volume holds reserved_ebs LEBs of usable_eb_size bytes each; # ubiupdatevol refuses anything larger (UBI_IOCVOLUP's -EINVAL). /dev/ubi[0-9]*_[0-9]*) local d="$UBI_SYS/${1#/dev/}" ebs usable ebs=$(cat "$d/reserved_ebs" 2>&3) && usable=$(cat "$d/usable_eb_size" 2>&3) || return 0 [ -n "$ebs" ] && [ -n "$usable" ] && echo $((ebs * usable)) return 0 ;; /dev/mtd[0-9]*) ;; *) return 0 ;; esac local hex=$(grep "^${1#/dev/}:" /proc/mtd 2>/dev/null | cut -d' ' -f2) [ -n "$hex" ] && echo $((0x$hex)) } # The character device of the UBI volume named $1 (/dev/ubi0_1), or nothing. # # By name, never by number: ubinize numbers the volumes, but a camera whose # layout came from somewhere else -- U-Boot's `ubi create`, defib's installer # -- numbers them in the order they were made. ubi_volume() { local d for d in "$UBI_SYS"/ubi[0-9]*_[0-9]*; do [ -f "$d/name" ] || continue [ "$(cat "$d/name" 2>&3)" = "$1" ] && { echo "/dev/${d##*/}"; return 0; } done return 1 } # Write image $1 to flash target $2: a UBI volume through ubiupdatevol, which # replaces the volume's contents and trims the 0xFF tail of every LEB (#2519); # anything else is an MTD partition and goes through flashcp as it always has. write_image() { case "$2" in /dev/ubi[0-9]*_[0-9]*) ubi_update "$2" "$1" ;; *) set_progress flashcp -v "$1" "$2" ;; esac } # ubiupdatevol volume $1 from image $2, saying how far it has got. # # flashcp -v prints its own progress; busybox ubiupdatevol has no such option # and is silent for as long as the rootfs takes (4 s for 17 MB on hi3516ev300), # which made the one step that matters the one that looked hung. It moves the image onto its # stdin and reads it one LEB at a time, each written before the next is read, # so the file position of its fd 0 is how far the write has got, to within a # LEB. Read from outside, the write is the same call it always was. # # Not under -s: the WebUI counts set_progress's lines and expects only those. ubi_update() { [ "1" = "$silent_update" ] && { set_progress ubiupdatevol "$1" "$2"; return; } local size=$(stat -c %s "$2" 2>&3) pid st watcher busybox ubiupdatevol "$1" "$2" & pid=$! ubi_progress "$pid" "$size" & watcher=$! wait "$pid" st=$? # It stops once the writer is reaped, which the wait above just did. wait "$watcher" [ "$st" -eq 0 ] && [ -n "$size" ] && echo "Written: 100% ($(mb "$size") MB)" return "$st" } # Print the write's progress every second while it changes. $1 the writer's # pid, $2 the image's size in bytes. ubi_progress() { local pid=$1 size=$2 pos pct last= [ -n "$size" ] && [ "$size" -ge 100 ] || return 0 while [ -d "/proc/$pid" ]; do pos=$(awk '$1 == "pos:" { print $2 }' "/proc/$pid/fdinfo/0" 2>&3) if [ -n "$pos" ]; then pct=$(( pos / (size / 100) )) [ "$pct" -gt 99 ] && pct=99 [ "$pct" = "$last" ] || echo "Written: $pct% ($(mb "$pos") of $(mb "$size") MB)" last=$pct fi sleep 1 done } # Bytes as megabytes to one decimal place, in integer arithmetic. mb() { local t=$(( $1 * 10 / 1048576 )) echo "$(( t / 10 )).$(( t % 10 ))" } # Record that a write is about to land on flash that backs the RUNNING # filesystem, so reboot_system knows --no_reboot can no longer be honoured. # # Called BEFORE the write, not after: flashcp erases the partition before it # writes, so an interrupted or failed write is exactly as fatal as a completed # one — and do_update_firmware's `|| die` reaches reboot_system by that path. mark_live_flash_dirty() { mark_flash_touched [ "1" = "$root_on_flash" ] && live_flash_dirty=1 return 0 } # Record that flash has been erased or written, so die() knows a reboot is now # the least-bad option. Broader than live_flash_dirty: it also covers the kernel # partition, which is not mounted (so it never forces a reboot on the success # path) but is just as much a point of no return once flashcp has started on it. mark_flash_touched() { flash_touched=1 return 0 } get_kernel_version() { # A FIT image (DTB magic d00dfeed) has no uImage timestamp at offset 8; # the legacy read would print a bogus 1970 date. Used for display and, on # split-layout boards, the kernel compare — keep it honest for both. [ -e "$1" ] || { echo "n/a"; return; } [ "$(xxd -p -l 4 "$1" 2>/dev/null)" = "d00dfeed" ] && { echo "n/a (FIT)"; return; } local ts=$(xxd -l 4 -s 8 -p "$1" 2>/dev/null | xargs) [ -n "$ts" ] || { echo "n/a"; return; } # Display the epoch via `-d @SECONDS`. A bare positional `@SECONDS` is # treated by busybox as a clock-SET request ("date: can't set date") and # yields nothing, which would make compare_versions see two empty strings # and wrongly skip the kernel update. date -d @$((0x$ts)) "+%H:%M:%S %Y-%m-%d" } get_system_info() { vendor=$(ipcinfo -v) # Upgrade identity is the BUILD platform, not the chip. BUILD_PLATFORM is # ${soc}_${variant}[_${device}]; the SoC model is its leading token (no # SOC_MODEL contains '_'). This decouples one published image (e.g. the # single cv6xx build) from the per-die id a camera reports via # fw_printenv/ipcinfo. soc is kept for display and as the pre-1.0.51 # fallback; fw_printenv is no longer fatal (it returns nothing when the # U-Boot env partition is empty/unwritten) as long as BUILD_PLATFORM is set. model=$(grep '^BUILD_PLATFORM=' /etc/os-release 2>/dev/null | cut -d= -f2 | cut -d_ -f1) soc=$(fw_printenv -n soc 2>/dev/null) [ -z "$model" ] && model="$soc" [ -z "$model" ] && die "Cannot determine SoC: no BUILD_PLATFORM and no U-Boot 'soc'" platform_soc=$model resolve_model_alias image_soc=$model detect_ubi_layout # A ubiblock camera running an image from before the hand-off has no # ::restart: entry, and only the upgrade being run would ship one. Its # rootfs was written through gluebi's mtd until now, which works with the # volume mounted (gluebi writes beside ubiblock's reader), so keep doing # that rather than refusing every upgrade. The next image brings the # entry, and the UBI path, with it. if [ "$ubi_layout" = "ubiblock" ] && ! nand_layout_moved && ! inittab_hands_off && [ "/dev/" != "$(get_device "rootfs")" ]; then ubi_layout= fi if [ -n "$ubi_layout" ]; then # Resolved here, on the whole system: the flash phase may run in a ramfs # where /sys came across best-effort, or not at all. kernel_device=$(ubi_volume kernel) ubi_rootfs_dev=$(ubi_volume rootfs) ubi_data_dev=$(ubi_volume rootfs_data) # ubifs: no kernel device; the running kernel is the uImage in the # rootfs, and the settings are what an upgrade carries across. if [ "$ubi_layout" = "ubifs" ]; then kernel_device=/rom/boot/uImage overlay_kb=$(df -k /overlay 2>&3 | awk 'NR == 2 { print $3 }') fi else kernel_device=$(get_device "kernel") [ "$kernel_device" = "/dev/" ] && kernel_device=$(get_device "firmware") fi kernel_version=$(get_kernel_version "$kernel_device") system_version=$(get_system_version "") system_platform=$(get_system_platform "") flash_type=$(ipcinfo -F) # 1 when the running filesystem is served from the flash this run may # rewrite. init(1) mounts the rw overlay from "rootfs_data" and pivots onto # the squashfs the kernel booted from, so on a flash root BOTH partitions # this script erases are backing the live mount. # # Only a `root=` naming something that is NOT flash proves the safe case, so # the test is anchored to that token. general/overlay/init greps for a bare # `mmcblk`/`ram` anywhere in the line — harmless when the answer only picks a # mount type, but here a stray substring (an mtdparts label, an allocator # name) would certify a flash-root camera as safe and brick it. Anything not # positively proven to live elsewhere — NAND `root=ubi0:rootfs`, an # unreadable /proc/cmdline — counts as flash, which at worst costs a reboot. root_on_flash=1 grep -qE '(^|[[:space:]])root=(/dev/)?(nfs|mmcblk|ram)' /proc/cmdline 2>/dev/null \ && root_on_flash=0 } # Which NAND layout this camera boots, when its rootfs is a UBI volume. Empty # for everything else -- NOR, and the retired split layout (a raw `kernel` # partition beside the UBI device, see is_split_nand), which no upgrade writes. # # ubifs one UBIFS `rootfs` volume holding the root filesystem AND the # kernel (/boot/fitImage, /boot/uImage), plus `rootfs_data`; no # `kernel` volume and no `kernel` partition (root=ubi0:rootfs). # Fetches the -nand- package: rootfs.ubifs., with # fitImage. beside it as the SoC witness. An upgrade writes # the one image and resizes the volumes to it (rewrite_ubifs). # ubifs-kvol the earlier UBIFS layout with the kernel in a `kernel` volume of # its own. Retired: a run that would write it is refused, and # the camera is reinstalled from U-Boot. # ubiblock kernel volume = uImage, rootfs volume = squashfs mounted through # ubiblock (root=/dev/ubiblockX_Y). Its volumes hold exactly the # NOR artifacts, so it fetches the NOR package. # # The rootfs volume is in use for as long as the camera runs: UBIFS or # ubiblock holds it open under init's overlay root, and ubiupdatevol needs the # volume to itself (see need_handoff). So a rootfs write on any of them has # PID 1 leave the flash first. See enter_ramfs. detect_ubi_layout() { ubi_layout= [ -n "$(ubi_volume rootfs)" ] || return 0 if grep -qE '(^|[[:space:]])root=ubi[0-9]*:rootfs([[:space:]]|$)' /proc/cmdline 2>&3; then if [ -n "$(ubi_volume kernel)" ]; then ubi_layout=ubifs-kvol elif ! grep -q '"kernel"$' /proc/mtd 2>&3; then ubi_layout=ubifs fi elif grep -qE '(^|[[:space:]])root=/dev/ubiblock' /proc/cmdline 2>&3 && [ -n "$(ubi_volume kernel)" ]; then ubi_layout=ubiblock fi return 0 } # The retired split NAND layout: the kernel a uImage in a raw `kernel` MTD # partition beside the UBI device (hinand:1024k(boot),1024k(env),8192k(kernel), # -(ubi)), and a UBIFS root in it with no `kernel` volume. detect_ubi_layout # does not recognise it, so it would fall to the MTD path, which reaches the # rootfs through gluebi's "rootfs" mtd and writes a NOR squashfs over the # mounted UBIFS volume -- a camera that no longer boots. Its bootloader is # retired too: the current one boots the kernel from the UBI `kernel` volume # and never reads the raw partition, so no part of an upgrade is safe here. # Reinstalling from U-Boot moves the camera to the UBI-only layout. is_split_nand() { [ -z "$ubi_layout" ] || return 1 [ -n "$(ubi_volume rootfs)" ] && [ -z "$(ubi_volume kernel)" ] || return 1 grep -qE '(^|[[:space:]])root=ubi[0-9]*:rootfs([[:space:]]|$)' /proc/cmdline 2>&3 || return 1 grep -q '"kernel"$' /proc/mtd 2>&3 } # The SoCs whose NAND images moved to the kernel-in-rootfs layout (ubifs). # On these the older NAND layouts with a kernel volume of their own are # retired and reinstalled; every other SoC keeps its ubiblock layout, which is # still what its NAND images are (SigmaStar, Rockchip). nand_layout_moved() { case "$model" in gk7205v500|gk7205v510|gk7205v530|hi3516ev200|hi3516ev300|hi3518ev300|hi3516dv200) return 0 ;; esac return 1 } # The package flavour this camera installs from, as the manifest spells it. pkg_flash() { [ "$ubi_layout" = "ubifs" ] && echo nand || echo nor } get_system_version() { grep "GITHUB_VERSION" "$1/etc/os-release" | head -1 | cut -d= -f2 | sed 's/"//g' } get_system_build() { grep "BUILD_OPTION" "/etc/os-release" | head -1 | cut -d= -f2 } get_system_platform() { grep '^BUILD_PLATFORM=' "$1/etc/os-release" 2>/dev/null | head -1 | cut -d= -f2 } # Where a plain -k/-r fetches from when the U-Boot env names nothing. A builder # per-device image (__) is published under its full # platform, ${BUILD_PLATFORM}-nor.tgz, and keying on the variant alone sent it # to the generic openipc.-nor-lite.tgz -- an image that passes every SoC, # size and checksum check and loses the profile's network driver and mtdparts # (#2484). The profile writes this URL into `upgrade` once, on first boot, so a # lost or never-written env lands here. Verbatim platform, not the aliased # $model: it is the name builder published under. default_upgrade_url() { local plat osr repo=builder plat=$(get_system_platform) case "$plat" in *_*_*) nor_split_url "https://github.com/OpenIPC/builder/releases/download/latest/$plat-$(pkg_flash).tgz" return ;; esac osr=$(get_system_build) case "$osr" in lite|ultimate|neo) repo=firmware ;; esac # A bootloader that boots the split NOR layout takes firmware.bin, from its # own archive: the plain one keeps the fixed-slot images every older # camera and installer expects. local u="https://github.com/OpenIPC/$repo/releases/download/latest/openipc.$model-$(pkg_flash)-$osr.tgz" # Wherever the archive lives: a board built split-only (hi3516ev300, # hi3518ev300, hi3516cv6xx) publishes -split for dev and glibc too. u=$(nor_split_url "$u") echo "$u" } is_device_platform() { case "$1" in *_*_*) return 0 ;; esac return 1 } # A device profile is what makes some cameras reachable at all -- its WiFi # driver, its mtdparts over a vendor U-Boot's layout -- and the SoC stamp cannot # tell a profile image from the generic one it was built on. So a camera running # a __ build takes only a rootfs stamped for the same # device (#2484). Compared past the SoC token, like the manifest lookup, so SoC # aliases do not matter. Moving onto a profile, or between stock variants, is # left alone: nothing that makes the camera reachable is lost there. # # Prints its verdict; a non-zero return is a refusal, left to the caller to # raise once it has released the mount. An image from before BUILD_PLATFORM # was stamped (1.0.51) has nothing to compare and passes, as it always did. check_platform() { local have=$system_platform want is_device_platform "$have" || return 0 want=$(get_system_platform "$1") [ -z "$want" ] && echo "Image carries no BUILD_PLATFORM; cannot compare it with '$have'" && return 0 [ "${want#*_}" = "${have#*_}" ] && echo "Platform OK" && return 0 [ "1" = "$skip_soc" ] && echo "Skip platform validation (image '$want', running '$have')" && return 0 echo "Wrong platform! (image '$want' != running '$have') This camera runs a device profile; the image would drop it. Nothing was written. Pass --force_soc to flash it anyway." return 1 } # The build this camera is running now. Beside get_system_build rather than # spelled a third way at each call site: --list-builds had its own grep|cut and # free_resources nearly grew another, which is two places to find when the # os-release format next changes. get_build_id() { grep '^BUILD_ID=' /etc/os-release 2>/dev/null | head -1 | cut -d= -f2 } # The platform key this camera's builds are listed under in the manifest: the # leading SoC token of BUILD_PLATFORM rewritten to the canonical $model, any # rich _variant_device suffix builder uses kept; ${model}_ for # pre-1.0.51 images, which carry no BUILD_PLATFORM. # # Except where the manifest lists nothing under that key and does list the # camera's own BUILD_PLATFORM: builder publishes a device profile under the # chip it was built for, so a gk7205v210 profile is gk7205v210_<...> there # while $model has been aliased to gk7205v200. Checked on a lab gk7205v210 # profile against builder's manifest. manifest_platform() { local p=$(get_system_platform) k [ -n "$p" ] || { echo "${model}_$(get_system_build)"; return 0; } k="${model}_${p#*_}" if [ "$k" != "$p" ] && [ -s "$MANIFEST_CACHE" ] && ! awk -v k="$k" '$2==k { f = 1; exit } END { exit !f }' "$MANIFEST_CACHE" && awk -v k="$p" '$2==k { f = 1; exit } END { exit !f }' "$MANIFEST_CACHE"; then k=$p fi echo "$k" } # Every build the fetched manifest lists for this camera, newest first. manifest_builds() { awk -v p="$(manifest_platform)" -v f="$(pkg_flash)" \ '$2==p && ($3==f || (f=="nor" && $3=="nor_split")) && !seen[$1]++ { print $1 }' "$MANIFEST_CACHE" } # One field of the os-release under root $1, quotes dropped. release_field() { grep "^$2=" "$1/etc/os-release" 2>/dev/null | head -1 | cut -d= -f2- | sed 's/"//g' } # The build this run is about to write, read out of the candidate image while # nothing is written yet, for the banner and the history (#2676). # # verify_rootfs reads the same os-release, but it runs in the flash phase -- # inside the ramfs, past the last moment an operator can still note anything # down or press Ctrl-C. So the read is made here, before the pivot. # # The mount also gets the image's SoC stamp and device profile, so they are # judged here, as verify_rootfs judges them: there a refusal is a reboot with # nothing written, here it leaves the camera as it was -- and comes before the # history records an upgrade that is never going to happen. Seen on a lab # gk7205v210 builder profile, whose image says gk7205v210 to a model aliased # to gk7205v200: the camera rebooted, and its history named the build it had # refused. # # Fails open, like every other look inside the image before the write: an # image this kernel cannot mount is not refused for it. A UBIFS rootfs cannot # be read without UBI under it at all; then the build the manifest resolved, # if any, is the best that can be said. A squashfs that did not mount is # remembered, so verify_rootfs does not wait out the same mount a second time # -- on a mount that hangs, that is mount_wait seconds each. read_candidate_build() { local x n loop err ofs= soc= verdict= refusal= y=/tmp/rootfs.peek to_build=$build_id to_version= to_note= kernel_only= if [ "1" = "$image_combined" ]; then # firmware.bin: the uImage, then the squashfs at the next 64 KiB boundary. x=$stage_dir/firmware.bin.$image_soc n=$(nor_split_blocks "$x") && ofs="-o $((n * 65536))" elif [ "1" = "$update_rootfs" ]; then x=$(rootfs_image) else # The rootfs carries the BUILD_ID, and it is not being written: the # camera stays on the build it runs, with another kernel under it. kernel_only=1 return 0 fi [ -f "$x" ] || return 0 if is_ubifs "$x"; then to_note="UBIFS image: not readable before it is written" return 0 fi if mkdir -p "$y" && loop=$(losetup -f) && err=$(losetup $ofs "$loop" "$x" 2>&1) && err=$(mount_rootfs "$loop" "$y"); then to_build=$(release_field "$y" BUILD_ID) to_version=$(release_field "$y" GITHUB_VERSION) # firmware.bin's SoC is its uImage's, which check_combined_fits read. # Its device profile is only in the squashfs, and nothing else on the # split path looked at it, so it is judged here like any rootfs's # (#2689 review): a profile camera must not take another profile's # firmware.bin. [ "1" != "$image_combined" ] && soc=$(head -1 "$y/etc/hostname" 2>&3 | cut -d- -f2) verdict=$(check_platform "$y") || refusal=$verdict umount "$y" else # As in verify_rootfs: a KILLed mount may still be attached. umount "$y" 2>/dev/null [ "1" = "$image_combined" ] || { rootfs_unmountable=1; rootfs_mount_err=$err; } to_note="not readable: this kernel cannot mount the image" fi [ -n "$loop" ] && losetup -d "$loop" 2>/dev/null rmdir "$y" 2>/dev/null [ -n "$refusal" ] && die "$refusal" [ -n "$soc" ] && check_soc "$soc" [ -n "$to_build" ] || to_build=$build_id return 0 } # The from -> to block, printed once every image is on the camera and checked, # and before the first write: the last point at which the build being left is # still on screen and the run can still be abandoned for free (#2676). Users # upgrade nightly by nightly and, once a build misbehaves, often cannot say # which one they came from -- --build can take them back, but only to a build # they can name. # # Warn, never ask: the WebUI and scripts run this non-interactively, and with # --web the block simply reaches /ws/upgrade with the rest of the log. print_transition() { local from=$(get_build_id) to rb from_build=${from:-unknown} to=${to_build:-unknown} if [ "1" = "$kernel_only" ]; then to="$from_build, with the kernel from ${to_build:-$(kernel_image)} (the rootfs stays)" elif [ -n "$to_version" ]; then to="$to ($to_version)" elif [ -n "$to_note" ] || [ -z "$to_build" ]; then to="$to (${to_note:-the image carries no build ID})" fi # Whether --build can still reach the build being left. Asked of the # manifest this run fetched, or fetched now on a run that downloaded its # image and so is online -- soft, a manifest that does not come is no # reason to stop an upgrade the operator asked for. A run from local files # may be offline, with its services already stopped: it does not wait on # the network for this, and gets the unverified wording instead. if [ "1" != "$manifest_fetched" ] && [ "1" = "$remote_update" ] && [ -z "$archive" ]; then curl --connect-timeout 10 -s -f -m 20 -L $(tls_opts "$MANIFEST_URL") "$MANIFEST_URL" \ -o "$MANIFEST_CACHE" 2>&3 && manifest_fetched=1 fi echo_c 36 "\n==================================================================" echo_c 36 " UPDATING: $from_build${system_version:+ ($system_version)}" echo_c 36 " TO: $to" case "$from_build" in unknown | local-*) echo_c 36 " The running build was never published, so --build cannot return to it." ;; *) rb="sysupgrade --build=$from_build" if [ "1" = "$manifest_fetched" ] && [ -s "$MANIFEST_CACHE" ] && ! manifest_builds | grep -qxF "$from_build"; then echo_c 36 " $from_build is no longer in the manifest; 'sysupgrade --list-builds'" echo_c 36 " shows the builds --build can still return to." elif [ "1" = "$manifest_fetched" ] && [ -s "$MANIFEST_CACHE" ]; then echo_c 36 " If the new build misbehaves, return to this one with:" echo_c 36 " $rb" else echo_c 36 " If the new build misbehaves, and $from_build is still published" echo_c 36 " ('sysupgrade --list-builds'), return to it with:" echo_c 36 " $rb" fi ;; esac [ "1" = "$clear_overlay" ] || [ "$to_build" = "$from_build" ] || echo_c 36 " Recorded on the camera: 'sysupgrade --history' lists every upgrade." case "$to_build" in nightly-*) echo_c 33 " Daily builds are not release-tested: features may regress." ;; esac echo_c 36 "==================================================================" # Nothing reads the manifest after this, and /tmp is RAM: kept, its 1.7 MB # was what check_flash_ram found missing on a 32 MB hi3516ev200 (1016 KB # left of the 2048 it wants; 2328 KB without it). rm -f "$MANIFEST_CACHE" } # One line per upgrade in $HISTORY_FILE: UTC time, from, to, and which door # started it. Written before the first write, because after it there may be no # writable /etc left to write to (the pivot, the hand-off), and kept to the # last 50 lines so a camera upgraded nightly for years does not grow its # overlay for it. A run that wipes the overlay records nothing: the wipe would # take the line with it. Nor does one that stays on the build it runs: the # flash phase skips a same-version image, and a forced reflash of the same # build is not a transition. A kernel-only run is recorded as kernel:, # because the rootfs, and with it the build the camera reports, stays. # # Not to syslog: free_resources stops syslogd before the download (#2415), so # by the time the target is known there is no daemon to hear it. The remote # line free_resources sends already names the build being left. record_transition() { [ "1" = "$clear_overlay" ] && return 0 local door=cli to=${to_build:-unknown} [ "1" = "$web_update" ] && door=web if [ "1" = "$kernel_only" ]; then to=${kernel_file##*/} to="kernel:${to_build:-${to:-unknown}}" elif [ "$to_build" = "$from_build" ]; then return 0 fi { tail -n 49 "$HISTORY_FILE" 2>/dev/null echo "$(date -u +%Y-%m-%dT%H:%M:%SZ) $from_build $to $door" } > "$HISTORY_FILE.new" 2>&3 && mv "$HISTORY_FILE.new" "$HISTORY_FILE" 2>&3 && sync || { rm -f "$HISTORY_FILE.new"; echo_c 33 "Could not record this upgrade in $HISTORY_FILE."; } return 0 } print_history() { if [ ! -s "$HISTORY_FILE" ]; then echo_c 33 "\nNo upgrades recorded on this camera since its last factory reset." return 0 fi echo_c 33 "\nUpgrades started on this camera, oldest first (UTC, from, to, started from):" cat "$HISTORY_FILE" } # Is this camera forwarding its log off-box? Only then is it worth spending a # second letting a datagram out before syslogd stops -- see free_resources. # Matches the knob S01syslogd reads (#2415). syslog_remote_set() { grep -qs '^[[:space:]]*SYSLOG_REMOTE=[^[:space:]]' /etc/default/syslogd } print_sysinfo() { get_system_info echo_c 33 "OpenIPC System Updater v$scr_version" echo_c 36 "\nVendor\t$vendor\nSoC\t$soc\nModel\t$model\nKernel\t$kernel_version\nRootFS\t$system_version" } print_usage() { echo " Usage: $0 [options] Where: -k Update kernel from online repository. -r Update rootfs from online repository. On a UBIFS NAND camera the kernel is inside the rootfs, so -k and -r both write rootfs.ubifs. --url=[URL] Custom URL to update from (.tgz format). --archive=[FILE] Custom archive to update from (.tgz format). --kernel=[FILE] Update kernel from file (uImage or FIT; zboot.img on Rockchip). Not on a UBIFS NAND camera: pass the rootfs.ubifs that carries the kernel with --rootfs. --rootfs=[FILE] Update rootfs from file (squashfs; rootfs.ubifs on a UBIFS NAND camera). --channel=[NAME] Update from a named channel via the manifest (nightly | latest). Default URL would be used otherwise. --build=[ID] Update to a specific dated build via the manifest, e.g. nightly-20260520-b9d5883. --list-builds[=N] List the last N (default 20) builds available for this platform; mark the running build with '*'. --history List the upgrades this camera has been through since its last factory reset (from, to, when). -f, --force_all Do not validate anything. -n, --wipe_overlay Wipe overlay partition. -x, --no_reboot Do not reboot after updating. Ignored when the update rewrites the flash the camera is running from, which no running system survives. -z, --no_update Do not update self. --insecure Skip TLS certificate verification for this run: a private mirror with a self-signed certificate. Never needed for OpenIPC's own servers. --no_ramfs Flash from the rootfs instead of moving into RAM first. Reinstates the failure this guards against; for board bring-up only. --web Invoked by the WebUI: keep majestic alive so it can stream progress over /ws/upgrade. -h, --help Display this help and exit. Manifest URL: \$MANIFEST_URL (default: $MANIFEST_URL) " } # Does this run rewrite flash that the camera is running FROM? # # One question behind three decisions, so it is asked once: whether -x can be # honoured, whether the operator gets a warning and an abort window before the # point of no return, and — since #2416 — whether the flash phase needs to move # into RAM at all. A kernel-only write touches no mounted partition, so it needs # none of the three. # # image_combined covers a plain `-k` against a combined image, which writes the # rootfs with update_rootfs unset; clear_overlay covers -n, which erases the # jffs2 that is the overlay's upper layer and is consulted on every lookup. # # And a kernel write is live too on a board with no separate "kernel" partition: # kernel_device is then the combined "firmware" partition (get_system_info's # fallback), which overlaps the rootfs. Without that last clause a local # `--kernel=... -x` on such a layout would skip the pivot and rewrite the # running filesystem in place -- the majestic-webui#120 failure, reached from # the one path that looks harmless. rewrites_live_flash() { [ "1" = "$root_on_flash" ] || return 1 [ "1" = "$update_rootfs" ] && return 0 [ "1" = "$image_combined" ] && return 0 [ "1" = "$clear_overlay" ] && return 0 # A UBI kernel volume is a volume of its own, never mounted. [ -z "$ubi_layout" ] && [ "1" = "$update_kernel" ] && [ "/dev/" = "$(get_device "kernel")" ] } reboot_system() { # Nothing landed on flash the running system is served from: -x is safe to # honour. Return rather than exit, so the caller owns the status — the main # path falls through to `exit 0` (a deliberate --no_reboot run is a success, # it used to report failure) while die() keeps its own `exit 1`. # # Not inside the ramfs, though. There the camera's shell, /etc, dropbear and # getty are all behind /mnt, so "staying up" hands back a box that answers # ping and nothing else: the console loops on `can't run '/sbin/getty'` and # ssh rejects a key that worked a minute earlier, until somebody power-cycles # it (#2416). die() has always reasoned this way about a failure in the # pivot; the same is true of a success. The pivot is now entered only when # the run needs it, so the case that reaches here is a run that intended to # rewrite the live rootfs and then wrote nothing — a same-version -r, say. if [ "1" = "$skip_reboot" ] && [ "1" != "$live_flash_dirty" ] && [ "1" != "$_ramfs_phase" ]; then echo_c 33 "\nYou asked me not to reboot, so I won't." echo_c 31 "Although a reboot is required to apply the changes." echo_c 37 "Please reboot the camera manually whenever possible." # Same reasoning as die(): this run stays up, so what free_resources # stopped has to come back rather than wait for a reboot that was # explicitly declined. # The images first, as die() does: they are of no further use, and # while they sit in /tmp a cap raised for them cannot be put back. discard_unpacked # Before majestic is back: its media buffers come from the zone the # stage maps, and the zone's allocator does not know the stage is there. release_mmz_stage restore_resources rm $LOCK_FILE restore_majestic return 0 fi if [ "1" = "$skip_reboot" ] && [ "1" = "$live_flash_dirty" ]; then # -x cannot be honoured here. The flash this camera runs from has been # rewritten under the running kernel, so the live filesystem is already # inconsistent: reads miss the cache free_resources() dropped and come # back as the new image at stale offsets. There is no "reboot it later" — # the shell that would issue the reboot is one of the things that stops # working, which is how issue #2231 was reported: the camera still # answered ping, but no login succeeded and only a power cycle recovered # it. Reboot now, while we still can. echo_c 31 "\n--no_reboot ignored: the flash this camera runs from was rewritten." echo_c 31 "The running filesystem cannot survive that; only a reboot applies it." elif [ "1" = "$skip_reboot" ]; then # Nothing was written, but we are inside the pivot: see above. #2416. echo_c 31 "\n--no_reboot ignored: the flash phase is running from RAM." echo_c 31 "This camera has no shell, no SSH and no console until it reboots." fi echo_c 37 "\nUnconditional reboot" follow_flush # Let the dog go, without the magic 'V' -- see watchdog_keep. [ -n "$wdog_keeper" ] && : > "$WDOG_RELEASE" busybox reboot -d 1 -f # As PID 1 (a handed-off flash phase) the shell must outlive that delayed # reboot: PID 1 exiting is a kernel panic, which does reboot -- after # panic= seconds, and through an oops on the console that reads like a # failed upgrade. if [ "$$" = "1" ]; then while :; do sleep 1; done fi } # Do everything that writes flash, then reboot. A function because it is entered # from two places: normally after enter_ramfs has re-exec'd us inside the ramfs, # and directly when there is no ramfs to move into. flash_and_reboot() { # Anything that kills majestic from here on leaves the watchdog armed and unfed. # Armed only when a userspace owner was seen before the pivot, and only inside # the pivot: there every exit is a reboot, so there is nothing to hand back, and # `sleep` is the copy in RAM rather than one more exec off the partition being # erased. The in-place fallback gets no keeper and no change. if [ "1" = "$_ramfs_phase" ] && [ "1" = "$wdog_userspace_owned" ]; then watchdog_keep & wdog_keeper=$! fi if [ "1" = "$image_combined" ]; then # One image covers both kernel and rootfs; any of -k/-r/--url/--channel # maps to a single write. { [ "1" = "$update_kernel" ] || [ "1" = "$update_rootfs" ]; } && do_update_firmware else # Verify the rootfs BEFORE the first write. The kernel is flashed first, and # this check used to live inside do_update_rootfs — so a rootfs that failed # verification was only ever discovered once the kernel had already been # committed, leaving a half-upgraded device: new kernel, old rootfs, and no # way back except a manual flash. Checking first means a failure costs nothing. [ "1" = "$update_rootfs" ] && verify_rootfs "$rootfs_file" # And judge its size here too, for the same reason: the kernel goes down # first, so a rootfs that cannot fit has to be found while nothing is # committed. exit_update means the rootfs is not being written at all, so # whether it would have fitted is not this run's business. [ "1" = "$update_rootfs" ] && [ "1" != "$exit_update" ] && check_rootfs_fits "$(rootfs_image)" [ "1" = "$update_kernel" ] && do_update_kernel "$kernel_file" [ "1" = "$update_rootfs" ] && do_update_rootfs "$rootfs_file" fi [ "1" = "$clear_overlay" ] && [ "1" != "$overlay_recreated" ] && do_wipe_overlay reboot_system exit 0 } for i in "$@"; do case $i in -h | --help) print_sysinfo print_usage exit 0 ;; -k) update_kernel=1 remote_update=1 shift ;; --kernel=*) update_kernel=1 kernel_file="${i#*=}" [ "$kernel_file" != ${kernel_file#/mnt/mmc} ] && skip_unmount=1 shift ;; -r) update_rootfs=1 remote_update=1 shift ;; --rootfs=*) update_rootfs=1 rootfs_file="${i#*=}" [ "$rootfs_file" != ${rootfs_file#/mnt/mmc} ] && skip_unmount=1 shift ;; --url=*) update_kernel=1 update_rootfs=1 remote_update=1 url="${i#*=}" shift ;; --channel=*) update_kernel=1 update_rootfs=1 remote_update=1 channel="${i#*=}" shift ;; --build=*) update_kernel=1 update_rootfs=1 remote_update=1 build_id="${i#*=}" shift ;; --list-builds*) list_n="${i#--list-builds=}" [ "$list_n" = "--list-builds" ] && list_n=20 list_only=1 shift ;; --history) history_only=1 shift ;; --archive=*) update_kernel=1 update_rootfs=1 remote_update=1 skip_selfupdate=1 archive="${i#*=}" shift ;; -f | --force_all | --force_md5 | --force_soc | --force_ver) skip_md5=1 skip_soc=1 skip_ver=1 shift ;; -n | --wipe_overlay) clear_overlay=1 shift ;; -x | --no_reboot) skip_reboot=1 shift ;; --no_ramfs) # Escape hatch: flash from the rootfs the old way. For bringing up a # board where the pivot itself is suspect — it gives back the failure # mode this option exists to avoid, so it is not a general-purpose flag. skip_ramfs=1 shift ;; -z | --no_update) skip_selfupdate=1 shift ;; --insecure) # Only for a mirror whose certificate the bundle cannot verify. With # this set a man-in-the-middle can hand the camera anything, including # the script self_update execs, so say so where it cannot be missed. curl_insecure=-k echo_c 31 "--insecure: TLS certificates will NOT be verified this run" shift ;; -s) silent_update=1 shift ;; --web) # Invoked by majestic's /ws/upgrade: majestic has already freed its # own video pipeline and is streaming this log over the WebSocket, so # don't kill it up front — stop it only just before the flash. web_update=1 shift ;; *) print_sysinfo echo_c 37 "\nUnknown option: $1" print_usage exit 1 ;; esac done # Second phase of a ramfs upgrade. enter_ramfs re-exec'd us with no arguments # inside the new root, so everything before the flash — probing, download, # verification, stopping services — has already happened in the process that # pivoted us here, and its results arrived in the environment. Go straight to # the writes; flash_and_reboot never returns. if [ "1" = "$_ramfs_phase" ]; then [ "1" = "$_handoff" ] && follow_start echo_c 34 "Flashing from RAM (pid $$)" # pivot_root detached the tmpfs from the old root's $RAM_ROOT and parked that # root at /mnt, so what is left there is the bare mount point enter_ramfs # created — an empty directory in the overlay's upper layer that nothing will # ever read again. Take it back now, while there is still a filesystem under # /mnt to take it from: the first flashcp write is a few lines away and the # old root does not survive it (OpenIPC/majestic-webui#120). # # ...but ONLY one we created. The rootfs now ships /ram (see the mount point # in general/overlay/), so on any current image this directory lives in the # read-only lower layer, and rmdir cannot delete one of those -- overlayfs # records a WHITEOUT in the upper layer instead, which HIDES the shipped # mount point from every later boot. ramfs_discard was given this guard when # /ram started shipping; this line is the same rmdir on the path that # actually runs (a successful pivot never reaches ramfs_discard), and it was # missed -- so the whiteout replaced the stray directory it was meant to fix, # on upgrade and on factory reset alike (@usa-, majestic-webui#120). [ "1" = "$ram_root_shipped" ] || rmdir "/mnt$RAM_ROOT" 2>/dev/null # Handed off: this is PID 1 now, and nothing else is left running from the # old root, so it can finally be let go -- see need_handoff. [ "1" = "$_handoff" ] && release_old_root # Stage 1 exports the SoC the images are named for; one that did not (or # a caller that sets up this phase by hand) names them for $model, as # every image did before #2688. image_soc=${image_soc:-$model} [ -n "$ramfs_kernel_file" ] && kernel_file=$ramfs_kernel_file [ -n "$ramfs_rootfs_file" ] && rootfs_file=$ramfs_rootfs_file flash_and_reboot fi print_sysinfo if [ "1" = "$history_only" ]; then print_history exit 0 fi if [ "1" = "$list_only" ]; then # `timeout 15` ends a slow listing with TERM, which would skip the EXIT trap. trap 'exit 1' INT TERM HUP fetch_manifest resolve_model_alias platform=$(manifest_platform) here=$(get_build_id) builds=$(manifest_builds | head -n "${list_n:-20}") if [ -z "$builds" ]; then echo_c 33 "\nNo builds available for $platform yet." echo "Manifest: $MANIFEST_URL" exit 0 fi echo_c 33 "\nAvailable builds for $platform (newest first):" echo "$builds" | awk -v h="$here" '{ printf " %s %s\n", ($1==h?"*":" "), $1 }' exit 0 fi [ "1" != "$clear_overlay" ] && [ "1" != "$update_kernel" ] && [ "1" != "$update_rootfs" ] && echo_c 37 "\nTry '$(basename "$0") --help' for options." && exit 0 [ "1" != "$skip_selfupdate" ] && self_update # Before anything is locked, downloaded or written. -n alone stays allowed: it # only empties rootfs_data, which this layout keeps in a UBI volume like any # other. if { [ "1" = "$update_kernel" ] || [ "1" = "$update_rootfs" ]; } && is_split_nand; then die "This camera uses the retired split NAND layout (kernel in its own partition, UBIFS root beside it), which this upgrade cannot write safely. Reinstall it from U-Boot with the current NAND instructions: https://openipc.org/cameras/vendors/$vendor/socs/$model -- nothing was written." fi # The NAND layouts with a kernel volume of their own are retired on the SoCs # that moved to the kernel-in-rootfs one: current images for them carry the # kernel inside the UBIFS rootfs and size the volumes to it. ubiblock stays # for the rest (nand_layout_moved). # # The first FIT layout (ubifs-kvol) can move over in place: its U-Boot is a # FIT NAND build, which loads a kernel from UBIFS, so `ipctool nand-migrate` # rewrites the rootfs, switches bootcmd and drops the kernel volume, with no # serial console (OpenIPC/ipctool#243). A ubiblock camera's U-Boot predates # FIT; that one is reinstalled from U-Boot. if { [ "1" = "$update_kernel" ] || [ "1" = "$update_rootfs" ]; } && [ "$ubi_layout" = "ubifs-kvol" ]; then die "This camera uses the retired NAND layout with a separate kernel volume; current images carry the kernel inside the rootfs. Move it over without a serial console: copy rootfs.ubifs.* from its -nand- package to /tmp and run 'ipctool nand-migrate /tmp/rootfs.ubifs.' (it keeps U-Boot and the settings, and reboots), or reinstall it from U-Boot: https://openipc.org/cameras/vendors/$vendor/socs/$model -- nothing was written." fi if { [ "1" = "$update_kernel" ] || [ "1" = "$update_rootfs" ]; } && [ "$ubi_layout" = "ubiblock" ] && nand_layout_moved; then die "This camera uses the retired NAND layout with a separate kernel volume; current images carry the kernel inside the rootfs. Reinstall it from U-Boot with the current NAND instructions: https://openipc.org/cameras/vendors/$vendor/socs/$model -- nothing was written." fi # On the ubifs layout the kernel is a file in the rootfs: there is no kernel # to write on its own, and either flag means writing the one image. if [ "$ubi_layout" = "ubifs" ] && [ "1" = "$update_kernel" ]; then # A local kernel file cannot be written on its own: it would have to be # inside the rootfs image. Say so rather than write some other rootfs. [ -n "$kernel_file" ] && [ -z "$rootfs_file" ] && [ "1" != "$remote_update" ] && die "On this camera the kernel lives inside the rootfs: pass the rootfs.ubifs that carries it with --rootfs=. Nothing was written." update_kernel=0 update_rootfs=1 echo_c 37 "\nThe kernel lives inside the rootfs on this camera: writing the rootfs." fi create_lock free_resources # A local archive is unpacked before check_sdcard: it is most often ON the card, # which check_sdcard unmounts, and an archive there was then "not found" every # time. Everything after the unpack reads /tmp, so the card can go afterwards. [ "1" = "$remote_update" ] && [ -n "$archive" ] && download_firmware [ "1" != "$skip_unmount" ] && check_sdcard [ "1" = "$remote_update" ] && [ -z "$archive" ] && download_firmware # Now that every image is on the camera, and while the camera is still whole. preflight_image_sizes read_candidate_build print_transition # Seconds to Ctrl-C out, before the trap below takes the signals away: always # for the -x notice, and for an operator at a console so the block above can be # read (#2676). Never for --web or a script, which have no one to wait for. # Through die(), which puts the services back: an interrupt left to its default # would leave the camera without syslog, cron and ntpd until a reboot. pause_before_flash= [ -t 0 ] && [ "1" != "$web_update" ] && pause_before_flash=1 # -x cannot be honoured for a write that lands on the flash this camera runs # from (see reboot_system). Say so before the first write rather than after it: # the trap below takes Ctrl-C away, deliberately, for the rest of the run, so # this is the last moment an operator who truly cannot afford a reboot can get # out. image_combined is only known once download_firmware has run, and it is # what covers a plain `-k` against a combined image (that writes the rootfs # with update_rootfs unset). if [ "1" = "$skip_reboot" ] && rewrites_live_flash; then echo_c 31 "\nNOTICE: -x was requested, but this run rewrites the flash this camera" echo_c 31 "is running from. -x cannot be honoured for that: the running filesystem" echo_c 31 "does not survive it. The camera will be rebooted once flashing is done." pause_before_flash=1 fi if [ "1" = "$pause_before_flash" ]; then trap 'die "Interrupted before the first write; nothing was written."' INT TERM HUP echo_c 37 "Press Ctrl-C now to abort. Continuing in $abort_wait seconds..." sleep "$abort_wait" fi # After the pause: a run abandoned there was never an upgrade. record_transition # Clear the bootloader's boot counter so the freshly written image starts with a # clean slate: if it never reaches a healthy userspace, U-Boot boots FAILSAFE # (network + SSH only) after bootlimit tries instead of looping on a broken # image, and S99bootok clears the counter again once majestic is up. # bootcount-clear ships only on families that reserve the counter # (package/openipc-failsafe) and reads its address from the board DTS, so it is # a safe no-op elsewhere and never writes another platform's memory (#2396); it # touches no flash either, unlike an env-backed counter whose per-boot NOR # writes wear the chip and risk a bricked env on a power cut. Done on the # still-normal system, before enter_ramfs moves things out of reach. command -v bootcount-clear >/dev/null 2>&1 && bootcount-clear # A camera that ever ran the pre-verify U-Boot has verify=n saved in flash, # where it still overrides the built-in default; drop the key so the kernel # image is CRC-checked on boot again. if command -v fw_setenv >/dev/null 2>&1; then fw_setenv verify 2>/dev/null || echo_c 33 "Warning: could not clear a saved verify=n; a corrupt kernel may not be caught on boot." fi # Past this point we erase flash. A controlling-TTY death (SSH session drop, # stray Ctrl-C, etc.) must not kill flashcp mid-write — that leaves the rootfs # partition partially erased and unbootable. SIG_IGN is inherited across # fork+exec, so this also shields the busybox flashcp child. See issue #2024. trap '' HUP INT TERM PIPE signals_ignored=1 echo_c 37 "\nProtected: flashing continues even if this terminal disconnects." # Only a run that rewrites the flash the camera is served from needs to leave # it. The pivot costs the camera its shell, SSH and console until the next # reboot -- there is no way back out of it, which is the whole of #2416 -- and # buys nothing when the partition under the running squashfs is not being # touched. A kernel-only write is the common case: it is also the one where -x # is honoured, so it was the one that stranded people. # # On success this never comes back — the rest runs in the re-exec'd process # inside the ramfs. A camera that cannot give us one still has to upgrade, so a # failure here falls through and flashes in place exactly as before. if ! rewrites_live_flash; then echo_c 37 "\nFlashing in place: this run does not rewrite the filesystem the camera runs from." elif need_handoff; then # A mounted UBI volume cannot be rewritten in place at all (see # need_handoff), so there is no fallback to fall back to: refuse while # nothing is written. enter_ramfs handoff ramfs_discard die "Could not move the flash phase into RAM; a mounted UBI volume cannot be rewritten without it. Nothing was written." elif want_handoff; then # On success this never returns. A hand-off that could not be staged or # pivoted falls back as any failed pivot does: tidy up and flash in place. enter_ramfs handoff || ramfs_discard elif ! enter_ramfs; then # The pivot did not take, so the in-place flash below needs none of what was # staged for it. enter_ramfs bails out from a dozen places and cannot tidy up # from inside each one, so the single path that means "no pivot" does it: # otherwise a camera that merely FAILED to move into RAM was still left with # the mount point, and on the earliest bail-outs with a tmpfs on it too. # ramfs_unwind may already have done both, in which case this is a no-op. ramfs_discard fi flash_and_reboot