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. |
| 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 | |
| 14 | from google.protobuf import json_format |
| 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 |
| 23 | |
LaMont Jones | dec69ad | 2021-01-27 13:02:00 -0700 | [diff] [blame] | 24 | def _call_entry(fw_loc, metric_proto, subcmd, **kwargs): |
Jett Rink | 17ed0f5 | 2020-09-25 17:14:31 -0600 | [diff] [blame] | 25 | """Calls into firmware_builder.py with the specified subcmd.""" |
| 26 | |
LaMont Jones | dec69ad | 2021-01-27 13:02:00 -0700 | [diff] [blame] | 27 | if fw_loc == common_pb2.PLATFORM_EC: |
Jett Rink | 17ed0f5 | 2020-09-25 17:14:31 -0600 | [diff] [blame] | 28 | fw_path = 'src/platform/ec/' |
LaMont Jones | dec69ad | 2021-01-27 13:02:00 -0700 | [diff] [blame] | 29 | elif fw_loc == common_pb2.PLATFORM_ZEPHYR: |
Jett Rink | 17ed0f5 | 2020-09-25 17:14:31 -0600 | [diff] [blame] | 30 | fw_path = 'src/platform/zephyr-chrome/' |
LaMont Jones | dec69ad | 2021-01-27 13:02:00 -0700 | [diff] [blame] | 31 | elif fw_loc == common_pb2.PLATFORM_TI50: |
Andrew Luo | 60439f3 | 2021-01-26 15:19:28 -0800 | [diff] [blame] | 32 | fw_path = 'src/platform/ti50/common/' |
Jett Rink | 17ed0f5 | 2020-09-25 17:14:31 -0600 | [diff] [blame] | 33 | else: |
LaMont Jones | dec69ad | 2021-01-27 13:02:00 -0700 | [diff] [blame] | 34 | cros_build_lib.Die(f'Unknown firmware location {fw_loc}.') |
Jett Rink | 17ed0f5 | 2020-09-25 17:14:31 -0600 | [diff] [blame] | 35 | |
| 36 | entry_point = os.path.join(constants.SOURCE_ROOT, |
| 37 | fw_path, 'firmware_builder.py') |
| 38 | |
LaMont Jones | dec69ad | 2021-01-27 13:02:00 -0700 | [diff] [blame] | 39 | with tempfile.NamedTemporaryFile() as tmpfile: |
| 40 | cmd = [entry_point, '--metrics', tmpfile.name, subcmd] |
| 41 | for key, value in kwargs.items(): |
| 42 | cmd += [f'--{key.replace("_", "-")}', value] |
| 43 | |
| 44 | result = cros_build_lib.run(cmd, check=False) |
| 45 | with open(tmpfile.name, 'r') as f: |
Jett Rink | 17ed0f5 | 2020-09-25 17:14:31 -0600 | [diff] [blame] | 46 | response = f.read() |
| 47 | |
| 48 | # Parse the entire metric file as our metric proto (as a passthru) |
| 49 | json_format.Parse(response, metric_proto) |
| 50 | |
| 51 | if result.returncode == 0: |
| 52 | return controller.RETURN_CODE_SUCCESS |
| 53 | else: |
| 54 | return controller.RETURN_CODE_COMPLETED_UNSUCCESSFULLY |
| 55 | |
| 56 | |
| 57 | def _BuildAllTotFirmwareResponse(_input_proto, output_proto, _config): |
LaMont Jones | dec69ad | 2021-01-27 13:02:00 -0700 | [diff] [blame] | 58 | """Add a fw region metric to a successful response.""" |
Jett Rink | 17ed0f5 | 2020-09-25 17:14:31 -0600 | [diff] [blame] | 59 | |
| 60 | metric = output_proto.success.value.add() |
| 61 | metric.target_name = 'foo' |
| 62 | metric.platform_name = 'bar' |
| 63 | fw_section = metric.fw_section.add() |
| 64 | fw_section.region = firmware_pb2.FwBuildMetric.FwSection.EC_RO |
| 65 | fw_section.used = 100 |
| 66 | fw_section.total = 150 |
| 67 | |
| 68 | @faux.success(_BuildAllTotFirmwareResponse) |
| 69 | @faux.empty_completed_unsuccessfully_error |
| 70 | @validate.require('firmware_location') |
| 71 | @validate.validation_complete |
| 72 | def BuildAllTotFirmware(input_proto, output_proto, _config): |
| 73 | """Build all of the firmware targets at the specified location.""" |
| 74 | |
| 75 | return _call_entry(input_proto.firmware_location, output_proto.metrics, |
| 76 | 'build') |
| 77 | |
| 78 | |
| 79 | def _TestAllTotFirmwareResponse(_input_proto, output_proto, _config): |
LaMont Jones | dec69ad | 2021-01-27 13:02:00 -0700 | [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 | |
| 82 | metric = output_proto.success.value.add() |
| 83 | metric.name = 'foo-test' |
| 84 | |
| 85 | @faux.success(_TestAllTotFirmwareResponse) |
| 86 | @faux.empty_completed_unsuccessfully_error |
| 87 | @validate.require('firmware_location') |
| 88 | @validate.validation_complete |
| 89 | def TestAllTotFirmware(input_proto, output_proto, _config): |
| 90 | """Runs all of the firmware tests at the specified location.""" |
| 91 | |
| 92 | return _call_entry(input_proto.firmware_location, output_proto.metrics, |
| 93 | 'test') |
LaMont Jones | dec69ad | 2021-01-27 13:02:00 -0700 | [diff] [blame] | 94 | |
| 95 | |
| 96 | def _BuildAllFirmwareResponse(_input_proto, output_proto, _config): |
| 97 | """Add a fw region metric to a successful response.""" |
| 98 | |
| 99 | metric = output_proto.success.value.add() |
| 100 | metric.target_name = 'foo' |
| 101 | metric.platform_name = 'bar' |
| 102 | fw_section = metric.fw_section.add() |
| 103 | fw_section.region = firmware_pb2.FwBuildMetric.FwSection.EC_RO |
| 104 | fw_section.used = 100 |
| 105 | fw_section.total = 150 |
| 106 | |
| 107 | @faux.success(_BuildAllFirmwareResponse) |
| 108 | @faux.empty_completed_unsuccessfully_error |
| 109 | @validate.require('firmware_location') |
| 110 | @validate.validation_complete |
| 111 | def BuildAllFirmware(input_proto, output_proto, _config): |
| 112 | """Build all of the firmware targets at the specified location.""" |
| 113 | |
| 114 | return _call_entry(input_proto.firmware_location, output_proto.metrics, |
| 115 | 'build') |
| 116 | |
| 117 | |
| 118 | def _TestAllFirmwareResponse(_input_proto, output_proto, _config): |
| 119 | """Add a fw region metric to a successful response.""" |
| 120 | |
| 121 | metric = output_proto.success.value.add() |
| 122 | metric.name = 'foo-test' |
| 123 | |
| 124 | @faux.success(_TestAllFirmwareResponse) |
| 125 | @faux.empty_completed_unsuccessfully_error |
| 126 | @validate.require('firmware_location') |
| 127 | @validate.validation_complete |
| 128 | def TestAllFirmware(input_proto, output_proto, _config): |
| 129 | """Runs all of the firmware tests at the specified location.""" |
| 130 | |
| 131 | return _call_entry(input_proto.firmware_location, output_proto.metrics, |
| 132 | 'test') |
| 133 | |
| 134 | |
| 135 | def _BundleFirmwareArtifactsResponse(_input_proto, output_proto, _config): |
| 136 | """Add a fw region metric to a successful response.""" |
| 137 | |
| 138 | metric = output_proto.success.value.add() |
| 139 | metric.name = 'foo-test' |
| 140 | |
| 141 | @faux.success(_BundleFirmwareArtifactsResponse) |
| 142 | @faux.empty_completed_unsuccessfully_error |
| 143 | @validate.require('firmware_location') |
| 144 | @validate.validation_complete |
| 145 | def BundleFirmwareArtifacts(input_proto, output_proto, _config): |
| 146 | """Runs all of the firmware tests at the specified location.""" |
| 147 | |
| 148 | if len(input_proto.output_artifacts) > 1: |
| 149 | raise ValueError('Must have exactly one output_artifact') |
| 150 | |
| 151 | with tempfile.NamedTemporaryFile() as meta: |
| 152 | info = input_proto.output_artifacts[0] |
| 153 | metadata_path = os.path.join(input_proto.result_path.path, meta.name) |
| 154 | resp = _call_entry( |
| 155 | info.location, output_proto.metrics, 'bundle', |
| 156 | output_dir=input_proto.result_path.path, metadata=metadata_path) |
| 157 | if common_pb2.FIRMWARE_TARBALL in info.artifact_types: |
| 158 | out = output_proto.artifacts.add() |
| 159 | out.artifact_types.append(common_pb2.FIRMWARE_TARBALL) |
| 160 | # TODO(b/177907747): gather the paths from the response and add them to |
| 161 | # out.paths. |
| 162 | out.location = info.location |
| 163 | if common_pb2.FIRMWARE_TARBALL_INFO in info.artifact_types: |
| 164 | out = output_proto.artifacts.add() |
| 165 | out.artifact_types.append(common_pb2.FIRMWARE_TARBALL_INFO) |
| 166 | out.paths.append( |
| 167 | common_pb2.Path(metadata_path, input_proto.result_path.path.location)) |
| 168 | return resp |