Initial commit
This commit is contained in:
297
package/base-files/files/lib/upgrade/common.sh
Normal file
297
package/base-files/files/lib/upgrade/common.sh
Normal file
@@ -0,0 +1,297 @@
|
||||
RAM_ROOT=/tmp/root
|
||||
|
||||
export BACKUP_FILE=sysupgrade.tgz # file extracted by preinit
|
||||
|
||||
[ -x /usr/bin/ldd ] || ldd() { LD_TRACE_LOADED_OBJECTS=1 $*; }
|
||||
libs() { ldd $* 2>/dev/null | sed -E 's/(.* => )?(.*) .*/\2/'; }
|
||||
|
||||
install_file() { # <file> [ <file> ... ]
|
||||
local target dest dir
|
||||
for file in "$@"; do
|
||||
if [ -L "$file" ]; then
|
||||
target="$(readlink -f "$file")"
|
||||
dest="$RAM_ROOT/$file"
|
||||
[ ! -f "$dest" ] && {
|
||||
dir="$(dirname "$dest")"
|
||||
mkdir -p "$dir"
|
||||
ln -s "$target" "$dest"
|
||||
}
|
||||
file="$target"
|
||||
fi
|
||||
dest="$RAM_ROOT/$file"
|
||||
[ -f "$file" -a ! -f "$dest" ] && {
|
||||
dir="$(dirname "$dest")"
|
||||
mkdir -p "$dir"
|
||||
cp "$file" "$dest"
|
||||
}
|
||||
done
|
||||
}
|
||||
|
||||
install_bin() {
|
||||
local src files
|
||||
src=$1
|
||||
files=$1
|
||||
[ -x "$src" ] && files="$src $(libs $src)"
|
||||
install_file $files
|
||||
}
|
||||
|
||||
run_hooks() {
|
||||
local arg="$1"; shift
|
||||
for func in "$@"; do
|
||||
eval "$func $arg"
|
||||
done
|
||||
}
|
||||
|
||||
ask_bool() {
|
||||
local default="$1"; shift;
|
||||
local answer="$default"
|
||||
|
||||
[ "$INTERACTIVE" -eq 1 ] && {
|
||||
case "$default" in
|
||||
0) echo -n "$* (y/N): ";;
|
||||
*) echo -n "$* (Y/n): ";;
|
||||
esac
|
||||
read answer
|
||||
case "$answer" in
|
||||
y*) answer=1;;
|
||||
n*) answer=0;;
|
||||
*) answer="$default";;
|
||||
esac
|
||||
}
|
||||
[ "$answer" -gt 0 ]
|
||||
}
|
||||
|
||||
_v() {
|
||||
[ -n "$VERBOSE" ] && [ "$VERBOSE" -ge 1 ] && echo "$*" >&2
|
||||
}
|
||||
|
||||
v() {
|
||||
_v "$(date) upgrade: $@"
|
||||
logger -p info -t upgrade "$@"
|
||||
}
|
||||
|
||||
json_string() {
|
||||
local v="$1"
|
||||
v="${v//\\/\\\\}"
|
||||
v="${v//\"/\\\"}"
|
||||
echo "\"$v\""
|
||||
}
|
||||
|
||||
rootfs_type() {
|
||||
/bin/mount | awk '($3 ~ /^\/$/) && ($5 !~ /rootfs/) { print $5 }'
|
||||
}
|
||||
|
||||
get_image() { # <source> [ <command> ]
|
||||
local from="$1"
|
||||
local cmd="$2"
|
||||
|
||||
if [ -z "$cmd" ]; then
|
||||
local magic="$(dd if="$from" bs=2 count=1 2>/dev/null | hexdump -n 2 -e '1/1 "%02x"')"
|
||||
case "$magic" in
|
||||
1f8b) cmd="busybox zcat";;
|
||||
*) cmd="cat";;
|
||||
esac
|
||||
fi
|
||||
|
||||
$cmd <"$from"
|
||||
}
|
||||
|
||||
get_image_dd() {
|
||||
local from="$1"; shift
|
||||
|
||||
(
|
||||
exec 3>&2
|
||||
( exec 3>&2; get_image "$from" 2>&1 1>&3 | grep -v -F ' Broken pipe' ) 2>&1 1>&3 \
|
||||
| ( exec 3>&2; dd "$@" 2>&1 1>&3 | grep -v -E ' records (in|out)') 2>&1 1>&3
|
||||
exec 3>&-
|
||||
)
|
||||
}
|
||||
|
||||
get_magic_word() {
|
||||
(get_image "$@" | dd bs=2 count=1 | hexdump -v -n 2 -e '1/1 "%02x"') 2>/dev/null
|
||||
}
|
||||
|
||||
get_magic_long() {
|
||||
(get_image "$@" | dd bs=4 count=1 | hexdump -v -n 4 -e '1/1 "%02x"') 2>/dev/null
|
||||
}
|
||||
|
||||
get_magic_gpt() {
|
||||
(get_image "$@" | dd bs=8 count=1 skip=64) 2>/dev/null
|
||||
}
|
||||
|
||||
get_magic_vfat() {
|
||||
(get_image "$@" | dd bs=3 count=1 skip=18) 2>/dev/null
|
||||
}
|
||||
|
||||
get_magic_fat32() {
|
||||
(get_image "$@" | dd bs=1 count=5 skip=82) 2>/dev/null
|
||||
}
|
||||
|
||||
part_magic_efi() {
|
||||
local magic=$(get_magic_gpt "$@")
|
||||
[ "$magic" = "EFI PART" ]
|
||||
}
|
||||
|
||||
part_magic_fat() {
|
||||
local magic=$(get_magic_vfat "$@")
|
||||
local magic_fat32=$(get_magic_fat32 "$@")
|
||||
[ "$magic" = "FAT" ] || [ "$magic_fat32" = "FAT32" ]
|
||||
}
|
||||
|
||||
export_bootdevice() {
|
||||
local cmdline uuid blockdev uevent line class
|
||||
local MAJOR MINOR DEVNAME DEVTYPE
|
||||
local rootpart="$(cmdline_get_var root)"
|
||||
|
||||
case "$rootpart" in
|
||||
PARTUUID=[a-f0-9][a-f0-9][a-f0-9][a-f0-9][a-f0-9][a-f0-9][a-f0-9][a-f0-9]-[a-f0-9][a-f0-9])
|
||||
uuid="${rootpart#PARTUUID=}"
|
||||
uuid="${uuid%-[a-f0-9][a-f0-9]}"
|
||||
for blockdev in $(find /dev -type b); do
|
||||
set -- $(dd if=$blockdev bs=1 skip=440 count=4 2>/dev/null | hexdump -v -e '4/1 "%02x "')
|
||||
if [ "$4$3$2$1" = "$uuid" ]; then
|
||||
uevent="/sys/class/block/${blockdev##*/}/uevent"
|
||||
break
|
||||
fi
|
||||
done
|
||||
;;
|
||||
PARTUUID=????????-????-????-????-??????????02)
|
||||
uuid="${rootpart#PARTUUID=}"
|
||||
uuid="${uuid%02}00"
|
||||
for disk in $(find /dev -type b); do
|
||||
set -- $(dd if=$disk bs=1 skip=568 count=16 2>/dev/null | hexdump -v -e '8/1 "%02x "" "2/1 "%02x""-"6/1 "%02x"')
|
||||
if [ "$4$3$2$1-$6$5-$8$7-$9" = "$uuid" ]; then
|
||||
uevent="/sys/class/block/${disk##*/}/uevent"
|
||||
break
|
||||
fi
|
||||
done
|
||||
;;
|
||||
/dev/*)
|
||||
uevent="/sys/class/block/${rootpart##*/}/../uevent"
|
||||
;;
|
||||
0x[a-f0-9][a-f0-9][a-f0-9] | 0x[a-f0-9][a-f0-9][a-f0-9][a-f0-9] | \
|
||||
[a-f0-9][a-f0-9][a-f0-9] | [a-f0-9][a-f0-9][a-f0-9][a-f0-9])
|
||||
rootpart=0x${rootpart#0x}
|
||||
for class in /sys/class/block/*; do
|
||||
while read line; do
|
||||
export -n "$line"
|
||||
done < "$class/uevent"
|
||||
if [ $((rootpart/256)) = $MAJOR -a $((rootpart%256)) = $MINOR ]; then
|
||||
uevent="$class/../uevent"
|
||||
fi
|
||||
done
|
||||
;;
|
||||
esac
|
||||
|
||||
if [ -e "$uevent" ]; then
|
||||
while read line; do
|
||||
export -n "$line"
|
||||
done < "$uevent"
|
||||
export BOOTDEV_MAJOR=$MAJOR
|
||||
export BOOTDEV_MINOR=$MINOR
|
||||
return 0
|
||||
fi
|
||||
|
||||
return 1
|
||||
}
|
||||
|
||||
export_partdevice() {
|
||||
local var="$1" offset="$2"
|
||||
local uevent line MAJOR MINOR DEVNAME DEVTYPE
|
||||
|
||||
for uevent in /sys/class/block/*/uevent; do
|
||||
while read line; do
|
||||
export -n "$line"
|
||||
done < "$uevent"
|
||||
if [ $BOOTDEV_MAJOR = $MAJOR -a $(($BOOTDEV_MINOR + $offset)) = $MINOR -a -b "/dev/$DEVNAME" ]; then
|
||||
export "$var=$DEVNAME"
|
||||
return 0
|
||||
fi
|
||||
done
|
||||
|
||||
return 1
|
||||
}
|
||||
|
||||
hex_le32_to_cpu() {
|
||||
[ "$(echo 01 | hexdump -v -n 2 -e '/2 "%x"')" = "3031" ] && {
|
||||
echo "${1:0:2}${1:8:2}${1:6:2}${1:4:2}${1:2:2}"
|
||||
return
|
||||
}
|
||||
echo "$@"
|
||||
}
|
||||
|
||||
get_partition_by_name() {
|
||||
for partname in /sys/class/block/$1/*/name; do
|
||||
[ "$(cat ${partname})" = "$2" ] && {
|
||||
basename ${partname%%/name}
|
||||
break
|
||||
}
|
||||
done
|
||||
}
|
||||
|
||||
get_partitions() { # <device> <filename>
|
||||
local disk="$1"
|
||||
local filename="$2"
|
||||
|
||||
if [ -b "$disk" -o -f "$disk" ]; then
|
||||
v "Reading partition table from $filename..."
|
||||
|
||||
local magic=$(dd if="$disk" bs=2 count=1 skip=255 2>/dev/null)
|
||||
if [ "$magic" != $'\x55\xAA' ]; then
|
||||
v "Invalid partition table on $disk"
|
||||
exit
|
||||
fi
|
||||
|
||||
rm -f "/tmp/partmap.$filename"
|
||||
|
||||
local part
|
||||
part_magic_efi "$disk" && {
|
||||
#export_partdevice will fail when partition number is greater than 15, as
|
||||
#the partition major device number is not equal to the disk major device number
|
||||
for part in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15; do
|
||||
set -- $(hexdump -v -n 48 -s "$((0x380 + $part * 0x80))" -e '4/4 "%08x"" "4/4 "%08x"" "4/4 "0x%08X "' "$disk")
|
||||
|
||||
local type="$1"
|
||||
local lba="$(( $(hex_le32_to_cpu $4) * 0x100000000 + $(hex_le32_to_cpu $3) ))"
|
||||
local end="$(( $(hex_le32_to_cpu $6) * 0x100000000 + $(hex_le32_to_cpu $5) ))"
|
||||
local num="$(( $end - $lba ))"
|
||||
|
||||
[ "$type" = "00000000000000000000000000000000" ] && continue
|
||||
|
||||
printf "%2d %5d %7d\n" $part $lba $num >> "/tmp/partmap.$filename"
|
||||
done
|
||||
} || {
|
||||
for part in 1 2 3 4; do
|
||||
set -- $(hexdump -v -n 12 -s "$((0x1B2 + $part * 16))" -e '3/4 "0x%08X "' "$disk")
|
||||
|
||||
local type="$(( $(hex_le32_to_cpu $1) % 256))"
|
||||
local lba="$(( $(hex_le32_to_cpu $2) ))"
|
||||
local num="$(( $(hex_le32_to_cpu $3) ))"
|
||||
|
||||
[ $type -gt 0 ] || continue
|
||||
|
||||
printf "%2d %5d %7d\n" $part $lba $num >> "/tmp/partmap.$filename"
|
||||
done
|
||||
}
|
||||
fi
|
||||
}
|
||||
|
||||
indicate_upgrade() {
|
||||
. /etc/diag.sh
|
||||
set_state upgrade
|
||||
}
|
||||
|
||||
# Flash firmware to MTD partition
|
||||
#
|
||||
# $(1): path to image
|
||||
# $(2): (optional) pipe command to extract firmware, e.g. dd bs=n skip=m
|
||||
default_do_upgrade() {
|
||||
sync
|
||||
echo 3 > /proc/sys/vm/drop_caches
|
||||
if [ -n "$UPGRADE_BACKUP" ]; then
|
||||
get_image "$1" "$2" | mtd $MTD_ARGS $MTD_CONFIG_ARGS -j "$UPGRADE_BACKUP" write - "${PART_NAME:-image}"
|
||||
else
|
||||
get_image "$1" "$2" | mtd $MTD_ARGS write - "${PART_NAME:-image}"
|
||||
fi
|
||||
[ $? -ne 0 ] && exit 1
|
||||
}
|
||||
25
package/base-files/files/lib/upgrade/do_stage2
Executable file
25
package/base-files/files/lib/upgrade/do_stage2
Executable file
@@ -0,0 +1,25 @@
|
||||
#!/bin/sh
|
||||
|
||||
. /lib/functions.sh
|
||||
|
||||
include /lib/upgrade
|
||||
|
||||
v "Performing system upgrade..."
|
||||
if type 'platform_do_upgrade' >/dev/null 2>/dev/null; then
|
||||
platform_do_upgrade "$IMAGE"
|
||||
else
|
||||
default_do_upgrade "$IMAGE"
|
||||
fi
|
||||
|
||||
if [ -n "$UPGRADE_BACKUP" ] && type 'platform_copy_config' >/dev/null 2>/dev/null; then
|
||||
platform_copy_config
|
||||
fi
|
||||
|
||||
v "Upgrade completed"
|
||||
sleep 1
|
||||
|
||||
v "Rebooting system..."
|
||||
umount -a
|
||||
reboot -f
|
||||
sleep 5
|
||||
echo b 2>/dev/null >/proc/sysrq-trigger
|
||||
92
package/base-files/files/lib/upgrade/fwtool.sh
Normal file
92
package/base-files/files/lib/upgrade/fwtool.sh
Normal file
@@ -0,0 +1,92 @@
|
||||
fwtool_check_signature() {
|
||||
[ $# -gt 1 ] && return 1
|
||||
|
||||
[ ! -x /usr/bin/ucert ] && {
|
||||
if [ "$REQUIRE_IMAGE_SIGNATURE" = 1 ]; then
|
||||
return 1
|
||||
else
|
||||
return 0
|
||||
fi
|
||||
}
|
||||
|
||||
if ! fwtool -q -s /tmp/sysupgrade.ucert "$1"; then
|
||||
v "Image signature not present"
|
||||
[ "$REQUIRE_IMAGE_SIGNATURE" = 1 -a "$FORCE" != 1 ] && {
|
||||
v "Use sysupgrade -F to override this check when downgrading or flashing to vendor firmware"
|
||||
}
|
||||
[ "$REQUIRE_IMAGE_SIGNATURE" = 1 ] && return 1
|
||||
return 0
|
||||
fi
|
||||
|
||||
fwtool -q -T -s /dev/null "$1" | \
|
||||
ucert -V -m - -c "/tmp/sysupgrade.ucert" -P /etc/opkg/keys
|
||||
|
||||
return $?
|
||||
}
|
||||
|
||||
fwtool_check_image() {
|
||||
[ $# -gt 1 ] && return 1
|
||||
|
||||
. /usr/share/libubox/jshn.sh
|
||||
|
||||
if ! fwtool -q -i /tmp/sysupgrade.meta "$1"; then
|
||||
v "Image metadata not present"
|
||||
[ "$REQUIRE_IMAGE_METADATA" = 1 -a "$FORCE" != 1 ] && {
|
||||
v "Use sysupgrade -F to override this check when downgrading or flashing to vendor firmware"
|
||||
}
|
||||
[ "$REQUIRE_IMAGE_METADATA" = 1 ] && return 1
|
||||
return 0
|
||||
fi
|
||||
|
||||
json_load "$(cat /tmp/sysupgrade.meta)" || {
|
||||
v "Invalid image metadata"
|
||||
return 1
|
||||
}
|
||||
|
||||
device="$(cat /tmp/sysinfo/board_name)"
|
||||
devicecompat="$(uci -q get system.@system[0].compat_version)"
|
||||
[ -n "$devicecompat" ] || devicecompat="1.0"
|
||||
|
||||
json_get_var imagecompat compat_version
|
||||
json_get_var compatmessage compat_message
|
||||
[ -n "$imagecompat" ] || imagecompat="1.0"
|
||||
|
||||
# select correct supported list based on compat_version
|
||||
# (using this ensures that compatibility check works for devices
|
||||
# not knowing about compat-version)
|
||||
local supported=supported_devices
|
||||
[ "$imagecompat" != "1.0" ] && supported=new_supported_devices
|
||||
json_select $supported || return 1
|
||||
|
||||
json_get_keys dev_keys
|
||||
for k in $dev_keys; do
|
||||
json_get_var dev "$k"
|
||||
if [ "$dev" = "$device" ]; then
|
||||
# major compat version -> no sysupgrade
|
||||
if [ "${devicecompat%.*}" != "${imagecompat%.*}" ]; then
|
||||
v "The device is supported, but this image is incompatible for sysupgrade based on the image version ($devicecompat->$imagecompat)."
|
||||
[ -n "$compatmessage" ] && v "$compatmessage"
|
||||
return 1
|
||||
fi
|
||||
|
||||
# minor compat version -> sysupgrade with -n required
|
||||
if [ "${devicecompat#.*}" != "${imagecompat#.*}" ] && [ "$SAVE_CONFIG" = "1" ]; then
|
||||
v "The device is supported, but the config is incompatible to the new image ($devicecompat->$imagecompat). Please upgrade without keeping config (sysupgrade -n)."
|
||||
[ -n "$compatmessage" ] && v "$compatmessage"
|
||||
return 1
|
||||
fi
|
||||
|
||||
return 0
|
||||
fi
|
||||
done
|
||||
|
||||
v "Device $device not supported by this image"
|
||||
local devices="Supported devices:"
|
||||
for k in $dev_keys; do
|
||||
json_get_var dev "$k"
|
||||
devices="$devices $dev"
|
||||
done
|
||||
v "$devices"
|
||||
|
||||
return 1
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
# Essential files that will be always kept
|
||||
/etc/hosts
|
||||
/etc/inittab
|
||||
/etc/group
|
||||
/etc/passwd
|
||||
/etc/profile
|
||||
/etc/shadow
|
||||
/etc/shells
|
||||
/etc/shinit
|
||||
/etc/sysctl.conf
|
||||
/etc/rc.local
|
||||
91
package/base-files/files/lib/upgrade/legacy-sdcard.sh
Normal file
91
package/base-files/files/lib/upgrade/legacy-sdcard.sh
Normal file
@@ -0,0 +1,91 @@
|
||||
legacy_sdcard_check_image() {
|
||||
local file="$1"
|
||||
local diskdev partdev diff
|
||||
|
||||
export_bootdevice && export_partdevice diskdev 0 || {
|
||||
v "Unable to determine upgrade device"
|
||||
return 1
|
||||
}
|
||||
|
||||
get_partitions "/dev/$diskdev" bootdisk
|
||||
|
||||
v "Extract boot sector from the image"
|
||||
get_image_dd "$1" of=/tmp/image.bs count=1 bs=512b
|
||||
|
||||
get_partitions /tmp/image.bs image
|
||||
|
||||
#compare tables
|
||||
diff="$(grep -F -x -v -f /tmp/partmap.bootdisk /tmp/partmap.image)"
|
||||
|
||||
rm -f /tmp/image.bs /tmp/partmap.bootdisk /tmp/partmap.image
|
||||
|
||||
if [ -n "$diff" ]; then
|
||||
v "Partition layout has changed. Full image will be written."
|
||||
ask_bool 0 "Abort" && exit 1
|
||||
return 0
|
||||
fi
|
||||
}
|
||||
|
||||
legacy_sdcard_do_upgrade() {
|
||||
local board=$(board_name)
|
||||
local diskdev partdev diff
|
||||
|
||||
export_bootdevice && export_partdevice diskdev 0 || {
|
||||
v "Unable to determine upgrade device"
|
||||
return 1
|
||||
}
|
||||
|
||||
sync
|
||||
|
||||
if [ "$UPGRADE_OPT_SAVE_PARTITIONS" = "1" ]; then
|
||||
get_partitions "/dev/$diskdev" bootdisk
|
||||
|
||||
v "Extract boot sector from the image"
|
||||
get_image_dd "$1" of=/tmp/image.bs count=1 bs=512b
|
||||
|
||||
get_partitions /tmp/image.bs image
|
||||
|
||||
#compare tables
|
||||
diff="$(grep -F -x -v -f /tmp/partmap.bootdisk /tmp/partmap.image)"
|
||||
else
|
||||
diff=1
|
||||
fi
|
||||
|
||||
if [ -n "$diff" ]; then
|
||||
get_image_dd "$1" of="/dev/$diskdev" bs=4096 conv=fsync
|
||||
|
||||
# Separate removal and addtion is necessary; otherwise, partition 1
|
||||
# will be missing if it overlaps with the old partition 2
|
||||
partx -d - "/dev/$diskdev"
|
||||
partx -a - "/dev/$diskdev"
|
||||
else
|
||||
v "Writing bootloader to /dev/$diskdev"
|
||||
get_image_dd "$1" of="$diskdev" bs=512 skip=1 seek=1 count=2048 conv=fsync
|
||||
#iterate over each partition from the image and write it to the boot disk
|
||||
while read part start size; do
|
||||
if export_partdevice partdev $part; then
|
||||
v "Writing image to /dev/$partdev..."
|
||||
get_image_dd "$1" of="/dev/$partdev" ibs="512" obs=1M skip="$start" count="$size" conv=fsync
|
||||
else
|
||||
v "Unable to find partition $part device, skipped."
|
||||
fi
|
||||
done < /tmp/partmap.image
|
||||
|
||||
v "Writing new UUID to /dev/$diskdev..."
|
||||
get_image_dd "$1" of="/dev/$diskdev" bs=1 skip=440 count=4 seek=440 conv=fsync
|
||||
fi
|
||||
|
||||
sleep 1
|
||||
}
|
||||
|
||||
legacy_sdcard_copy_config() {
|
||||
local partdev
|
||||
|
||||
if export_partdevice partdev 1; then
|
||||
mkdir -p /boot
|
||||
[ -f /boot/kernel.img ] || mount -o rw,noatime /dev/$partdev /boot
|
||||
cp -af "$UPGRADE_BACKUP" "/boot/$BACKUP_FILE"
|
||||
sync
|
||||
umount /boot
|
||||
fi
|
||||
}
|
||||
356
package/base-files/files/lib/upgrade/nand.sh
Normal file
356
package/base-files/files/lib/upgrade/nand.sh
Normal file
@@ -0,0 +1,356 @@
|
||||
# Copyright (C) 2014 OpenWrt.org
|
||||
#
|
||||
|
||||
. /lib/functions.sh
|
||||
|
||||
# 'kernel' partition or UBI volume on NAND contains the kernel
|
||||
CI_KERNPART="${CI_KERNPART:-kernel}"
|
||||
|
||||
# 'ubi' partition on NAND contains UBI
|
||||
CI_UBIPART="${CI_UBIPART:-ubi}"
|
||||
|
||||
# 'rootfs' UBI volume on NAND contains the rootfs
|
||||
CI_ROOTPART="${CI_ROOTPART:-rootfs}"
|
||||
|
||||
ubi_mknod() {
|
||||
local dir="$1"
|
||||
local dev="/dev/$(basename $dir)"
|
||||
|
||||
[ -e "$dev" ] && return 0
|
||||
|
||||
local devid="$(cat $dir/dev)"
|
||||
local major="${devid%%:*}"
|
||||
local minor="${devid##*:}"
|
||||
mknod "$dev" c $major $minor
|
||||
}
|
||||
|
||||
nand_find_volume() {
|
||||
local ubidevdir ubivoldir
|
||||
ubidevdir="/sys/devices/virtual/ubi/$1"
|
||||
[ ! -d "$ubidevdir" ] && return 1
|
||||
for ubivoldir in $ubidevdir/${1}_*; do
|
||||
[ ! -d "$ubivoldir" ] && continue
|
||||
if [ "$( cat $ubivoldir/name )" = "$2" ]; then
|
||||
basename $ubivoldir
|
||||
ubi_mknod "$ubivoldir"
|
||||
return 0
|
||||
fi
|
||||
done
|
||||
}
|
||||
|
||||
nand_find_ubi() {
|
||||
local ubidevdir ubidev mtdnum
|
||||
mtdnum="$( find_mtd_index $1 )"
|
||||
[ ! "$mtdnum" ] && return 1
|
||||
for ubidevdir in /sys/devices/virtual/ubi/ubi*; do
|
||||
[ ! -d "$ubidevdir" ] && continue
|
||||
cmtdnum="$( cat $ubidevdir/mtd_num )"
|
||||
[ ! "$mtdnum" ] && continue
|
||||
if [ "$mtdnum" = "$cmtdnum" ]; then
|
||||
ubidev=$( basename $ubidevdir )
|
||||
ubi_mknod "$ubidevdir"
|
||||
echo $ubidev
|
||||
return 0
|
||||
fi
|
||||
done
|
||||
}
|
||||
|
||||
nand_get_magic_long() {
|
||||
dd if="$1" skip=$2 bs=4 count=1 2>/dev/null | hexdump -v -n 4 -e '1/1 "%02x"'
|
||||
}
|
||||
|
||||
get_magic_long_tar() {
|
||||
( tar xf $1 $2 -O | dd bs=4 count=1 | hexdump -v -n 4 -e '1/1 "%02x"') 2> /dev/null
|
||||
}
|
||||
|
||||
identify_magic() {
|
||||
local magic=$1
|
||||
case "$magic" in
|
||||
"55424923")
|
||||
echo "ubi"
|
||||
;;
|
||||
"31181006")
|
||||
echo "ubifs"
|
||||
;;
|
||||
"68737173")
|
||||
echo "squashfs"
|
||||
;;
|
||||
"d00dfeed")
|
||||
echo "fit"
|
||||
;;
|
||||
"4349"*)
|
||||
echo "combined"
|
||||
;;
|
||||
*)
|
||||
echo "unknown $magic"
|
||||
;;
|
||||
esac
|
||||
}
|
||||
|
||||
|
||||
identify() {
|
||||
identify_magic $(nand_get_magic_long "$1" "${2:-0}")
|
||||
}
|
||||
|
||||
identify_tar() {
|
||||
identify_magic $(get_magic_long_tar "$1" "$2")
|
||||
}
|
||||
|
||||
nand_restore_config() {
|
||||
sync
|
||||
local ubidev=$( nand_find_ubi $CI_UBIPART )
|
||||
local ubivol="$( nand_find_volume $ubidev rootfs_data )"
|
||||
[ ! "$ubivol" ] &&
|
||||
ubivol="$( nand_find_volume $ubidev $CI_ROOTPART )"
|
||||
mkdir /tmp/new_root
|
||||
if ! mount -t ubifs /dev/$ubivol /tmp/new_root; then
|
||||
echo "mounting ubifs $ubivol failed"
|
||||
rmdir /tmp/new_root
|
||||
return 1
|
||||
fi
|
||||
mv "$1" "/tmp/new_root/$BACKUP_FILE"
|
||||
umount /tmp/new_root
|
||||
sync
|
||||
rmdir /tmp/new_root
|
||||
}
|
||||
|
||||
nand_upgrade_prepare_ubi() {
|
||||
local rootfs_length="$1"
|
||||
local rootfs_type="$2"
|
||||
local rootfs_data_max="$(fw_printenv -n rootfs_data_max 2>/dev/null)"
|
||||
[ -n "$rootfs_data_max" ] && rootfs_data_max=$((rootfs_data_max))
|
||||
|
||||
local kernel_length="$3"
|
||||
local has_env="${4:-0}"
|
||||
|
||||
[ -n "$rootfs_length" -o -n "$kernel_length" ] || return 1
|
||||
|
||||
local mtdnum="$( find_mtd_index "$CI_UBIPART" )"
|
||||
if [ ! "$mtdnum" ]; then
|
||||
echo "cannot find ubi mtd partition $CI_UBIPART"
|
||||
return 1
|
||||
fi
|
||||
|
||||
local ubidev="$( nand_find_ubi "$CI_UBIPART" )"
|
||||
if [ ! "$ubidev" ]; then
|
||||
ubiattach -m "$mtdnum"
|
||||
sync
|
||||
ubidev="$( nand_find_ubi "$CI_UBIPART" )"
|
||||
fi
|
||||
|
||||
if [ ! "$ubidev" ]; then
|
||||
ubiformat /dev/mtd$mtdnum -y
|
||||
ubiattach -m "$mtdnum"
|
||||
sync
|
||||
ubidev="$( nand_find_ubi "$CI_UBIPART" )"
|
||||
[ "$has_env" -gt 0 ] && {
|
||||
ubimkvol /dev/$ubidev -n 0 -N ubootenv -s 1MiB
|
||||
ubimkvol /dev/$ubidev -n 1 -N ubootenv2 -s 1MiB
|
||||
}
|
||||
fi
|
||||
|
||||
local kern_ubivol="$( nand_find_volume $ubidev $CI_KERNPART )"
|
||||
local root_ubivol="$( nand_find_volume $ubidev $CI_ROOTPART )"
|
||||
local data_ubivol="$( nand_find_volume $ubidev rootfs_data )"
|
||||
|
||||
local ubiblk ubiblkvol
|
||||
for ubiblk in /dev/ubiblock*_? ; do
|
||||
[ -e "$ubiblk" ] || continue
|
||||
echo "removing ubiblock${ubiblk:13}"
|
||||
ubiblkvol=ubi${ubiblk:13}
|
||||
if ! ubiblock -r /dev/$ubiblkvol; then
|
||||
echo "cannot remove $ubiblk"
|
||||
return 1
|
||||
fi
|
||||
done
|
||||
|
||||
# kill volumes
|
||||
[ "$kern_ubivol" ] && ubirmvol /dev/$ubidev -N $CI_KERNPART || true
|
||||
[ "$root_ubivol" -a "$root_ubivol" != "$kern_ubivol" ] && ubirmvol /dev/$ubidev -N $CI_ROOTPART || true
|
||||
[ "$data_ubivol" ] && ubirmvol /dev/$ubidev -N rootfs_data || true
|
||||
|
||||
# update kernel
|
||||
if [ -n "$kernel_length" ]; then
|
||||
if ! ubimkvol /dev/$ubidev -N $CI_KERNPART -s $kernel_length; then
|
||||
echo "cannot create kernel volume"
|
||||
return 1;
|
||||
fi
|
||||
fi
|
||||
|
||||
# update rootfs
|
||||
if [ -n "$rootfs_length" ]; then
|
||||
local rootfs_size_param
|
||||
if [ "$rootfs_type" = "ubifs" ]; then
|
||||
rootfs_size_param="-m"
|
||||
else
|
||||
rootfs_size_param="-s $rootfs_length"
|
||||
fi
|
||||
if ! ubimkvol /dev/$ubidev -N $CI_ROOTPART $rootfs_size_param; then
|
||||
echo "cannot create rootfs volume"
|
||||
return 1;
|
||||
fi
|
||||
fi
|
||||
|
||||
# create rootfs_data for non-ubifs rootfs
|
||||
if [ "$rootfs_type" != "ubifs" ]; then
|
||||
local availeb=$(cat /sys/devices/virtual/ubi/$ubidev/avail_eraseblocks)
|
||||
local ebsize=$(cat /sys/devices/virtual/ubi/$ubidev/eraseblock_size)
|
||||
local avail_size=$((availeb * ebsize))
|
||||
local rootfs_data_size_param="-m"
|
||||
if [ -n "$rootfs_data_max" ] &&
|
||||
[ "$rootfs_data_max" != "0" ] &&
|
||||
[ "$rootfs_data_max" -le "$avail_size" ]; then
|
||||
rootfs_data_size_param="-s $rootfs_data_max"
|
||||
fi
|
||||
if ! ubimkvol /dev/$ubidev -N rootfs_data $rootfs_data_size_param; then
|
||||
echo "cannot initialize rootfs_data volume"
|
||||
return 1
|
||||
fi
|
||||
fi
|
||||
sync
|
||||
return 0
|
||||
}
|
||||
|
||||
nand_do_upgrade_success() {
|
||||
local conf_tar="/tmp/sysupgrade.tgz"
|
||||
|
||||
sync
|
||||
[ -f "$conf_tar" ] && nand_restore_config "$conf_tar"
|
||||
echo "sysupgrade successful"
|
||||
umount -a
|
||||
reboot -f
|
||||
}
|
||||
|
||||
# Flash the UBI image to MTD partition
|
||||
nand_upgrade_ubinized() {
|
||||
local ubi_file="$1"
|
||||
local mtdnum="$(find_mtd_index "$CI_UBIPART")"
|
||||
|
||||
[ ! "$mtdnum" ] && {
|
||||
CI_UBIPART="rootfs"
|
||||
mtdnum="$(find_mtd_index "$CI_UBIPART")"
|
||||
}
|
||||
|
||||
if [ ! "$mtdnum" ]; then
|
||||
echo "cannot find mtd device $CI_UBIPART"
|
||||
umount -a
|
||||
reboot -f
|
||||
fi
|
||||
|
||||
local mtddev="/dev/mtd${mtdnum}"
|
||||
ubidetach -p "${mtddev}" || true
|
||||
sync
|
||||
ubiformat "${mtddev}" -y -f "${ubi_file}"
|
||||
ubiattach -p "${mtddev}"
|
||||
nand_do_upgrade_success
|
||||
}
|
||||
|
||||
# Write the UBIFS image to UBI volume
|
||||
nand_upgrade_ubifs() {
|
||||
local rootfs_length=$( (cat $1 | wc -c) 2> /dev/null)
|
||||
|
||||
nand_upgrade_prepare_ubi "$rootfs_length" "ubifs" "" ""
|
||||
|
||||
local ubidev="$( nand_find_ubi "$CI_UBIPART" )"
|
||||
local root_ubivol="$(nand_find_volume $ubidev $CI_ROOTPART)"
|
||||
ubiupdatevol /dev/$root_ubivol -s $rootfs_length $1
|
||||
|
||||
nand_do_upgrade_success
|
||||
}
|
||||
|
||||
nand_upgrade_fit() {
|
||||
local fit_file="$1"
|
||||
local fit_length="$(wc -c < "$fit_file")"
|
||||
|
||||
nand_upgrade_prepare_ubi "" "" "$fit_length" "1"
|
||||
|
||||
local fit_ubidev="$(nand_find_ubi "$CI_UBIPART")"
|
||||
local fit_ubivol="$(nand_find_volume $fit_ubidev "$CI_KERNPART")"
|
||||
ubiupdatevol /dev/$fit_ubivol -s $fit_length $fit_file
|
||||
|
||||
nand_do_upgrade_success
|
||||
}
|
||||
|
||||
nand_upgrade_tar() {
|
||||
local tar_file="$1"
|
||||
local kernel_mtd="$(find_mtd_index $CI_KERNPART)"
|
||||
|
||||
local board_dir=$(tar tf "$tar_file" | grep -m 1 '^sysupgrade-.*/$')
|
||||
board_dir=${board_dir%/}
|
||||
|
||||
kernel_length=$( (tar xf "$tar_file" ${board_dir}/kernel -O | wc -c) 2> /dev/null)
|
||||
local has_rootfs=0
|
||||
local rootfs_length
|
||||
local rootfs_type
|
||||
|
||||
tar tf "$tar_file" ${board_dir}/root 1>/dev/null 2>/dev/null && has_rootfs=1
|
||||
[ "$has_rootfs" = "1" ] && {
|
||||
rootfs_length=$( (tar xf "$tar_file" ${board_dir}/root -O | wc -c) 2> /dev/null)
|
||||
rootfs_type="$(identify_tar "$tar_file" ${board_dir}/root)"
|
||||
}
|
||||
|
||||
local has_kernel=1
|
||||
local has_env=0
|
||||
|
||||
[ "$kernel_length" != 0 -a -n "$kernel_mtd" ] && {
|
||||
tar xf "$tar_file" ${board_dir}/kernel -O | mtd write - $CI_KERNPART
|
||||
}
|
||||
[ "$kernel_length" = 0 -o ! -z "$kernel_mtd" ] && has_kernel=
|
||||
|
||||
nand_upgrade_prepare_ubi "$rootfs_length" "$rootfs_type" "${has_kernel:+$kernel_length}" "$has_env"
|
||||
|
||||
local ubidev="$( nand_find_ubi "$CI_UBIPART" )"
|
||||
[ "$has_kernel" = "1" ] && {
|
||||
local kern_ubivol="$( nand_find_volume $ubidev $CI_KERNPART )"
|
||||
tar xf "$tar_file" ${board_dir}/kernel -O | \
|
||||
ubiupdatevol /dev/$kern_ubivol -s $kernel_length -
|
||||
}
|
||||
|
||||
[ "$has_rootfs" = "1" ] && {
|
||||
local root_ubivol="$( nand_find_volume $ubidev $CI_ROOTPART )"
|
||||
tar xf "$tar_file" ${board_dir}/root -O | \
|
||||
ubiupdatevol /dev/$root_ubivol -s $rootfs_length -
|
||||
}
|
||||
nand_do_upgrade_success
|
||||
}
|
||||
|
||||
# Recognize type of passed file and start the upgrade process
|
||||
nand_do_upgrade() {
|
||||
local file_type=$(identify $1)
|
||||
|
||||
[ ! "$(find_mtd_index "$CI_UBIPART")" ] && CI_UBIPART="rootfs"
|
||||
|
||||
case "$file_type" in
|
||||
"fit") nand_upgrade_fit $1;;
|
||||
"ubi") nand_upgrade_ubinized $1;;
|
||||
"ubifs") nand_upgrade_ubifs $1;;
|
||||
*) nand_upgrade_tar $1;;
|
||||
esac
|
||||
}
|
||||
|
||||
# Check if passed file is a valid one for NAND sysupgrade. Currently it accepts
|
||||
# 3 types of files:
|
||||
# 1) UBI - should contain an ubinized image, header is checked for the proper
|
||||
# MAGIC
|
||||
# 2) UBIFS - should contain UBIFS partition that will replace "rootfs" volume,
|
||||
# header is checked for the proper MAGIC
|
||||
# 3) TAR - archive has to include "sysupgrade-BOARD" directory with a non-empty
|
||||
# "CONTROL" file (at this point its content isn't verified)
|
||||
#
|
||||
# You usually want to call this function in platform_check_image.
|
||||
#
|
||||
# $(1): board name, used in case of passing TAR file
|
||||
# $(2): file to be checked
|
||||
nand_do_platform_check() {
|
||||
local board_name="$1"
|
||||
local tar_file="$2"
|
||||
local control_length=$( (tar xf $tar_file sysupgrade-$board_name/CONTROL -O | wc -c) 2> /dev/null)
|
||||
local file_type="$(identify $2)"
|
||||
|
||||
[ "$control_length" = 0 -a "$file_type" != "ubi" -a "$file_type" != "ubifs" -a "$file_type" != "fit" ] && {
|
||||
echo "Invalid sysupgrade file."
|
||||
return 1
|
||||
}
|
||||
|
||||
return 0
|
||||
}
|
||||
166
package/base-files/files/lib/upgrade/stage2
Executable file
166
package/base-files/files/lib/upgrade/stage2
Executable file
@@ -0,0 +1,166 @@
|
||||
#!/bin/sh
|
||||
|
||||
. /lib/functions.sh
|
||||
. /lib/functions/system.sh
|
||||
|
||||
export IMAGE="$1"
|
||||
COMMAND="$2"
|
||||
|
||||
export INTERACTIVE=0
|
||||
export VERBOSE=1
|
||||
export CONFFILES=/tmp/sysupgrade.conffiles
|
||||
|
||||
RAMFS_COPY_BIN= # extra programs for temporary ramfs root
|
||||
RAMFS_COPY_DATA= # extra data files
|
||||
|
||||
include /lib/upgrade
|
||||
|
||||
|
||||
supivot() { # <new_root> <old_root>
|
||||
/bin/mount | grep "on $1 type" 2>&- 1>&- || /bin/mount -o bind $1 $1
|
||||
mkdir -p $1$2 $1/proc $1/sys $1/dev $1/tmp $1/overlay && \
|
||||
/bin/mount -o noatime,move /proc $1/proc && \
|
||||
pivot_root $1 $1$2 || {
|
||||
/bin/umount -l $1 $1
|
||||
return 1
|
||||
}
|
||||
|
||||
/bin/mount -o noatime,move $2/sys /sys
|
||||
/bin/mount -o noatime,move $2/dev /dev
|
||||
/bin/mount -o noatime,move $2/tmp /tmp
|
||||
/bin/mount -o noatime,move $2/overlay /overlay 2>&-
|
||||
return 0
|
||||
}
|
||||
|
||||
switch_to_ramfs() {
|
||||
RAMFS_COPY_LOSETUP="$(command -v /usr/sbin/losetup)"
|
||||
RAMFS_COPY_LVM="$(command -v lvm)"
|
||||
|
||||
for binary in \
|
||||
/bin/busybox /bin/ash /bin/sh /bin/mount /bin/umount \
|
||||
pivot_root mount_root reboot sync kill sleep \
|
||||
md5sum hexdump cat zcat dd tar \
|
||||
ls basename find cp mv rm mkdir rmdir mknod touch chmod \
|
||||
'[' printf wc grep awk sed cut \
|
||||
mtd partx losetup mkfs.ext4 nandwrite flash_erase \
|
||||
ubiupdatevol ubiattach ubiblock ubiformat \
|
||||
ubidetach ubirsvol ubirmvol ubimkvol \
|
||||
snapshot snapshot_tool date logger \
|
||||
$RAMFS_COPY_LOSETUP $RAMFS_COPY_LVM \
|
||||
$RAMFS_COPY_BIN
|
||||
do
|
||||
local file="$(command -v "$binary" 2>/dev/null)"
|
||||
[ -n "$file" ] && install_bin "$file"
|
||||
done
|
||||
install_file /etc/resolv.conf /lib/*.sh /lib/functions/*.sh /lib/upgrade/*.sh /lib/upgrade/do_stage2 /usr/share/libubox/jshn.sh $RAMFS_COPY_DATA
|
||||
|
||||
[ -L "/lib64" ] && ln -s /lib $RAM_ROOT/lib64
|
||||
|
||||
supivot $RAM_ROOT /mnt || {
|
||||
v "Failed to switch over to ramfs. Please reboot."
|
||||
exit 1
|
||||
}
|
||||
|
||||
/bin/mount -o remount,ro /mnt
|
||||
/bin/umount -l /mnt
|
||||
|
||||
grep -e "^/dev/dm-.*" -e "^/dev/loop.*" /proc/mounts | while read bdev mp _r; do
|
||||
umount $mp
|
||||
done
|
||||
|
||||
[ "$RAMFS_COPY_LOSETUP" ] && losetup -D
|
||||
[ "$RAMFS_COPY_LVM" ] && {
|
||||
mkdir -p /tmp/lvm/cache
|
||||
$RAMFS_COPY_LVM vgchange -aln --ignorelockingfailure
|
||||
}
|
||||
|
||||
grep /overlay /proc/mounts > /dev/null && {
|
||||
/bin/mount -o noatime,remount,ro /overlay
|
||||
/bin/umount -l /overlay
|
||||
}
|
||||
}
|
||||
|
||||
kill_remaining() { # [ <signal> [ <loop> ] ]
|
||||
local loop_limit=10
|
||||
|
||||
local sig="${1:-TERM}"
|
||||
local loop="${2:-0}"
|
||||
local run=true
|
||||
local stat
|
||||
local proc_ppid=$(cut -d' ' -f4 /proc/$$/stat)
|
||||
|
||||
v "Sending $sig to remaining processes ..."
|
||||
|
||||
while $run; do
|
||||
run=false
|
||||
for stat in /proc/[0-9]*/stat; do
|
||||
[ -f "$stat" ] || continue
|
||||
|
||||
local pid name state ppid rest
|
||||
read pid name state ppid rest < $stat
|
||||
name="${name#(}"; name="${name%)}"
|
||||
|
||||
# Skip PID1, our parent, ourself and our children
|
||||
[ $pid -ne 1 -a $pid -ne $proc_ppid -a $pid -ne $$ -a $ppid -ne $$ ] || continue
|
||||
|
||||
local cmdline
|
||||
read cmdline < /proc/$pid/cmdline
|
||||
|
||||
# Skip kernel threads
|
||||
[ -n "$cmdline" ] || continue
|
||||
|
||||
v "Sending signal $sig to $name ($pid)"
|
||||
kill -$sig $pid 2>/dev/null
|
||||
|
||||
[ $loop -eq 1 ] && run=true
|
||||
done
|
||||
|
||||
let loop_limit--
|
||||
[ $loop_limit -eq 0 ] && {
|
||||
v "Failed to kill all processes."
|
||||
exit 1
|
||||
}
|
||||
done
|
||||
}
|
||||
|
||||
indicate_upgrade
|
||||
|
||||
while read -r a b c; do
|
||||
case "$a" in
|
||||
MemT*) mem="$b" ;; esac
|
||||
done < /proc/meminfo
|
||||
|
||||
[ "$mem" -gt 32768 ] && \
|
||||
skip_services="dnsmasq log network"
|
||||
for service in /etc/init.d/*; do
|
||||
service=${service##*/}
|
||||
|
||||
case " $skip_services " in
|
||||
*" $service "*) continue ;; esac
|
||||
|
||||
ubus call service delete '{ "name": "'"$service"'" }' 2>/dev/null
|
||||
done
|
||||
|
||||
killall -9 telnetd 2>/dev/null
|
||||
killall -9 dropbear 2>/dev/null
|
||||
killall -9 ash 2>/dev/null
|
||||
|
||||
kill_remaining TERM
|
||||
sleep 4
|
||||
kill_remaining KILL 1
|
||||
|
||||
sleep 6
|
||||
|
||||
echo 3 > /proc/sys/vm/drop_caches
|
||||
|
||||
if [ -n "$IMAGE" ] && type 'platform_pre_upgrade' >/dev/null 2>/dev/null; then
|
||||
platform_pre_upgrade "$IMAGE"
|
||||
fi
|
||||
|
||||
if [ -n "$(rootfs_type)" ]; then
|
||||
v "Switching to ramdisk..."
|
||||
switch_to_ramfs
|
||||
fi
|
||||
|
||||
# Exec new shell from ramfs
|
||||
exec /bin/busybox ash -c "$COMMAND"
|
||||
Reference in New Issue
Block a user