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 |
C Shapiro | 90fda25 | 2020-04-17 14:34:57 -0500 | [diff] [blame] | 11 | import os |
David Burger | 7fd1dbe | 2020-03-26 09:26:55 -0600 | [diff] [blame] | 12 | import sys |
C Shapiro | 90fda25 | 2020-04-17 14:34:57 -0500 | [diff] [blame] | 13 | import re |
C Shapiro | 5bf23a7 | 2020-04-24 11:40:17 -0500 | [diff] [blame] | 14 | import xml.etree.ElementTree as etree |
C Shapiro | 9a3ac8c | 2020-04-25 07:49:21 -0500 | [diff] [blame^] | 15 | import xml.dom.minidom as minidom |
David Burger | 7fd1dbe | 2020-03-26 09:26:55 -0600 | [diff] [blame] | 16 | |
| 17 | from collections import namedtuple |
| 18 | |
Prathmesh Prabhu | 72f8a00 | 2020-04-10 09:57:53 -0700 | [diff] [blame] | 19 | from chromiumos.config.api import device_brand_pb2 |
David Burger | 92609a3 | 2020-04-23 10:38:50 -0600 | [diff] [blame] | 20 | from chromiumos.config.api import topology_pb2 |
C Shapiro | 5bf23a7 | 2020-04-24 11:40:17 -0500 | [diff] [blame] | 21 | from chromiumos.config.payload import config_bundle_pb2 |
Prathmesh Prabhu | 72f8a00 | 2020-04-10 09:57:53 -0700 | [diff] [blame] | 22 | from chromiumos.config.api.software import brand_config_pb2 |
David Burger | 7fd1dbe | 2020-03-26 09:26:55 -0600 | [diff] [blame] | 23 | |
| 24 | Config = namedtuple('Config', |
| 25 | ['program', |
| 26 | 'hw_design', |
| 27 | 'odm', |
| 28 | 'hw_design_config', |
| 29 | 'device_brand', |
C Shapiro | 2f0bb5d | 2020-04-14 10:07:47 -0500 | [diff] [blame] | 30 | 'device_signer_config', |
David Burger | 7fd1dbe | 2020-03-26 09:26:55 -0600 | [diff] [blame] | 31 | 'oem', |
| 32 | 'sw_config', |
| 33 | 'brand_config', |
| 34 | 'build_target']) |
| 35 | |
C Shapiro | 5bf23a7 | 2020-04-24 11:40:17 -0500 | [diff] [blame] | 36 | ConfigFiles = namedtuple('ConfigFiles', |
| 37 | ['bluetooth', |
| 38 | 'arc_hw_features']) |
| 39 | |
David Burger | 7fd1dbe | 2020-03-26 09:26:55 -0600 | [diff] [blame] | 40 | |
| 41 | def ParseArgs(argv): |
| 42 | """Parse the available arguments. |
| 43 | |
| 44 | Invalid arguments or -h cause this function to print a message and exit. |
| 45 | |
| 46 | Args: |
| 47 | argv: List of string arguments (excluding program name / argv[0]) |
| 48 | |
| 49 | Returns: |
| 50 | argparse.Namespace object containing the attributes. |
| 51 | """ |
| 52 | parser = argparse.ArgumentParser( |
| 53 | description='Converts source proto config into platform JSON config.') |
| 54 | parser.add_argument( |
| 55 | '-c', |
| 56 | '--project_configs', |
| 57 | nargs='+', |
| 58 | type=str, |
| 59 | help='Space delimited list of source protobinary project config files.') |
| 60 | parser.add_argument( |
| 61 | '-p', |
| 62 | '--program_config', |
| 63 | type=str, |
| 64 | help='Path to the source program-level protobinary file') |
| 65 | parser.add_argument( |
| 66 | '-o', |
| 67 | '--output', |
| 68 | type=str, |
| 69 | help='Output file that will be generated') |
| 70 | return parser.parse_args(argv) |
| 71 | |
| 72 | |
| 73 | def _Set(field, target, target_name): |
| 74 | if field: |
| 75 | target[target_name] = field |
| 76 | |
| 77 | |
C Shapiro | 5bf23a7 | 2020-04-24 11:40:17 -0500 | [diff] [blame] | 78 | def _BuildArc(config, config_files): |
David Burger | 7fd1dbe | 2020-03-26 09:26:55 -0600 | [diff] [blame] | 79 | if config.build_target.arc: |
| 80 | build_properties = { |
| 81 | 'device': config.build_target.arc.device, |
| 82 | 'first-api-level': config.build_target.arc.first_api_level, |
| 83 | 'marketing-name': config.device_brand.brand_name, |
| 84 | 'metrics-tag': config.hw_design.name.lower(), |
Andrew Lamb | b47b7dc | 2020-04-07 10:20:32 -0600 | [diff] [blame] | 85 | 'product': config.build_target.id.value, |
David Burger | 7fd1dbe | 2020-03-26 09:26:55 -0600 | [diff] [blame] | 86 | } |
| 87 | if config.oem: |
| 88 | build_properties['oem'] = config.oem.name |
C Shapiro | 5bf23a7 | 2020-04-24 11:40:17 -0500 | [diff] [blame] | 89 | result = { |
| 90 | 'build-properties': build_properties |
| 91 | } |
| 92 | feature_id = _ArcHardwareFeatureId(config.hw_design_config) |
| 93 | if feature_id in config_files.arc_hw_features: |
| 94 | result['hardware-features'] = config_files.arc_hw_features[feature_id] |
| 95 | return result |
David Burger | 7fd1dbe | 2020-03-26 09:26:55 -0600 | [diff] [blame] | 96 | |
C Shapiro | 90fda25 | 2020-04-17 14:34:57 -0500 | [diff] [blame] | 97 | def _BuildBluetooth(config, bluetooth_files): |
| 98 | bt_flags = config.sw_config.bluetooth_config.flags |
| 99 | # Convert to native map (from proto wrapper) |
| 100 | bt_flags_map = dict(bt_flags) |
| 101 | result = {} |
| 102 | if bt_flags_map: |
| 103 | result['flags'] = bt_flags_map |
| 104 | bt_comp = config.hw_design_config.hardware_features.bluetooth.component |
| 105 | if bt_comp.vendor_id: |
| 106 | bt_id = _BluetoothId(config.hw_design.name.lower(), bt_comp) |
| 107 | if bt_id in bluetooth_files: |
| 108 | result['config'] = bluetooth_files[bt_id] |
| 109 | return result |
| 110 | |
David Burger | 7fd1dbe | 2020-03-26 09:26:55 -0600 | [diff] [blame] | 111 | |
| 112 | def _BuildFingerprint(hw_topology): |
Andrew Lamb | c2c5546 | 2020-04-06 08:43:34 -0600 | [diff] [blame] | 113 | if hw_topology.HasField('fingerprint'): |
David Burger | 7fd1dbe | 2020-03-26 09:26:55 -0600 | [diff] [blame] | 114 | fp = hw_topology.fingerprint.hardware_feature.fingerprint |
David Burger | 92609a3 | 2020-04-23 10:38:50 -0600 | [diff] [blame] | 115 | result = {} |
| 116 | if fp.location != topology_pb2.HardwareFeatures.Fingerprint.NOT_PRESENT: |
| 117 | location = fp.Location.DESCRIPTOR.values_by_number[fp.location].name |
| 118 | result['sensor-location'] = location.lower().replace('_', '-') |
| 119 | if fp.board: |
| 120 | result['board'] = fp.board |
David Burger | 7fd1dbe | 2020-03-26 09:26:55 -0600 | [diff] [blame] | 121 | return result |
| 122 | |
| 123 | |
| 124 | def _FwBcsPath(payload): |
| 125 | if payload and payload.firmware_image_name: |
| 126 | return 'bcs://%s.%d.%d.0.tbz2' % ( |
| 127 | payload.firmware_image_name, |
| 128 | payload.version.major, |
| 129 | payload.version.minor) |
| 130 | |
| 131 | |
| 132 | def _FwBuildTarget(payload): |
| 133 | if payload: |
| 134 | return payload.build_target_name |
| 135 | |
| 136 | |
| 137 | def _BuildFirmware(config): |
Andrew Lamb | 3da156d | 2020-04-16 16:00:56 -0600 | [diff] [blame] | 138 | fw_payload_config = config.sw_config.firmware |
| 139 | fw_build_config = config.sw_config.firmware_build_config |
| 140 | main_ro = fw_payload_config.main_ro_payload |
| 141 | main_rw = fw_payload_config.main_rw_payload |
| 142 | ec_ro = fw_payload_config.ec_ro_payload |
| 143 | pd_ro = fw_payload_config.pd_ro_payload |
David Burger | 7fd1dbe | 2020-03-26 09:26:55 -0600 | [diff] [blame] | 144 | |
| 145 | build_targets = {} |
Andrew Lamb | 3da156d | 2020-04-16 16:00:56 -0600 | [diff] [blame] | 146 | |
Andrew Lamb | f8954ee | 2020-04-21 10:24:40 -0600 | [diff] [blame] | 147 | _Set(fw_build_config.build_targets.depthcharge, build_targets, 'depthcharge') |
| 148 | _Set(fw_build_config.build_targets.coreboot, build_targets, 'coreboot') |
| 149 | _Set(fw_build_config.build_targets.ec, build_targets, 'ec') |
| 150 | _Set( |
| 151 | list(fw_build_config.build_targets.ec_extras), build_targets, 'ec_extras') |
| 152 | _Set(fw_build_config.build_targets.libpayload, build_targets, 'libpayload') |
David Burger | 7fd1dbe | 2020-03-26 09:26:55 -0600 | [diff] [blame] | 153 | |
| 154 | result = { |
| 155 | 'bcs-overlay': config.build_target.overlay_name, |
| 156 | 'build-targets': build_targets, |
David Burger | 7fd1dbe | 2020-03-26 09:26:55 -0600 | [diff] [blame] | 157 | } |
Andrew Lamb | 883fa04 | 2020-04-06 11:37:22 -0600 | [diff] [blame] | 158 | |
| 159 | _Set(main_ro.firmware_image_name.lower(), result, 'image-name') |
| 160 | |
| 161 | if not any(( |
| 162 | main_ro.firmware_image_name, |
| 163 | main_rw.firmware_image_name, |
| 164 | ec_ro.firmware_image_name, |
| 165 | pd_ro.firmware_image_name, |
| 166 | )): |
Andrew Lamb | b9e660f | 2020-04-06 11:37:22 -0600 | [diff] [blame] | 167 | result['no-firmware'] = True |
Andrew Lamb | 883fa04 | 2020-04-06 11:37:22 -0600 | [diff] [blame] | 168 | |
| 169 | _Set(_FwBcsPath(main_ro), result, 'main-ro-image') |
| 170 | _Set(_FwBcsPath(main_rw), result, 'main-rw-image') |
| 171 | _Set(_FwBcsPath(ec_ro), result, 'ec-ro-image') |
| 172 | _Set(_FwBcsPath(pd_ro), result, 'pd-ro-image') |
David Burger | 7fd1dbe | 2020-03-26 09:26:55 -0600 | [diff] [blame] | 173 | |
Andrew Lamb | f39fbe8 | 2020-04-13 16:14:33 -0600 | [diff] [blame] | 174 | _Set( |
| 175 | config.hw_design_config.hardware_features.fw_config.value, |
| 176 | result, |
| 177 | 'firmware-config', |
| 178 | ) |
| 179 | |
David Burger | 7fd1dbe | 2020-03-26 09:26:55 -0600 | [diff] [blame] | 180 | return result |
| 181 | |
| 182 | |
| 183 | def _BuildFwSigning(config): |
C Shapiro | 2f0bb5d | 2020-04-14 10:07:47 -0500 | [diff] [blame] | 184 | if config.sw_config.firmware and config.device_signer_config: |
| 185 | return { |
| 186 | 'key-id': config.device_signer_config.key_id, |
| 187 | 'signature-id': config.hw_design.name.lower(), |
| 188 | } |
| 189 | return {} |
David Burger | 7fd1dbe | 2020-03-26 09:26:55 -0600 | [diff] [blame] | 190 | |
| 191 | |
| 192 | def _File(source, destination): |
| 193 | return { |
| 194 | 'destination': destination, |
| 195 | 'source': source |
| 196 | } |
| 197 | |
| 198 | |
| 199 | def _BuildAudio(config): |
| 200 | alsa_path = '/usr/share/alsa/ucm' |
| 201 | cras_path = '/etc/cras' |
| 202 | project_name = config.hw_design.name.lower() |
Andrew Lamb | 7d53678 | 2020-04-07 10:23:55 -0600 | [diff] [blame] | 203 | if not config.sw_config.HasField('audio_config'): |
David Burger | 7fd1dbe | 2020-03-26 09:26:55 -0600 | [diff] [blame] | 204 | return {} |
| 205 | audio = config.sw_config.audio_config |
| 206 | card = audio.card_name |
David Burger | 599ff7b | 2020-04-06 16:29:31 -0600 | [diff] [blame] | 207 | card_with_suffix = audio.card_name |
| 208 | if audio.ucm_suffix: |
| 209 | card_with_suffix += '.' + audio.ucm_suffix |
David Burger | 7fd1dbe | 2020-03-26 09:26:55 -0600 | [diff] [blame] | 210 | files = [] |
| 211 | if audio.ucm_file: |
David Burger | 599ff7b | 2020-04-06 16:29:31 -0600 | [diff] [blame] | 212 | files.append(_File( |
| 213 | audio.ucm_file, |
| 214 | '%s/%s/HiFi.conf' % (alsa_path, card_with_suffix))) |
David Burger | 7fd1dbe | 2020-03-26 09:26:55 -0600 | [diff] [blame] | 215 | if audio.ucm_master_file: |
| 216 | files.append(_File( |
David Burger | 599ff7b | 2020-04-06 16:29:31 -0600 | [diff] [blame] | 217 | audio.ucm_master_file, |
| 218 | '%s/%s/%s.conf' % (alsa_path, card_with_suffix, card_with_suffix))) |
David Burger | 7fd1dbe | 2020-03-26 09:26:55 -0600 | [diff] [blame] | 219 | if audio.card_config_file: |
| 220 | files.append(_File( |
| 221 | audio.card_config_file, '%s/%s/%s' % (cras_path, project_name, card))) |
| 222 | if audio.dsp_file: |
| 223 | files.append( |
David Burger | 2e25490 | 2020-04-02 16:56:01 -0600 | [diff] [blame] | 224 | _File(audio.dsp_file, '%s/%s/dsp.ini' % (cras_path, project_name))) |
David Burger | 599ff7b | 2020-04-06 16:29:31 -0600 | [diff] [blame] | 225 | |
| 226 | result = { |
David Burger | 7fd1dbe | 2020-03-26 09:26:55 -0600 | [diff] [blame] | 227 | 'main': { |
| 228 | 'cras-config-dir': project_name, |
| 229 | 'files': files, |
| 230 | } |
| 231 | } |
David Burger | 599ff7b | 2020-04-06 16:29:31 -0600 | [diff] [blame] | 232 | if audio.ucm_suffix: |
David Burger | 03cdcbd | 2020-04-13 13:54:48 -0600 | [diff] [blame] | 233 | result['main']['ucm-suffix'] = audio.ucm_suffix |
David Burger | 599ff7b | 2020-04-06 16:29:31 -0600 | [diff] [blame] | 234 | |
| 235 | return result |
David Burger | 7fd1dbe | 2020-03-26 09:26:55 -0600 | [diff] [blame] | 236 | |
| 237 | |
David Burger | 8aa8fa3 | 2020-04-14 08:30:34 -0600 | [diff] [blame] | 238 | def _BuildCamera(hw_topology): |
| 239 | if hw_topology.HasField('camera'): |
| 240 | camera = hw_topology.camera.hardware_feature.camera |
| 241 | result = {} |
| 242 | if camera.count.value: |
| 243 | result['count'] = camera.count.value |
| 244 | return result |
| 245 | |
| 246 | |
Andrew Lamb | 7806ce9 | 2020-04-07 10:22:17 -0600 | [diff] [blame] | 247 | def _BuildIdentity(hw_scan_config, program, brand_scan_config=None): |
David Burger | 7fd1dbe | 2020-03-26 09:26:55 -0600 | [diff] [blame] | 248 | identity = {} |
| 249 | _Set(hw_scan_config.firmware_sku, identity, 'sku-id') |
| 250 | _Set(hw_scan_config.smbios_name_match, identity, 'smbios-name-match') |
Andrew Lamb | 7806ce9 | 2020-04-07 10:22:17 -0600 | [diff] [blame] | 251 | # 'platform-name' is needed to support 'mosys platform name'. Clients should |
| 252 | # longer require platform name, but set it here for backwards compatibility. |
| 253 | _Set(program.name, identity, 'platform-name') |
David Burger | 7fd1dbe | 2020-03-26 09:26:55 -0600 | [diff] [blame] | 254 | # ARM architecture |
| 255 | _Set(hw_scan_config.device_tree_compatible_match, identity, |
| 256 | 'device-tree-compatible-match') |
| 257 | |
| 258 | if brand_scan_config: |
| 259 | _Set(brand_scan_config.whitelabel_tag, identity, 'whitelabel-tag') |
| 260 | |
| 261 | return identity |
| 262 | |
| 263 | |
| 264 | def _Lookup(id_value, id_map): |
| 265 | if id_value.value: |
| 266 | key = id_value.value |
| 267 | if key in id_map: |
| 268 | return id_map[id_value.value] |
| 269 | error = 'Failed to lookup %s with value: %s' % ( |
| 270 | id_value.__class__.__name__.replace('Id', ''), key) |
| 271 | print(error) |
| 272 | print('Check the config contents provided:') |
| 273 | pp = pprint.PrettyPrinter(indent=4) |
| 274 | pp.pprint(id_map) |
| 275 | raise Exception(error) |
| 276 | |
| 277 | |
C Shapiro | 5bf23a7 | 2020-04-24 11:40:17 -0500 | [diff] [blame] | 278 | def _TransformBuildConfigs(config, config_files=ConfigFiles({}, {})): |
David Burger | 7fd1dbe | 2020-03-26 09:26:55 -0600 | [diff] [blame] | 279 | partners = dict([(x.id.value, x) for x in config.partners.value]) |
| 280 | programs = dict([(x.id.value, x) for x in config.programs.value]) |
David Burger | 7fd1dbe | 2020-03-26 09:26:55 -0600 | [diff] [blame] | 281 | sw_configs = list(config.software_configs) |
| 282 | brand_configs = dict([(x.brand_id.value, x) for x in config.brand_configs]) |
| 283 | |
C Shapiro | a0b766c | 2020-03-31 08:35:28 -0500 | [diff] [blame] | 284 | if len(config.build_targets) != 1: |
| 285 | # Artifact of sharing the config_bundle for analysis and transforms. |
| 286 | # Integrated analysis of multiple programs/projects it the only time |
| 287 | # having multiple build targets would be valid. |
| 288 | raise Exception('Single build_target required for transform') |
| 289 | |
David Burger | 7fd1dbe | 2020-03-26 09:26:55 -0600 | [diff] [blame] | 290 | results = {} |
| 291 | for hw_design in config.designs.value: |
| 292 | if config.device_brands.value: |
| 293 | device_brands = [x for x in config.device_brands.value |
| 294 | if x.design_id.value == hw_design.id.value] |
| 295 | else: |
| 296 | device_brands = [device_brand_pb2.DeviceBrand()] |
| 297 | |
| 298 | for device_brand in device_brands: |
| 299 | # Brand config can be empty since platform JSON config allows it |
| 300 | brand_config = brand_config_pb2.BrandConfig() |
| 301 | if device_brand.id.value in brand_configs: |
| 302 | brand_config = brand_configs[device_brand.id.value] |
| 303 | |
| 304 | for hw_design_config in hw_design.configs: |
| 305 | design_id = hw_design_config.id.value |
| 306 | sw_config_matches = [x for x in sw_configs |
| 307 | if x.design_config_id.value == design_id] |
| 308 | if len(sw_config_matches) == 1: |
| 309 | sw_config = sw_config_matches[0] |
| 310 | elif len(sw_config_matches) > 1: |
| 311 | raise Exception('Multiple software configs found for: %s' % design_id) |
| 312 | else: |
| 313 | raise Exception('Software config is required for: %s' % design_id) |
| 314 | |
C Shapiro | 2f0bb5d | 2020-04-14 10:07:47 -0500 | [diff] [blame] | 315 | program = _Lookup(hw_design.program_id, programs) |
| 316 | signer_configs = dict( |
| 317 | [(x.brand_id.value, x) for x in program.device_signer_configs]) |
| 318 | device_signer_config = None |
| 319 | if signer_configs: |
| 320 | device_signer_config = _Lookup(device_brand.id, signer_configs) |
| 321 | |
C Shapiro | 90fda25 | 2020-04-17 14:34:57 -0500 | [diff] [blame] | 322 | transformed_config = _TransformBuildConfig( |
| 323 | Config( |
| 324 | program=program, |
| 325 | hw_design=hw_design, |
| 326 | odm=_Lookup(hw_design.odm_id, partners), |
| 327 | hw_design_config=hw_design_config, |
| 328 | device_brand=device_brand, |
| 329 | device_signer_config=device_signer_config, |
| 330 | oem=_Lookup(device_brand.oem_id, partners), |
| 331 | sw_config=sw_config, |
| 332 | brand_config=brand_config, |
| 333 | build_target=config.build_targets[0]), |
C Shapiro | 5bf23a7 | 2020-04-24 11:40:17 -0500 | [diff] [blame] | 334 | config_files) |
David Burger | 7fd1dbe | 2020-03-26 09:26:55 -0600 | [diff] [blame] | 335 | |
| 336 | config_json = json.dumps(transformed_config, |
| 337 | sort_keys=True, |
| 338 | indent=2, |
| 339 | separators=(',', ': ')) |
| 340 | |
| 341 | if config_json not in results: |
| 342 | results[config_json] = transformed_config |
| 343 | |
| 344 | return list(results.values()) |
| 345 | |
| 346 | |
C Shapiro | 5bf23a7 | 2020-04-24 11:40:17 -0500 | [diff] [blame] | 347 | def _TransformBuildConfig(config, config_files): |
David Burger | 7fd1dbe | 2020-03-26 09:26:55 -0600 | [diff] [blame] | 348 | """Transforms Config instance into target platform JSON schema. |
| 349 | |
| 350 | Args: |
| 351 | config: Config namedtuple |
C Shapiro | 5bf23a7 | 2020-04-24 11:40:17 -0500 | [diff] [blame] | 352 | config_files: Map to look up the generated config files. |
David Burger | 7fd1dbe | 2020-03-26 09:26:55 -0600 | [diff] [blame] | 353 | |
| 354 | Returns: |
| 355 | Unique config payload based on the platform JSON schema. |
| 356 | """ |
| 357 | result = { |
| 358 | 'identity': _BuildIdentity( |
| 359 | config.sw_config.id_scan_config, |
Andrew Lamb | 7806ce9 | 2020-04-07 10:22:17 -0600 | [diff] [blame] | 360 | config.program, |
David Burger | 7fd1dbe | 2020-03-26 09:26:55 -0600 | [diff] [blame] | 361 | config.brand_config.scan_config), |
| 362 | 'name': config.hw_design.name.lower(), |
| 363 | } |
| 364 | |
C Shapiro | 5bf23a7 | 2020-04-24 11:40:17 -0500 | [diff] [blame] | 365 | _Set(_BuildArc(config, config_files), result, 'arc') |
David Burger | 7fd1dbe | 2020-03-26 09:26:55 -0600 | [diff] [blame] | 366 | _Set(_BuildAudio(config), result, 'audio') |
C Shapiro | 5bf23a7 | 2020-04-24 11:40:17 -0500 | [diff] [blame] | 367 | _Set(_BuildBluetooth(config, config_files.bluetooth), result, 'bluetooth') |
David Burger | 7fd1dbe | 2020-03-26 09:26:55 -0600 | [diff] [blame] | 368 | _Set(config.device_brand.brand_code, result, 'brand-code') |
David Burger | 8aa8fa3 | 2020-04-14 08:30:34 -0600 | [diff] [blame] | 369 | _Set(_BuildCamera( |
| 370 | config.hw_design_config.hardware_topology), result, 'camera') |
David Burger | 7fd1dbe | 2020-03-26 09:26:55 -0600 | [diff] [blame] | 371 | _Set(_BuildFirmware(config), result, 'firmware') |
| 372 | _Set(_BuildFwSigning(config), result, 'firmware-signing') |
| 373 | _Set(_BuildFingerprint( |
| 374 | config.hw_design_config.hardware_topology), result, 'fingerprint') |
| 375 | power_prefs = config.sw_config.power_config.preferences |
| 376 | power_prefs_map = dict( |
| 377 | (x.replace('_', '-'), |
| 378 | power_prefs[x]) for x in power_prefs) |
| 379 | _Set(power_prefs_map, result, 'power') |
| 380 | |
| 381 | return result |
| 382 | |
| 383 | |
| 384 | def WriteOutput(configs, output=None): |
| 385 | """Writes a list of configs to platform JSON format. |
| 386 | |
| 387 | Args: |
| 388 | configs: List of config dicts defined in cros_config_schema.yaml |
| 389 | output: Target file output (if None, prints to stdout) |
| 390 | """ |
| 391 | json_output = json.dumps( |
| 392 | {'chromeos': { |
| 393 | 'configs': configs, |
| 394 | }}, |
| 395 | sort_keys=True, |
| 396 | indent=2, |
| 397 | separators=(',', ': ')) |
| 398 | if output: |
| 399 | with open(output, 'w') as output_stream: |
| 400 | # Using print function adds proper trailing newline. |
| 401 | print(json_output, file=output_stream) |
| 402 | else: |
| 403 | print(json_output) |
| 404 | |
| 405 | |
C Shapiro | 90fda25 | 2020-04-17 14:34:57 -0500 | [diff] [blame] | 406 | def _BluetoothId(project_name, bt_comp): |
| 407 | return '_'.join([project_name, |
| 408 | bt_comp.vendor_id, |
| 409 | bt_comp.product_id, |
| 410 | bt_comp.bcd_device]) |
| 411 | |
| 412 | |
C Shapiro | 5bf23a7 | 2020-04-24 11:40:17 -0500 | [diff] [blame] | 413 | def _Feature(name, present): |
| 414 | attrib = {'name': name} |
| 415 | if present: |
| 416 | return etree.Element('feature', attrib=attrib) |
| 417 | else: |
| 418 | return etree.Element('unavailable-feature', attrib=attrib) |
| 419 | |
| 420 | |
| 421 | def _AnyPresent(features): |
| 422 | return topology_pb2.HardwareFeatures.PRESENT in features; |
| 423 | |
| 424 | |
| 425 | def _ArcHardwareFeatureId(design_config): |
| 426 | return design_config.id.value.lower().replace(':', '_') |
| 427 | |
| 428 | |
| 429 | def WriteArcHardwareFeatureFiles(config, output_dir): |
| 430 | """Writes ARC hardware_feature.xml files for each config |
| 431 | |
| 432 | Args: |
| 433 | config: Source ConfigBundle to process. |
| 434 | output_dir: Path to the generated output. |
| 435 | Returns: |
| 436 | dict that maps the design_config_id onto the correct file. |
| 437 | """ |
| 438 | project_gen_path = re.match(r'.*(generated.*)', output_dir).groups(1)[0] |
| 439 | result = {} |
| 440 | for hw_design in config.designs.value: |
| 441 | for design_config in hw_design.configs: |
| 442 | hw_features = design_config.hardware_features |
| 443 | multi_camera = hw_features.camera.count == 2 |
| 444 | touchscreen = _AnyPresent([hw_features.screen.touch_support]) |
| 445 | acc = hw_features.accelerometer |
| 446 | gyro = hw_features.gyroscope |
| 447 | compass = hw_features.magnetometer |
| 448 | ls = hw_features.light_sensor |
| 449 | root = etree.Element('permissions') |
| 450 | root.extend([ |
| 451 | _Feature('android.hardware.camera', multi_camera), |
| 452 | _Feature('android.hardware.camera.autofocus', multi_camera), |
| 453 | _Feature('android.hardware.sensor.accelerometer', |
| 454 | _AnyPresent( |
| 455 | [acc.lid_accelerometer, acc.base_accelerometer])), |
| 456 | _Feature('android.hardware.sensor.gyroscope', |
| 457 | _AnyPresent( |
| 458 | [gyro.lid_gyroscope, gyro.base_gyroscope])), |
| 459 | _Feature('android.hardware.sensor.compass', |
| 460 | _AnyPresent( |
| 461 | [compass.lid_magnetometer, compass.base_magnetometer])), |
| 462 | _Feature('android.hardware.sensor.light', |
| 463 | _AnyPresent( |
| 464 | [ls.lid_lightsensor, ls.base_lightsensor])), |
| 465 | _Feature('android.hardware.touchscreen', touchscreen), |
| 466 | _Feature('android.hardware.touchscreen.multitouch', touchscreen), |
| 467 | _Feature( |
| 468 | 'android.hardware.touchscreen.multitouch.distinct', touchscreen), |
| 469 | _Feature( |
| 470 | 'android.hardware.touchscreen.multitouch.jazzhand', touchscreen), |
| 471 | ]) |
| 472 | |
| 473 | feature_id = _ArcHardwareFeatureId( design_config) |
| 474 | |
| 475 | file_name = 'hardware_features_%s.xml' % feature_id |
| 476 | output = '%s/arc/%s' % (output_dir, file_name) |
C Shapiro | 9a3ac8c | 2020-04-25 07:49:21 -0500 | [diff] [blame^] | 477 | file_content = minidom.parseString( |
| 478 | etree.tostring(root)).toprettyxml(indent=' ', encoding='utf-8') |
| 479 | |
| 480 | with open(output, 'wb') as f: |
| 481 | f.write(file_content) |
| 482 | |
C Shapiro | 5bf23a7 | 2020-04-24 11:40:17 -0500 | [diff] [blame] | 483 | result[feature_id] = { |
| 484 | 'build-path': '%s/arc/%s' % (project_gen_path, file_name), |
| 485 | 'system-path': '/etc/%s' % file_name, |
| 486 | } |
| 487 | return result |
| 488 | |
| 489 | |
C Shapiro | 90fda25 | 2020-04-17 14:34:57 -0500 | [diff] [blame] | 490 | def WriteBluetoothConfigFiles(config, output_dir): |
| 491 | """Writes bluetooth conf files for every unique bluetooth chip. |
| 492 | |
| 493 | Args: |
| 494 | config: Source ConfigBundle to process. |
| 495 | output_dir: Path to the generated output. |
| 496 | Returns: |
| 497 | dict that maps the bluetooth component id onto the file config. |
| 498 | """ |
| 499 | project_gen_path = re.match(r'.*(generated.*)', output_dir).groups(1)[0] |
| 500 | result = {} |
| 501 | for hw_design in config.designs.value: |
| 502 | project_name = hw_design.name.lower() |
| 503 | for design_config in hw_design.configs: |
| 504 | bt_comp = design_config.hardware_features.bluetooth.component |
| 505 | if bt_comp.vendor_id: |
| 506 | bt_id = _BluetoothId(project_name, bt_comp) |
| 507 | result[bt_id] = { |
C Shapiro | 8556b2d | 2020-04-22 11:04:14 -0500 | [diff] [blame] | 508 | 'build-path': '%s/bluetooth/%s.conf' % (project_gen_path, bt_id), |
C Shapiro | 90fda25 | 2020-04-17 14:34:57 -0500 | [diff] [blame] | 509 | 'system-path': '/etc/bluetooth/%s/main.conf' % bt_id, |
| 510 | } |
| 511 | bt_content = '''[General] |
| 512 | DeviceID = bluetooth:%s:%s:%s''' % (bt_comp.vendor_id, |
| 513 | bt_comp.product_id, |
| 514 | bt_comp.bcd_device) |
| 515 | |
| 516 | output = '%s/bluetooth/%s.conf' % (output_dir, bt_id) |
| 517 | with open(output, 'w') as output_stream: |
| 518 | # Using print function adds proper trailing newline. |
| 519 | print(bt_content, file=output_stream) |
| 520 | return result |
| 521 | |
| 522 | |
David Burger | 7fd1dbe | 2020-03-26 09:26:55 -0600 | [diff] [blame] | 523 | def _ReadConfig(path): |
| 524 | """Reads a binary proto from a file. |
| 525 | |
| 526 | Args: |
| 527 | path: Path to the binary proto. |
| 528 | """ |
| 529 | config = config_bundle_pb2.ConfigBundle() |
| 530 | with open(path, 'rb') as f: |
| 531 | config.ParseFromString(f.read()) |
| 532 | return config |
| 533 | |
| 534 | |
| 535 | def _MergeConfigs(configs): |
| 536 | result = config_bundle_pb2.ConfigBundle() |
| 537 | for config in configs: |
| 538 | result.MergeFrom(config) |
| 539 | |
| 540 | return result |
| 541 | |
| 542 | |
| 543 | def Main(project_configs, |
| 544 | program_config, |
| 545 | output): |
| 546 | """Transforms source proto config into platform JSON. |
| 547 | |
| 548 | Args: |
| 549 | project_configs: List of source project configs to transform. |
| 550 | program_config: Program config for the given set of projects. |
| 551 | output: Output file that will be generated by the transform. |
| 552 | """ |
C Shapiro | 90fda25 | 2020-04-17 14:34:57 -0500 | [diff] [blame] | 553 | configs =_MergeConfigs( |
| 554 | [_ReadConfig(program_config)] + |
| 555 | [_ReadConfig(config) for config in project_configs]) |
C Shapiro | 5bf23a7 | 2020-04-24 11:40:17 -0500 | [diff] [blame] | 556 | bluetooth_files = {} |
| 557 | arc_hw_feature_files = {} |
| 558 | output_dir = os.path.dirname(output) |
| 559 | if os.path.exists(os.path.join(output_dir, 'bluetooth')): |
| 560 | bluetooth_files = WriteBluetoothConfigFiles(configs, output_dir) |
| 561 | if os.path.exists(os.path.join(output_dir, 'arc')): |
| 562 | arc_hw_feature_files = WriteArcHardwareFeatureFiles( |
| 563 | configs, output_dir) |
| 564 | config_files = ConfigFiles( |
| 565 | bluetooth=bluetooth_files, |
| 566 | arc_hw_features=arc_hw_feature_files, |
| 567 | ) |
| 568 | WriteOutput(_TransformBuildConfigs(configs, config_files), output) |
David Burger | 7fd1dbe | 2020-03-26 09:26:55 -0600 | [diff] [blame] | 569 | |
| 570 | |
| 571 | def main(argv=None): |
| 572 | """Main program which parses args and runs |
| 573 | |
| 574 | Args: |
| 575 | argv: List of command line arguments, if None uses sys.argv. |
| 576 | """ |
| 577 | if argv is None: |
| 578 | argv = sys.argv[1:] |
| 579 | opts = ParseArgs(argv) |
| 580 | Main(opts.project_configs, opts.program_config, opts.output) |
| 581 | |
| 582 | |
| 583 | if __name__ == '__main__': |
| 584 | sys.exit(main(sys.argv[1:])) |