David Burger | 7fd1dbe | 2020-03-26 09:26:55 -0600 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
| 2 | # -*- coding: utf-8 -*- |
| 3 | # Copyright 2020 The Chromium OS Authors. All rights reserved. |
| 4 | # Use of this source code is governed by a BSD-style license that can be |
| 5 | # found in the LICENSE file. |
| 6 | """Transforms config from /config/proto/api proto format to platform JSON.""" |
| 7 | |
| 8 | import argparse |
| 9 | import json |
| 10 | import pprint |
| 11 | import sys |
| 12 | |
| 13 | from collections import namedtuple |
| 14 | |
C Shapiro | fd83a5f | 2020-03-31 08:56:20 -0500 | [diff] [blame] | 15 | from config.payload import config_bundle_pb2 |
David Burger | 7fd1dbe | 2020-03-26 09:26:55 -0600 | [diff] [blame] | 16 | from config.api import device_brand_pb2 |
| 17 | from config.api.software import brand_config_pb2 |
| 18 | |
| 19 | Config = namedtuple('Config', |
| 20 | ['program', |
| 21 | 'hw_design', |
| 22 | 'odm', |
| 23 | 'hw_design_config', |
| 24 | 'device_brand', |
| 25 | 'oem', |
| 26 | 'sw_config', |
| 27 | 'brand_config', |
| 28 | 'build_target']) |
| 29 | |
| 30 | |
| 31 | def ParseArgs(argv): |
| 32 | """Parse the available arguments. |
| 33 | |
| 34 | Invalid arguments or -h cause this function to print a message and exit. |
| 35 | |
| 36 | Args: |
| 37 | argv: List of string arguments (excluding program name / argv[0]) |
| 38 | |
| 39 | Returns: |
| 40 | argparse.Namespace object containing the attributes. |
| 41 | """ |
| 42 | parser = argparse.ArgumentParser( |
| 43 | description='Converts source proto config into platform JSON config.') |
| 44 | parser.add_argument( |
| 45 | '-c', |
| 46 | '--project_configs', |
| 47 | nargs='+', |
| 48 | type=str, |
| 49 | help='Space delimited list of source protobinary project config files.') |
| 50 | parser.add_argument( |
| 51 | '-p', |
| 52 | '--program_config', |
| 53 | type=str, |
| 54 | help='Path to the source program-level protobinary file') |
| 55 | parser.add_argument( |
| 56 | '-o', |
| 57 | '--output', |
| 58 | type=str, |
| 59 | help='Output file that will be generated') |
| 60 | return parser.parse_args(argv) |
| 61 | |
| 62 | |
| 63 | def _Set(field, target, target_name): |
| 64 | if field: |
| 65 | target[target_name] = field |
| 66 | |
| 67 | |
| 68 | def _BuildArc(config): |
| 69 | if config.build_target.arc: |
| 70 | build_properties = { |
| 71 | 'device': config.build_target.arc.device, |
| 72 | 'first-api-level': config.build_target.arc.first_api_level, |
| 73 | 'marketing-name': config.device_brand.brand_name, |
| 74 | 'metrics-tag': config.hw_design.name.lower(), |
Andrew Lamb | b47b7dc | 2020-04-07 10:20:32 -0600 | [diff] [blame] | 75 | 'product': config.build_target.id.value, |
David Burger | 7fd1dbe | 2020-03-26 09:26:55 -0600 | [diff] [blame] | 76 | } |
| 77 | if config.oem: |
| 78 | build_properties['oem'] = config.oem.name |
| 79 | return { |
| 80 | 'build-properties': build_properties |
| 81 | } |
| 82 | |
| 83 | |
| 84 | def _BuildFingerprint(hw_topology): |
Andrew Lamb | c2c5546 | 2020-04-06 08:43:34 -0600 | [diff] [blame] | 85 | if hw_topology.HasField('fingerprint'): |
David Burger | 7fd1dbe | 2020-03-26 09:26:55 -0600 | [diff] [blame] | 86 | fp = hw_topology.fingerprint.hardware_feature.fingerprint |
| 87 | location = fp.Location.DESCRIPTOR.values_by_number[fp.location].name |
| 88 | result = { |
| 89 | 'sensor-location': location.lower().replace('_', '-'), |
| 90 | } |
| 91 | if fp.board: |
| 92 | result['board'] = fp.board |
| 93 | return result |
| 94 | |
| 95 | |
| 96 | def _FwBcsPath(payload): |
| 97 | if payload and payload.firmware_image_name: |
| 98 | return 'bcs://%s.%d.%d.0.tbz2' % ( |
| 99 | payload.firmware_image_name, |
| 100 | payload.version.major, |
| 101 | payload.version.minor) |
| 102 | |
| 103 | |
| 104 | def _FwBuildTarget(payload): |
| 105 | if payload: |
| 106 | return payload.build_target_name |
| 107 | |
| 108 | |
| 109 | def _BuildFirmware(config): |
David Burger | 7fd1dbe | 2020-03-26 09:26:55 -0600 | [diff] [blame] | 110 | fw = config.sw_config.firmware |
| 111 | main_ro = fw.main_ro_payload |
| 112 | main_rw = fw.main_rw_payload |
| 113 | ec_ro = fw.ec_ro_payload |
| 114 | pd_ro = fw.pd_ro_payload |
| 115 | |
| 116 | build_targets = {} |
| 117 | _Set(_FwBuildTarget(main_ro), build_targets, 'depthcharge') |
| 118 | # Default to RO build target if no RW set |
| 119 | _Set(_FwBuildTarget(main_rw) or _FwBuildTarget(main_ro), |
| 120 | build_targets, |
| 121 | 'coreboot') |
| 122 | _Set(_FwBuildTarget(ec_ro), build_targets, 'ec') |
| 123 | _Set(list(fw.ec_extras), build_targets, 'ec_extras') |
| 124 | # Default to EC build target if no PD set |
| 125 | _Set(_FwBuildTarget(pd_ro) or _FwBuildTarget(ec_ro), |
| 126 | build_targets, |
| 127 | 'libpayload') |
| 128 | |
| 129 | result = { |
| 130 | 'bcs-overlay': config.build_target.overlay_name, |
| 131 | 'build-targets': build_targets, |
David Burger | 7fd1dbe | 2020-03-26 09:26:55 -0600 | [diff] [blame] | 132 | } |
Andrew Lamb | 883fa04 | 2020-04-06 11:37:22 -0600 | [diff] [blame] | 133 | |
| 134 | _Set(main_ro.firmware_image_name.lower(), result, 'image-name') |
| 135 | |
| 136 | if not any(( |
| 137 | main_ro.firmware_image_name, |
| 138 | main_rw.firmware_image_name, |
| 139 | ec_ro.firmware_image_name, |
| 140 | pd_ro.firmware_image_name, |
| 141 | )): |
Andrew Lamb | b9e660f | 2020-04-06 11:37:22 -0600 | [diff] [blame] | 142 | result['no-firmware'] = True |
Andrew Lamb | 883fa04 | 2020-04-06 11:37:22 -0600 | [diff] [blame] | 143 | |
| 144 | _Set(_FwBcsPath(main_ro), result, 'main-ro-image') |
| 145 | _Set(_FwBcsPath(main_rw), result, 'main-rw-image') |
| 146 | _Set(_FwBcsPath(ec_ro), result, 'ec-ro-image') |
| 147 | _Set(_FwBcsPath(pd_ro), result, 'pd-ro-image') |
David Burger | 7fd1dbe | 2020-03-26 09:26:55 -0600 | [diff] [blame] | 148 | |
Andrew Lamb | f39fbe8 | 2020-04-13 16:14:33 -0600 | [diff] [blame] | 149 | _Set( |
| 150 | config.hw_design_config.hardware_features.fw_config.value, |
| 151 | result, |
| 152 | 'firmware-config', |
| 153 | ) |
| 154 | |
David Burger | 7fd1dbe | 2020-03-26 09:26:55 -0600 | [diff] [blame] | 155 | return result |
| 156 | |
| 157 | |
| 158 | def _BuildFwSigning(config): |
| 159 | if not config.sw_config.firmware: |
| 160 | return {} |
| 161 | # TODO(shapiroc): Source signing config from separate private repo |
| 162 | return { |
| 163 | 'key-id': 'DEFAULT', |
| 164 | 'signature-id': config.hw_design.name.lower(), |
| 165 | } |
| 166 | |
| 167 | |
| 168 | def _File(source, destination): |
| 169 | return { |
| 170 | 'destination': destination, |
| 171 | 'source': source |
| 172 | } |
| 173 | |
| 174 | |
| 175 | def _BuildAudio(config): |
| 176 | alsa_path = '/usr/share/alsa/ucm' |
| 177 | cras_path = '/etc/cras' |
| 178 | project_name = config.hw_design.name.lower() |
Andrew Lamb | 7d53678 | 2020-04-07 10:23:55 -0600 | [diff] [blame] | 179 | if not config.sw_config.HasField('audio_config'): |
David Burger | 7fd1dbe | 2020-03-26 09:26:55 -0600 | [diff] [blame] | 180 | return {} |
| 181 | audio = config.sw_config.audio_config |
| 182 | card = audio.card_name |
David Burger | 599ff7b | 2020-04-06 16:29:31 -0600 | [diff] [blame] | 183 | card_with_suffix = audio.card_name |
| 184 | if audio.ucm_suffix: |
| 185 | card_with_suffix += '.' + audio.ucm_suffix |
David Burger | 7fd1dbe | 2020-03-26 09:26:55 -0600 | [diff] [blame] | 186 | files = [] |
| 187 | if audio.ucm_file: |
David Burger | 599ff7b | 2020-04-06 16:29:31 -0600 | [diff] [blame] | 188 | files.append(_File( |
| 189 | audio.ucm_file, |
| 190 | '%s/%s/HiFi.conf' % (alsa_path, card_with_suffix))) |
David Burger | 7fd1dbe | 2020-03-26 09:26:55 -0600 | [diff] [blame] | 191 | if audio.ucm_master_file: |
| 192 | files.append(_File( |
David Burger | 599ff7b | 2020-04-06 16:29:31 -0600 | [diff] [blame] | 193 | audio.ucm_master_file, |
| 194 | '%s/%s/%s.conf' % (alsa_path, card_with_suffix, card_with_suffix))) |
David Burger | 7fd1dbe | 2020-03-26 09:26:55 -0600 | [diff] [blame] | 195 | if audio.card_config_file: |
| 196 | files.append(_File( |
| 197 | audio.card_config_file, '%s/%s/%s' % (cras_path, project_name, card))) |
| 198 | if audio.dsp_file: |
| 199 | files.append( |
David Burger | 2e25490 | 2020-04-02 16:56:01 -0600 | [diff] [blame] | 200 | _File(audio.dsp_file, '%s/%s/dsp.ini' % (cras_path, project_name))) |
David Burger | 599ff7b | 2020-04-06 16:29:31 -0600 | [diff] [blame] | 201 | |
| 202 | result = { |
David Burger | 7fd1dbe | 2020-03-26 09:26:55 -0600 | [diff] [blame] | 203 | 'main': { |
| 204 | 'cras-config-dir': project_name, |
| 205 | 'files': files, |
| 206 | } |
| 207 | } |
David Burger | 599ff7b | 2020-04-06 16:29:31 -0600 | [diff] [blame] | 208 | if audio.ucm_suffix: |
David Burger | 03cdcbd | 2020-04-13 13:54:48 -0600 | [diff] [blame] | 209 | result['main']['ucm-suffix'] = audio.ucm_suffix |
David Burger | 599ff7b | 2020-04-06 16:29:31 -0600 | [diff] [blame] | 210 | |
| 211 | return result |
David Burger | 7fd1dbe | 2020-03-26 09:26:55 -0600 | [diff] [blame] | 212 | |
| 213 | |
Andrew Lamb | 7806ce9 | 2020-04-07 10:22:17 -0600 | [diff] [blame] | 214 | def _BuildIdentity(hw_scan_config, program, brand_scan_config=None): |
David Burger | 7fd1dbe | 2020-03-26 09:26:55 -0600 | [diff] [blame] | 215 | identity = {} |
| 216 | _Set(hw_scan_config.firmware_sku, identity, 'sku-id') |
| 217 | _Set(hw_scan_config.smbios_name_match, identity, 'smbios-name-match') |
Andrew Lamb | 7806ce9 | 2020-04-07 10:22:17 -0600 | [diff] [blame] | 218 | # 'platform-name' is needed to support 'mosys platform name'. Clients should |
| 219 | # longer require platform name, but set it here for backwards compatibility. |
| 220 | _Set(program.name, identity, 'platform-name') |
David Burger | 7fd1dbe | 2020-03-26 09:26:55 -0600 | [diff] [blame] | 221 | # ARM architecture |
| 222 | _Set(hw_scan_config.device_tree_compatible_match, identity, |
| 223 | 'device-tree-compatible-match') |
| 224 | |
| 225 | if brand_scan_config: |
| 226 | _Set(brand_scan_config.whitelabel_tag, identity, 'whitelabel-tag') |
| 227 | |
| 228 | return identity |
| 229 | |
| 230 | |
| 231 | def _Lookup(id_value, id_map): |
| 232 | if id_value.value: |
| 233 | key = id_value.value |
| 234 | if key in id_map: |
| 235 | return id_map[id_value.value] |
| 236 | error = 'Failed to lookup %s with value: %s' % ( |
| 237 | id_value.__class__.__name__.replace('Id', ''), key) |
| 238 | print(error) |
| 239 | print('Check the config contents provided:') |
| 240 | pp = pprint.PrettyPrinter(indent=4) |
| 241 | pp.pprint(id_map) |
| 242 | raise Exception(error) |
| 243 | |
| 244 | |
| 245 | def _TransformBuildConfigs(config): |
| 246 | partners = dict([(x.id.value, x) for x in config.partners.value]) |
| 247 | programs = dict([(x.id.value, x) for x in config.programs.value]) |
David Burger | 7fd1dbe | 2020-03-26 09:26:55 -0600 | [diff] [blame] | 248 | sw_configs = list(config.software_configs) |
| 249 | brand_configs = dict([(x.brand_id.value, x) for x in config.brand_configs]) |
| 250 | |
C Shapiro | a0b766c | 2020-03-31 08:35:28 -0500 | [diff] [blame] | 251 | if len(config.build_targets) != 1: |
| 252 | # Artifact of sharing the config_bundle for analysis and transforms. |
| 253 | # Integrated analysis of multiple programs/projects it the only time |
| 254 | # having multiple build targets would be valid. |
| 255 | raise Exception('Single build_target required for transform') |
| 256 | |
David Burger | 7fd1dbe | 2020-03-26 09:26:55 -0600 | [diff] [blame] | 257 | results = {} |
| 258 | for hw_design in config.designs.value: |
| 259 | if config.device_brands.value: |
| 260 | device_brands = [x for x in config.device_brands.value |
| 261 | if x.design_id.value == hw_design.id.value] |
| 262 | else: |
| 263 | device_brands = [device_brand_pb2.DeviceBrand()] |
| 264 | |
| 265 | for device_brand in device_brands: |
| 266 | # Brand config can be empty since platform JSON config allows it |
| 267 | brand_config = brand_config_pb2.BrandConfig() |
| 268 | if device_brand.id.value in brand_configs: |
| 269 | brand_config = brand_configs[device_brand.id.value] |
| 270 | |
| 271 | for hw_design_config in hw_design.configs: |
| 272 | design_id = hw_design_config.id.value |
| 273 | sw_config_matches = [x for x in sw_configs |
| 274 | if x.design_config_id.value == design_id] |
| 275 | if len(sw_config_matches) == 1: |
| 276 | sw_config = sw_config_matches[0] |
| 277 | elif len(sw_config_matches) > 1: |
| 278 | raise Exception('Multiple software configs found for: %s' % design_id) |
| 279 | else: |
| 280 | raise Exception('Software config is required for: %s' % design_id) |
| 281 | |
| 282 | transformed_config = _TransformBuildConfig(Config( |
| 283 | program=_Lookup(hw_design.program_id, programs), |
| 284 | hw_design=hw_design, |
| 285 | odm=_Lookup(hw_design.odm_id, partners), |
| 286 | hw_design_config=hw_design_config, |
| 287 | device_brand=device_brand, |
| 288 | oem=_Lookup(device_brand.oem_id, partners), |
| 289 | sw_config=sw_config, |
| 290 | brand_config=brand_config, |
C Shapiro | a0b766c | 2020-03-31 08:35:28 -0500 | [diff] [blame] | 291 | build_target=config.build_targets[0])) |
David Burger | 7fd1dbe | 2020-03-26 09:26:55 -0600 | [diff] [blame] | 292 | |
| 293 | config_json = json.dumps(transformed_config, |
| 294 | sort_keys=True, |
| 295 | indent=2, |
| 296 | separators=(',', ': ')) |
| 297 | |
| 298 | if config_json not in results: |
| 299 | results[config_json] = transformed_config |
| 300 | |
| 301 | return list(results.values()) |
| 302 | |
| 303 | |
| 304 | def _TransformBuildConfig(config): |
| 305 | """Transforms Config instance into target platform JSON schema. |
| 306 | |
| 307 | Args: |
| 308 | config: Config namedtuple |
| 309 | |
| 310 | Returns: |
| 311 | Unique config payload based on the platform JSON schema. |
| 312 | """ |
| 313 | result = { |
| 314 | 'identity': _BuildIdentity( |
| 315 | config.sw_config.id_scan_config, |
Andrew Lamb | 7806ce9 | 2020-04-07 10:22:17 -0600 | [diff] [blame] | 316 | config.program, |
David Burger | 7fd1dbe | 2020-03-26 09:26:55 -0600 | [diff] [blame] | 317 | config.brand_config.scan_config), |
| 318 | 'name': config.hw_design.name.lower(), |
| 319 | } |
| 320 | |
| 321 | _Set(_BuildArc(config), result, 'arc') |
| 322 | _Set(_BuildAudio(config), result, 'audio') |
| 323 | _Set(config.device_brand.brand_code, result, 'brand-code') |
| 324 | _Set(_BuildFirmware(config), result, 'firmware') |
| 325 | _Set(_BuildFwSigning(config), result, 'firmware-signing') |
| 326 | _Set(_BuildFingerprint( |
| 327 | config.hw_design_config.hardware_topology), result, 'fingerprint') |
| 328 | power_prefs = config.sw_config.power_config.preferences |
| 329 | power_prefs_map = dict( |
| 330 | (x.replace('_', '-'), |
| 331 | power_prefs[x]) for x in power_prefs) |
| 332 | _Set(power_prefs_map, result, 'power') |
| 333 | |
| 334 | return result |
| 335 | |
| 336 | |
| 337 | def WriteOutput(configs, output=None): |
| 338 | """Writes a list of configs to platform JSON format. |
| 339 | |
| 340 | Args: |
| 341 | configs: List of config dicts defined in cros_config_schema.yaml |
| 342 | output: Target file output (if None, prints to stdout) |
| 343 | """ |
| 344 | json_output = json.dumps( |
| 345 | {'chromeos': { |
| 346 | 'configs': configs, |
| 347 | }}, |
| 348 | sort_keys=True, |
| 349 | indent=2, |
| 350 | separators=(',', ': ')) |
| 351 | if output: |
| 352 | with open(output, 'w') as output_stream: |
| 353 | # Using print function adds proper trailing newline. |
| 354 | print(json_output, file=output_stream) |
| 355 | else: |
| 356 | print(json_output) |
| 357 | |
| 358 | |
| 359 | def _ReadConfig(path): |
| 360 | """Reads a binary proto from a file. |
| 361 | |
| 362 | Args: |
| 363 | path: Path to the binary proto. |
| 364 | """ |
| 365 | config = config_bundle_pb2.ConfigBundle() |
| 366 | with open(path, 'rb') as f: |
| 367 | config.ParseFromString(f.read()) |
| 368 | return config |
| 369 | |
| 370 | |
| 371 | def _MergeConfigs(configs): |
| 372 | result = config_bundle_pb2.ConfigBundle() |
| 373 | for config in configs: |
| 374 | result.MergeFrom(config) |
| 375 | |
| 376 | return result |
| 377 | |
| 378 | |
| 379 | def Main(project_configs, |
| 380 | program_config, |
| 381 | output): |
| 382 | """Transforms source proto config into platform JSON. |
| 383 | |
| 384 | Args: |
| 385 | project_configs: List of source project configs to transform. |
| 386 | program_config: Program config for the given set of projects. |
| 387 | output: Output file that will be generated by the transform. |
| 388 | """ |
| 389 | WriteOutput( |
| 390 | _TransformBuildConfigs( |
| 391 | _MergeConfigs( |
| 392 | [_ReadConfig(program_config)] + |
| 393 | [_ReadConfig(config) for config in project_configs],) |
| 394 | ,), |
| 395 | output) |
| 396 | |
| 397 | |
| 398 | def main(argv=None): |
| 399 | """Main program which parses args and runs |
| 400 | |
| 401 | Args: |
| 402 | argv: List of command line arguments, if None uses sys.argv. |
| 403 | """ |
| 404 | if argv is None: |
| 405 | argv = sys.argv[1:] |
| 406 | opts = ParseArgs(argv) |
| 407 | Main(opts.project_configs, opts.program_config, opts.output) |
| 408 | |
| 409 | |
| 410 | if __name__ == '__main__': |
| 411 | sys.exit(main(sys.argv[1:])) |