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 | |
Andrew Lamb | 319cc92 | 2020-06-15 10:45:46 -0600 | [diff] [blame] | 17 | from typing import List |
| 18 | |
David Burger | 7fd1dbe | 2020-03-26 09:26:55 -0600 | [diff] [blame] | 19 | from collections import namedtuple |
| 20 | |
Andrew Lamb | cd33f70 | 2020-06-11 10:45:16 -0600 | [diff] [blame] | 21 | from google.protobuf import json_format |
| 22 | |
Prathmesh Prabhu | 72f8a00 | 2020-04-10 09:57:53 -0700 | [diff] [blame] | 23 | from chromiumos.config.api import device_brand_pb2 |
David Burger | 92609a3 | 2020-04-23 10:38:50 -0600 | [diff] [blame] | 24 | from chromiumos.config.api import topology_pb2 |
C Shapiro | 5bf23a7 | 2020-04-24 11:40:17 -0500 | [diff] [blame] | 25 | from chromiumos.config.payload import config_bundle_pb2 |
Prathmesh Prabhu | 72f8a00 | 2020-04-10 09:57:53 -0700 | [diff] [blame] | 26 | from chromiumos.config.api.software import brand_config_pb2 |
David Burger | 7fd1dbe | 2020-03-26 09:26:55 -0600 | [diff] [blame] | 27 | |
Andrew Lamb | 2413c98 | 2020-05-29 12:15:36 -0600 | [diff] [blame] | 28 | Config = namedtuple('Config', [ |
| 29 | 'program', 'hw_design', 'odm', 'hw_design_config', 'device_brand', |
| 30 | 'device_signer_config', 'oem', 'sw_config', 'brand_config', 'build_target' |
| 31 | ]) |
David Burger | 7fd1dbe | 2020-03-26 09:26:55 -0600 | [diff] [blame] | 32 | |
Andrew Lamb | 2413c98 | 2020-05-29 12:15:36 -0600 | [diff] [blame] | 33 | ConfigFiles = namedtuple( |
David Burger | 8ee9b4d | 2020-06-16 17:40:21 -0600 | [diff] [blame] | 34 | 'ConfigFiles', |
| 35 | ['bluetooth', 'arc_hw_features', 'touch_fw', 'dptf_map', 'camera_map']) |
| 36 | |
| 37 | CAMERA_CONFIG_DEST_PATH_TEMPLATE = '/etc/camera/camera_config_{}.json' |
| 38 | CAMERA_CONFIG_SOURCE_PATH_TEMPLATE = ( |
| 39 | 'sw_build_config/platform/chromeos-config/camera/camera_config_{}.json') |
C Shapiro | 5bf23a7 | 2020-04-24 11:40:17 -0500 | [diff] [blame] | 40 | |
David Burger | 52c9d32 | 2020-06-09 07:16:18 -0600 | [diff] [blame] | 41 | DPTF_PATH = 'sw_build_config/platform/chromeos-config/thermal' |
| 42 | DPTF_FILE = 'dptf.dv' |
C Shapiro | 2b6d533 | 2020-05-06 17:51:35 -0500 | [diff] [blame] | 43 | TOUCH_PATH = 'sw_build_config/platform/chromeos-config/touch' |
Andrew Lamb | 6c42efc | 2020-06-16 10:40:43 -0600 | [diff] [blame] | 44 | WALLPAPER_BASE_PATH = '/usr/share/chromeos-assets/wallpaper' |
David Burger | 7fd1dbe | 2020-03-26 09:26:55 -0600 | [diff] [blame] | 45 | |
Andrew Lamb | 2413c98 | 2020-05-29 12:15:36 -0600 | [diff] [blame] | 46 | |
Andrew Lamb | cd33f70 | 2020-06-11 10:45:16 -0600 | [diff] [blame] | 47 | def parse_args(argv): |
David Burger | 7fd1dbe | 2020-03-26 09:26:55 -0600 | [diff] [blame] | 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( |
Andrew Lamb | 2413c98 | 2020-05-29 12:15:36 -0600 | [diff] [blame] | 72 | '-o', '--output', type=str, help='Output file that will be generated') |
David Burger | 7fd1dbe | 2020-03-26 09:26:55 -0600 | [diff] [blame] | 73 | return parser.parse_args(argv) |
| 74 | |
| 75 | |
David Burger | 8ee9b4d | 2020-06-16 17:40:21 -0600 | [diff] [blame] | 76 | def _upsert(field, target, target_name): |
| 77 | """Updates or inserts `field` within `target`. |
| 78 | |
| 79 | If `target_name` already exists within `target` an update is performed, |
| 80 | otherwise, an insert is performed. |
| 81 | """ |
Sam McNally | 9a873f7 | 2020-06-05 19:47:22 +1000 | [diff] [blame] | 82 | if field or field == 0: |
David Burger | 8ee9b4d | 2020-06-16 17:40:21 -0600 | [diff] [blame] | 83 | if target_name in target: |
| 84 | target[target_name].update(field) |
| 85 | else: |
| 86 | target[target_name] = field |
David Burger | 7fd1dbe | 2020-03-26 09:26:55 -0600 | [diff] [blame] | 87 | |
| 88 | |
Andrew Lamb | cd33f70 | 2020-06-11 10:45:16 -0600 | [diff] [blame] | 89 | def _build_arc(config, config_files): |
| 90 | if not config.build_target.arc: |
| 91 | return None |
| 92 | |
| 93 | build_properties = { |
| 94 | 'device': config.build_target.arc.device, |
| 95 | 'first-api-level': config.build_target.arc.first_api_level, |
| 96 | 'marketing-name': config.device_brand.brand_name, |
| 97 | 'metrics-tag': config.hw_design.name.lower(), |
| 98 | 'product': config.build_target.id.value, |
| 99 | } |
| 100 | if config.oem: |
| 101 | build_properties['oem'] = config.oem.name |
| 102 | result = {'build-properties': build_properties} |
| 103 | feature_id = _arc_hardware_feature_id(config.hw_design_config) |
| 104 | if feature_id in config_files.arc_hw_features: |
| 105 | result['hardware-features'] = config_files.arc_hw_features[feature_id] |
| 106 | topology = config.hw_design_config.hardware_topology |
| 107 | ppi = topology.screen.hardware_feature.screen.panel_properties.pixels_per_in |
| 108 | # Only set for high resolution displays |
| 109 | if ppi and ppi > 250: |
| 110 | result['scale'] = ppi |
| 111 | return result |
David Burger | 7fd1dbe | 2020-03-26 09:26:55 -0600 | [diff] [blame] | 112 | |
Andrew Lamb | 2413c98 | 2020-05-29 12:15:36 -0600 | [diff] [blame] | 113 | |
Andrew Lamb | 319cc92 | 2020-06-15 10:45:46 -0600 | [diff] [blame] | 114 | def _build_ash_flags(config: Config) -> List[str]: |
| 115 | """Returns a list of Ash flags for config. |
| 116 | |
| 117 | Ash is the window manager and system UI for ChromeOS, see |
| 118 | https://chromium.googlesource.com/chromium/src/+/refs/heads/master/ash/. |
| 119 | """ |
| 120 | # A map from flag name -> value. Value may be None for boolean flags. |
| 121 | flags = {} |
| 122 | |
| 123 | hw_features = config.hw_design_config.hardware_features |
| 124 | if hw_features.stylus.stylus == topology_pb2.HardwareFeatures.Stylus.INTERNAL: |
Andrew Lamb | 2e641e2 | 2020-06-15 12:30:41 -0600 | [diff] [blame] | 125 | flags['has-internal-stylus'] = None |
Andrew Lamb | 319cc92 | 2020-06-15 10:45:46 -0600 | [diff] [blame] | 126 | |
Andrew Lamb | 2e641e2 | 2020-06-15 12:30:41 -0600 | [diff] [blame] | 127 | fp_loc = hw_features.fingerprint.location |
| 128 | if fp_loc and fp_loc != topology_pb2.HardwareFeatures.Fingerprint.NOT_PRESENT: |
| 129 | loc_name = topology_pb2.HardwareFeatures.Fingerprint.Location.Name(fp_loc) |
| 130 | flags['fingerprint-sensor-location'] = loc_name.lower().replace('_', '-') |
| 131 | |
Andrew Lamb | 6c42efc | 2020-06-16 10:40:43 -0600 | [diff] [blame] | 132 | wallpaper = config.brand_config.wallpaper |
| 133 | # If a wallpaper is set, the 'default-wallpaper-is-oem' flag needs to be set. |
| 134 | # If a wallpaper is not set, the 'default_[large|small].jpg' wallpapers |
| 135 | # should still be set. |
| 136 | if wallpaper: |
| 137 | flags['default-wallpaper-is-oem'] = None |
| 138 | else: |
| 139 | wallpaper = 'default' |
| 140 | |
| 141 | for size in ('small', 'large'): |
| 142 | flags[f'default-wallpaper-{size}'] = ( |
| 143 | f'{WALLPAPER_BASE_PATH}/{wallpaper}_{size}.jpg') |
| 144 | |
| 145 | # For each size, also install 'guest' and 'child' wallpapers. |
| 146 | for wallpaper_type in ('guest', 'child'): |
| 147 | flags[f'{wallpaper_type}-wallpaper-{size}'] = ( |
| 148 | f'{WALLPAPER_BASE_PATH}/{wallpaper_type}_{size}.jpg') |
| 149 | |
Andrew Lamb | 72d4136 | 2020-06-17 09:19:02 -0600 | [diff] [blame] | 150 | flags['arc-build-properties'] = json_format.MessageToDict( |
| 151 | config.build_target.arc) |
| 152 | |
Andrew Lamb | 90b168c | 2020-06-22 10:42:30 -0600 | [diff] [blame] | 153 | power_button = hw_features.power_button |
| 154 | if power_button.edge: |
| 155 | flags['ash-power-button-position'] = json.dumps({ |
| 156 | 'edge': |
| 157 | topology_pb2.HardwareFeatures.Button.Edge.Name(power_button.edge |
| 158 | ).lower(), |
| 159 | # Starlark sometimes represents float literals strangely, e.g. changing |
| 160 | # 0.9 to 0.899999. Round to two digits here. |
| 161 | 'position': |
| 162 | round(power_button.position, 2) |
| 163 | }) |
| 164 | |
| 165 | volume_button = hw_features.volume_button |
| 166 | if volume_button.edge: |
| 167 | flags['ash-side-volume-button-position'] = json.dumps({ |
| 168 | 'region': |
| 169 | topology_pb2.HardwareFeatures.Button.Region.Name( |
| 170 | volume_button.region).lower(), |
| 171 | 'edge': |
| 172 | topology_pb2.HardwareFeatures.Button.Edge.Name(volume_button.edge |
| 173 | ).lower(), |
| 174 | }) |
| 175 | |
Andrew Lamb | 2e641e2 | 2020-06-15 12:30:41 -0600 | [diff] [blame] | 176 | return sorted([f'--{k}={v}' if v else f'--{k}' for k, v in flags.items()]) |
Andrew Lamb | 319cc92 | 2020-06-15 10:45:46 -0600 | [diff] [blame] | 177 | |
| 178 | |
| 179 | def _build_ui(config: Config) -> dict: |
| 180 | """Builds the 'ui' property from cros_config_schema.""" |
| 181 | return {'extra-ash-flags': _build_ash_flags(config)} |
| 182 | |
| 183 | |
Andrew Lamb | cd33f70 | 2020-06-11 10:45:16 -0600 | [diff] [blame] | 184 | def _build_bluetooth(config, bluetooth_files): |
C Shapiro | 90fda25 | 2020-04-17 14:34:57 -0500 | [diff] [blame] | 185 | bt_flags = config.sw_config.bluetooth_config.flags |
| 186 | # Convert to native map (from proto wrapper) |
| 187 | bt_flags_map = dict(bt_flags) |
| 188 | result = {} |
| 189 | if bt_flags_map: |
| 190 | result['flags'] = bt_flags_map |
C Shapiro | 74da76e | 2020-05-04 13:02:20 -0500 | [diff] [blame] | 191 | bt_comp = config.hw_design_config.hardware_features.bluetooth.component.usb |
C Shapiro | 90fda25 | 2020-04-17 14:34:57 -0500 | [diff] [blame] | 192 | if bt_comp.vendor_id: |
Andrew Lamb | cd33f70 | 2020-06-11 10:45:16 -0600 | [diff] [blame] | 193 | bt_id = _bluetooth_id(config.hw_design.name.lower(), bt_comp) |
C Shapiro | 90fda25 | 2020-04-17 14:34:57 -0500 | [diff] [blame] | 194 | if bt_id in bluetooth_files: |
| 195 | result['config'] = bluetooth_files[bt_id] |
| 196 | return result |
| 197 | |
David Burger | 7fd1dbe | 2020-03-26 09:26:55 -0600 | [diff] [blame] | 198 | |
Andrew Lamb | cd33f70 | 2020-06-11 10:45:16 -0600 | [diff] [blame] | 199 | def _build_fingerprint(hw_topology): |
| 200 | if not hw_topology.HasField('fingerprint'): |
| 201 | return None |
| 202 | |
| 203 | fp = hw_topology.fingerprint.hardware_feature.fingerprint |
| 204 | result = {} |
| 205 | if fp.location != topology_pb2.HardwareFeatures.Fingerprint.NOT_PRESENT: |
| 206 | location = fp.Location.DESCRIPTOR.values_by_number[fp.location].name |
| 207 | result['sensor-location'] = location.lower().replace('_', '-') |
| 208 | if fp.board: |
| 209 | result['board'] = fp.board |
| 210 | return result |
David Burger | 7fd1dbe | 2020-03-26 09:26:55 -0600 | [diff] [blame] | 211 | |
| 212 | |
Andrew Lamb | cd33f70 | 2020-06-11 10:45:16 -0600 | [diff] [blame] | 213 | def _fw_bcs_path(payload): |
David Burger | 7fd1dbe | 2020-03-26 09:26:55 -0600 | [diff] [blame] | 214 | if payload and payload.firmware_image_name: |
Andrew Lamb | 2413c98 | 2020-05-29 12:15:36 -0600 | [diff] [blame] | 215 | return 'bcs://%s.%d.%d.0.tbz2' % (payload.firmware_image_name, |
| 216 | payload.version.major, |
| 217 | payload.version.minor) |
David Burger | 7fd1dbe | 2020-03-26 09:26:55 -0600 | [diff] [blame] | 218 | |
Andrew Lamb | cd33f70 | 2020-06-11 10:45:16 -0600 | [diff] [blame] | 219 | return None |
David Burger | 7fd1dbe | 2020-03-26 09:26:55 -0600 | [diff] [blame] | 220 | |
Andrew Lamb | cd33f70 | 2020-06-11 10:45:16 -0600 | [diff] [blame] | 221 | |
| 222 | def _fw_build_target(payload): |
David Burger | 7fd1dbe | 2020-03-26 09:26:55 -0600 | [diff] [blame] | 223 | if payload: |
| 224 | return payload.build_target_name |
| 225 | |
Andrew Lamb | cd33f70 | 2020-06-11 10:45:16 -0600 | [diff] [blame] | 226 | return None |
David Burger | 7fd1dbe | 2020-03-26 09:26:55 -0600 | [diff] [blame] | 227 | |
Andrew Lamb | cd33f70 | 2020-06-11 10:45:16 -0600 | [diff] [blame] | 228 | |
| 229 | def _build_firmware(config): |
David Burger | b70b676 | 2020-05-21 12:14:59 -0600 | [diff] [blame] | 230 | """Returns firmware config, or None if no build targets.""" |
Andrew Lamb | 3da156d | 2020-04-16 16:00:56 -0600 | [diff] [blame] | 231 | fw_payload_config = config.sw_config.firmware |
| 232 | fw_build_config = config.sw_config.firmware_build_config |
| 233 | main_ro = fw_payload_config.main_ro_payload |
| 234 | main_rw = fw_payload_config.main_rw_payload |
| 235 | ec_ro = fw_payload_config.ec_ro_payload |
| 236 | pd_ro = fw_payload_config.pd_ro_payload |
David Burger | 7fd1dbe | 2020-03-26 09:26:55 -0600 | [diff] [blame] | 237 | |
| 238 | build_targets = {} |
Andrew Lamb | 3da156d | 2020-04-16 16:00:56 -0600 | [diff] [blame] | 239 | |
David Burger | 8ee9b4d | 2020-06-16 17:40:21 -0600 | [diff] [blame] | 240 | _upsert(fw_build_config.build_targets.depthcharge, build_targets, |
| 241 | 'depthcharge') |
| 242 | _upsert(fw_build_config.build_targets.coreboot, build_targets, 'coreboot') |
| 243 | _upsert(fw_build_config.build_targets.ec, build_targets, 'ec') |
| 244 | _upsert( |
Andrew Lamb | f8954ee | 2020-04-21 10:24:40 -0600 | [diff] [blame] | 245 | list(fw_build_config.build_targets.ec_extras), build_targets, 'ec_extras') |
David Burger | 8ee9b4d | 2020-06-16 17:40:21 -0600 | [diff] [blame] | 246 | _upsert(fw_build_config.build_targets.libpayload, build_targets, 'libpayload') |
David Burger | 7fd1dbe | 2020-03-26 09:26:55 -0600 | [diff] [blame] | 247 | |
David Burger | b70b676 | 2020-05-21 12:14:59 -0600 | [diff] [blame] | 248 | if not build_targets: |
| 249 | return None |
| 250 | |
David Burger | 7fd1dbe | 2020-03-26 09:26:55 -0600 | [diff] [blame] | 251 | result = { |
| 252 | 'bcs-overlay': config.build_target.overlay_name, |
| 253 | 'build-targets': build_targets, |
David Burger | 7fd1dbe | 2020-03-26 09:26:55 -0600 | [diff] [blame] | 254 | } |
Andrew Lamb | 883fa04 | 2020-04-06 11:37:22 -0600 | [diff] [blame] | 255 | |
David Burger | 8ee9b4d | 2020-06-16 17:40:21 -0600 | [diff] [blame] | 256 | _upsert(main_ro.firmware_image_name.lower(), result, 'image-name') |
Andrew Lamb | 883fa04 | 2020-04-06 11:37:22 -0600 | [diff] [blame] | 257 | |
David Burger | 8ee9b4d | 2020-06-16 17:40:21 -0600 | [diff] [blame] | 258 | _upsert(_fw_bcs_path(main_ro), result, 'main-ro-image') |
| 259 | _upsert(_fw_bcs_path(main_rw), result, 'main-rw-image') |
| 260 | _upsert(_fw_bcs_path(ec_ro), result, 'ec-ro-image') |
| 261 | _upsert(_fw_bcs_path(pd_ro), result, 'pd-ro-image') |
David Burger | 7fd1dbe | 2020-03-26 09:26:55 -0600 | [diff] [blame] | 262 | |
David Burger | 8ee9b4d | 2020-06-16 17:40:21 -0600 | [diff] [blame] | 263 | _upsert( |
Andrew Lamb | f39fbe8 | 2020-04-13 16:14:33 -0600 | [diff] [blame] | 264 | config.hw_design_config.hardware_features.fw_config.value, |
| 265 | result, |
| 266 | 'firmware-config', |
| 267 | ) |
| 268 | |
David Burger | 7fd1dbe | 2020-03-26 09:26:55 -0600 | [diff] [blame] | 269 | return result |
| 270 | |
| 271 | |
Andrew Lamb | cd33f70 | 2020-06-11 10:45:16 -0600 | [diff] [blame] | 272 | def _build_fw_signing(config): |
C Shapiro | 2f0bb5d | 2020-04-14 10:07:47 -0500 | [diff] [blame] | 273 | if config.sw_config.firmware and config.device_signer_config: |
David Burger | 68e0d14 | 2020-05-15 17:29:33 -0600 | [diff] [blame] | 274 | hw_design = config.hw_design.name.lower() |
C Shapiro | 2f0bb5d | 2020-04-14 10:07:47 -0500 | [diff] [blame] | 275 | return { |
| 276 | 'key-id': config.device_signer_config.key_id, |
C Shapiro | 10e9a61 | 2020-05-19 17:06:43 -0500 | [diff] [blame] | 277 | # TODO(shapiroc): Need to fix for whitelabel. |
| 278 | # Whitelabel will collide on unique signature-id values. |
David Burger | 68e0d14 | 2020-05-15 17:29:33 -0600 | [diff] [blame] | 279 | 'signature-id': hw_design, |
C Shapiro | 2f0bb5d | 2020-04-14 10:07:47 -0500 | [diff] [blame] | 280 | } |
| 281 | return {} |
David Burger | 7fd1dbe | 2020-03-26 09:26:55 -0600 | [diff] [blame] | 282 | |
| 283 | |
Andrew Lamb | cd33f70 | 2020-06-11 10:45:16 -0600 | [diff] [blame] | 284 | def _file(source, destination): |
Andrew Lamb | 2413c98 | 2020-05-29 12:15:36 -0600 | [diff] [blame] | 285 | return {'destination': destination, 'source': source} |
David Burger | 7fd1dbe | 2020-03-26 09:26:55 -0600 | [diff] [blame] | 286 | |
| 287 | |
David Burger | 40dfe3a | 2020-06-18 17:09:13 -0600 | [diff] [blame] | 288 | def _file_v2(build_path, system_path): |
| 289 | return {'build-path': build_path, 'system-path': system_path} |
| 290 | |
| 291 | |
Andrew Lamb | cd33f70 | 2020-06-11 10:45:16 -0600 | [diff] [blame] | 292 | def _build_audio(config): |
David Burger | 178f3ef | 2020-06-26 12:11:57 -0600 | [diff] [blame] | 293 | if not config.sw_config.audio_configs: |
| 294 | return {} |
David Burger | 7fd1dbe | 2020-03-26 09:26:55 -0600 | [diff] [blame] | 295 | alsa_path = '/usr/share/alsa/ucm' |
| 296 | cras_path = '/etc/cras' |
| 297 | project_name = config.hw_design.name.lower() |
David Burger | 4325066 | 2020-05-07 11:21:50 -0600 | [diff] [blame] | 298 | program_name = config.program.name.lower() |
David Burger | 7fd1dbe | 2020-03-26 09:26:55 -0600 | [diff] [blame] | 299 | files = [] |
David Burger | 178f3ef | 2020-06-26 12:11:57 -0600 | [diff] [blame] | 300 | ucm_suffix = None |
| 301 | for audio in config.sw_config.audio_configs: |
| 302 | card = audio.card_name |
| 303 | card_with_suffix = audio.card_name |
| 304 | if audio.ucm_suffix: |
| 305 | # TODO: last ucm_suffix wins. |
| 306 | ucm_suffix = audio.ucm_suffix |
| 307 | card_with_suffix += '.' + audio.ucm_suffix |
| 308 | if audio.ucm_file: |
| 309 | files.append( |
| 310 | _file(audio.ucm_file, |
| 311 | '%s/%s/HiFi.conf' % (alsa_path, card_with_suffix))) |
| 312 | if audio.ucm_master_file: |
| 313 | files.append( |
| 314 | _file( |
| 315 | audio.ucm_master_file, '%s/%s/%s.conf' % |
Andrew Lamb | 2413c98 | 2020-05-29 12:15:36 -0600 | [diff] [blame] | 316 | (alsa_path, card_with_suffix, card_with_suffix))) |
David Burger | 178f3ef | 2020-06-26 12:11:57 -0600 | [diff] [blame] | 317 | if audio.card_config_file: |
| 318 | files.append( |
| 319 | _file(audio.card_config_file, |
| 320 | '%s/%s/%s' % (cras_path, project_name, card))) |
| 321 | if audio.dsp_file: |
| 322 | files.append( |
| 323 | _file(audio.dsp_file, '%s/%s/dsp.ini' % (cras_path, project_name))) |
| 324 | if audio.module_file: |
| 325 | files.append( |
| 326 | _file(audio.module_file, |
| 327 | '/etc/modprobe.d/alsa-%s.conf' % program_name)) |
| 328 | if audio.board_file: |
| 329 | files.append( |
| 330 | _file(audio.board_file, |
| 331 | '%s/%s/board.ini' % (cras_path, project_name))) |
David Burger | 599ff7b | 2020-04-06 16:29:31 -0600 | [diff] [blame] | 332 | |
| 333 | result = { |
David Burger | 7fd1dbe | 2020-03-26 09:26:55 -0600 | [diff] [blame] | 334 | 'main': { |
| 335 | 'cras-config-dir': project_name, |
| 336 | 'files': files, |
| 337 | } |
| 338 | } |
David Burger | 178f3ef | 2020-06-26 12:11:57 -0600 | [diff] [blame] | 339 | |
| 340 | if ucm_suffix: |
| 341 | result['main']['ucm-suffix'] = ucm_suffix |
David Burger | 599ff7b | 2020-04-06 16:29:31 -0600 | [diff] [blame] | 342 | |
| 343 | return result |
David Burger | 7fd1dbe | 2020-03-26 09:26:55 -0600 | [diff] [blame] | 344 | |
| 345 | |
Andrew Lamb | cd33f70 | 2020-06-11 10:45:16 -0600 | [diff] [blame] | 346 | def _build_camera(hw_topology): |
David Burger | 8aa8fa3 | 2020-04-14 08:30:34 -0600 | [diff] [blame] | 347 | if hw_topology.HasField('camera'): |
| 348 | camera = hw_topology.camera.hardware_feature.camera |
| 349 | result = {} |
| 350 | if camera.count.value: |
| 351 | result['count'] = camera.count.value |
| 352 | return result |
| 353 | |
Andrew Lamb | cd33f70 | 2020-06-11 10:45:16 -0600 | [diff] [blame] | 354 | return None |
David Burger | 8aa8fa3 | 2020-04-14 08:30:34 -0600 | [diff] [blame] | 355 | |
Andrew Lamb | cd33f70 | 2020-06-11 10:45:16 -0600 | [diff] [blame] | 356 | |
| 357 | def _build_identity(hw_scan_config, program, brand_scan_config=None): |
David Burger | 7fd1dbe | 2020-03-26 09:26:55 -0600 | [diff] [blame] | 358 | identity = {} |
David Burger | 8ee9b4d | 2020-06-16 17:40:21 -0600 | [diff] [blame] | 359 | _upsert(hw_scan_config.firmware_sku, identity, 'sku-id') |
| 360 | _upsert(hw_scan_config.smbios_name_match, identity, 'smbios-name-match') |
Andrew Lamb | 7806ce9 | 2020-04-07 10:22:17 -0600 | [diff] [blame] | 361 | # 'platform-name' is needed to support 'mosys platform name'. Clients should |
| 362 | # longer require platform name, but set it here for backwards compatibility. |
David Burger | 8ee9b4d | 2020-06-16 17:40:21 -0600 | [diff] [blame] | 363 | _upsert(program.name, identity, 'platform-name') |
David Burger | 7fd1dbe | 2020-03-26 09:26:55 -0600 | [diff] [blame] | 364 | # ARM architecture |
David Burger | 8ee9b4d | 2020-06-16 17:40:21 -0600 | [diff] [blame] | 365 | _upsert(hw_scan_config.device_tree_compatible_match, identity, |
| 366 | 'device-tree-compatible-match') |
David Burger | 7fd1dbe | 2020-03-26 09:26:55 -0600 | [diff] [blame] | 367 | |
| 368 | if brand_scan_config: |
David Burger | 8ee9b4d | 2020-06-16 17:40:21 -0600 | [diff] [blame] | 369 | _upsert(brand_scan_config.whitelabel_tag, identity, 'whitelabel-tag') |
David Burger | 7fd1dbe | 2020-03-26 09:26:55 -0600 | [diff] [blame] | 370 | |
| 371 | return identity |
| 372 | |
| 373 | |
Andrew Lamb | cd33f70 | 2020-06-11 10:45:16 -0600 | [diff] [blame] | 374 | def _lookup(id_value, id_map): |
| 375 | if not id_value.value: |
| 376 | return None |
| 377 | |
| 378 | key = id_value.value |
| 379 | if key in id_map: |
| 380 | return id_map[id_value.value] |
| 381 | error = 'Failed to lookup %s with value: %s' % ( |
| 382 | id_value.__class__.__name__.replace('Id', ''), key) |
| 383 | print(error) |
| 384 | print('Check the config contents provided:') |
| 385 | printer = pprint.PrettyPrinter(indent=4) |
| 386 | printer.pprint(id_map) |
| 387 | raise Exception(error) |
David Burger | 7fd1dbe | 2020-03-26 09:26:55 -0600 | [diff] [blame] | 388 | |
| 389 | |
Andrew Lamb | cd33f70 | 2020-06-11 10:45:16 -0600 | [diff] [blame] | 390 | def _build_touch_file_config(config, project_name): |
| 391 | partners = {x.id.value: x for x in config.partners.value} |
C Shapiro | 2b6d533 | 2020-05-06 17:51:35 -0500 | [diff] [blame] | 392 | files = [] |
| 393 | for comp in config.components: |
C Shapiro | 4813be6 | 2020-05-13 17:31:58 -0500 | [diff] [blame] | 394 | touch = comp.touchscreen |
| 395 | # Everything is the same for Touch screen/pad, except different fields |
| 396 | if comp.HasField('touchpad'): |
| 397 | touch = comp.touchpad |
| 398 | if touch.product_id: |
Andrew Lamb | cd33f70 | 2020-06-11 10:45:16 -0600 | [diff] [blame] | 399 | vendor = _lookup(comp.manufacturer_id, partners) |
C Shapiro | 2b6d533 | 2020-05-06 17:51:35 -0500 | [diff] [blame] | 400 | if not vendor: |
Andrew Lamb | 2413c98 | 2020-05-29 12:15:36 -0600 | [diff] [blame] | 401 | raise Exception("Manufacturer must be set for touch device %s" % |
| 402 | comp.id.value) |
C Shapiro | 2b6d533 | 2020-05-06 17:51:35 -0500 | [diff] [blame] | 403 | |
C Shapiro | 4813be6 | 2020-05-13 17:31:58 -0500 | [diff] [blame] | 404 | product_id = touch.product_id |
| 405 | fw_version = touch.fw_version |
C Shapiro | 2b6d533 | 2020-05-06 17:51:35 -0500 | [diff] [blame] | 406 | |
C Shapiro | 5c6fc21 | 2020-05-13 16:32:09 -0500 | [diff] [blame] | 407 | touch_vendor = vendor.touch_vendor |
| 408 | sym_link = touch_vendor.fw_file_format.format( |
Andrew Lamb | 2413c98 | 2020-05-29 12:15:36 -0600 | [diff] [blame] | 409 | vendor_name=vendor.name, |
| 410 | vendor_id=touch_vendor.vendor_id, |
| 411 | product_id=product_id, |
| 412 | fw_version=fw_version, |
| 413 | product_series=touch.product_series) |
C Shapiro | 2b6d533 | 2020-05-06 17:51:35 -0500 | [diff] [blame] | 414 | |
| 415 | file_name = "%s_%s.bin" % (product_id, fw_version) |
| 416 | fw_file_path = os.path.join(TOUCH_PATH, vendor.name, file_name) |
| 417 | |
| 418 | if not os.path.exists(fw_file_path): |
Andrew Lamb | 2413c98 | 2020-05-29 12:15:36 -0600 | [diff] [blame] | 419 | raise Exception("Touchscreen fw bin file doesn't exist at: %s" % |
| 420 | fw_file_path) |
C Shapiro | 2b6d533 | 2020-05-06 17:51:35 -0500 | [diff] [blame] | 421 | |
| 422 | files.append({ |
Andrew Lamb | 2413c98 | 2020-05-29 12:15:36 -0600 | [diff] [blame] | 423 | "destination": |
| 424 | "/opt/google/touch/firmware/%s_%s" % (vendor.name, file_name), |
| 425 | "source": |
| 426 | os.path.join(project_name, fw_file_path), |
| 427 | "symlink": |
| 428 | os.path.join("/lib/firmware", sym_link), |
C Shapiro | 2b6d533 | 2020-05-06 17:51:35 -0500 | [diff] [blame] | 429 | }) |
| 430 | |
| 431 | result = {} |
David Burger | 8ee9b4d | 2020-06-16 17:40:21 -0600 | [diff] [blame] | 432 | _upsert(files, result, 'files') |
C Shapiro | 2b6d533 | 2020-05-06 17:51:35 -0500 | [diff] [blame] | 433 | return result |
| 434 | |
| 435 | |
David Burger | 8ee9b4d | 2020-06-16 17:40:21 -0600 | [diff] [blame] | 436 | def _transform_build_configs(config, |
| 437 | config_files=ConfigFiles({}, {}, {}, {}, {})): |
Andrew Lamb | cd33f70 | 2020-06-11 10:45:16 -0600 | [diff] [blame] | 438 | # pylint: disable=too-many-locals,too-many-branches |
| 439 | partners = {x.id.value: x for x in config.partners.value} |
| 440 | programs = {x.id.value: x for x in config.programs.value} |
David Burger | 7fd1dbe | 2020-03-26 09:26:55 -0600 | [diff] [blame] | 441 | sw_configs = list(config.software_configs) |
Andrew Lamb | cd33f70 | 2020-06-11 10:45:16 -0600 | [diff] [blame] | 442 | brand_configs = {x.brand_id.value: x for x in config.brand_configs} |
David Burger | 7fd1dbe | 2020-03-26 09:26:55 -0600 | [diff] [blame] | 443 | |
C Shapiro | a0b766c | 2020-03-31 08:35:28 -0500 | [diff] [blame] | 444 | if len(config.build_targets) != 1: |
| 445 | # Artifact of sharing the config_bundle for analysis and transforms. |
| 446 | # Integrated analysis of multiple programs/projects it the only time |
| 447 | # having multiple build targets would be valid. |
| 448 | raise Exception('Single build_target required for transform') |
| 449 | |
David Burger | 7fd1dbe | 2020-03-26 09:26:55 -0600 | [diff] [blame] | 450 | results = {} |
| 451 | for hw_design in config.designs.value: |
| 452 | if config.device_brands.value: |
Andrew Lamb | 2413c98 | 2020-05-29 12:15:36 -0600 | [diff] [blame] | 453 | device_brands = [ |
| 454 | x for x in config.device_brands.value |
| 455 | if x.design_id.value == hw_design.id.value |
| 456 | ] |
David Burger | 7fd1dbe | 2020-03-26 09:26:55 -0600 | [diff] [blame] | 457 | else: |
| 458 | device_brands = [device_brand_pb2.DeviceBrand()] |
| 459 | |
| 460 | for device_brand in device_brands: |
| 461 | # Brand config can be empty since platform JSON config allows it |
| 462 | brand_config = brand_config_pb2.BrandConfig() |
| 463 | if device_brand.id.value in brand_configs: |
| 464 | brand_config = brand_configs[device_brand.id.value] |
| 465 | |
| 466 | for hw_design_config in hw_design.configs: |
| 467 | design_id = hw_design_config.id.value |
Andrew Lamb | 2413c98 | 2020-05-29 12:15:36 -0600 | [diff] [blame] | 468 | sw_config_matches = [ |
| 469 | x for x in sw_configs if x.design_config_id.value == design_id |
| 470 | ] |
David Burger | 7fd1dbe | 2020-03-26 09:26:55 -0600 | [diff] [blame] | 471 | if len(sw_config_matches) == 1: |
| 472 | sw_config = sw_config_matches[0] |
| 473 | elif len(sw_config_matches) > 1: |
| 474 | raise Exception('Multiple software configs found for: %s' % design_id) |
| 475 | else: |
| 476 | raise Exception('Software config is required for: %s' % design_id) |
| 477 | |
Andrew Lamb | cd33f70 | 2020-06-11 10:45:16 -0600 | [diff] [blame] | 478 | program = _lookup(hw_design.program_id, programs) |
C Shapiro | adefd7c | 2020-05-19 16:37:21 -0500 | [diff] [blame] | 479 | signer_configs_by_design = {} |
| 480 | signer_configs_by_brand = {} |
| 481 | for signer_config in program.device_signer_configs: |
| 482 | design_id = signer_config.design_id.value |
| 483 | brand_id = signer_config.brand_id.value |
| 484 | if design_id: |
| 485 | signer_configs_by_design[design_id] = signer_config |
| 486 | elif brand_id: |
| 487 | signer_configs_by_brand[brand_id] = signer_config |
| 488 | else: |
| 489 | raise Exception('No ID found for signer config: %s' % signer_config) |
| 490 | |
C Shapiro | 2f0bb5d | 2020-04-14 10:07:47 -0500 | [diff] [blame] | 491 | device_signer_config = None |
C Shapiro | adefd7c | 2020-05-19 16:37:21 -0500 | [diff] [blame] | 492 | if signer_configs_by_design or signer_configs_by_brand: |
| 493 | design_id = hw_design.id.value |
| 494 | brand_id = device_brand.id.value |
| 495 | if design_id in signer_configs_by_design: |
| 496 | device_signer_config = signer_configs_by_design[design_id] |
| 497 | elif brand_id in signer_configs_by_brand: |
| 498 | device_signer_config = signer_configs_by_brand[brand_id] |
| 499 | else: |
| 500 | # Assume that if signer configs are set, every config is setup |
Andrew Lamb | 2413c98 | 2020-05-29 12:15:36 -0600 | [diff] [blame] | 501 | raise Exception('Signer config missing for design: %s, brand: %s' % |
| 502 | (design_id, brand_id)) |
C Shapiro | 2f0bb5d | 2020-04-14 10:07:47 -0500 | [diff] [blame] | 503 | |
Andrew Lamb | cd33f70 | 2020-06-11 10:45:16 -0600 | [diff] [blame] | 504 | transformed_config = _transform_build_config( |
C Shapiro | 90fda25 | 2020-04-17 14:34:57 -0500 | [diff] [blame] | 505 | Config( |
| 506 | program=program, |
| 507 | hw_design=hw_design, |
Andrew Lamb | cd33f70 | 2020-06-11 10:45:16 -0600 | [diff] [blame] | 508 | odm=_lookup(hw_design.odm_id, partners), |
C Shapiro | 90fda25 | 2020-04-17 14:34:57 -0500 | [diff] [blame] | 509 | hw_design_config=hw_design_config, |
| 510 | device_brand=device_brand, |
| 511 | device_signer_config=device_signer_config, |
Andrew Lamb | cd33f70 | 2020-06-11 10:45:16 -0600 | [diff] [blame] | 512 | oem=_lookup(device_brand.oem_id, partners), |
C Shapiro | 90fda25 | 2020-04-17 14:34:57 -0500 | [diff] [blame] | 513 | sw_config=sw_config, |
| 514 | brand_config=brand_config, |
Andrew Lamb | 2413c98 | 2020-05-29 12:15:36 -0600 | [diff] [blame] | 515 | build_target=config.build_targets[0]), config_files) |
David Burger | 7fd1dbe | 2020-03-26 09:26:55 -0600 | [diff] [blame] | 516 | |
Andrew Lamb | 2413c98 | 2020-05-29 12:15:36 -0600 | [diff] [blame] | 517 | config_json = json.dumps( |
| 518 | transformed_config, |
| 519 | sort_keys=True, |
| 520 | indent=2, |
| 521 | separators=(',', ': ')) |
David Burger | 7fd1dbe | 2020-03-26 09:26:55 -0600 | [diff] [blame] | 522 | |
| 523 | if config_json not in results: |
| 524 | results[config_json] = transformed_config |
| 525 | |
| 526 | return list(results.values()) |
| 527 | |
| 528 | |
Andrew Lamb | cd33f70 | 2020-06-11 10:45:16 -0600 | [diff] [blame] | 529 | def _transform_build_config(config, config_files): |
David Burger | 7fd1dbe | 2020-03-26 09:26:55 -0600 | [diff] [blame] | 530 | """Transforms Config instance into target platform JSON schema. |
| 531 | |
| 532 | Args: |
| 533 | config: Config namedtuple |
C Shapiro | 5bf23a7 | 2020-04-24 11:40:17 -0500 | [diff] [blame] | 534 | config_files: Map to look up the generated config files. |
David Burger | 7fd1dbe | 2020-03-26 09:26:55 -0600 | [diff] [blame] | 535 | |
| 536 | Returns: |
| 537 | Unique config payload based on the platform JSON schema. |
| 538 | """ |
| 539 | result = { |
Andrew Lamb | 2413c98 | 2020-05-29 12:15:36 -0600 | [diff] [blame] | 540 | 'identity': |
Andrew Lamb | cd33f70 | 2020-06-11 10:45:16 -0600 | [diff] [blame] | 541 | _build_identity(config.sw_config.id_scan_config, config.program, |
| 542 | config.brand_config.scan_config), |
Andrew Lamb | 2413c98 | 2020-05-29 12:15:36 -0600 | [diff] [blame] | 543 | 'name': |
| 544 | config.hw_design.name.lower(), |
David Burger | 7fd1dbe | 2020-03-26 09:26:55 -0600 | [diff] [blame] | 545 | } |
| 546 | |
David Burger | 8ee9b4d | 2020-06-16 17:40:21 -0600 | [diff] [blame] | 547 | _upsert(_build_arc(config, config_files), result, 'arc') |
| 548 | _upsert(_build_audio(config), result, 'audio') |
| 549 | _upsert(_build_bluetooth(config, config_files.bluetooth), result, 'bluetooth') |
| 550 | _upsert(config.device_brand.brand_code, result, 'brand-code') |
| 551 | _upsert( |
Andrew Lamb | cd33f70 | 2020-06-11 10:45:16 -0600 | [diff] [blame] | 552 | _build_camera(config.hw_design_config.hardware_topology), result, |
| 553 | 'camera') |
David Burger | 8ee9b4d | 2020-06-16 17:40:21 -0600 | [diff] [blame] | 554 | _upsert(_build_firmware(config), result, 'firmware') |
| 555 | _upsert(_build_fw_signing(config), result, 'firmware-signing') |
| 556 | _upsert( |
Andrew Lamb | cd33f70 | 2020-06-11 10:45:16 -0600 | [diff] [blame] | 557 | _build_fingerprint(config.hw_design_config.hardware_topology), result, |
Andrew Lamb | 2413c98 | 2020-05-29 12:15:36 -0600 | [diff] [blame] | 558 | 'fingerprint') |
Andrew Lamb | 0d236ab | 2020-06-30 12:30:20 -0600 | [diff] [blame] | 559 | _upsert(_build_ui(config), result, 'ui') |
David Burger | 7fd1dbe | 2020-03-26 09:26:55 -0600 | [diff] [blame] | 560 | power_prefs = config.sw_config.power_config.preferences |
| 561 | power_prefs_map = dict( |
Andrew Lamb | 2413c98 | 2020-05-29 12:15:36 -0600 | [diff] [blame] | 562 | (x.replace('_', '-'), power_prefs[x]) for x in power_prefs) |
David Burger | 8ee9b4d | 2020-06-16 17:40:21 -0600 | [diff] [blame] | 563 | _upsert(power_prefs_map, result, 'power') |
| 564 | if config_files.camera_map: |
| 565 | camera_file = config_files.camera_map.get(config.hw_design.name, {}) |
| 566 | _upsert(camera_file, result, 'camera') |
David Burger | 52c9d32 | 2020-06-09 07:16:18 -0600 | [diff] [blame] | 567 | if config_files.dptf_map: |
| 568 | # Prefer design specific if found, if not fall back to project wide config |
| 569 | # mapped under the empty string. |
| 570 | if config_files.dptf_map.get(config.hw_design.name): |
| 571 | dptf_file = config_files.dptf_map[config.hw_design.name] |
| 572 | else: |
| 573 | dptf_file = config_files.dptf_map.get('') |
David Burger | 8ee9b4d | 2020-06-16 17:40:21 -0600 | [diff] [blame] | 574 | _upsert(dptf_file, result, 'thermal') |
| 575 | _upsert(config_files.touch_fw, result, 'touch') |
David Burger | 7fd1dbe | 2020-03-26 09:26:55 -0600 | [diff] [blame] | 576 | |
| 577 | return result |
| 578 | |
| 579 | |
Andrew Lamb | cd33f70 | 2020-06-11 10:45:16 -0600 | [diff] [blame] | 580 | def write_output(configs, output=None): |
David Burger | 7fd1dbe | 2020-03-26 09:26:55 -0600 | [diff] [blame] | 581 | """Writes a list of configs to platform JSON format. |
| 582 | |
| 583 | Args: |
| 584 | configs: List of config dicts defined in cros_config_schema.yaml |
| 585 | output: Target file output (if None, prints to stdout) |
| 586 | """ |
Andrew Lamb | 2413c98 | 2020-05-29 12:15:36 -0600 | [diff] [blame] | 587 | json_output = json.dumps({'chromeos': { |
| 588 | 'configs': configs, |
| 589 | }}, |
| 590 | sort_keys=True, |
| 591 | indent=2, |
| 592 | separators=(',', ': ')) |
David Burger | 7fd1dbe | 2020-03-26 09:26:55 -0600 | [diff] [blame] | 593 | if output: |
| 594 | with open(output, 'w') as output_stream: |
| 595 | # Using print function adds proper trailing newline. |
| 596 | print(json_output, file=output_stream) |
| 597 | else: |
| 598 | print(json_output) |
| 599 | |
| 600 | |
Andrew Lamb | cd33f70 | 2020-06-11 10:45:16 -0600 | [diff] [blame] | 601 | def _bluetooth_id(project_name, bt_comp): |
Andrew Lamb | 2413c98 | 2020-05-29 12:15:36 -0600 | [diff] [blame] | 602 | return '_'.join( |
| 603 | [project_name, bt_comp.vendor_id, bt_comp.product_id, bt_comp.bcd_device]) |
C Shapiro | 90fda25 | 2020-04-17 14:34:57 -0500 | [diff] [blame] | 604 | |
| 605 | |
Andrew Lamb | cd33f70 | 2020-06-11 10:45:16 -0600 | [diff] [blame] | 606 | def _feature(name, present): |
C Shapiro | 5bf23a7 | 2020-04-24 11:40:17 -0500 | [diff] [blame] | 607 | attrib = {'name': name} |
| 608 | if present: |
| 609 | return etree.Element('feature', attrib=attrib) |
Andrew Lamb | cd33f70 | 2020-06-11 10:45:16 -0600 | [diff] [blame] | 610 | |
| 611 | return etree.Element('unavailable-feature', attrib=attrib) |
C Shapiro | 5bf23a7 | 2020-04-24 11:40:17 -0500 | [diff] [blame] | 612 | |
| 613 | |
Andrew Lamb | cd33f70 | 2020-06-11 10:45:16 -0600 | [diff] [blame] | 614 | def _any_present(features): |
Andrew Lamb | 2413c98 | 2020-05-29 12:15:36 -0600 | [diff] [blame] | 615 | return topology_pb2.HardwareFeatures.PRESENT in features |
C Shapiro | 5bf23a7 | 2020-04-24 11:40:17 -0500 | [diff] [blame] | 616 | |
| 617 | |
Andrew Lamb | cd33f70 | 2020-06-11 10:45:16 -0600 | [diff] [blame] | 618 | def _arc_hardware_feature_id(design_config): |
C Shapiro | 5bf23a7 | 2020-04-24 11:40:17 -0500 | [diff] [blame] | 619 | return design_config.id.value.lower().replace(':', '_') |
| 620 | |
| 621 | |
Andrew Lamb | cd33f70 | 2020-06-11 10:45:16 -0600 | [diff] [blame] | 622 | def _write_arc_hardware_feature_file(output_dir, file_name, config_content): |
David Burger | 77a1d31 | 2020-05-23 16:05:45 -0600 | [diff] [blame] | 623 | output_dir += '/arc' |
| 624 | os.makedirs(output_dir, exist_ok=True) |
| 625 | output = '%s/%s' % (output_dir, file_name) |
Andrew Lamb | 2413c98 | 2020-05-29 12:15:36 -0600 | [diff] [blame] | 626 | file_content = minidom.parseString(config_content).toprettyxml( |
| 627 | indent=' ', encoding='utf-8') |
C Shapiro | ea33cff | 2020-05-11 13:32:05 -0500 | [diff] [blame] | 628 | |
| 629 | with open(output, 'wb') as f: |
| 630 | f.write(file_content) |
| 631 | |
| 632 | |
Andrew Lamb | cd33f70 | 2020-06-11 10:45:16 -0600 | [diff] [blame] | 633 | def _write_arc_hardware_feature_files(config, output_dir, build_root_dir): |
C Shapiro | 5bf23a7 | 2020-04-24 11:40:17 -0500 | [diff] [blame] | 634 | """Writes ARC hardware_feature.xml files for each config |
| 635 | |
| 636 | Args: |
| 637 | config: Source ConfigBundle to process. |
| 638 | output_dir: Path to the generated output. |
C Shapiro | 5c87799 | 2020-04-29 12:11:28 -0500 | [diff] [blame] | 639 | build_root_path: Path to the config file from portage's perspective. |
C Shapiro | 5bf23a7 | 2020-04-24 11:40:17 -0500 | [diff] [blame] | 640 | Returns: |
| 641 | dict that maps the design_config_id onto the correct file. |
| 642 | """ |
Andrew Lamb | cd33f70 | 2020-06-11 10:45:16 -0600 | [diff] [blame] | 643 | # pylint: disable=too-many-locals |
C Shapiro | 5bf23a7 | 2020-04-24 11:40:17 -0500 | [diff] [blame] | 644 | result = {} |
C Shapiro | ea33cff | 2020-05-11 13:32:05 -0500 | [diff] [blame] | 645 | configs_by_design = {} |
C Shapiro | 5bf23a7 | 2020-04-24 11:40:17 -0500 | [diff] [blame] | 646 | for hw_design in config.designs.value: |
| 647 | for design_config in hw_design.configs: |
| 648 | hw_features = design_config.hardware_features |
Kazuhiro Inaba | 76d8bb5 | 2020-06-26 10:34:07 +0900 | [diff] [blame] | 649 | any_camera = hw_features.camera.count.value > 0 |
Josie Nordrum | adc27a7 | 2020-07-08 11:05:26 -0600 | [diff] [blame] | 650 | multi_camera = hw_features.camera.count.value > 1 |
Andrew Lamb | cd33f70 | 2020-06-11 10:45:16 -0600 | [diff] [blame] | 651 | touchscreen = _any_present([hw_features.screen.touch_support]) |
C Shapiro | 5bf23a7 | 2020-04-24 11:40:17 -0500 | [diff] [blame] | 652 | acc = hw_features.accelerometer |
| 653 | gyro = hw_features.gyroscope |
| 654 | compass = hw_features.magnetometer |
Andrew Lamb | cd33f70 | 2020-06-11 10:45:16 -0600 | [diff] [blame] | 655 | light_sensor = hw_features.light_sensor |
C Shapiro | 5bf23a7 | 2020-04-24 11:40:17 -0500 | [diff] [blame] | 656 | root = etree.Element('permissions') |
| 657 | root.extend([ |
Andrew Lamb | cd33f70 | 2020-06-11 10:45:16 -0600 | [diff] [blame] | 658 | _feature('android.hardware.camera', multi_camera), |
| 659 | _feature('android.hardware.camera.autofocus', multi_camera), |
Kazuhiro Inaba | 76d8bb5 | 2020-06-26 10:34:07 +0900 | [diff] [blame] | 660 | _feature('android.hardware.camera.any', any_camera), |
| 661 | _feature('android.hardware.camera.front', any_camera), |
Andrew Lamb | cd33f70 | 2020-06-11 10:45:16 -0600 | [diff] [blame] | 662 | _feature( |
| 663 | 'android.hardware.sensor.accelerometer', |
| 664 | _any_present([acc.lid_accelerometer, acc.base_accelerometer])), |
| 665 | _feature('android.hardware.sensor.gyroscope', |
| 666 | _any_present([gyro.lid_gyroscope, gyro.base_gyroscope])), |
| 667 | _feature( |
Andrew Lamb | 2413c98 | 2020-05-29 12:15:36 -0600 | [diff] [blame] | 668 | 'android.hardware.sensor.compass', |
Andrew Lamb | cd33f70 | 2020-06-11 10:45:16 -0600 | [diff] [blame] | 669 | _any_present( |
| 670 | [compass.lid_magnetometer, compass.base_magnetometer])), |
| 671 | _feature( |
| 672 | 'android.hardware.sensor.light', |
| 673 | _any_present( |
| 674 | [light_sensor.lid_lightsensor, |
| 675 | light_sensor.base_lightsensor])), |
| 676 | _feature('android.hardware.touchscreen', touchscreen), |
| 677 | _feature('android.hardware.touchscreen.multitouch', touchscreen), |
| 678 | _feature('android.hardware.touchscreen.multitouch.distinct', |
Andrew Lamb | 2413c98 | 2020-05-29 12:15:36 -0600 | [diff] [blame] | 679 | touchscreen), |
Andrew Lamb | cd33f70 | 2020-06-11 10:45:16 -0600 | [diff] [blame] | 680 | _feature('android.hardware.touchscreen.multitouch.jazzhand', |
Andrew Lamb | 2413c98 | 2020-05-29 12:15:36 -0600 | [diff] [blame] | 681 | touchscreen), |
C Shapiro | 5bf23a7 | 2020-04-24 11:40:17 -0500 | [diff] [blame] | 682 | ]) |
| 683 | |
C Shapiro | ea33cff | 2020-05-11 13:32:05 -0500 | [diff] [blame] | 684 | design_name = hw_design.name.lower() |
C Shapiro | 5bf23a7 | 2020-04-24 11:40:17 -0500 | [diff] [blame] | 685 | |
C Shapiro | ea33cff | 2020-05-11 13:32:05 -0500 | [diff] [blame] | 686 | # Constructs the following map: |
| 687 | # design_name -> config -> design_configs |
| 688 | # This allows any of the following file naming schemes: |
| 689 | # - All configs within a design share config (design_name prefix only) |
| 690 | # - Nobody shares (full design_name and config id prefix needed) |
| 691 | # |
| 692 | # Having shared configs when possible makes code reviews easier around |
| 693 | # the configs and makes debugging easier on the platform side. |
| 694 | config_content = etree.tostring(root) |
| 695 | arc_configs = configs_by_design.get(design_name, {}) |
| 696 | design_configs = arc_configs.get(config_content, []) |
| 697 | design_configs.append(design_config) |
| 698 | arc_configs[config_content] = design_configs |
| 699 | configs_by_design[design_name] = arc_configs |
C Shapiro | 9a3ac8c | 2020-04-25 07:49:21 -0500 | [diff] [blame] | 700 | |
C Shapiro | ea33cff | 2020-05-11 13:32:05 -0500 | [diff] [blame] | 701 | for design_name, unique_configs in configs_by_design.items(): |
| 702 | for file_content, design_configs in unique_configs.items(): |
Andrew Lamb | 2413c98 | 2020-05-29 12:15:36 -0600 | [diff] [blame] | 703 | file_name = 'hardware_features_%s.xml' % design_name |
| 704 | if len(unique_configs) == 1: |
Andrew Lamb | cd33f70 | 2020-06-11 10:45:16 -0600 | [diff] [blame] | 705 | _write_arc_hardware_feature_file(output_dir, file_name, file_content) |
C Shapiro | 9a3ac8c | 2020-04-25 07:49:21 -0500 | [diff] [blame] | 706 | |
Andrew Lamb | 2413c98 | 2020-05-29 12:15:36 -0600 | [diff] [blame] | 707 | for design_config in design_configs: |
Andrew Lamb | cd33f70 | 2020-06-11 10:45:16 -0600 | [diff] [blame] | 708 | feature_id = _arc_hardware_feature_id(design_config) |
Andrew Lamb | 2413c98 | 2020-05-29 12:15:36 -0600 | [diff] [blame] | 709 | if len(unique_configs) > 1: |
| 710 | file_name = 'hardware_features_%s.xml' % feature_id |
Andrew Lamb | cd33f70 | 2020-06-11 10:45:16 -0600 | [diff] [blame] | 711 | _write_arc_hardware_feature_file(output_dir, file_name, file_content) |
David Burger | 40dfe3a | 2020-06-18 17:09:13 -0600 | [diff] [blame] | 712 | result[feature_id] = _file_v2('%s/arc/%s' % (build_root_dir, file_name), |
| 713 | '/etc/%s' % file_name) |
C Shapiro | 5bf23a7 | 2020-04-24 11:40:17 -0500 | [diff] [blame] | 714 | return result |
| 715 | |
| 716 | |
Andrew Lamb | cd33f70 | 2020-06-11 10:45:16 -0600 | [diff] [blame] | 717 | def _write_bluetooth_config_files(config, output_dir, build_root_path): |
C Shapiro | 90fda25 | 2020-04-17 14:34:57 -0500 | [diff] [blame] | 718 | """Writes bluetooth conf files for every unique bluetooth chip. |
| 719 | |
| 720 | Args: |
| 721 | config: Source ConfigBundle to process. |
| 722 | output_dir: Path to the generated output. |
C Shapiro | 5c87799 | 2020-04-29 12:11:28 -0500 | [diff] [blame] | 723 | build_root_path: Path to the config file from portage's perspective. |
C Shapiro | 90fda25 | 2020-04-17 14:34:57 -0500 | [diff] [blame] | 724 | Returns: |
| 725 | dict that maps the bluetooth component id onto the file config. |
| 726 | """ |
David Burger | 77a1d31 | 2020-05-23 16:05:45 -0600 | [diff] [blame] | 727 | output_dir += '/bluetooth' |
C Shapiro | 90fda25 | 2020-04-17 14:34:57 -0500 | [diff] [blame] | 728 | result = {} |
| 729 | for hw_design in config.designs.value: |
| 730 | project_name = hw_design.name.lower() |
| 731 | for design_config in hw_design.configs: |
C Shapiro | 74da76e | 2020-05-04 13:02:20 -0500 | [diff] [blame] | 732 | bt_comp = design_config.hardware_features.bluetooth.component.usb |
C Shapiro | 90fda25 | 2020-04-17 14:34:57 -0500 | [diff] [blame] | 733 | if bt_comp.vendor_id: |
Andrew Lamb | cd33f70 | 2020-06-11 10:45:16 -0600 | [diff] [blame] | 734 | bt_id = _bluetooth_id(project_name, bt_comp) |
David Burger | 40dfe3a | 2020-06-18 17:09:13 -0600 | [diff] [blame] | 735 | result[bt_id] = _file_v2( |
| 736 | '%s/bluetooth/%s.conf' % (build_root_path, bt_id), |
| 737 | '/etc/bluetooth/%s/main.conf' % bt_id) |
C Shapiro | 90fda25 | 2020-04-17 14:34:57 -0500 | [diff] [blame] | 738 | bt_content = '''[General] |
Andrew Lamb | 2413c98 | 2020-05-29 12:15:36 -0600 | [diff] [blame] | 739 | DeviceID = bluetooth:%s:%s:%s''' % (bt_comp.vendor_id, bt_comp.product_id, |
C Shapiro | 90fda25 | 2020-04-17 14:34:57 -0500 | [diff] [blame] | 740 | bt_comp.bcd_device) |
| 741 | |
David Burger | 77a1d31 | 2020-05-23 16:05:45 -0600 | [diff] [blame] | 742 | os.makedirs(output_dir, exist_ok=True) |
| 743 | output = '%s/%s.conf' % (output_dir, bt_id) |
C Shapiro | 90fda25 | 2020-04-17 14:34:57 -0500 | [diff] [blame] | 744 | with open(output, 'w') as output_stream: |
| 745 | # Using print function adds proper trailing newline. |
| 746 | print(bt_content, file=output_stream) |
| 747 | return result |
| 748 | |
| 749 | |
Andrew Lamb | cd33f70 | 2020-06-11 10:45:16 -0600 | [diff] [blame] | 750 | def _read_config(path): |
David Burger | d4f3296 | 2020-05-02 12:07:40 -0600 | [diff] [blame] | 751 | """Reads a ConfigBundle proto from a json pb file. |
David Burger | e6f7622 | 2020-04-27 11:08:01 -0600 | [diff] [blame] | 752 | |
| 753 | Args: |
David Burger | d4f3296 | 2020-05-02 12:07:40 -0600 | [diff] [blame] | 754 | path: Path to the file encoding the json pb proto. |
David Burger | e6f7622 | 2020-04-27 11:08:01 -0600 | [diff] [blame] | 755 | """ |
| 756 | config = config_bundle_pb2.ConfigBundle() |
| 757 | with open(path, 'r') as f: |
| 758 | return json_format.Parse(f.read(), config) |
| 759 | |
| 760 | |
Andrew Lamb | cd33f70 | 2020-06-11 10:45:16 -0600 | [diff] [blame] | 761 | def _merge_configs(configs): |
David Burger | 7fd1dbe | 2020-03-26 09:26:55 -0600 | [diff] [blame] | 762 | result = config_bundle_pb2.ConfigBundle() |
| 763 | for config in configs: |
| 764 | result.MergeFrom(config) |
| 765 | |
| 766 | return result |
| 767 | |
| 768 | |
David Burger | 1ba78a2 | 2020-06-18 18:42:47 -0600 | [diff] [blame] | 769 | def _camera_map(configs, project_name): |
David Burger | 8ee9b4d | 2020-06-16 17:40:21 -0600 | [diff] [blame] | 770 | """Produces a camera config map for the given configs. |
| 771 | |
| 772 | Produces a map that maps from the design name to the camera config for that |
| 773 | design. |
| 774 | |
| 775 | Args: |
| 776 | configs: Source ConfigBundle to process. |
David Burger | 1ba78a2 | 2020-06-18 18:42:47 -0600 | [diff] [blame] | 777 | project_name: Name of project processing for. |
David Burger | 8ee9b4d | 2020-06-16 17:40:21 -0600 | [diff] [blame] | 778 | |
| 779 | Returns: |
| 780 | map from design name to camera config. |
| 781 | """ |
| 782 | result = {} |
| 783 | for design in configs.designs.value: |
| 784 | design_name = design.name |
David Burger | 0d9e846 | 2020-06-19 14:12:37 -0600 | [diff] [blame] | 785 | config_path = CAMERA_CONFIG_SOURCE_PATH_TEMPLATE.format(design_name.lower()) |
David Burger | 8ee9b4d | 2020-06-16 17:40:21 -0600 | [diff] [blame] | 786 | if os.path.exists(config_path): |
David Burger | 0d9e846 | 2020-06-19 14:12:37 -0600 | [diff] [blame] | 787 | destination = CAMERA_CONFIG_DEST_PATH_TEMPLATE.format(design_name.lower()) |
David Burger | 8ee9b4d | 2020-06-16 17:40:21 -0600 | [diff] [blame] | 788 | result[design_name] = { |
David Burger | 1ba78a2 | 2020-06-18 18:42:47 -0600 | [diff] [blame] | 789 | 'config-path': |
| 790 | destination, |
| 791 | 'config-file': |
| 792 | _file_v2(os.path.join(project_name, config_path), destination), |
David Burger | 8ee9b4d | 2020-06-16 17:40:21 -0600 | [diff] [blame] | 793 | } |
| 794 | return result |
| 795 | |
| 796 | |
David Burger | 52c9d32 | 2020-06-09 07:16:18 -0600 | [diff] [blame] | 797 | def _dptf_map(configs, project_name): |
| 798 | """Produces a dptf map for the given configs. |
| 799 | |
| 800 | Produces a map that maps from design name to the dptf file config for that |
| 801 | design. It looks for the dptf files at: |
| 802 | DPTF_PATH + DPTF_FILE |
| 803 | for a project wide config, that it maps under the empty string, and at: |
| 804 | DPTF_PATH + design_name + DPTF_FILE |
| 805 | for design specific configs that it maps under the design name. |
| 806 | |
| 807 | Args: |
| 808 | configs: Source ConfigBundle to process. |
| 809 | project_name: Name of project processing for. |
| 810 | |
| 811 | Returns: |
David Burger | 8ee9b4d | 2020-06-16 17:40:21 -0600 | [diff] [blame] | 812 | map from design name or empty string (project wide), to dptf config. |
David Burger | 52c9d32 | 2020-06-09 07:16:18 -0600 | [diff] [blame] | 813 | """ |
| 814 | result = {} |
David Burger | 52c9d32 | 2020-06-09 07:16:18 -0600 | [diff] [blame] | 815 | # Looking at top level for project wide, and then for each design name |
| 816 | # for design specific. |
| 817 | dirs = [""] + [d.name for d in configs.designs.value] |
| 818 | for directory in dirs: |
David Burger | a225276 | 2020-07-09 15:09:49 -0600 | [diff] [blame] | 819 | design = directory.lower() |
| 820 | if os.path.exists(os.path.join(DPTF_PATH, design, DPTF_FILE)): |
| 821 | project_dptf_path = os.path.join(project_name, design, 'dptf.dv') |
David Burger | 52c9d32 | 2020-06-09 07:16:18 -0600 | [diff] [blame] | 822 | dptf_file = { |
| 823 | 'dptf-dv': |
| 824 | project_dptf_path, |
| 825 | 'files': [ |
| 826 | _file( |
David Burger | a225276 | 2020-07-09 15:09:49 -0600 | [diff] [blame] | 827 | os.path.join(project_name, DPTF_PATH, design, DPTF_FILE), |
David Burger | 52c9d32 | 2020-06-09 07:16:18 -0600 | [diff] [blame] | 828 | os.path.join('/etc/dptf', project_dptf_path)) |
| 829 | ] |
| 830 | } |
| 831 | result[directory] = dptf_file |
| 832 | return result |
| 833 | |
| 834 | |
Andrew Lamb | cd33f70 | 2020-06-11 10:45:16 -0600 | [diff] [blame] | 835 | def Main(project_configs, program_config, output): # pylint: disable=invalid-name |
David Burger | 7fd1dbe | 2020-03-26 09:26:55 -0600 | [diff] [blame] | 836 | """Transforms source proto config into platform JSON. |
| 837 | |
| 838 | Args: |
| 839 | project_configs: List of source project configs to transform. |
| 840 | program_config: Program config for the given set of projects. |
| 841 | output: Output file that will be generated by the transform. |
| 842 | """ |
Andrew Lamb | cd33f70 | 2020-06-11 10:45:16 -0600 | [diff] [blame] | 843 | configs = _merge_configs([_read_config(program_config)] + |
| 844 | [_read_config(config) for config in project_configs]) |
C Shapiro | 5bf23a7 | 2020-04-24 11:40:17 -0500 | [diff] [blame] | 845 | bluetooth_files = {} |
| 846 | arc_hw_feature_files = {} |
C Shapiro | 2b6d533 | 2020-05-06 17:51:35 -0500 | [diff] [blame] | 847 | touch_fw = {} |
David Burger | 52c9d32 | 2020-06-09 07:16:18 -0600 | [diff] [blame] | 848 | dptf_map = {} |
David Burger | 8ee9b4d | 2020-06-16 17:40:21 -0600 | [diff] [blame] | 849 | camera_map = {} |
C Shapiro | 5bf23a7 | 2020-04-24 11:40:17 -0500 | [diff] [blame] | 850 | output_dir = os.path.dirname(output) |
C Shapiro | 5c87799 | 2020-04-29 12:11:28 -0500 | [diff] [blame] | 851 | build_root_dir = output_dir |
C Shapiro | 5c87799 | 2020-04-29 12:11:28 -0500 | [diff] [blame] | 852 | if 'sw_build_config' in output_dir: |
| 853 | full_path = os.path.realpath(output) |
Andrew Lamb | 2413c98 | 2020-05-29 12:15:36 -0600 | [diff] [blame] | 854 | project_name = re.match(r'.*/(\w*)/sw_build_config/.*', |
| 855 | full_path).groups(1)[0] |
C Shapiro | 5c87799 | 2020-04-29 12:11:28 -0500 | [diff] [blame] | 856 | # Projects don't know about each other until they are integrated into the |
| 857 | # build system. When this happens, the files need to be able to co-exist |
| 858 | # without any collisions. This prefixes the project name (which is how |
| 859 | # portage maps in the project), so project files co-exist and can be |
| 860 | # installed together. |
| 861 | # This is necessary to allow projects to share files at the program level |
| 862 | # without having portage file installation collisions. |
| 863 | build_root_dir = os.path.join(project_name, output_dir) |
C Shapiro | 6830e6c | 2020-04-29 13:29:56 -0500 | [diff] [blame] | 864 | |
David Burger | 1ba78a2 | 2020-06-18 18:42:47 -0600 | [diff] [blame] | 865 | camera_map = _camera_map(configs, project_name) |
David Burger | 52c9d32 | 2020-06-09 07:16:18 -0600 | [diff] [blame] | 866 | dptf_map = _dptf_map(configs, project_name) |
| 867 | |
C Shapiro | 2b6d533 | 2020-05-06 17:51:35 -0500 | [diff] [blame] | 868 | if os.path.exists(TOUCH_PATH): |
Andrew Lamb | cd33f70 | 2020-06-11 10:45:16 -0600 | [diff] [blame] | 869 | touch_fw = _build_touch_file_config(configs, project_name) |
| 870 | bluetooth_files = _write_bluetooth_config_files(configs, output_dir, |
| 871 | build_root_dir) |
| 872 | arc_hw_feature_files = _write_arc_hardware_feature_files( |
| 873 | configs, output_dir, build_root_dir) |
C Shapiro | 5bf23a7 | 2020-04-24 11:40:17 -0500 | [diff] [blame] | 874 | config_files = ConfigFiles( |
| 875 | bluetooth=bluetooth_files, |
| 876 | arc_hw_features=arc_hw_feature_files, |
C Shapiro | 2b6d533 | 2020-05-06 17:51:35 -0500 | [diff] [blame] | 877 | touch_fw=touch_fw, |
David Burger | 8ee9b4d | 2020-06-16 17:40:21 -0600 | [diff] [blame] | 878 | dptf_map=dptf_map, |
| 879 | camera_map=camera_map) |
Andrew Lamb | cd33f70 | 2020-06-11 10:45:16 -0600 | [diff] [blame] | 880 | write_output(_transform_build_configs(configs, config_files), output) |
David Burger | 7fd1dbe | 2020-03-26 09:26:55 -0600 | [diff] [blame] | 881 | |
| 882 | |
| 883 | def main(argv=None): |
| 884 | """Main program which parses args and runs |
| 885 | |
| 886 | Args: |
| 887 | argv: List of command line arguments, if None uses sys.argv. |
| 888 | """ |
| 889 | if argv is None: |
| 890 | argv = sys.argv[1:] |
Andrew Lamb | cd33f70 | 2020-06-11 10:45:16 -0600 | [diff] [blame] | 891 | opts = parse_args(argv) |
David Burger | 7fd1dbe | 2020-03-26 09:26:55 -0600 | [diff] [blame] | 892 | Main(opts.project_configs, opts.program_config, opts.output) |
| 893 | |
| 894 | |
| 895 | if __name__ == '__main__': |
| 896 | sys.exit(main(sys.argv[1:])) |