summaryrefslogtreecommitdiffstats
path: root/resources/scripts/helpers/download/coreboot
blob: 16bd1a87f8337b5582740cc8c06c25919b90591f (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
#!/bin/bash

#  helper script: downloads coreboot and patches/deblobs it
#
#	Copyright (C) 2014, 2015, 2016 Francis Rowe <info@gluglug.org.uk>
#	Copyright (C) 2015 Paul Kocialkowski <contact@paulk.fr>
#
#    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/>.
#

# This script assumes that the working directory is the
# root of libreboot_src or libreboot git.

[ "x${DEBUG+set}" = 'xset' ] && set -v
set -u -e

if [ -f "version" ]; then
	# _src release archive is being used
	version="libreboot-$(cat version)"
else
	# git repo is being used
	version="libreboot-$(git describe --tags HEAD)"
fi

# coreboot revisios used for crossgcc
crossgccrevision="4351ace145c4069f9c93ca7541d6dfaf8300b53b"

# sanity check (check for invalid paths in the reused.list patch lists before proceeding)
for payloads in resources/libreboot/config/*; do

	if [ ! -d "${payloads}/" ]; then
		continue
	fi

	payload="${payloads##*/}"

	for boardconfig in resources/libreboot/config/${payload}/*; do

		if [ ! -d "${boardconfig}/" ]; then
			continue
		fi

		boardname="${boardconfig##*/}"
		cbrevision=$(cat "${boardconfig}/cbrevision")
		vbootrevision=$(cat "${boardconfig}/vbootrevision")

		reused_coreboot_patches="resources/libreboot/patch/coreboot/${cbrevision}/${payload}/${boardname}/reused.list"
		reused_vboot_patches="resources/libreboot/patch/vboot/${vbootrevision}/${payload}/${boardname}/reused.list"
		for reused_patches in "${reused_coreboot_patches}" "${reused_vboot_patches}"; do
			if [ -f "${reused_patches}" ]; then
				for patch in $(cat "${reused_patches}"); do
					if [ ! -f "./${patch}" ]; then
						printf "%s listed in %s does not exist\n" "${patch}" "${reused_patches}"
						exit 1
					fi
				done
			fi
		done

	done
done

# in ascending filename order, apply patches from a directory
apply_patches_from_directory() {
	patch_directory="${1}" # directory containing the patch files

	if [ -d "${patch_directory}" ]; then
		for patch in ${patch_directory}/*.patch;  do

			if [ "${patch##*/}" = "*.patch" ]; then # oh so ugly
				continue # ugly ugly ugly ugly ugly
			fi # most hideous thing you've ever seen

			git am "${patch}" || return 1
		done
	fi
}
# files listed in the file (if found) are absolute paths, relative to the root of the libreboot src directory
# the file lists patches patches that should be applied
apply_patches_from_file() {
	patch_list="${1}" # file listing the paths to all the patches
	libreboot_src_root="${2}" # path to the root of the libreboot_src directory

	if [ -f "${patch_list}" ]; then
		for patchname in $(cat "${patch_list}"); do
			git am "${libreboot_src_root}/${patchname}" || return 1
		done
	fi
}
make_coreboot_src_directory() {
	payload="${1}"
	boardname="${2}"
	firmwarepath="${3}" # libreboot_src/coreboot/
(
	cd "${firmwarepath}/"
	# copy coreboot directory there
	rm -Rf "${payload:?}/${boardname:?}/"
	if [ ! -d "${payload}/" ]; then
		mkdir -p "${payload}/"
	fi
	cp -R "coreboot/" "${payload}/${boardname}/"
)
}
reset_at_revision() {
	revision="${1}"
	git reset --hard ${revision}
}

printf "Downloading coreboot, patching coreboot and deblobbing coreboot\n"

# This grabs current base used, and applies patches
# This is also used to run the deblob scripts.

# Remove the old version that may exist
# ------------------------------------------------------------------------------

rm -Rf "coreboot/"

mkdir "coreboot/"

