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 | |
| 15 | from config.api import config_bundle_pb2 |
| 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(), |
| 75 | 'product': config.hw_design.name.lower(), |
| 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): |
| 85 | if hw_topology.fingerprint: |
| 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): |
| 110 | if not config.sw_config.firmware: |
| 111 | return { |
| 112 | 'no-firmware': True, |
| 113 | } |
| 114 | fw = config.sw_config.firmware |
| 115 | main_ro = fw.main_ro_payload |
| 116 | main_rw = fw.main_rw_payload |
| 117 | ec_ro = fw.ec_ro_payload |
| 118 | pd_ro = fw.pd_ro_payload |
| 119 | |
| 120 | build_targets = {} |
| 121 | _Set(_FwBuildTarget(main_ro), build_targets, 'depthcharge') |
| 122 | # Default to RO build target if no RW set |
| 123 | _Set(_FwBuildTarget(main_rw) or _FwBuildTarget(main_ro), |
| 124 | build_targets, |
| 125 | 'coreboot') |
| 126 | _Set(_FwBuildTarget(ec_ro), build_targets, 'ec') |
| 127 | _Set(list(fw.ec_extras), build_targets, 'ec_extras') |
| 128 | # Default to EC build target if no PD set |
| 129 | _Set(_FwBuildTarget(pd_ro) or _FwBuildTarget(ec_ro), |
| 130 | build_targets, |
| 131 | 'libpayload') |
| 132 | |
| 133 | result = { |
| 134 | 'bcs-overlay': config.build_target.overlay_name, |
| 135 | 'build-targets': build_targets, |
| 136 | 'image-name': main_ro.firmware_image_name.lower(), |
| 137 | } |
| 138 | _Set(_FwBcsPath(fw.main_ro_payload), result, 'main-ro-image') |
| 139 | _Set(_FwBcsPath(fw.main_rw_payload), result, 'main-rw-image') |
| 140 | _Set(_FwBcsPath(fw.ec_ro_payload), result, 'ec-ro-image') |
| 141 | _Set(_FwBcsPath(fw.pd_ro_payload), result, 'pd-ro-image') |
| 142 | |
| 143 | return result |
| 144 | |
| 145 | |
| 146 | def _BuildFwSigning(config): |
| 147 | if not config.sw_config.firmware: |
| 148 | return {} |
| 149 | # TODO(shapiroc): Source signing config from separate private repo |
| 150 | return { |
| 151 | 'key-id': 'DEFAULT', |
| 152 | 'signature-id': config.hw_design.name.lower(), |
| 153 | } |
| 154 | |
| 155 | |
| 156 | def _File(source, destination): |
| 157 | return { |
| 158 | 'destination': destination, |
| 159 | 'source': source |
| 160 | } |
| 161 | |
| 162 | |
| 163 | def _BuildAudio(config): |
| 164 | alsa_path = '/usr/share/alsa/ucm' |
| 165 | cras_path = '/etc/cras' |
| 166 | project_name = config.hw_design.name.lower() |
| 167 | if not config.sw_config.audio_config: |
| 168 | return {} |
| 169 | audio = config.sw_config.audio_config |
| 170 | card = audio.card_name |
| 171 | files = [] |
| 172 | if audio.ucm_file: |
| 173 | files.append(_File(audio.ucm_file, '%s/%s/HiFi.conf' % (alsa_path, card))) |
| 174 | if audio.ucm_master_file: |
| 175 | files.append(_File( |
| 176 | audio.ucm_master_file, '%s/%s/%s.conf' % (alsa_path, card, card))) |
| 177 | if audio.card_config_file: |
| 178 | files.append(_File( |
| 179 | audio.card_config_file, '%s/%s/%s' % (cras_path, project_name, card))) |
| 180 | if audio.dsp_file: |
| 181 | files.append( |
| 182 | _File(audio.ucm_file, '%s/%s/dsp.ini' % (cras_path, project_name))) |
| 183 | return { |
| 184 | 'main': { |
| 185 | 'cras-config-dir': project_name, |
| 186 | 'files': files, |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | |
| 191 | def _BuildIdentity(hw_scan_config, brand_scan_config=None): |
| 192 | identity = {} |
| 193 | _Set(hw_scan_config.firmware_sku, identity, 'sku-id') |
| 194 | _Set(hw_scan_config.smbios_name_match, identity, 'smbios-name-match') |
| 195 | # Platform name is a redundant relic of mosys |
| 196 | _Set(hw_scan_config.smbios_name_match, identity, 'platform-name') |
| 197 | # ARM architecture |
| 198 | _Set(hw_scan_config.device_tree_compatible_match, identity, |
| 199 | 'device-tree-compatible-match') |
| 200 | |
| 201 | if brand_scan_config: |
| 202 | _Set(brand_scan_config.whitelabel_tag, identity, 'whitelabel-tag') |
| 203 | |
| 204 | return identity |
| 205 | |
| 206 | |
| 207 | def _Lookup(id_value, id_map): |
| 208 | if id_value.value: |
| 209 | key = id_value.value |
| 210 | if key in id_map: |
| 211 | return id_map[id_value.value] |
| 212 | error = 'Failed to lookup %s with value: %s' % ( |
| 213 | id_value.__class__.__name__.replace('Id', ''), key) |
| 214 | print(error) |
| 215 | print('Check the config contents provided:') |
| 216 | pp = pprint.PrettyPrinter(indent=4) |
| 217 | pp.pprint(id_map) |
| 218 | raise Exception(error) |
| 219 | |
| 220 | |
| 221 | def _TransformBuildConfigs(config): |
| 222 | partners = dict([(x.id.value, x) for x in config.partners.value]) |
| 223 | programs = dict([(x.id.value, x) for x in config.programs.value]) |
| 224 | build_targets = dict([(x.id.value, x) for x in config.build_targets]) |
| 225 | sw_configs = list(config.software_configs) |
| 226 | brand_configs = dict([(x.brand_id.value, x) for x in config.brand_configs]) |
| 227 | |
| 228 | results = {} |
| 229 | for hw_design in config.designs.value: |
| 230 | if config.device_brands.value: |
| 231 | device_brands = [x for x in config.device_brands.value |
| 232 | if x.design_id.value == hw_design.id.value] |
| 233 | else: |
| 234 | device_brands = [device_brand_pb2.DeviceBrand()] |
| 235 | |
| 236 | for device_brand in device_brands: |
| 237 | # Brand config can be empty since platform JSON config allows it |
| 238 | brand_config = brand_config_pb2.BrandConfig() |
| 239 | if device_brand.id.value in brand_configs: |
| 240 | brand_config = brand_configs[device_brand.id.value] |
| 241 | |
| 242 | for hw_design_config in hw_design.configs: |
| 243 | design_id = hw_design_config.id.value |
| 244 | sw_config_matches = [x for x in sw_configs |
| 245 | if x.design_config_id.value == design_id] |
| 246 | if len(sw_config_matches) == 1: |
| 247 | sw_config = sw_config_matches[0] |
| 248 | elif len(sw_config_matches) > 1: |
| 249 | raise Exception('Multiple software configs found for: %s' % design_id) |
| 250 | else: |
| 251 | raise Exception('Software config is required for: %s' % design_id) |
| 252 | |
| 253 | transformed_config = _TransformBuildConfig(Config( |
| 254 | program=_Lookup(hw_design.program_id, programs), |
| 255 | hw_design=hw_design, |
| 256 | odm=_Lookup(hw_design.odm_id, partners), |
| 257 | hw_design_config=hw_design_config, |
| 258 | device_brand=device_brand, |
| 259 | oem=_Lookup(device_brand.oem_id, partners), |
| 260 | sw_config=sw_config, |
| 261 | brand_config=brand_config, |
| 262 | build_target=_Lookup(hw_design.build_target_id, build_targets))) |
| 263 | |
| 264 | config_json = json.dumps(transformed_config, |
| 265 | sort_keys=True, |
| 266 | indent=2, |
| 267 | separators=(',', ': ')) |
| 268 | |
| 269 | if config_json not in results: |
| 270 | results[config_json] = transformed_config |
| 271 | |
| 272 | return list(results.values()) |
| 273 | |
| 274 | |
| 275 | def _TransformBuildConfig(config): |
| 276 | """Transforms Config instance into target platform JSON schema. |
| 277 | |
| 278 | Args: |
| 279 | config: Config namedtuple |
| 280 | |
| 281 | Returns: |
| 282 | Unique config payload based on the platform JSON schema. |
| 283 | """ |
| 284 | result = { |
| 285 | 'identity': _BuildIdentity( |
| 286 | config.sw_config.id_scan_config, |
| 287 | config.brand_config.scan_config), |
| 288 | 'name': config.hw_design.name.lower(), |
| 289 | } |
| 290 | |
| 291 | _Set(_BuildArc(config), result, 'arc') |
| 292 | _Set(_BuildAudio(config), result, 'audio') |
| 293 | _Set(config.device_brand.brand_code, result, 'brand-code') |
| 294 | _Set(_BuildFirmware(config), result, 'firmware') |
| 295 | _Set(_BuildFwSigning(config), result, 'firmware-signing') |
| 296 | _Set(_BuildFingerprint( |
| 297 | config.hw_design_config.hardware_topology), result, 'fingerprint') |
| 298 | power_prefs = config.sw_config.power_config.preferences |
| 299 | power_prefs_map = dict( |
| 300 | (x.replace('_', '-'), |
| 301 | power_prefs[x]) for x in power_prefs) |
| 302 | _Set(power_prefs_map, result, 'power') |
| 303 | |
| 304 | return result |
| 305 | |
| 306 | |
| 307 | def WriteOutput(configs, output=None): |
| 308 | """Writes a list of configs to platform JSON format. |
| 309 | |
| 310 | Args: |
| 311 | configs: List of config dicts defined in cros_config_schema.yaml |
| 312 | output: Target file output (if None, prints to stdout) |
| 313 | """ |
| 314 | json_output = json.dumps( |
| 315 | {'chromeos': { |
| 316 | 'configs': configs, |
| 317 | }}, |
| 318 | sort_keys=True, |
| 319 | indent=2, |
| 320 | separators=(',', ': ')) |
| 321 | if output: |
| 322 | with open(output, 'w') as output_stream: |
| 323 | # Using print function adds proper trailing newline. |
| 324 | print(json_output, file=output_stream) |
| 325 | else: |
| 326 | print(json_output) |
| 327 | |
| 328 | |
| 329 | def _ReadConfig(path): |
| 330 | """Reads a binary proto from a file. |
| 331 | |
| 332 | Args: |
| 333 | path: Path to the binary proto. |
| 334 | """ |
| 335 | config = config_bundle_pb2.ConfigBundle() |
| 336 | with open(path, 'rb') as f: |
| 337 | config.ParseFromString(f.read()) |
| 338 | return config |
| 339 | |
| 340 | |
| 341 | def _MergeConfigs(configs): |
| 342 | result = config_bundle_pb2.ConfigBundle() |
| 343 | for config in configs: |
| 344 | result.MergeFrom(config) |
| 345 | |
| 346 | return result |
| 347 | |
| 348 | |
| 349 | def Main(project_configs, |
| 350 | program_config, |
| 351 | output): |
| 352 | """Transforms source proto config into platform JSON. |
| 353 | |
| 354 | Args: |
| 355 | project_configs: List of source project configs to transform. |
| 356 | program_config: Program config for the given set of projects. |
| 357 | output: Output file that will be generated by the transform. |
| 358 | """ |
| 359 | WriteOutput( |
| 360 | _TransformBuildConfigs( |
| 361 | _MergeConfigs( |
| 362 | [_ReadConfig(program_config)] + |
| 363 | [_ReadConfig(config) for config in project_configs],) |
| 364 | ,), |
| 365 | output) |
| 366 | |
| 367 | |
| 368 | def main(argv=None): |
| 369 | """Main program which parses args and runs |
| 370 | |
| 371 | Args: |
| 372 | argv: List of command line arguments, if None uses sys.argv. |
| 373 | """ |
| 374 | if argv is None: |
| 375 | argv = sys.argv[1:] |
| 376 | opts = ParseArgs(argv) |
| 377 | Main(opts.project_configs, opts.program_config, opts.output) |
| 378 | |
| 379 | |
| 380 | if __name__ == '__main__': |
| 381 | sys.exit(main(sys.argv[1:])) |