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)" |
| 57 | pnor="${pnormtd#mtd}" |
| 58 | ubidev="/dev/ubi${pnor}" |
| 59 | mountdir="/media/${name}" |
| 60 | |
| 61 | if [ ! -d "${mountdir}" ]; then |
| 62 | mkdir "${mountdir}" |
| 63 | fi |
| 64 | |
Adriana Kobylak | d316559 | 2017-07-18 12:24:24 -0500 | [diff] [blame] | 65 | # Create a static ubi volume of arbitrary size 24MB, |
| 66 | # the current pnor image is ~19MB |
| 67 | # TODO Set size based on file size openbmc/openbmc#1840 |
Adriana Kobylak | f15e77c | 2017-07-06 11:26:42 -0500 | [diff] [blame] | 68 | vol="$(findubi "${name}")" |
| 69 | if [ -z "${vol}" ]; then |
Adriana Kobylak | d316559 | 2017-07-18 12:24:24 -0500 | [diff] [blame] | 70 | ubimkvol "${ubidev}" -N "${name}" -s 24MiB --type=static |
Adriana Kobylak | f15e77c | 2017-07-06 11:26:42 -0500 | [diff] [blame] | 71 | vol="$(findubi "${name}")" |
| 72 | fi |
| 73 | |
| 74 | # Create a ubi block needed for read-only volumes, |
| 75 | # and update the volume with the pnor squashfs image |
| 76 | ubidevid="${vol#ubi}" |
| 77 | block="/dev/ubiblock${ubidevid}" |
| 78 | if [ ! -e "$block" ]; then |
| 79 | img="/tmp/images/${version}/pnor.xz.squashfs" |
| 80 | ubiblock --create "/dev/ubi${ubidevid}" |
| 81 | ubiupdatevol "/dev/ubi${ubidevid}" "${img}" |
| 82 | fi |
| 83 | |
| 84 | if ! is_mounted "${name}"; then |
| 85 | mount -t squashfs -o ro "${block}" "${mountdir}" |
| 86 | fi |
| 87 | } |
| 88 | |
Adriana Kobylak | 6aff574 | 2017-07-06 11:29:46 -0500 | [diff] [blame] | 89 | mount_ubi() { |
| 90 | pnormtd="$(findmtd pnor)" |
| 91 | pnor="${pnormtd#mtd}" |
| 92 | ubidev="/dev/ubi${pnor}" |
Adriana Kobylak | f287af3 | 2017-07-12 13:52:22 -0500 | [diff] [blame] | 93 | |
| 94 | if [[ "${name}" == "pnor-patch" ]]; then |
| 95 | mountdir="/usr/local/share/pnor" |
| 96 | else |
| 97 | mountdir="/media/${name}" |
| 98 | fi |
Adriana Kobylak | 6aff574 | 2017-07-06 11:29:46 -0500 | [diff] [blame] | 99 | |
Adriana Kobylak | d316559 | 2017-07-18 12:24:24 -0500 | [diff] [blame] | 100 | if [[ "${name}" == "pnor-prsv" ]]; then |
| 101 | size="2MiB" |
| 102 | else |
| 103 | size="16MiB" |
| 104 | fi |
| 105 | |
Adriana Kobylak | 6aff574 | 2017-07-06 11:29:46 -0500 | [diff] [blame] | 106 | if [ ! -d "${mountdir}" ]; then |
Adriana Kobylak | f287af3 | 2017-07-12 13:52:22 -0500 | [diff] [blame] | 107 | mkdir -p "${mountdir}" |
Adriana Kobylak | 6aff574 | 2017-07-06 11:29:46 -0500 | [diff] [blame] | 108 | fi |
| 109 | |
Adriana Kobylak | 6aff574 | 2017-07-06 11:29:46 -0500 | [diff] [blame] | 110 | vol="$(findubi "${name}")" |
| 111 | if [ -z "${vol}" ]; then |
Adriana Kobylak | d316559 | 2017-07-18 12:24:24 -0500 | [diff] [blame] | 112 | ubimkvol "${ubidev}" -N "${name}" -s "${size}" |
Adriana Kobylak | 6aff574 | 2017-07-06 11:29:46 -0500 | [diff] [blame] | 113 | fi |
| 114 | |
| 115 | if ! is_mounted "${name}"; then |
| 116 | mountdev="ubi${pnor}:${name}" |
| 117 | mount -t ubifs "${mountdev}" "${mountdir}" |
| 118 | fi |
| 119 | } |
| 120 | |
Adriana Kobylak | e5764af | 2017-06-07 15:24:52 -0500 | [diff] [blame] | 121 | umount_ubi() { |
| 122 | pnormtd="$(findmtd pnor)" |
| 123 | pnor="${pnormtd#mtd}" |
| 124 | ubidev="/dev/ubi${pnor}" |
| 125 | mountdir="/media/${name}" |
| 126 | |
| 127 | if is_mounted "${name}"; then |
| 128 | umount "${mountdir}" |
| 129 | fi |
| 130 | |
| 131 | vol="$(findubi "${name}")" |
| 132 | if [ -n "${vol}" ]; then |
| 133 | ubirmvol "${ubidev}" -N "${name}" |
| 134 | fi |
| 135 | |
| 136 | if [ -d "${mountdir}" ]; then |
| 137 | rm -r "${mountdir}" |
| 138 | fi |
| 139 | } |
| 140 | |
Saqib Khan | 58c870d | 2017-07-26 23:48:20 -0500 | [diff] [blame] | 141 | remount_ubi() { |
| 142 | pnormtd="$(findmtd pnor)" |
| 143 | pnor="${pnormtd#mtd}" |
Saqib Khan | ce61193 | 2017-08-01 09:46:22 -0500 | [diff] [blame] | 144 | pnordev="/dev/mtd${pnor}" |
| 145 | |
| 146 | # Re-Attach the pnor mtd device to ubi |
| 147 | ubiattach /dev/ubi_ctrl -m "${pnor}" -d "${pnor}" |
| 148 | rc=$? |
| 149 | if [ ${rc} -ne 0 ]; then |
| 150 | # Check the pnor mtd device is formatted as ubi by reading |
| 151 | # the first 3 byes, which should be the ascii chars 'UBI' |
| 152 | magic="$(hexdump -C -n 3 ${pnordev})" |
| 153 | if [[ ! "${magic}" =~ "UBI" ]]; then |
| 154 | # Device not formatted as ubi. |
| 155 | return |
| 156 | fi |
| 157 | fi |
| 158 | |
Saqib Khan | 58c870d | 2017-07-26 23:48:20 -0500 | [diff] [blame] | 159 | # Get information on all ubi volumes |
| 160 | ubinfo=$(ubinfo -d ${pnor}) |
| 161 | presentVolumes=${ubinfo##*:} |
| 162 | IFS=', ' read -r -a array <<< "$presentVolumes" |
| 163 | for element in ${array[@]}; |
| 164 | do |
| 165 | elementProperties=$(ubinfo -d $pnor -n $element) |
| 166 | # Get ubi volume name by getting rid of additional properties |
| 167 | name=${elementProperties#*Name:} |
| 168 | name="${name%Character*}" |
| 169 | name="$(echo -e "${name}" | tr -d '[:space:]')" |
| 170 | |
| 171 | if [[ ${name} == pnor-prsv ]] || [[ ${name} == pnor-rw* ]] || [[ ${name} == pnor-ro* ]]; then |
| 172 | mountdir="/media/${name}" |
Saqib Khan | 76b7b83 | 2017-08-01 11:32:27 -0500 | [diff] [blame] | 173 | if [ ! -d "${mountdir}" ]; then |
| 174 | mkdir -p "${mountdir}" |
| 175 | fi |
| 176 | |
Saqib Khan | 58c870d | 2017-07-26 23:48:20 -0500 | [diff] [blame] | 177 | if [[ ${name} == pnor-ro* ]] |
| 178 | then |
| 179 | ubiblock --create /dev/ubi${pnor}_${element} |
| 180 | mount -t squashfs -o ro "/dev/ubiblock${pnor}_${element}" "${mountdir}" |
| 181 | else |
| 182 | mount -t ubifs "ubi${pnor}:${name}" "${mountdir}" |
| 183 | fi |
| 184 | fi |
| 185 | done |
| 186 | } |
| 187 | |
Adriana Kobylak | e882f91 | 2017-07-06 10:31:59 -0500 | [diff] [blame] | 188 | case "$1" in |
| 189 | ubiattach) |
| 190 | attach_ubi |
| 191 | ;; |
Adriana Kobylak | f15e77c | 2017-07-06 11:26:42 -0500 | [diff] [blame] | 192 | squashfsmount) |
| 193 | name="$2" |
| 194 | version="$3" |
| 195 | mount_squashfs |
| 196 | ;; |
Adriana Kobylak | 6aff574 | 2017-07-06 11:29:46 -0500 | [diff] [blame] | 197 | ubimount) |
| 198 | name="$2" |
| 199 | mount_ubi |
| 200 | ;; |
Adriana Kobylak | e5764af | 2017-06-07 15:24:52 -0500 | [diff] [blame] | 201 | ubiumount) |
| 202 | name="$2" |
| 203 | umount_ubi |
| 204 | ;; |
Saqib Khan | 58c870d | 2017-07-26 23:48:20 -0500 | [diff] [blame] | 205 | ubiremount) |
| 206 | remount_ubi |
| 207 | ;; |
Adriana Kobylak | e882f91 | 2017-07-06 10:31:59 -0500 | [diff] [blame] | 208 | *) |
| 209 | echo "Invalid argument" |
| 210 | exit 1 |
| 211 | ;; |
| 212 | esac |
| 213 | rc=$? |
| 214 | if [ ${rc} -ne 0 ]; then |
| 215 | echo "$0: error ${rc}" |
| 216 | exit ${rc} |
| 217 | fi |