Milton D. Miller II | d89d5e0 | 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 | acc2c85 | 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 | d89d5e0 | 2016-01-20 14:57:54 -0600 | [diff] [blame] | 52 | rwfs=$(findmtd rwfs) |
| 53 | |
Milton Miller | 54d882e | 2016-02-05 12:07:53 -0600 | [diff] [blame] | 54 | rwdev=/dev/mtdblock${rwfs#mtd} |
Milton Miller | 54d882e | 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 | d89d5e0 | 2016-01-20 14:57:54 -0600 | [diff] [blame] | 61 | |
| 62 | if test -n "$rwfs" && test -s whitelist |
| 63 | then |
| 64 | |
Milton Miller | 54d882e | 2016-02-05 12:07:53 -0600 | [diff] [blame] | 65 | mkdir -p $rwdir |
Andrew Jeffery | acc2c85 | 2016-02-23 23:47:23 +1030 | [diff] [blame] | 66 | mount $rwdev $rwdir -t $(probe_fs_type $rwdev) -o $rorwopts |
Milton D. Miller II | d89d5e0 | 2016-01-20 14:57:54 -0600 | [diff] [blame] | 67 | |
| 68 | while read f |
| 69 | do |
Milton Miller | 54d882e | 2016-02-05 12:07:53 -0600 | [diff] [blame] | 70 | if ! test -e $upper/$f |
Milton D. Miller II | d89d5e0 | 2016-01-20 14:57:54 -0600 | [diff] [blame] | 71 | then |
| 72 | continue |
| 73 | fi |
Milton Miller | 54d882e | 2016-02-05 12:07:53 -0600 | [diff] [blame] | 74 | d="$save/$f" |
Milton D. Miller II | d89d5e0 | 2016-01-20 14:57:54 -0600 | [diff] [blame] | 75 | mkdir -p "${d%/*}" |
Milton Miller | 54d882e | 2016-02-05 12:07:53 -0600 | [diff] [blame] | 76 | cp -rp $upper/$f "${d%/*}/" |
Milton D. Miller II | d89d5e0 | 2016-01-20 14:57:54 -0600 | [diff] [blame] | 77 | done < whitelist |
| 78 | |
Milton Miller | 54d882e | 2016-02-05 12:07:53 -0600 | [diff] [blame] | 79 | umount $rwdir |
Milton D. Miller II | d89d5e0 | 2016-01-20 14:57:54 -0600 | [diff] [blame] | 80 | fi |
| 81 | |
Milton Miller | d93b48e | 2016-02-05 11:19:58 -0600 | [diff] [blame] | 82 | image=/run/initramfs/image- |
| 83 | for f in $image* |
Milton D. Miller II | d89d5e0 | 2016-01-20 14:57:54 -0600 | [diff] [blame] | 84 | do |
Milton Miller | d93b48e | 2016-02-05 11:19:58 -0600 | [diff] [blame] | 85 | m=$(findmtd ${f#$image}) |
Milton D. Miller II | d89d5e0 | 2016-01-20 14:57:54 -0600 | [diff] [blame] | 86 | if test -z "$m" |
| 87 | then |
Milton Miller | 54d882e | 2016-02-05 12:07:53 -0600 | [diff] [blame] | 88 | echo 1>&2 "Unable to find mtd partiton for ${f##*/}." |
Milton D. Miller II | d89d5e0 | 2016-01-20 14:57:54 -0600 | [diff] [blame] | 89 | exec /bin/sh |
| 90 | fi |
| 91 | done |
| 92 | |
Milton Miller | d93b48e | 2016-02-05 11:19:58 -0600 | [diff] [blame] | 93 | for f in $image* |
Milton D. Miller II | d89d5e0 | 2016-01-20 14:57:54 -0600 | [diff] [blame] | 94 | do |
Milton Miller | d93b48e | 2016-02-05 11:19:58 -0600 | [diff] [blame] | 95 | m=$(findmtd ${f#$image}) |
| 96 | echo "Updating ${f#$image}..." |
Milton D. Miller II | d89d5e0 | 2016-01-20 14:57:54 -0600 | [diff] [blame] | 97 | # flasheraseall /dev/$m && dd if=$f of=/dev/$m |
| 98 | flashcp -v $f /dev/$m |
| 99 | done |
| 100 | |
Milton Miller | 54d882e | 2016-02-05 12:07:53 -0600 | [diff] [blame] | 101 | if test -d $save |
Milton D. Miller II | d89d5e0 | 2016-01-20 14:57:54 -0600 | [diff] [blame] | 102 | then |
Andrew Jeffery | acc2c85 | 2016-02-23 23:47:23 +1030 | [diff] [blame] | 103 | mount $rwdev $rwdir -t $(probe_fs_type $rwdev) -o $rwopts |
Milton Miller | 54d882e | 2016-02-05 12:07:53 -0600 | [diff] [blame] | 104 | cp -rp $save/. $upper/ |
| 105 | umount $rwdir |
Milton D. Miller II | d89d5e0 | 2016-01-20 14:57:54 -0600 | [diff] [blame] | 106 | fi |
| 107 | |
Milton Miller | 39b6faa | 2016-02-05 13:56:18 -0600 | [diff] [blame] | 108 | exit |
Milton D. Miller II | d89d5e0 | 2016-01-20 14:57:54 -0600 | [diff] [blame] | 109 | |
Milton Miller | 39b6faa | 2016-02-05 13:56:18 -0600 | [diff] [blame] | 110 | # NOT REACHED without edit |
| 111 | # NOT REACHED without edit |
Milton D. Miller II | d89d5e0 | 2016-01-20 14:57:54 -0600 | [diff] [blame] | 112 | |
Milton Miller | 39b6faa | 2016-02-05 13:56:18 -0600 | [diff] [blame] | 113 | echo "Flash completed. Inspect, cleanup and reboot -f to continue." |
Milton D. Miller II | d89d5e0 | 2016-01-20 14:57:54 -0600 | [diff] [blame] | 114 | |
| 115 | export PS1=update-sh#\ |
| 116 | exec /bin/sh |