blob: 6ed87ab1983702a64714c1694dce8f5ee38e28f1 [file] [log] [blame]
Chirayu Desai58b51ee2021-12-03 03:21:59 +05301#!/bin/bash
Chirayu Desaicc931dc2022-07-08 19:43:32 +05302
Michael Bestasd137feb2023-12-01 02:26:28 +02003# SPDX-FileCopyrightText: 2022-2023 The Calyx Institute
Chirayu Desaicc931dc2022-07-08 19:43:32 +05304#
5# SPDX-License-Identifier: Apache-2.0
6
Chirayu Desai58b51ee2021-12-03 03:21:59 +05307#
Michael Bestasd137feb2023-12-01 02:26:28 +02008# firmware:
Chirayu Desai58b51ee2021-12-03 03:21:59 +05309#
10# Setup pixel firmware
11#
12#
13##############################################################################
14
15
16### SET ###
17
18# use bash strict mode
19set -euo pipefail
20
21
22### TRAPS ###
23
24# trap signals for clean exit
25trap 'exit $?' EXIT
26trap 'error_m interrupted!' SIGINT
27
28### CONSTANTS ###
29readonly script_path="$(cd "$(dirname "$0")";pwd -P)"
Jis G Jacob4c84cfa2024-02-09 11:20:03 -050030readonly vars_path="${script_path}/../../../vendor/bliss/vars"
Michael Bestas7c70cb82022-01-13 16:52:45 +020031readonly top="${script_path}/../../.."
Chirayu Desai58b51ee2021-12-03 03:21:59 +053032
Jis G Jacob4c84cfa2024-02-09 11:20:03 -050033readonly fbpacktool="${top}/bliss/scripts/fbpacktool/fbpacktool.py"
Chirayu Desai3bbe0972022-03-13 16:06:16 +053034readonly qc_image_unpacker="${top}/prebuilts/extract-tools/linux-x86/bin/qc_image_unpacker"
Chirayu Desai58b51ee2021-12-03 03:21:59 +053035readonly extract_ota_py="${top}/tools/extract-utils/extract_ota.py"
36
37readonly work_dir="${WORK_DIR:-/tmp/pixel}"
38
Chirayu Desaidd101d62022-03-01 23:38:44 +053039source "${vars_path}/pixels"
Chirayu Desai58b51ee2021-12-03 03:21:59 +053040
41readonly device="${1}"
42source "${vars_path}/${device}"
43
Chirayu Desaib319e392023-06-21 15:56:44 +053044readonly _wifi_only="${wifi_only:-false}"
45
Chirayu Desai58b51ee2021-12-03 03:21:59 +053046readonly factory_dir="${work_dir}/${device}/${build_id}/factory/${device}-${build_id,,}"
47readonly ota_zip="${work_dir}/${device}/${build_id}/$(basename ${ota_url})"
48readonly ota_firmware_dir="${work_dir}/${device}/${build_id}/firmware"
49
Michael Bestasdf57e2f2022-01-13 17:06:01 +020050readonly vendor_path="${top}/vendor/firmware/${device}"
Chirayu Desai58b51ee2021-12-03 03:21:59 +053051
52## HELP MESSAGE (USAGE INFO)
53# TODO
54
55### FUNCTIONS ###
56
Chirayu Desai58b51ee2021-12-03 03:21:59 +053057# Unpack the seperate partitions needed for OTA
58# from the factory image's bootloader.img
59unpack_firmware() {
Michael Bestasda703f32023-06-12 12:24:26 +030060 local fbpk="${fbpk_version:-v1}"
61
Chirayu Desaib319e392023-06-21 15:56:44 +053062 if [[ "${_wifi_only}" != "true" ]]; then
63 # modem.img
64 "${qc_image_unpacker}" -i "${factory_dir}"/radio-*.img -o "${ota_firmware_dir}"
65 # Alternative: dd bs=4 skip=35
66 fi
Michael Bestasda703f32023-06-12 12:24:26 +030067
68 if [[ "$fbpk" == "v1" ]]; then
69 # All other ${firmware_partitions[@]}
70 "${qc_image_unpacker}" -i "${factory_dir}"/bootloader-*.img -o "${ota_firmware_dir}"
71 else
72 # All other ${firmware_partitions[@]}
73 python3 "${fbpacktool}" unpack -o "${ota_firmware_dir}" "${factory_dir}"/bootloader-*.img
74 fi
Chirayu Desai58b51ee2021-12-03 03:21:59 +053075}
76
77extract_firmware() {
78 echo "${ota_sha256} ${ota_zip}" | sha256sum --check --status
79 python3 ${extract_ota_py} ${ota_zip} -o "${ota_firmware_dir}" -p ${firmware_partitions[@]}
80}
81
82# Firmware included in OTAs, separate partitions
83# Can be extracted from bootloader.img inside the factory image,
84# or directly from the OTA zip
85copy_ota_firmware() {
86 for fp in ${firmware_partitions[@]}; do
Michael Bestasdf57e2f2022-01-13 17:06:01 +020087 cp "${ota_firmware_dir}/${fp}.img" "${vendor_path}/radio/${fp}.img"
Chirayu Desai58b51ee2021-12-03 03:21:59 +053088 done
89}
90
91setup_makefiles() {
Michael Bestasdf57e2f2022-01-13 17:06:01 +020092 echo "AB_OTA_PARTITIONS += \\" > "${vendor_path}/config.mk"
93 for fp in ${firmware_partitions[@]}; do
94 echo " ${fp} \\" >> "${vendor_path}/config.mk"
95 done
Chirayu Desai58b51ee2021-12-03 03:21:59 +053096
Michael Bestasdf57e2f2022-01-13 17:06:01 +020097 echo "LOCAL_PATH := \$(call my-dir)" > "${vendor_path}/firmware.mk"
98 echo >> "${vendor_path}/firmware.mk"
99 echo "ifeq (\$(TARGET_DEVICE),${device})" >> "${vendor_path}/firmware.mk"
100 for fp in ${firmware_partitions[@]}; do
101 echo "\$(call add-radio-file,radio/${fp}.img)" >> "${vendor_path}/firmware.mk"
102 done
103 echo "endif" >> "${vendor_path}/firmware.mk"
Chirayu Desai58b51ee2021-12-03 03:21:59 +0530104}
105
106# error message
107# ARG1: error message for STDERR
108# ARG2: error status
109error_m() {
110 echo "ERROR: ${1:-'failed.'}" 1>&2
111 return "${2:-1}"
112}
113
114# print help message.
115help_message() {
116 echo "${help_message:-'No help available.'}"
117}
118
119main() {
Chirayu Desai39c51552021-12-24 06:00:54 +0530120 rm -rf "${ota_firmware_dir}"
Chirayu Desai58b51ee2021-12-03 03:21:59 +0530121 mkdir -p "${ota_firmware_dir}"
Michael Bestasdf57e2f2022-01-13 17:06:01 +0200122 rm -rf "${vendor_path}/radio"
123 mkdir -p "${vendor_path}/radio"
Chirayu Desai58b51ee2021-12-03 03:21:59 +0530124
Chirayu Desai58b51ee2021-12-03 03:21:59 +0530125 # Not all devices need OTA, most are supported in image_unpacker
126 if [[ -n ${needs_ota-} ]]; then
127 extract_firmware
128 else
129 unpack_firmware
130 fi
131 copy_ota_firmware
132 setup_makefiles
133}
134
135### RUN PROGRAM ###
136
137main "${@}"
138
139
140##