summaryrefslogtreecommitdiffstats
path: root/src/flashrom.sh
blob: cf0bcbf6681d1d9fd23a16ad1747b569b3e7f67f (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
# Functions for working with flashrom
#
# 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/>.

flashrom_probe()
{
	local line=''
	local board_vendor=''
	local board_part=''
	local board_found=false
	local chip_vendor=''
	local chip_device=''
	local chip_size=''
	local chip_bus=''
	local chip_found=false

	IFS=''
	while read -r line; do
		unset IFS
		case "${line}" in
			'Vendor ID: '*', part ID: '*)
				board_vendor="${line%, part ID: *}"
				board_vendor="${board_vendor#Vendor ID: }"
				board_part="${line#*, part ID: }"
				dbg 'Found board:'
				dbg '  Vendor: %s' "${board_vendor}"
				dbg '  Part:   %s' "${board_part}"
				board_found=true
				;;
			'Found '*' flash chip "'*'" ('*' kB, '*').')
				if ! ${board_found}; then
					err 'Failed to probe board'
					return 1
				fi
				chip_vendor="${line% flash chip *}"
				chip_vendor="${line#Found }"
				chip_device="${line#* flash chip \"}"
				chip_device="${chip_device%\" (*}"
				chip_size="${line#* (}"
				chip_size="${chip_size% kB, *}"
				chip_bus="${line#* kB, }"
				chip_bus="${chip_bus%).}"
				dbg 'Found chip:'
				dbg '  Vendor: %s' "${chip_vendor}"
				dbg '  Device: %s' "${chip_device}"
				dbg '  Size:   %u' "${chip_size}"
				dbg '  Bus:    %s' "${chip_bus}"
				if find_board "${board_vendor}" \
						"${board_part}" "${chip_device}"
				then
					chip_found=true
					break
				fi
				;;
		esac
		IFS=''
	done <flashrom.log
	#done <<-EOF
	#	$(${FLASHROM} -p internal -V)
	#	EOF
	unset IFS

	if ! ${board_found}; then
		err 'Failed to probe board'
		return 1
	fi
	if ! ${chip_found}; then
		err 'Failed to probe chip'
		return 1
	fi

	return 0
}

flashrom_write()
{
	local chip="${1}"
	shift 1
	local line=''
	local verified=false

	info 'Writing flash'

	IFS=''
	while read -r line; do
		unset IFS
		if [ "x${line}" = 'xVerifying flash... VERIFIED.' ]; then
			verified=true
		fi
		dbg 'flashrom: %s' "${line}"
		IFS=''
	done <<-EOF
		$(${FLASHROM} -p internal -c "${chip}" -w "$(get_temp_dir)/rom")
		EOF
	unset IFS

	if ! ${verified}; then
		err 'Failed to write firmware'
		return 1
	fi

	return 0
}