cd "coreboot/"

# Get latest coreboot:
# ------------------------------------------------------------------------------

# download it using git
git clone https://review.coreboot.org/coreboot

# there are modifications required
cd "coreboot/"

# Define a common version (based on the libreboot version)
# Most likely redundant, because the build system needs to update
# this every time when building a ROM image anyway
printf '%s\n' "${version}" >".coreboot-version"

# vboot submodule is needed
# ------------------------------------------------------------------------------

git submodule update --init --checkout -- 3rdparty/vboot/

# Create branches, with patches in each branch
# Create separate coreboot source directories *for each board*
# ------------------------------------------------------------------------------

for payloads in ../../resources/libreboot/config/*; do

	if [ ! -d "${payloads}/" ]; then
		continue
	fi

	payload="${payloads##*/}"

	for boardconfig in ../../resources/libreboot/config/${payload}/*; do

		if [ ! -d "${boardconfig}/" ]; then
			continue
		fi

		boardname="${boardconfig##*/}"
		cbrevision=$(cat "${boardconfig}/cbrevision")
		vbootrevision=$(cat "${boardconfig}/vbootrevision")

		make_coreboot_src_directory ${payload} ${boardname} ..

		# patch that version
		(

			cd "../${payload}/${boardname}/"
			reset_at_revision ${cbrevision}

			# apply patches (coreboot, common to all systems using this revision)
			apply_patches_from_directory "../../../resources/libreboot/patch/common/coreboot/${cbrevision}"
			# apply patches re-used from other boards, before applying main patches (common patches for similar boards)
			apply_patches_from_file "../../../resources/libreboot/patch/coreboot/${cbrevision}/${payload}/${boardname}/reused.list" ../../..
			# apply patches (coreboot, machine-specific for this revision)
			apply_patches_from_directory "../../../resources/libreboot/patch/coreboot/${cbrevision}/${payload}/${boardname}"

			cd "3rdparty/vboot/"
			# reset to known revision (vboot)
			reset_at_revision ${vbootrevision}

			# apply patches (vboot, common to all systems using this revision)
			apply_patches_from_directory "../../../../../resources/libreboot/patch/common/vboot/${vbootrevision}"
			# apply patches re-used from other boards, before applying main patches (common patches for similar boards)
			apply_patches_from_file "../../../../../resources/libreboot/patch/vboot/${vbootrevision}/${payload}/${boardname}/reused.list" ../../../../..
			# apply patches (vboot, machine-specific for this revision)
			apply_patches_from_directory "../../../../../resources/libreboot/patch/vboot/${vbootrevision}/${payload}/${boardname}"

		)
	done
done

# prepare directory for crossgcc
rm -Rf "../../crossgcc/"
make_coreboot_src_directory crossgcc ${crossgccrevision} ..
(
    cd "../crossgcc/${crossgccrevision}"
    reset_at_revision ${crossgccrevision}
    rm -Rf "3rdparty/"

    # Put this in its own separate directory 
    rm -Rf "../../../crossgcc"
    cd ../
    mv "${crossgccrevision}/" "crossgcc/"
    mv "crossgcc/" ../../
)

# go back to _src/coreboot/ (containing all coreboot directories)
cd "../"
# delete the gitted one (not needed anymore)
rm -Rf "coreboot/"

# Run coreboot-libre deblob scripts
# ------------------------------------------------------------------------------

printf "Deleting .git* in coreboot/ (history inside .git contains the blobs that were deleted)\n"
rm -Rf */*/.git* ../crossgcc/*.git
rm -Rf */*/3rdparty/*/.git*

# Delete crossgcc from non-crossgcc coreboot archives
# (the build system will create symlinks later when building the ROM images)
for payload in *; do
	if [ "${payload##*/}" != "crossgcc" ]; then
		rm -Rf ${payload:?}/*/util/crossgcc/
	fi
done
cd "../"

printf "Deblobbing coreboot\n"
./resources/utilities/coreboot-libre/deblob

printf "\n\n"