Adriana Kobylak | a290eff | 2017-06-27 09:39:57 -0500 | [diff] [blame] | 1 | #!/bin/sh |
| 2 | |
Adriana Kobylak | 54c9f79 | 2017-08-17 14:08:20 -0500 | [diff] [blame] | 3 | # Get the root mtd device number (mtdX) from "/dev/ubiblockX_Y on /" |
| 4 | findrootmtd() { |
| 5 | rootmatch=" on / " |
| 6 | m="$(mount | grep "${rootmatch}" | grep "ubiblock")" |
| 7 | m="${m##*ubiblock}" |
| 8 | m="${m%_*}" |
| 9 | if [ -z "${m}" ]; then |
| 10 | # default to bmc mtd (0) |
| 11 | m=0 |
| 12 | fi |
| 13 | echo "mtd${m}" |
| 14 | } |
| 15 | |
Adriana Kobylak | a290eff | 2017-06-27 09:39:57 -0500 | [diff] [blame] | 16 | # Get the mtd device number (mtdX) |
| 17 | findmtd() { |
| 18 | m="$(grep -xl "$1" /sys/class/mtd/*/name)" |
| 19 | m="${m%/name}" |
| 20 | m="${m##*/}" |
| 21 | echo "${m}" |
| 22 | } |
| 23 | |
| 24 | # Get the ubi device number (ubiX_Y) |
| 25 | findubi() { |
| 26 | u="$(grep -xl "$1" /sys/class/ubi/ubi?/subsystem/ubi*/name)" |
| 27 | u="${u%/name}" |
| 28 | u="${u##*/}" |
| 29 | echo "${u}" |
| 30 | } |
| 31 | |
| 32 | ubi_rw() { |
| 33 | rwmtd="$(findmtd "${reqmtd}")" |
| 34 | rw="${rwmtd#mtd}" |
| 35 | ubidev="/dev/ubi${rw}" |
| 36 | |
Michael Tritz | a694eed | 2017-07-13 16:26:15 -0500 | [diff] [blame] | 37 | if [ "$(fw_printenv rwreset)" == "rwreset=true" ]; then |
| 38 | ubi_remove |
| 39 | fw_setenv rwreset |
| 40 | fi |
| 41 | |
Adriana Kobylak | a290eff | 2017-06-27 09:39:57 -0500 | [diff] [blame] | 42 | # Create a ubi volume of size 4MB, that is the current size of the rwfs image |
| 43 | vol="$(findubi "${name}")" |
| 44 | if [ -z "${vol}" ]; then |
| 45 | ubimkvol "${ubidev}" -N "${name}" -s 4MiB |
| 46 | fi |
| 47 | } |
| 48 | |
Adriana Kobylak | e993949 | 2017-07-11 11:34:23 -0500 | [diff] [blame] | 49 | ubi_ro() { |
| 50 | romtd="$(findmtd "${reqmtd}")" |
Adriana Kobylak | 54c9f79 | 2017-08-17 14:08:20 -0500 | [diff] [blame] | 51 | romtd2="$(findmtd "${reqmtd2}")" |
| 52 | |
| 53 | if [ ! "${romtd}" == "${romtd2}" ]; then |
| 54 | # Request to use alternate mtd device, choose the non-root one |
| 55 | rootmtd="$(findrootmtd)" |
| 56 | if [ "${rootmtd}" == "${romtd}" ]; then |
| 57 | romtd="${romtd2}" |
| 58 | fi |
| 59 | fi |
Adriana Kobylak | e993949 | 2017-07-11 11:34:23 -0500 | [diff] [blame] | 60 | ro="${romtd#mtd}" |
| 61 | ubidev="/dev/ubi${ro}" |
| 62 | |
| 63 | # Create a static ubi volume |
| 64 | # TODO Get the actual image size openbmc/openbmc#1840 |
| 65 | vol="$(findubi "${name}")" |
| 66 | if [ -z "${vol}" ]; then |
| 67 | ubimkvol "${ubidev}" -N "${name}" -s "${imgsize}" --type=static |
| 68 | vol="$(findubi "${name}")" |
| 69 | fi |
| 70 | } |
| 71 | |
| 72 | # Squashfs images need a ubi block |
| 73 | ubi_block() { |
| 74 | vol="$(findubi "${name}")" |
| 75 | ubidevid="${vol#ubi}" |
| 76 | block="/dev/ubiblock${ubidevid}" |
| 77 | if [ ! -e "$block" ]; then |
| 78 | ubiblock --create "/dev/ubi${ubidevid}" |
| 79 | fi |
| 80 | } |
| 81 | |
| 82 | ubi_updatevol() { |
| 83 | vol="$(findubi "${name}")" |
| 84 | ubidevid="${vol#ubi}" |
| 85 | img="/tmp/images/${version}/${imgfile}" |
| 86 | ubiupdatevol "/dev/ubi${ubidevid}" "${img}" |
| 87 | } |
| 88 | |
Michael Tritz | a694eed | 2017-07-13 16:26:15 -0500 | [diff] [blame] | 89 | ubi_remove() { |
| 90 | vol="$(findubi "${name}")" |
| 91 | |
| 92 | if [ ! -z "$vol" ]; then |
| 93 | vol="${vol%_*}" |
| 94 | |
| 95 | if grep -q "${vol}:$name" /proc/mounts; then |
| 96 | mountdir=$(grep "${vol}:$name" /proc/mounts | cut -d " " -f 2) |
| 97 | umount "$mountdir" |
| 98 | rm -r "$mountdir" |
| 99 | fi |
| 100 | |
| 101 | ubirmvol "/dev/${vol}" -N "$name" |
| 102 | fi |
| 103 | } |
| 104 | |
Saqib Khan | bb93e64 | 2017-08-10 11:24:38 -0500 | [diff] [blame] | 105 | remount_ubi() { |
| 106 | # Get a list of all devices formatted as UBI |
| 107 | for file in /dev/mtd*; do |
| 108 | if [[ $(hexdump -C -n 3 $file) =~ "UBI" ]]; then |
| 109 | mtd="${file: -1}" |
| 110 | if [[ $mtd =~ ^-?[0-9]+$ ]]; then |
| 111 | mtds=${mtd},${mtds} |
| 112 | fi |
| 113 | fi |
| 114 | done |
| 115 | |
| 116 | IFS=',' read -r -a mtds <<< "$mtds" |
| 117 | mtds=($(echo "${mtds[@]}" | tr ' ' '\n' | sort -u | tr '\n' ' ')) |
| 118 | for mtd in ${mtds[@]}; do |
| 119 | # Re-attach mtd device to ubi if not already done |
| 120 | ubiattach /dev/ubi_ctrl -m "${mtd}" -d "${mtd}" &> /dev/null |
| 121 | # Get information on all ubi volumes |
| 122 | ubinfo=$(ubinfo -d ${mtd}) |
| 123 | presentVolumes=${ubinfo##*:} |
| 124 | IFS=', ' read -r -a array <<< "$presentVolumes" |
| 125 | for element in ${array[@]}; do |
| 126 | elementProperties=$(ubinfo -d $mtd -n $element) |
| 127 | # Get ubi volume name by getting rid of additional properties |
| 128 | name=${elementProperties#*Name:} |
| 129 | name="${name%Character*}" |
| 130 | name="$(echo -e "${name}" | tr -d '[:space:]')" |
| 131 | |
Adriana Kobylak | 2c7c8d1 | 2017-08-21 11:00:56 -0500 | [diff] [blame] | 132 | if [[ ${name} == rofs-* ]]; then |
Saqib Khan | bb93e64 | 2017-08-10 11:24:38 -0500 | [diff] [blame] | 133 | mountdir="/media/${name}" |
| 134 | mkdir -p "${mountdir}" |
| 135 | ubiblock --create /dev/ubi${mtd}_${element} &> /dev/null |
| 136 | mount -t squashfs -o ro "/dev/ubiblock${mtd}_${element}" "${mountdir}" |
| 137 | fi |
| 138 | done |
| 139 | done |
| 140 | } |
| 141 | |
Michael Tritz | a694eed | 2017-07-13 16:26:15 -0500 | [diff] [blame] | 142 | ubi_setenv() { |
| 143 | varName="${variable%"\x3d"*}" |
| 144 | value="${variable##*"\x3d"}" |
| 145 | fw_setenv "$varName" "$value" |
| 146 | } |
| 147 | |
Adriana Kobylak | a290eff | 2017-06-27 09:39:57 -0500 | [diff] [blame] | 148 | case "$1" in |
| 149 | ubirw) |
| 150 | reqmtd="$2" |
| 151 | name="$3" |
| 152 | ubi_rw |
| 153 | ;; |
Adriana Kobylak | e993949 | 2017-07-11 11:34:23 -0500 | [diff] [blame] | 154 | ubiro) |
Adriana Kobylak | 54c9f79 | 2017-08-17 14:08:20 -0500 | [diff] [blame] | 155 | reqmtd="$(echo "$2" | cut -d "+" -f 1)" |
| 156 | reqmtd2="$(echo "$2" | cut -d "+" -f 2)" |
Adriana Kobylak | e993949 | 2017-07-11 11:34:23 -0500 | [diff] [blame] | 157 | name="$3" |
| 158 | version="$4" |
| 159 | imgfile="image-rofs" |
| 160 | imgsize="16MiB" |
| 161 | ubi_ro |
| 162 | ubi_block |
| 163 | ubi_updatevol |
| 164 | ;; |
| 165 | ubikernel) |
Adriana Kobylak | 54c9f79 | 2017-08-17 14:08:20 -0500 | [diff] [blame] | 166 | reqmtd="$(echo "$2" | cut -d "+" -f 1)" |
| 167 | reqmtd2="$(echo "$2" | cut -d "+" -f 2)" |
Adriana Kobylak | e993949 | 2017-07-11 11:34:23 -0500 | [diff] [blame] | 168 | name="$3" |
| 169 | version="$4" |
| 170 | imgfile="image-kernel" |
| 171 | imgsize="4MiB" |
| 172 | ubi_ro |
| 173 | ubi_updatevol |
| 174 | ;; |
Michael Tritz | a694eed | 2017-07-13 16:26:15 -0500 | [diff] [blame] | 175 | ubiremove) |
| 176 | name="$2" |
| 177 | ubi_remove |
| 178 | ;; |
| 179 | ubisetenv) |
| 180 | variable="$2" |
| 181 | ubi_setenv |
| 182 | ;; |
Saqib Khan | bb93e64 | 2017-08-10 11:24:38 -0500 | [diff] [blame] | 183 | ubiremount) |
| 184 | remount_ubi |
| 185 | ;; |
Adriana Kobylak | a290eff | 2017-06-27 09:39:57 -0500 | [diff] [blame] | 186 | *) |
| 187 | echo "Invalid argument" |
| 188 | exit 1 |
| 189 | ;; |
| 190 | esac |
| 191 | rc=$? |
| 192 | if [ ${rc} -ne 0 ]; then |
| 193 | echo "$0: error ${rc}" |
| 194 | exit ${rc} |
| 195 | fi |