blob: 034b3dcb8d7bb152b0e41bf1166d8cb357a843b2 [file] [log] [blame]
David Burger7fd1dbe2020-03-26 09:26:55 -06001#!/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
8import argparse
9import json
10import pprint
C Shapiro90fda252020-04-17 14:34:57 -050011import os
David Burger7fd1dbe2020-03-26 09:26:55 -060012import sys
C Shapiro90fda252020-04-17 14:34:57 -050013import re
C Shapiro5bf23a72020-04-24 11:40:17 -050014import xml.etree.ElementTree as etree
C Shapiro9a3ac8c2020-04-25 07:49:21 -050015import xml.dom.minidom as minidom
David Burger7fd1dbe2020-03-26 09:26:55 -060016
Andrew Lamb319cc922020-06-15 10:45:46 -060017from typing import List
18
David Burger7fd1dbe2020-03-26 09:26:55 -060019from collections import namedtuple
20
Andrew Lambcd33f702020-06-11 10:45:16 -060021from google.protobuf import json_format
22
Prathmesh Prabhu72f8a002020-04-10 09:57:53 -070023from chromiumos.config.api import device_brand_pb2
David Burger92609a32020-04-23 10:38:50 -060024from chromiumos.config.api import topology_pb2
C Shapiro5bf23a72020-04-24 11:40:17 -050025from chromiumos.config.payload import config_bundle_pb2
Prathmesh Prabhu72f8a002020-04-10 09:57:53 -070026from chromiumos.config.api.software import brand_config_pb2
David Burger7fd1dbe2020-03-26 09:26:55 -060027
Andrew Lamb2413c982020-05-29 12:15:36 -060028Config = namedtuple('Config', [
29 'program', 'hw_design', 'odm', 'hw_design_config', 'device_brand',
30 'device_signer_config', 'oem', 'sw_config', 'brand_config', 'build_target'
31])
David Burger7fd1dbe2020-03-26 09:26:55 -060032
David Burger07af5242020-08-11 11:08:25 -060033ConfigFiles = namedtuple(
34 'ConfigFiles',
35 ['arc_hw_features', 'touch_fw', 'dptf_map', 'camera_map', 'arc_camera_map'])
David Burger2f0d9522020-07-30 10:52:28 -060036
37ARC_CONFIG_PATH = 'sw_build_config/platform/chromeos-config/arc'
38ARC_CAMERA_CHARACTERISTICS_FILE = 'camera_characteristics.conf'
David Burger8ee9b4d2020-06-16 17:40:21 -060039
40CAMERA_CONFIG_DEST_PATH_TEMPLATE = '/etc/camera/camera_config_{}.json'
41CAMERA_CONFIG_SOURCE_PATH_TEMPLATE = (
42 'sw_build_config/platform/chromeos-config/camera/camera_config_{}.json')
C Shapiro5bf23a72020-04-24 11:40:17 -050043
David Burger52c9d322020-06-09 07:16:18 -060044DPTF_PATH = 'sw_build_config/platform/chromeos-config/thermal'
45DPTF_FILE = 'dptf.dv'
David Burger2f0d9522020-07-30 10:52:28 -060046
C Shapiro2b6d5332020-05-06 17:51:35 -050047TOUCH_PATH = 'sw_build_config/platform/chromeos-config/touch'
Andrew Lamb6c42efc2020-06-16 10:40:43 -060048WALLPAPER_BASE_PATH = '/usr/share/chromeos-assets/wallpaper'
David Burger7fd1dbe2020-03-26 09:26:55 -060049
Andrew Lamb2413c982020-05-29 12:15:36 -060050
Andrew Lambcd33f702020-06-11 10:45:16 -060051def parse_args(argv):
David Burger7fd1dbe2020-03-26 09:26:55 -060052 """Parse the available arguments.
53
54 Invalid arguments or -h cause this function to print a message and exit.
55
56 Args:
57 argv: List of string arguments (excluding program name / argv[0])
58
59 Returns:
60 argparse.Namespace object containing the attributes.
61 """
62 parser = argparse.ArgumentParser(
63 description='Converts source proto config into platform JSON config.')
64 parser.add_argument(
65 '-c',
66 '--project_configs',
67 nargs='+',
68 type=str,
69 help='Space delimited list of source protobinary project config files.')
70 parser.add_argument(
71 '-p',
72 '--program_config',
73 type=str,
74 help='Path to the source program-level protobinary file')
75 parser.add_argument(
Andrew Lamb2413c982020-05-29 12:15:36 -060076 '-o', '--output', type=str, help='Output file that will be generated')
David Burger7fd1dbe2020-03-26 09:26:55 -060077 return parser.parse_args(argv)
78
79
David Burger8ee9b4d2020-06-16 17:40:21 -060080def _upsert(field, target, target_name):
81 """Updates or inserts `field` within `target`.
82
83 If `target_name` already exists within `target` an update is performed,
84 otherwise, an insert is performed.
85 """
Sam McNally9a873f72020-06-05 19:47:22 +100086 if field or field == 0:
David Burger8ee9b4d2020-06-16 17:40:21 -060087 if target_name in target:
88 target[target_name].update(field)
89 else:
90 target[target_name] = field
David Burger7fd1dbe2020-03-26 09:26:55 -060091
92
Andrew Lambcd33f702020-06-11 10:45:16 -060093def _build_arc(config, config_files):
94 if not config.build_target.arc:
95 return None
96
97 build_properties = {
98 'device': config.build_target.arc.device,
99 'first-api-level': config.build_target.arc.first_api_level,
100 'marketing-name': config.device_brand.brand_name,
101 'metrics-tag': config.hw_design.name.lower(),
102 'product': config.build_target.id.value,
103 }
104 if config.oem:
105 build_properties['oem'] = config.oem.name
106 result = {'build-properties': build_properties}
107 feature_id = _arc_hardware_feature_id(config.hw_design_config)
108 if feature_id in config_files.arc_hw_features:
109 result['hardware-features'] = config_files.arc_hw_features[feature_id]
110 topology = config.hw_design_config.hardware_topology
111 ppi = topology.screen.hardware_feature.screen.panel_properties.pixels_per_in
112 # Only set for high resolution displays
113 if ppi and ppi > 250:
114 result['scale'] = ppi
David Burger2f0d9522020-07-30 10:52:28 -0600115
116 if config_files.arc_camera_map:
117 # Prefer design specific if found, if not fall back to project wide config
118 # mapped under the empty string.
119 if config.hw_design.name in config_files.arc_camera_map:
120 camera_characteristics = config_files.arc_camera_map[
121 config.hw_design.name]
122 else:
123 camera_characteristics = config_files.arc_camera_map.get('')
124 result['camera-characteristics'] = camera_characteristics
125
Andrew Lambcd33f702020-06-11 10:45:16 -0600126 return result
David Burger7fd1dbe2020-03-26 09:26:55 -0600127
Andrew Lamb2413c982020-05-29 12:15:36 -0600128
Andrew Lamb319cc922020-06-15 10:45:46 -0600129def _build_ash_flags(config: Config) -> List[str]:
130 """Returns a list of Ash flags for config.
131
132 Ash is the window manager and system UI for ChromeOS, see
133 https://chromium.googlesource.com/chromium/src/+/refs/heads/master/ash/.
134 """
135 # A map from flag name -> value. Value may be None for boolean flags.
136 flags = {}
137
138 hw_features = config.hw_design_config.hardware_features
139 if hw_features.stylus.stylus == topology_pb2.HardwareFeatures.Stylus.INTERNAL:
Andrew Lamb2e641e22020-06-15 12:30:41 -0600140 flags['has-internal-stylus'] = None
Andrew Lamb319cc922020-06-15 10:45:46 -0600141
Andrew Lamb2e641e22020-06-15 12:30:41 -0600142 fp_loc = hw_features.fingerprint.location
143 if fp_loc and fp_loc != topology_pb2.HardwareFeatures.Fingerprint.NOT_PRESENT:
144 loc_name = topology_pb2.HardwareFeatures.Fingerprint.Location.Name(fp_loc)
145 flags['fingerprint-sensor-location'] = loc_name.lower().replace('_', '-')
146
Andrew Lamb6c42efc2020-06-16 10:40:43 -0600147 wallpaper = config.brand_config.wallpaper
148 # If a wallpaper is set, the 'default-wallpaper-is-oem' flag needs to be set.
149 # If a wallpaper is not set, the 'default_[large|small].jpg' wallpapers
150 # should still be set.
151 if wallpaper:
152 flags['default-wallpaper-is-oem'] = None
153 else:
154 wallpaper = 'default'
155
156 for size in ('small', 'large'):
157 flags[f'default-wallpaper-{size}'] = (
158 f'{WALLPAPER_BASE_PATH}/{wallpaper}_{size}.jpg')
159
160 # For each size, also install 'guest' and 'child' wallpapers.
161 for wallpaper_type in ('guest', 'child'):
162 flags[f'{wallpaper_type}-wallpaper-{size}'] = (
163 f'{WALLPAPER_BASE_PATH}/{wallpaper_type}_{size}.jpg')
164
Andrew Lamb72d41362020-06-17 09:19:02 -0600165 flags['arc-build-properties'] = json_format.MessageToDict(
166 config.build_target.arc)
167
Andrew Lamb90b168c2020-06-22 10:42:30 -0600168 power_button = hw_features.power_button
169 if power_button.edge:
170 flags['ash-power-button-position'] = json.dumps({
171 'edge':
172 topology_pb2.HardwareFeatures.Button.Edge.Name(power_button.edge
173 ).lower(),
174 # Starlark sometimes represents float literals strangely, e.g. changing
175 # 0.9 to 0.899999. Round to two digits here.
176 'position':
177 round(power_button.position, 2)
178 })
179
180 volume_button = hw_features.volume_button
181 if volume_button.edge:
182 flags['ash-side-volume-button-position'] = json.dumps({
183 'region':
184 topology_pb2.HardwareFeatures.Button.Region.Name(
185 volume_button.region).lower(),
186 'edge':
187 topology_pb2.HardwareFeatures.Button.Edge.Name(volume_button.edge
188 ).lower(),
189 })
190
Andrew Lamb2e641e22020-06-15 12:30:41 -0600191 return sorted([f'--{k}={v}' if v else f'--{k}' for k, v in flags.items()])
Andrew Lamb319cc922020-06-15 10:45:46 -0600192
193
194def _build_ui(config: Config) -> dict:
195 """Builds the 'ui' property from cros_config_schema."""
196 return {'extra-ash-flags': _build_ash_flags(config)}
197
198
David Burger07af5242020-08-11 11:08:25 -0600199def _build_bluetooth(config):
C Shapiro90fda252020-04-17 14:34:57 -0500200 bt_flags = config.sw_config.bluetooth_config.flags
201 # Convert to native map (from proto wrapper)
202 bt_flags_map = dict(bt_flags)
203 result = {}
204 if bt_flags_map:
205 result['flags'] = bt_flags_map
C Shapiro90fda252020-04-17 14:34:57 -0500206 return result
207
David Burger7fd1dbe2020-03-26 09:26:55 -0600208
David Burgerec753912020-08-10 12:59:11 -0600209def _build_wifi(config):
210 result = {}
211 if config.sw_config.wifi_config.HasField('ath10k_config'):
212 ath10k_config = config.sw_config.wifi_config.ath10k_config
213
214 def power_chain(power):
215 return {
216 'limit-2g': power.limit_2g,
217 'limit-5g': power.limit_5g,
218 }
219
220 result['tablet-mode-power-table-ath10k'] = power_chain(
221 ath10k_config.tablet_mode_power_table)
222 result['non-tablet-mode-power-table-ath10k'] = power_chain(
223 ath10k_config.non_tablet_mode_power_table)
224 elif config.sw_config.wifi_config.HasField('rtw88_config'):
225 rtw88_config = config.sw_config.wifi_config.rtw88_config
226
227 def power_chain(power):
228 return {
229 'limit-2g': power.limit_2g,
230 'limit-5g-1': power.limit_5g_1,
231 'limit-5g-3': power.limit_5g_3,
232 'limit-5g-4': power.limit_5g_4,
233 }
234
235 result['tablet-mode-power-table-rtw'] = power_chain(
236 rtw88_config.tablet_mode_power_table)
237 result['non-tablet-mode-power-table-rtw'] = power_chain(
238 rtw88_config.non_tablet_mode_power_table)
239
240 def offsets(offset):
241 return {
242 'offset-2g': offset.offset_2g,
243 'offset-5g': offset.offset_5g,
244 }
245
246 result['geo-offsets-fcc'] = offsets(rtw88_config.offset_fcc)
247 result['geo-offsets-eu'] = offsets(rtw88_config.offset_eu)
248 result['geo-offsets-rest-of-world'] = offsets(rtw88_config.offset_other)
249 return result
250
251
Andrew Lambcd33f702020-06-11 10:45:16 -0600252def _build_fingerprint(hw_topology):
253 if not hw_topology.HasField('fingerprint'):
254 return None
255
256 fp = hw_topology.fingerprint.hardware_feature.fingerprint
257 result = {}
258 if fp.location != topology_pb2.HardwareFeatures.Fingerprint.NOT_PRESENT:
259 location = fp.Location.DESCRIPTOR.values_by_number[fp.location].name
260 result['sensor-location'] = location.lower().replace('_', '-')
261 if fp.board:
262 result['board'] = fp.board
Tom Hughesdfc35402020-06-29 16:02:09 -0700263 if fp.ro_version:
264 result['ro-version'] = fp.ro_version
265
Andrew Lambcd33f702020-06-11 10:45:16 -0600266 return result
David Burger7fd1dbe2020-03-26 09:26:55 -0600267
268
Andrew Lambcd33f702020-06-11 10:45:16 -0600269def _fw_bcs_path(payload):
David Burger7fd1dbe2020-03-26 09:26:55 -0600270 if payload and payload.firmware_image_name:
Andrew Lamb2413c982020-05-29 12:15:36 -0600271 return 'bcs://%s.%d.%d.0.tbz2' % (payload.firmware_image_name,
272 payload.version.major,
273 payload.version.minor)
David Burger7fd1dbe2020-03-26 09:26:55 -0600274
Andrew Lambcd33f702020-06-11 10:45:16 -0600275 return None
David Burger7fd1dbe2020-03-26 09:26:55 -0600276
Andrew Lambcd33f702020-06-11 10:45:16 -0600277
278def _fw_build_target(payload):
David Burger7fd1dbe2020-03-26 09:26:55 -0600279 if payload:
280 return payload.build_target_name
281
Andrew Lambcd33f702020-06-11 10:45:16 -0600282 return None
David Burger7fd1dbe2020-03-26 09:26:55 -0600283
Andrew Lambcd33f702020-06-11 10:45:16 -0600284
285def _build_firmware(config):
David Burgerb70b6762020-05-21 12:14:59 -0600286 """Returns firmware config, or None if no build targets."""
Andrew Lamb3da156d2020-04-16 16:00:56 -0600287 fw_payload_config = config.sw_config.firmware
288 fw_build_config = config.sw_config.firmware_build_config
289 main_ro = fw_payload_config.main_ro_payload
290 main_rw = fw_payload_config.main_rw_payload
291 ec_ro = fw_payload_config.ec_ro_payload
292 pd_ro = fw_payload_config.pd_ro_payload
David Burger7fd1dbe2020-03-26 09:26:55 -0600293
294 build_targets = {}
Andrew Lamb3da156d2020-04-16 16:00:56 -0600295
David Burger8ee9b4d2020-06-16 17:40:21 -0600296 _upsert(fw_build_config.build_targets.depthcharge, build_targets,
297 'depthcharge')
298 _upsert(fw_build_config.build_targets.coreboot, build_targets, 'coreboot')
299 _upsert(fw_build_config.build_targets.ec, build_targets, 'ec')
300 _upsert(
Andrew Lambf8954ee2020-04-21 10:24:40 -0600301 list(fw_build_config.build_targets.ec_extras), build_targets, 'ec_extras')
David Burger8ee9b4d2020-06-16 17:40:21 -0600302 _upsert(fw_build_config.build_targets.libpayload, build_targets, 'libpayload')
David Burger7fd1dbe2020-03-26 09:26:55 -0600303
David Burgerb70b6762020-05-21 12:14:59 -0600304 if not build_targets:
305 return None
306
David Burger7fd1dbe2020-03-26 09:26:55 -0600307 result = {
308 'bcs-overlay': config.build_target.overlay_name,
309 'build-targets': build_targets,
David Burger7fd1dbe2020-03-26 09:26:55 -0600310 }
Andrew Lamb883fa042020-04-06 11:37:22 -0600311
David Burger8ee9b4d2020-06-16 17:40:21 -0600312 _upsert(main_ro.firmware_image_name.lower(), result, 'image-name')
Andrew Lamb883fa042020-04-06 11:37:22 -0600313
David Burger8ee9b4d2020-06-16 17:40:21 -0600314 _upsert(_fw_bcs_path(main_ro), result, 'main-ro-image')
315 _upsert(_fw_bcs_path(main_rw), result, 'main-rw-image')
316 _upsert(_fw_bcs_path(ec_ro), result, 'ec-ro-image')
317 _upsert(_fw_bcs_path(pd_ro), result, 'pd-ro-image')
David Burger7fd1dbe2020-03-26 09:26:55 -0600318
David Burger8ee9b4d2020-06-16 17:40:21 -0600319 _upsert(
Andrew Lambf39fbe82020-04-13 16:14:33 -0600320 config.hw_design_config.hardware_features.fw_config.value,
321 result,
322 'firmware-config',
323 )
324
David Burger7fd1dbe2020-03-26 09:26:55 -0600325 return result
326
327
Andrew Lambcd33f702020-06-11 10:45:16 -0600328def _build_fw_signing(config):
C Shapiro2f0bb5d2020-04-14 10:07:47 -0500329 if config.sw_config.firmware and config.device_signer_config:
David Burger68e0d142020-05-15 17:29:33 -0600330 hw_design = config.hw_design.name.lower()
Sam McNally2fc807f2020-07-16 18:13:53 +1000331 brand_scan_config = config.brand_config.scan_config
332 if brand_scan_config and brand_scan_config.whitelabel_tag:
333 signature_id = '%s-%s' % (hw_design, brand_scan_config.whitelabel_tag)
334 else:
335 signature_id = hw_design
336
C Shapiro2f0bb5d2020-04-14 10:07:47 -0500337 return {
338 'key-id': config.device_signer_config.key_id,
Sam McNally2fc807f2020-07-16 18:13:53 +1000339 'signature-id': signature_id,
C Shapiro2f0bb5d2020-04-14 10:07:47 -0500340 }
341 return {}
David Burger7fd1dbe2020-03-26 09:26:55 -0600342
343
Andrew Lambcd33f702020-06-11 10:45:16 -0600344def _file(source, destination):
Andrew Lamb2413c982020-05-29 12:15:36 -0600345 return {'destination': destination, 'source': source}
David Burger7fd1dbe2020-03-26 09:26:55 -0600346
347
David Burger40dfe3a2020-06-18 17:09:13 -0600348def _file_v2(build_path, system_path):
349 return {'build-path': build_path, 'system-path': system_path}
350
351
Andrew Lambcd33f702020-06-11 10:45:16 -0600352def _build_audio(config):
David Burger178f3ef2020-06-26 12:11:57 -0600353 if not config.sw_config.audio_configs:
354 return {}
David Burger7fd1dbe2020-03-26 09:26:55 -0600355 alsa_path = '/usr/share/alsa/ucm'
356 cras_path = '/etc/cras'
357 project_name = config.hw_design.name.lower()
David Burger43250662020-05-07 11:21:50 -0600358 program_name = config.program.name.lower()
David Burger7fd1dbe2020-03-26 09:26:55 -0600359 files = []
David Burger178f3ef2020-06-26 12:11:57 -0600360 ucm_suffix = None
361 for audio in config.sw_config.audio_configs:
362 card = audio.card_name
363 card_with_suffix = audio.card_name
364 if audio.ucm_suffix:
365 # TODO: last ucm_suffix wins.
366 ucm_suffix = audio.ucm_suffix
367 card_with_suffix += '.' + audio.ucm_suffix
368 if audio.ucm_file:
369 files.append(
370 _file(audio.ucm_file,
371 '%s/%s/HiFi.conf' % (alsa_path, card_with_suffix)))
372 if audio.ucm_master_file:
373 files.append(
374 _file(
375 audio.ucm_master_file, '%s/%s/%s.conf' %
Andrew Lamb2413c982020-05-29 12:15:36 -0600376 (alsa_path, card_with_suffix, card_with_suffix)))
David Burger178f3ef2020-06-26 12:11:57 -0600377 if audio.card_config_file:
378 files.append(
379 _file(audio.card_config_file,
380 '%s/%s/%s' % (cras_path, project_name, card)))
381 if audio.dsp_file:
382 files.append(
383 _file(audio.dsp_file, '%s/%s/dsp.ini' % (cras_path, project_name)))
384 if audio.module_file:
385 files.append(
386 _file(audio.module_file,
387 '/etc/modprobe.d/alsa-%s.conf' % program_name))
388 if audio.board_file:
389 files.append(
390 _file(audio.board_file,
391 '%s/%s/board.ini' % (cras_path, project_name)))
David Burger599ff7b2020-04-06 16:29:31 -0600392
393 result = {
David Burger7fd1dbe2020-03-26 09:26:55 -0600394 'main': {
395 'cras-config-dir': project_name,
396 'files': files,
397 }
398 }
David Burger178f3ef2020-06-26 12:11:57 -0600399
400 if ucm_suffix:
401 result['main']['ucm-suffix'] = ucm_suffix
David Burger599ff7b2020-04-06 16:29:31 -0600402
403 return result
David Burger7fd1dbe2020-03-26 09:26:55 -0600404
405
Andrew Lambcd33f702020-06-11 10:45:16 -0600406def _build_camera(hw_topology):
David Burger8aa8fa32020-04-14 08:30:34 -0600407 if hw_topology.HasField('camera'):
408 camera = hw_topology.camera.hardware_feature.camera
409 result = {}
410 if camera.count.value:
411 result['count'] = camera.count.value
412 return result
413
Andrew Lambcd33f702020-06-11 10:45:16 -0600414 return None
David Burger8aa8fa32020-04-14 08:30:34 -0600415
Andrew Lambcd33f702020-06-11 10:45:16 -0600416
417def _build_identity(hw_scan_config, program, brand_scan_config=None):
David Burger7fd1dbe2020-03-26 09:26:55 -0600418 identity = {}
David Burger8ee9b4d2020-06-16 17:40:21 -0600419 _upsert(hw_scan_config.firmware_sku, identity, 'sku-id')
420 _upsert(hw_scan_config.smbios_name_match, identity, 'smbios-name-match')
Andrew Lamb7806ce92020-04-07 10:22:17 -0600421 # 'platform-name' is needed to support 'mosys platform name'. Clients should
422 # longer require platform name, but set it here for backwards compatibility.
David Burger8ee9b4d2020-06-16 17:40:21 -0600423 _upsert(program.name, identity, 'platform-name')
David Burger7fd1dbe2020-03-26 09:26:55 -0600424 # ARM architecture
David Burger8ee9b4d2020-06-16 17:40:21 -0600425 _upsert(hw_scan_config.device_tree_compatible_match, identity,
426 'device-tree-compatible-match')
David Burger7fd1dbe2020-03-26 09:26:55 -0600427
428 if brand_scan_config:
David Burger8ee9b4d2020-06-16 17:40:21 -0600429 _upsert(brand_scan_config.whitelabel_tag, identity, 'whitelabel-tag')
David Burger7fd1dbe2020-03-26 09:26:55 -0600430
431 return identity
432
433
Andrew Lambcd33f702020-06-11 10:45:16 -0600434def _lookup(id_value, id_map):
435 if not id_value.value:
436 return None
437
438 key = id_value.value
439 if key in id_map:
440 return id_map[id_value.value]
441 error = 'Failed to lookup %s with value: %s' % (
442 id_value.__class__.__name__.replace('Id', ''), key)
443 print(error)
444 print('Check the config contents provided:')
445 printer = pprint.PrettyPrinter(indent=4)
446 printer.pprint(id_map)
447 raise Exception(error)
David Burger7fd1dbe2020-03-26 09:26:55 -0600448
449
Andrew Lambcd33f702020-06-11 10:45:16 -0600450def _build_touch_file_config(config, project_name):
Sean McAllistereaf10b72020-08-03 13:41:06 -0600451 partners = {x.id.value: x for x in config.partner_list}
C Shapiro2b6d5332020-05-06 17:51:35 -0500452 files = []
453 for comp in config.components:
C Shapiro4813be62020-05-13 17:31:58 -0500454 touch = comp.touchscreen
455 # Everything is the same for Touch screen/pad, except different fields
456 if comp.HasField('touchpad'):
457 touch = comp.touchpad
458 if touch.product_id:
Andrew Lambcd33f702020-06-11 10:45:16 -0600459 vendor = _lookup(comp.manufacturer_id, partners)
C Shapiro2b6d5332020-05-06 17:51:35 -0500460 if not vendor:
Andrew Lamb2413c982020-05-29 12:15:36 -0600461 raise Exception("Manufacturer must be set for touch device %s" %
462 comp.id.value)
C Shapiro2b6d5332020-05-06 17:51:35 -0500463
C Shapiro4813be62020-05-13 17:31:58 -0500464 product_id = touch.product_id
465 fw_version = touch.fw_version
C Shapiro2b6d5332020-05-06 17:51:35 -0500466
C Shapiro2b6d5332020-05-06 17:51:35 -0500467 file_name = "%s_%s.bin" % (product_id, fw_version)
468 fw_file_path = os.path.join(TOUCH_PATH, vendor.name, file_name)
469
470 if not os.path.exists(fw_file_path):
Andrew Lamb2413c982020-05-29 12:15:36 -0600471 raise Exception("Touchscreen fw bin file doesn't exist at: %s" %
472 fw_file_path)
C Shapiro2b6d5332020-05-06 17:51:35 -0500473
C Shapiro303cece2020-07-22 07:15:21 -0500474 touch_vendor = vendor.touch_vendor
475 sym_link = touch_vendor.symlink_file_format.format(
476 vendor_name=vendor.name,
477 vendor_id=touch_vendor.vendor_id,
478 product_id=product_id,
479 fw_version=fw_version,
480 product_series=touch.product_series)
481
482 dest = "%s_%s" % (vendor.name, file_name)
483 if touch_vendor.destination_file_format:
484 dest = touch_vendor.destination_file_format.format(
485 vendor_name=vendor.name,
486 vendor_id=touch_vendor.vendor_id,
487 product_id=product_id,
488 fw_version=fw_version,
489 product_series=touch.product_series)
490
C Shapiro2b6d5332020-05-06 17:51:35 -0500491 files.append({
C Shapiro303cece2020-07-22 07:15:21 -0500492 "destination": os.path.join("/opt/google/touch/firmware", dest),
YH Lin9160fc52020-07-22 16:35:28 -0700493 "source": os.path.join(project_name, fw_file_path),
494 "symlink": os.path.join("/lib/firmware", sym_link),
C Shapiro2b6d5332020-05-06 17:51:35 -0500495 })
496
497 result = {}
David Burger8ee9b4d2020-06-16 17:40:21 -0600498 _upsert(files, result, 'files')
C Shapiro2b6d5332020-05-06 17:51:35 -0500499 return result
500
501
David Burger8ee9b4d2020-06-16 17:40:21 -0600502def _transform_build_configs(config,
David Burger07af5242020-08-11 11:08:25 -0600503 config_files=ConfigFiles({}, {}, {}, {}, {})):
Andrew Lambcd33f702020-06-11 10:45:16 -0600504 # pylint: disable=too-many-locals,too-many-branches
Sean McAllistereaf10b72020-08-03 13:41:06 -0600505 partners = {x.id.value: x for x in config.partner_list}
Sean McAllisterf38d1e92020-08-03 13:57:53 -0600506 programs = {x.id.value: x for x in config.program_list}
David Burger7fd1dbe2020-03-26 09:26:55 -0600507 sw_configs = list(config.software_configs)
Andrew Lambcd33f702020-06-11 10:45:16 -0600508 brand_configs = {x.brand_id.value: x for x in config.brand_configs}
David Burger7fd1dbe2020-03-26 09:26:55 -0600509
C Shapiroa0b766c2020-03-31 08:35:28 -0500510 if len(config.build_targets) != 1:
511 # Artifact of sharing the config_bundle for analysis and transforms.
512 # Integrated analysis of multiple programs/projects it the only time
513 # having multiple build targets would be valid.
514 raise Exception('Single build_target required for transform')
515
David Burger7fd1dbe2020-03-26 09:26:55 -0600516 results = {}
Sean McAllisterf66887b2020-08-03 14:00:51 -0600517 for hw_design in config.design_list:
Sean McAllister6cbb0ec2020-08-03 14:03:37 -0600518 if config.device_brand_list:
Andrew Lamb2413c982020-05-29 12:15:36 -0600519 device_brands = [
Sean McAllister6cbb0ec2020-08-03 14:03:37 -0600520 x for x in config.device_brand_list
Andrew Lamb2413c982020-05-29 12:15:36 -0600521 if x.design_id.value == hw_design.id.value
522 ]
David Burger7fd1dbe2020-03-26 09:26:55 -0600523 else:
524 device_brands = [device_brand_pb2.DeviceBrand()]
525
526 for device_brand in device_brands:
527 # Brand config can be empty since platform JSON config allows it
528 brand_config = brand_config_pb2.BrandConfig()
529 if device_brand.id.value in brand_configs:
530 brand_config = brand_configs[device_brand.id.value]
531
532 for hw_design_config in hw_design.configs:
533 design_id = hw_design_config.id.value
Andrew Lamb2413c982020-05-29 12:15:36 -0600534 sw_config_matches = [
535 x for x in sw_configs if x.design_config_id.value == design_id
536 ]
David Burger7fd1dbe2020-03-26 09:26:55 -0600537 if len(sw_config_matches) == 1:
538 sw_config = sw_config_matches[0]
539 elif len(sw_config_matches) > 1:
540 raise Exception('Multiple software configs found for: %s' % design_id)
541 else:
542 raise Exception('Software config is required for: %s' % design_id)
543
Andrew Lambcd33f702020-06-11 10:45:16 -0600544 program = _lookup(hw_design.program_id, programs)
C Shapiroadefd7c2020-05-19 16:37:21 -0500545 signer_configs_by_design = {}
546 signer_configs_by_brand = {}
547 for signer_config in program.device_signer_configs:
548 design_id = signer_config.design_id.value
549 brand_id = signer_config.brand_id.value
550 if design_id:
551 signer_configs_by_design[design_id] = signer_config
552 elif brand_id:
553 signer_configs_by_brand[brand_id] = signer_config
554 else:
555 raise Exception('No ID found for signer config: %s' % signer_config)
556
C Shapiro2f0bb5d2020-04-14 10:07:47 -0500557 device_signer_config = None
C Shapiroadefd7c2020-05-19 16:37:21 -0500558 if signer_configs_by_design or signer_configs_by_brand:
559 design_id = hw_design.id.value
560 brand_id = device_brand.id.value
561 if design_id in signer_configs_by_design:
562 device_signer_config = signer_configs_by_design[design_id]
563 elif brand_id in signer_configs_by_brand:
564 device_signer_config = signer_configs_by_brand[brand_id]
565 else:
566 # Assume that if signer configs are set, every config is setup
Andrew Lamb2413c982020-05-29 12:15:36 -0600567 raise Exception('Signer config missing for design: %s, brand: %s' %
568 (design_id, brand_id))
C Shapiro2f0bb5d2020-04-14 10:07:47 -0500569
Andrew Lambcd33f702020-06-11 10:45:16 -0600570 transformed_config = _transform_build_config(
C Shapiro90fda252020-04-17 14:34:57 -0500571 Config(
572 program=program,
573 hw_design=hw_design,
Andrew Lambcd33f702020-06-11 10:45:16 -0600574 odm=_lookup(hw_design.odm_id, partners),
C Shapiro90fda252020-04-17 14:34:57 -0500575 hw_design_config=hw_design_config,
576 device_brand=device_brand,
577 device_signer_config=device_signer_config,
Andrew Lambcd33f702020-06-11 10:45:16 -0600578 oem=_lookup(device_brand.oem_id, partners),
C Shapiro90fda252020-04-17 14:34:57 -0500579 sw_config=sw_config,
580 brand_config=brand_config,
Andrew Lamb2413c982020-05-29 12:15:36 -0600581 build_target=config.build_targets[0]), config_files)
David Burger7fd1dbe2020-03-26 09:26:55 -0600582
Andrew Lamb2413c982020-05-29 12:15:36 -0600583 config_json = json.dumps(
584 transformed_config,
585 sort_keys=True,
586 indent=2,
587 separators=(',', ': '))
David Burger7fd1dbe2020-03-26 09:26:55 -0600588
589 if config_json not in results:
590 results[config_json] = transformed_config
591
592 return list(results.values())
593
594
Andrew Lambcd33f702020-06-11 10:45:16 -0600595def _transform_build_config(config, config_files):
David Burger7fd1dbe2020-03-26 09:26:55 -0600596 """Transforms Config instance into target platform JSON schema.
597
598 Args:
599 config: Config namedtuple
C Shapiro5bf23a72020-04-24 11:40:17 -0500600 config_files: Map to look up the generated config files.
David Burger7fd1dbe2020-03-26 09:26:55 -0600601
602 Returns:
603 Unique config payload based on the platform JSON schema.
604 """
605 result = {
Andrew Lamb2413c982020-05-29 12:15:36 -0600606 'identity':
Andrew Lambcd33f702020-06-11 10:45:16 -0600607 _build_identity(config.sw_config.id_scan_config, config.program,
608 config.brand_config.scan_config),
Andrew Lamb2413c982020-05-29 12:15:36 -0600609 'name':
610 config.hw_design.name.lower(),
David Burger7fd1dbe2020-03-26 09:26:55 -0600611 }
612
David Burger8ee9b4d2020-06-16 17:40:21 -0600613 _upsert(_build_arc(config, config_files), result, 'arc')
614 _upsert(_build_audio(config), result, 'audio')
David Burger07af5242020-08-11 11:08:25 -0600615 _upsert(_build_bluetooth(config), result, 'bluetooth')
David Burgerec753912020-08-10 12:59:11 -0600616 _upsert(_build_wifi(config), result, 'wifi')
Andrew Lambca279902020-08-06 10:13:42 -0600617 _upsert(config.brand_config.wallpaper, result, 'wallpaper')
David Burger8ee9b4d2020-06-16 17:40:21 -0600618 _upsert(config.device_brand.brand_code, result, 'brand-code')
619 _upsert(
Andrew Lambcd33f702020-06-11 10:45:16 -0600620 _build_camera(config.hw_design_config.hardware_topology), result,
621 'camera')
David Burger8ee9b4d2020-06-16 17:40:21 -0600622 _upsert(_build_firmware(config), result, 'firmware')
623 _upsert(_build_fw_signing(config), result, 'firmware-signing')
624 _upsert(
Andrew Lambcd33f702020-06-11 10:45:16 -0600625 _build_fingerprint(config.hw_design_config.hardware_topology), result,
Andrew Lamb2413c982020-05-29 12:15:36 -0600626 'fingerprint')
Andrew Lamb0d236ab2020-06-30 12:30:20 -0600627 _upsert(_build_ui(config), result, 'ui')
David Burger7fd1dbe2020-03-26 09:26:55 -0600628 power_prefs = config.sw_config.power_config.preferences
629 power_prefs_map = dict(
Andrew Lamb2413c982020-05-29 12:15:36 -0600630 (x.replace('_', '-'), power_prefs[x]) for x in power_prefs)
David Burger8ee9b4d2020-06-16 17:40:21 -0600631 _upsert(power_prefs_map, result, 'power')
632 if config_files.camera_map:
633 camera_file = config_files.camera_map.get(config.hw_design.name, {})
634 _upsert(camera_file, result, 'camera')
David Burger52c9d322020-06-09 07:16:18 -0600635 if config_files.dptf_map:
636 # Prefer design specific if found, if not fall back to project wide config
637 # mapped under the empty string.
638 if config_files.dptf_map.get(config.hw_design.name):
639 dptf_file = config_files.dptf_map[config.hw_design.name]
640 else:
641 dptf_file = config_files.dptf_map.get('')
David Burger8ee9b4d2020-06-16 17:40:21 -0600642 _upsert(dptf_file, result, 'thermal')
643 _upsert(config_files.touch_fw, result, 'touch')
David Burger7fd1dbe2020-03-26 09:26:55 -0600644
645 return result
646
647
Andrew Lambcd33f702020-06-11 10:45:16 -0600648def write_output(configs, output=None):
David Burger7fd1dbe2020-03-26 09:26:55 -0600649 """Writes a list of configs to platform JSON format.
650
651 Args:
652 configs: List of config dicts defined in cros_config_schema.yaml
653 output: Target file output (if None, prints to stdout)
654 """
Andrew Lamb2413c982020-05-29 12:15:36 -0600655 json_output = json.dumps({'chromeos': {
656 'configs': configs,
657 }},
658 sort_keys=True,
659 indent=2,
660 separators=(',', ': '))
David Burger7fd1dbe2020-03-26 09:26:55 -0600661 if output:
662 with open(output, 'w') as output_stream:
663 # Using print function adds proper trailing newline.
664 print(json_output, file=output_stream)
665 else:
666 print(json_output)
667
668
Andrew Lambcd33f702020-06-11 10:45:16 -0600669def _feature(name, present):
C Shapiro5bf23a72020-04-24 11:40:17 -0500670 attrib = {'name': name}
671 if present:
672 return etree.Element('feature', attrib=attrib)
Andrew Lambcd33f702020-06-11 10:45:16 -0600673
674 return etree.Element('unavailable-feature', attrib=attrib)
C Shapiro5bf23a72020-04-24 11:40:17 -0500675
676
Andrew Lambcd33f702020-06-11 10:45:16 -0600677def _any_present(features):
Andrew Lamb2413c982020-05-29 12:15:36 -0600678 return topology_pb2.HardwareFeatures.PRESENT in features
C Shapiro5bf23a72020-04-24 11:40:17 -0500679
680
Andrew Lambcd33f702020-06-11 10:45:16 -0600681def _arc_hardware_feature_id(design_config):
C Shapiro5bf23a72020-04-24 11:40:17 -0500682 return design_config.id.value.lower().replace(':', '_')
683
684
Andrew Lambcd33f702020-06-11 10:45:16 -0600685def _write_arc_hardware_feature_file(output_dir, file_name, config_content):
David Burger77a1d312020-05-23 16:05:45 -0600686 output_dir += '/arc'
687 os.makedirs(output_dir, exist_ok=True)
688 output = '%s/%s' % (output_dir, file_name)
Andrew Lamb2413c982020-05-29 12:15:36 -0600689 file_content = minidom.parseString(config_content).toprettyxml(
690 indent=' ', encoding='utf-8')
C Shapiroea33cff2020-05-11 13:32:05 -0500691
692 with open(output, 'wb') as f:
693 f.write(file_content)
694
695
Andrew Lambcd33f702020-06-11 10:45:16 -0600696def _write_arc_hardware_feature_files(config, output_dir, build_root_dir):
C Shapiro5bf23a72020-04-24 11:40:17 -0500697 """Writes ARC hardware_feature.xml files for each config
698
699 Args:
700 config: Source ConfigBundle to process.
701 output_dir: Path to the generated output.
C Shapiro5c877992020-04-29 12:11:28 -0500702 build_root_path: Path to the config file from portage's perspective.
C Shapiro5bf23a72020-04-24 11:40:17 -0500703 Returns:
704 dict that maps the design_config_id onto the correct file.
705 """
Andrew Lambcd33f702020-06-11 10:45:16 -0600706 # pylint: disable=too-many-locals
C Shapiro5bf23a72020-04-24 11:40:17 -0500707 result = {}
C Shapiroea33cff2020-05-11 13:32:05 -0500708 configs_by_design = {}
Sean McAllisterf66887b2020-08-03 14:00:51 -0600709 for hw_design in config.design_list:
C Shapiro5bf23a72020-04-24 11:40:17 -0500710 for design_config in hw_design.configs:
711 hw_features = design_config.hardware_features
Kazuhiro Inaba76d8bb52020-06-26 10:34:07 +0900712 any_camera = hw_features.camera.count.value > 0
Josie Nordrumadc27a72020-07-08 11:05:26 -0600713 multi_camera = hw_features.camera.count.value > 1
Andrew Lambcd33f702020-06-11 10:45:16 -0600714 touchscreen = _any_present([hw_features.screen.touch_support])
C Shapiro5bf23a72020-04-24 11:40:17 -0500715 acc = hw_features.accelerometer
716 gyro = hw_features.gyroscope
717 compass = hw_features.magnetometer
Andrew Lambcd33f702020-06-11 10:45:16 -0600718 light_sensor = hw_features.light_sensor
C Shapiro5bf23a72020-04-24 11:40:17 -0500719 root = etree.Element('permissions')
720 root.extend([
Andrew Lambcd33f702020-06-11 10:45:16 -0600721 _feature('android.hardware.camera', multi_camera),
722 _feature('android.hardware.camera.autofocus', multi_camera),
Kazuhiro Inaba76d8bb52020-06-26 10:34:07 +0900723 _feature('android.hardware.camera.any', any_camera),
724 _feature('android.hardware.camera.front', any_camera),
Andrew Lambcd33f702020-06-11 10:45:16 -0600725 _feature(
726 'android.hardware.sensor.accelerometer',
727 _any_present([acc.lid_accelerometer, acc.base_accelerometer])),
728 _feature('android.hardware.sensor.gyroscope',
729 _any_present([gyro.lid_gyroscope, gyro.base_gyroscope])),
730 _feature(
Andrew Lamb2413c982020-05-29 12:15:36 -0600731 'android.hardware.sensor.compass',
Andrew Lambcd33f702020-06-11 10:45:16 -0600732 _any_present(
733 [compass.lid_magnetometer, compass.base_magnetometer])),
734 _feature(
735 'android.hardware.sensor.light',
736 _any_present(
737 [light_sensor.lid_lightsensor,
738 light_sensor.base_lightsensor])),
739 _feature('android.hardware.touchscreen', touchscreen),
740 _feature('android.hardware.touchscreen.multitouch', touchscreen),
741 _feature('android.hardware.touchscreen.multitouch.distinct',
Andrew Lamb2413c982020-05-29 12:15:36 -0600742 touchscreen),
Andrew Lambcd33f702020-06-11 10:45:16 -0600743 _feature('android.hardware.touchscreen.multitouch.jazzhand',
Andrew Lamb2413c982020-05-29 12:15:36 -0600744 touchscreen),
C Shapiro5bf23a72020-04-24 11:40:17 -0500745 ])
746
C Shapiroea33cff2020-05-11 13:32:05 -0500747 design_name = hw_design.name.lower()
C Shapiro5bf23a72020-04-24 11:40:17 -0500748
C Shapiroea33cff2020-05-11 13:32:05 -0500749 # Constructs the following map:
750 # design_name -> config -> design_configs
751 # This allows any of the following file naming schemes:
752 # - All configs within a design share config (design_name prefix only)
753 # - Nobody shares (full design_name and config id prefix needed)
754 #
755 # Having shared configs when possible makes code reviews easier around
756 # the configs and makes debugging easier on the platform side.
757 config_content = etree.tostring(root)
758 arc_configs = configs_by_design.get(design_name, {})
759 design_configs = arc_configs.get(config_content, [])
760 design_configs.append(design_config)
761 arc_configs[config_content] = design_configs
762 configs_by_design[design_name] = arc_configs
C Shapiro9a3ac8c2020-04-25 07:49:21 -0500763
C Shapiroea33cff2020-05-11 13:32:05 -0500764 for design_name, unique_configs in configs_by_design.items():
765 for file_content, design_configs in unique_configs.items():
Andrew Lamb2413c982020-05-29 12:15:36 -0600766 file_name = 'hardware_features_%s.xml' % design_name
767 if len(unique_configs) == 1:
Andrew Lambcd33f702020-06-11 10:45:16 -0600768 _write_arc_hardware_feature_file(output_dir, file_name, file_content)
C Shapiro9a3ac8c2020-04-25 07:49:21 -0500769
Andrew Lamb2413c982020-05-29 12:15:36 -0600770 for design_config in design_configs:
Andrew Lambcd33f702020-06-11 10:45:16 -0600771 feature_id = _arc_hardware_feature_id(design_config)
Andrew Lamb2413c982020-05-29 12:15:36 -0600772 if len(unique_configs) > 1:
773 file_name = 'hardware_features_%s.xml' % feature_id
Andrew Lambcd33f702020-06-11 10:45:16 -0600774 _write_arc_hardware_feature_file(output_dir, file_name, file_content)
David Burger40dfe3a2020-06-18 17:09:13 -0600775 result[feature_id] = _file_v2('%s/arc/%s' % (build_root_dir, file_name),
776 '/etc/%s' % file_name)
C Shapiro5bf23a72020-04-24 11:40:17 -0500777 return result
778
779
Andrew Lambcd33f702020-06-11 10:45:16 -0600780def _read_config(path):
David Burgerd4f32962020-05-02 12:07:40 -0600781 """Reads a ConfigBundle proto from a json pb file.
David Burgere6f76222020-04-27 11:08:01 -0600782
783 Args:
David Burgerd4f32962020-05-02 12:07:40 -0600784 path: Path to the file encoding the json pb proto.
David Burgere6f76222020-04-27 11:08:01 -0600785 """
786 config = config_bundle_pb2.ConfigBundle()
787 with open(path, 'r') as f:
788 return json_format.Parse(f.read(), config)
789
790
Andrew Lambcd33f702020-06-11 10:45:16 -0600791def _merge_configs(configs):
David Burger7fd1dbe2020-03-26 09:26:55 -0600792 result = config_bundle_pb2.ConfigBundle()
793 for config in configs:
794 result.MergeFrom(config)
795
796 return result
797
798
David Burger1ba78a22020-06-18 18:42:47 -0600799def _camera_map(configs, project_name):
David Burger8ee9b4d2020-06-16 17:40:21 -0600800 """Produces a camera config map for the given configs.
801
802 Produces a map that maps from the design name to the camera config for that
803 design.
804
805 Args:
806 configs: Source ConfigBundle to process.
David Burger1ba78a22020-06-18 18:42:47 -0600807 project_name: Name of project processing for.
David Burger8ee9b4d2020-06-16 17:40:21 -0600808
809 Returns:
810 map from design name to camera config.
811 """
812 result = {}
Sean McAllisterf66887b2020-08-03 14:00:51 -0600813 for design in configs.design_list:
David Burger8ee9b4d2020-06-16 17:40:21 -0600814 design_name = design.name
David Burger0d9e8462020-06-19 14:12:37 -0600815 config_path = CAMERA_CONFIG_SOURCE_PATH_TEMPLATE.format(design_name.lower())
David Burger8ee9b4d2020-06-16 17:40:21 -0600816 if os.path.exists(config_path):
David Burger0d9e8462020-06-19 14:12:37 -0600817 destination = CAMERA_CONFIG_DEST_PATH_TEMPLATE.format(design_name.lower())
David Burger8ee9b4d2020-06-16 17:40:21 -0600818 result[design_name] = {
David Burger1ba78a22020-06-18 18:42:47 -0600819 'config-path':
820 destination,
821 'config-file':
822 _file_v2(os.path.join(project_name, config_path), destination),
David Burger8ee9b4d2020-06-16 17:40:21 -0600823 }
824 return result
825
826
David Burger3abda442020-08-06 16:15:59 -0600827def _config_map(configs, project_name, config_dir, config_file, system_dir):
David Burger2f0d9522020-07-30 10:52:28 -0600828 """Produces a config map for the given configs.
829
830 Produces a map that maps from design name to the config file for that
831 design. It looks for the config files at:
832 config_dir + '/' + config_file
833 for a project wide config, that it maps under the empty string, and at:
834 config_dir + '/' + design_name + '/' + config_file
835 for design specific configs that it maps under the design name.
836
837 Args:
838 configs: Source ConfigBundle to process.
David Burger3abda442020-08-06 16:15:59 -0600839 project_name: Name of project processing for.
David Burger2f0d9522020-07-30 10:52:28 -0600840 config_dir: Path to the directory containing configuration files.
841 config_file: Name of the configuration files.
842 system_dir: Base directory for the output system path.
843
844 Returns:
845 map from design name or empty string (project wide), to config.
846 """
847 result = {}
848 # Looking at top level for project wide, and then for each design name
849 # for design specific.
Sean McAllistera3f7df42020-08-04 18:24:02 -0600850 dirs = [""] + [d.name for d in configs.design_list]
David Burger2f0d9522020-07-30 10:52:28 -0600851 for directory in dirs:
852 design = directory.lower()
David Burger3abda442020-08-06 16:15:59 -0600853 config_file_path = os.path.join(config_dir, design, config_file)
854 if os.path.exists(config_file_path):
855 build_path = os.path.join(project_name, config_file_path)
David Burger2f0d9522020-07-30 10:52:28 -0600856 if design:
857 system_file = config_file.replace('.', '_{}.'.format(design))
858 else:
859 system_file = config_file
David Burger3abda442020-08-06 16:15:59 -0600860 system_path = os.path.join(system_dir, project_name, system_file)
David Burger2f0d9522020-07-30 10:52:28 -0600861 result[directory] = _file_v2(build_path, system_path)
862 return result
863
864
David Burger52c9d322020-06-09 07:16:18 -0600865def _dptf_map(configs, project_name):
866 """Produces a dptf map for the given configs.
867
868 Produces a map that maps from design name to the dptf file config for that
869 design. It looks for the dptf files at:
David Burger2f0d9522020-07-30 10:52:28 -0600870 DPTF_PATH + '/' + DPTF_FILE
David Burger52c9d322020-06-09 07:16:18 -0600871 for a project wide config, that it maps under the empty string, and at:
David Burger2f0d9522020-07-30 10:52:28 -0600872 DPTF_PATH + '/' + design_name + '/' + DPTF_FILE
David Burger52c9d322020-06-09 07:16:18 -0600873 for design specific configs that it maps under the design name.
874
875 Args:
876 configs: Source ConfigBundle to process.
877 project_name: Name of project processing for.
878
879 Returns:
David Burger8ee9b4d2020-06-16 17:40:21 -0600880 map from design name or empty string (project wide), to dptf config.
David Burger52c9d322020-06-09 07:16:18 -0600881 """
882 result = {}
David Burger52c9d322020-06-09 07:16:18 -0600883 # Looking at top level for project wide, and then for each design name
884 # for design specific.
Sean McAllisterf66887b2020-08-03 14:00:51 -0600885 dirs = [""] + [d.name for d in configs.design_list]
David Burger52c9d322020-06-09 07:16:18 -0600886 for directory in dirs:
David Burgera2252762020-07-09 15:09:49 -0600887 design = directory.lower()
888 if os.path.exists(os.path.join(DPTF_PATH, design, DPTF_FILE)):
David Burger2f0d9522020-07-30 10:52:28 -0600889 project_dptf_path = os.path.join(project_name, design, DPTF_FILE)
David Burger52c9d322020-06-09 07:16:18 -0600890 dptf_file = {
891 'dptf-dv':
892 project_dptf_path,
893 'files': [
894 _file(
David Burgera2252762020-07-09 15:09:49 -0600895 os.path.join(project_name, DPTF_PATH, design, DPTF_FILE),
David Burger52c9d322020-06-09 07:16:18 -0600896 os.path.join('/etc/dptf', project_dptf_path))
897 ]
898 }
899 result[directory] = dptf_file
900 return result
901
902
Andrew Lambcd33f702020-06-11 10:45:16 -0600903def Main(project_configs, program_config, output): # pylint: disable=invalid-name
David Burger7fd1dbe2020-03-26 09:26:55 -0600904 """Transforms source proto config into platform JSON.
905
906 Args:
907 project_configs: List of source project configs to transform.
908 program_config: Program config for the given set of projects.
909 output: Output file that will be generated by the transform.
910 """
Andrew Lambcd33f702020-06-11 10:45:16 -0600911 configs = _merge_configs([_read_config(program_config)] +
912 [_read_config(config) for config in project_configs])
C Shapiro5bf23a72020-04-24 11:40:17 -0500913 arc_hw_feature_files = {}
C Shapiro2b6d5332020-05-06 17:51:35 -0500914 touch_fw = {}
David Burger2f0d9522020-07-30 10:52:28 -0600915 arc_camera_map = {}
David Burger52c9d322020-06-09 07:16:18 -0600916 dptf_map = {}
David Burger8ee9b4d2020-06-16 17:40:21 -0600917 camera_map = {}
C Shapiro5bf23a72020-04-24 11:40:17 -0500918 output_dir = os.path.dirname(output)
C Shapiro5c877992020-04-29 12:11:28 -0500919 build_root_dir = output_dir
C Shapiro5c877992020-04-29 12:11:28 -0500920 if 'sw_build_config' in output_dir:
921 full_path = os.path.realpath(output)
Andrew Lamb2413c982020-05-29 12:15:36 -0600922 project_name = re.match(r'.*/(\w*)/sw_build_config/.*',
923 full_path).groups(1)[0]
C Shapiro5c877992020-04-29 12:11:28 -0500924 # Projects don't know about each other until they are integrated into the
925 # build system. When this happens, the files need to be able to co-exist
926 # without any collisions. This prefixes the project name (which is how
927 # portage maps in the project), so project files co-exist and can be
928 # installed together.
929 # This is necessary to allow projects to share files at the program level
930 # without having portage file installation collisions.
931 build_root_dir = os.path.join(project_name, output_dir)
C Shapiro6830e6c2020-04-29 13:29:56 -0500932
David Burger3abda442020-08-06 16:15:59 -0600933 arc_camera_map = _config_map(configs, project_name, ARC_CONFIG_PATH,
David Burger2f0d9522020-07-30 10:52:28 -0600934 ARC_CAMERA_CHARACTERISTICS_FILE, '/etc/arc')
David Burger1ba78a22020-06-18 18:42:47 -0600935 camera_map = _camera_map(configs, project_name)
David Burger52c9d322020-06-09 07:16:18 -0600936 dptf_map = _dptf_map(configs, project_name)
937
C Shapiro2b6d5332020-05-06 17:51:35 -0500938 if os.path.exists(TOUCH_PATH):
Andrew Lambcd33f702020-06-11 10:45:16 -0600939 touch_fw = _build_touch_file_config(configs, project_name)
Andrew Lambcd33f702020-06-11 10:45:16 -0600940 arc_hw_feature_files = _write_arc_hardware_feature_files(
941 configs, output_dir, build_root_dir)
C Shapiro5bf23a72020-04-24 11:40:17 -0500942 config_files = ConfigFiles(
C Shapiro5bf23a72020-04-24 11:40:17 -0500943 arc_hw_features=arc_hw_feature_files,
C Shapiro2b6d5332020-05-06 17:51:35 -0500944 touch_fw=touch_fw,
David Burger8ee9b4d2020-06-16 17:40:21 -0600945 dptf_map=dptf_map,
David Burger2f0d9522020-07-30 10:52:28 -0600946 camera_map=camera_map,
947 arc_camera_map=arc_camera_map)
Andrew Lambcd33f702020-06-11 10:45:16 -0600948 write_output(_transform_build_configs(configs, config_files), output)
David Burger7fd1dbe2020-03-26 09:26:55 -0600949
950
951def main(argv=None):
952 """Main program which parses args and runs
953
954 Args:
955 argv: List of command line arguments, if None uses sys.argv.
956 """
957 if argv is None:
958 argv = sys.argv[1:]
Andrew Lambcd33f702020-06-11 10:45:16 -0600959 opts = parse_args(argv)
David Burger7fd1dbe2020-03-26 09:26:55 -0600960 Main(opts.project_configs, opts.program_config, opts.output)
961
962
963if __name__ == '__main__':
964 sys.exit(main(sys.argv[1:]))