blob: acc550ba42134bdb6e6b981ce42c1968fe313171 [file] [log] [blame]
Adriana Kobylake882f912017-07-06 10:31:59 -05001#!/bin/sh
2
3# Get the mtd device number (mtdX)
4findmtd() {
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
12attach_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
36case "$1" in
37 ubiattach)
38 attach_ubi
39 ;;
40 *)
41 echo "Invalid argument"
42 exit 1
43 ;;
44esac
45rc=$?
46if [ ${rc} -ne 0 ]; then
47 echo "$0: error ${rc}"
48 exit ${rc}
49fi