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