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 | |
Andrew Jeffery | 25a5022 | 2016-02-23 23:47:23 +1030 | [diff] [blame] | 38 | blkid_fs_type() { |
| 39 | # Emulate util-linux's `blkid -s TYPE -o value $1` |
| 40 | # Example busybox blkid output: |
| 41 | # # blkid /dev/mtdblock5 |
| 42 | # /dev/mtdblock5: TYPE="squashfs" |
| 43 | # Process output to extract TYPE value "squashfs". |
| 44 | blkid $1 | sed -e 's/^.*TYPE="//' -e 's/".*$//' |
| 45 | } |
| 46 | |
| 47 | probe_fs_type() { |
| 48 | fst=$(blkid_fs_type $1) |
| 49 | echo ${fst:=jffs2} |
| 50 | } |
| 51 | |
Milton D. Miller II | 0e77514 | 2016-01-20 14:57:54 -0600 | [diff] [blame] | 52 | rwfs=$(findmtd rwfs) |
| 53 | |
Milton Miller | ba65b7b | 2016-02-05 12:07:53 -0600 | [diff] [blame] | 54 | rwdev=/dev/mtdblock${rwfs#mtd} |
Milton Miller | ba65b7b | 2016-02-05 12:07:53 -0600 | [diff] [blame] | 55 | rwopts=rw |
| 56 | rorwopts=ro${rwopts#rw} |
| 57 | |
| 58 | rwdir=rw |
| 59 | upper=$rwdir/cow |
| 60 | save=save/${upper##*/} |
Milton D. Miller II | 0e77514 | 2016-01-20 14:57:54 -0600 | [diff] [blame] | 61 | |
Milton D. Miller II | d0b0c6a | 2016-02-28 16:32:14 -0600 | [diff] [blame^] | 62 | doclean= |
| 63 | |
| 64 | while test "$1" != "${1#-}" |
| 65 | do |
| 66 | case "$1" in |
| 67 | --no-clean-saved-files) |
| 68 | doclean= |
| 69 | shift ;; |
| 70 | --clean-saved-files) |
| 71 | doclean=y |
| 72 | shift ;; |
| 73 | *) |
| 74 | echo 2>&1 "Unknown option $1" |
| 75 | exit 1 ;; |
| 76 | esac |
| 77 | done |
| 78 | |
Milton D. Miller II | 0e77514 | 2016-01-20 14:57:54 -0600 | [diff] [blame] | 79 | if test -n "$rwfs" && test -s whitelist |
| 80 | then |
| 81 | |
Milton Miller | ba65b7b | 2016-02-05 12:07:53 -0600 | [diff] [blame] | 82 | mkdir -p $rwdir |
Andrew Jeffery | 25a5022 | 2016-02-23 23:47:23 +1030 | [diff] [blame] | 83 | mount $rwdev $rwdir -t $(probe_fs_type $rwdev) -o $rorwopts |
Milton D. Miller II | 0e77514 | 2016-01-20 14:57:54 -0600 | [diff] [blame] | 84 | |
| 85 | while read f |
| 86 | do |
Milton Miller | ba65b7b | 2016-02-05 12:07:53 -0600 | [diff] [blame] | 87 | if ! test -e $upper/$f |
Milton D. Miller II | 0e77514 | 2016-01-20 14:57:54 -0600 | [diff] [blame] | 88 | then |
| 89 | continue |
| 90 | fi |
Milton Miller | ba65b7b | 2016-02-05 12:07:53 -0600 | [diff] [blame] | 91 | d="$save/$f" |
Milton D. Miller II | 0e77514 | 2016-01-20 14:57:54 -0600 | [diff] [blame] | 92 | mkdir -p "${d%/*}" |
Milton Miller | ba65b7b | 2016-02-05 12:07:53 -0600 | [diff] [blame] | 93 | cp -rp $upper/$f "${d%/*}/" |
Milton D. Miller II | 0e77514 | 2016-01-20 14:57:54 -0600 | [diff] [blame] | 94 | done < whitelist |
| 95 | |
Milton Miller | ba65b7b | 2016-02-05 12:07:53 -0600 | [diff] [blame] | 96 | umount $rwdir |
Milton D. Miller II | 0e77514 | 2016-01-20 14:57:54 -0600 | [diff] [blame] | 97 | fi |
| 98 | |
Milton Miller | 36d336c | 2016-02-05 11:19:58 -0600 | [diff] [blame] | 99 | image=/run/initramfs/image- |
| 100 | for f in $image* |
Milton D. Miller II | 0e77514 | 2016-01-20 14:57:54 -0600 | [diff] [blame] | 101 | do |
Milton Miller | 36d336c | 2016-02-05 11:19:58 -0600 | [diff] [blame] | 102 | m=$(findmtd ${f#$image}) |
Milton D. Miller II | 0e77514 | 2016-01-20 14:57:54 -0600 | [diff] [blame] | 103 | if test -z "$m" |
| 104 | then |
Milton Miller | ba65b7b | 2016-02-05 12:07:53 -0600 | [diff] [blame] | 105 | echo 1>&2 "Unable to find mtd partiton for ${f##*/}." |
Milton D. Miller II | 0e77514 | 2016-01-20 14:57:54 -0600 | [diff] [blame] | 106 | exec /bin/sh |
| 107 | fi |
| 108 | done |
| 109 | |
Milton Miller | 36d336c | 2016-02-05 11:19:58 -0600 | [diff] [blame] | 110 | for f in $image* |
Milton D. Miller II | 0e77514 | 2016-01-20 14:57:54 -0600 | [diff] [blame] | 111 | do |
Milton Miller | 36d336c | 2016-02-05 11:19:58 -0600 | [diff] [blame] | 112 | m=$(findmtd ${f#$image}) |
| 113 | echo "Updating ${f#$image}..." |
Milton D. Miller II | 0e77514 | 2016-01-20 14:57:54 -0600 | [diff] [blame] | 114 | # flasheraseall /dev/$m && dd if=$f of=/dev/$m |
| 115 | flashcp -v $f /dev/$m |
| 116 | done |
| 117 | |
Milton Miller | ba65b7b | 2016-02-05 12:07:53 -0600 | [diff] [blame] | 118 | if test -d $save |
Milton D. Miller II | 0e77514 | 2016-01-20 14:57:54 -0600 | [diff] [blame] | 119 | then |
Andrew Jeffery | 25a5022 | 2016-02-23 23:47:23 +1030 | [diff] [blame] | 120 | mount $rwdev $rwdir -t $(probe_fs_type $rwdev) -o $rwopts |
Milton Miller | ba65b7b | 2016-02-05 12:07:53 -0600 | [diff] [blame] | 121 | cp -rp $save/. $upper/ |
| 122 | umount $rwdir |
Milton D. Miller II | 0e77514 | 2016-01-20 14:57:54 -0600 | [diff] [blame] | 123 | fi |
| 124 | |
Milton D. Miller II | d0b0c6a | 2016-02-28 16:32:14 -0600 | [diff] [blame^] | 125 | if test "x$doclean" = xy |
| 126 | then |
| 127 | rm -rf $save |
| 128 | fi |
| 129 | |
Milton Miller | dbacf10 | 2016-02-05 13:56:18 -0600 | [diff] [blame] | 130 | exit |
Milton D. Miller II | 0e77514 | 2016-01-20 14:57:54 -0600 | [diff] [blame] | 131 | |
Milton Miller | dbacf10 | 2016-02-05 13:56:18 -0600 | [diff] [blame] | 132 | # NOT REACHED without edit |
| 133 | # NOT REACHED without edit |
Milton D. Miller II | 0e77514 | 2016-01-20 14:57:54 -0600 | [diff] [blame] | 134 | |
Milton Miller | dbacf10 | 2016-02-05 13:56:18 -0600 | [diff] [blame] | 135 | echo "Flash completed. Inspect, cleanup and reboot -f to continue." |
Milton D. Miller II | 0e77514 | 2016-01-20 14:57:54 -0600 | [diff] [blame] | 136 | |
| 137 | export PS1=update-sh#\ |
| 138 | exec /bin/sh |