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