summaryrefslogtreecommitdiff
path: root/script/update/vendor/inject
blob: c4a7f70210f1419b4529c632b8112352d3023e19 (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
#!/usr/bin/env sh
# SPDX-License-Identifier: GPL-3.0-only
# SPDX-FileCopyrightText: 2022 Caleb La Grange <thonkpeasant@protonmail.com>
# SPDX-FileCopyrightText: 2022 Ferass El Hafidi <vitali64pmemail@protonmail.com>
# SPDX-FileCopyrightText: 2023 Leah Rowe <leah@libreboot.org>

. "include/err.sh"
. "include/option.sh"

nvmutil="util/nvmutil/nvm"

eval "$(setvars "" archive rom modifygbe nukemode release new_mac)"

main()
{
	[ $# -lt 1 ] && err "No options specified."
	[ "${1}" = "listboards" ] && \
		listitems config/coreboot && exit 0

	archive="${1}"

	while getopts n:r:b:m: option; do
		case "${option}" in
		n) nukemode="${OPTARG}" ;;
		r) rom=${OPTARG} ;;
		b) board=${OPTARG} ;;
		m) modifygbe=true
		   new_mac=${OPTARG} ;;
		esac
	done

	check_board
	build_dependencies
	inject_vendorfiles
	[ "${nukemode}" = "nuke" ] && return 0
	printf "Friendly reminder (this is *not* an error message):\n"
	printf "Please always ensure that the files were inserted correctly.\n"
}

check_board()
{
	if ! check_release "${archive}" ; then
		[ -f "${rom}" ] || \
			err "check_board: \"${rom}\" is not a valid path"
		[ -z "${rom+x}" ] && \
			err "check_board: no rom specified"
		[ ! -z ${board+x} ] || \
			board=$(detect_board "${rom}")
	else
		release="y"
		board=$(detect_board "${archive}")
	fi

	boarddir="${cbcfgsdir}/${board}"
	[ -d "${boarddir}" ] && return 0
	err "check_board: board ${board} not found"
}

check_release()
{
	[ -f "${archive}" ] || return 1
	[ "${archive##*.}" = "xz" ] || return 1
	printf "%s\n" "Release archive ${archive} detected"
}

# This function tries to determine the board from the filename of the rom.
# It will only succeed if the filename is not changed from the build/download
detect_board()
{
	path="${1}"
	filename=$(basename ${path})
	case ${filename} in
	grub_*)
		board=$(echo "${filename}" | cut -d '_' -f2-3) ;;
	seabios_withgrub_*)
		board=$(echo "${filename}" | cut -d '_' -f3-4) ;;
	*.tar.xz)
		_stripped_prefix=${filename#*_}
		board="${_stripped_prefix%.tar.xz}" ;;
	*)
		err "detect_board: could not detect board type"
	esac	
	[ -d "${boarddir}/" ] || \
		err "detect_board: dir, ${boarddir}, doesn't exist"
	printf "%s\n" "${board}"
}

build_dependencies()
{
	[ -d "${cbdir}" ] || x_ ./update project trees -f coreboot default
	if [ ! -f "${cbfstool}" ] || [ ! -f "${ifdtool}" ]; then
		x_ ./update project trees -b coreboot utils default
	fi
	[ -z "${new_mac}" ] || [ -f "${nvmutil}" ] || x_ make -C util/nvmutil
	[ "${nukemode}" = "nuke" ] && return 0
	x_ ./update vendor download ${board}
}

inject_vendorfiles()
{
	[ "${release}" != "y" ] && x_ patch_rom "${rom}" && return 0
	printf "patching release images\n"
	patch_release_roms
}

