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 | """ |
Alex Klein | eec90f7 | 2023-02-03 10:53:12 -0700 | [diff] [blame] | 10 | import logging |
Jett Rink | 17ed0f5 | 2020-09-25 17:14:31 -0600 | [diff] [blame] | 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: |
Alex Klein | 611dddd | 2022-10-11 17:02:01 -0600 | [diff] [blame] | 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: |
Alex Klein | 611dddd | 2022-10-11 17:02:01 -0600 | [diff] [blame] | 33 | path to firmware_builder.py for valid fw_loc. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 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) |
Mike Frysinger | d97829b | 2023-02-24 16:09:20 -0500 | [diff] [blame] | 61 | with open(tmpfile.name, "r", encoding="utf-8") as f: |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 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: |
Alex Klein | eec90f7 | 2023-02-03 10:53:12 -0700 | [diff] [blame] | 65 | if not response: |
| 66 | logging.warning("Metrics data empty.") |
| 67 | else: |
| 68 | # Parse the entire metric file as our metric proto (as a passthru). |
| 69 | # TODO(b/177907747): BundleFirmwareArtifacts doesn't use this |
| 70 | # (yet?), but firmware_builder.py requires it. |
| 71 | json_format.Parse(response, metric_proto) |
Jett Rink | 17ed0f5 | 2020-09-25 17:14:31 -0600 | [diff] [blame] | 72 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 73 | if result.returncode == 0: |
| 74 | return controller.RETURN_CODE_SUCCESS |
| 75 | else: |
| 76 | return controller.RETURN_CODE_COMPLETED_UNSUCCESSFULLY |
Jett Rink | 17ed0f5 | 2020-09-25 17:14:31 -0600 | [diff] [blame] | 77 | |
| 78 | |
| 79 | def _BuildAllTotFirmwareResponse(_input_proto, output_proto, _config): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 80 | """Add a fw region metric to a successful response.""" |
Jett Rink | 17ed0f5 | 2020-09-25 17:14:31 -0600 | [diff] [blame] | 81 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 82 | metric = output_proto.success.value.add() |
| 83 | metric.target_name = "foo" |
| 84 | metric.platform_name = "bar" |
| 85 | fw_section = metric.fw_section.add() |
| 86 | fw_section.region = "EC_RO" |
| 87 | fw_section.used = 100 |
| 88 | fw_section.total = 150 |
Jett Rink | 17ed0f5 | 2020-09-25 17:14:31 -0600 | [diff] [blame] | 89 | |
LaMont Jones | ed9a5de | 2021-02-03 11:06:12 -0700 | [diff] [blame] | 90 | |
Jett Rink | 17ed0f5 | 2020-09-25 17:14:31 -0600 | [diff] [blame] | 91 | @faux.success(_BuildAllTotFirmwareResponse) |
| 92 | @faux.empty_completed_unsuccessfully_error |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 93 | @validate.require("firmware_location") |
Jett Rink | 17ed0f5 | 2020-09-25 17:14:31 -0600 | [diff] [blame] | 94 | @validate.validation_complete |
| 95 | def BuildAllTotFirmware(input_proto, output_proto, _config): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 96 | """Build all of the firmware targets at the specified location.""" |
Jett Rink | 17ed0f5 | 2020-09-25 17:14:31 -0600 | [diff] [blame] | 97 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 98 | args = ["--code-coverage"] if input_proto.code_coverage else [] |
| 99 | return _call_entry( |
| 100 | input_proto.firmware_location, output_proto.metrics, "build", *args |
| 101 | ) |
Jett Rink | 17ed0f5 | 2020-09-25 17:14:31 -0600 | [diff] [blame] | 102 | |
| 103 | |
| 104 | def _TestAllTotFirmwareResponse(_input_proto, output_proto, _config): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 105 | """Add a fw region metric to a successful response.""" |
Jett Rink | 17ed0f5 | 2020-09-25 17:14:31 -0600 | [diff] [blame] | 106 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 107 | metric = output_proto.success.value.add() |
| 108 | metric.name = "foo-test" |
Jett Rink | 17ed0f5 | 2020-09-25 17:14:31 -0600 | [diff] [blame] | 109 | |
LaMont Jones | ed9a5de | 2021-02-03 11:06:12 -0700 | [diff] [blame] | 110 | |
Jett Rink | 17ed0f5 | 2020-09-25 17:14:31 -0600 | [diff] [blame] | 111 | @faux.success(_TestAllTotFirmwareResponse) |
| 112 | @faux.empty_completed_unsuccessfully_error |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 113 | @validate.require("firmware_location") |
Jett Rink | 17ed0f5 | 2020-09-25 17:14:31 -0600 | [diff] [blame] | 114 | @validate.validation_complete |
| 115 | def TestAllTotFirmware(input_proto, output_proto, _config): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 116 | """Runs all of the firmware tests at the specified location.""" |
Jett Rink | 17ed0f5 | 2020-09-25 17:14:31 -0600 | [diff] [blame] | 117 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 118 | args = ["--code-coverage"] if input_proto.code_coverage else [] |
| 119 | return _call_entry( |
| 120 | input_proto.firmware_location, output_proto.metrics, "test", *args |
| 121 | ) |
LaMont Jones | dec69ad | 2021-01-27 13:02:00 -0700 | [diff] [blame] | 122 | |
| 123 | |
| 124 | def _BuildAllFirmwareResponse(_input_proto, output_proto, _config): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 125 | """Add a fw region metric to a successful response.""" |
LaMont Jones | dec69ad | 2021-01-27 13:02:00 -0700 | [diff] [blame] | 126 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 127 | metric = output_proto.metrics.value.add() |
| 128 | metric.target_name = "foo" |
| 129 | metric.platform_name = "bar" |
| 130 | fw_section = metric.fw_section.add() |
| 131 | fw_section.region = "EC_RO" |
| 132 | fw_section.used = 100 |
| 133 | fw_section.total = 150 |
LaMont Jones | dec69ad | 2021-01-27 13:02:00 -0700 | [diff] [blame] | 134 | |
LaMont Jones | ed9a5de | 2021-02-03 11:06:12 -0700 | [diff] [blame] | 135 | |
LaMont Jones | dec69ad | 2021-01-27 13:02:00 -0700 | [diff] [blame] | 136 | @faux.success(_BuildAllFirmwareResponse) |
| 137 | @faux.empty_completed_unsuccessfully_error |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 138 | @validate.require("firmware_location") |
LaMont Jones | dec69ad | 2021-01-27 13:02:00 -0700 | [diff] [blame] | 139 | @validate.validation_complete |
| 140 | def BuildAllFirmware(input_proto, output_proto, _config): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 141 | """Build all of the firmware targets at the specified location.""" |
LaMont Jones | dec69ad | 2021-01-27 13:02:00 -0700 | [diff] [blame] | 142 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 143 | args = ["--code-coverage"] if input_proto.code_coverage else [] |
| 144 | return _call_entry( |
| 145 | input_proto.firmware_location, output_proto.metrics, "build", *args |
| 146 | ) |
LaMont Jones | dec69ad | 2021-01-27 13:02:00 -0700 | [diff] [blame] | 147 | |
| 148 | |
| 149 | def _TestAllFirmwareResponse(_input_proto, output_proto, _config): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 150 | """Add a fw region metric to a successful response.""" |
LaMont Jones | dec69ad | 2021-01-27 13:02:00 -0700 | [diff] [blame] | 151 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 152 | metric = output_proto.success.value.add() |
| 153 | metric.name = "foo-test" |
LaMont Jones | dec69ad | 2021-01-27 13:02:00 -0700 | [diff] [blame] | 154 | |
LaMont Jones | ed9a5de | 2021-02-03 11:06:12 -0700 | [diff] [blame] | 155 | |
LaMont Jones | dec69ad | 2021-01-27 13:02:00 -0700 | [diff] [blame] | 156 | @faux.success(_TestAllFirmwareResponse) |
| 157 | @faux.empty_completed_unsuccessfully_error |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 158 | @validate.require("firmware_location") |
LaMont Jones | dec69ad | 2021-01-27 13:02:00 -0700 | [diff] [blame] | 159 | @validate.validation_complete |
| 160 | def TestAllFirmware(input_proto, output_proto, _config): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 161 | """Runs all of the firmware tests at the specified location.""" |
LaMont Jones | dec69ad | 2021-01-27 13:02:00 -0700 | [diff] [blame] | 162 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 163 | args = ["--code-coverage"] if input_proto.code_coverage else [] |
| 164 | return _call_entry( |
| 165 | input_proto.firmware_location, output_proto.metrics, "test", *args |
| 166 | ) |
LaMont Jones | dec69ad | 2021-01-27 13:02:00 -0700 | [diff] [blame] | 167 | |
| 168 | |
| 169 | def _BundleFirmwareArtifactsResponse(_input_proto, output_proto, _config): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 170 | """Add a fw region metric to a successful response.""" |
LaMont Jones | dec69ad | 2021-01-27 13:02:00 -0700 | [diff] [blame] | 171 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 172 | metric = output_proto.success.value.add() |
| 173 | metric.name = "foo-test" |
LaMont Jones | dec69ad | 2021-01-27 13:02:00 -0700 | [diff] [blame] | 174 | |
LaMont Jones | ed9a5de | 2021-02-03 11:06:12 -0700 | [diff] [blame] | 175 | |
LaMont Jones | dec69ad | 2021-01-27 13:02:00 -0700 | [diff] [blame] | 176 | @faux.success(_BundleFirmwareArtifactsResponse) |
| 177 | @faux.empty_completed_unsuccessfully_error |
LaMont Jones | dec69ad | 2021-01-27 13:02:00 -0700 | [diff] [blame] | 178 | @validate.validation_complete |
| 179 | def BundleFirmwareArtifacts(input_proto, output_proto, _config): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 180 | """Runs all of the firmware tests at the specified location.""" |
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 | if len(input_proto.artifacts.output_artifacts) > 1: |
| 183 | raise ValueError("Must have exactly one output_artifact entry") |
LaMont Jones | dec69ad | 2021-01-27 13:02:00 -0700 | [diff] [blame] | 184 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 185 | with osutils.TempDir(delete=False) as tmpdir: |
| 186 | info = input_proto.artifacts.output_artifacts[0] |
| 187 | metadata_path = os.path.join(tmpdir, "firmware_metadata.jsonpb") |
| 188 | args = [] |
| 189 | if input_proto.artifacts.FIRMWARE_LCOV in info.artifact_types: |
| 190 | args += ["--code-coverage"] |
| 191 | resp = _call_entry( |
| 192 | info.location, |
| 193 | None, |
| 194 | "bundle", |
| 195 | *args, |
| 196 | output_dir=tmpdir, |
| 197 | metadata=metadata_path, |
| 198 | ) |
| 199 | file_paths = [] |
| 200 | if os.path.exists(metadata_path): |
Mike Frysinger | d97829b | 2023-02-24 16:09:20 -0500 | [diff] [blame] | 201 | with open(metadata_path, "r", encoding="utf-8") as f: |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 202 | metadata = json_format.Parse( |
| 203 | f.read(), firmware_pb2.FirmwareArtifactInfo() |
| 204 | ) |
| 205 | else: |
| 206 | metadata = firmware_pb2.FirmwareArtifactInfo() |
| 207 | if input_proto.artifacts.FIRMWARE_TARBALL_INFO in info.artifact_types: |
| 208 | output_proto.artifacts.artifacts.add( |
| 209 | artifact_type=input_proto.artifacts.FIRMWARE_TARBALL_INFO, |
| 210 | location=info.location, |
| 211 | paths=[ |
| 212 | common_pb2.Path( |
| 213 | path=metadata_path, location=common_pb2.Path.INSIDE |
| 214 | ) |
| 215 | ], |
| 216 | ) |
LaMont Jones | 9a0594d | 2021-03-17 15:51:20 -0600 | [diff] [blame] | 217 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 218 | full_path = lambda x: common_pb2.Path( |
| 219 | path=os.path.join(tmpdir, x.file_name), |
| 220 | location=common_pb2.Path.INSIDE, |
| 221 | ) |
LaMont Jones | 9a0594d | 2021-03-17 15:51:20 -0600 | [diff] [blame] | 222 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 223 | for typ, name in ( |
| 224 | (input_proto.artifacts.FIRMWARE_TARBALL, "tarball_info"), |
| 225 | (input_proto.artifacts.FIRMWARE_LCOV, "lcov_info"), |
| 226 | (input_proto.artifacts.CODE_COVERAGE_HTML, "coverage_html"), |
| 227 | ): |
| 228 | file_paths = [ |
| 229 | full_path(x) |
| 230 | for x in metadata.objects |
| 231 | if x.WhichOneof("firmware_object_info") == name |
| 232 | ] |
| 233 | if file_paths and typ in info.artifact_types: |
| 234 | output_proto.artifacts.artifacts.add( |
| 235 | artifact_type=typ, paths=file_paths, location=info.location |
| 236 | ) |
LaMont Jones | 9a0594d | 2021-03-17 15:51:20 -0600 | [diff] [blame] | 237 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 238 | return resp |