summaryrefslogtreecommitdiffstats
path: root/src/action/update.sh
blob: 477300fac4a53f0388dd1240f5da43d7436a5f5e (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
# 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
		if ! i=$(show_menu 'Select a firmware distributor' "${@}"); then
			return 1
		fi
		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
	if ! i=$(show_menu 'Select a mirror' "${@}"); then
		return 1
	fi
	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=''

	if ! versions="$(dist_get_versions "${mirror}" "$(board_get)")"; then
		return 1
	fi
	if ! i=$(show_menu 'Select a version' ${versions}); then
		return 1
	fi
	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.
	if ! get_dist; then
		return 1
	fi
	if ! mirror="$(get_mirror)"; then
		return 1
	fi
	if ! version="$(get_version "${mirror}")"; then
		return 1
	fi

	# Get and prepare a ROM.
	if ! dist_get_rom "${mirror}" "${version}" "$(board_get_id)"; then
		return 1
	fi
	if ! board_prepare_rom; then
		rm_temp 'rom'
		return 1
	fi

	# Write the ROM to flash.
	info 'Installing new firmware...'
	if flashrom_write "$(board_get_chip)"; then
		info 'Firmware installation complete'
		sleep 5
		rm_temp 'rom'
		return 1
	fi

	# Clean up the ROM.
	rm_temp 'rom'

	return 0
}