Jett Rink | 17ed0f5 | 2020-09-25 17:14:31 -0600 | [diff] [blame] | 1 | # -*- coding: utf-8 -*- |
| 2 | # Copyright 2020 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. |
LaMont Jones | ed9a5de | 2021-02-03 11:06:12 -0700 | [diff] [blame] | 5 | |
Jett Rink | 17ed0f5 | 2020-09-25 17:14:31 -0600 | [diff] [blame] | 6 | """Firmware builder controller. |
| 7 | |
LaMont Jones | dec69ad | 2021-01-27 13:02:00 -0700 | [diff] [blame] | 8 | Handle all firmware builder related functionality. Currently no service module |
| 9 | exists: all of the work is done here. |
Jett Rink | 17ed0f5 | 2020-09-25 17:14:31 -0600 | [diff] [blame] | 10 | """ |
| 11 | |
| 12 | import os |
| 13 | import tempfile |
| 14 | |
| 15 | from google.protobuf import json_format |
| 16 | |
| 17 | from chromite.api import controller |
| 18 | from chromite.api import faux |
| 19 | from chromite.api import validate |
| 20 | from chromite.api.gen.chromite.api import firmware_pb2 |
LaMont Jones | dec69ad | 2021-01-27 13:02:00 -0700 | [diff] [blame] | 21 | from chromite.api.gen.chromiumos import common_pb2 |
Jett Rink | 17ed0f5 | 2020-09-25 17:14:31 -0600 | [diff] [blame] | 22 | from chromite.lib import constants |
| 23 | from chromite.lib import cros_build_lib |
LaMont Jones | ed9a5de | 2021-02-03 11:06:12 -0700 | [diff] [blame] | 24 | from chromite.lib import osutils |
| 25 | |
Jett Rink | 17ed0f5 | 2020-09-25 17:14:31 -0600 | [diff] [blame] | 26 | |
LaMont Jones | 4bab978 | 2021-02-17 22:52:44 +0000 | [diff] [blame] | 27 | def _call_entry(fw_loc, metric_proto, subcmd, **kwargs): |
Jett Rink | 17ed0f5 | 2020-09-25 17:14:31 -0600 | [diff] [blame] | 28 | """Calls into firmware_builder.py with the specified subcmd.""" |
| 29 | |
LaMont Jones | dec69ad | 2021-01-27 13:02:00 -0700 | [diff] [blame] | 30 | if fw_loc == common_pb2.PLATFORM_EC: |
Jett Rink | 17ed0f5 | 2020-09-25 17:14:31 -0600 | [diff] [blame] | 31 | fw_path = 'src/platform/ec/' |
LaMont Jones | dec69ad | 2021-01-27 13:02:00 -0700 | [diff] [blame] | 32 | elif fw_loc == common_pb2.PLATFORM_ZEPHYR: |
Jett Rink | 17ed0f5 | 2020-09-25 17:14:31 -0600 | [diff] [blame] | 33 | fw_path = 'src/platform/zephyr-chrome/' |
LaMont Jones | dec69ad | 2021-01-27 13:02:00 -0700 | [diff] [blame] | 34 | elif fw_loc == common_pb2.PLATFORM_TI50: |
Andrew Luo | 60439f3 | 2021-01-26 15:19:28 -0800 | [diff] [blame] | 35 | fw_path = 'src/platform/ti50/common/' |
Jett Rink | 17ed0f5 | 2020-09-25 17:14:31 -0600 | [diff] [blame] | 36 | else: |
LaMont Jones | dec69ad | 2021-01-27 13:02:00 -0700 | [diff] [blame] | 37 | cros_build_lib.Die(f'Unknown firmware location {fw_loc}.') |
Jett Rink | 17ed0f5 | 2020-09-25 17:14:31 -0600 | [diff] [blame] | 38 | |
LaMont Jones | ed9a5de | 2021-02-03 11:06:12 -0700 | [diff] [blame] | 39 | entry_point = os.path.join(constants.SOURCE_ROOT, fw_path, |
| 40 | 'firmware_builder.py') |
Jett Rink | 17ed0f5 | 2020-09-25 17:14:31 -0600 | [diff] [blame] | 41 | |
LaMont Jones | dec69ad | 2021-01-27 13:02:00 -0700 | [diff] [blame] | 42 | with tempfile.NamedTemporaryFile() as tmpfile: |
LaMont Jones | 4bab978 | 2021-02-17 22:52:44 +0000 | [diff] [blame] | 43 | cmd = [entry_point, '--metrics', tmpfile.name] |
LaMont Jones | dec69ad | 2021-01-27 13:02:00 -0700 | [diff] [blame] | 44 | for key, value in kwargs.items(): |
| 45 | cmd += [f'--{key.replace("_", "-")}', value] |
LaMont Jones | ed9a5de | 2021-02-03 11:06:12 -0700 | [diff] [blame] | 46 | cmd += [subcmd] |
LaMont Jones | dec69ad | 2021-01-27 13:02:00 -0700 | [diff] [blame] | 47 | |
| 48 | result = cros_build_lib.run(cmd, check=False) |
| 49 | with open(tmpfile.name, 'r') as f: |
Jett Rink | 17ed0f5 | 2020-09-25 17:14:31 -0600 | [diff] [blame] | 50 | response = f.read() |
| 51 | |
LaMont Jones | ed9a5de | 2021-02-03 11:06:12 -0700 | [diff] [blame] | 52 | if metric_proto: |
| 53 | # Parse the entire metric file as our metric proto (as a passthru). |
| 54 | # TODO(b/177907747): BundleFirmwareArtifacts doesn't use this (yet?), but |
| 55 | # firmware_builder.py requires it. |
| 56 | json_format.Parse(response, metric_proto) |
Jett Rink | 17ed0f5 | 2020-09-25 17:14:31 -0600 | [diff] [blame] | 57 | |
| 58 | if result.returncode == 0: |
| 59 | return controller.RETURN_CODE_SUCCESS |
| 60 | else: |
| 61 | return controller.RETURN_CODE_COMPLETED_UNSUCCESSFULLY |
| 62 | |
| 63 | |
| 64 | def _BuildAllTotFirmwareResponse(_input_proto, output_proto, _config): |
LaMont Jones | dec69ad | 2021-01-27 13:02:00 -0700 | [diff] [blame] | 65 | """Add a fw region metric to a successful response.""" |
Jett Rink | 17ed0f5 | 2020-09-25 17:14:31 -0600 | [diff] [blame] | 66 | |
| 67 | metric = output_proto.success.value.add() |
| 68 | metric.target_name = 'foo' |
| 69 | metric.platform_name = 'bar' |
| 70 | fw_section = metric.fw_section.add() |
| 71 | fw_section.region = firmware_pb2.FwBuildMetric.FwSection.EC_RO |
| 72 | fw_section.used = 100 |
| 73 | fw_section.total = 150 |
| 74 | |
LaMont Jones | ed9a5de | 2021-02-03 11:06:12 -0700 | [diff] [blame] | 75 | |
Jett Rink | 17ed0f5 | 2020-09-25 17:14:31 -0600 | [diff] [blame] | 76 | @faux.success(_BuildAllTotFirmwareResponse) |
| 77 | @faux.empty_completed_unsuccessfully_error |
| 78 | @validate.require('firmware_location') |
| 79 | @validate.validation_complete |
| 80 | def BuildAllTotFirmware(input_proto, output_proto, _config): |
| 81 | """Build all of the firmware targets at the specified location.""" |
| 82 | |
| 83 | return _call_entry(input_proto.firmware_location, output_proto.metrics, |
LaMont Jones | 4bab978 | 2021-02-17 22:52:44 +0000 | [diff] [blame] | 84 | 'build') |
Jett Rink | 17ed0f5 | 2020-09-25 17:14:31 -0600 | [diff] [blame] | 85 | |
| 86 | |
| 87 | def _TestAllTotFirmwareResponse(_input_proto, output_proto, _config): |
LaMont Jones | dec69ad | 2021-01-27 13:02:00 -0700 | [diff] [blame] | 88 | """Add a fw region metric to a successful response.""" |
Jett Rink | 17ed0f5 | 2020-09-25 17:14:31 -0600 | [diff] [blame] | 89 | |
| 90 | metric = output_proto.success.value.add() |
| 91 | metric.name = 'foo-test' |
| 92 | |
LaMont Jones | ed9a5de | 2021-02-03 11:06:12 -0700 | [diff] [blame] | 93 | |
Jett Rink | 17ed0f5 | 2020-09-25 17:14:31 -0600 | [diff] [blame] | 94 | @faux.success(_TestAllTotFirmwareResponse) |
| 95 | @faux.empty_completed_unsuccessfully_error |
| 96 | @validate.require('firmware_location') |
| 97 | @validate.validation_complete |
| 98 | def TestAllTotFirmware(input_proto, output_proto, _config): |
| 99 | """Runs all of the firmware tests at the specified location.""" |
| 100 | |
| 101 | return _call_entry(input_proto.firmware_location, output_proto.metrics, |
LaMont Jones | 4bab978 | 2021-02-17 22:52:44 +0000 | [diff] [blame] | 102 | 'test') |
LaMont Jones | dec69ad | 2021-01-27 13:02:00 -0700 | [diff] [blame] | 103 | |
| 104 | |
| 105 | def _BuildAllFirmwareResponse(_input_proto, output_proto, _config): |
| 106 | """Add a fw region metric to a successful response.""" |
| 107 | |
| 108 | metric = output_proto.success.value.add() |
| 109 | metric.target_name = 'foo' |
| 110 | metric.platform_name = 'bar' |
| 111 | fw_section = metric.fw_section.add() |
| 112 | fw_section.region = firmware_pb2.FwBuildMetric.FwSection.EC_RO |
| 113 | fw_section.used = 100 |
| 114 | fw_section.total = 150 |
| 115 | |
LaMont Jones | ed9a5de | 2021-02-03 11:06:12 -0700 | [diff] [blame] | 116 | |
LaMont Jones | dec69ad | 2021-01-27 13:02:00 -0700 | [diff] [blame] | 117 | @faux.success(_BuildAllFirmwareResponse) |
| 118 | @faux.empty_completed_unsuccessfully_error |
| 119 | @validate.require('firmware_location') |
| 120 | @validate.validation_complete |
| 121 | def BuildAllFirmware(input_proto, output_proto, _config): |
| 122 | """Build all of the firmware targets at the specified location.""" |
| 123 | |
| 124 | return _call_entry(input_proto.firmware_location, output_proto.metrics, |
LaMont Jones | 4bab978 | 2021-02-17 22:52:44 +0000 | [diff] [blame] | 125 | 'build') |
LaMont Jones | dec69ad | 2021-01-27 13:02:00 -0700 | [diff] [blame] | 126 | |
| 127 | |
| 128 | def _TestAllFirmwareResponse(_input_proto, output_proto, _config): |
| 129 | """Add a fw region metric to a successful response.""" |
| 130 | |
| 131 | metric = output_proto.success.value.add() |
| 132 | metric.name = 'foo-test' |
| 133 | |
LaMont Jones | ed9a5de | 2021-02-03 11:06:12 -0700 | [diff] [blame] | 134 | |
LaMont Jones | dec69ad | 2021-01-27 13:02:00 -0700 | [diff] [blame] | 135 | @faux.success(_TestAllFirmwareResponse) |
| 136 | @faux.empty_completed_unsuccessfully_error |
| 137 | @validate.require('firmware_location') |
| 138 | @validate.validation_complete |
| 139 | def TestAllFirmware(input_proto, output_proto, _config): |
| 140 | """Runs all of the firmware tests at the specified location.""" |
| 141 | |
| 142 | return _call_entry(input_proto.firmware_location, output_proto.metrics, |
LaMont Jones | 4bab978 | 2021-02-17 22:52:44 +0000 | [diff] [blame] | 143 | 'test') |
LaMont Jones | dec69ad | 2021-01-27 13:02:00 -0700 | [diff] [blame] | 144 | |
| 145 | |
| 146 | def _BundleFirmwareArtifactsResponse(_input_proto, output_proto, _config): |
| 147 | """Add a fw region metric to a successful response.""" |
| 148 | |
| 149 | metric = output_proto.success.value.add() |
| 150 | metric.name = 'foo-test' |
| 151 | |
LaMont Jones | ed9a5de | 2021-02-03 11:06:12 -0700 | [diff] [blame] | 152 | |
LaMont Jones | dec69ad | 2021-01-27 13:02:00 -0700 | [diff] [blame] | 153 | @faux.success(_BundleFirmwareArtifactsResponse) |
| 154 | @faux.empty_completed_unsuccessfully_error |
LaMont Jones | dec69ad | 2021-01-27 13:02:00 -0700 | [diff] [blame] | 155 | @validate.validation_complete |
| 156 | def BundleFirmwareArtifacts(input_proto, output_proto, _config): |
| 157 | """Runs all of the firmware tests at the specified location.""" |
| 158 | |
LaMont Jones | ed9a5de | 2021-02-03 11:06:12 -0700 | [diff] [blame] | 159 | if len(input_proto.artifacts.output_artifacts) > 1: |
LaMont Jones | dec69ad | 2021-01-27 13:02:00 -0700 | [diff] [blame] | 160 | raise ValueError('Must have exactly one output_artifact') |
| 161 | |
LaMont Jones | ed9a5de | 2021-02-03 11:06:12 -0700 | [diff] [blame] | 162 | with osutils.TempDir(delete=False) as tmpdir: |
| 163 | info = input_proto.artifacts.output_artifacts[0] |
| 164 | metadata_path = os.path.join(tmpdir, 'firmware_metadata.jsonpb') |
LaMont Jones | dec69ad | 2021-01-27 13:02:00 -0700 | [diff] [blame] | 165 | resp = _call_entry( |
LaMont Jones | ed9a5de | 2021-02-03 11:06:12 -0700 | [diff] [blame] | 166 | info.location, |
| 167 | None, |
| 168 | 'bundle', |
| 169 | output_dir=tmpdir, |
| 170 | metadata=metadata_path) |
LaMont Jones | 80dc8bc | 2021-02-09 16:29:30 -0700 | [diff] [blame] | 171 | tarball_paths = [] |
LaMont Jones | ed9a5de | 2021-02-03 11:06:12 -0700 | [diff] [blame] | 172 | if (input_proto.artifacts.FIRMWARE_TARBALL_INFO in info.artifact_types and |
| 173 | os.path.exists(metadata_path)): |
LaMont Jones | 80dc8bc | 2021-02-09 16:29:30 -0700 | [diff] [blame] | 174 | with open(metadata_path, 'r') as f: |
| 175 | metadata = json_format.Parse(f.read(), |
| 176 | firmware_pb2.FirmwareArtifactInfo()) |
LaMont Jones | ed9a5de | 2021-02-03 11:06:12 -0700 | [diff] [blame] | 177 | out = output_proto.artifacts.artifacts.add( |
| 178 | artifact_type=input_proto.artifacts.FIRMWARE_TARBALL_INFO, |
| 179 | paths=[ |
| 180 | common_pb2.Path( |
| 181 | path=metadata_path, location=common_pb2.Path.INSIDE) |
| 182 | ]) |
LaMont Jones | 80dc8bc | 2021-02-09 16:29:30 -0700 | [diff] [blame] | 183 | tarball_paths = [ |
| 184 | common_pb2.Path( |
| 185 | path=os.path.join(tmpdir, x.file_name), |
| 186 | location=common_pb2.Path.INSIDE) for x in metadata.objects |
| 187 | ] |
| 188 | if (tarball_paths and |
| 189 | input_proto.artifacts.FIRMWARE_TARBALL in info.artifact_types): |
| 190 | out = output_proto.artifacts.artifacts.add( |
| 191 | artifact_type=input_proto.artifacts.FIRMWARE_TARBALL, |
| 192 | paths=tarball_paths) |
| 193 | out.location = info.location |
LaMont Jones | dec69ad | 2021-01-27 13:02:00 -0700 | [diff] [blame] | 194 | return resp |