summaryrefslogtreecommitdiff
path: root/kflash.sh
blob: f8c4dd2945a6c073487121e25144c81700438c46 (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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
#!/bin/bash
# SPDX-License-Identifier: GPL-2.0+

set -e

kbuild_root=$(dirname $(readlink -f "$0"))

declare -A cross_compile
declare -A extra_kcflags
declare -A target_root

if [[ -r $kbuild_root/config.sh ]] ; then
	source $kbuild_root/config.sh || exit 1
fi

#
# Options parsing
#

dry_run=0
partitions=
silent=0
sudo=sudo

while [[ $# != 0 ]] ; do
	option=$1
	shift

	case $option in
		--dry-run)
			dry_run=1
			sudo=echo
			;;
		--partitions)
			partitions=($1)
			shift
			;;
		--silent)
			silent=1
			;;
		--*|-*)
			echo "Unrecognized option $option"
			exit 1
			;;
		*)
			platform=$option
			;;
	esac
done

#
# Platform
#

if [[ -z "$platform" ]] ; then
	for f in $kbuild_root/platforms/*.sh ; do
		pattern=$(cat $f | grep "dir_pattern=" | head -1)
		pattern=${pattern/#dir_pattern=/}
		if [[ -n "$pattern" ]] ; then
			echo $PWD | grep -q "$pattern" || continue
			platform=$(basename -s .sh $f)
			break
		fi
	done
fi

if [[ ! -r "$kbuild_root/platforms/$platform.sh" ]] ; then
	echo "Unknown platform '$platform'. Supported platforms are"
	for f in $kbuild_root/platforms/*.sh ; do
		echo "- $(basename -s .sh $f)"
	done
	exit 1
fi

source "${kbuild_root}/platforms/$platform.sh" || exit 1

if [[ "${#flash_parts[@]}" -lt 1 ]] ; then
	echo "Platform '$platform' doesn't declare flash_parts, can't flash"
	exit 1
fi

#
# Architecture
#

if [[ ! -r "$kbuild_root/arch/$arch.sh" ]] ; then
	echo "Unknown architecture '$arch'. Supported architectures are"
	for f in $kbuild_root/arch/*.sh ; do
		echo "- $(basename -s .sh $f)"
	done
	exit 1
fi

source "${kbuild_root}/arch/$arch.sh" || exit 1

# Override the output directory for ARM for historical reasons
case $arch in
	arm)
		output_dir=$PWD/output/arm32
		;;
	*)
		output_dir=$PWD/output/$arch
		;;
esac

target_dir=${target_dir:-${target_root[$arch]}}
tftp_dir=${tftp_dir:-$tftp_root/$platform}

#
# Determine the image source and destination file names
#

_image_type=${image/:*/}

if [[ -r "$kbuild_root/images/${_image_type}.sh" ]] ; then
	source "${kbuild_root}/images/${_image_type}.sh" || exit 1
else
	_image_file=${_image_type}
fi

