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