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