if echo "${image}" | grep -q ':' ; then
	_image_name=${image/*:/}
else
	_image_name=${_image_file}
fi

_image_file="$output_dir/arch/$arch/boot/${_image_file}"

#
# Validate the partition names and paths
#

if [[ ! $partitions ]] ; then
	partitions=(${!flash_parts[@]})
fi

for part_name in ${partitions[@]} ; do
	case $part_name in
	dt|kernel|modules)
		;;
	*)
		echo "Unknown partition name '$part_name'"
		exit 1
		;;
	esac

	ifs="$IFS"; IFS=";" read -ra flash_part <<< "${flash_parts[$part_name]}"; IFS="$ifs"
	part_path=${flash_part[2]}

	case $part_path in
	RAW|/*)
		;;
	*)
		echo "Unknown partition path '$part_path'"
		exit 1
		;;
	esac
done

#
# Locate the flash device
#

probe_device() {
	local dev=$1

	# Skip devices that don't exist.
	if [[ ! -b /dev/$dev ]] ; then
		return
	fi

	# Add the 'p' suffix for MMC devices.
	local partdev=$dev
	echo $dev | grep -q mmcblk && partdev=${partdev}p

	#
	# Safety check: make sure the number and name of the partitions we will
	# flash match the expectations.
	#
	local part_count=$(ls -1 /dev/ | grep "${partdev}[0-9]" | wc -l)
	if [[ $part_count != $flash_parts_num ]] ; then
		echo "Device /dev/$dev has $part_count partitions, $flash_parts_num expected" >&2
		return
	fi

	local part_name
	for part_name in ${!flash_parts[@]} ; do
		local flash_part
		IFS=";" read -ra flash_part <<< "${flash_parts[$part_name]}"

		local part_idx=${flash_part[0]}
		local part_label=${flash_part[1]}
		local part_dev=/dev/${partdev}${part_idx}

		local label=$(lsblk -n -o PARTLABEL ${part_dev})
		if [[ -z "$label" ]] ; then
			label=$(lsblk -n -o LABEL ${part_dev})
		fi

		if [[ "$label" != ${part_label} ]] ; then
			echo "Partition ${part_dev} has label '$label', expected '${part_label}'" >&2
			return
		fi
	done

	echo /dev/$partdev
}

disks="mmcblk0 sda"
dev=

for disk in $disks ; do
	dev=$(probe_device "$disk")
	if [[ -n "$dev" ]] ; then
		break
	fi
done

if [[ -z "$dev" ]] ; then
	echo "No flash device found"
	exit 1
fi

echo "Found flash device ${dev}"

#
# Safety check: make sure nothing is mounted on $flash_mount_point, and no
# partition from the device is mounted.
#

if mount | grep -q " on $flash_mount_point " ; then
	echo "File system already mounted on $flash_mount_point."
	exit 1
fi

if mount | grep -q "$dev" ; then
	echo "Please unmount partitions $dev* before proceeding."
	exit 1
fi

#
# Safety check: make sure the kernel modules are installed.
#

version=$(cat $output_dir/include/generated/utsrelease.h | cut -d '"' -f 2)
mod_path="$target_dir/lib/modules/$version"

if [[ ! -d "$mod_path" ]] ; then
	echo "Modules not installed in $mod_path"
	exit 1
fi

#
# Flash
#

for part_name in ${partitions[@]} ; do
	IFS=";" read -ra flash_part <<< "${flash_parts[$part_name]}"
	part_idx=${flash_part[0]}

	if [[ -z "$flash_msg" ]] ; then
		flash_msg="Will flash $part_name to ${dev}${part_idx}"
	else
		flash_msg="${flash_msg}, $part_name to ${dev}${part_idx}"
	fi
done

if [[ ${dry_run} == 1 ]] ; then
	flash_msg="${flash_msg} (dry-run)"
fi

echo "${flash_msg}."
echo -n "Proceed? [y/N] "

read answer

if [[ x$answer != xy ]] ; then
	echo "Aborted"
	exit 1
fi

flash_raw() {
	local dst="$1"
	local src="$3"

	${sudo} dd if="$src" of="$dst" bs=64k
}

flash_fs() {
	local dev="$1"
	local dst="${flash_mount_point}$2"
	local src="$3"

	${sudo} mount "$dev" "$flash_mount_point"
	trap "${sudo} umount $flash_mount_point" EXIT

	if [[ -f "$src" ]] ; then
		${sudo} cp "$src" "$dst"
	else
		${sudo} rsync -av --delete "$src" "$dst"
	fi

	trap - EXIT
	${sudo} umount "$flash_mount_point"
}

for part_name in ${partitions[@]} ; do
	IFS=";" read -ra flash_part <<< "${flash_parts[$part_name]}"
	part_idx=${flash_part[0]}
	part_path=${flash_part[2]}

	echo "Flashing ${part_name} (${part_path})"

	case $part_path in
	RAW)
		flash_cmd="flash_raw"
		;;
	/*)
		flash_cmd="flash_fs"
		;;
	esac

	case $part_name in
	dt)
		# For DT, only flash the first entry. This can be improved later if
		# needed.
		_dtb=${dtbs[0]}
		_dtb_src="$output_dir/arch/${arch}/boot/dts/${_dtb/:*/}"
		_dtb_dst=${_dtb/*:/}
		_dtb_dst=${_dtb_dst/*\//}

		$flash_cmd "${dev}${part_idx}" "${part_path}/${_dtb_dst}" "${_dtb_src}"
		;;
	kernel)
		$flash_cmd "${dev}${part_idx}" "${part_path}/${_image_name}" "${_image_file}"
		;;
	modules)
		$flash_cmd "${dev}${part_idx}" "${part_path}/" "${mod_path}"
		;;
	esac
done

echo "Files copied, syncing"
${sudo} sync
echo "Done"

[[ $silent == 0 ]] && play -q -n synth 0.1 sin 880