summaryrefslogtreecommitdiff
path: root/resources/scripts/blobs/inject
blob: 99cfc56968b3bce4278c8292156aa098d37c9e77 (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
#!/usr/bin/env sh

# SPDX-FileCopyrightText: 2022 Caleb La Grange <thonkpeasant@protonmail.com>
# SPDX-FileCopyrightText: 2022 Ferass El Hafidi <vitali64pmemail@protonmail.com>
# SPDX-License-Identifier: GPL-3.0-only

Fail(){
	if [ ! -z ${@+x} ]; then
		printf "\nERROR: ${@}\n"
	fi

	cat <<- EOF
	USAGE: ./blobutil inject -r [/path/to/rom] -b [boardname] -m [macaddress]
	Example: ./blobutil 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 './blobutil inject listboards' to get a list of valid boards
	EOF

	exit 1
}

Modify_gbe(){
	rom=${1}
	printf "changing mac address in gbe to ${new_mac}\n"
	_gbe_location=${CONFIG_GBE_BIN_PATH#../../}

	if [ ! -f util/nvmutil/nvm ]; then
		make -C util/nvmutil || Fail 'failed to build nvmutil'
	fi

	_gbe_tmp=$(mktemp -t gbeXXXX.bin)
	cp ${_gbe_location} ${_gbe_tmp}
	./util/nvmutil/nvm ${_gbe_tmp} setmac ${new_mac} || Fail 'failed to modify mac address\nmake sure the mac address in the correct format'
	
	./coreboot/default/util/ifdtool/ifdtool -i GbE:${_gbe_tmp} ${rom} -O ${rom} || exit 1

	rm ${_gbe_tmp}
}

listboards() {
	for boarddir in resources/coreboot/*; do
		if [ ! -d "${boarddir}" ]; then continue; fi
		board="${boarddir/##resources/coreboot/}"
		board="${board%/}"
		printf '%s\n' "${board##*/}"
	done
}

# 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}"
		;;
		*)
		return 1
	esac	

	if [ -d "resources/coreboot/${board}/" ]; then
		printf '%s\n' "${board}"
	else
		return 1
	fi
}

Patch(){
rom="${1}"
set -- "resources/coreboot/${board}/config/*"
. ${1} 2>/dev/null
. "resources/coreboot/${board}/board.cfg"

	if [ "$CONFIG_HAVE_MRC" = "y" ]; then
		printf 'adding mrc\n'
		./coreboot/default/util/cbfstool/cbfstool ${rom} add -f mrc/haswell/mrc.bin -n mrc.bin -t mrc -b 0x78fe00 || exit 1
	fi

	if [ "${CONFIG_HAVE_ME_BIN}" = "y" ]; then
		_me_location=${CONFIG_ME_BIN_PATH#../../}
		printf 'adding intel management engine\n'
		./coreboot/default/util/ifdtool/ifdtool -i me:${_me_location} ${rom} -O ${rom} || exit 1
	fi

	if [ "${modifygbe}" = "true" ] && ! [ "${release}" = "true" ]; then
		Modify_gbe ${rom}
	fi
}

Patch_release(){
	_tmpdir=$(mktemp -d "/tmp/${board}_tmpXXXX")
	tar xf "${releasearchive}" -C "${_tmpdir}" || \
	Fail 'could not extract release archive'
	
	for rom in ${_tmpdir}/bin/*/*.rom ; do
		echo "patching rom $rom"
		Patch ${rom} || \
		Fail "could not patch ${rom}"
	done

	( cd ${_tmpdir}/bin/*
	sha1sum --status -c blobhashes || \
	Fail 'ROMs did not match expected hashes'
	)
	

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

	if ! [ -d bin/release ]; then
		mkdir -p bin/release
	fi

	mv ${_tmpdir}/bin/* bin/release/ && \
	printf '%s\n' 'Success! Your ROMs are in bin/release'

	rm -r "${_tmpdir}"
}

Check_release(){
if ! [ -f ${1} ]; then
	return 1
fi

_filetype=$(file -b "${1}")

if [ "${_filetype%%,*}" = "XZ compressed data" ]; then
	printf "%s\n" "Release archive ${1} detected"
else
	return 1
fi
}

if [ "${1}" = "listboards" ]; then
	listboards
	exit 0
fi

# Implementing parameter parsing now so more options can be added later
while getopts r:b:m: option
do 
    case "${option}"
        in
        r)rom=${OPTARG};;
	b)board=${OPTARG};;
	m)
		modifygbe=true
		new_mac=${OPTARG}
		;;
    esac
done



if ! Check_release ${1} ; then
	if [ ! -f "${rom}" ]; then
		Fail "${rom} is not a valid path"
	elif [ -z ${rom+x} ]; then
		Fail 'no rom specified'
	elif [ -z ${board+x} ]; then
		board=$(Detect_board ${rom}) || \
		Fail 'no board specified'
	fi
else
	release=true
	releasearchive="${1}"
	board=$(Detect_board ${1}) || \
	Fail 'Could not detect board type'
fi


if [ ! -d "resources/coreboot/${board}/" ]; then
	Fail "board ${board} not found"
fi

if [ ! -d coreboot/default ]; then
	printf "downloading coreboot\n"
	./download coreboot default
fi

if [ ! -f "coreboot/default/util/ifdtool/ifdtool" ]; then
printf "building ifdtool from coreboot\n"
	./build module cbutils default || Fail 'could not build ifdtool'
fi

if [ ! -f "coreboot/default/util/cbfstool/cbfstool" ]; then
printf "building cbfstool from coreboot\n"
./build module cbutils default || Fail 'could not build cbfstool'
fi

./blobutil download ${board} || \
Fail "Could not download blobs for ${board}, check network connection"

if [ "${release}" = "true" ]; then
	echo 'patching release file'
	Patch_release
else
	Patch ${rom}
fi