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 | |
Ren-Pei Zeng | ce869dd | 2020-08-18 01:29:37 +0800 | [diff] [blame] | 8 | # pylint: disable=too-many-lines |
| 9 | |
David Burger | 7fd1dbe | 2020-03-26 09:26:55 -0600 | [diff] [blame] | 10 | import argparse |
| 11 | import json |
| 12 | import pprint |
C Shapiro | 90fda25 | 2020-04-17 14:34:57 -0500 | [diff] [blame] | 13 | import os |
David Burger | 7fd1dbe | 2020-03-26 09:26:55 -0600 | [diff] [blame] | 14 | import sys |
C Shapiro | 90fda25 | 2020-04-17 14:34:57 -0500 | [diff] [blame] | 15 | import re |
C Shapiro | 5bf23a7 | 2020-04-24 11:40:17 -0500 | [diff] [blame] | 16 | import xml.etree.ElementTree as etree |
C Shapiro | 9a3ac8c | 2020-04-25 07:49:21 -0500 | [diff] [blame] | 17 | import xml.dom.minidom as minidom |
David Burger | 7fd1dbe | 2020-03-26 09:26:55 -0600 | [diff] [blame] | 18 | |
Andrew Lamb | 319cc92 | 2020-06-15 10:45:46 -0600 | [diff] [blame] | 19 | from typing import List |
| 20 | |
David Burger | 7fd1dbe | 2020-03-26 09:26:55 -0600 | [diff] [blame] | 21 | from collections import namedtuple |
| 22 | |
Andrew Lamb | cd33f70 | 2020-06-11 10:45:16 -0600 | [diff] [blame] | 23 | from google.protobuf import json_format |
| 24 | |
Prathmesh Prabhu | 72f8a00 | 2020-04-10 09:57:53 -0700 | [diff] [blame] | 25 | from chromiumos.config.api import device_brand_pb2 |
David Burger | 92609a3 | 2020-04-23 10:38:50 -0600 | [diff] [blame] | 26 | from chromiumos.config.api import topology_pb2 |
C Shapiro | 5bf23a7 | 2020-04-24 11:40:17 -0500 | [diff] [blame] | 27 | from chromiumos.config.payload import config_bundle_pb2 |
Prathmesh Prabhu | 72f8a00 | 2020-04-10 09:57:53 -0700 | [diff] [blame] | 28 | from chromiumos.config.api.software import brand_config_pb2 |
David Burger | 7fd1dbe | 2020-03-26 09:26:55 -0600 | [diff] [blame] | 29 | |
Andrew Lamb | 2413c98 | 2020-05-29 12:15:36 -0600 | [diff] [blame] | 30 | Config = namedtuple('Config', [ |
| 31 | 'program', 'hw_design', 'odm', 'hw_design_config', 'device_brand', |
| 32 | 'device_signer_config', 'oem', 'sw_config', 'brand_config', 'build_target' |
| 33 | ]) |
David Burger | 7fd1dbe | 2020-03-26 09:26:55 -0600 | [diff] [blame] | 34 | |
David Burger | 07af524 | 2020-08-11 11:08:25 -0600 | [diff] [blame] | 35 | ConfigFiles = namedtuple( |
David Burger | ceeb68a | 2020-09-03 11:31:10 -0600 | [diff] [blame] | 36 | 'ConfigFiles', |
| 37 | ['arc_hw_features', 'touch_fw', 'dptf_map', 'camera_map', 'wifi_sar_map']) |
David Burger | 8ee9b4d | 2020-06-16 17:40:21 -0600 | [diff] [blame] | 38 | |
| 39 | CAMERA_CONFIG_DEST_PATH_TEMPLATE = '/etc/camera/camera_config_{}.json' |
| 40 | CAMERA_CONFIG_SOURCE_PATH_TEMPLATE = ( |
| 41 | 'sw_build_config/platform/chromeos-config/camera/camera_config_{}.json') |
C Shapiro | 5bf23a7 | 2020-04-24 11:40:17 -0500 | [diff] [blame] | 42 | |
David Burger | 52c9d32 | 2020-06-09 07:16:18 -0600 | [diff] [blame] | 43 | DPTF_PATH = 'sw_build_config/platform/chromeos-config/thermal' |
| 44 | DPTF_FILE = 'dptf.dv' |
David Burger | 2f0d952 | 2020-07-30 10:52:28 -0600 | [diff] [blame] | 45 | |
C Shapiro | 2b6d533 | 2020-05-06 17:51:35 -0500 | [diff] [blame] | 46 | TOUCH_PATH = 'sw_build_config/platform/chromeos-config/touch' |
Andrew Lamb | 6c42efc | 2020-06-16 10:40:43 -0600 | [diff] [blame] | 47 | WALLPAPER_BASE_PATH = '/usr/share/chromeos-assets/wallpaper' |
David Burger | 7fd1dbe | 2020-03-26 09:26:55 -0600 | [diff] [blame] | 48 | |
Andrew Lamb | 2413c98 | 2020-05-29 12:15:36 -0600 | [diff] [blame] | 49 | |
Andrew Lamb | cd33f70 | 2020-06-11 10:45:16 -0600 | [diff] [blame] | 50 | def parse_args(argv): |
David Burger | 7fd1dbe | 2020-03-26 09:26:55 -0600 | [diff] [blame] | 51 | """Parse the available arguments. |
| 52 | |
| 53 | Invalid arguments or -h cause this function to print a message and exit. |
| 54 | |
| 55 | Args: |
| 56 | argv: List of string arguments (excluding program name / argv[0]) |
| 57 | |
| 58 | Returns: |
| 59 | argparse.Namespace object containing the attributes. |
| 60 | """ |
| 61 | parser = argparse.ArgumentParser( |
| 62 | description='Converts source proto config into platform JSON config.') |
| 63 | parser.add_argument( |
| 64 | '-c', |
| 65 | '--project_configs', |
| 66 | nargs='+', |
| 67 | type=str, |
| 68 | help='Space delimited list of source protobinary project config files.') |
| 69 | parser.add_argument( |
| 70 | '-p', |
| 71 | '--program_config', |
| 72 | type=str, |
| 73 | help='Path to the source program-level protobinary file') |
| 74 | parser.add_argument( |
Andrew Lamb | 2413c98 | 2020-05-29 12:15:36 -0600 | [diff] [blame] | 75 | '-o', '--output', type=str, help='Output file that will be generated') |
David Burger | 7fd1dbe | 2020-03-26 09:26:55 -0600 | [diff] [blame] | 76 | return parser.parse_args(argv) |
| 77 | |
| 78 | |
David Burger | 8ee9b4d | 2020-06-16 17:40:21 -0600 | [diff] [blame] | 79 | def _upsert(field, target, target_name): |
| 80 | """Updates or inserts `field` within `target`. |
| 81 | |
| 82 | If `target_name` already exists within `target` an update is performed, |
| 83 | otherwise, an insert is performed. |
| 84 | """ |
Sam McNally | 9a873f7 | 2020-06-05 19:47:22 +1000 | [diff] [blame] | 85 | if field or field == 0: |
David Burger | 8ee9b4d | 2020-06-16 17:40:21 -0600 | [diff] [blame] | 86 | if target_name in target: |
| 87 | target[target_name].update(field) |
| 88 | else: |
| 89 | target[target_name] = field |
David Burger | 7fd1dbe | 2020-03-26 09:26:55 -0600 | [diff] [blame] | 90 | |
| 91 | |
Andrew Lamb | cd33f70 | 2020-06-11 10:45:16 -0600 | [diff] [blame] | 92 | def _build_arc(config, config_files): |
| 93 | if not config.build_target.arc: |
| 94 | return None |
| 95 | |
| 96 | build_properties = { |
| 97 | 'device': config.build_target.arc.device, |
| 98 | 'first-api-level': config.build_target.arc.first_api_level, |
| 99 | 'marketing-name': config.device_brand.brand_name, |
| 100 | 'metrics-tag': config.hw_design.name.lower(), |
| 101 | 'product': config.build_target.id.value, |
| 102 | } |
| 103 | if config.oem: |
| 104 | build_properties['oem'] = config.oem.name |
| 105 | result = {'build-properties': build_properties} |
| 106 | feature_id = _arc_hardware_feature_id(config.hw_design_config) |
| 107 | if feature_id in config_files.arc_hw_features: |
| 108 | result['hardware-features'] = config_files.arc_hw_features[feature_id] |
| 109 | topology = config.hw_design_config.hardware_topology |
| 110 | ppi = topology.screen.hardware_feature.screen.panel_properties.pixels_per_in |
| 111 | # Only set for high resolution displays |
| 112 | if ppi and ppi > 250: |
| 113 | result['scale'] = ppi |
David Burger | 2f0d952 | 2020-07-30 10:52:28 -0600 | [diff] [blame] | 114 | |
Andrew Lamb | cd33f70 | 2020-06-11 10:45:16 -0600 | [diff] [blame] | 115 | return result |
David Burger | 7fd1dbe | 2020-03-26 09:26:55 -0600 | [diff] [blame] | 116 | |
Andrew Lamb | 2413c98 | 2020-05-29 12:15:36 -0600 | [diff] [blame] | 117 | |
Andrew Lamb | 319cc92 | 2020-06-15 10:45:46 -0600 | [diff] [blame] | 118 | def _build_ash_flags(config: Config) -> List[str]: |
| 119 | """Returns a list of Ash flags for config. |
| 120 | |
| 121 | Ash is the window manager and system UI for ChromeOS, see |
| 122 | https://chromium.googlesource.com/chromium/src/+/refs/heads/master/ash/. |
| 123 | """ |
| 124 | # A map from flag name -> value. Value may be None for boolean flags. |
| 125 | flags = {} |
| 126 | |
| 127 | hw_features = config.hw_design_config.hardware_features |
| 128 | if hw_features.stylus.stylus == topology_pb2.HardwareFeatures.Stylus.INTERNAL: |
Andrew Lamb | 2e641e2 | 2020-06-15 12:30:41 -0600 | [diff] [blame] | 129 | flags['has-internal-stylus'] = None |
Andrew Lamb | 319cc92 | 2020-06-15 10:45:46 -0600 | [diff] [blame] | 130 | |
Andrew Lamb | 2e641e2 | 2020-06-15 12:30:41 -0600 | [diff] [blame] | 131 | fp_loc = hw_features.fingerprint.location |
| 132 | if fp_loc and fp_loc != topology_pb2.HardwareFeatures.Fingerprint.NOT_PRESENT: |
| 133 | loc_name = topology_pb2.HardwareFeatures.Fingerprint.Location.Name(fp_loc) |
| 134 | flags['fingerprint-sensor-location'] = loc_name.lower().replace('_', '-') |
| 135 | |
Andrew Lamb | 6c42efc | 2020-06-16 10:40:43 -0600 | [diff] [blame] | 136 | wallpaper = config.brand_config.wallpaper |
| 137 | # If a wallpaper is set, the 'default-wallpaper-is-oem' flag needs to be set. |
| 138 | # If a wallpaper is not set, the 'default_[large|small].jpg' wallpapers |
| 139 | # should still be set. |
| 140 | if wallpaper: |
| 141 | flags['default-wallpaper-is-oem'] = None |
| 142 | else: |
| 143 | wallpaper = 'default' |
| 144 | |
| 145 | for size in ('small', 'large'): |
| 146 | flags[f'default-wallpaper-{size}'] = ( |
| 147 | f'{WALLPAPER_BASE_PATH}/{wallpaper}_{size}.jpg') |
| 148 | |
| 149 | # For each size, also install 'guest' and 'child' wallpapers. |
| 150 | for wallpaper_type in ('guest', 'child'): |
| 151 | flags[f'{wallpaper_type}-wallpaper-{size}'] = ( |
| 152 | f'{WALLPAPER_BASE_PATH}/{wallpaper_type}_{size}.jpg') |
| 153 | |
Andrew Lamb | 72d4136 | 2020-06-17 09:19:02 -0600 | [diff] [blame] | 154 | flags['arc-build-properties'] = json_format.MessageToDict( |
| 155 | config.build_target.arc) |
| 156 | |
Andrew Lamb | 90b168c | 2020-06-22 10:42:30 -0600 | [diff] [blame] | 157 | power_button = hw_features.power_button |
| 158 | if power_button.edge: |
| 159 | flags['ash-power-button-position'] = json.dumps({ |
| 160 | 'edge': |
| 161 | topology_pb2.HardwareFeatures.Button.Edge.Name(power_button.edge |
| 162 | ).lower(), |
| 163 | # Starlark sometimes represents float literals strangely, e.g. changing |
| 164 | # 0.9 to 0.899999. Round to two digits here. |
| 165 | 'position': |
| 166 | round(power_button.position, 2) |
| 167 | }) |
| 168 | |
| 169 | volume_button = hw_features.volume_button |
| 170 | if volume_button.edge: |
| 171 | flags['ash-side-volume-button-position'] = json.dumps({ |
| 172 | 'region': |
| 173 | topology_pb2.HardwareFeatures.Button.Region.Name( |
| 174 | volume_button.region).lower(), |
| 175 | 'edge': |
| 176 | topology_pb2.HardwareFeatures.Button.Edge.Name(volume_button.edge |
| 177 | ).lower(), |
| 178 | }) |
| 179 | |
Andrew Lamb | 2e641e2 | 2020-06-15 12:30:41 -0600 | [diff] [blame] | 180 | 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] | 181 | |
| 182 | |
| 183 | def _build_ui(config: Config) -> dict: |
| 184 | """Builds the 'ui' property from cros_config_schema.""" |
| 185 | return {'extra-ash-flags': _build_ash_flags(config)} |
| 186 | |
| 187 | |
David Burger | 07af524 | 2020-08-11 11:08:25 -0600 | [diff] [blame] | 188 | def _build_bluetooth(config): |
C Shapiro | 90fda25 | 2020-04-17 14:34:57 -0500 | [diff] [blame] | 189 | bt_flags = config.sw_config.bluetooth_config.flags |
| 190 | # Convert to native map (from proto wrapper) |
| 191 | bt_flags_map = dict(bt_flags) |
| 192 | result = {} |
| 193 | if bt_flags_map: |
| 194 | result['flags'] = bt_flags_map |
C Shapiro | 90fda25 | 2020-04-17 14:34:57 -0500 | [diff] [blame] | 195 | return result |
| 196 | |
David Burger | 7fd1dbe | 2020-03-26 09:26:55 -0600 | [diff] [blame] | 197 | |
David Burger | ceeb68a | 2020-09-03 11:31:10 -0600 | [diff] [blame] | 198 | def _build_ath10k_config(ath10k_config): |
| 199 | """Builds the wifi configuration for the ath10k driver. |
| 200 | |
| 201 | Args: |
| 202 | ath10k_config: Ath10kConfig config. |
| 203 | |
| 204 | Returns: |
| 205 | wifi configuration for the ath10k driver. |
| 206 | """ |
David Burger | ec75391 | 2020-08-10 12:59:11 -0600 | [diff] [blame] | 207 | result = {} |
David Burger | ec75391 | 2020-08-10 12:59:11 -0600 | [diff] [blame] | 208 | |
David Burger | ceeb68a | 2020-09-03 11:31:10 -0600 | [diff] [blame] | 209 | def power_chain(power): |
| 210 | return { |
| 211 | 'limit-2g': power.limit_2g, |
| 212 | 'limit-5g': power.limit_5g, |
| 213 | } |
David Burger | ec75391 | 2020-08-10 12:59:11 -0600 | [diff] [blame] | 214 | |
David Burger | ceeb68a | 2020-09-03 11:31:10 -0600 | [diff] [blame] | 215 | result['tablet-mode-power-table-ath10k'] = power_chain( |
| 216 | ath10k_config.tablet_mode_power_table) |
| 217 | result['non-tablet-mode-power-table-ath10k'] = power_chain( |
| 218 | ath10k_config.non_tablet_mode_power_table) |
David Burger | ec75391 | 2020-08-10 12:59:11 -0600 | [diff] [blame] | 219 | return result |
| 220 | |
| 221 | |
David Burger | ceeb68a | 2020-09-03 11:31:10 -0600 | [diff] [blame] | 222 | def _build_rtw88_config(rtw88_config): |
| 223 | """Builds the wifi configuration for the rtw88 driver. |
| 224 | |
| 225 | Args: |
| 226 | rtw88_config: Rtw88Config config. |
| 227 | |
| 228 | Returns: |
| 229 | wifi configuration for the rtw88 driver. |
| 230 | """ |
| 231 | result = {} |
| 232 | |
| 233 | def power_chain(power): |
| 234 | return { |
| 235 | 'limit-2g': power.limit_2g, |
| 236 | 'limit-5g-1': power.limit_5g_1, |
| 237 | 'limit-5g-3': power.limit_5g_3, |
| 238 | 'limit-5g-4': power.limit_5g_4, |
| 239 | } |
| 240 | |
| 241 | result['tablet-mode-power-table-rtw'] = power_chain( |
| 242 | rtw88_config.tablet_mode_power_table) |
| 243 | result['non-tablet-mode-power-table-rtw'] = power_chain( |
| 244 | rtw88_config.non_tablet_mode_power_table) |
| 245 | |
| 246 | def offsets(offset): |
| 247 | return { |
| 248 | 'offset-2g': offset.offset_2g, |
| 249 | 'offset-5g': offset.offset_5g, |
| 250 | } |
| 251 | |
| 252 | result['geo-offsets-fcc'] = offsets(rtw88_config.offset_fcc) |
| 253 | result['geo-offsets-eu'] = offsets(rtw88_config.offset_eu) |
| 254 | result['geo-offsets-rest-of-world'] = offsets(rtw88_config.offset_other) |
| 255 | return result |
| 256 | |
| 257 | |
| 258 | def _build_intel_config(config, config_files): |
| 259 | """Builds the wifi configuration for the intel driver. |
| 260 | |
| 261 | Args: |
| 262 | config: Config namedtuple |
| 263 | config_files: Map to look up the generated config files. |
| 264 | |
| 265 | Returns: |
| 266 | wifi configuration for the intel driver. |
| 267 | """ |
| 268 | design_name = config.hw_design.name.lower() |
| 269 | return config_files.wifi_sar_map.get(design_name) |
| 270 | |
| 271 | |
| 272 | def _build_wifi(config, config_files): |
| 273 | """Builds the wifi configuration. |
| 274 | |
| 275 | Args: |
| 276 | config: Config namedtuple |
| 277 | config_files: Map to look up the generated config files. |
| 278 | |
| 279 | Returns: |
| 280 | wifi configuration. |
| 281 | """ |
| 282 | config_field = config.sw_config.wifi_config.WhichOneof('wifi_config') |
| 283 | if config_field == 'ath10k_config': |
| 284 | return _build_ath10k_config(config.sw_config.wifi_config.ath10k_config) |
| 285 | if config_field == 'rtw88_config': |
| 286 | return _build_rtw88_config(config.sw_config.wifi_config.rtw88_config) |
| 287 | if config_field == 'intel_config': |
| 288 | return _build_intel_config(config, config_files) |
| 289 | return {} |
| 290 | |
| 291 | |
Andrew Lamb | cd33f70 | 2020-06-11 10:45:16 -0600 | [diff] [blame] | 292 | def _build_fingerprint(hw_topology): |
| 293 | if not hw_topology.HasField('fingerprint'): |
| 294 | return None |
| 295 | |
| 296 | fp = hw_topology.fingerprint.hardware_feature.fingerprint |
| 297 | result = {} |
| 298 | if fp.location != topology_pb2.HardwareFeatures.Fingerprint.NOT_PRESENT: |
| 299 | location = fp.Location.DESCRIPTOR.values_by_number[fp.location].name |
| 300 | result['sensor-location'] = location.lower().replace('_', '-') |
| 301 | if fp.board: |
| 302 | result['board'] = fp.board |
Tom Hughes | dfc3540 | 2020-06-29 16:02:09 -0700 | [diff] [blame] | 303 | if fp.ro_version: |
| 304 | result['ro-version'] = fp.ro_version |
| 305 | |
Andrew Lamb | cd33f70 | 2020-06-11 10:45:16 -0600 | [diff] [blame] | 306 | return result |
David Burger | 7fd1dbe | 2020-03-26 09:26:55 -0600 | [diff] [blame] | 307 | |
| 308 | |
Trent Begin | f067ccb | 2020-08-12 12:33:53 -0600 | [diff] [blame] | 309 | def _build_hardware_properties(hw_topology): |
| 310 | if not hw_topology.HasField('form_factor'): |
| 311 | return None |
| 312 | |
| 313 | form_factor = hw_topology.form_factor.hardware_feature.form_factor.form_factor |
| 314 | result = {} |
| 315 | if form_factor in [ |
| 316 | topology_pb2.HardwareFeatures.FormFactor.CHROMEBIT, |
| 317 | topology_pb2.HardwareFeatures.FormFactor.CHROMEBASE, |
| 318 | topology_pb2.HardwareFeatures.FormFactor.CHROMEBOX |
| 319 | ]: |
| 320 | result['psu-type'] = "AC_only" |
| 321 | else: |
| 322 | result['psu-type'] = "battery" |
| 323 | |
| 324 | result['has-backlight'] = form_factor not in [ |
| 325 | topology_pb2.HardwareFeatures.FormFactor.CHROMEBIT, |
| 326 | topology_pb2.HardwareFeatures.FormFactor.CHROMEBOX |
| 327 | ] |
| 328 | |
| 329 | return result |
| 330 | |
| 331 | |
Andrew Lamb | cd33f70 | 2020-06-11 10:45:16 -0600 | [diff] [blame] | 332 | def _fw_bcs_path(payload): |
David Burger | 7fd1dbe | 2020-03-26 09:26:55 -0600 | [diff] [blame] | 333 | if payload and payload.firmware_image_name: |
Andrew Lamb | 2413c98 | 2020-05-29 12:15:36 -0600 | [diff] [blame] | 334 | return 'bcs://%s.%d.%d.0.tbz2' % (payload.firmware_image_name, |
| 335 | payload.version.major, |
| 336 | payload.version.minor) |
David Burger | 7fd1dbe | 2020-03-26 09:26:55 -0600 | [diff] [blame] | 337 | |
Andrew Lamb | cd33f70 | 2020-06-11 10:45:16 -0600 | [diff] [blame] | 338 | return None |
David Burger | 7fd1dbe | 2020-03-26 09:26:55 -0600 | [diff] [blame] | 339 | |
Andrew Lamb | cd33f70 | 2020-06-11 10:45:16 -0600 | [diff] [blame] | 340 | |
| 341 | def _fw_build_target(payload): |
David Burger | 7fd1dbe | 2020-03-26 09:26:55 -0600 | [diff] [blame] | 342 | if payload: |
| 343 | return payload.build_target_name |
| 344 | |
Andrew Lamb | cd33f70 | 2020-06-11 10:45:16 -0600 | [diff] [blame] | 345 | return None |
David Burger | 7fd1dbe | 2020-03-26 09:26:55 -0600 | [diff] [blame] | 346 | |
Andrew Lamb | cd33f70 | 2020-06-11 10:45:16 -0600 | [diff] [blame] | 347 | |
| 348 | def _build_firmware(config): |
David Burger | b70b676 | 2020-05-21 12:14:59 -0600 | [diff] [blame] | 349 | """Returns firmware config, or None if no build targets.""" |
Andrew Lamb | 3da156d | 2020-04-16 16:00:56 -0600 | [diff] [blame] | 350 | fw_payload_config = config.sw_config.firmware |
| 351 | fw_build_config = config.sw_config.firmware_build_config |
| 352 | main_ro = fw_payload_config.main_ro_payload |
| 353 | main_rw = fw_payload_config.main_rw_payload |
| 354 | ec_ro = fw_payload_config.ec_ro_payload |
| 355 | pd_ro = fw_payload_config.pd_ro_payload |
David Burger | 7fd1dbe | 2020-03-26 09:26:55 -0600 | [diff] [blame] | 356 | |
| 357 | build_targets = {} |
Andrew Lamb | 3da156d | 2020-04-16 16:00:56 -0600 | [diff] [blame] | 358 | |
David Burger | 8ee9b4d | 2020-06-16 17:40:21 -0600 | [diff] [blame] | 359 | _upsert(fw_build_config.build_targets.depthcharge, build_targets, |
| 360 | 'depthcharge') |
| 361 | _upsert(fw_build_config.build_targets.coreboot, build_targets, 'coreboot') |
| 362 | _upsert(fw_build_config.build_targets.ec, build_targets, 'ec') |
| 363 | _upsert( |
Andrew Lamb | f8954ee | 2020-04-21 10:24:40 -0600 | [diff] [blame] | 364 | list(fw_build_config.build_targets.ec_extras), build_targets, 'ec_extras') |
David Burger | 8ee9b4d | 2020-06-16 17:40:21 -0600 | [diff] [blame] | 365 | _upsert(fw_build_config.build_targets.libpayload, build_targets, 'libpayload') |
David Burger | 7fd1dbe | 2020-03-26 09:26:55 -0600 | [diff] [blame] | 366 | |
David Burger | b70b676 | 2020-05-21 12:14:59 -0600 | [diff] [blame] | 367 | if not build_targets: |
| 368 | return None |
| 369 | |
David Burger | 7fd1dbe | 2020-03-26 09:26:55 -0600 | [diff] [blame] | 370 | result = { |
| 371 | 'bcs-overlay': config.build_target.overlay_name, |
| 372 | 'build-targets': build_targets, |
David Burger | 7fd1dbe | 2020-03-26 09:26:55 -0600 | [diff] [blame] | 373 | } |
Andrew Lamb | 883fa04 | 2020-04-06 11:37:22 -0600 | [diff] [blame] | 374 | |
David Burger | 8ee9b4d | 2020-06-16 17:40:21 -0600 | [diff] [blame] | 375 | _upsert(main_ro.firmware_image_name.lower(), result, 'image-name') |
Andrew Lamb | 883fa04 | 2020-04-06 11:37:22 -0600 | [diff] [blame] | 376 | |
David Burger | 8ee9b4d | 2020-06-16 17:40:21 -0600 | [diff] [blame] | 377 | _upsert(_fw_bcs_path(main_ro), result, 'main-ro-image') |
| 378 | _upsert(_fw_bcs_path(main_rw), result, 'main-rw-image') |
| 379 | _upsert(_fw_bcs_path(ec_ro), result, 'ec-ro-image') |
| 380 | _upsert(_fw_bcs_path(pd_ro), result, 'pd-ro-image') |
David Burger | 7fd1dbe | 2020-03-26 09:26:55 -0600 | [diff] [blame] | 381 | |
David Burger | 8ee9b4d | 2020-06-16 17:40:21 -0600 | [diff] [blame] | 382 | _upsert( |
Andrew Lamb | f39fbe8 | 2020-04-13 16:14:33 -0600 | [diff] [blame] | 383 | config.hw_design_config.hardware_features.fw_config.value, |
| 384 | result, |
| 385 | 'firmware-config', |
| 386 | ) |
| 387 | |
David Burger | 7fd1dbe | 2020-03-26 09:26:55 -0600 | [diff] [blame] | 388 | return result |
| 389 | |
| 390 | |
Sam McNally | fd14e4b | 2020-09-12 10:26:35 +1000 | [diff] [blame] | 391 | def _build_fw_signing(config, whitelabel): |
C Shapiro | 2f0bb5d | 2020-04-14 10:07:47 -0500 | [diff] [blame] | 392 | if config.sw_config.firmware and config.device_signer_config: |
David Burger | 68e0d14 | 2020-05-15 17:29:33 -0600 | [diff] [blame] | 393 | hw_design = config.hw_design.name.lower() |
Sam McNally | 2fc807f | 2020-07-16 18:13:53 +1000 | [diff] [blame] | 394 | brand_scan_config = config.brand_config.scan_config |
| 395 | if brand_scan_config and brand_scan_config.whitelabel_tag: |
| 396 | signature_id = '%s-%s' % (hw_design, brand_scan_config.whitelabel_tag) |
| 397 | else: |
| 398 | signature_id = hw_design |
| 399 | |
Sam McNally | fd14e4b | 2020-09-12 10:26:35 +1000 | [diff] [blame] | 400 | result = { |
C Shapiro | 2f0bb5d | 2020-04-14 10:07:47 -0500 | [diff] [blame] | 401 | 'key-id': config.device_signer_config.key_id, |
Sam McNally | 2fc807f | 2020-07-16 18:13:53 +1000 | [diff] [blame] | 402 | 'signature-id': signature_id, |
C Shapiro | 2f0bb5d | 2020-04-14 10:07:47 -0500 | [diff] [blame] | 403 | } |
Sam McNally | fd14e4b | 2020-09-12 10:26:35 +1000 | [diff] [blame] | 404 | if whitelabel: |
| 405 | result['sig-id-in-customization-id'] = True |
| 406 | return result |
C Shapiro | 2f0bb5d | 2020-04-14 10:07:47 -0500 | [diff] [blame] | 407 | return {} |
David Burger | 7fd1dbe | 2020-03-26 09:26:55 -0600 | [diff] [blame] | 408 | |
| 409 | |
Andrew Lamb | cd33f70 | 2020-06-11 10:45:16 -0600 | [diff] [blame] | 410 | def _file(source, destination): |
Andrew Lamb | 2413c98 | 2020-05-29 12:15:36 -0600 | [diff] [blame] | 411 | return {'destination': destination, 'source': source} |
David Burger | 7fd1dbe | 2020-03-26 09:26:55 -0600 | [diff] [blame] | 412 | |
| 413 | |
David Burger | 40dfe3a | 2020-06-18 17:09:13 -0600 | [diff] [blame] | 414 | def _file_v2(build_path, system_path): |
| 415 | return {'build-path': build_path, 'system-path': system_path} |
| 416 | |
| 417 | |
Andrew Lamb | cd33f70 | 2020-06-11 10:45:16 -0600 | [diff] [blame] | 418 | def _build_audio(config): |
David Burger | 178f3ef | 2020-06-26 12:11:57 -0600 | [diff] [blame] | 419 | if not config.sw_config.audio_configs: |
| 420 | return {} |
David Burger | 7fd1dbe | 2020-03-26 09:26:55 -0600 | [diff] [blame] | 421 | alsa_path = '/usr/share/alsa/ucm' |
| 422 | cras_path = '/etc/cras' |
| 423 | project_name = config.hw_design.name.lower() |
David Burger | 4325066 | 2020-05-07 11:21:50 -0600 | [diff] [blame] | 424 | program_name = config.program.name.lower() |
David Burger | 7fd1dbe | 2020-03-26 09:26:55 -0600 | [diff] [blame] | 425 | files = [] |
David Burger | 178f3ef | 2020-06-26 12:11:57 -0600 | [diff] [blame] | 426 | ucm_suffix = None |
| 427 | for audio in config.sw_config.audio_configs: |
| 428 | card = audio.card_name |
| 429 | card_with_suffix = audio.card_name |
| 430 | if audio.ucm_suffix: |
| 431 | # TODO: last ucm_suffix wins. |
| 432 | ucm_suffix = audio.ucm_suffix |
| 433 | card_with_suffix += '.' + audio.ucm_suffix |
| 434 | if audio.ucm_file: |
| 435 | files.append( |
| 436 | _file(audio.ucm_file, |
| 437 | '%s/%s/HiFi.conf' % (alsa_path, card_with_suffix))) |
| 438 | if audio.ucm_master_file: |
| 439 | files.append( |
| 440 | _file( |
| 441 | audio.ucm_master_file, '%s/%s/%s.conf' % |
Andrew Lamb | 2413c98 | 2020-05-29 12:15:36 -0600 | [diff] [blame] | 442 | (alsa_path, card_with_suffix, card_with_suffix))) |
David Burger | 178f3ef | 2020-06-26 12:11:57 -0600 | [diff] [blame] | 443 | if audio.card_config_file: |
| 444 | files.append( |
| 445 | _file(audio.card_config_file, |
| 446 | '%s/%s/%s' % (cras_path, project_name, card))) |
| 447 | if audio.dsp_file: |
| 448 | files.append( |
| 449 | _file(audio.dsp_file, '%s/%s/dsp.ini' % (cras_path, project_name))) |
| 450 | if audio.module_file: |
| 451 | files.append( |
| 452 | _file(audio.module_file, |
| 453 | '/etc/modprobe.d/alsa-%s.conf' % program_name)) |
| 454 | if audio.board_file: |
| 455 | files.append( |
| 456 | _file(audio.board_file, |
| 457 | '%s/%s/board.ini' % (cras_path, project_name))) |
David Burger | 599ff7b | 2020-04-06 16:29:31 -0600 | [diff] [blame] | 458 | |
| 459 | result = { |
David Burger | 7fd1dbe | 2020-03-26 09:26:55 -0600 | [diff] [blame] | 460 | 'main': { |
| 461 | 'cras-config-dir': project_name, |
| 462 | 'files': files, |
| 463 | } |
| 464 | } |
David Burger | 178f3ef | 2020-06-26 12:11:57 -0600 | [diff] [blame] | 465 | |
| 466 | if ucm_suffix: |
| 467 | result['main']['ucm-suffix'] = ucm_suffix |
David Burger | 599ff7b | 2020-04-06 16:29:31 -0600 | [diff] [blame] | 468 | |
| 469 | return result |
David Burger | 7fd1dbe | 2020-03-26 09:26:55 -0600 | [diff] [blame] | 470 | |
| 471 | |
Andrew Lamb | cd33f70 | 2020-06-11 10:45:16 -0600 | [diff] [blame] | 472 | def _build_camera(hw_topology): |
David Burger | 8aa8fa3 | 2020-04-14 08:30:34 -0600 | [diff] [blame] | 473 | if hw_topology.HasField('camera'): |
Ren-Pei Zeng | ce869dd | 2020-08-18 01:29:37 +0800 | [diff] [blame] | 474 | camera_pb = topology_pb2.HardwareFeatures.Camera |
David Burger | 8aa8fa3 | 2020-04-14 08:30:34 -0600 | [diff] [blame] | 475 | camera = hw_topology.camera.hardware_feature.camera |
| 476 | result = {} |
Ren-Pei Zeng | ce869dd | 2020-08-18 01:29:37 +0800 | [diff] [blame] | 477 | if camera.devices: |
Ren-Pei Zeng | 9b5682d | 2020-10-14 17:37:30 +0800 | [diff] [blame^] | 478 | result['count'] = len(camera.devices) |
Ren-Pei Zeng | ce869dd | 2020-08-18 01:29:37 +0800 | [diff] [blame] | 479 | result['devices'] = [] |
| 480 | for device in camera.devices: |
| 481 | interface = { |
| 482 | camera_pb.INTERFACE_USB: 'usb', |
| 483 | camera_pb.INTERFACE_MIPI: 'mipi', |
| 484 | }[device.interface] |
| 485 | facing = { |
| 486 | camera_pb.FACING_FRONT: 'front', |
| 487 | camera_pb.FACING_BACK: 'back', |
| 488 | }[device.facing] |
| 489 | orientation = { |
| 490 | camera_pb.ORIENTATION_0: 0, |
| 491 | camera_pb.ORIENTATION_90: 90, |
| 492 | camera_pb.ORIENTATION_180: 180, |
| 493 | camera_pb.ORIENTATION_270: 270, |
| 494 | }[device.orientation] |
Ren-Pei Zeng | 0bf9635 | 2020-09-28 18:44:42 +0800 | [diff] [blame] | 495 | flags = { |
| 496 | 'support-1080p': |
| 497 | bool(device.flags & camera_pb.FLAGS_SUPPORT_1080P), |
| 498 | 'support-autofocus': |
| 499 | bool(device.flags & camera_pb.FLAGS_SUPPORT_AUTOFOCUS), |
| 500 | } |
Ren-Pei Zeng | ce869dd | 2020-08-18 01:29:37 +0800 | [diff] [blame] | 501 | result['devices'].append({ |
Ren-Pei Zeng | ce869dd | 2020-08-18 01:29:37 +0800 | [diff] [blame] | 502 | 'interface': interface, |
| 503 | 'facing': facing, |
| 504 | 'orientation': orientation, |
Ren-Pei Zeng | 0bf9635 | 2020-09-28 18:44:42 +0800 | [diff] [blame] | 505 | 'flags': flags, |
| 506 | 'ids': list(device.ids), |
Ren-Pei Zeng | ce869dd | 2020-08-18 01:29:37 +0800 | [diff] [blame] | 507 | }) |
David Burger | 8aa8fa3 | 2020-04-14 08:30:34 -0600 | [diff] [blame] | 508 | return result |
| 509 | |
Andrew Lamb | cd33f70 | 2020-06-11 10:45:16 -0600 | [diff] [blame] | 510 | return None |
David Burger | 8aa8fa3 | 2020-04-14 08:30:34 -0600 | [diff] [blame] | 511 | |
Andrew Lamb | cd33f70 | 2020-06-11 10:45:16 -0600 | [diff] [blame] | 512 | |
| 513 | def _build_identity(hw_scan_config, program, brand_scan_config=None): |
David Burger | 7fd1dbe | 2020-03-26 09:26:55 -0600 | [diff] [blame] | 514 | identity = {} |
David Burger | 8ee9b4d | 2020-06-16 17:40:21 -0600 | [diff] [blame] | 515 | _upsert(hw_scan_config.firmware_sku, identity, 'sku-id') |
| 516 | _upsert(hw_scan_config.smbios_name_match, identity, 'smbios-name-match') |
Andrew Lamb | 7806ce9 | 2020-04-07 10:22:17 -0600 | [diff] [blame] | 517 | # 'platform-name' is needed to support 'mosys platform name'. Clients should |
| 518 | # longer require platform name, but set it here for backwards compatibility. |
David Burger | 8ee9b4d | 2020-06-16 17:40:21 -0600 | [diff] [blame] | 519 | _upsert(program.name, identity, 'platform-name') |
David Burger | 7fd1dbe | 2020-03-26 09:26:55 -0600 | [diff] [blame] | 520 | # ARM architecture |
David Burger | 8ee9b4d | 2020-06-16 17:40:21 -0600 | [diff] [blame] | 521 | _upsert(hw_scan_config.device_tree_compatible_match, identity, |
| 522 | 'device-tree-compatible-match') |
David Burger | 7fd1dbe | 2020-03-26 09:26:55 -0600 | [diff] [blame] | 523 | |
| 524 | if brand_scan_config: |
David Burger | 8ee9b4d | 2020-06-16 17:40:21 -0600 | [diff] [blame] | 525 | _upsert(brand_scan_config.whitelabel_tag, identity, 'whitelabel-tag') |
David Burger | 7fd1dbe | 2020-03-26 09:26:55 -0600 | [diff] [blame] | 526 | |
| 527 | return identity |
| 528 | |
| 529 | |
Andrew Lamb | cd33f70 | 2020-06-11 10:45:16 -0600 | [diff] [blame] | 530 | def _lookup(id_value, id_map): |
| 531 | if not id_value.value: |
| 532 | return None |
| 533 | |
| 534 | key = id_value.value |
| 535 | if key in id_map: |
| 536 | return id_map[id_value.value] |
| 537 | error = 'Failed to lookup %s with value: %s' % ( |
| 538 | id_value.__class__.__name__.replace('Id', ''), key) |
| 539 | print(error) |
| 540 | print('Check the config contents provided:') |
| 541 | printer = pprint.PrettyPrinter(indent=4) |
| 542 | printer.pprint(id_map) |
| 543 | raise Exception(error) |
David Burger | 7fd1dbe | 2020-03-26 09:26:55 -0600 | [diff] [blame] | 544 | |
| 545 | |
Andrew Lamb | cd33f70 | 2020-06-11 10:45:16 -0600 | [diff] [blame] | 546 | def _build_touch_file_config(config, project_name): |
Sean McAllister | eaf10b7 | 2020-08-03 13:41:06 -0600 | [diff] [blame] | 547 | partners = {x.id.value: x for x in config.partner_list} |
C Shapiro | 2b6d533 | 2020-05-06 17:51:35 -0500 | [diff] [blame] | 548 | files = [] |
| 549 | for comp in config.components: |
C Shapiro | 4813be6 | 2020-05-13 17:31:58 -0500 | [diff] [blame] | 550 | touch = comp.touchscreen |
| 551 | # Everything is the same for Touch screen/pad, except different fields |
| 552 | if comp.HasField('touchpad'): |
| 553 | touch = comp.touchpad |
| 554 | if touch.product_id: |
Andrew Lamb | cd33f70 | 2020-06-11 10:45:16 -0600 | [diff] [blame] | 555 | vendor = _lookup(comp.manufacturer_id, partners) |
C Shapiro | 2b6d533 | 2020-05-06 17:51:35 -0500 | [diff] [blame] | 556 | if not vendor: |
Andrew Lamb | 2413c98 | 2020-05-29 12:15:36 -0600 | [diff] [blame] | 557 | raise Exception("Manufacturer must be set for touch device %s" % |
| 558 | comp.id.value) |
C Shapiro | 2b6d533 | 2020-05-06 17:51:35 -0500 | [diff] [blame] | 559 | |
C Shapiro | 4813be6 | 2020-05-13 17:31:58 -0500 | [diff] [blame] | 560 | product_id = touch.product_id |
| 561 | fw_version = touch.fw_version |
C Shapiro | 2b6d533 | 2020-05-06 17:51:35 -0500 | [diff] [blame] | 562 | |
C Shapiro | 2b6d533 | 2020-05-06 17:51:35 -0500 | [diff] [blame] | 563 | file_name = "%s_%s.bin" % (product_id, fw_version) |
| 564 | fw_file_path = os.path.join(TOUCH_PATH, vendor.name, file_name) |
| 565 | |
| 566 | if not os.path.exists(fw_file_path): |
Andrew Lamb | 2413c98 | 2020-05-29 12:15:36 -0600 | [diff] [blame] | 567 | raise Exception("Touchscreen fw bin file doesn't exist at: %s" % |
| 568 | fw_file_path) |
C Shapiro | 2b6d533 | 2020-05-06 17:51:35 -0500 | [diff] [blame] | 569 | |
C Shapiro | 303cece | 2020-07-22 07:15:21 -0500 | [diff] [blame] | 570 | touch_vendor = vendor.touch_vendor |
| 571 | sym_link = touch_vendor.symlink_file_format.format( |
| 572 | vendor_name=vendor.name, |
| 573 | vendor_id=touch_vendor.vendor_id, |
| 574 | product_id=product_id, |
| 575 | fw_version=fw_version, |
| 576 | product_series=touch.product_series) |
| 577 | |
| 578 | dest = "%s_%s" % (vendor.name, file_name) |
| 579 | if touch_vendor.destination_file_format: |
| 580 | dest = touch_vendor.destination_file_format.format( |
| 581 | vendor_name=vendor.name, |
| 582 | vendor_id=touch_vendor.vendor_id, |
| 583 | product_id=product_id, |
| 584 | fw_version=fw_version, |
| 585 | product_series=touch.product_series) |
| 586 | |
C Shapiro | 2b6d533 | 2020-05-06 17:51:35 -0500 | [diff] [blame] | 587 | files.append({ |
C Shapiro | 303cece | 2020-07-22 07:15:21 -0500 | [diff] [blame] | 588 | "destination": os.path.join("/opt/google/touch/firmware", dest), |
YH Lin | 9160fc5 | 2020-07-22 16:35:28 -0700 | [diff] [blame] | 589 | "source": os.path.join(project_name, fw_file_path), |
| 590 | "symlink": os.path.join("/lib/firmware", sym_link), |
C Shapiro | 2b6d533 | 2020-05-06 17:51:35 -0500 | [diff] [blame] | 591 | }) |
| 592 | |
| 593 | result = {} |
David Burger | 8ee9b4d | 2020-06-16 17:40:21 -0600 | [diff] [blame] | 594 | _upsert(files, result, 'files') |
C Shapiro | 2b6d533 | 2020-05-06 17:51:35 -0500 | [diff] [blame] | 595 | return result |
| 596 | |
| 597 | |
David Burger | ceeb68a | 2020-09-03 11:31:10 -0600 | [diff] [blame] | 598 | def _sw_config(sw_configs, design_config_id): |
| 599 | """Returns the correct software config for `design_config_id`. |
| 600 | |
| 601 | Returns the correct software config match for `design_config_id`. If no such |
| 602 | config or multiple such configs are found an exception is raised. |
| 603 | """ |
| 604 | sw_config_matches = [ |
| 605 | x for x in sw_configs if x.design_config_id.value == design_config_id |
| 606 | ] |
| 607 | if len(sw_config_matches) == 1: |
| 608 | return sw_config_matches[0] |
| 609 | if len(sw_config_matches) > 1: |
| 610 | raise ValueError('Multiple software configs found for: %s' % |
| 611 | design_config_id) |
| 612 | raise ValueError('Software config is required for: %s' % design_config_id) |
| 613 | |
| 614 | |
Sam McNally | fd14e4b | 2020-09-12 10:26:35 +1000 | [diff] [blame] | 615 | def _is_whitelabel(brand_configs, device_brands): |
| 616 | for device_brand in device_brands: |
| 617 | if device_brand.id.value in brand_configs: |
| 618 | brand_scan_config = brand_configs[device_brand.id.value].scan_config |
| 619 | if brand_scan_config and brand_scan_config.whitelabel_tag: |
| 620 | return True |
| 621 | return False |
| 622 | |
| 623 | |
David Burger | ceeb68a | 2020-09-03 11:31:10 -0600 | [diff] [blame] | 624 | def _transform_build_configs(config, |
| 625 | config_files=ConfigFiles({}, {}, {}, {}, {})): |
Andrew Lamb | cd33f70 | 2020-06-11 10:45:16 -0600 | [diff] [blame] | 626 | # pylint: disable=too-many-locals,too-many-branches |
Sean McAllister | eaf10b7 | 2020-08-03 13:41:06 -0600 | [diff] [blame] | 627 | partners = {x.id.value: x for x in config.partner_list} |
Sean McAllister | f38d1e9 | 2020-08-03 13:57:53 -0600 | [diff] [blame] | 628 | programs = {x.id.value: x for x in config.program_list} |
David Burger | 7fd1dbe | 2020-03-26 09:26:55 -0600 | [diff] [blame] | 629 | sw_configs = list(config.software_configs) |
Andrew Lamb | cd33f70 | 2020-06-11 10:45:16 -0600 | [diff] [blame] | 630 | 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] | 631 | |
C Shapiro | a0b766c | 2020-03-31 08:35:28 -0500 | [diff] [blame] | 632 | if len(config.build_targets) != 1: |
| 633 | # Artifact of sharing the config_bundle for analysis and transforms. |
| 634 | # Integrated analysis of multiple programs/projects it the only time |
| 635 | # having multiple build targets would be valid. |
| 636 | raise Exception('Single build_target required for transform') |
| 637 | |
David Burger | 7fd1dbe | 2020-03-26 09:26:55 -0600 | [diff] [blame] | 638 | results = {} |
Sean McAllister | f66887b | 2020-08-03 14:00:51 -0600 | [diff] [blame] | 639 | for hw_design in config.design_list: |
Sean McAllister | 6cbb0ec | 2020-08-03 14:03:37 -0600 | [diff] [blame] | 640 | if config.device_brand_list: |
Andrew Lamb | 2413c98 | 2020-05-29 12:15:36 -0600 | [diff] [blame] | 641 | device_brands = [ |
Sean McAllister | 6cbb0ec | 2020-08-03 14:03:37 -0600 | [diff] [blame] | 642 | x for x in config.device_brand_list |
Andrew Lamb | 2413c98 | 2020-05-29 12:15:36 -0600 | [diff] [blame] | 643 | if x.design_id.value == hw_design.id.value |
| 644 | ] |
David Burger | 7fd1dbe | 2020-03-26 09:26:55 -0600 | [diff] [blame] | 645 | else: |
| 646 | device_brands = [device_brand_pb2.DeviceBrand()] |
| 647 | |
Sam McNally | fd14e4b | 2020-09-12 10:26:35 +1000 | [diff] [blame] | 648 | whitelabel = _is_whitelabel(brand_configs, device_brands) |
| 649 | |
David Burger | 7fd1dbe | 2020-03-26 09:26:55 -0600 | [diff] [blame] | 650 | for device_brand in device_brands: |
| 651 | # Brand config can be empty since platform JSON config allows it |
| 652 | brand_config = brand_config_pb2.BrandConfig() |
| 653 | if device_brand.id.value in brand_configs: |
| 654 | brand_config = brand_configs[device_brand.id.value] |
| 655 | |
| 656 | for hw_design_config in hw_design.configs: |
David Burger | ceeb68a | 2020-09-03 11:31:10 -0600 | [diff] [blame] | 657 | sw_config = _sw_config(sw_configs, hw_design_config.id.value) |
Andrew Lamb | cd33f70 | 2020-06-11 10:45:16 -0600 | [diff] [blame] | 658 | program = _lookup(hw_design.program_id, programs) |
C Shapiro | adefd7c | 2020-05-19 16:37:21 -0500 | [diff] [blame] | 659 | signer_configs_by_design = {} |
| 660 | signer_configs_by_brand = {} |
| 661 | for signer_config in program.device_signer_configs: |
| 662 | design_id = signer_config.design_id.value |
| 663 | brand_id = signer_config.brand_id.value |
| 664 | if design_id: |
| 665 | signer_configs_by_design[design_id] = signer_config |
| 666 | elif brand_id: |
| 667 | signer_configs_by_brand[brand_id] = signer_config |
| 668 | else: |
| 669 | raise Exception('No ID found for signer config: %s' % signer_config) |
| 670 | |
C Shapiro | 2f0bb5d | 2020-04-14 10:07:47 -0500 | [diff] [blame] | 671 | device_signer_config = None |
C Shapiro | adefd7c | 2020-05-19 16:37:21 -0500 | [diff] [blame] | 672 | if signer_configs_by_design or signer_configs_by_brand: |
| 673 | design_id = hw_design.id.value |
| 674 | brand_id = device_brand.id.value |
| 675 | if design_id in signer_configs_by_design: |
| 676 | device_signer_config = signer_configs_by_design[design_id] |
| 677 | elif brand_id in signer_configs_by_brand: |
| 678 | device_signer_config = signer_configs_by_brand[brand_id] |
| 679 | else: |
| 680 | # Assume that if signer configs are set, every config is setup |
Andrew Lamb | 2413c98 | 2020-05-29 12:15:36 -0600 | [diff] [blame] | 681 | raise Exception('Signer config missing for design: %s, brand: %s' % |
| 682 | (design_id, brand_id)) |
C Shapiro | 2f0bb5d | 2020-04-14 10:07:47 -0500 | [diff] [blame] | 683 | |
Andrew Lamb | cd33f70 | 2020-06-11 10:45:16 -0600 | [diff] [blame] | 684 | transformed_config = _transform_build_config( |
C Shapiro | 90fda25 | 2020-04-17 14:34:57 -0500 | [diff] [blame] | 685 | Config( |
| 686 | program=program, |
| 687 | hw_design=hw_design, |
Andrew Lamb | cd33f70 | 2020-06-11 10:45:16 -0600 | [diff] [blame] | 688 | odm=_lookup(hw_design.odm_id, partners), |
C Shapiro | 90fda25 | 2020-04-17 14:34:57 -0500 | [diff] [blame] | 689 | hw_design_config=hw_design_config, |
| 690 | device_brand=device_brand, |
| 691 | device_signer_config=device_signer_config, |
Andrew Lamb | cd33f70 | 2020-06-11 10:45:16 -0600 | [diff] [blame] | 692 | oem=_lookup(device_brand.oem_id, partners), |
C Shapiro | 90fda25 | 2020-04-17 14:34:57 -0500 | [diff] [blame] | 693 | sw_config=sw_config, |
| 694 | brand_config=brand_config, |
Sam McNally | fd14e4b | 2020-09-12 10:26:35 +1000 | [diff] [blame] | 695 | build_target=config.build_targets[0]), config_files, whitelabel) |
David Burger | 7fd1dbe | 2020-03-26 09:26:55 -0600 | [diff] [blame] | 696 | |
Andrew Lamb | 2413c98 | 2020-05-29 12:15:36 -0600 | [diff] [blame] | 697 | config_json = json.dumps( |
| 698 | transformed_config, |
| 699 | sort_keys=True, |
| 700 | indent=2, |
| 701 | separators=(',', ': ')) |
David Burger | 7fd1dbe | 2020-03-26 09:26:55 -0600 | [diff] [blame] | 702 | |
| 703 | if config_json not in results: |
| 704 | results[config_json] = transformed_config |
| 705 | |
| 706 | return list(results.values()) |
| 707 | |
| 708 | |
Sam McNally | fd14e4b | 2020-09-12 10:26:35 +1000 | [diff] [blame] | 709 | def _transform_build_config(config, config_files, whitelabel): |
David Burger | 7fd1dbe | 2020-03-26 09:26:55 -0600 | [diff] [blame] | 710 | """Transforms Config instance into target platform JSON schema. |
| 711 | |
| 712 | Args: |
| 713 | config: Config namedtuple |
C Shapiro | 5bf23a7 | 2020-04-24 11:40:17 -0500 | [diff] [blame] | 714 | config_files: Map to look up the generated config files. |
Sam McNally | fd14e4b | 2020-09-12 10:26:35 +1000 | [diff] [blame] | 715 | whitelabel: Whether the config is for a whitelabel design |
David Burger | 7fd1dbe | 2020-03-26 09:26:55 -0600 | [diff] [blame] | 716 | |
| 717 | Returns: |
| 718 | Unique config payload based on the platform JSON schema. |
| 719 | """ |
| 720 | result = { |
Andrew Lamb | 2413c98 | 2020-05-29 12:15:36 -0600 | [diff] [blame] | 721 | 'identity': |
Andrew Lamb | cd33f70 | 2020-06-11 10:45:16 -0600 | [diff] [blame] | 722 | _build_identity(config.sw_config.id_scan_config, config.program, |
| 723 | config.brand_config.scan_config), |
Andrew Lamb | 2413c98 | 2020-05-29 12:15:36 -0600 | [diff] [blame] | 724 | 'name': |
| 725 | config.hw_design.name.lower(), |
David Burger | 7fd1dbe | 2020-03-26 09:26:55 -0600 | [diff] [blame] | 726 | } |
| 727 | |
David Burger | 8ee9b4d | 2020-06-16 17:40:21 -0600 | [diff] [blame] | 728 | _upsert(_build_arc(config, config_files), result, 'arc') |
| 729 | _upsert(_build_audio(config), result, 'audio') |
David Burger | 07af524 | 2020-08-11 11:08:25 -0600 | [diff] [blame] | 730 | _upsert(_build_bluetooth(config), result, 'bluetooth') |
David Burger | ceeb68a | 2020-09-03 11:31:10 -0600 | [diff] [blame] | 731 | _upsert(_build_wifi(config, config_files), result, 'wifi') |
Andrew Lamb | ca27990 | 2020-08-06 10:13:42 -0600 | [diff] [blame] | 732 | _upsert(config.brand_config.wallpaper, result, 'wallpaper') |
David Burger | 8ee9b4d | 2020-06-16 17:40:21 -0600 | [diff] [blame] | 733 | _upsert(config.device_brand.brand_code, result, 'brand-code') |
| 734 | _upsert( |
Andrew Lamb | cd33f70 | 2020-06-11 10:45:16 -0600 | [diff] [blame] | 735 | _build_camera(config.hw_design_config.hardware_topology), result, |
| 736 | 'camera') |
David Burger | 8ee9b4d | 2020-06-16 17:40:21 -0600 | [diff] [blame] | 737 | _upsert(_build_firmware(config), result, 'firmware') |
Sam McNally | fd14e4b | 2020-09-12 10:26:35 +1000 | [diff] [blame] | 738 | _upsert(_build_fw_signing(config, whitelabel), result, 'firmware-signing') |
David Burger | 8ee9b4d | 2020-06-16 17:40:21 -0600 | [diff] [blame] | 739 | _upsert( |
Andrew Lamb | cd33f70 | 2020-06-11 10:45:16 -0600 | [diff] [blame] | 740 | _build_fingerprint(config.hw_design_config.hardware_topology), result, |
Andrew Lamb | 2413c98 | 2020-05-29 12:15:36 -0600 | [diff] [blame] | 741 | 'fingerprint') |
Andrew Lamb | 0d236ab | 2020-06-30 12:30:20 -0600 | [diff] [blame] | 742 | _upsert(_build_ui(config), result, 'ui') |
David Burger | 7fd1dbe | 2020-03-26 09:26:55 -0600 | [diff] [blame] | 743 | power_prefs = config.sw_config.power_config.preferences |
| 744 | power_prefs_map = dict( |
Andrew Lamb | 2413c98 | 2020-05-29 12:15:36 -0600 | [diff] [blame] | 745 | (x.replace('_', '-'), power_prefs[x]) for x in power_prefs) |
David Burger | 8ee9b4d | 2020-06-16 17:40:21 -0600 | [diff] [blame] | 746 | _upsert(power_prefs_map, result, 'power') |
| 747 | if config_files.camera_map: |
| 748 | camera_file = config_files.camera_map.get(config.hw_design.name, {}) |
| 749 | _upsert(camera_file, result, 'camera') |
David Burger | 52c9d32 | 2020-06-09 07:16:18 -0600 | [diff] [blame] | 750 | if config_files.dptf_map: |
| 751 | # Prefer design specific if found, if not fall back to project wide config |
| 752 | # mapped under the empty string. |
| 753 | if config_files.dptf_map.get(config.hw_design.name): |
| 754 | dptf_file = config_files.dptf_map[config.hw_design.name] |
| 755 | else: |
| 756 | dptf_file = config_files.dptf_map.get('') |
David Burger | 8ee9b4d | 2020-06-16 17:40:21 -0600 | [diff] [blame] | 757 | _upsert(dptf_file, result, 'thermal') |
| 758 | _upsert(config_files.touch_fw, result, 'touch') |
Trent Begin | f067ccb | 2020-08-12 12:33:53 -0600 | [diff] [blame] | 759 | _upsert( |
| 760 | _build_hardware_properties(config.hw_design_config.hardware_topology), |
| 761 | result, 'hardware-properties') |
David Burger | 7fd1dbe | 2020-03-26 09:26:55 -0600 | [diff] [blame] | 762 | |
| 763 | return result |
| 764 | |
| 765 | |
Andrew Lamb | cd33f70 | 2020-06-11 10:45:16 -0600 | [diff] [blame] | 766 | def write_output(configs, output=None): |
David Burger | 7fd1dbe | 2020-03-26 09:26:55 -0600 | [diff] [blame] | 767 | """Writes a list of configs to platform JSON format. |
| 768 | |
| 769 | Args: |
| 770 | configs: List of config dicts defined in cros_config_schema.yaml |
| 771 | output: Target file output (if None, prints to stdout) |
| 772 | """ |
Andrew Lamb | 2413c98 | 2020-05-29 12:15:36 -0600 | [diff] [blame] | 773 | json_output = json.dumps({'chromeos': { |
| 774 | 'configs': configs, |
| 775 | }}, |
| 776 | sort_keys=True, |
| 777 | indent=2, |
| 778 | separators=(',', ': ')) |
David Burger | 7fd1dbe | 2020-03-26 09:26:55 -0600 | [diff] [blame] | 779 | if output: |
| 780 | with open(output, 'w') as output_stream: |
| 781 | # Using print function adds proper trailing newline. |
| 782 | print(json_output, file=output_stream) |
| 783 | else: |
| 784 | print(json_output) |
| 785 | |
| 786 | |
Andrew Lamb | cd33f70 | 2020-06-11 10:45:16 -0600 | [diff] [blame] | 787 | def _feature(name, present): |
C Shapiro | 5bf23a7 | 2020-04-24 11:40:17 -0500 | [diff] [blame] | 788 | attrib = {'name': name} |
| 789 | if present: |
| 790 | return etree.Element('feature', attrib=attrib) |
Andrew Lamb | cd33f70 | 2020-06-11 10:45:16 -0600 | [diff] [blame] | 791 | |
| 792 | return etree.Element('unavailable-feature', attrib=attrib) |
C Shapiro | 5bf23a7 | 2020-04-24 11:40:17 -0500 | [diff] [blame] | 793 | |
| 794 | |
Andrew Lamb | cd33f70 | 2020-06-11 10:45:16 -0600 | [diff] [blame] | 795 | def _any_present(features): |
Andrew Lamb | 2413c98 | 2020-05-29 12:15:36 -0600 | [diff] [blame] | 796 | return topology_pb2.HardwareFeatures.PRESENT in features |
C Shapiro | 5bf23a7 | 2020-04-24 11:40:17 -0500 | [diff] [blame] | 797 | |
| 798 | |
Andrew Lamb | cd33f70 | 2020-06-11 10:45:16 -0600 | [diff] [blame] | 799 | def _arc_hardware_feature_id(design_config): |
C Shapiro | 5bf23a7 | 2020-04-24 11:40:17 -0500 | [diff] [blame] | 800 | return design_config.id.value.lower().replace(':', '_') |
| 801 | |
| 802 | |
Andrew Lamb | cd33f70 | 2020-06-11 10:45:16 -0600 | [diff] [blame] | 803 | def _write_arc_hardware_feature_file(output_dir, file_name, config_content): |
David Burger | 77a1d31 | 2020-05-23 16:05:45 -0600 | [diff] [blame] | 804 | output_dir += '/arc' |
| 805 | os.makedirs(output_dir, exist_ok=True) |
| 806 | output = '%s/%s' % (output_dir, file_name) |
Andrew Lamb | 2413c98 | 2020-05-29 12:15:36 -0600 | [diff] [blame] | 807 | file_content = minidom.parseString(config_content).toprettyxml( |
| 808 | indent=' ', encoding='utf-8') |
C Shapiro | ea33cff | 2020-05-11 13:32:05 -0500 | [diff] [blame] | 809 | |
| 810 | with open(output, 'wb') as f: |
| 811 | f.write(file_content) |
| 812 | |
| 813 | |
Ren-Pei Zeng | f22b538 | 2020-09-02 13:31:22 +0800 | [diff] [blame] | 814 | def _get_arc_camera_features(camera): |
| 815 | """Gets camera related features for ARC hardware_features.xml from camera |
| 816 | topology. Check |
| 817 | https://developer.android.com/reference/android/content/pm/PackageManager#FEATURE_CAMERA |
| 818 | and CTS android.app.cts.SystemFeaturesTest#testCameraFeatures for the correct |
| 819 | settings. |
| 820 | |
| 821 | Args: |
| 822 | camera: A HardwareFeatures.Camera proto message. |
| 823 | Returns: |
| 824 | list of camera related ARC features as XML elements. |
| 825 | """ |
| 826 | camera_pb = topology_pb2.HardwareFeatures.Camera |
| 827 | |
Ren-Pei Zeng | 9b5682d | 2020-10-14 17:37:30 +0800 | [diff] [blame^] | 828 | count = len(camera.devices) |
| 829 | has_front_camera = any( |
| 830 | (d.facing == camera_pb.FACING_FRONT for d in camera.devices)) |
| 831 | has_back_camera = any( |
| 832 | (d.facing == camera_pb.FACING_BACK for d in camera.devices)) |
| 833 | has_autofocus_back_camera = any((d.facing == camera_pb.FACING_BACK and |
| 834 | d.flags & camera_pb.FLAGS_SUPPORT_AUTOFOCUS |
| 835 | for d in camera.devices)) |
| 836 | # Assumes MIPI cameras support FULL-level. |
| 837 | # TODO(kamesan): Setting this in project configs when there's an exception. |
| 838 | has_level_full_camera = any( |
| 839 | (d.interface == camera_pb.INTERFACE_MIPI for d in camera.devices)) |
Ren-Pei Zeng | f22b538 | 2020-09-02 13:31:22 +0800 | [diff] [blame] | 840 | |
Ren-Pei Zeng | f22b538 | 2020-09-02 13:31:22 +0800 | [diff] [blame] | 841 | return [ |
| 842 | _feature('android.hardware.camera', has_back_camera), |
| 843 | _feature('android.hardware.camera.any', count > 0), |
| 844 | _feature('android.hardware.camera.autofocus', has_autofocus_back_camera), |
| 845 | _feature('android.hardware.camera.capability.manual_post_processing', |
| 846 | has_level_full_camera), |
| 847 | _feature('android.hardware.camera.capability.manual_sensor', |
| 848 | has_level_full_camera), |
| 849 | _feature('android.hardware.camera.front', has_front_camera), |
| 850 | _feature('android.hardware.camera.level.full', has_level_full_camera), |
| 851 | ] |
| 852 | |
| 853 | |
Andrew Lamb | cd33f70 | 2020-06-11 10:45:16 -0600 | [diff] [blame] | 854 | def _write_arc_hardware_feature_files(config, output_dir, build_root_dir): |
C Shapiro | 5bf23a7 | 2020-04-24 11:40:17 -0500 | [diff] [blame] | 855 | """Writes ARC hardware_feature.xml files for each config |
| 856 | |
| 857 | Args: |
| 858 | config: Source ConfigBundle to process. |
| 859 | output_dir: Path to the generated output. |
C Shapiro | 5c87799 | 2020-04-29 12:11:28 -0500 | [diff] [blame] | 860 | build_root_path: Path to the config file from portage's perspective. |
C Shapiro | 5bf23a7 | 2020-04-24 11:40:17 -0500 | [diff] [blame] | 861 | Returns: |
| 862 | dict that maps the design_config_id onto the correct file. |
| 863 | """ |
Andrew Lamb | cd33f70 | 2020-06-11 10:45:16 -0600 | [diff] [blame] | 864 | # pylint: disable=too-many-locals |
C Shapiro | 5bf23a7 | 2020-04-24 11:40:17 -0500 | [diff] [blame] | 865 | result = {} |
C Shapiro | ea33cff | 2020-05-11 13:32:05 -0500 | [diff] [blame] | 866 | configs_by_design = {} |
Sean McAllister | f66887b | 2020-08-03 14:00:51 -0600 | [diff] [blame] | 867 | for hw_design in config.design_list: |
C Shapiro | 5bf23a7 | 2020-04-24 11:40:17 -0500 | [diff] [blame] | 868 | for design_config in hw_design.configs: |
| 869 | hw_features = design_config.hardware_features |
Andrew Lamb | cd33f70 | 2020-06-11 10:45:16 -0600 | [diff] [blame] | 870 | touchscreen = _any_present([hw_features.screen.touch_support]) |
C Shapiro | 5bf23a7 | 2020-04-24 11:40:17 -0500 | [diff] [blame] | 871 | acc = hw_features.accelerometer |
| 872 | gyro = hw_features.gyroscope |
| 873 | compass = hw_features.magnetometer |
Andrew Lamb | cd33f70 | 2020-06-11 10:45:16 -0600 | [diff] [blame] | 874 | light_sensor = hw_features.light_sensor |
C Shapiro | 5bf23a7 | 2020-04-24 11:40:17 -0500 | [diff] [blame] | 875 | root = etree.Element('permissions') |
Ren-Pei Zeng | f22b538 | 2020-09-02 13:31:22 +0800 | [diff] [blame] | 876 | root.extend( |
| 877 | _get_arc_camera_features(hw_features.camera) + [ |
| 878 | _feature( |
| 879 | 'android.hardware.sensor.accelerometer', |
| 880 | _any_present([acc.lid_accelerometer, acc.base_accelerometer |
| 881 | ])), |
| 882 | _feature('android.hardware.sensor.gyroscope', |
| 883 | _any_present([gyro.lid_gyroscope, gyro.base_gyroscope])), |
| 884 | _feature( |
| 885 | 'android.hardware.sensor.compass', |
| 886 | _any_present( |
| 887 | [compass.lid_magnetometer, compass.base_magnetometer])), |
| 888 | _feature( |
| 889 | 'android.hardware.sensor.light', |
| 890 | _any_present([ |
| 891 | light_sensor.lid_lightsensor, |
| 892 | light_sensor.base_lightsensor |
| 893 | ])), |
| 894 | _feature('android.hardware.touchscreen', touchscreen), |
| 895 | _feature('android.hardware.touchscreen.multitouch', touchscreen), |
| 896 | _feature('android.hardware.touchscreen.multitouch.distinct', |
| 897 | touchscreen), |
| 898 | _feature('android.hardware.touchscreen.multitouch.jazzhand', |
| 899 | touchscreen), |
| 900 | ]) |
C Shapiro | 5bf23a7 | 2020-04-24 11:40:17 -0500 | [diff] [blame] | 901 | |
C Shapiro | ea33cff | 2020-05-11 13:32:05 -0500 | [diff] [blame] | 902 | design_name = hw_design.name.lower() |
C Shapiro | 5bf23a7 | 2020-04-24 11:40:17 -0500 | [diff] [blame] | 903 | |
C Shapiro | ea33cff | 2020-05-11 13:32:05 -0500 | [diff] [blame] | 904 | # Constructs the following map: |
| 905 | # design_name -> config -> design_configs |
| 906 | # This allows any of the following file naming schemes: |
| 907 | # - All configs within a design share config (design_name prefix only) |
| 908 | # - Nobody shares (full design_name and config id prefix needed) |
| 909 | # |
| 910 | # Having shared configs when possible makes code reviews easier around |
| 911 | # the configs and makes debugging easier on the platform side. |
| 912 | config_content = etree.tostring(root) |
| 913 | arc_configs = configs_by_design.get(design_name, {}) |
| 914 | design_configs = arc_configs.get(config_content, []) |
| 915 | design_configs.append(design_config) |
| 916 | arc_configs[config_content] = design_configs |
| 917 | configs_by_design[design_name] = arc_configs |
C Shapiro | 9a3ac8c | 2020-04-25 07:49:21 -0500 | [diff] [blame] | 918 | |
C Shapiro | ea33cff | 2020-05-11 13:32:05 -0500 | [diff] [blame] | 919 | for design_name, unique_configs in configs_by_design.items(): |
| 920 | for file_content, design_configs in unique_configs.items(): |
Andrew Lamb | 2413c98 | 2020-05-29 12:15:36 -0600 | [diff] [blame] | 921 | file_name = 'hardware_features_%s.xml' % design_name |
| 922 | if len(unique_configs) == 1: |
Andrew Lamb | cd33f70 | 2020-06-11 10:45:16 -0600 | [diff] [blame] | 923 | _write_arc_hardware_feature_file(output_dir, file_name, file_content) |
C Shapiro | 9a3ac8c | 2020-04-25 07:49:21 -0500 | [diff] [blame] | 924 | |
Andrew Lamb | 2413c98 | 2020-05-29 12:15:36 -0600 | [diff] [blame] | 925 | for design_config in design_configs: |
Andrew Lamb | cd33f70 | 2020-06-11 10:45:16 -0600 | [diff] [blame] | 926 | feature_id = _arc_hardware_feature_id(design_config) |
Andrew Lamb | 2413c98 | 2020-05-29 12:15:36 -0600 | [diff] [blame] | 927 | if len(unique_configs) > 1: |
| 928 | file_name = 'hardware_features_%s.xml' % feature_id |
Andrew Lamb | cd33f70 | 2020-06-11 10:45:16 -0600 | [diff] [blame] | 929 | _write_arc_hardware_feature_file(output_dir, file_name, file_content) |
David Burger | 40dfe3a | 2020-06-18 17:09:13 -0600 | [diff] [blame] | 930 | result[feature_id] = _file_v2('%s/arc/%s' % (build_root_dir, file_name), |
| 931 | '/etc/%s' % file_name) |
C Shapiro | 5bf23a7 | 2020-04-24 11:40:17 -0500 | [diff] [blame] | 932 | return result |
| 933 | |
| 934 | |
Andrew Lamb | cd33f70 | 2020-06-11 10:45:16 -0600 | [diff] [blame] | 935 | def _read_config(path): |
David Burger | d4f3296 | 2020-05-02 12:07:40 -0600 | [diff] [blame] | 936 | """Reads a ConfigBundle proto from a json pb file. |
David Burger | e6f7622 | 2020-04-27 11:08:01 -0600 | [diff] [blame] | 937 | |
| 938 | Args: |
David Burger | d4f3296 | 2020-05-02 12:07:40 -0600 | [diff] [blame] | 939 | path: Path to the file encoding the json pb proto. |
David Burger | e6f7622 | 2020-04-27 11:08:01 -0600 | [diff] [blame] | 940 | """ |
| 941 | config = config_bundle_pb2.ConfigBundle() |
| 942 | with open(path, 'r') as f: |
| 943 | return json_format.Parse(f.read(), config) |
| 944 | |
| 945 | |
Andrew Lamb | cd33f70 | 2020-06-11 10:45:16 -0600 | [diff] [blame] | 946 | def _merge_configs(configs): |
David Burger | 7fd1dbe | 2020-03-26 09:26:55 -0600 | [diff] [blame] | 947 | result = config_bundle_pb2.ConfigBundle() |
| 948 | for config in configs: |
| 949 | result.MergeFrom(config) |
| 950 | |
| 951 | return result |
| 952 | |
| 953 | |
David Burger | 1ba78a2 | 2020-06-18 18:42:47 -0600 | [diff] [blame] | 954 | def _camera_map(configs, project_name): |
David Burger | 8ee9b4d | 2020-06-16 17:40:21 -0600 | [diff] [blame] | 955 | """Produces a camera config map for the given configs. |
| 956 | |
| 957 | Produces a map that maps from the design name to the camera config for that |
| 958 | design. |
| 959 | |
| 960 | Args: |
| 961 | configs: Source ConfigBundle to process. |
David Burger | 1ba78a2 | 2020-06-18 18:42:47 -0600 | [diff] [blame] | 962 | project_name: Name of project processing for. |
David Burger | 8ee9b4d | 2020-06-16 17:40:21 -0600 | [diff] [blame] | 963 | |
| 964 | Returns: |
| 965 | map from design name to camera config. |
| 966 | """ |
| 967 | result = {} |
Sean McAllister | f66887b | 2020-08-03 14:00:51 -0600 | [diff] [blame] | 968 | for design in configs.design_list: |
David Burger | 8ee9b4d | 2020-06-16 17:40:21 -0600 | [diff] [blame] | 969 | design_name = design.name |
David Burger | 0d9e846 | 2020-06-19 14:12:37 -0600 | [diff] [blame] | 970 | config_path = CAMERA_CONFIG_SOURCE_PATH_TEMPLATE.format(design_name.lower()) |
David Burger | 8ee9b4d | 2020-06-16 17:40:21 -0600 | [diff] [blame] | 971 | if os.path.exists(config_path): |
David Burger | 0d9e846 | 2020-06-19 14:12:37 -0600 | [diff] [blame] | 972 | destination = CAMERA_CONFIG_DEST_PATH_TEMPLATE.format(design_name.lower()) |
David Burger | 8ee9b4d | 2020-06-16 17:40:21 -0600 | [diff] [blame] | 973 | result[design_name] = { |
David Burger | 1ba78a2 | 2020-06-18 18:42:47 -0600 | [diff] [blame] | 974 | 'config-file': |
| 975 | _file_v2(os.path.join(project_name, config_path), destination), |
David Burger | 8ee9b4d | 2020-06-16 17:40:21 -0600 | [diff] [blame] | 976 | } |
| 977 | return result |
| 978 | |
| 979 | |
David Burger | 52c9d32 | 2020-06-09 07:16:18 -0600 | [diff] [blame] | 980 | def _dptf_map(configs, project_name): |
| 981 | """Produces a dptf map for the given configs. |
| 982 | |
| 983 | Produces a map that maps from design name to the dptf file config for that |
| 984 | design. It looks for the dptf files at: |
David Burger | 2f0d952 | 2020-07-30 10:52:28 -0600 | [diff] [blame] | 985 | DPTF_PATH + '/' + DPTF_FILE |
David Burger | 52c9d32 | 2020-06-09 07:16:18 -0600 | [diff] [blame] | 986 | for a project wide config, that it maps under the empty string, and at: |
David Burger | 2f0d952 | 2020-07-30 10:52:28 -0600 | [diff] [blame] | 987 | DPTF_PATH + '/' + design_name + '/' + DPTF_FILE |
David Burger | 52c9d32 | 2020-06-09 07:16:18 -0600 | [diff] [blame] | 988 | for design specific configs that it maps under the design name. |
| 989 | |
| 990 | Args: |
| 991 | configs: Source ConfigBundle to process. |
| 992 | project_name: Name of project processing for. |
| 993 | |
| 994 | Returns: |
David Burger | 8ee9b4d | 2020-06-16 17:40:21 -0600 | [diff] [blame] | 995 | map from design name or empty string (project wide), to dptf config. |
David Burger | 52c9d32 | 2020-06-09 07:16:18 -0600 | [diff] [blame] | 996 | """ |
| 997 | result = {} |
David Burger | 52c9d32 | 2020-06-09 07:16:18 -0600 | [diff] [blame] | 998 | # Looking at top level for project wide, and then for each design name |
| 999 | # for design specific. |
Sean McAllister | f66887b | 2020-08-03 14:00:51 -0600 | [diff] [blame] | 1000 | dirs = [""] + [d.name for d in configs.design_list] |
David Burger | 52c9d32 | 2020-06-09 07:16:18 -0600 | [diff] [blame] | 1001 | for directory in dirs: |
David Burger | a225276 | 2020-07-09 15:09:49 -0600 | [diff] [blame] | 1002 | design = directory.lower() |
| 1003 | if os.path.exists(os.path.join(DPTF_PATH, design, DPTF_FILE)): |
David Burger | 2f0d952 | 2020-07-30 10:52:28 -0600 | [diff] [blame] | 1004 | project_dptf_path = os.path.join(project_name, design, DPTF_FILE) |
David Burger | 52c9d32 | 2020-06-09 07:16:18 -0600 | [diff] [blame] | 1005 | dptf_file = { |
| 1006 | 'dptf-dv': |
| 1007 | project_dptf_path, |
| 1008 | 'files': [ |
| 1009 | _file( |
David Burger | a225276 | 2020-07-09 15:09:49 -0600 | [diff] [blame] | 1010 | os.path.join(project_name, DPTF_PATH, design, DPTF_FILE), |
David Burger | 52c9d32 | 2020-06-09 07:16:18 -0600 | [diff] [blame] | 1011 | os.path.join('/etc/dptf', project_dptf_path)) |
| 1012 | ] |
| 1013 | } |
| 1014 | result[directory] = dptf_file |
| 1015 | return result |
| 1016 | |
| 1017 | |
David Burger | ceeb68a | 2020-09-03 11:31:10 -0600 | [diff] [blame] | 1018 | def _wifi_sar_map(configs, project_name, output_dir, build_root_dir): |
| 1019 | """Constructs a map from design name to wifi sar config for that design. |
| 1020 | |
| 1021 | Constructs a map from design name to the wifi sar config for that design. |
| 1022 | In the process a wifi sar hex file is generated that the config points at. |
| 1023 | This mapping is only made for the intel wifi where the generated file is |
| 1024 | provided when building coreboot. |
| 1025 | |
| 1026 | Args: |
| 1027 | configs: Source ConfigBundle to process. |
| 1028 | project_name: Name of project processing for. |
| 1029 | output_dir: Path to the generated output. |
| 1030 | build_root_path: Path to the config file from portage's perspective. |
| 1031 | |
| 1032 | Returns: |
| 1033 | dict that maps the design name onto the wifi config for that design. |
| 1034 | """ |
| 1035 | # pylint: disable=too-many-locals |
| 1036 | result = {} |
| 1037 | programs = {p.id.value: p for p in configs.program_list} |
| 1038 | sw_configs = list(configs.software_configs) |
| 1039 | for hw_design in configs.design_list: |
| 1040 | for hw_design_config in hw_design.configs: |
| 1041 | sw_config = _sw_config(sw_configs, hw_design_config.id.value) |
| 1042 | if sw_config.wifi_config.HasField('intel_config'): |
| 1043 | sar_file_content = _create_intel_sar_file_content( |
| 1044 | sw_config.wifi_config.intel_config) |
| 1045 | design_name = hw_design.name.lower() |
| 1046 | program = _lookup(hw_design.program_id, programs) |
| 1047 | wifi_sar_id = _extract_fw_config_value(hw_design_config, program, |
| 1048 | 'Intel wifi sar id') |
| 1049 | output_path = os.path.join(output_dir, 'wifi') |
| 1050 | os.makedirs(output_path, exist_ok=True) |
| 1051 | filename = 'wifi_sar_{}.hex'.format(wifi_sar_id) |
| 1052 | output_path = os.path.join(output_path, filename) |
| 1053 | build_path = os.path.join(build_root_dir, 'wifi', filename) |
| 1054 | if os.path.exists(output_path): |
| 1055 | with open(output_path, 'r') as f: |
| 1056 | if f.read() != sar_file_content: |
| 1057 | raise Exception( |
| 1058 | 'Project {} has conflicting wifi sar file content under ' |
| 1059 | 'wifi sar id {}.'.format(project_name, wifi_sar_id)) |
| 1060 | else: |
| 1061 | with open(output_path, 'w') as f: |
| 1062 | f.write(sar_file_content) |
| 1063 | system_path = '/firmware/cbfs-rw-raw/{}/{}'.format( |
| 1064 | project_name, filename) |
David Burger | 89e7cae | 2020-09-15 17:04:12 -0600 | [diff] [blame] | 1065 | result[design_name] = {'sar-file': _file_v2(build_path, system_path)} |
David Burger | ceeb68a | 2020-09-03 11:31:10 -0600 | [diff] [blame] | 1066 | return result |
| 1067 | |
| 1068 | |
| 1069 | def _extract_fw_config_value(hw_design_config, program, name): |
| 1070 | """Extracts the firwmare config value with the given name. |
| 1071 | |
| 1072 | Args: |
| 1073 | hw_design_config: Design extracting value from. |
| 1074 | program: Program the `hw_design_config` belongs to. |
| 1075 | name: Name of firmware config segment to extract. |
| 1076 | |
| 1077 | Returns: the extracted value or raises a ValueError if no firmware |
| 1078 | configuration segment with `name` is found. |
| 1079 | """ |
| 1080 | fw_config = hw_design_config.hardware_features.fw_config.value |
| 1081 | for fcs in program.firmware_configuration_segments: |
| 1082 | if fcs.name == name: |
| 1083 | value = fw_config & fcs.mask |
| 1084 | lsb_bit_set = (~fcs.mask + 1) & fcs.mask |
| 1085 | return value // lsb_bit_set |
| 1086 | raise ValueError( |
| 1087 | 'No firmware configuration segment with name {} found'.format(name)) |
| 1088 | |
| 1089 | |
| 1090 | def _create_intel_sar_file_content(intel_config): |
| 1091 | """Creates and returns the intel sar file content for the given config. |
| 1092 | |
| 1093 | Creates and returns the sar file content that is used with intel drivers |
| 1094 | only. |
| 1095 | |
| 1096 | Args: |
| 1097 | intel_config: IntelConfig config. |
| 1098 | |
| 1099 | Returns: |
| 1100 | sar file content for the given config, see: |
| 1101 | https://chromeos.google.com/partner/dlm/docs/connectivity/wifidyntxpower.html |
| 1102 | """ |
| 1103 | |
| 1104 | def to_hex(val): |
| 1105 | if val > 255 or val < 0: |
| 1106 | raise Exception('Sar file value %s out of range' % val) |
| 1107 | return '{0:0{1}X}'.format(val, 2) |
| 1108 | |
| 1109 | def power_table(tpc): |
| 1110 | return (to_hex(tpc.limit_2g) + to_hex(tpc.limit_5g_1) + |
| 1111 | to_hex(tpc.limit_5g_2) + to_hex(tpc.limit_5g_3) + |
| 1112 | to_hex(tpc.limit_5g_4)) |
| 1113 | |
| 1114 | def wgds_value(wgds): |
| 1115 | return to_hex(wgds) |
| 1116 | |
| 1117 | def offset_table(offsets): |
| 1118 | return (to_hex(offsets.max_2g) + to_hex(offsets.offset_2g_a) + |
| 1119 | to_hex(offsets.offset_2g_b) + to_hex(offsets.max_5g) + |
| 1120 | to_hex(offsets.offset_5g_a) + to_hex(offsets.offset_5g_b)) |
| 1121 | |
| 1122 | # See https://chromeos.google.com/partner/dlm/docs/connectivity/wifidyntxpower.html |
| 1123 | return (power_table(intel_config.tablet_mode_power_table_a) + |
| 1124 | power_table(intel_config.tablet_mode_power_table_b) + |
| 1125 | power_table(intel_config.non_tablet_mode_power_table_a) + |
| 1126 | power_table(intel_config.non_tablet_mode_power_table_b) + |
| 1127 | '00000000000000000000' + '00000000000000000000' + |
| 1128 | wgds_value(intel_config.wgds_version) + |
| 1129 | offset_table(intel_config.offset_fcc) + |
| 1130 | offset_table(intel_config.offset_eu) + |
| 1131 | offset_table(intel_config.offset_other)) |
| 1132 | |
| 1133 | |
Andrew Lamb | cd33f70 | 2020-06-11 10:45:16 -0600 | [diff] [blame] | 1134 | def Main(project_configs, program_config, output): # pylint: disable=invalid-name |
David Burger | 7fd1dbe | 2020-03-26 09:26:55 -0600 | [diff] [blame] | 1135 | """Transforms source proto config into platform JSON. |
| 1136 | |
| 1137 | Args: |
| 1138 | project_configs: List of source project configs to transform. |
| 1139 | program_config: Program config for the given set of projects. |
| 1140 | output: Output file that will be generated by the transform. |
| 1141 | """ |
Andrew Lamb | cd33f70 | 2020-06-11 10:45:16 -0600 | [diff] [blame] | 1142 | configs = _merge_configs([_read_config(program_config)] + |
| 1143 | [_read_config(config) for config in project_configs]) |
C Shapiro | 5bf23a7 | 2020-04-24 11:40:17 -0500 | [diff] [blame] | 1144 | arc_hw_feature_files = {} |
C Shapiro | 2b6d533 | 2020-05-06 17:51:35 -0500 | [diff] [blame] | 1145 | touch_fw = {} |
David Burger | 8ee9b4d | 2020-06-16 17:40:21 -0600 | [diff] [blame] | 1146 | camera_map = {} |
David Burger | ceeb68a | 2020-09-03 11:31:10 -0600 | [diff] [blame] | 1147 | dptf_map = {} |
| 1148 | wifi_sar_map = {} |
C Shapiro | 5bf23a7 | 2020-04-24 11:40:17 -0500 | [diff] [blame] | 1149 | output_dir = os.path.dirname(output) |
C Shapiro | 5c87799 | 2020-04-29 12:11:28 -0500 | [diff] [blame] | 1150 | build_root_dir = output_dir |
C Shapiro | 5c87799 | 2020-04-29 12:11:28 -0500 | [diff] [blame] | 1151 | if 'sw_build_config' in output_dir: |
| 1152 | full_path = os.path.realpath(output) |
Andrew Lamb | 6b60773 | 2020-08-24 15:52:46 -0600 | [diff] [blame] | 1153 | project_name = re.match(r'.*/(\w*)/(public_)?sw_build_config/.*', |
Andrew Lamb | 2413c98 | 2020-05-29 12:15:36 -0600 | [diff] [blame] | 1154 | full_path).groups(1)[0] |
C Shapiro | 5c87799 | 2020-04-29 12:11:28 -0500 | [diff] [blame] | 1155 | # Projects don't know about each other until they are integrated into the |
| 1156 | # build system. When this happens, the files need to be able to co-exist |
| 1157 | # without any collisions. This prefixes the project name (which is how |
| 1158 | # portage maps in the project), so project files co-exist and can be |
| 1159 | # installed together. |
| 1160 | # This is necessary to allow projects to share files at the program level |
| 1161 | # without having portage file installation collisions. |
| 1162 | build_root_dir = os.path.join(project_name, output_dir) |
C Shapiro | 6830e6c | 2020-04-29 13:29:56 -0500 | [diff] [blame] | 1163 | |
David Burger | 1ba78a2 | 2020-06-18 18:42:47 -0600 | [diff] [blame] | 1164 | camera_map = _camera_map(configs, project_name) |
David Burger | 52c9d32 | 2020-06-09 07:16:18 -0600 | [diff] [blame] | 1165 | dptf_map = _dptf_map(configs, project_name) |
David Burger | ceeb68a | 2020-09-03 11:31:10 -0600 | [diff] [blame] | 1166 | wifi_sar_map = _wifi_sar_map(configs, project_name, output_dir, |
| 1167 | build_root_dir) |
David Burger | 52c9d32 | 2020-06-09 07:16:18 -0600 | [diff] [blame] | 1168 | |
C Shapiro | 2b6d533 | 2020-05-06 17:51:35 -0500 | [diff] [blame] | 1169 | if os.path.exists(TOUCH_PATH): |
Andrew Lamb | cd33f70 | 2020-06-11 10:45:16 -0600 | [diff] [blame] | 1170 | touch_fw = _build_touch_file_config(configs, project_name) |
Andrew Lamb | cd33f70 | 2020-06-11 10:45:16 -0600 | [diff] [blame] | 1171 | arc_hw_feature_files = _write_arc_hardware_feature_files( |
| 1172 | configs, output_dir, build_root_dir) |
C Shapiro | 5bf23a7 | 2020-04-24 11:40:17 -0500 | [diff] [blame] | 1173 | config_files = ConfigFiles( |
C Shapiro | 5bf23a7 | 2020-04-24 11:40:17 -0500 | [diff] [blame] | 1174 | arc_hw_features=arc_hw_feature_files, |
C Shapiro | 2b6d533 | 2020-05-06 17:51:35 -0500 | [diff] [blame] | 1175 | touch_fw=touch_fw, |
David Burger | 8ee9b4d | 2020-06-16 17:40:21 -0600 | [diff] [blame] | 1176 | dptf_map=dptf_map, |
David Burger | ceeb68a | 2020-09-03 11:31:10 -0600 | [diff] [blame] | 1177 | camera_map=camera_map, |
| 1178 | wifi_sar_map=wifi_sar_map) |
Andrew Lamb | cd33f70 | 2020-06-11 10:45:16 -0600 | [diff] [blame] | 1179 | write_output(_transform_build_configs(configs, config_files), output) |
David Burger | 7fd1dbe | 2020-03-26 09:26:55 -0600 | [diff] [blame] | 1180 | |
| 1181 | |
| 1182 | def main(argv=None): |
| 1183 | """Main program which parses args and runs |
| 1184 | |
| 1185 | Args: |
| 1186 | argv: List of command line arguments, if None uses sys.argv. |
| 1187 | """ |
| 1188 | if argv is None: |
| 1189 | argv = sys.argv[1:] |
Andrew Lamb | cd33f70 | 2020-06-11 10:45:16 -0600 | [diff] [blame] | 1190 | opts = parse_args(argv) |
David Burger | 7fd1dbe | 2020-03-26 09:26:55 -0600 | [diff] [blame] | 1191 | Main(opts.project_configs, opts.program_config, opts.output) |
| 1192 | |
| 1193 | |
| 1194 | if __name__ == '__main__': |
| 1195 | sys.exit(main(sys.argv[1:])) |