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