xref: /aosp_15_r20/external/coreboot/payloads/external/tint/generate_core.sh (revision b9411a12aaaa7e1e6a6fb7c5e057f44ee179a49c)
1#!/bin/sh
2#
3# generate_core.sh extracts the core part from buildgcc script,
4# most importantly the checksum calculation/verification functions.
5#
6# Copyright (C) 2019 Mike Banon <[email protected]>
7#
8##############################################################################
9#
10# USAGE:
11# 	./generate_core.sh <buildgcc> <corescript> prepare_before_patch
12# where
13# 	buildgcc   - path to input buildgcc script
14# 	corescript - path to output core part script
15# 	prepare_before_patch - optional argument to insert prepare_${package}
16# 				call into the unpack_and_patch function, e.g.
17# 				for removing some files with rm command
18# 				in order to reduce the size of patch file
19#
20##############################################################################
21
22buildgcc="$1"
23corescript="$2"
24prepare_before_patch="$3"
25
26#
27# Imports the source file fragment between start and end into the
28# destination file, optionally excluding the last line if not needed
29#
30
31import_from_file() {
32        source="$1"
33        destination="$2"
34	start="$3"
35	end="$4"
36	last_line_disabled="$5"
37	if [ -z "${last_line_disabled}" ] ; then
38		sed -n "/^${start}/,/^${end}/{/^${start}/{p;n};{p}}" "$source" >> "$destination"
39	else
40		sed -n "/^${start}/,/^${end}/{/^${start}/{p;n};/^${end}/{q};{p}}" "$source" >> "$destination"
41	fi
42}
43
44#
45# Import the color defines together with UNAME/HALT_FOR_TOOLS variables
46#
47
48import_from_file "$buildgcc" "$corescript" "red=" "HALT_FOR_TOOLS=0" || exit "$?"
49
50#
51# Import the core functions
52#
53
54FUNCTIONS="please_install searchtool download compute_hash error_hash_mismatch verify_hash unpack_and_patch"
55
56for F in $FUNCTIONS ; do
57	import_from_file "$buildgcc" "$corescript" "$F()" "}" || exit "$?"
58done
59
60#
61# Import a fragment where we find tar/patch/make and other essential tools
62#
63
64import_from_file "$buildgcc" "$corescript" "# Find all the required tools" "# Allow" "last_line_disabled" || exit "$?"
65
66#
67# Import a fragment with conditional exit if some required tools were not found
68#
69
70import_from_file "$buildgcc" "$corescript" "if \[ \"\$HALT_FOR_TOOLS" "fi" || exit "$?"
71
72#
73# Avoid the unnecessary subdirectories holding a single file each
74#
75
76sed -i -e "s/patches\///g" "$corescript"
77sed -i -e "s/sum\///g" "$corescript"
78sed -i -e "s/tarballs\///g" "$corescript"
79sed -i -e "s/cd tarballs || exit 1//g" "$corescript"
80sed -i -e "s/cd \.\.//g" "$corescript"
81
82#
83# Get the known checksum without using a dedicated single-line file
84#
85
86sed -i -e "s/\tknown_hash=\"\$(get_known_hash.*/\tknown_hash=\"\$2\"/g" "$corescript"
87
88#
89# Update the paths printed at the error messages
90#
91
92sed -i -e "s/util\/crossgcc\///g" "$corescript"
93
94#
95# Insert prepare_${package} function call between the unpack and patch operations
96#
97
98if [ ! -z "${prepare_before_patch}" ] ; then
99	sed -i -e "/\$TAR \$FLAGS \"\$(basename \"\$archive\")\"/a prepare_\${package} || exit \"\$?\"" "$corescript"
100	sed -i -e "/\$TAR \$FLAGS \"\$archive\"/a prepare_\${package} || exit \"\$?\"" "$corescript"
101fi
102
103#
104