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 | a694eed | 2017-07-13 16:26:15 -0500 | [diff] [blame] | 85 | if [ "$(fw_printenv rwreset)" == "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 |
Adriana Kobylak | 8cdb82e | 2017-08-22 14:18:39 -0500 | [diff] [blame] | 136 | copy_env_var_to_alt ubiblock |
| 137 | ubi_setenv "ubiblock=$(echo "${ubidevid}" | sed 's/_/,/')" |
| 138 | copy_env_var_to_alt root |
| 139 | ubi_setenv "root=${block}" |
Adriana Kobylak | e993949 | 2017-07-11 11:34:23 -0500 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | ubi_updatevol() { |
| 143 | vol="$(findubi "${name}")" |
| 144 | ubidevid="${vol#ubi}" |
| 145 | img="/tmp/images/${version}/${imgfile}" |
| 146 | ubiupdatevol "/dev/ubi${ubidevid}" "${img}" |
Adriana Kobylak | 8cdb82e | 2017-08-22 14:18:39 -0500 | [diff] [blame] | 147 | if [ $? != 0 ]; then |
| 148 | echo "Unable to update volume ${name}!" |
| 149 | return 1 |
| 150 | fi |
| 151 | if [[ "${name}" =~ "kernel" ]]; then |
| 152 | copy_env_var_to_alt kernelname |
| 153 | ubi_setenv "kernelname=${name}" |
| 154 | fi |
Adriana Kobylak | e993949 | 2017-07-11 11:34:23 -0500 | [diff] [blame] | 155 | } |
| 156 | |
Michael Tritz | a694eed | 2017-07-13 16:26:15 -0500 | [diff] [blame] | 157 | ubi_remove() { |
Adriana Kobylak | 582bdea | 2017-08-22 14:51:18 -0500 | [diff] [blame] | 158 | rmname="$1" |
| 159 | vol="$(findubi "${rmname}")" |
Michael Tritz | a694eed | 2017-07-13 16:26:15 -0500 | [diff] [blame] | 160 | |
| 161 | if [ ! -z "$vol" ]; then |
| 162 | vol="${vol%_*}" |
| 163 | |
Saqib Khan | f8d2a66 | 2017-09-07 13:57:43 -0500 | [diff] [blame] | 164 | if grep -q "$rmname" /proc/mounts; then |
| 165 | mountdir=$(grep "$rmname" /proc/mounts | cut -d " " -f 2) |
Michael Tritz | a694eed | 2017-07-13 16:26:15 -0500 | [diff] [blame] | 166 | umount "$mountdir" |
| 167 | rm -r "$mountdir" |
| 168 | fi |
| 169 | |
Adriana Kobylak | 582bdea | 2017-08-22 14:51:18 -0500 | [diff] [blame] | 170 | ubirmvol "/dev/${vol}" -N "$rmname" |
Michael Tritz | a694eed | 2017-07-13 16:26:15 -0500 | [diff] [blame] | 171 | fi |
| 172 | } |
| 173 | |
Saqib Khan | bb93e64 | 2017-08-10 11:24:38 -0500 | [diff] [blame] | 174 | remount_ubi() { |
Saqib Khan | d6d1c44 | 2017-09-06 23:24:40 -0500 | [diff] [blame] | 175 | bmcmtd="$(findmtd "bmc")" |
| 176 | altbmcmtd="$(findmtd "alt-bmc")" |
| 177 | mtds="${bmcmtd: -1}","${altbmcmtd: -1}" |
Saqib Khan | bb93e64 | 2017-08-10 11:24:38 -0500 | [diff] [blame] | 178 | |
| 179 | IFS=',' read -r -a mtds <<< "$mtds" |
| 180 | mtds=($(echo "${mtds[@]}" | tr ' ' '\n' | sort -u | tr '\n' ' ')) |
| 181 | for mtd in ${mtds[@]}; do |
| 182 | # Re-attach mtd device to ubi if not already done |
| 183 | ubiattach /dev/ubi_ctrl -m "${mtd}" -d "${mtd}" &> /dev/null |
| 184 | # Get information on all ubi volumes |
| 185 | ubinfo=$(ubinfo -d ${mtd}) |
| 186 | presentVolumes=${ubinfo##*:} |
| 187 | IFS=', ' read -r -a array <<< "$presentVolumes" |
| 188 | for element in ${array[@]}; do |
| 189 | elementProperties=$(ubinfo -d $mtd -n $element) |
| 190 | # Get ubi volume name by getting rid of additional properties |
| 191 | name=${elementProperties#*Name:} |
| 192 | name="${name%Character*}" |
| 193 | name="$(echo -e "${name}" | tr -d '[:space:]')" |
| 194 | |
Adriana Kobylak | 2c7c8d1 | 2017-08-21 11:00:56 -0500 | [diff] [blame] | 195 | if [[ ${name} == rofs-* ]]; then |
Saqib Khan | bb93e64 | 2017-08-10 11:24:38 -0500 | [diff] [blame] | 196 | mountdir="/media/${name}" |
| 197 | mkdir -p "${mountdir}" |
| 198 | ubiblock --create /dev/ubi${mtd}_${element} &> /dev/null |
| 199 | mount -t squashfs -o ro "/dev/ubiblock${mtd}_${element}" "${mountdir}" |
| 200 | fi |
| 201 | done |
| 202 | done |
| 203 | } |
| 204 | |
Adriana Kobylak | 8cdb82e | 2017-08-22 14:18:39 -0500 | [diff] [blame] | 205 | # Read the current env variable and set it on the alternate boot env |
| 206 | copy_env_var_to_alt() { |
| 207 | varName=$1 |
| 208 | value="$(fw_printenv -n "${varName}")" |
| 209 | fw_setenv -c /etc/alt_fw_env.config "${varName}" "${value}" |
| 210 | } |
| 211 | |
Michael Tritz | a694eed | 2017-07-13 16:26:15 -0500 | [diff] [blame] | 212 | ubi_setenv() { |
Adriana Kobylak | 8cdb82e | 2017-08-22 14:18:39 -0500 | [diff] [blame] | 213 | variable=$1 |
Michael Tritz | 8863f70 | 2017-08-25 15:47:07 -0500 | [diff] [blame] | 214 | if [[ "$variable" == *"="* ]]; then |
| 215 | varName="${variable%=*}" |
| 216 | value="${variable##*=}" |
| 217 | fw_setenv "$varName" "$value" |
| 218 | else |
| 219 | fw_setenv "$variable" |
| 220 | fi |
Michael Tritz | a694eed | 2017-07-13 16:26:15 -0500 | [diff] [blame] | 221 | } |
| 222 | |
Adriana Kobylak | 1fa612c | 2017-08-17 21:56:14 -0500 | [diff] [blame] | 223 | mtd_write() { |
| 224 | flashmtd="$(findmtd "${reqmtd}")" |
| 225 | img="/tmp/images/${version}/${imgfile}" |
| 226 | flashcp -v ${img} /dev/${flashmtd} |
| 227 | } |
| 228 | |
Adriana Kobylak | a290eff | 2017-06-27 09:39:57 -0500 | [diff] [blame] | 229 | case "$1" in |
Adriana Kobylak | 1fa612c | 2017-08-17 21:56:14 -0500 | [diff] [blame] | 230 | mtduboot) |
| 231 | reqmtd="$2" |
| 232 | version="$3" |
| 233 | imgfile="image-u-boot" |
| 234 | mtd_write |
| 235 | ;; |
Adriana Kobylak | a290eff | 2017-06-27 09:39:57 -0500 | [diff] [blame] | 236 | ubirw) |
| 237 | reqmtd="$2" |
| 238 | name="$3" |
| 239 | ubi_rw |
| 240 | ;; |
Adriana Kobylak | e993949 | 2017-07-11 11:34:23 -0500 | [diff] [blame] | 241 | ubiro) |
Adriana Kobylak | 54c9f79 | 2017-08-17 14:08:20 -0500 | [diff] [blame] | 242 | reqmtd="$(echo "$2" | cut -d "+" -f 1)" |
| 243 | reqmtd2="$(echo "$2" | cut -d "+" -f 2)" |
Adriana Kobylak | e993949 | 2017-07-11 11:34:23 -0500 | [diff] [blame] | 244 | name="$3" |
| 245 | version="$4" |
| 246 | imgfile="image-rofs" |
| 247 | imgsize="16MiB" |
| 248 | ubi_ro |
Adriana Kobylak | e993949 | 2017-07-11 11:34:23 -0500 | [diff] [blame] | 249 | ubi_updatevol |
Adriana Kobylak | 8cdb82e | 2017-08-22 14:18:39 -0500 | [diff] [blame] | 250 | ubi_block |
Adriana Kobylak | e993949 | 2017-07-11 11:34:23 -0500 | [diff] [blame] | 251 | ;; |
| 252 | ubikernel) |
Adriana Kobylak | 54c9f79 | 2017-08-17 14:08:20 -0500 | [diff] [blame] | 253 | reqmtd="$(echo "$2" | cut -d "+" -f 1)" |
| 254 | reqmtd2="$(echo "$2" | cut -d "+" -f 2)" |
Adriana Kobylak | e993949 | 2017-07-11 11:34:23 -0500 | [diff] [blame] | 255 | name="$3" |
| 256 | version="$4" |
| 257 | imgfile="image-kernel" |
| 258 | imgsize="4MiB" |
| 259 | ubi_ro |
| 260 | ubi_updatevol |
| 261 | ;; |
Michael Tritz | a694eed | 2017-07-13 16:26:15 -0500 | [diff] [blame] | 262 | ubiremove) |
| 263 | name="$2" |
Adriana Kobylak | 582bdea | 2017-08-22 14:51:18 -0500 | [diff] [blame] | 264 | ubi_remove "${name}" |
Michael Tritz | a694eed | 2017-07-13 16:26:15 -0500 | [diff] [blame] | 265 | ;; |
| 266 | ubisetenv) |
Adriana Kobylak | 8cdb82e | 2017-08-22 14:18:39 -0500 | [diff] [blame] | 267 | ubi_setenv "$2" |
Michael Tritz | a694eed | 2017-07-13 16:26:15 -0500 | [diff] [blame] | 268 | ;; |
Saqib Khan | bb93e64 | 2017-08-10 11:24:38 -0500 | [diff] [blame] | 269 | ubiremount) |
| 270 | remount_ubi |
| 271 | ;; |
Adriana Kobylak | a290eff | 2017-06-27 09:39:57 -0500 | [diff] [blame] | 272 | *) |
| 273 | echo "Invalid argument" |
| 274 | exit 1 |
| 275 | ;; |
| 276 | esac |
| 277 | rc=$? |
| 278 | if [ ${rc} -ne 0 ]; then |
| 279 | echo "$0: error ${rc}" |
| 280 | exit ${rc} |
| 281 | fi |