-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild_uboot.sh
More file actions
197 lines (171 loc) · 6.48 KB
/
Copy pathbuild_uboot.sh
File metadata and controls
197 lines (171 loc) · 6.48 KB
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
#!/bin/bash
# Build U-Boot for Spacemit K1
set -e
set -u
set -o pipefail
# Use BASH_SOURCE[0] (not $0) so path resolution works even when sourced
# after a pushd has changed the working directory.
SRC=$(dirname "$(readlink -e "${BASH_SOURCE[0]}")")
if ! type -t config_value &>/dev/null; then
source "${SRC}/utils.sh"
source "${SRC}/secure.sh"
fi
function build_uboot {
local config="$1"
local clean="${2:-false}"
local mode="${3:-release}"
local out_dir=$(out_dir "${config}" "${mode}")
display_current_build "${config}" "uboot" "${mode}"
# Get config values
local defconfig=$(config_value "${config}" uboot.defconfig)
local defconfig_fragments=$(config_value "${config}" uboot.defconfig_fragments)
local opensbi_path="${out_dir}/fw_dynamic-${mode}.bin"
# Default defconfig for Spacemit K1
[ -z "${defconfig}" ] && defconfig="k1_defconfig"
# Setup environment
clear_vars
riscv64_env
mkdir -p "${out_dir}"
pushd "${UBOOT_DIR}"
# Clean if requested
if [[ "${clean}" == true ]]; then
make distclean || true
fi
# Configure U-Boot
make "${defconfig}"
# Apply defconfig fragments if specified
local has_android_fragment=false
if [ -n "${defconfig_fragments}" ]; then
for fragment in ${defconfig_fragments}; do
local fragment_path="${SRC}/config/defconfig_fragment/${fragment}"
if [ -f "${fragment_path}" ]; then
echo "Applying defconfig fragment: ${fragment}"
./scripts/kconfig/merge_config.sh -m .config "${fragment_path}"
if [[ "${fragment}" == *android* ]]; then
has_android_fragment=true
fi
else
warning "Defconfig fragment not found: ${fragment_path}"
fi
done
make olddefconfig
fi
# For Android builds: override the board .env bootcmd
# The board k1-x.env sets bootcmd=run autoboot (legacy Linux boot).
# Android needs bootcmd=bootflow scan -lb (BOOTSTD Android bootmeth).
if [ "${has_android_fragment}" = true ]; then
local board_env="${UBOOT_DIR}/board/spacemit/k1-x/k1-x.env"
local android_env="${SRC}/config/env_android.txt"
if [ -f "${board_env}" ] && [ -f "${android_env}" ]; then
echo "Patching board env for Android boot..."
# Save original if not already saved
if [ ! -f "${board_env}.orig" ]; then
cp -f "${board_env}" "${board_env}.orig"
fi
# Restore original before patching (idempotent)
cp -f "${board_env}.orig" "${board_env}"
# Replace bootcmd with Android bootflow
sed -i 's|^bootcmd=.*|bootcmd=bootflow scan -lb|' "${board_env}"
# Append Android-specific env vars
cat >> "${board_env}" << 'ANDROID_ENV'
// Android boot override (auto-generated by build_uboot.sh)
bootmeths=android
boot_targets=mmc2
mmcdev=2
android_slot_suffix=_a
android_boot_mode=normal
android_boot_addr=0x20000000
android_vendor_boot_addr=0x24000000
android_init_boot_addr=0x28000000
vendor_boot_comp_addr_r=0x50000000
init_boot_comp_addr_r=0x54000000
fastboot_flash_mmc_dev=2
fastboot.partition-type:metadata=f2fs
avb_state=unlocked
bootdelay=1
ANDROID_ENV
echo "Board env patched for Android"
fi
# Rebuild with patched env
make olddefconfig
fi
# Mode-specific configuration
if [[ "${mode}" == "factory" ]]; then
# Apply factory mode config if exists
local factory_config="${SRC}/config/defconfig_fragment/uboot-factory.config"
if [ -f "${factory_config}" ]; then
echo "Applying factory config"
./scripts/kconfig/merge_config.sh -m .config "${factory_config}"
make olddefconfig
fi
# Inject AVB public key for factory mode
local avb_pub_key=$(get_avb_key_path "${config}" "pub")
if [ -n "${avb_pub_key}" ] && [ -f "${avb_pub_key}" ]; then
echo "Injecting AVB public key: ${avb_pub_key}"
# AVB key injection would go here
fi
fi
# Set OpenSBI path if available
local opensbi_flag=""
if [ -f "${opensbi_path}" ]; then
opensbi_flag="OPENSBI=${opensbi_path}"
elif [ -f "${OPENSBI_DIR}/build/platform/generic/firmware/fw_dynamic.bin" ]; then
opensbi_flag="OPENSBI=${OPENSBI_DIR}/build/platform/generic/firmware/fw_dynamic.bin"
fi
# Build U-Boot
make ${opensbi_flag} -j$(nproc)
# Restore original board env if it was patched
local board_env="${UBOOT_DIR}/board/spacemit/k1-x/k1-x.env"
if [ -f "${board_env}.orig" ]; then
cp -f "${board_env}.orig" "${board_env}"
echo "Board env restored to original"
fi
# Copy outputs
if [ -f "u-boot.itb" ]; then
cp "u-boot.itb" "${out_dir}/u-boot-${mode}.itb"
echo "U-Boot ITB built: ${out_dir}/u-boot-${mode}.itb"
elif [ -f "u-boot.bin" ]; then
cp "u-boot.bin" "${out_dir}/u-boot-${mode}.bin"
echo "U-Boot BIN built: ${out_dir}/u-boot-${mode}.bin"
else
error_exit "U-Boot build failed - no output found"
fi
# Copy SPL if generated
if [ -f "spl/u-boot-spl.bin" ]; then
cp "spl/u-boot-spl.bin" "${out_dir}/u-boot-spl-${mode}.bin"
echo "U-Boot SPL built: ${out_dir}/u-boot-spl-${mode}.bin"
fi
# Generate environment binary
if [ -f "u-boot-env-default.bin" ]; then
cp "u-boot-env-default.bin" "${out_dir}/env-${mode}.bin"
fi
# Copy device tree if available
if [ -f "u-boot.dtb" ]; then
cp "u-boot.dtb" "${out_dir}/u-boot-${mode}.dtb"
fi
# Verify Android boot configs if Android fragment was applied
if [ "${has_android_fragment}" = true ]; then
echo "Verifying Android boot configuration..."
local verify_ok=true
for cfg in CONFIG_BOOTSTD CONFIG_BOOTMETH_ANDROID CONFIG_CMD_BCB CONFIG_ANDROID_AB; do
if ! grep -q "^${cfg}=y" .config; then
warning "${cfg} is NOT enabled in .config"
verify_ok=false
fi
done
if ! grep -q 'CONFIG_BOOTCOMMAND="bootflow scan' .config; then
warning "CONFIG_BOOTCOMMAND is not set to bootflow scan"
verify_ok=false
fi
if [ "${verify_ok}" = true ]; then
echo "Android boot configuration: OK"
else
warning "Android boot configuration may be incomplete"
fi
fi
popd
clear_vars
}
if [ "$0" = "$BASH_SOURCE" ]; then
main "$@"
fi