summaryrefslogtreecommitdiffstats
path: root/src/action/update.sh
blob: 31e218b6ff20fdc9a431398df5f2751b230f5bdb (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
# Firmware update action
#
# Copyright (C) 2015  Patrick "P. J." McDermott
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

get_dist()
{
	local dist_ids=''
	local i=0
	local dist=''

	dist_ids="$(board_get_dists)"
	if [ $(printf '%s' "${dist_ids}" | wc -w | cut -d ' ' -f 1) -gt 1 ]; then
		set --
		for dist_id in ${dist_ids}; do
			set -- "${@}" "$(dist_get_name "${dist_id}")"
		done
		i=$(show_menu 'Select a firmware distributor' "${@}")
		i=$(($i + 1))
		dist="$(printf '%s ' ${dist_ids} | cut -d ' ' -f ${i})"
	else
		dist="$(printf '%s' ${dist_ids})"
	fi
	select_dist "${dist}"

	dbg 'Distributor: %s' "${dist}"

	return 0
}

get_mirror()
{
	local mirror_url=''
	local mirror_label=''
	local mirror_urls=''
	local i=0

	set --
	while :; do
		if ! read -r mirror_url || ! read -r mirror_label; then
			break
		fi
		mirror_urls="${mirror_urls} ${mirror_url}"
		set -- "${@}" "${mirror_label}"
	done <<-EOF
		$(dist_get_mirrors)
		EOF
	i=$(show_menu 'Select a mirror' "${@}")
	i=$(($i + 1))
	mirror_url="$(printf '%s ' ${mirror_urls} | cut -d ' ' -f ${i})"

	dbg 'Mirror: %s' "${mirror_url}"

	printf '%s' "${mirror_url}"
	return 0
}

get_version()
{
	local mirror="${1}"
	shift 1
	local versions=''
	local i=0
	local version=''

	versions="$(dist_get_versions "${mirror}" "$(board_get)")"
	i=$(show_menu 'Select a version' ${versions})
	i=$(($i + 1))
	version="$(printf '%s ' ${versions} | cut -d ' ' -f ${i})"

	dbg 'Version: %s' "${version}"

	printf '%s' "${version}"
	return 0
}

do_update()
{
	local mirror=''
	local version=''
	local rom=''

	dbg 'Updating firmware'

	# Probe for board and chip.
	if ! flashrom_probe; then
		return 1
	fi

	# Select firmware distributor, mirror, and version.
	get_dist
	mirror="$(get_mirror)"
	version="$(get_version "${mirror}")"

	# Get, prepare, and flash ROM.
	dist_get_rom "${mirror}" "${version}" "$(board_get_id)"
	board_prepare_rom
	flashrom_write "$(board_get_chip)"
	rm_temp 'rom'

	return 0
}