blob: e683727ed75a30d8226187aa2cbfb8b640cb3a16 [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
Andrew Lambfeacb4c2020-09-23 15:00:09 -060029DEFINE_boolean append_srcuris "${FLAGS_TRUE}" "Only applicable when the project
30flag is set. Append firmware uris to srcuris instead of overwriting. This is
31usually needed if you have limited access to project config repos, so
32overwriting would delete some uris."
Mike Frysinger39bf51f2018-08-16 14:48:40 -040033
34# Parse command line.
35FLAGS "$@" || exit 1
36eval set -- "${FLAGS_ARGV}"
37set -e
38
39# Script must be run inside the chroot.
40assert_inside_chroot
41
42start_workon() {
43 local board="$1"
44 local pkg item
45 if [[ -z "$2" ]]; then
46 echo "Empty package. Skipping..."
47 return 1
48 fi
Andrew McRaed26b2792020-08-06 23:28:26 +100049 pkg=$(cros workon --board="${board}" info "$2" | cut -d ' ' -f 1)
Mike Frysinger39bf51f2018-08-16 14:48:40 -040050 shift 2
51 for item in "$@"; do
52 if [[ "${pkg}" == "${item}" ]]; then
53 echo "Already workon ${pkg}. Skipping..."
54 return 1
55 fi
56 done
Andrew McRaed26b2792020-08-06 23:28:26 +100057 cros workon --board="${board}" start "${pkg}"
Mike Frysinger39bf51f2018-08-16 14:48:40 -040058 return $?
59}
60
61stop_workon() {
Andrew McRaed26b2792020-08-06 23:28:26 +100062 cros workon --board="$1" stop "$2"
Mike Frysinger39bf51f2018-08-16 14:48:40 -040063}
64
65update_firmware() {
66 local board="$1"
YH Lin7050ce72020-03-18 16:54:51 -070067 local project="$2"
Mike Frysinger39bf51f2018-08-16 14:48:40 -040068 local base ebuild srcuris cfg_bsp_pkg cfg_bsp_baseboard_pkg
Andrew McRae3fccc992020-06-18 14:49:33 +100069 local current_workon_pkg_list start_pkg result i quiet
70 [[ "$3" == "${FLAGS_TRUE}" ]] && quiet="--quiet-build"
Andrew Lambfeacb4c2020-09-23 15:00:09 -060071 local append_srcuris="$4"
Mike Frysinger39bf51f2018-08-16 14:48:40 -040072
73 set -e
74
75 # query the current working package
Andrew McRaed26b2792020-08-06 23:28:26 +100076 mapfile -t current_workon_pkg_list < <(cros workon --board="${board}" list)
Mike Frysinger39bf51f2018-08-16 14:48:40 -040077
78 # check if chromeos-config-bsp is a virtual package
79 cfg_bsp_pkg="chromeos-config-bsp"
80 equery-"${board}" w chromeos-base/chromeos-config-bsp > /dev/null 2>&1 \
81 || cfg_bsp_pkg="chromeos-config-bsp-${board}-private"
82 # check if chromeos-config-bsp-baseboard is in use
83 cfg_bsp_baseboard_pkg="chromeos-config-bsp-baseboard"
84 equery-"${board}" w chromeos-base/chromeos-config-bsp-baseboard \
85 > /dev/null 2>&1 || cfg_bsp_baseboard_pkg=
86
87 start_pkg=("${cfg_bsp_pkg}")
88 start_pkg+=("chromeos-firmware-${board}")
89 start_pkg+=("${cfg_bsp_baseboard_pkg}")
90
91 for i in "${!start_pkg[@]}"; do
92 result[i]=0
93 start_workon "${board}" "${start_pkg[i]}" "${current_workon_pkg_list[@]}" \
94 || result[i]=$?
95 done
96
Ned Nguyen6d6cd602019-01-22 17:17:13 -070097 overlay="${GCLIENT_ROOT}/src/private-overlays/overlay-${board}-private"
98 metadata="${overlay}/metadata"
Ned Nguyenfc67e1f2019-01-31 16:06:53 -070099 edb_cache="/var/cache/edb/dep/mnt/host/source/src/private-overlays/overlay-${board}-private"
Ned Nguyen6d6cd602019-01-22 17:17:13 -0700100 base="${overlay}/chromeos-base"
Mike Frysinger39bf51f2018-08-16 14:48:40 -0400101 ebuild="${base}/chromeos-firmware-${board}/chromeos-firmware-${board}-9999.ebuild"
102 srcuris="${base}/chromeos-firmware-${board}/files/srcuris"
103 yaml_config="/build/${board}/usr/share/chromeos-config/yaml/config.yaml"
104
YH Lin7050ce72020-03-18 16:54:51 -0700105 # Check project config repo exists -- if so generate the config first.
YH Lin3f600de2020-04-13 10:55:06 -0700106 if [[ -d "${GCLIENT_ROOT}/src/project/${project}" ]]; then
107 pushd "${GCLIENT_ROOT}/src/project/${project}" > /dev/null
108 for brd in *; do
109 if [[ -d "${brd}" && -x "${brd}/config/bin/gen_config" && -f "${brd}/config.star" ]]; then
110 pushd "${brd}" > /dev/null
111 ./config/bin/gen_config config.star
112 popd > /dev/null
113 fi
114 done
YH Lin7050ce72020-03-18 16:54:51 -0700115 popd > /dev/null
116 fi
117
Andrew McRaebf312632020-07-14 17:44:28 +1000118 "emerge-${board}" -j ${quiet} ${cfg_bsp_baseboard_pkg} "${cfg_bsp_pkg}" chromeos-config
Andrew Lambfeacb4c2020-09-23 15:00:09 -0600119
120 # If the append_srcuris and project flags are set, append uris that aren't
121 # already in srcuris. Otherwise, overwrite srcuris.
122 if [[ -n "${project}" && "${append_srcuris}" == "${FLAGS_TRUE}" ]]; then
123 for uri in $(cros_config_host -c "${yaml_config}" get-firmware-uris); do
124 if ! grep -q "${uri}" "${srcuris}"; then
125 # Append uri to the last line of srcuris (srcuris should be a file with
126 # a single line). Escape slashes in uri.
127 sed -i "$ s/$/ ${uri//\//\\/}/" "${srcuris}"
128 fi
129 done
130 else
131 cros_config_host -c "${yaml_config}" get-firmware-uris > "${srcuris}"
132 fi
133
Mike Frysinger39bf51f2018-08-16 14:48:40 -0400134 touch "${ebuild}"
YH Lin7050ce72020-03-18 16:54:51 -0700135
Ned Nguyenfc67e1f2019-01-31 16:06:53 -0700136 # Clean metadata cache to make sure SRC_URI is fetched from ${srcuris}
137 # instead from portage cache which maybe out of sync.
Ned Nguyen6d6cd602019-01-22 17:17:13 -0700138 # Note: this workaround is needed because we break the SRC_URI API contract
139 # which is supposed to be a static (see
Ned Nguyenfc67e1f2019-01-31 16:06:53 -0700140 # https://devmanual.gentoo.org/general-concepts/portage-cache/index.html).
141 # TODO(crbug.com/927917): find a different way to generate SRC_URI and remove
142 # these cache cleanup code.
143 rm -rf "${metadata}/cache" "${metadata}/md5-cache" "${edb_cache}"
Mike Frysinger39bf51f2018-08-16 14:48:40 -0400144 "ebuild-${board}" "${ebuild}" manifest
Andrew McRaebf312632020-07-14 17:44:28 +1000145 "emerge-${board}" -j ${quiet} "chromeos-firmware-${board}"
Mike Frysinger39bf51f2018-08-16 14:48:40 -0400146
147 for i in "${!result[@]}"; do
148 if [[ "${result[i]}" -eq "0" ]]; then
149 stop_workon "${board}" "${start_pkg[i]}"
150 fi
151 done
152}
153
154main() {
155 if [[ -z "${FLAGS_board}" ]]; then
156 die "-b or --board required."
157 fi
158
Andrew Lambfeacb4c2020-09-23 15:00:09 -0600159 update_firmware "${FLAGS_board}" "${FLAGS_project}" "${FLAGS_quiet}" "${FLAGS_append_srcuris}"
Mike Frysinger39bf51f2018-08-16 14:48:40 -0400160}
161
162main "$@"