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 | 582bdea | 2017-08-22 14:51:18 -0500 | [diff] [blame] | 16 | findrootubi() { |
| 17 | rootmatch=" on / " |
| 18 | m="$(mount | grep "${rootmatch}")" |
| 19 | m="${m##*ubiblock}" |
| 20 | m="${m% on*}" |
| 21 | echo "ubi${m}" |
| 22 | } |
| 23 | |
Adriana Kobylak | a290eff | 2017-06-27 09:39:57 -0500 | [diff] [blame] | 24 | # Get the mtd device number (mtdX) |
| 25 | findmtd() { |
| 26 | m="$(grep -xl "$1" /sys/class/mtd/*/name)" |
| 27 | m="${m%/name}" |
| 28 | m="${m##*/}" |
| 29 | echo "${m}" |
| 30 | } |
| 31 | |
| 32 | # Get the ubi device number (ubiX_Y) |
| 33 | findubi() { |
| 34 | u="$(grep -xl "$1" /sys/class/ubi/ubi?/subsystem/ubi*/name)" |
| 35 | u="${u%/name}" |
| 36 | u="${u##*/}" |
| 37 | echo "${u}" |
| 38 | } |
| 39 | |
Adriana Kobylak | 582bdea | 2017-08-22 14:51:18 -0500 | [diff] [blame] | 40 | # Get all ubi devices on a specific mtd that match requested string |
| 41 | findubi_onmtd() { |
| 42 | u="$(grep -h "$1" /sys/class/ubi/ubi"$2"/subsystem/ubi"$2"*/name)" |
| 43 | u="${u%/name}" |
| 44 | u="${u##*/}" |
| 45 | echo "${u}" |
| 46 | } |
| 47 | |
| 48 | # Make space on flash before creating new volumes. This can be enhanced |
| 49 | # determine current flash usage. For now only keep a "keepmax" number of them |
| 50 | ubi_remove_volumes() |
| 51 | { |
| 52 | kernelname="$(fw_printenv -n kernelname)" |
| 53 | curversion="${kernelname##*-}" |
| 54 | rootubi="$(findrootubi)" |
| 55 | |
| 56 | # Just keep max number of volumes before updating, don't delete the version |
| 57 | # the BMC is booted from, and when a version is identified to be deleted, |
| 58 | # delete both the rofs and kernel volumes for that version. |
| 59 | rmnames="$(findubi_onmtd "${name%-*}-" "${ro}")" |
| 60 | rmnames=(${rmnames}) |
| 61 | ubicount="${#rmnames[@]}" |
| 62 | while [ ${ubicount} -ge ${keepmax} ]; do |
| 63 | # Loop through existing volumes and skip currently active ones |
| 64 | for (( index=0; index<${#rmnames[@]}; index++ )); do |
| 65 | rmname="${rmnames[${index}]}" |
| 66 | rmversion="${rmname##*-}" |
| 67 | rmubi="$(findubi "rofs-${rmversion}")" |
| 68 | if [[ ( "${rmubi}" != "${rootubi}" ) && |
| 69 | ( "${rmname}" != "${kernelname}" ) ]]; then |
| 70 | ubi_remove "rofs-${rmversion}" |
| 71 | ubi_remove "kernel-${rmversion}" |
| 72 | break |
| 73 | fi |
| 74 | done |
| 75 | # Decrease count regardless to avoid an infinite loop |
| 76 | (( ubicount-- )) |
| 77 | done |
| 78 | } |
| 79 | |
Adriana Kobylak | a290eff | 2017-06-27 09:39:57 -0500 | [diff] [blame] | 80 | ubi_rw() { |
| 81 | rwmtd="$(findmtd "${reqmtd}")" |
| 82 | rw="${rwmtd#mtd}" |
| 83 | ubidev="/dev/ubi${rw}" |
| 84 | |
Michael Tritz | 741aac1 | 2017-09-07 15:13:04 -0500 | [diff] [blame] | 85 | if [ "$(fw_printenv rwreset 2>/dev/null)" == "rwreset=true" ]; then |
Adriana Kobylak | 582bdea | 2017-08-22 14:51:18 -0500 | [diff] [blame] | 86 | ubi_remove "${name}" |
Michael Tritz | a694eed | 2017-07-13 16:26:15 -0500 | [diff] [blame] | 87 | fw_setenv rwreset |
| 88 | fi |
| 89 | |
Adriana Kobylak | a290eff | 2017-06-27 09:39:57 -0500 | [diff] [blame] | 90 | # Create a ubi volume of size 4MB, that is the current size of the rwfs image |
| 91 | vol="$(findubi "${name}")" |
| 92 | if [ -z "${vol}" ]; then |
| 93 | ubimkvol "${ubidev}" -N "${name}" -s 4MiB |
| 94 | fi |
| 95 | } |
| 96 | |
Adriana Kobylak | e993949 | 2017-07-11 11:34:23 -0500 | [diff] [blame] | 97 | ubi_ro() { |
Adriana Kobylak | 582bdea | 2017-08-22 14:51:18 -0500 | [diff] [blame] | 98 | keepmax=2 # Default 2 volumes per mtd |
Adriana Kobylak | e993949 | 2017-07-11 11:34:23 -0500 | [diff] [blame] | 99 | romtd="$(findmtd "${reqmtd}")" |
Adriana Kobylak | 54c9f79 | 2017-08-17 14:08:20 -0500 | [diff] [blame] | 100 | romtd2="$(findmtd "${reqmtd2}")" |
| 101 | |
| 102 | if [ ! "${romtd}" == "${romtd2}" ]; then |
| 103 | # Request to use alternate mtd device, choose the non-root one |
Adriana Kobylak | 582bdea | 2017-08-22 14:51:18 -0500 | [diff] [blame] | 104 | keepmax=1 # 1 volume on each of the requested mtds |
Adriana Kobylak | 54c9f79 | 2017-08-17 14:08:20 -0500 | [diff] [blame] | 105 | rootmtd="$(findrootmtd)" |
| 106 | if [ "${rootmtd}" == "${romtd}" ]; then |
| 107 | romtd="${romtd2}" |
| 108 | fi |
| 109 | fi |
Adriana Kobylak | e993949 | 2017-07-11 11:34:23 -0500 | [diff] [blame] | 110 | ro="${romtd#mtd}" |
| 111 | ubidev="/dev/ubi${ro}" |
| 112 | |
Adriana Kobylak | 582bdea | 2017-08-22 14:51:18 -0500 | [diff] [blame] | 113 | ubi_remove_volumes |
| 114 | |
Adriana Kobylak | e993949 | 2017-07-11 11:34:23 -0500 | [diff] [blame] | 115 | # Create a static ubi volume |
| 116 | # TODO Get the actual image size openbmc/openbmc#1840 |
| 117 | vol="$(findubi "${name}")" |
| 118 | if [ -z "${vol}" ]; then |
| 119 | ubimkvol "${ubidev}" -N "${name}" -s "${imgsize}" --type=static |
| 120 | vol="$(findubi "${name}")" |
| 121 | fi |
| 122 | } |
| 123 | |
| 124 | # Squashfs images need a ubi block |
| 125 | ubi_block() { |
| 126 | vol="$(findubi "${name}")" |
| 127 | ubidevid="${vol#ubi}" |
| 128 | block="/dev/ubiblock${ubidevid}" |
| 129 | if [ ! -e "$block" ]; then |
| 130 | ubiblock --create "/dev/ubi${ubidevid}" |
Adriana Kobylak | 8cdb82e | 2017-08-22 14:18:39 -0500 | [diff] [blame] | 131 | if [ $? != 0 ]; then |
| 132 | echo "Unable to create ubiblock ${name}:${ubidevid}" |
| 133 | return 1 |
| 134 | fi |
Adriana Kobylak | e993949 | 2017-07-11 11:34:23 -0500 | [diff] [blame] | 135 | fi |
| 136 | } |
| 137 | |
| 138 | ubi_updatevol() { |
| 139 | vol="$(findubi "${name}")" |
| 140 | ubidevid="${vol#ubi}" |
| 141 | img="/tmp/images/${version}/${imgfile}" |
| 142 | ubiupdatevol "/dev/ubi${ubidevid}" "${img}" |
Adriana Kobylak | 8cdb82e | 2017-08-22 14:18:39 -0500 | [diff] [blame] | 143 | if [ $? != 0 ]; then |
| 144 | echo "Unable to update volume ${name}!" |
| 145 | return 1 |
| 146 | fi |
Adriana Kobylak | e993949 | 2017-07-11 11:34:23 -0500 | [diff] [blame] | 147 | } |
| 148 | |
Michael Tritz | a694eed | 2017-07-13 16:26:15 -0500 | [diff] [blame] | 149 | ubi_remove() { |
Adriana Kobylak | 582bdea | 2017-08-22 14:51:18 -0500 | [diff] [blame] | 150 | rmname="$1" |
| 151 | vol="$(findubi "${rmname}")" |
Michael Tritz | a694eed | 2017-07-13 16:26:15 -0500 | [diff] [blame] | 152 | |
| 153 | if [ ! -z "$vol" ]; then |
| 154 | vol="${vol%_*}" |
| 155 | |
Saqib Khan | f8d2a66 | 2017-09-07 13:57:43 -0500 | [diff] [blame] | 156 | if grep -q "$rmname" /proc/mounts; then |
| 157 | mountdir=$(grep "$rmname" /proc/mounts | cut -d " " -f 2) |
Michael Tritz | a694eed | 2017-07-13 16:26:15 -0500 | [diff] [blame] | 158 | umount "$mountdir" |
| 159 | rm -r "$mountdir" |
| 160 | fi |
| 161 | |
Adriana Kobylak | 582bdea | 2017-08-22 14:51:18 -0500 | [diff] [blame] | 162 | ubirmvol "/dev/${vol}" -N "$rmname" |
Michael Tritz | a694eed | 2017-07-13 16:26:15 -0500 | [diff] [blame] | 163 | fi |
| 164 | } |
| 165 | |
Saqib Khan | bb93e64 | 2017-08-10 11:24:38 -0500 | [diff] [blame] | 166 | remount_ubi() { |
Saqib Khan | d6d1c44 | 2017-09-06 23:24:40 -0500 | [diff] [blame] | 167 | bmcmtd="$(findmtd "bmc")" |
| 168 | altbmcmtd="$(findmtd "alt-bmc")" |
| 169 | mtds="${bmcmtd: -1}","${altbmcmtd: -1}" |
Saqib Khan | bb93e64 | 2017-08-10 11:24:38 -0500 | [diff] [blame] | 170 | |
| 171 | IFS=',' read -r -a mtds <<< "$mtds" |
| 172 | mtds=($(echo "${mtds[@]}" | tr ' ' '\n' | sort -u | tr '\n' ' ')) |
| 173 | for mtd in ${mtds[@]}; do |
| 174 | # Re-attach mtd device to ubi if not already done |
| 175 | ubiattach /dev/ubi_ctrl -m "${mtd}" -d "${mtd}" &> /dev/null |
| 176 | # Get information on all ubi volumes |
| 177 | ubinfo=$(ubinfo -d ${mtd}) |
| 178 | presentVolumes=${ubinfo##*:} |
| 179 | IFS=', ' read -r -a array <<< "$presentVolumes" |
| 180 | for element in ${array[@]}; do |
| 181 | elementProperties=$(ubinfo -d $mtd -n $element) |
| 182 | # Get ubi volume name by getting rid of additional properties |
| 183 | name=${elementProperties#*Name:} |
| 184 | name="${name%Character*}" |
| 185 | name="$(echo -e "${name}" | tr -d '[:space:]')" |
| 186 | |
Adriana Kobylak | 2c7c8d1 | 2017-08-21 11:00:56 -0500 | [diff] [blame] | 187 | if [[ ${name} == rofs-* ]]; then |
Saqib Khan | bb93e64 | 2017-08-10 11:24:38 -0500 | [diff] [blame] | 188 | mountdir="/media/${name}" |
| 189 | mkdir -p "${mountdir}" |
| 190 | ubiblock --create /dev/ubi${mtd}_${element} &> /dev/null |
| 191 | mount -t squashfs -o ro "/dev/ubiblock${mtd}_${element}" "${mountdir}" |
| 192 | fi |
| 193 | done |
| 194 | done |
| 195 | } |
| 196 | |
Adriana Kobylak | 8cdb82e | 2017-08-22 14:18:39 -0500 | [diff] [blame] | 197 | # Read the current env variable and set it on the alternate boot env |
| 198 | copy_env_var_to_alt() { |
| 199 | varName=$1 |
| 200 | value="$(fw_printenv -n "${varName}")" |
| 201 | fw_setenv -c /etc/alt_fw_env.config "${varName}" "${value}" |
| 202 | } |
| 203 | |
Michael Tritz | a694eed | 2017-07-13 16:26:15 -0500 | [diff] [blame] | 204 | ubi_setenv() { |
Adriana Kobylak | 8cdb82e | 2017-08-22 14:18:39 -0500 | [diff] [blame] | 205 | variable=$1 |
Michael Tritz | 8863f70 | 2017-08-25 15:47:07 -0500 | [diff] [blame] | 206 | if [[ "$variable" == *"="* ]]; then |
| 207 | varName="${variable%=*}" |
| 208 | value="${variable##*=}" |
| 209 | fw_setenv "$varName" "$value" |
| 210 | else |
| 211 | fw_setenv "$variable" |
| 212 | fi |
Michael Tritz | a694eed | 2017-07-13 16:26:15 -0500 | [diff] [blame] | 213 | } |
| 214 | |
Adriana Kobylak | 1fa612c | 2017-08-17 21:56:14 -0500 | [diff] [blame] | 215 | mtd_write() { |
| 216 | flashmtd="$(findmtd "${reqmtd}")" |
| 217 | img="/tmp/images/${version}/${imgfile}" |
| 218 | flashcp -v ${img} /dev/${flashmtd} |
| 219 | } |
| 220 | |
Adriana Kobylak | e2fd46d | 2017-09-05 12:38:19 -0500 | [diff] [blame] | 221 | backup_env_vars() { |
| 222 | copy_env_var_to_alt kernelname |
| 223 | copy_env_var_to_alt ubiblock |
| 224 | copy_env_var_to_alt root |
| 225 | } |
| 226 | |
| 227 | update_env_vars() { |
| 228 | vol="$(findubi rofs-"${version}")" |
| 229 | if [ -z "${vol}" ]; then |
| 230 | return 1 |
| 231 | fi |
| 232 | ubidevid="${vol#ubi}" |
| 233 | block="/dev/ubiblock${ubidevid}" |
| 234 | if [ ! -e "${block}" ]; then |
| 235 | return 1 |
| 236 | fi |
| 237 | ubi_setenv "kernelname=kernel-${version}" |
| 238 | ubi_setenv "ubiblock=$(echo "${ubidevid}" | sed 's/_/,/')" |
| 239 | ubi_setenv "root=${block}" |
| 240 | } |
| 241 | |
Adriana Kobylak | a290eff | 2017-06-27 09:39:57 -0500 | [diff] [blame] | 242 | case "$1" in |
Adriana Kobylak | 1fa612c | 2017-08-17 21:56:14 -0500 | [diff] [blame] | 243 | mtduboot) |
| 244 | reqmtd="$2" |
| 245 | version="$3" |
| 246 | imgfile="image-u-boot" |
| 247 | mtd_write |
| 248 | ;; |
Adriana Kobylak | a290eff | 2017-06-27 09:39:57 -0500 | [diff] [blame] | 249 | ubirw) |
| 250 | reqmtd="$2" |
| 251 | name="$3" |
| 252 | ubi_rw |
| 253 | ;; |
Adriana Kobylak | e993949 | 2017-07-11 11:34:23 -0500 | [diff] [blame] | 254 | ubiro) |
Adriana Kobylak | 54c9f79 | 2017-08-17 14:08:20 -0500 | [diff] [blame] | 255 | reqmtd="$(echo "$2" | cut -d "+" -f 1)" |
| 256 | reqmtd2="$(echo "$2" | cut -d "+" -f 2)" |
Adriana Kobylak | e993949 | 2017-07-11 11:34:23 -0500 | [diff] [blame] | 257 | name="$3" |
| 258 | version="$4" |
| 259 | imgfile="image-rofs" |
| 260 | imgsize="16MiB" |
| 261 | ubi_ro |
Adriana Kobylak | e993949 | 2017-07-11 11:34:23 -0500 | [diff] [blame] | 262 | ubi_updatevol |
Adriana Kobylak | 8cdb82e | 2017-08-22 14:18:39 -0500 | [diff] [blame] | 263 | ubi_block |
Adriana Kobylak | e993949 | 2017-07-11 11:34:23 -0500 | [diff] [blame] | 264 | ;; |
| 265 | ubikernel) |
Adriana Kobylak | 54c9f79 | 2017-08-17 14:08:20 -0500 | [diff] [blame] | 266 | reqmtd="$(echo "$2" | cut -d "+" -f 1)" |
| 267 | reqmtd2="$(echo "$2" | cut -d "+" -f 2)" |
Adriana Kobylak | e993949 | 2017-07-11 11:34:23 -0500 | [diff] [blame] | 268 | name="$3" |
| 269 | version="$4" |
| 270 | imgfile="image-kernel" |
| 271 | imgsize="4MiB" |
| 272 | ubi_ro |
| 273 | ubi_updatevol |
| 274 | ;; |
Michael Tritz | a694eed | 2017-07-13 16:26:15 -0500 | [diff] [blame] | 275 | ubiremove) |
| 276 | name="$2" |
Adriana Kobylak | 582bdea | 2017-08-22 14:51:18 -0500 | [diff] [blame] | 277 | ubi_remove "${name}" |
Michael Tritz | a694eed | 2017-07-13 16:26:15 -0500 | [diff] [blame] | 278 | ;; |
| 279 | ubisetenv) |
Adriana Kobylak | 8cdb82e | 2017-08-22 14:18:39 -0500 | [diff] [blame] | 280 | ubi_setenv "$2" |
Michael Tritz | a694eed | 2017-07-13 16:26:15 -0500 | [diff] [blame] | 281 | ;; |
Saqib Khan | bb93e64 | 2017-08-10 11:24:38 -0500 | [diff] [blame] | 282 | ubiremount) |
| 283 | remount_ubi |
| 284 | ;; |
Adriana Kobylak | e2fd46d | 2017-09-05 12:38:19 -0500 | [diff] [blame] | 285 | preupdate) |
| 286 | backup_env_vars |
| 287 | ;; |
| 288 | postupdate) |
| 289 | version="$2" |
| 290 | update_env_vars |
| 291 | ;; |
Adriana Kobylak | a290eff | 2017-06-27 09:39:57 -0500 | [diff] [blame] | 292 | *) |
| 293 | echo "Invalid argument" |
| 294 | exit 1 |
| 295 | ;; |
| 296 | esac |
| 297 | rc=$? |
| 298 | if [ ${rc} -ne 0 ]; then |
| 299 | echo "$0: error ${rc}" |
| 300 | exit ${rc} |
| 301 | fi |