blob: 5c960f3b85dcbbdace7b5f63e4d1e83171ffcfe6 [file] [log] [blame]
Adriana Kobylaka290eff2017-06-27 09:39:57 -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# Get the ubi device number (ubiX_Y)
12findubi() {
13 u="$(grep -xl "$1" /sys/class/ubi/ubi?/subsystem/ubi*/name)"
14 u="${u%/name}"
15 u="${u##*/}"
16 echo "${u}"
17}
18
19ubi_rw() {
20 rwmtd="$(findmtd "${reqmtd}")"
21 rw="${rwmtd#mtd}"
22 ubidev="/dev/ubi${rw}"
23
Michael Tritza694eed2017-07-13 16:26:15 -050024 if [ "$(fw_printenv rwreset)" == "rwreset=true" ]; then
25 ubi_remove
26 fw_setenv rwreset
27 fi
28
Adriana Kobylaka290eff2017-06-27 09:39:57 -050029 # Create a ubi volume of size 4MB, that is the current size of the rwfs image
30 vol="$(findubi "${name}")"
31 if [ -z "${vol}" ]; then
32 ubimkvol "${ubidev}" -N "${name}" -s 4MiB
33 fi
34}
35
Adriana Kobylake9939492017-07-11 11:34:23 -050036ubi_ro() {
37 romtd="$(findmtd "${reqmtd}")"
38 ro="${romtd#mtd}"
39 ubidev="/dev/ubi${ro}"
40
41 # Create a static ubi volume
42 # TODO Get the actual image size openbmc/openbmc#1840
43 vol="$(findubi "${name}")"
44 if [ -z "${vol}" ]; then
45 ubimkvol "${ubidev}" -N "${name}" -s "${imgsize}" --type=static
46 vol="$(findubi "${name}")"
47 fi
48}
49
50# Squashfs images need a ubi block
51ubi_block() {
52 vol="$(findubi "${name}")"
53 ubidevid="${vol#ubi}"
54 block="/dev/ubiblock${ubidevid}"
55 if [ ! -e "$block" ]; then
56 ubiblock --create "/dev/ubi${ubidevid}"
57 fi
58}
59
60ubi_updatevol() {
61 vol="$(findubi "${name}")"
62 ubidevid="${vol#ubi}"
63 img="/tmp/images/${version}/${imgfile}"
64 ubiupdatevol "/dev/ubi${ubidevid}" "${img}"
65}
66
Michael Tritza694eed2017-07-13 16:26:15 -050067ubi_remove() {
68 vol="$(findubi "${name}")"
69
70 if [ ! -z "$vol" ]; then
71 vol="${vol%_*}"
72
73 if grep -q "${vol}:$name" /proc/mounts; then
74 mountdir=$(grep "${vol}:$name" /proc/mounts | cut -d " " -f 2)
75 umount "$mountdir"
76 rm -r "$mountdir"
77 fi
78
79 ubirmvol "/dev/${vol}" -N "$name"
80 fi
81}
82
83ubi_setenv() {
84 varName="${variable%"\x3d"*}"
85 value="${variable##*"\x3d"}"
86 fw_setenv "$varName" "$value"
87}
88
Adriana Kobylaka290eff2017-06-27 09:39:57 -050089case "$1" in
90 ubirw)
91 reqmtd="$2"
92 name="$3"
93 ubi_rw
94 ;;
Adriana Kobylake9939492017-07-11 11:34:23 -050095 ubiro)
96 reqmtd="$2"
97 name="$3"
98 version="$4"
99 imgfile="image-rofs"
100 imgsize="16MiB"
101 ubi_ro
102 ubi_block
103 ubi_updatevol
104 ;;
105 ubikernel)
106 reqmtd="$2"
107 name="$3"
108 version="$4"
109 imgfile="image-kernel"
110 imgsize="4MiB"
111 ubi_ro
112 ubi_updatevol
113 ;;
Michael Tritza694eed2017-07-13 16:26:15 -0500114 ubiremove)
115 name="$2"
116 ubi_remove
117 ;;
118 ubisetenv)
119 variable="$2"
120 ubi_setenv
121 ;;
Adriana Kobylaka290eff2017-06-27 09:39:57 -0500122 *)
123 echo "Invalid argument"
124 exit 1
125 ;;
126esac
127rc=$?
128if [ ${rc} -ne 0 ]; then
129 echo "$0: error ${rc}"
130 exit ${rc}
131fi