Milton D. Miller II | 0e77514 | 2016-01-20 14:57:54 -0600 | [diff] [blame] | 1 | #!/bin/sh |
| 2 | |
| 3 | echo update: "$@" |
| 4 | |
| 5 | export PS1=update-sh#\ |
| 6 | # exec /bin/sh |
| 7 | |
| 8 | cd / |
| 9 | if ! test -r /proc/mounts || ! test -f /proc/mounts |
| 10 | then |
| 11 | mkdir -p /proc |
| 12 | mount -t proc proc proc |
| 13 | fi |
| 14 | if ! test -d /sys/class |
| 15 | then |
| 16 | mkdir -p /sys |
| 17 | mount -t sysfs sys sys |
| 18 | fi |
| 19 | if ! test -c /dev/null |
| 20 | then |
| 21 | mkdir -p /dev |
| 22 | mount -t devtmpfs dev dev |
| 23 | fi |
| 24 | while grep mtd /proc/mounts |
| 25 | do |
| 26 | echo 1>&2 "Error: A mtd device is mounted." |
| 27 | sulogin |
| 28 | # exec /bin/sh |
| 29 | done |
| 30 | |
| 31 | findmtd() { |
| 32 | m=$(grep -xl "$1" /sys/class/mtd/*/name) |
| 33 | m=${m%/name} |
| 34 | m=${m##*/} |
| 35 | echo $m |
| 36 | } |
| 37 | |
Milton D. Miller II | 0e77514 | 2016-01-20 14:57:54 -0600 | [diff] [blame] | 38 | rwfs=$(findmtd rwfs) |
| 39 | |
Milton Miller | ba65b7b | 2016-02-05 12:07:53 -0600 | [diff] [blame^] | 40 | rwdev=/dev/mtdblock${rwfs#mtd} |
Milton D. Miller II | 0e77514 | 2016-01-20 14:57:54 -0600 | [diff] [blame] | 41 | rwfst=ext4 |
Milton Miller | ba65b7b | 2016-02-05 12:07:53 -0600 | [diff] [blame^] | 42 | rwopts=rw |
| 43 | rorwopts=ro${rwopts#rw} |
| 44 | |
| 45 | rwdir=rw |
| 46 | upper=$rwdir/cow |
| 47 | save=save/${upper##*/} |
Milton D. Miller II | 0e77514 | 2016-01-20 14:57:54 -0600 | [diff] [blame] | 48 | |
| 49 | if test -n "$rwfs" && test -s whitelist |
| 50 | then |
| 51 | |
Milton Miller | ba65b7b | 2016-02-05 12:07:53 -0600 | [diff] [blame^] | 52 | mkdir -p $rwdir |
| 53 | mount $rwdev $rwdir -t $rwfst -o $rorwopts |
Milton D. Miller II | 0e77514 | 2016-01-20 14:57:54 -0600 | [diff] [blame] | 54 | |
| 55 | while read f |
| 56 | do |
Milton Miller | ba65b7b | 2016-02-05 12:07:53 -0600 | [diff] [blame^] | 57 | if ! test -e $upper/$f |
Milton D. Miller II | 0e77514 | 2016-01-20 14:57:54 -0600 | [diff] [blame] | 58 | then |
| 59 | continue |
| 60 | fi |
Milton Miller | ba65b7b | 2016-02-05 12:07:53 -0600 | [diff] [blame^] | 61 | d="$save/$f" |
Milton D. Miller II | 0e77514 | 2016-01-20 14:57:54 -0600 | [diff] [blame] | 62 | mkdir -p "${d%/*}" |
Milton Miller | ba65b7b | 2016-02-05 12:07:53 -0600 | [diff] [blame^] | 63 | cp -rp $upper/$f "${d%/*}/" |
Milton D. Miller II | 0e77514 | 2016-01-20 14:57:54 -0600 | [diff] [blame] | 64 | done < whitelist |
| 65 | |
Milton Miller | ba65b7b | 2016-02-05 12:07:53 -0600 | [diff] [blame^] | 66 | umount $rwdir |
Milton D. Miller II | 0e77514 | 2016-01-20 14:57:54 -0600 | [diff] [blame] | 67 | fi |
| 68 | |
Milton Miller | 36d336c | 2016-02-05 11:19:58 -0600 | [diff] [blame] | 69 | image=/run/initramfs/image- |
| 70 | for f in $image* |
Milton D. Miller II | 0e77514 | 2016-01-20 14:57:54 -0600 | [diff] [blame] | 71 | do |
Milton Miller | 36d336c | 2016-02-05 11:19:58 -0600 | [diff] [blame] | 72 | m=$(findmtd ${f#$image}) |
Milton D. Miller II | 0e77514 | 2016-01-20 14:57:54 -0600 | [diff] [blame] | 73 | if test -z "$m" |
| 74 | then |
Milton Miller | ba65b7b | 2016-02-05 12:07:53 -0600 | [diff] [blame^] | 75 | echo 1>&2 "Unable to find mtd partiton for ${f##*/}." |
Milton D. Miller II | 0e77514 | 2016-01-20 14:57:54 -0600 | [diff] [blame] | 76 | exec /bin/sh |
| 77 | fi |
| 78 | done |
| 79 | |
Milton Miller | 36d336c | 2016-02-05 11:19:58 -0600 | [diff] [blame] | 80 | for f in $image* |
Milton D. Miller II | 0e77514 | 2016-01-20 14:57:54 -0600 | [diff] [blame] | 81 | do |
Milton Miller | 36d336c | 2016-02-05 11:19:58 -0600 | [diff] [blame] | 82 | m=$(findmtd ${f#$image}) |
| 83 | echo "Updating ${f#$image}..." |
Milton D. Miller II | 0e77514 | 2016-01-20 14:57:54 -0600 | [diff] [blame] | 84 | # flasheraseall /dev/$m && dd if=$f of=/dev/$m |
| 85 | flashcp -v $f /dev/$m |
| 86 | done |
| 87 | |
Milton Miller | ba65b7b | 2016-02-05 12:07:53 -0600 | [diff] [blame^] | 88 | if test -d $save |
Milton D. Miller II | 0e77514 | 2016-01-20 14:57:54 -0600 | [diff] [blame] | 89 | then |
Milton Miller | ba65b7b | 2016-02-05 12:07:53 -0600 | [diff] [blame^] | 90 | mount $rwdev $rwdir -t $rwfst -o $rwopts |
| 91 | cp -rp $save/. $upper/ |
| 92 | umount $rwdir |
Milton D. Miller II | 0e77514 | 2016-01-20 14:57:54 -0600 | [diff] [blame] | 93 | fi |
| 94 | |
| 95 | # Execute the command systemd told us to ... |
| 96 | if test -d /oldroot && test -x "/sbin/$1" && test -f "/sbin/$1" |
| 97 | then |
| 98 | if test "$1" == kexec |
| 99 | then |
| 100 | /sbin/$1 -f -e |
| 101 | else |
| 102 | /sbin/$1 -f |
| 103 | fi |
| 104 | fi |
| 105 | |
| 106 | |
| 107 | echo "Execute ${1-reboot} -f if all is ok" |
| 108 | |
| 109 | export PS1=update-sh#\ |
| 110 | exec /bin/sh |