Jett Rink | 17ed0f5 | 2020-09-25 17:14:31 -0600 | [diff] [blame] | 1 | # Copyright 2020 The Chromium OS Authors. All rights reserved. |
| 2 | # Use of this source code is governed by a BSD-style license that can be |
| 3 | # found in the LICENSE file. |
LaMont Jones | ed9a5de | 2021-02-03 11:06:12 -0700 | [diff] [blame] | 4 | |
Jett Rink | 17ed0f5 | 2020-09-25 17:14:31 -0600 | [diff] [blame] | 5 | """Firmware builder controller. |
| 6 | |
LaMont Jones | dec69ad | 2021-01-27 13:02:00 -0700 | [diff] [blame] | 7 | Handle all firmware builder related functionality. Currently no service module |
| 8 | exists: all of the work is done here. |
Jett Rink | 17ed0f5 | 2020-09-25 17:14:31 -0600 | [diff] [blame] | 9 | """ |
| 10 | |
| 11 | import os |
| 12 | import tempfile |
| 13 | |
Mike Frysinger | 2c02406 | 2021-05-22 15:43:22 -0400 | [diff] [blame] | 14 | from chromite.third_party.google.protobuf import json_format |
Jett Rink | 17ed0f5 | 2020-09-25 17:14:31 -0600 | [diff] [blame] | 15 | |
| 16 | from chromite.api import controller |
| 17 | from chromite.api import faux |
| 18 | from chromite.api import validate |
| 19 | from chromite.api.gen.chromite.api import firmware_pb2 |
LaMont Jones | dec69ad | 2021-01-27 13:02:00 -0700 | [diff] [blame] | 20 | from chromite.api.gen.chromiumos import common_pb2 |
Jett Rink | 17ed0f5 | 2020-09-25 17:14:31 -0600 | [diff] [blame] | 21 | from chromite.lib import constants |
| 22 | from chromite.lib import cros_build_lib |
LaMont Jones | ed9a5de | 2021-02-03 11:06:12 -0700 | [diff] [blame] | 23 | from chromite.lib import osutils |
| 24 | |
Mike Frysinger | 1cc8f1f | 2022-04-28 22:40:40 -0400 | [diff] [blame] | 25 | |
Jeremy Bettis | e87bbaf | 2022-07-07 11:57:16 -0600 | [diff] [blame] | 26 | def get_fw_loc(fw_loc: int) -> str: |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame^] | 27 | """Get firmware_builder.py location. |
Azizur Rahman | c6cf6e9 | 2021-10-21 18:58:19 +0000 | [diff] [blame] | 28 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame^] | 29 | Args: |
| 30 | fw_loc: FwLocation enum. |
Azizur Rahman | c6cf6e9 | 2021-10-21 18:58:19 +0000 | [diff] [blame] | 31 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame^] | 32 | Returns: |
| 33 | path to firmware_builder.py for valid fw_loc. |
| 34 | """ |
| 35 | return { |
| 36 | common_pb2.PLATFORM_EC: "src/platform/ec/", |
| 37 | common_pb2.PLATFORM_ZEPHYR: "src/platform/ec/zephyr/", |
| 38 | common_pb2.PLATFORM_TI50: "src/platform/ti50/common/", |
| 39 | common_pb2.PLATFORM_CR50: "src/platform/cr50/", |
| 40 | }.get(fw_loc, "") |
Azizur Rahman | c6cf6e9 | 2021-10-21 18:58:19 +0000 | [diff] [blame] | 41 | |
Jett Rink | 17ed0f5 | 2020-09-25 17:14:31 -0600 | [diff] [blame] | 42 | |
LaMont Jones | b7be76b | 2021-02-16 16:22:14 -0700 | [diff] [blame] | 43 | def _call_entry(fw_loc, metric_proto, subcmd, *args, **kwargs): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame^] | 44 | """Calls into firmware_builder.py with the specified subcmd.""" |
Jett Rink | 17ed0f5 | 2020-09-25 17:14:31 -0600 | [diff] [blame] | 45 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame^] | 46 | fw_path = get_fw_loc(fw_loc) |
| 47 | if not fw_path: |
| 48 | cros_build_lib.Die(f"Unknown firmware location {fw_loc}.") |
Jett Rink | 17ed0f5 | 2020-09-25 17:14:31 -0600 | [diff] [blame] | 49 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame^] | 50 | entry_point = os.path.join( |
| 51 | constants.SOURCE_ROOT, fw_path, "firmware_builder.py" |
| 52 | ) |
Jett Rink | 17ed0f5 | 2020-09-25 17:14:31 -0600 | [diff] [blame] | 53 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame^] | 54 | with tempfile.NamedTemporaryFile() as tmpfile: |
| 55 | cmd = [entry_point, "--metrics", tmpfile.name] + list(args) |
| 56 | for key, value in kwargs.items(): |
| 57 | cmd += [f'--{key.replace("_", "-")}', value] |
| 58 | cmd += [subcmd] |
LaMont Jones | dec69ad | 2021-01-27 13:02:00 -0700 | [diff] [blame] | 59 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame^] | 60 | result = cros_build_lib.run(cmd, check=False) |
| 61 | with open(tmpfile.name, "r") as f: |
| 62 | response = f.read() |
Jett Rink | 17ed0f5 | 2020-09-25 17:14:31 -0600 | [diff] [blame] | 63 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame^] | 64 | if metric_proto: |
| 65 | # Parse the entire metric file as our metric proto (as a passthru). |
| 66 | # TODO(b/177907747): BundleFirmwareArtifacts doesn't use this (yet?), but |
| 67 | # firmware_builder.py requires it. |
| 68 | json_format.Parse(response, metric_proto) |
Jett Rink | 17ed0f5 | 2020-09-25 17:14:31 -0600 | [diff] [blame] | 69 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame^] | 70 | if result.returncode == 0: |
| 71 | return controller.RETURN_CODE_SUCCESS |
| 72 | else: |
| 73 | return controller.RETURN_CODE_COMPLETED_UNSUCCESSFULLY |
Jett Rink | 17ed0f5 | 2020-09-25 17:14:31 -0600 | [diff] [blame] | 74 | |
| 75 | |
| 76 | def _BuildAllTotFirmwareResponse(_input_proto, output_proto, _config): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame^] | 77 | """Add a fw region metric to a successful response.""" |
Jett Rink | 17ed0f5 | 2020-09-25 17:14:31 -0600 | [diff] [blame] | 78 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame^] | 79 | metric = output_proto.success.value.add() |
| 80 | metric.target_name = "foo" |
| 81 | metric.platform_name = "bar" |
| 82 | fw_section = metric.fw_section.add() |
| 83 | fw_section.region = "EC_RO" |
| 84 | fw_section.used = 100 |
| 85 | fw_section.total = 150 |
Jett Rink | 17ed0f5 | 2020-09-25 17:14:31 -0600 | [diff] [blame] | 86 | |
LaMont Jones | ed9a5de | 2021-02-03 11:06:12 -0700 | [diff] [blame] | 87 | |
Jett Rink | 17ed0f5 | 2020-09-25 17:14:31 -0600 | [diff] [blame] | 88 | @faux.success(_BuildAllTotFirmwareResponse) |
| 89 | @faux.empty_completed_unsuccessfully_error |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame^] | 90 | @validate.require("firmware_location") |
Jett Rink | 17ed0f5 | 2020-09-25 17:14:31 -0600 | [diff] [blame] | 91 | @validate.validation_complete |
| 92 | def BuildAllTotFirmware(input_proto, output_proto, _config): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame^] | 93 | """Build all of the firmware targets at the specified location.""" |
Jett Rink | 17ed0f5 | 2020-09-25 17:14:31 -0600 | [diff] [blame] | 94 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame^] | 95 | args = ["--code-coverage"] if input_proto.code_coverage else [] |
| 96 | return _call_entry( |
| 97 | input_proto.firmware_location, output_proto.metrics, "build", *args |
| 98 | ) |
Jett Rink | 17ed0f5 | 2020-09-25 17:14:31 -0600 | [diff] [blame] | 99 | |
| 100 | |
| 101 | def _TestAllTotFirmwareResponse(_input_proto, output_proto, _config): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame^] | 102 | """Add a fw region metric to a successful response.""" |
Jett Rink | 17ed0f5 | 2020-09-25 17:14:31 -0600 | [diff] [blame] | 103 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame^] | 104 | metric = output_proto.success.value.add() |
| 105 | metric.name = "foo-test" |
Jett Rink | 17ed0f5 | 2020-09-25 17:14:31 -0600 | [diff] [blame] | 106 | |
LaMont Jones | ed9a5de | 2021-02-03 11:06:12 -0700 | [diff] [blame] | 107 | |
Jett Rink | 17ed0f5 | 2020-09-25 17:14:31 -0600 | [diff] [blame] | 108 | @faux.success(_TestAllTotFirmwareResponse) |
| 109 | @faux.empty_completed_unsuccessfully_error |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame^] | 110 | @validate.require("firmware_location") |
Jett Rink | 17ed0f5 | 2020-09-25 17:14:31 -0600 | [diff] [blame] | 111 | @validate.validation_complete |
| 112 | def TestAllTotFirmware(input_proto, output_proto, _config): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame^] | 113 | """Runs all of the firmware tests at the specified location.""" |
Jett Rink | 17ed0f5 | 2020-09-25 17:14:31 -0600 | [diff] [blame] | 114 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame^] | 115 | args = ["--code-coverage"] if input_proto.code_coverage else [] |
| 116 | return _call_entry( |
| 117 | input_proto.firmware_location, output_proto.metrics, "test", *args |
| 118 | ) |
LaMont Jones | dec69ad | 2021-01-27 13:02:00 -0700 | [diff] [blame] | 119 | |
| 120 | |
| 121 | def _BuildAllFirmwareResponse(_input_proto, output_proto, _config): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame^] | 122 | """Add a fw region metric to a successful response.""" |
LaMont Jones | dec69ad | 2021-01-27 13:02:00 -0700 | [diff] [blame] | 123 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame^] | 124 | metric = output_proto.metrics.value.add() |
| 125 | metric.target_name = "foo" |
| 126 | metric.platform_name = "bar" |
| 127 | fw_section = metric.fw_section.add() |
| 128 | fw_section.region = "EC_RO" |
| 129 | fw_section.used = 100 |
| 130 | fw_section.total = 150 |
LaMont Jones | dec69ad | 2021-01-27 13:02:00 -0700 | [diff] [blame] | 131 | |
LaMont Jones | ed9a5de | 2021-02-03 11:06:12 -0700 | [diff] [blame] | 132 | |
LaMont Jones | dec69ad | 2021-01-27 13:02:00 -0700 | [diff] [blame] | 133 | @faux.success(_BuildAllFirmwareResponse) |
| 134 | @faux.empty_completed_unsuccessfully_error |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame^] | 135 | @validate.require("firmware_location") |
LaMont Jones | dec69ad | 2021-01-27 13:02:00 -0700 | [diff] [blame] | 136 | @validate.validation_complete |
| 137 | def BuildAllFirmware(input_proto, output_proto, _config): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame^] | 138 | """Build all of the firmware targets at the specified location.""" |
LaMont Jones | dec69ad | 2021-01-27 13:02:00 -0700 | [diff] [blame] | 139 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame^] | 140 | args = ["--code-coverage"] if input_proto.code_coverage else [] |
| 141 | return _call_entry( |
| 142 | input_proto.firmware_location, output_proto.metrics, "build", *args |
| 143 | ) |
LaMont Jones | dec69ad | 2021-01-27 13:02:00 -0700 | [diff] [blame] | 144 | |
| 145 | |
| 146 | def _TestAllFirmwareResponse(_input_proto, output_proto, _config): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame^] | 147 | """Add a fw region metric to a successful response.""" |
LaMont Jones | dec69ad | 2021-01-27 13:02:00 -0700 | [diff] [blame] | 148 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame^] | 149 | metric = output_proto.success.value.add() |
| 150 | metric.name = "foo-test" |
LaMont Jones | dec69ad | 2021-01-27 13:02:00 -0700 | [diff] [blame] | 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(_TestAllFirmwareResponse) |
| 154 | @faux.empty_completed_unsuccessfully_error |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame^] | 155 | @validate.require("firmware_location") |
LaMont Jones | dec69ad | 2021-01-27 13:02:00 -0700 | [diff] [blame] | 156 | @validate.validation_complete |
| 157 | def TestAllFirmware(input_proto, output_proto, _config): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame^] | 158 | """Runs all of the firmware tests at the specified location.""" |
LaMont Jones | dec69ad | 2021-01-27 13:02:00 -0700 | [diff] [blame] | 159 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame^] | 160 | args = ["--code-coverage"] if input_proto.code_coverage else [] |
| 161 | return _call_entry( |
| 162 | input_proto.firmware_location, output_proto.metrics, "test", *args |
| 163 | ) |
LaMont Jones | dec69ad | 2021-01-27 13:02:00 -0700 | [diff] [blame] | 164 | |
| 165 | |
| 166 | def _BundleFirmwareArtifactsResponse(_input_proto, output_proto, _config): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame^] | 167 | """Add a fw region metric to a successful response.""" |
LaMont Jones | dec69ad | 2021-01-27 13:02:00 -0700 | [diff] [blame] | 168 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame^] | 169 | metric = output_proto.success.value.add() |
| 170 | metric.name = "foo-test" |
LaMont Jones | dec69ad | 2021-01-27 13:02:00 -0700 | [diff] [blame] | 171 | |
LaMont Jones | ed9a5de | 2021-02-03 11:06:12 -0700 | [diff] [blame] | 172 | |
LaMont Jones | dec69ad | 2021-01-27 13:02:00 -0700 | [diff] [blame] | 173 | @faux.success(_BundleFirmwareArtifactsResponse) |
| 174 | @faux.empty_completed_unsuccessfully_error |
LaMont Jones | dec69ad | 2021-01-27 13:02:00 -0700 | [diff] [blame] | 175 | @validate.validation_complete |
| 176 | def BundleFirmwareArtifacts(input_proto, output_proto, _config): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame^] | 177 | """Runs all of the firmware tests at the specified location.""" |
LaMont Jones | dec69ad | 2021-01-27 13:02:00 -0700 | [diff] [blame] | 178 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame^] | 179 | if len(input_proto.artifacts.output_artifacts) > 1: |
| 180 | raise ValueError("Must have exactly one output_artifact entry") |
LaMont Jones | dec69ad | 2021-01-27 13:02:00 -0700 | [diff] [blame] | 181 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame^] | 182 | with osutils.TempDir(delete=False) as tmpdir: |
| 183 | info = input_proto.artifacts.output_artifacts[0] |
| 184 | metadata_path = os.path.join(tmpdir, "firmware_metadata.jsonpb") |
| 185 | args = [] |
| 186 | if input_proto.artifacts.FIRMWARE_LCOV in info.artifact_types: |
| 187 | args += ["--code-coverage"] |
| 188 | resp = _call_entry( |
| 189 | info.location, |
| 190 | None, |
| 191 | "bundle", |
| 192 | *args, |
| 193 | output_dir=tmpdir, |
| 194 | metadata=metadata_path, |
| 195 | ) |
| 196 | file_paths = [] |
| 197 | if os.path.exists(metadata_path): |
| 198 | with open(metadata_path, "r") as f: |
| 199 | metadata = json_format.Parse( |
| 200 | f.read(), firmware_pb2.FirmwareArtifactInfo() |
| 201 | ) |
| 202 | else: |
| 203 | metadata = firmware_pb2.FirmwareArtifactInfo() |
| 204 | if input_proto.artifacts.FIRMWARE_TARBALL_INFO in info.artifact_types: |
| 205 | output_proto.artifacts.artifacts.add( |
| 206 | artifact_type=input_proto.artifacts.FIRMWARE_TARBALL_INFO, |
| 207 | location=info.location, |
| 208 | paths=[ |
| 209 | common_pb2.Path( |
| 210 | path=metadata_path, location=common_pb2.Path.INSIDE |
| 211 | ) |
| 212 | ], |
| 213 | ) |
LaMont Jones | 9a0594d | 2021-03-17 15:51:20 -0600 | [diff] [blame] | 214 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame^] | 215 | full_path = lambda x: common_pb2.Path( |
| 216 | path=os.path.join(tmpdir, x.file_name), |
| 217 | location=common_pb2.Path.INSIDE, |
| 218 | ) |
LaMont Jones | 9a0594d | 2021-03-17 15:51:20 -0600 | [diff] [blame] | 219 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame^] | 220 | for typ, name in ( |
| 221 | (input_proto.artifacts.FIRMWARE_TARBALL, "tarball_info"), |
| 222 | (input_proto.artifacts.FIRMWARE_LCOV, "lcov_info"), |
| 223 | (input_proto.artifacts.CODE_COVERAGE_HTML, "coverage_html"), |
| 224 | ): |
| 225 | file_paths = [ |
| 226 | full_path(x) |
| 227 | for x in metadata.objects |
| 228 | if x.WhichOneof("firmware_object_info") == name |
| 229 | ] |
| 230 | if file_paths and typ in info.artifact_types: |
| 231 | output_proto.artifacts.artifacts.add( |
| 232 | artifact_type=typ, paths=file_paths, location=info.location |
| 233 | ) |
LaMont Jones | 9a0594d | 2021-03-17 15:51:20 -0600 | [diff] [blame] | 234 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame^] | 235 | return resp |