| #!/bin/bash | |
| # Copyright 2023 The ChromiumOS Authors | |
| # Use of this source code is governed by a BSD-style license that can be | |
| # found in the LICENSE file. | |
| # shellcheck disable=SC1091 | |
| CONTRIB_DIR="$(dirname "$(readlink -f "$0")")/.." | |
| . "${CONTRIB_DIR}/common.sh" || exit 1 | |
| FLAGS_HELP=" | |
| Repack the official firmware tarball (such as ChromeOS-firmware-R109-15194.10.0-corsola.tar.bz2) | |
| for uploading to Binary Component Server (BCS). | |
| Usage: $0 [flags] tarball | |
| tarball: The firmware tarball to be repacked" | |
| SWAP_EC_RW="$(dirname "$0")/swap_ec_rw" | |
| # Flags. | |
| DEFINE_string model "" "Comma-separated list of models (variants)" m | |
| DEFINE_string output "." "Output directory" o | |
| DEFINE_string ecrw "" "The EC firmware tarball to be swapped into AP firmware" | |
| # Parse command line. | |
| FLAGS "$@" || exit 1 | |
| eval set -- "${FLAGS_ARGV}" | |
| # Only after this point should you enable `set -e` as shflags does not work | |
| # when that is turned on first. | |
| set -e | |
| make_tar() { | |
| local tar=$1 | |
| local dir=$2 | |
| local files=("${@:3}") | |
| tar -cjvf "${tar}" -C "${dir}" "${files[@]}" | |
| } | |
| make_fw_tar() { | |
| local fw_dir=$1 | |
| local ecrw_dir=$2 | |
| local model=$3 | |
| info "Packing ${model} firmware..." | |
| local output=$4 | |
| local ap="${fw_dir}/image-${model}.bin" | |
| local ec="${fw_dir}/${model}/ec.bin" | |
| local ec_manifest="${fw_dir}/${model}/component_manifest.json" | |
| local ecrw="${ecrw_dir}/${model}/ec.bin" | |
| local model_uc="${model^}" | |
| local version | |
| version="$(futility update --manifest -i "${ap}" | | |
| jq -r -c .default.host.versions.ro)" | |
| version="$(cut -d "." -f2- <<< "${version}")" | |
| # Swap ecrw in AP | |
| if [[ -n "${ecrw_dir}" ]]; then | |
| local ecrw_version | |
| ecrw_version="$(futility update --manifest -e "${ecrw}" | | |
| jq -r -c .default.ec.versions.rw)" | |
| info "Swapping ecrw with version ${ecrw_version}" | |
| "${SWAP_EC_RW}" --image "${ap}" --ec "${ecrw}" > /dev/null | |
| fi | |
| # Make AP tar | |
| local ap_tar="${output}/${model_uc}.${version}.tbz2" | |
| local ec_tar="${output}/${model_uc}_EC.${version}.tbz2" | |
| make_tar "${ap_tar}" "$(dirname "${ap}")" "$(basename "${ap}")" | |
| # Make EC tar | |
| local ec_files=("$(basename "${ec}")") | |
| if [ -f "${ec_manifest}" ]; then | |
| ec_files+=("$(basename "${ec_manifest}")") | |
| else | |
| warn "$(basename "${ec_manifest}") not found for ${model}" | |
| fi | |
| make_tar "${ec_tar}" "$(dirname "${ec}")" "${ec_files[@]}" | |
| } | |
| main() { | |
| assert_inside_chroot | |
| if [ $# -eq 0 ]; then | |
| flags_help | |
| die "tarball missing" | |
| fi | |
| local tarball | |
| tarball=$(readlink -f "$1") | |
| if [[ -z "${FLAGS_model}" ]]; then | |
| flags_help | |
| die "-m or --model required" | |
| fi | |
| IFS=',' read -ra models <<< "${FLAGS_model}" | |
| # Extract from tarball | |
| local temp_dir | |
| temp_dir=$(mktemp -d) | |
| local ap_temp_dir="${temp_dir}/ap" | |
| # Files such as component_manifest.json may not exist in the tarball, | |
| # so we cannot pass the file name to tar as "tar -xjf ${tarball} FILES". | |
| # Instead, we exclude a few patterns to save some space. | |
| local excluded_files=( | |
| "cbfs-ro-compress/*" | |
| "*/coreboot*/*" | |
| "*/depthcharge/*" | |
| "*/libpayload*/*" | |
| ) | |
| local opts=() | |
| for file in "${excluded_files[@]}"; do | |
| opts+=("--exclude" "${file}") | |
| done | |
| info "Extracting images to ${ap_temp_dir}..." | |
| mkdir "${ap_temp_dir}" | |
| (cd "${ap_temp_dir}" && tar -xjf "${tarball}" "${opts[@]}") | |
| # Extract from EC tarball | |
| local ecrw_temp_dir | |
| local ecrw_tarball | |
| if [[ -n "${FLAGS_ecrw}" ]]; then | |
| ecrw_temp_dir="${temp_dir}/ecrw" | |
| info "Extracting EC images to ${ecrw_temp_dir}..." | |
| mkdir "${ecrw_temp_dir}" | |
| ecrw_tarball=$(readlink -f "${FLAGS_ecrw}") | |
| (cd "${ecrw_temp_dir}" && tar -xjf "${ecrw_tarball}" "${opts[@]}") | |
| fi | |
| for model in "${models[@]}"; do | |
| make_fw_tar "${ap_temp_dir}" "${ecrw_temp_dir}" "${model}" "${FLAGS_output}" | |
| done | |
| info "Output files saved to ${FLAGS_output}/" | |
| info "Done" | |
| } | |
| main "$@" |