| Adriana Kobylak | e882f91 | 2017-07-06 10:31:59 -0500 | [diff] [blame] | 1 | #!/bin/sh | 
|  | 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 |  | 
| Adriana Kobylak | f15e77c | 2017-07-06 11:26:42 -0500 | [diff] [blame] | 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 |  | 
| Saqib Khan | ce61193 | 2017-08-01 09:46:22 -0500 | [diff] [blame] | 25 | # Attach the pnor mtd device to ubi. | 
| Adriana Kobylak | e882f91 | 2017-07-06 10:31:59 -0500 | [diff] [blame] | 26 | attach_ubi() { | 
|  | 27 | pnormtd="$(findmtd pnor)" | 
|  | 28 | pnor="${pnormtd#mtd}" | 
|  | 29 | pnordev="/dev/mtd${pnor}" | 
|  | 30 |  | 
| Saqib Khan | 4f609b8 | 2017-08-02 15:13:26 -0500 | [diff] [blame] | 31 | if [ -d "/sys/class/ubi/ubi${pnor}" ]; then | 
|  | 32 | # Already attached | 
|  | 33 | return 0 | 
|  | 34 | fi | 
|  | 35 |  | 
| Adriana Kobylak | e882f91 | 2017-07-06 10:31:59 -0500 | [diff] [blame] | 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' | 
|  | 41 | magic="$(hexdump -C -n 3 ${pnordev})" | 
|  | 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 |  | 
| Adriana Kobylak | f15e77c | 2017-07-06 11:26:42 -0500 | [diff] [blame] | 55 | mount_squashfs() { | 
|  | 56 | pnormtd="$(findmtd pnor)" | 
| Michael Tritz | 45e55f8 | 2017-08-08 13:35:17 -0500 | [diff] [blame^] | 57 | ubidev="/dev/ubi${pnormtd#mtd}" | 
| Adriana Kobylak | f15e77c | 2017-07-06 11:26:42 -0500 | [diff] [blame] | 58 | mountdir="/media/${name}" | 
| Michael Tritz | 45e55f8 | 2017-08-08 13:35:17 -0500 | [diff] [blame^] | 59 | vol="$(findubi "${name}")" | 
|  | 60 |  | 
|  | 61 | if is_mounted "${name}"; then | 
|  | 62 | echo "${name} is already mounted." | 
|  | 63 | return 0 | 
|  | 64 | fi | 
|  | 65 |  | 
|  | 66 | if [ ! -z "${vol}" ]; then | 
|  | 67 | ubirmvol "${ubidev}" -N "${name}" | 
|  | 68 | fi | 
| Adriana Kobylak | f15e77c | 2017-07-06 11:26:42 -0500 | [diff] [blame] | 69 |  | 
|  | 70 | if [ ! -d "${mountdir}" ]; then | 
|  | 71 | mkdir "${mountdir}" | 
|  | 72 | fi | 
|  | 73 |  | 
| Adriana Kobylak | d316559 | 2017-07-18 12:24:24 -0500 | [diff] [blame] | 74 | # Create a static ubi volume of arbitrary size 24MB, | 
|  | 75 | # the current pnor image is ~19MB | 
|  | 76 | # TODO Set size based on file size openbmc/openbmc#1840 | 
| Michael Tritz | 45e55f8 | 2017-08-08 13:35:17 -0500 | [diff] [blame^] | 77 | ubimkvol "${ubidev}" -N "${name}" -s 24MiB --type=static | 
| Adriana Kobylak | f15e77c | 2017-07-06 11:26:42 -0500 | [diff] [blame] | 78 | vol="$(findubi "${name}")" | 
| Michael Tritz | 45e55f8 | 2017-08-08 13:35:17 -0500 | [diff] [blame^] | 79 |  | 
|  | 80 | if [ $? != 0 ]; then | 
|  | 81 | echo "Unable to create RO volume!" | 
|  | 82 | return 1 | 
| Adriana Kobylak | f15e77c | 2017-07-06 11:26:42 -0500 | [diff] [blame] | 83 | fi | 
|  | 84 |  | 
| Adriana Kobylak | f15e77c | 2017-07-06 11:26:42 -0500 | [diff] [blame] | 85 | ubidevid="${vol#ubi}" | 
| Michael Tritz | 45e55f8 | 2017-08-08 13:35:17 -0500 | [diff] [blame^] | 86 | img="/tmp/images/${version}/pnor.xz.squashfs" | 
|  | 87 | ubiupdatevol "/dev/ubi${ubidevid}" "${img}" | 
|  | 88 |  | 
|  | 89 | if [ $? != 0 ]; then | 
|  | 90 | echo "Unable to update RO volume!" | 
|  | 91 | return 1 | 
| Adriana Kobylak | f15e77c | 2017-07-06 11:26:42 -0500 | [diff] [blame] | 92 | fi | 
|  | 93 |  | 
| Michael Tritz | 45e55f8 | 2017-08-08 13:35:17 -0500 | [diff] [blame^] | 94 | ubiblock --create "/dev/ubi${ubidevid}" | 
|  | 95 |  | 
|  | 96 | if [ $? != 0 ]; then | 
|  | 97 | echo "Unable to create UBI block for RO volume!" | 
|  | 98 | return 1 | 
|  | 99 | fi | 
|  | 100 |  | 
|  | 101 | mount -t squashfs -o ro "/dev/ubiblock${ubidevid}" "${mountdir}" | 
|  | 102 |  | 
|  | 103 | if [ $? != 0 ]; then | 
|  | 104 | echo "Unable to mount RO volume!" | 
|  | 105 | return 1 | 
| Adriana Kobylak | f15e77c | 2017-07-06 11:26:42 -0500 | [diff] [blame] | 106 | fi | 
|  | 107 | } | 
|  | 108 |  | 
| Adriana Kobylak | 6aff574 | 2017-07-06 11:29:46 -0500 | [diff] [blame] | 109 | mount_ubi() { | 
|  | 110 | pnormtd="$(findmtd pnor)" | 
|  | 111 | pnor="${pnormtd#mtd}" | 
|  | 112 | ubidev="/dev/ubi${pnor}" | 
| Adriana Kobylak | f287af3 | 2017-07-12 13:52:22 -0500 | [diff] [blame] | 113 |  | 
|  | 114 | if [[ "${name}" == "pnor-patch" ]]; then | 
|  | 115 | mountdir="/usr/local/share/pnor" | 
|  | 116 | else | 
|  | 117 | mountdir="/media/${name}" | 
|  | 118 | fi | 
| Adriana Kobylak | 6aff574 | 2017-07-06 11:29:46 -0500 | [diff] [blame] | 119 |  | 
| Adriana Kobylak | d316559 | 2017-07-18 12:24:24 -0500 | [diff] [blame] | 120 | if [[ "${name}" == "pnor-prsv" ]]; then | 
|  | 121 | size="2MiB" | 
|  | 122 | else | 
|  | 123 | size="16MiB" | 
|  | 124 | fi | 
|  | 125 |  | 
| Adriana Kobylak | 6aff574 | 2017-07-06 11:29:46 -0500 | [diff] [blame] | 126 | if [ ! -d "${mountdir}" ]; then | 
| Adriana Kobylak | f287af3 | 2017-07-12 13:52:22 -0500 | [diff] [blame] | 127 | mkdir -p "${mountdir}" | 
| Adriana Kobylak | 6aff574 | 2017-07-06 11:29:46 -0500 | [diff] [blame] | 128 | fi | 
|  | 129 |  | 
| Adriana Kobylak | 6aff574 | 2017-07-06 11:29:46 -0500 | [diff] [blame] | 130 | vol="$(findubi "${name}")" | 
|  | 131 | if [ -z "${vol}" ]; then | 
| Adriana Kobylak | d316559 | 2017-07-18 12:24:24 -0500 | [diff] [blame] | 132 | ubimkvol "${ubidev}" -N "${name}" -s "${size}" | 
| Adriana Kobylak | 6aff574 | 2017-07-06 11:29:46 -0500 | [diff] [blame] | 133 | fi | 
|  | 134 |  | 
|  | 135 | if ! is_mounted "${name}"; then | 
|  | 136 | mountdev="ubi${pnor}:${name}" | 
|  | 137 | mount -t ubifs "${mountdev}" "${mountdir}" | 
|  | 138 | fi | 
|  | 139 | } | 
|  | 140 |  | 
| Adriana Kobylak | e5764af | 2017-06-07 15:24:52 -0500 | [diff] [blame] | 141 | umount_ubi() { | 
|  | 142 | pnormtd="$(findmtd pnor)" | 
|  | 143 | pnor="${pnormtd#mtd}" | 
|  | 144 | ubidev="/dev/ubi${pnor}" | 
|  | 145 | mountdir="/media/${name}" | 
|  | 146 |  | 
|  | 147 | if is_mounted "${name}"; then | 
|  | 148 | umount "${mountdir}" | 
|  | 149 | fi | 
|  | 150 |  | 
|  | 151 | vol="$(findubi "${name}")" | 
|  | 152 | if [ -n "${vol}" ]; then | 
|  | 153 | ubirmvol "${ubidev}" -N "${name}" | 
|  | 154 | fi | 
|  | 155 |  | 
|  | 156 | if [ -d "${mountdir}" ]; then | 
|  | 157 | rm -r "${mountdir}" | 
|  | 158 | fi | 
|  | 159 | } | 
|  | 160 |  | 
| Saqib Khan | 58c870d | 2017-07-26 23:48:20 -0500 | [diff] [blame] | 161 | remount_ubi() { | 
|  | 162 | pnormtd="$(findmtd pnor)" | 
|  | 163 | pnor="${pnormtd#mtd}" | 
| Saqib Khan | ce61193 | 2017-08-01 09:46:22 -0500 | [diff] [blame] | 164 | pnordev="/dev/mtd${pnor}" | 
|  | 165 |  | 
|  | 166 | # Re-Attach the pnor mtd device to ubi | 
| Saqib Khan | 7bfc76f | 2017-08-07 12:51:48 -0500 | [diff] [blame] | 167 | if [[ $(hexdump -C -n 3 ${pnordev}) =~ "UBI" ]]; then | 
|  | 168 | ubiattach /dev/ubi_ctrl -m "${pnor}" -d "${pnor}" | 
|  | 169 | else | 
|  | 170 | # Device not formatted as ubi. | 
|  | 171 | return 0 | 
| Saqib Khan | ce61193 | 2017-08-01 09:46:22 -0500 | [diff] [blame] | 172 | fi | 
|  | 173 |  | 
| Saqib Khan | 58c870d | 2017-07-26 23:48:20 -0500 | [diff] [blame] | 174 | # Get information on all ubi volumes | 
|  | 175 | ubinfo=$(ubinfo -d ${pnor}) | 
|  | 176 | presentVolumes=${ubinfo##*:} | 
|  | 177 | IFS=', ' read -r -a array <<< "$presentVolumes" | 
|  | 178 | for element in ${array[@]}; | 
|  | 179 | do | 
|  | 180 | elementProperties=$(ubinfo -d $pnor -n $element) | 
|  | 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}" | 
| Saqib Khan | 76b7b83 | 2017-08-01 11:32:27 -0500 | [diff] [blame] | 188 | if [ ! -d "${mountdir}" ]; then | 
|  | 189 | mkdir -p "${mountdir}" | 
|  | 190 | fi | 
|  | 191 |  | 
| Saqib Khan | 58c870d | 2017-07-26 23:48:20 -0500 | [diff] [blame] | 192 | if [[ ${name} == pnor-ro* ]] | 
|  | 193 | then | 
|  | 194 | ubiblock --create /dev/ubi${pnor}_${element} | 
|  | 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 |  | 
| Saqib Khan | d536f0d | 2017-08-02 12:48:12 -0500 | [diff] [blame] | 203 | update_symlinks() { | 
|  | 204 | PNOR_ACTIVE_PATH="/var/lib/phosphor-software-manager/pnor/" | 
|  | 205 | PNOR_RO_ACTIVE_PATH="/var/lib/phosphor-software-manager/pnor/ro" | 
|  | 206 | PNOR_RO_PREFIX="/media/pnor-ro-" | 
|  | 207 | PNOR_RW_ACTIVE_PATH="/var/lib/phosphor-software-manager/pnor/rw" | 
|  | 208 | PNOR_RW_PREFIX="/media/pnor-rw-" | 
|  | 209 | PNOR_PRSV_ACTIVE_PATH="/var/lib/phosphor-software-manager/pnor/prsv" | 
|  | 210 | PNOR_PRSV="/media/pnor-prsv" | 
|  | 211 |  | 
|  | 212 | # Get the current PNOR version using the priorities stored in | 
|  | 213 | # the persistence storage files. | 
|  | 214 | persistencePath="/var/lib/obmc/openpower-pnor-code-mgmt/" | 
|  | 215 | data="$(grep -r "priority" ${persistencePath})" | 
|  | 216 | if [[ -z "$data" ]]; then | 
|  | 217 | return 0; | 
|  | 218 | fi | 
|  | 219 | IFS=$'\n' array=(${data}) | 
|  | 220 | for element in ${array[@]}; | 
|  | 221 | do | 
|  | 222 | element="${element#$persistencePath}" | 
|  | 223 | version="$( cut -d ':' -f 1 <<< "$element" )"; | 
|  | 224 | priority="${element: -1}" | 
|  | 225 |  | 
|  | 226 | if [[ priority -le lowestPriority  ]]; then | 
|  | 227 | lowestPriority=${priority} | 
|  | 228 | currentVersion=${version} | 
|  | 229 | fi | 
|  | 230 | done | 
|  | 231 |  | 
|  | 232 | if [ ! -d "${PNOR_ACTIVE_PATH}" ]; then | 
|  | 233 | mkdir -p "${PNOR_ACTIVE_PATH}" | 
|  | 234 | fi | 
|  | 235 |  | 
|  | 236 | # If the RW or RO active links doesn't point to the version with | 
|  | 237 | # lowest priority, then remove the symlink and create new ones. | 
|  | 238 | if [[ $(readlink -f "${PNOR_RO_ACTIVE_PATH}") != ${PNOR_RO_PREFIX}${currentVersion}  ]]; then | 
|  | 239 | rm -f ${PNOR_RO_ACTIVE_PATH} | 
|  | 240 | ln -sfv ${PNOR_RO_PREFIX}${currentVersion} ${PNOR_RO_ACTIVE_PATH} | 
|  | 241 | fi | 
|  | 242 |  | 
|  | 243 | if [[ $(readlink -f "${PNOR_RW_ACTIVE_PATH}") != ${PNOR_RW_PREFIX}${currentVersion}  ]]; then | 
|  | 244 | rm -f ${PNOR_RW_ACTIVE_PATH} | 
|  | 245 | ln -sfv ${PNOR_RW_PREFIX}${currentVersion} ${PNOR_RW_ACTIVE_PATH} | 
|  | 246 | fi | 
|  | 247 |  | 
|  | 248 | if [[ ! -h ${PNOR_PRSV_ACTIVE_PATH}  ]]; then | 
|  | 249 | ln -sfv ${PNOR_PRSV} ${PNOR_PRSV_ACTIVE_PATH} | 
|  | 250 | fi | 
|  | 251 | } | 
|  | 252 |  | 
| Adriana Kobylak | e882f91 | 2017-07-06 10:31:59 -0500 | [diff] [blame] | 253 | case "$1" in | 
|  | 254 | ubiattach) | 
|  | 255 | attach_ubi | 
|  | 256 | ;; | 
| Adriana Kobylak | f15e77c | 2017-07-06 11:26:42 -0500 | [diff] [blame] | 257 | squashfsmount) | 
|  | 258 | name="$2" | 
|  | 259 | version="$3" | 
|  | 260 | mount_squashfs | 
|  | 261 | ;; | 
| Adriana Kobylak | 6aff574 | 2017-07-06 11:29:46 -0500 | [diff] [blame] | 262 | ubimount) | 
|  | 263 | name="$2" | 
|  | 264 | mount_ubi | 
|  | 265 | ;; | 
| Adriana Kobylak | e5764af | 2017-06-07 15:24:52 -0500 | [diff] [blame] | 266 | ubiumount) | 
|  | 267 | name="$2" | 
|  | 268 | umount_ubi | 
|  | 269 | ;; | 
| Saqib Khan | 58c870d | 2017-07-26 23:48:20 -0500 | [diff] [blame] | 270 | ubiremount) | 
|  | 271 | remount_ubi | 
|  | 272 | ;; | 
| Saqib Khan | d536f0d | 2017-08-02 12:48:12 -0500 | [diff] [blame] | 273 | updatesymlinks) | 
|  | 274 | update_symlinks | 
|  | 275 | ;; | 
| Adriana Kobylak | e882f91 | 2017-07-06 10:31:59 -0500 | [diff] [blame] | 276 | *) | 
|  | 277 | echo "Invalid argument" | 
|  | 278 | exit 1 | 
|  | 279 | ;; | 
|  | 280 | esac | 
|  | 281 | rc=$? | 
|  | 282 | if [ ${rc} -ne 0 ]; then | 
|  | 283 | echo "$0: error ${rc}" | 
|  | 284 | exit ${rc} | 
|  | 285 | fi |