blob: ed274ae539d10d856d0386522c4ed58a021ad365 [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
Mike Frysinger39bf51f2018-08-16 14:48:40 -040028
29# Parse command line.
30FLAGS "$@" || exit 1
31eval set -- "${FLAGS_ARGV}"
32set -e
33
34# Script must be run inside the chroot.
35assert_inside_chroot
36
37start_workon() {
38 local board="$1"
39 local pkg item
40 if [[ -z "$2" ]]; then
41 echo "Empty package. Skipping..."
42 return 1
43 fi
44 pkg=$(cros_workon --board="${board}" info "$2" | cut -d ' ' -f 1)
45 shift 2
46 for item in "$@"; do
47 if [[ "${pkg}" == "${item}" ]]; then
48 echo "Already workon ${pkg}. Skipping..."
49 return 1
50 fi
51 done
52 cros_workon --board="${board}" start "${pkg}"
53 return $?
54}
55
56stop_workon() {
57 cros_workon --board="$1" stop "$2"
58}
59
60update_firmware() {
61 local board="$1"
YH Lin7050ce72020-03-18 16:54:51 -070062 local project="$2"
Mike Frysinger39bf51f2018-08-16 14:48:40 -040063 local base ebuild srcuris cfg_bsp_pkg cfg_bsp_baseboard_pkg
64 local current_workon_pkg_list start_pkg result i
65
66 set -e
67
68 # query the current working package
69 mapfile -t current_workon_pkg_list < <(cros_workon --board="${board}" list)
70
71 # check if chromeos-config-bsp is a virtual package
72 cfg_bsp_pkg="chromeos-config-bsp"
73 equery-"${board}" w chromeos-base/chromeos-config-bsp > /dev/null 2>&1 \
74 || cfg_bsp_pkg="chromeos-config-bsp-${board}-private"
75 # check if chromeos-config-bsp-baseboard is in use
76 cfg_bsp_baseboard_pkg="chromeos-config-bsp-baseboard"
77 equery-"${board}" w chromeos-base/chromeos-config-bsp-baseboard \
78 > /dev/null 2>&1 || cfg_bsp_baseboard_pkg=
79
80 start_pkg=("${cfg_bsp_pkg}")
81 start_pkg+=("chromeos-firmware-${board}")
82 start_pkg+=("${cfg_bsp_baseboard_pkg}")
83
84 for i in "${!start_pkg[@]}"; do
85 result[i]=0
86 start_workon "${board}" "${start_pkg[i]}" "${current_workon_pkg_list[@]}" \
87 || result[i]=$?
88 done
89
Ned Nguyen6d6cd602019-01-22 17:17:13 -070090 overlay="${GCLIENT_ROOT}/src/private-overlays/overlay-${board}-private"
91 metadata="${overlay}/metadata"
Ned Nguyenfc67e1f2019-01-31 16:06:53 -070092 edb_cache="/var/cache/edb/dep/mnt/host/source/src/private-overlays/overlay-${board}-private"
Ned Nguyen6d6cd602019-01-22 17:17:13 -070093 base="${overlay}/chromeos-base"
Mike Frysinger39bf51f2018-08-16 14:48:40 -040094 ebuild="${base}/chromeos-firmware-${board}/chromeos-firmware-${board}-9999.ebuild"
95 srcuris="${base}/chromeos-firmware-${board}/files/srcuris"
96 yaml_config="/build/${board}/usr/share/chromeos-config/yaml/config.yaml"
97
YH Lin7050ce72020-03-18 16:54:51 -070098 # Check project config repo exists -- if so generate the config first.
YH Lin3f600de2020-04-13 10:55:06 -070099 if [[ -d "${GCLIENT_ROOT}/src/project/${project}" ]]; then
100 pushd "${GCLIENT_ROOT}/src/project/${project}" > /dev/null
101 for brd in *; do
102 if [[ -d "${brd}" && -x "${brd}/config/bin/gen_config" && -f "${brd}/config.star" ]]; then
103 pushd "${brd}" > /dev/null
104 ./config/bin/gen_config config.star
105 popd > /dev/null
106 fi
107 done
YH Lin7050ce72020-03-18 16:54:51 -0700108 popd > /dev/null
109 fi
110
Mike Frysinger39bf51f2018-08-16 14:48:40 -0400111 "emerge-${board}" ${cfg_bsp_baseboard_pkg} "${cfg_bsp_pkg}" chromeos-config
C Shapiro15ecf7a2018-11-06 12:04:38 -0700112 cros_config_host -c "${yaml_config}" get-firmware-uris > "${srcuris}"
Mike Frysinger39bf51f2018-08-16 14:48:40 -0400113 touch "${ebuild}"
YH Lin7050ce72020-03-18 16:54:51 -0700114
Ned Nguyenfc67e1f2019-01-31 16:06:53 -0700115 # Clean metadata cache to make sure SRC_URI is fetched from ${srcuris}
116 # instead from portage cache which maybe out of sync.
Ned Nguyen6d6cd602019-01-22 17:17:13 -0700117 # Note: this workaround is needed because we break the SRC_URI API contract
118 # which is supposed to be a static (see
Ned Nguyenfc67e1f2019-01-31 16:06:53 -0700119 # https://devmanual.gentoo.org/general-concepts/portage-cache/index.html).
120 # TODO(crbug.com/927917): find a different way to generate SRC_URI and remove
121 # these cache cleanup code.
122 rm -rf "${metadata}/cache" "${metadata}/md5-cache" "${edb_cache}"
Mike Frysinger39bf51f2018-08-16 14:48:40 -0400123 "ebuild-${board}" "${ebuild}" manifest
124 "emerge-${board}" "chromeos-firmware-${board}"
125
126 for i in "${!result[@]}"; do
127 if [[ "${result[i]}" -eq "0" ]]; then
128 stop_workon "${board}" "${start_pkg[i]}"
129 fi
130 done
131}
132
133main() {
134 if [[ -z "${FLAGS_board}" ]]; then
135 die "-b or --board required."
136 fi
137
YH Lin7050ce72020-03-18 16:54:51 -0700138 update_firmware "${FLAGS_board}" "${FLAGS_project}"
Mike Frysinger39bf51f2018-08-16 14:48:40 -0400139}
140
141main "$@"