diff options
Diffstat (limited to 'meta-phosphor/common/recipes-phosphor/obmc-phosphor-initfs/files/obmc-update.sh')
-rwxr-xr-x | meta-phosphor/common/recipes-phosphor/obmc-phosphor-initfs/files/obmc-update.sh | 165 |
1 files changed, 129 insertions, 36 deletions
diff --git a/meta-phosphor/common/recipes-phosphor/obmc-phosphor-initfs/files/obmc-update.sh b/meta-phosphor/common/recipes-phosphor/obmc-phosphor-initfs/files/obmc-update.sh index aa8fd8934f..b061e262fc 100755 --- a/meta-phosphor/common/recipes-phosphor/obmc-phosphor-initfs/files/obmc-update.sh +++ b/meta-phosphor/common/recipes-phosphor/obmc-phosphor-initfs/files/obmc-update.sh @@ -2,8 +2,9 @@ echo update: "$@" -export PS1=update-sh#\ -# exec /bin/sh +echoerr() { + echo 1>&2 "ERROR: $@" +} cd / if ! test -r /proc/mounts || ! test -f /proc/mounts @@ -21,12 +22,43 @@ then mkdir -p /dev mount -t devtmpfs dev dev fi -while grep mtd /proc/mounts -do - echo 1>&2 "Error: A mtd device is mounted." - sulogin - # exec /bin/sh -done + +# mtd number N with mtd name Name can be mounted via mtdN, or mtd:Name +# (with a mtd aware fs) or by /dev/mtdblockN (with a mtd or block fs). +mtdismounted() { + m=${1##mtd} + if grep -s "mtdblock$m " /proc/mounts || grep -s "mtd$m " /proc/mounts + then + return 0 + fi + n=$(cat /sys/class/mtd/mtd$m/name) + if test -n "$n" && grep -s "mtd:$n " /proc/mounts + then + return 0 + fi + return 1 +} + +# Detect child partitions when the whole flash is to be updated. +# Ignore mtdNro and mtdblockN names in the class subsystem directory. +childmtds() { + for m in /sys/class/mtd/$1/mtd* + do + m=${m##*/} + if test "${m%ro}" = "${m#mtdblock}" + then + echo $m + fi + done +} + +toobig() { + if test $(stat -L -c "%s" "$1") -gt $(cat /sys/class/mtd/"$2"/size) + then + return 0 + fi + return 1 +} findmtd() { m=$(grep -xl "$1" /sys/class/mtd/*/name) @@ -60,17 +92,39 @@ upper=$rwdir/cow save=/run/save/${upper##*/} mounted= +doflash=y doclean= dosave=y dorestore=y toram= +checksize=y +checkmount=y -whitelist=/run/initramfs/whitelist +whitelist=/run/initramfs/whitelist.d/ image=/run/initramfs/image- +imglist= while test "$1" != "${1#-}" do case "$1" in + --help) + cat <<HERE +Usage: $0 [options] -- Write images in /run/initramfs to flash (/dev/mtd*) + --help Show this message + --no-flash Don't attempt to write images to flash + --ignore-size Don't compare image size to mtd device size + --ignore-mount Don't check if destination is mounted + --save-files Copy whitelisted files to save directory in RAM + --no-save-files Don't copy whitelisted files to save directory + --copy-files Copy files from save directory to rwfs mountpoint + --restore-files Restore files from save directory to rwfs layer + --no-restore-files Don't restore saved files from ram to rwfs layer + --clean-saved-files Delete saved whitelisted files from RAM + --no-clean-saved-files Retain saved whitelisted files in RAM +HERE + + exit 0 ;; + --no-clean-saved-files) doclean= shift ;; @@ -89,11 +143,21 @@ do --restore-files) dorestore=y shift ;; + --no-flash) + doflash= + shift ;; + --ignore-size) + checksize= + shift ;; + --ignore-mount) + checkmount= + doflash= + shift ;; --copy-files) toram=y shift ;; *) - echo 2>&1 "Unknown option $1" + echoerr "Unknown option $1. Try $0 --help." exit 1 ;; esac done @@ -109,14 +173,27 @@ then while read f do - if ! test -e $upper/$f + # Entries shall start with /, no trailing /.. or embedded /../ + if test "/${f#/}" != "$f" -o "${f%/..}" != "${f#*/../}" + then + echo 1>&2 "WARNING: Skipping bad whitelist entry $f." + continue + fi + if ! test -e "$upper/$f" then continue fi d="$save/$f" + while test "${d%/}" != "${d%/.}" + do + d="${d%/.}" + d="${d%/}" + done mkdir -p "${d%/*}" - cp -rp $upper/$f "${d%/*}/" - done < $whitelist + cp -rp "$upper/$f" "${d%/*}/" + done << HERE +$(grep -v ^# $whitelist*) +HERE if test -n "$mounted" then @@ -124,30 +201,54 @@ then fi fi -for f in $image* +imglist=$(echo $image*) +if test "$imglist" = "$image*" -a ! -e "$imglist" +then + # shell didn't expand the wildcard, so no files exist + echo "No images found to update." + imglist= +fi + +for f in $imglist do m=$(findmtd ${f#$image}) if test -z "$m" then - echo 1>&2 "Unable to find mtd partiton for ${f##*/}." - exec /bin/sh + echoerr "Unable to find mtd partiton for ${f##*/}." + exit 1 fi -done - -for f in $image* -do - if test ! -s $f + if test -n "$checksize" && toobig "$f" "$m" then - echo "Skipping empty update of ${f#$image}." - rm $f - continue + echoerr "Image ${f##*/} too big for $m." + exit 1 fi - m=$(findmtd ${f#$image}) - echo "Updating ${f#$image}..." - flashcp -v $f /dev/$m && rm $f + for s in $m $(childmtds $m) + do + if test -n "$checkmount" && mtdismounted $s + then + echoerr "Device $s is mounted, ${f##*/} is busy." + exit 1 + fi + done done -if test "x$toram" = xy +if test -n "$doflash" +then + for f in $imglist + do + if test ! -s $f + then + echo "Skipping empty update of ${f#$image}." + rm $f + continue + fi + m=$(findmtd ${f#$image}) + echo "Updating ${f#$image}..." + flashcp -v $f /dev/$m && rm $f + done +fi + +if test -d $save -a "x$toram" = xy then mkdir -p $upper cp -rp $save/. $upper/ @@ -173,11 +274,3 @@ then fi exit - -# NOT REACHED without edit -# NOT REACHED without edit - -echo "Flash completed. Inspect, cleanup and reboot -f to continue." - -export PS1=update-sh#\ -exec /bin/sh |