| Adriana Kobylak | bee8ffd | 2021-04-21 14:17:34 -0500 | [diff] [blame] | 1 | #!/bin/bash | 
| Adriana Kobylak | 10e915a | 2019-05-31 13:25:36 -0500 | [diff] [blame] | 2 |  | 
 | 3 | # Get the mtd device number (mtdX) | 
 | 4 | findmtd() { | 
 | 5 |   m="$(grep -xl "$1" /sys/class/mtd/*/name)" | 
 | 6 |   m="${m%/name}" | 
 | 7 |   m="${m##*/}" | 
 | 8 |   echo "${m}" | 
 | 9 | } | 
 | 10 |  | 
 | 11 | # Get the ubi device number (ubiX_Y) | 
 | 12 | findubi() { | 
 | 13 |   u="$(grep -xl "$1" /sys/class/ubi/ubi?/subsystem/ubi*/name)" | 
 | 14 |   u="${u%/name}" | 
 | 15 |   u="${u##*/}" | 
 | 16 |   echo "${u}" | 
 | 17 | } | 
 | 18 |  | 
 | 19 | # Get the mount information | 
 | 20 | is_mounted() { | 
 | 21 |   grep -q "$1" /proc/mounts | 
 | 22 |   return $? | 
 | 23 | } | 
 | 24 |  | 
 | 25 | # Attach the pnor mtd device to ubi. | 
 | 26 | attach_ubi() { | 
 | 27 |   pnormtd="$(findmtd pnor)" | 
 | 28 |   pnor="${pnormtd#mtd}" | 
 | 29 |   pnordev="/dev/mtd${pnor}" | 
 | 30 |  | 
 | 31 |   if [ -d "/sys/class/ubi/ubi${pnor}" ]; then | 
 | 32 |     # Already attached | 
 | 33 |     return 0 | 
 | 34 |   fi | 
 | 35 |  | 
 | 36 |   ubiattach /dev/ubi_ctrl -m "${pnor}" -d "${pnor}" | 
 | 37 |   rc=$? | 
 | 38 |   if [ ${rc} -ne 0 ]; then | 
 | 39 |     # Check the pnor mtd device is formatted as ubi by reading the first 3 byes, | 
 | 40 |     # which should be the ascii chars 'UBI' | 
| Adriana Kobylak | bee8ffd | 2021-04-21 14:17:34 -0500 | [diff] [blame] | 41 |     magic="$(hexdump -C -n 3 "${pnordev}")" | 
| Adriana Kobylak | 10e915a | 2019-05-31 13:25:36 -0500 | [diff] [blame] | 42 |     if [[ "${magic}" =~ "UBI" ]]; then | 
 | 43 |       # Device already formatted as ubi, ubiattach failed for some other reason | 
 | 44 |       return ${rc} | 
 | 45 |     else | 
 | 46 |       # Format device as ubi | 
 | 47 |       echo "Starting ubiformat ${pnordev}" | 
 | 48 |       ubiformat "${pnordev}" -y -q | 
 | 49 |       # Retry the ubiattach | 
 | 50 |       ubiattach /dev/ubi_ctrl -m "${pnor}" -d "${pnor}" | 
 | 51 |     fi | 
 | 52 |   fi | 
 | 53 | } | 
 | 54 |  | 
 | 55 | mount_squashfs() { | 
 | 56 |   pnormtd="$(findmtd pnor)" | 
 | 57 |   ubidev="/dev/ubi${pnormtd#mtd}" | 
 | 58 |   mountdir="/media/${name}" | 
 | 59 |   vol="$(findubi "${name}")" | 
 | 60 |   img="/tmp/images/${version}/pnor.xz.squashfs" | 
| Adriana Kobylak | bee8ffd | 2021-04-21 14:17:34 -0500 | [diff] [blame] | 61 |   # shellcheck disable=SC2012 # ls provides the size in human-readable form | 
 | 62 |   filesize="$(ls -sh "$img" | awk -F " " '{print $1}')" | 
| Adriana Kobylak | 10e915a | 2019-05-31 13:25:36 -0500 | [diff] [blame] | 63 |  | 
 | 64 |   if is_mounted "${name}"; then | 
 | 65 |     echo "${name} is already mounted." | 
 | 66 |     return 0 | 
 | 67 |   fi | 
 | 68 |  | 
| Adriana Kobylak | bee8ffd | 2021-04-21 14:17:34 -0500 | [diff] [blame] | 69 |   if [ -n "${vol}" ]; then | 
| Adriana Kobylak | 10e915a | 2019-05-31 13:25:36 -0500 | [diff] [blame] | 70 |     ubirmvol "${ubidev}" -N "${name}" | 
 | 71 |   fi | 
 | 72 |  | 
 | 73 |   if [ ! -d "${mountdir}" ]; then | 
 | 74 |     mkdir "${mountdir}" | 
 | 75 |   fi | 
 | 76 |  | 
 | 77 |   # Set size of read-only partition equal to pnor.xz.squashfs | 
 | 78 |   ubimkvol "${ubidev}" -N "${name}" -s "${filesize}"KiB --type=static | 
| Adriana Kobylak | bee8ffd | 2021-04-21 14:17:34 -0500 | [diff] [blame] | 79 |   if ! vol="$(findubi "${name}")"; then | 
| Adriana Kobylak | 10e915a | 2019-05-31 13:25:36 -0500 | [diff] [blame] | 80 |     echo "Unable to create RO volume!" | 
 | 81 |     return 1 | 
 | 82 |   fi | 
 | 83 |  | 
 | 84 |   ubidevid="${vol#ubi}" | 
| Adriana Kobylak | bee8ffd | 2021-04-21 14:17:34 -0500 | [diff] [blame] | 85 |   if ! ubiupdatevol "/dev/ubi${ubidevid}" "${img}"; then | 
| Adriana Kobylak | 10e915a | 2019-05-31 13:25:36 -0500 | [diff] [blame] | 86 |     echo "Unable to update RO volume!" | 
 | 87 |     return 1 | 
 | 88 |   fi | 
 | 89 |  | 
| Adriana Kobylak | bee8ffd | 2021-04-21 14:17:34 -0500 | [diff] [blame] | 90 |   if ! ubiblock --create "/dev/ubi${ubidevid}"; then | 
| Adriana Kobylak | 10e915a | 2019-05-31 13:25:36 -0500 | [diff] [blame] | 91 |     echo "Unable to create UBI block for RO volume!" | 
 | 92 |     return 1 | 
 | 93 |   fi | 
 | 94 |  | 
| Adriana Kobylak | bee8ffd | 2021-04-21 14:17:34 -0500 | [diff] [blame] | 95 |   if ! mount -t squashfs -o ro "/dev/ubiblock${ubidevid}" "${mountdir}"; then | 
| Adriana Kobylak | 10e915a | 2019-05-31 13:25:36 -0500 | [diff] [blame] | 96 |     echo "Unable to mount RO volume!" | 
 | 97 |     return 1 | 
 | 98 |   fi | 
 | 99 | } | 
 | 100 |  | 
 | 101 | mount_ubi() { | 
 | 102 |   pnormtd="$(findmtd pnor)" | 
 | 103 |   pnor="${pnormtd#mtd}" | 
 | 104 |   ubidev="/dev/ubi${pnor}" | 
 | 105 |   pnordev="/dev/mtd${pnor}" | 
 | 106 |  | 
 | 107 |   if [[ "${name}" == "pnor-patch" ]]; then | 
 | 108 |     if [[ "$(fw_printenv fieldmode 2>/dev/null)" == "fieldmode=true" ]]; then | 
 | 109 |       return 0 | 
 | 110 |     fi | 
| Adriana Kobylak | bee8ffd | 2021-04-21 14:17:34 -0500 | [diff] [blame] | 111 |     if [[ ! "$(hexdump -C -n 3 "${pnordev}")" =~ "UBI" ]]; then | 
| Adriana Kobylak | 10e915a | 2019-05-31 13:25:36 -0500 | [diff] [blame] | 112 |       return 0 | 
 | 113 |     fi | 
 | 114 |     mountdir="/usr/local/share/pnor" | 
 | 115 |   else | 
 | 116 |     mountdir="/media/${name}" | 
 | 117 |   fi | 
 | 118 |  | 
 | 119 |   if [[ "${name}" == "pnor-prsv" ]]; then | 
 | 120 |     size="2MiB" | 
 | 121 |   else | 
 | 122 |     size="16MiB" | 
 | 123 |   fi | 
 | 124 |  | 
 | 125 |   if [ ! -d "${mountdir}" ]; then | 
 | 126 |     mkdir -p "${mountdir}" | 
 | 127 |   fi | 
 | 128 |  | 
 | 129 |   vol="$(findubi "${name}")" | 
 | 130 |   if [ -z "${vol}" ]; then | 
 | 131 |     ubimkvol "${ubidev}" -N "${name}" -s "${size}" | 
 | 132 |   fi | 
 | 133 |  | 
 | 134 |   if ! is_mounted "${name}"; then | 
 | 135 |     mountdev="ubi${pnor}:${name}" | 
 | 136 |     mount -t ubifs "${mountdev}" "${mountdir}" | 
 | 137 |   fi | 
 | 138 | } | 
 | 139 |  | 
 | 140 | umount_ubi() { | 
 | 141 |   pnormtd="$(findmtd pnor)" | 
 | 142 |   pnor="${pnormtd#mtd}" | 
 | 143 |   ubidev="/dev/ubi${pnor}" | 
 | 144 |   mountdir="/media/${name}" | 
 | 145 |  | 
 | 146 |   if is_mounted "${name}"; then | 
 | 147 |     umount "${mountdir}" | 
 | 148 |   fi | 
 | 149 |  | 
 | 150 |   vol="$(findubi "${name}")" | 
 | 151 |   id="${vol##*_}" | 
 | 152 |   if [ -n "${id}" ]; then | 
 | 153 |     ubirmvol "${ubidev}" -n "${id}" | 
 | 154 |   fi | 
 | 155 |  | 
 | 156 |   if [ -d "${mountdir}" ]; then | 
 | 157 |     rm -r "${mountdir}" | 
 | 158 |   fi | 
 | 159 | } | 
 | 160 |  | 
 | 161 | remount_ubi() { | 
 | 162 |   pnormtd="$(findmtd pnor)" | 
 | 163 |   pnor="${pnormtd#mtd}" | 
 | 164 |   pnordev="/dev/mtd${pnor}" | 
 | 165 |  | 
 | 166 |   # Re-Attach the pnor mtd device to ubi | 
| Adriana Kobylak | bee8ffd | 2021-04-21 14:17:34 -0500 | [diff] [blame] | 167 |   if [[ $(hexdump -C -n 3 "${pnordev}") =~ "UBI" ]]; then | 
| Adriana Kobylak | 10e915a | 2019-05-31 13:25:36 -0500 | [diff] [blame] | 168 |     ubiattach /dev/ubi_ctrl -m "${pnor}" -d "${pnor}" | 
 | 169 |   else | 
 | 170 |     # Device not formatted as ubi. | 
 | 171 |     return 0 | 
 | 172 |   fi | 
 | 173 |  | 
 | 174 |   # Get information on all ubi volumes | 
| Adriana Kobylak | bee8ffd | 2021-04-21 14:17:34 -0500 | [diff] [blame] | 175 |   ubinfo=$(ubinfo -d "${pnor}") | 
| Adriana Kobylak | 10e915a | 2019-05-31 13:25:36 -0500 | [diff] [blame] | 176 |   presentVolumes=${ubinfo##*:} | 
 | 177 |   IFS=', ' read -r -a array <<< "$presentVolumes" | 
| Adriana Kobylak | bee8ffd | 2021-04-21 14:17:34 -0500 | [diff] [blame] | 178 |   for element in "${array[@]}"; | 
| Adriana Kobylak | 10e915a | 2019-05-31 13:25:36 -0500 | [diff] [blame] | 179 |   do | 
| Adriana Kobylak | bee8ffd | 2021-04-21 14:17:34 -0500 | [diff] [blame] | 180 |     elementProperties=$(ubinfo -d "$pnor" -n "$element") | 
| Adriana Kobylak | 10e915a | 2019-05-31 13:25:36 -0500 | [diff] [blame] | 181 |     # Get ubi volume name by getting rid of additional properties | 
 | 182 |     name=${elementProperties#*Name:} | 
 | 183 |     name="${name%Character*}" | 
 | 184 |     name="$(echo -e "${name}" | tr -d '[:space:]')" | 
 | 185 |  | 
 | 186 |     if [[ ${name} == pnor-prsv ]] || [[ ${name} == pnor-rw* ]] || [[ ${name} == pnor-ro* ]]; then | 
 | 187 |       mountdir="/media/${name}" | 
 | 188 |       if [ ! -d "${mountdir}" ]; then | 
 | 189 |         mkdir -p "${mountdir}" | 
 | 190 |       fi | 
 | 191 |  | 
 | 192 |       if [[ ${name} == pnor-ro* ]] | 
 | 193 |       then | 
| Adriana Kobylak | bee8ffd | 2021-04-21 14:17:34 -0500 | [diff] [blame] | 194 |         ubiblock --create "/dev/ubi${pnor}_${element}" | 
| Adriana Kobylak | 10e915a | 2019-05-31 13:25:36 -0500 | [diff] [blame] | 195 |         mount -t squashfs -o ro "/dev/ubiblock${pnor}_${element}" "${mountdir}" | 
 | 196 |       else | 
 | 197 |         mount -t ubifs "ubi${pnor}:${name}" "${mountdir}" | 
 | 198 |       fi | 
 | 199 |     fi | 
 | 200 |   done | 
 | 201 | } | 
 | 202 |  | 
| Adriana Kobylak | 10e915a | 2019-05-31 13:25:36 -0500 | [diff] [blame] | 203 | ubi_cleanup() { | 
 | 204 |     # When ubi_cleanup is run, it expects one or no active version. | 
 | 205 |     activeVersion=$(busctl --list --no-pager tree \ | 
 | 206 |             org.open_power.Software.Host.Updater | \ | 
 | 207 |             grep /xyz/openbmc_project/software/ | tail -c 9) | 
 | 208 |  | 
 | 209 |     if [[ -z "$activeVersion" ]]; then | 
 | 210 |         vols=$(ubinfo -a | grep -e "pnor-ro-" -e "pnor-rw-" | cut -c 14-) | 
| Adriana Kobylak | bee8ffd | 2021-04-21 14:17:34 -0500 | [diff] [blame] | 211 |         mapfile -t array <<< "${vols}" | 
| Adriana Kobylak | 10e915a | 2019-05-31 13:25:36 -0500 | [diff] [blame] | 212 |     else | 
 | 213 |         vols=$(ubinfo -a | grep -e "pnor-ro-" -e "pnor-rw-" | \ | 
 | 214 |                 grep -v "$activeVersion" | cut -c 14-) | 
| Adriana Kobylak | bee8ffd | 2021-04-21 14:17:34 -0500 | [diff] [blame] | 215 |         mapfile -t array <<< "${vols}" | 
| Adriana Kobylak | 10e915a | 2019-05-31 13:25:36 -0500 | [diff] [blame] | 216 |     fi | 
 | 217 |  | 
| Adriana Kobylak | bee8ffd | 2021-04-21 14:17:34 -0500 | [diff] [blame] | 218 |     for (( index=0; index<${#array[@]}; index++ )); do | 
 | 219 |          name=${array[index]} | 
| Adriana Kobylak | 10e915a | 2019-05-31 13:25:36 -0500 | [diff] [blame] | 220 |          umount_ubi | 
 | 221 |     done | 
 | 222 | } | 
 | 223 |  | 
| Adriana Kobylak | 10e915a | 2019-05-31 13:25:36 -0500 | [diff] [blame] | 224 | case "$1" in | 
 | 225 |   ubiattach) | 
 | 226 |     attach_ubi | 
 | 227 |     ;; | 
 | 228 |   squashfsmount) | 
 | 229 |     name="$2" | 
 | 230 |     version="$3" | 
 | 231 |     mount_squashfs | 
 | 232 |     ;; | 
 | 233 |   ubimount) | 
 | 234 |     name="$2" | 
 | 235 |     mount_ubi | 
 | 236 |     ;; | 
 | 237 |   ubiumount) | 
 | 238 |     name="$2" | 
 | 239 |     umount_ubi | 
 | 240 |     ;; | 
 | 241 |   ubiremount) | 
 | 242 |     remount_ubi | 
 | 243 |     ;; | 
| Adriana Kobylak | 10e915a | 2019-05-31 13:25:36 -0500 | [diff] [blame] | 244 |   ubicleanup) | 
 | 245 |     ubi_cleanup | 
 | 246 |     ;; | 
| Adriana Kobylak | 10e915a | 2019-05-31 13:25:36 -0500 | [diff] [blame] | 247 |   *) | 
 | 248 |     echo "Invalid argument" | 
 | 249 |     exit 1 | 
 | 250 |     ;; | 
 | 251 | esac | 
 | 252 | rc=$? | 
 | 253 | if [ ${rc} -ne 0 ]; then | 
 | 254 |   echo "$0: error ${rc}" | 
 | 255 |   exit ${rc} | 
 | 256 | fi |