summaryrefslogtreecommitdiffstats
path: root/meta-phosphor/common/recipes-phosphor/obmc-phosphor-initfs/files/obmc-update.sh
blob: 9d3c81276acbe7f3d77a9f9fc09b2ff279e900bd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
#!/bin/sh

echo update: "$@"

echoerr() {
	echo 1>&2 "ERROR: $@"
}

cd /
if ! test -r /proc/mounts || ! test -f /proc/mounts
then
	mkdir -p /proc
	mount -t proc proc proc
fi
if ! test -d /sys/class
then
	mkdir -p /sys
	mount -t sysfs sys sys
fi
if ! test -c /dev/null
then
	mkdir -p /dev
	mount -t devtmpfs dev dev
fi

# mtd number N with mtd name Name can be mounted via mtdN, or mtd:Name
# (with a mtd aware fs) or by /dev/mtdblockN (with a mtd or block fs).
mtdismounted() {
	m=${1##mtd}
	if grep -s "mtdblock$m " /proc/mounts || grep -s "mtd$m " /proc/mounts
	then
		return 0
	fi
	n=$(cat /sys/class/mtd/mtd$m/name)
	if test -n "$n" && grep -s "mtd:$n " /proc/mounts
	then
		return 0
	fi
	return 1
}

# Detect child partitions when the whole flash is to be updated.
# Ignore mtdNro and mtdblockN names in the class subsystem directory.
childmtds() {
	for m in /sys/class/mtd/$1/mtd*
	do
		m=${m##*/}
		if test "${m%ro}" = "${m#mtdblock}"
		then
			echo $m
		fi
	done
}

findmtd() {
	m=$(grep -xl "$1" /sys/class/mtd/*/name)
	m=${m%/name}
	m=${m##*/}
	echo $m
}

blkid_fs_type() {
	# Emulate util-linux's `blkid -s TYPE -o value $1`
	# Example busybox blkid output:
	#    # blkid /dev/mtdblock5
	#    /dev/mtdblock5: TYPE="squashfs"
	# Process output to extract TYPE value "squashfs".
	blkid $1 | sed -e 's/^.*TYPE="//' -e 's/".*$//'
}

probe_fs_type() {
	fst=$(blkid_fs_type $1)
	echo ${fst:=jffs2}
}

rwfs=$(findmtd rwfs)

rwdev=/dev/mtdblock${rwfs#mtd}
rwopts=rw
rorwopts=ro${rwopts#rw}

rwdir=/run/initramfs/rw
upper=$rwdir/cow
save=/run/save/${upper##*/}

mounted=
doflash=y
doclean=
dosave=y
dorestore=y
toram=
checkmount=y

whitelist=/run/initramfs/whitelist
image=/run/initramfs/image-

while test "$1" != "${1#-}"
do
	case "$1" in
	--no-clean-saved-files)
		doclean=
		shift ;;
	--clean-saved-files)
		doclean=y
		shift ;;
	--no-save-files)
		dosave=
		shift ;;
	--save-files)
		dosave=y
		shift ;;
	--no-restore-files)
		dorestore=
		shift ;;
	--restore-files)
		dorestore=y
		shift ;;
	--no-flash)
		doflash=
		shift ;;
	--ignore-mount)
		checkmount=
		doflash=
		shift ;;
	--copy-files)
		toram=y
		shift ;;
	*)
		echoerr "Unknown option $1"
		exit 1 ;;
	esac
done

if test "x$dosave" = xy
then
	if test ! -d $upper -a -n "$rwfs"
	then
		mkdir -p $rwdir
		mount $rwdev $rwdir -t $(probe_fs_type $rwdev) -o $rorwopts
		mounted=$rwdir
	fi

	while read f
	do
		if ! test -e $upper/$f
		then
			continue
		fi
		d="$save/$f"
		mkdir -p "${d%/*}"
		cp -rp $upper/$f "${d%/*}/"
	done < $whitelist

	if test -n "$mounted"
	then
		umount $mounted
	fi
fi

for f in $image*
do
	m=$(findmtd ${f#$image})
	if test -z "$m"
	then
		echoerr "Unable to find mtd partiton for ${f##*/}."
		exit 1
	fi
	for s in $m $(childmtds $m)
	do
		if test -n "$checkmount" && mtdismounted $s
		then
			echoerr "Device $s is mounted, ${f##*/} is busy."
			exit 1
		fi
	done
done

if test -n "$doflash"
then
	for f in $image*
	do
		if test ! -s $f
		then
			echo "Skipping empty update of ${f#$image}."
			rm $f
			continue
		fi
		m=$(findmtd ${f#$image})
		echo "Updating ${f#$image}..."
		flashcp -v $f /dev/$m && rm $f
	done
fi

if test -d $save -a "x$toram" = xy
then
	mkdir -p $upper
	cp -rp $save/. $upper/
fi

if test -d $save -a "x$dorestore" = xy
then
	odir=$rwdir
	rwdir=/run/rw
	upper=$rwdir${upper#$odir}

	mkdir -p $rwdir
	mount $rwdev $rwdir -t $(probe_fs_type $rwdev) -o $rwopts
	mkdir -p $upper
	cp -rp $save/. $upper/
	umount $rwdir
	rmdir $rwdir
fi

if test "x$doclean" = xy
then
	rm -rf $save
fi

exit
OpenPOWER on IntegriCloud