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