| #!/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 file=$1 |
| local tar=$2 |
| tar -cjvf "${tar}" -C "$(dirname "${file}")" "$(basename "${file}")" |
| } |
| |
| 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 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 |
| |
| local ap_tar="${output}/${model_uc}.${version}.tbz2" |
| local ec_tar="${output}/${model_uc}_EC.${version}.tbz2" |
| make_tar "${ap}" "${ap_tar}" |
| make_tar "${ec}" "${ec_tar}" |
| } |
| |
| 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" |
| |
| local ap_files=() |
| local ec_files=() |
| for model in "${models[@]}"; do |
| ap_files+=("image-${model}.bin") |
| ec_files+=("${model}/ec.bin") |
| done |
| |
| info "Extracting images to ${ap_temp_dir}..." |
| mkdir "${ap_temp_dir}" |
| (cd "${ap_temp_dir}" && |
| tar -xjf "${tarball}" "${ap_files[@]}" "${ec_files[@]}") |
| |
| # 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}" "${ec_files[@]}") |
| 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 "$@" |