summaryrefslogtreecommitdiff
path: root/include/inject.sh
blob: 53128995f19cf535803715fba4566d171e022d0d (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
# SPDX-License-Identifier: GPL-3.0-only

# Copyright (c) 2022 Caleb La Grange <thonkpeasant@protonmail.com>
# Copyright (c) 2022 Ferass El Hafidi <vitali64pmemail@protonmail.com>
# Copyright (c) 2023-2025 Leah Rowe <leah@libreboot.org>

cbcfgsdir="config/coreboot"
tmpromdel="$XBMK_CACHE/DO_NOT_FLASH"
nvmutil="util/nvmutil/nvmutil"
ifdtool="elf/coreboot/default/ifdtool"

checkvars="CONFIG_GBE_BIN_PATH"

if [ -n "$checkvarsxbmk" ]; then

	checkvars="$checkvars $checkvarsxbmk"
fi

if [ -n "$checkvarschk" ]; then

	checkvars="$checkvars $checkvarschk"
fi

archive=""
board=""
boarddir=""
IFD_platform=""
ifdprefix=""
new_mac=""
tmpromdir=""
tree=""
xchanged=""

eval "`setvars "" $checkvars`"

inject()
{
	remkdir "$tmpromdel"

	if [ $# -lt 1 ]; then

		err "No options specified" "inject" "$@"
	fi

	archive="$1";
	new_mac="xx:xx:xx:xx:xx:xx"

	nuke=""
	xchanged=""

	[ $# -gt 1 ] && case "$2" in

	nuke)

		new_mac=""
		nuke="nuke"
		;;

	setmac)

		if [ $# -gt 2 ]; then

			new_mac="$3" && \

			if [ -z "$new_mac" ]; then

				err "Empty MAC address specified" "inject" "$@"
			fi
		fi
		;;

	*)

		err "Unrecognised inject mode: '$2'" "inject" "$@" ;;
	esac

	if [ "$new_mac" = "keep" ]; then

		new_mac=""
	fi

	check_release

	if check_target; then

		if ! patch_release; then

			return 0
		fi
	fi

	if [ "$xchanged" = "y" ]; then

		remktar
	fi

	if [ "$xchanged" = "y" ]; then

		printf "\n'%s' was modified\n" "$archive" 1>&2

	else

		printf "\n'%s' was NOT modified\n" "$archive" 1>&2
	fi

	x_ rm -Rf "$tmpromdel"
}

check_release()
{
	if [ -L "$archive" ]; then

		err "'$archive' is a symlink" "check_release" "$@"
	fi

	if e "$archive" f missing; then

		err "'$archive' missing" "check_release" "$@"
	fi

	archivename="`basename "$archive" || err "Can't get '$archive' name"`" \
	    || err "can't get '$archive' name" "check_release" "$@"

	if [ -z "$archivename" ]; then

		err "Can't determine archive name" "check_release" "$@"
	fi

	case "$archivename" in

	*_src.tar.xz)

		err "'$archive' is a src archive!" "check_release" "$@"
		;;

	grub_*|seagrub_*|custom_*|seauboot_*|seabios_withgrub_*)

		err "'$archive' is a ROM image" "check_release" "$@"
		;;

	*.tar.xz) _stripped_prefix="${archivename#*_}"

		board="${_stripped_prefix%.tar.xz}"
		;;

	*)

		err "'$archive': cannot detect board" "check_release" "$@"
		;;

	esac; :
}

check_target()
{
	if [ "$board" != "${board#serprog_}" ]; then

		return 1
	fi

	boarddir="$cbcfgsdir/$board"

	. "$boarddir/target.cfg" || \
	    err "Can't read '$boarddir/target.cfg'" "check_target" "$@"

	if [ -z "$tree" ]; then

		err "tree unset in '$boarddir/target.cfg'" "check_target" "$@"
	fi

	x_ ./mk -d coreboot "$tree"

	ifdtool="elf/coreboot/$tree/ifdtool"

	if [ -n "$IFD_platform" ]; then

		ifdprefix="-p $IFD_platform"
	fi
}

patch_release()
{
	if [ "$nuke" != "nuke" ]; then

		x_ ./mk download "$board"
	fi

	has_hashes="n"
	tmpromdir="$tmpromdel/bin/$board"

	remkdir "${tmpromdir%"/bin/$board"}"

	x_ tar -xf "$archive" -C "${tmpromdir%"/bin/$board"}"

	for _hashes in "vendorhashes" "blobhashes"; do

		if e "$tmpromdir/$_hashes" f; then

			has_hashes="y"
			hashfile="$_hashes"

			break
		fi
	done

	if ! readkconfig; then

		return 1

	elif [ -n "$new_mac" ] && \
	    [ -n "$CONFIG_GBE_BIN_PATH" ]; then

		modify_mac
	fi
}

readkconfig()
{
	x_ rm -f "$xbtmp/cbcfg"

	fx_ scankconfig x_ find "$boarddir/config" -type f

	if e "$xbtmp/cbcfg" f missing; then

		return 1
	fi

	. "$xbtmp/cbcfg" || \
	    err "Can't read '$xbtmp/cbcfg'" "readkconfig" "$@"

	if ! setvfile "$@"; then

		return 1
	fi
}

scankconfig()
{
	for cbc in $checkvars; do

		grep "$cbc" "$1" 2>/dev/null 1>>"$xbtmp/cbcfg" || :
	done
}

modify_mac()
{
	x_ cp "${CONFIG_GBE_BIN_PATH##*../}" "$xbtmp/gbe"

	if [ -n "$new_mac" ] && \
	    [ "$new_mac" != "restore" ]; then

		x_ make -C util/nvmutil clean

		x_ make -C util/nvmutil

		x_ "$nvmutil" "$xbtmp/gbe" setmac "$new_mac"
	fi

	fx_ newmac x_ find "$tmpromdir" -maxdepth 1 -type f -name "*.rom"

	printf "\nThe following GbE NVM data will be written:\n"

	x_ "$nvmutil" "$xbtmp/gbe" dump | grep -v "bytes read from file" || :
}

newmac()
{
	if e "$1" f; then

		xchanged="y"

		x_ "$ifdtool" $ifdprefix -i GbE:"$xbtmp/gbe" "$1" -O "$1"
	fi
}

remktar()
{
	(
		x_ cd "${tmpromdir%"/bin/$board"}"

		printf "Re-building tar archive (please wait)\n"

		mkrom_tarball "bin/$board" 1>/dev/null

	) || err "Cannot re-generate '$archive'" "remktar" "$@"

	mv "${tmpromdir%"/bin/$board"}/bin/${relname}_${board}.tar.xz" \
	    "$archive" || \
	    err "'$archive' -> Can't overwrite" "remktar" "$@"; :
}