blob: 22372aa19955f5b24b90aee9a5e43e4e9ac13397 [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.
99 if [ -d ${GCLIENT_ROOT}/src/project/${project}/${board} ]; then
100 pushd ${GCLIENT_ROOT}/src/project/${project}/${board} > /dev/null
101 if [[ -x ./config/bin/gen_config && -f ./config.star ]]; then
102 ./config/bin/gen_config config.star
103 fi
104 popd > /dev/null
105 fi
106
Mike Frysinger39bf51f2018-08-16 14:48:40 -0400107 "emerge-${board}" ${cfg_bsp_baseboard_pkg} "${cfg_bsp_pkg}" chromeos-config
C Shapiro15ecf7a2018-11-06 12:04:38 -0700108 cros_config_host -c "${yaml_config}" get-firmware-uris > "${srcuris}"
Mike Frysinger39bf51f2018-08-16 14:48:40 -0400109 touch "${ebuild}"
YH Lin7050ce72020-03-18 16:54:51 -0700110
Ned Nguyenfc67e1f2019-01-31 16:06:53 -0700111 # Clean metadata cache to make sure SRC_URI is fetched from ${srcuris}
112 # instead from portage cache which maybe out of sync.
Ned Nguyen6d6cd602019-01-22 17:17:13 -0700113 # Note: this workaround is needed because we break the SRC_URI API contract
114 # which is supposed to be a static (see
Ned Nguyenfc67e1f2019-01-31 16:06:53 -0700115 # https://devmanual.gentoo.org/general-concepts/portage-cache/index.html).
116 # TODO(crbug.com/927917): find a different way to generate SRC_URI and remove
117 # these cache cleanup code.
118 rm -rf "${metadata}/cache" "${metadata}/md5-cache" "${edb_cache}"
Mike Frysinger39bf51f2018-08-16 14:48:40 -0400119 "ebuild-${board}" "${ebuild}" manifest
120 "emerge-${board}" "chromeos-firmware-${board}"
121
122 for i in "${!result[@]}"; do
123 if [[ "${result[i]}" -eq "0" ]]; then
124 stop_workon "${board}" "${start_pkg[i]}"
125 fi
126 done
127}
128
129main() {
130 if [[ -z "${FLAGS_board}" ]]; then
131 die "-b or --board required."
132 fi
133
YH Lin7050ce72020-03-18 16:54:51 -0700134 update_firmware "${FLAGS_board}" "${FLAGS_project}"
Mike Frysinger39bf51f2018-08-16 14:48:40 -0400135}
136
137main "$@"