summaryrefslogtreecommitdiff
path: root/resources/scripts/update/blobs/extract
blob: fa76dfb52284cec56988003f4768ac3293c1dad9 (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
#!/usr/bin/env sh
# script to automate extracting blobs from an existing vendor bios

# SPDX-FileCopyrightText: 2022 Caleb La Grange <thonkpeasant@protonmail.com>
# SPDX-FileCopyrightText: 2023 Leah Rowe <leah@libreboot.org>
# SPDX-License-Identifier: GPL-3.0-only

. "include/err.sh"

sname=""
board=""
vendor_rom=""

cbdir="coreboot/default"
cbcfgsdir="resources/coreboot"
ifdtool="${cbdir}/util/ifdtool/ifdtool"
mecleaner="me_cleaner/me_cleaner.py"
me7updateparser="resources/blobs/me7_update_parser.py"

boarddir=""

CONFIG_HAVE_MRC=""
CONFIG_ME_BIN_PATH=""
CONFIG_GBE_BIN_PATH=""
CONFIG_IFD_BIN_PATH=""

_me_destination=""
_gbe_destination=""
_ifd_destination=""

main()
{
	sname=${0}
	[ $# -lt 2 ] && err "Missing arguments (fewer than two)."

	board="${1}"
	vendor_rom="${2}"
	boarddir="${cbcfgsdir}/${board}"

	check_board
	build_dependencies
	extract_blobs
}

check_board()
{
	if [ ! -f "${vendor_rom}" ]; then
		err "check_board: ${board}: file does not exist: ${vendor_rom}"
	elif [ ! -d "${boarddir}" ]; then
		err "check_board: ${board}: target not defined"
	elif [ ! -f "${boarddir}/target.cfg" ]; then
		err "check_board: ${board}: missing target.cfg"
	fi
}

build_dependencies()
{
	if [ ! -d me_cleaner ]; then
		./fetch me_cleaner || \
		    err "build_dependencies: can't fetch me_cleaner"
	elif [ ! -d "${cbdir}" ]; then
		./fetch_trees coreboot default || \
		    err "build_dependencies: can't fetch coreboot"
	elif [ ! -f "${ifdtool}" ]; then
		make -C "${ifdtool%/ifdtool}" || \
		    err "build_dependencies: can't build ifdtool"
	fi
}

extract_blobs()
{
	printf "extracting blobs for %s from %s\n" ${board} ${vendor_rom}

	set -- "${boarddir}/config/"*
	. "${1}"
	. "${boarddir}/target.cfg"

	[ "$CONFIG_HAVE_MRC" != "y" ] || \
		./update blobs mrc || err "extract_blobs: can't fetch mrc"

	_me_destination=${CONFIG_ME_BIN_PATH#../../}
	_gbe_destination=${CONFIG_GBE_BIN_PATH#../../}
	_ifd_destination=${CONFIG_IFD_BIN_PATH#../../}

	extract_blob_intel_me
	extract_blob_intel_gbe_nvm

	# Cleans up other files extracted with ifdtool
	rm -f flashregion*.bin || err "extract_blobs: !rm -f flashregion*.bin"

	[ -f "${_ifd_destination}" ] || err "extract_blobs: Cannot extract IFD"
	printf "gbe, ifd, and me extracted to %s\n" "${_me_destination%/*}"
}

extract_blob_intel_me()
{
	printf "extracting clean ime and modified ifd\n"

	"${mecleaner}" -D "${_ifd_destination}" \
		-M "${_me_destination}" "${vendor_rom}" -t -r -S || \
	    "${me7updateparser}" \
		-O "${_me_destination}" "${vendor_rom}" || \
	    err "extract_blob_intel_me: cannot extract from vendor rom"
}

extract_blob_intel_gbe_nvm()
{
	printf "extracting gigabit ethernet firmware"
	./"${ifdtool}" -x "${vendor_rom}" || \
	    err "extract_blob_intel_gbe_nvm: cannot extract gbe.bin from rom"
	mv flashregion*gbe.bin "${_gbe_destination}" || \
	    err "extract_blob_intel_gbe_nvm: cannot move gbe.bin"
}

print_help()
{
	printf "Usage: ./update blobs extract {boardname} {path/to/vendor_rom}\n"
	printf "Example: ./update blobs extract x230 12mb_flash.bin\n"
	printf "\nYou need to specify exactly 2 arguments\n"
}

main $@