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 | |
| 11 | # Attach the pnor mtd device to ubi |
| 12 | attach_ubi() { |
| 13 | pnormtd="$(findmtd pnor)" |
| 14 | pnor="${pnormtd#mtd}" |
| 15 | pnordev="/dev/mtd${pnor}" |
| 16 | |
| 17 | ubiattach /dev/ubi_ctrl -m "${pnor}" -d "${pnor}" |
| 18 | rc=$? |
| 19 | if [ ${rc} -ne 0 ]; then |
| 20 | # Check the pnor mtd device is formatted as ubi by reading the first 3 byes, |
| 21 | # which should be the ascii chars 'UBI' |
| 22 | magic="$(hexdump -C -n 3 ${pnordev})" |
| 23 | if [[ "${magic}" =~ "UBI" ]]; then |
| 24 | # Device already formatted as ubi, ubiattach failed for some other reason |
| 25 | return ${rc} |
| 26 | else |
| 27 | # Format device as ubi |
| 28 | echo "Starting ubiformat ${pnordev}" |
| 29 | ubiformat "${pnordev}" -y -q |
| 30 | # Retry the ubiattach |
| 31 | ubiattach /dev/ubi_ctrl -m "${pnor}" -d "${pnor}" |
| 32 | fi |
| 33 | fi |
| 34 | } |
| 35 | |
| 36 | case "$1" in |
| 37 | ubiattach) |
| 38 | attach_ubi |
| 39 | ;; |
| 40 | *) |
| 41 | echo "Invalid argument" |
| 42 | exit 1 |
| 43 | ;; |
| 44 | esac |
| 45 | rc=$? |
| 46 | if [ ${rc} -ne 0 ]; then |
| 47 | echo "$0: error ${rc}" |
| 48 | exit ${rc} |
| 49 | fi |