blob: 8aaa5cc042b2efc3c4300bf592e22268b10e312b [file] [log] [blame]
Mike Frysinger39bf51f2018-08-16 14:48:40 -04001#!/bin/bash
2# Copyright 2017 The Chromium OS Authors. All rights reserved.
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5
6# Loads script libraries.
7CONTRIB_DIR=$(dirname "$(readlink -f "$0")")
8. "${CONTRIB_DIR}/common.sh" || exit 1
9
10FLAGS_HELP="
11Command to rebuild firmware after a config change.
12
13After you make a change to the firmware version in the master configuration,
14run this script to test it. It will rebuild the master configuration, update
15the Manifest and build the new firmware. You can then submit your CL to update
16the master configuration, safe in the knowledge that you have tested it.
17
18Usage:
YH Lin7050ce72020-03-18 16:54:51 -070019 cros_update_firmware <board> [project]
Mike Frysinger39bf51f2018-08-16 14:48:40 -040020
21where <board> is the board whose firmware needs updating (e.g. 'coral').
YH Lin7050ce72020-03-18 16:54:51 -070022 [project] is the project the board belongs to.
Mike Frysinger39bf51f2018-08-16 14:48:40 -040023"
24
25# Flags
26DEFINE_string board "${DEFAULT_BOARD}" "Which board the firmware is for" b
YH Lin7050ce72020-03-18 16:54:51 -070027DEFINE_string project "${DEFAULT_PROJECT}" "Which project this board is for" p
Andrew McRae3fccc992020-06-18 14:49:33 +100028DEFINE_boolean quiet "${FLAGS_FALSE}" "Reduce emerge build output" q
Mike Frysinger39bf51f2018-08-16 14:48:40 -040029
30# Parse command line.
31FLAGS "$@" || exit 1
32eval set -- "${FLAGS_ARGV}"
33set -e
34
35# Script must be run inside the chroot.
36assert_inside_chroot
37
38start_workon() {
39 local board="$1"
40 local pkg item
41 if [[ -z "$2" ]]; then
42 echo "Empty package. Skipping..."
43 return 1
44 fi
Andrew McRaed26b2792020-08-06 23:28:26 +100045 pkg=$(cros workon --board="${board}" info "$2" | cut -d ' ' -f 1)
Mike Frysinger39bf51f2018-08-16 14:48:40 -040046 shift 2
47 for item in "$@"; do
48 if [[ "${pkg}" == "${item}" ]]; then
49 echo "Already workon ${pkg}. Skipping..."
50 return 1
51 fi
52 done
Andrew McRaed26b2792020-08-06 23:28:26 +100053 cros workon --board="${board}" start "${pkg}"
Mike Frysinger39bf51f2018-08-16 14:48:40 -040054 return $?
55}
56
57stop_workon() {
Andrew McRaed26b2792020-08-06 23:28:26 +100058 cros workon --board="$1" stop "$2"
Mike Frysinger39bf51f2018-08-16 14:48:40 -040059}
60
61update_firmware() {
62 local board="$1"
YH Lin7050ce72020-03-18 16:54:51 -070063 local project="$2"
Mike Frysinger39bf51f2018-08-16 14:48:40 -040064 local base ebuild srcuris cfg_bsp_pkg cfg_bsp_baseboard_pkg
Andrew McRae3fccc992020-06-18 14:49:33 +100065 local current_workon_pkg_list start_pkg result i quiet
66 [[ "$3" == "${FLAGS_TRUE}" ]] && quiet="--quiet-build"
Mike Frysinger39bf51f2018-08-16 14:48:40 -040067
68 set -e
69
70 # query the current working package
Andrew McRaed26b2792020-08-06 23:28:26 +100071 mapfile -t current_workon_pkg_list < <(cros workon --board="${board}" list)
Mike Frysinger39bf51f2018-08-16 14:48:40 -040072
73 # check if chromeos-config-bsp is a virtual package
74 cfg_bsp_pkg="chromeos-config-bsp"
75 equery-"${board}" w chromeos-base/chromeos-config-bsp > /dev/null 2>&1 \
76 || cfg_bsp_pkg="chromeos-config-bsp-${board}-private"
77 # check if chromeos-config-bsp-baseboard is in use
78 cfg_bsp_baseboard_pkg="chromeos-config-bsp-baseboard"
79 equery-"${board}" w chromeos-base/chromeos-config-bsp-baseboard \
80 > /dev/null 2>&1 || cfg_bsp_baseboard_pkg=
81
82 start_pkg=("${cfg_bsp_pkg}")
83 start_pkg+=("chromeos-firmware-${board}")
84 start_pkg+=("${cfg_bsp_baseboard_pkg}")
85
86 for i in "${!start_pkg[@]}"; do
87 result[i]=0
88 start_workon "${board}" "${start_pkg[i]}" "${current_workon_pkg_list[@]}" \
89 || result[i]=$?
90 done
91
Ned Nguyen6d6cd602019-01-22 17:17:13 -070092 overlay="${GCLIENT_ROOT}/src/private-overlays/overlay-${board}-private"
93 metadata="${overlay}/metadata"
Ned Nguyenfc67e1f2019-01-31 16:06:53 -070094 edb_cache="/var/cache/edb/dep/mnt/host/source/src/private-overlays/overlay-${board}-private"
Ned Nguyen6d6cd602019-01-22 17:17:13 -070095 base="${overlay}/chromeos-base"
Mike Frysinger39bf51f2018-08-16 14:48:40 -040096 ebuild="${base}/chromeos-firmware-${board}/chromeos-firmware-${board}-9999.ebuild"
97 srcuris="${base}/chromeos-firmware-${board}/files/srcuris"
98 yaml_config="/build/${board}/usr/share/chromeos-config/yaml/config.yaml"
99
YH Lin7050ce72020-03-18 16:54:51 -0700100 # Check project config repo exists -- if so generate the config first.
YH Lin3f600de2020-04-13 10:55:06 -0700101 if [[ -d "${GCLIENT_ROOT}/src/project/${project}" ]]; then
102 pushd "${GCLIENT_ROOT}/src/project/${project}" > /dev/null
103 for brd in *; do
104 if [[ -d "${brd}" && -x "${brd}/config/bin/gen_config" && -f "${brd}/config.star" ]]; then
105 pushd "${brd}" > /dev/null
106 ./config/bin/gen_config config.star
107 popd > /dev/null
108 fi
109 done
YH Lin7050ce72020-03-18 16:54:51 -0700110 popd > /dev/null
111 fi
112
Andrew McRaebf312632020-07-14 17:44:28 +1000113 "emerge-${board}" -j ${quiet} ${cfg_bsp_baseboard_pkg} "${cfg_bsp_pkg}" chromeos-config
C Shapiro15ecf7a2018-11-06 12:04:38 -0700114 cros_config_host -c "${yaml_config}" get-firmware-uris > "${srcuris}"
Mike Frysinger39bf51f2018-08-16 14:48:40 -0400115 touch "${ebuild}"
YH Lin7050ce72020-03-18 16:54:51 -0700116
Ned Nguyenfc67e1f2019-01-31 16:06:53 -0700117 # Clean metadata cache to make sure SRC_URI is fetched from ${srcuris}
118 # instead from portage cache which maybe out of sync.
Ned Nguyen6d6cd602019-01-22 17:17:13 -0700119 # Note: this workaround is needed because we break the SRC_URI API contract
120 # which is supposed to be a static (see
Ned Nguyenfc67e1f2019-01-31 16:06:53 -0700121 # https://devmanual.gentoo.org/general-concepts/portage-cache/index.html).
122 # TODO(crbug.com/927917): find a different way to generate SRC_URI and remove
123 # these cache cleanup code.
124 rm -rf "${metadata}/cache" "${metadata}/md5-cache" "${edb_cache}"
Mike Frysinger39bf51f2018-08-16 14:48:40 -0400125 "ebuild-${board}" "${ebuild}" manifest
Andrew McRaebf312632020-07-14 17:44:28 +1000126 "emerge-${board}" -j ${quiet} "chromeos-firmware-${board}"
Mike Frysinger39bf51f2018-08-16 14:48:40 -0400127
128 for i in "${!result[@]}"; do
129 if [[ "${result[i]}" -eq "0" ]]; then
130 stop_workon "${board}" "${start_pkg[i]}"
131 fi
132 done
133}
134
135main() {
136 if [[ -z "${FLAGS_board}" ]]; then
137 die "-b or --board required."
138 fi
139
Andrew McRae3fccc992020-06-18 14:49:33 +1000140 update_firmware "${FLAGS_board}" "${FLAGS_project}" "${FLAGS_quiet}"
Mike Frysinger39bf51f2018-08-16 14:48:40 -0400141}
142
143main "$@"