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