patch_release_roms()
{
	_tmpdir="tmp/romdir"
	x_ rm -Rf "${_tmpdir}"
	x_ mkdir -p "${_tmpdir}"
	x_ tar -xf "${archive}" -C "${_tmpdir}"

	for x in "${_tmpdir}"/bin/*/*.rom ; do
		printf "patching rom: %s\n" "$x"
		x_ patch_rom "${x}"
	done
	for x in "${_tmpdir}"/bin/*/*_nomicrocode.rom ; do
		[ -f "${x}" ] || continue
		[ -f "${x%_nomicrocode.rom}.rom" ] || continue

		x_ cp "${x%_nomicrocode.rom}.rom" "${x}"
		x_ "${cbfstool}" "${x}" remove -n cpu_microcode_blob.bin
	done

	(
	x_ cd "${_tmpdir}/bin/"*

	# NOTE: For compatibility with older rom releases, defer to sha1
	[ "${nukemode}" = "nuke" ] || \
	    sha512sum --status -c vendorhashes || \
	    sha1sum --status -c vendorhashes || \
	    sha512sum --status -c blobhashes || \
	    sha1sum --status -c blobhashes || \
	    err "patch_release_roms: ROMs did not match expected hashes"
	)

	if [ "${modifygbe}" = "true" ]; then
		for x in "${_tmpdir}"/bin/*/*.rom ; do
			modify_gbe "${x}"
		done
	fi

	[ -d bin/release ] || x_ mkdir -p bin/release
	x_ mv "${_tmpdir}"/bin/* bin/release/

	printf "Success! Your ROMs are in bin/release\n"

	x_ rm -Rf "${_tmpdir}"
}

patch_rom()
{
	rom="${1}"

	# we don't process no-microcode roms; these are
	# instead re-created at the end, after re-inserting
	# on roms with microcode, by copying and then removing,
	# so that the hashes will match (otherwise, cbfstool
	# may sometimes insert certain vendor files at the wrong offset)
	# (unless nomicrocode is the only config provided)
	[ "${rom}" != "${rom%_nomicrocode.rom}.rom" ] && \
	[ -f "${rom%_nomicrocode.rom}.rom" ] && \
	[ "${release}" = "y" ] && return 0

	x_ check_defconfig "${boarddir}"

	set -- "${boarddir}/config/"*
	. "${1}" 2>/dev/null

	[ "$CONFIG_HAVE_MRC" = "y" ] && \
		inject "mrc.bin" "${CONFIG_MRC_FILE}" "mrc" "0xfffa0000"
	[ "${CONFIG_HAVE_ME_BIN}" = "y" ] && \
		inject "IFD" "${CONFIG_ME_BIN_PATH}" "me"
	[ "${CONFIG_KBC1126_FIRMWARE}" = "y" ] && \
		inject "ecfw1.bin" "$CONFIG_KBC1126_FW1" "raw" \
		    "${CONFIG_KBC1126_FW1_OFFSET}" && \
		inject "ecfw2.bin" "$CONFIG_KBC1126_FW2" "raw" \
		    "${CONFIG_KBC1126_FW2_OFFSET}"
	[ ! -z "${CONFIG_VGA_BIOS_FILE}" ] && \
	    [ ! -z "${CONFIG_VGA_BIOS_ID}" ] && \
		inject "pci${CONFIG_VGA_BIOS_ID}.rom" \
		    "${CONFIG_VGA_BIOS_FILE}" "optionrom"
	[ "${CONFIG_INCLUDE_SMSC_SCH5545_EC_FW}" = "y" ] && \
	    [ ! -z "${CONFIG_SMSC_SCH5545_EC_FW_FILE}" ] && \
		inject "sch5545_ecfw.bin" "$CONFIG_SMSC_SCH5545_EC_FW_FILE" raw
	[ "${modifygbe}" = "true" ] && ! [ "${release}" = "y" ] && \
		inject "IFD" "${CONFIG_GBE_BIN_PATH}" "GbE"

	printf "ROM image successfully patched: %s\n" "${rom}"
}

inject()
{
	[ $# -lt 3 ] && \
		err "inject $@, $rom: usage: inject name path type (offset)"

	eval "$(setvars "" cbfsname _dest _t _offset)"
	cbfsname="${1}"
	_dest="${2##*../}"
	_t="${3}"
	[ $# -gt 3 ] && _offset="-b ${4}" && [ -z "${4}" ] && \
	    err "inject $@, $rom: offset passed, but empty (not defined)"

	[ -z "${_dest}" ] && err "inject $@, ${rom}: empty destination path"
	[ ! -f "${_dest}" ] && [ "${nukemode}" != "nuke" ] && \
		err "inject_${dl_type}: file missing, ${_dest}"

	[ "$nukemode" = "nuke" ] || \
		printf "Inserting %s/%s into file: %s\n" \
		    "${cbfsname}" "${_t}" "$rom"

	if [ "${_t}" = "GbE" ]; then
		x_ mkdir -p tmp
		x_ cp "${_dest}" "tmp/gbe.bin"
		_dest="tmp/gbe.bin"
		x_ "${nvmutil}" "${_dest}" setmac "${new_mac}"
	fi
	if [ "${cbfsname}" = "IFD" ]; then
		if [ "${nukemode}" != "nuke" ]; then
			x_ "${ifdtool}" -i ${_t}:${_dest} "${rom}" -O "$rom"
		else
			x_ "${ifdtool}" --nuke ${_t} "${rom}" -O "${rom}"
		fi
	else
		if [ "${nukemode}" != "nuke" ]; then
			x_ "${cbfstool}" "${rom}" add -f "${_dest}" \
			    -n "${cbfsname}" -t ${_t} ${_offset}
		else
			x_ "${cbfstool}" "${rom}" remove -n "${cbfsname}"
		fi

	fi
}

usage()
{
	cat <<- EOF
	USAGE: ./update vendor inject -r [rom path] -b [boardname] -m [macaddress]
	Example: ./update vendor inject -r x230_12mb.rom -b x230_12mb

	Adding a macadress to the gbe is optional.
	If the [-m] parameter is left blank, the gbe will not be touched.

	Type './update vendor inject listboards' to get a list of valid boards
	EOF
}

main $@