blob: aa933831dbbc4b32dfb4ad98d5318a1e4fc556a2 [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
Trent Beginf067ccb2020-08-12 12:33:53 -0600269def _build_hardware_properties(hw_topology):
270 if not hw_topology.HasField('form_factor'):
271 return None
272
273 form_factor = hw_topology.form_factor.hardware_feature.form_factor.form_factor
274 result = {}
275 if form_factor in [
276 topology_pb2.HardwareFeatures.FormFactor.CHROMEBIT,
277 topology_pb2.HardwareFeatures.FormFactor.CHROMEBASE,
278 topology_pb2.HardwareFeatures.FormFactor.CHROMEBOX
279 ]:
280 result['psu-type'] = "AC_only"
281 else:
282 result['psu-type'] = "battery"
283
284 result['has-backlight'] = form_factor not in [
285 topology_pb2.HardwareFeatures.FormFactor.CHROMEBIT,
286 topology_pb2.HardwareFeatures.FormFactor.CHROMEBOX
287 ]
288
289 return result
290
291
Andrew Lambcd33f702020-06-11 10:45:16 -0600292def _fw_bcs_path(payload):
David Burger7fd1dbe2020-03-26 09:26:55 -0600293 if payload and payload.firmware_image_name:
Andrew Lamb2413c982020-05-29 12:15:36 -0600294 return 'bcs://%s.%d.%d.0.tbz2' % (payload.firmware_image_name,
295 payload.version.major,
296 payload.version.minor)
David Burger7fd1dbe2020-03-26 09:26:55 -0600297
Andrew Lambcd33f702020-06-11 10:45:16 -0600298 return None
David Burger7fd1dbe2020-03-26 09:26:55 -0600299
Andrew Lambcd33f702020-06-11 10:45:16 -0600300
301def _fw_build_target(payload):
David Burger7fd1dbe2020-03-26 09:26:55 -0600302 if payload:
303 return payload.build_target_name
304
Andrew Lambcd33f702020-06-11 10:45:16 -0600305 return None
David Burger7fd1dbe2020-03-26 09:26:55 -0600306
Andrew Lambcd33f702020-06-11 10:45:16 -0600307
308def _build_firmware(config):
David Burgerb70b6762020-05-21 12:14:59 -0600309 """Returns firmware config, or None if no build targets."""
Andrew Lamb3da156d2020-04-16 16:00:56 -0600310 fw_payload_config = config.sw_config.firmware
311 fw_build_config = config.sw_config.firmware_build_config
312 main_ro = fw_payload_config.main_ro_payload
313 main_rw = fw_payload_config.main_rw_payload
314 ec_ro = fw_payload_config.ec_ro_payload
315 pd_ro = fw_payload_config.pd_ro_payload
David Burger7fd1dbe2020-03-26 09:26:55 -0600316
317 build_targets = {}
Andrew Lamb3da156d2020-04-16 16:00:56 -0600318
David Burger8ee9b4d2020-06-16 17:40:21 -0600319 _upsert(fw_build_config.build_targets.depthcharge, build_targets,
320 'depthcharge')
321 _upsert(fw_build_config.build_targets.coreboot, build_targets, 'coreboot')
322 _upsert(fw_build_config.build_targets.ec, build_targets, 'ec')
323 _upsert(
Andrew Lambf8954ee2020-04-21 10:24:40 -0600324 list(fw_build_config.build_targets.ec_extras), build_targets, 'ec_extras')
David Burger8ee9b4d2020-06-16 17:40:21 -0600325 _upsert(fw_build_config.build_targets.libpayload, build_targets, 'libpayload')
David Burger7fd1dbe2020-03-26 09:26:55 -0600326
David Burgerb70b6762020-05-21 12:14:59 -0600327 if not build_targets:
328 return None
329
David Burger7fd1dbe2020-03-26 09:26:55 -0600330 result = {
331 'bcs-overlay': config.build_target.overlay_name,
332 'build-targets': build_targets,
David Burger7fd1dbe2020-03-26 09:26:55 -0600333 }
Andrew Lamb883fa042020-04-06 11:37:22 -0600334
David Burger8ee9b4d2020-06-16 17:40:21 -0600335 _upsert(main_ro.firmware_image_name.lower(), result, 'image-name')
Andrew Lamb883fa042020-04-06 11:37:22 -0600336
David Burger8ee9b4d2020-06-16 17:40:21 -0600337 _upsert(_fw_bcs_path(main_ro), result, 'main-ro-image')
338 _upsert(_fw_bcs_path(main_rw), result, 'main-rw-image')
339 _upsert(_fw_bcs_path(ec_ro), result, 'ec-ro-image')
340 _upsert(_fw_bcs_path(pd_ro), result, 'pd-ro-image')
David Burger7fd1dbe2020-03-26 09:26:55 -0600341
David Burger8ee9b4d2020-06-16 17:40:21 -0600342 _upsert(
Andrew Lambf39fbe82020-04-13 16:14:33 -0600343 config.hw_design_config.hardware_features.fw_config.value,
344 result,
345 'firmware-config',
346 )
347
David Burger7fd1dbe2020-03-26 09:26:55 -0600348 return result
349
350
Andrew Lambcd33f702020-06-11 10:45:16 -0600351def _build_fw_signing(config):
C Shapiro2f0bb5d2020-04-14 10:07:47 -0500352 if config.sw_config.firmware and config.device_signer_config:
David Burger68e0d142020-05-15 17:29:33 -0600353 hw_design = config.hw_design.name.lower()
Sam McNally2fc807f2020-07-16 18:13:53 +1000354 brand_scan_config = config.brand_config.scan_config
355 if brand_scan_config and brand_scan_config.whitelabel_tag:
356 signature_id = '%s-%s' % (hw_design, brand_scan_config.whitelabel_tag)
357 else:
358 signature_id = hw_design
359
C Shapiro2f0bb5d2020-04-14 10:07:47 -0500360 return {
361 'key-id': config.device_signer_config.key_id,
Sam McNally2fc807f2020-07-16 18:13:53 +1000362 'signature-id': signature_id,
C Shapiro2f0bb5d2020-04-14 10:07:47 -0500363 }
364 return {}
David Burger7fd1dbe2020-03-26 09:26:55 -0600365
366
Andrew Lambcd33f702020-06-11 10:45:16 -0600367def _file(source, destination):
Andrew Lamb2413c982020-05-29 12:15:36 -0600368 return {'destination': destination, 'source': source}
David Burger7fd1dbe2020-03-26 09:26:55 -0600369
370
David Burger40dfe3a2020-06-18 17:09:13 -0600371def _file_v2(build_path, system_path):
372 return {'build-path': build_path, 'system-path': system_path}
373
374
Andrew Lambcd33f702020-06-11 10:45:16 -0600375def _build_audio(config):
David Burger178f3ef2020-06-26 12:11:57 -0600376 if not config.sw_config.audio_configs:
377 return {}
David Burger7fd1dbe2020-03-26 09:26:55 -0600378 alsa_path = '/usr/share/alsa/ucm'
379 cras_path = '/etc/cras'
380 project_name = config.hw_design.name.lower()
David Burger43250662020-05-07 11:21:50 -0600381 program_name = config.program.name.lower()
David Burger7fd1dbe2020-03-26 09:26:55 -0600382 files = []
David Burger178f3ef2020-06-26 12:11:57 -0600383 ucm_suffix = None
384 for audio in config.sw_config.audio_configs:
385 card = audio.card_name
386 card_with_suffix = audio.card_name
387 if audio.ucm_suffix:
388 # TODO: last ucm_suffix wins.
389 ucm_suffix = audio.ucm_suffix
390 card_with_suffix += '.' + audio.ucm_suffix
391 if audio.ucm_file:
392 files.append(
393 _file(audio.ucm_file,
394 '%s/%s/HiFi.conf' % (alsa_path, card_with_suffix)))
395 if audio.ucm_master_file:
396 files.append(
397 _file(
398 audio.ucm_master_file, '%s/%s/%s.conf' %
Andrew Lamb2413c982020-05-29 12:15:36 -0600399 (alsa_path, card_with_suffix, card_with_suffix)))
David Burger178f3ef2020-06-26 12:11:57 -0600400 if audio.card_config_file:
401 files.append(
402 _file(audio.card_config_file,
403 '%s/%s/%s' % (cras_path, project_name, card)))
404 if audio.dsp_file:
405 files.append(
406 _file(audio.dsp_file, '%s/%s/dsp.ini' % (cras_path, project_name)))
407 if audio.module_file:
408 files.append(
409 _file(audio.module_file,
410 '/etc/modprobe.d/alsa-%s.conf' % program_name))
411 if audio.board_file:
412 files.append(
413 _file(audio.board_file,
414 '%s/%s/board.ini' % (cras_path, project_name)))
David Burger599ff7b2020-04-06 16:29:31 -0600415
416 result = {
David Burger7fd1dbe2020-03-26 09:26:55 -0600417 'main': {
418 'cras-config-dir': project_name,
419 'files': files,
420 }
421 }
David Burger178f3ef2020-06-26 12:11:57 -0600422
423 if ucm_suffix:
424 result['main']['ucm-suffix'] = ucm_suffix
David Burger599ff7b2020-04-06 16:29:31 -0600425
426 return result
David Burger7fd1dbe2020-03-26 09:26:55 -0600427
428
Andrew Lambcd33f702020-06-11 10:45:16 -0600429def _build_camera(hw_topology):
David Burger8aa8fa32020-04-14 08:30:34 -0600430 if hw_topology.HasField('camera'):
431 camera = hw_topology.camera.hardware_feature.camera
432 result = {}
433 if camera.count.value:
434 result['count'] = camera.count.value
435 return result
436
Andrew Lambcd33f702020-06-11 10:45:16 -0600437 return None
David Burger8aa8fa32020-04-14 08:30:34 -0600438
Andrew Lambcd33f702020-06-11 10:45:16 -0600439
440def _build_identity(hw_scan_config, program, brand_scan_config=None):
David Burger7fd1dbe2020-03-26 09:26:55 -0600441 identity = {}
David Burger8ee9b4d2020-06-16 17:40:21 -0600442 _upsert(hw_scan_config.firmware_sku, identity, 'sku-id')
443 _upsert(hw_scan_config.smbios_name_match, identity, 'smbios-name-match')
Andrew Lamb7806ce92020-04-07 10:22:17 -0600444 # 'platform-name' is needed to support 'mosys platform name'. Clients should
445 # longer require platform name, but set it here for backwards compatibility.
David Burger8ee9b4d2020-06-16 17:40:21 -0600446 _upsert(program.name, identity, 'platform-name')
David Burger7fd1dbe2020-03-26 09:26:55 -0600447 # ARM architecture
David Burger8ee9b4d2020-06-16 17:40:21 -0600448 _upsert(hw_scan_config.device_tree_compatible_match, identity,
449 'device-tree-compatible-match')
David Burger7fd1dbe2020-03-26 09:26:55 -0600450
451 if brand_scan_config:
David Burger8ee9b4d2020-06-16 17:40:21 -0600452 _upsert(brand_scan_config.whitelabel_tag, identity, 'whitelabel-tag')
David Burger7fd1dbe2020-03-26 09:26:55 -0600453
454 return identity
455
456
Andrew Lambcd33f702020-06-11 10:45:16 -0600457def _lookup(id_value, id_map):
458 if not id_value.value:
459 return None
460
461 key = id_value.value
462 if key in id_map:
463 return id_map[id_value.value]
464 error = 'Failed to lookup %s with value: %s' % (
465 id_value.__class__.__name__.replace('Id', ''), key)
466 print(error)
467 print('Check the config contents provided:')
468 printer = pprint.PrettyPrinter(indent=4)
469 printer.pprint(id_map)
470 raise Exception(error)
David Burger7fd1dbe2020-03-26 09:26:55 -0600471
472
Andrew Lambcd33f702020-06-11 10:45:16 -0600473def _build_touch_file_config(config, project_name):
Sean McAllistereaf10b72020-08-03 13:41:06 -0600474 partners = {x.id.value: x for x in config.partner_list}
C Shapiro2b6d5332020-05-06 17:51:35 -0500475 files = []
476 for comp in config.components:
C Shapiro4813be62020-05-13 17:31:58 -0500477 touch = comp.touchscreen
478 # Everything is the same for Touch screen/pad, except different fields
479 if comp.HasField('touchpad'):
480 touch = comp.touchpad
481 if touch.product_id:
Andrew Lambcd33f702020-06-11 10:45:16 -0600482 vendor = _lookup(comp.manufacturer_id, partners)
C Shapiro2b6d5332020-05-06 17:51:35 -0500483 if not vendor:
Andrew Lamb2413c982020-05-29 12:15:36 -0600484 raise Exception("Manufacturer must be set for touch device %s" %
485 comp.id.value)
C Shapiro2b6d5332020-05-06 17:51:35 -0500486
C Shapiro4813be62020-05-13 17:31:58 -0500487 product_id = touch.product_id
488 fw_version = touch.fw_version
C Shapiro2b6d5332020-05-06 17:51:35 -0500489
C Shapiro2b6d5332020-05-06 17:51:35 -0500490 file_name = "%s_%s.bin" % (product_id, fw_version)
491 fw_file_path = os.path.join(TOUCH_PATH, vendor.name, file_name)
492
493 if not os.path.exists(fw_file_path):
Andrew Lamb2413c982020-05-29 12:15:36 -0600494 raise Exception("Touchscreen fw bin file doesn't exist at: %s" %
495 fw_file_path)
C Shapiro2b6d5332020-05-06 17:51:35 -0500496
C Shapiro303cece2020-07-22 07:15:21 -0500497 touch_vendor = vendor.touch_vendor
498 sym_link = touch_vendor.symlink_file_format.format(
499 vendor_name=vendor.name,
500 vendor_id=touch_vendor.vendor_id,
501 product_id=product_id,
502 fw_version=fw_version,
503 product_series=touch.product_series)
504
505 dest = "%s_%s" % (vendor.name, file_name)
506 if touch_vendor.destination_file_format:
507 dest = touch_vendor.destination_file_format.format(
508 vendor_name=vendor.name,
509 vendor_id=touch_vendor.vendor_id,
510 product_id=product_id,
511 fw_version=fw_version,
512 product_series=touch.product_series)
513
C Shapiro2b6d5332020-05-06 17:51:35 -0500514 files.append({
C Shapiro303cece2020-07-22 07:15:21 -0500515 "destination": os.path.join("/opt/google/touch/firmware", dest),
YH Lin9160fc52020-07-22 16:35:28 -0700516 "source": os.path.join(project_name, fw_file_path),
517 "symlink": os.path.join("/lib/firmware", sym_link),
C Shapiro2b6d5332020-05-06 17:51:35 -0500518 })
519
520 result = {}
David Burger8ee9b4d2020-06-16 17:40:21 -0600521 _upsert(files, result, 'files')
C Shapiro2b6d5332020-05-06 17:51:35 -0500522 return result
523
524
David Burger8ee9b4d2020-06-16 17:40:21 -0600525def _transform_build_configs(config,
David Burger07af5242020-08-11 11:08:25 -0600526 config_files=ConfigFiles({}, {}, {}, {}, {})):
Andrew Lambcd33f702020-06-11 10:45:16 -0600527 # pylint: disable=too-many-locals,too-many-branches
Sean McAllistereaf10b72020-08-03 13:41:06 -0600528 partners = {x.id.value: x for x in config.partner_list}
Sean McAllisterf38d1e92020-08-03 13:57:53 -0600529 programs = {x.id.value: x for x in config.program_list}
David Burger7fd1dbe2020-03-26 09:26:55 -0600530 sw_configs = list(config.software_configs)
Andrew Lambcd33f702020-06-11 10:45:16 -0600531 brand_configs = {x.brand_id.value: x for x in config.brand_configs}
David Burger7fd1dbe2020-03-26 09:26:55 -0600532
C Shapiroa0b766c2020-03-31 08:35:28 -0500533 if len(config.build_targets) != 1:
534 # Artifact of sharing the config_bundle for analysis and transforms.
535 # Integrated analysis of multiple programs/projects it the only time
536 # having multiple build targets would be valid.
537 raise Exception('Single build_target required for transform')
538
David Burger7fd1dbe2020-03-26 09:26:55 -0600539 results = {}
Sean McAllisterf66887b2020-08-03 14:00:51 -0600540 for hw_design in config.design_list:
Sean McAllister6cbb0ec2020-08-03 14:03:37 -0600541 if config.device_brand_list:
Andrew Lamb2413c982020-05-29 12:15:36 -0600542 device_brands = [
Sean McAllister6cbb0ec2020-08-03 14:03:37 -0600543 x for x in config.device_brand_list
Andrew Lamb2413c982020-05-29 12:15:36 -0600544 if x.design_id.value == hw_design.id.value
545 ]
David Burger7fd1dbe2020-03-26 09:26:55 -0600546 else:
547 device_brands = [device_brand_pb2.DeviceBrand()]
548
549 for device_brand in device_brands:
550 # Brand config can be empty since platform JSON config allows it
551 brand_config = brand_config_pb2.BrandConfig()
552 if device_brand.id.value in brand_configs:
553 brand_config = brand_configs[device_brand.id.value]
554
555 for hw_design_config in hw_design.configs:
556 design_id = hw_design_config.id.value
Andrew Lamb2413c982020-05-29 12:15:36 -0600557 sw_config_matches = [
558 x for x in sw_configs if x.design_config_id.value == design_id
559 ]
David Burger7fd1dbe2020-03-26 09:26:55 -0600560 if len(sw_config_matches) == 1:
561 sw_config = sw_config_matches[0]
562 elif len(sw_config_matches) > 1:
563 raise Exception('Multiple software configs found for: %s' % design_id)
564 else:
565 raise Exception('Software config is required for: %s' % design_id)
566
Andrew Lambcd33f702020-06-11 10:45:16 -0600567 program = _lookup(hw_design.program_id, programs)
C Shapiroadefd7c2020-05-19 16:37:21 -0500568 signer_configs_by_design = {}
569 signer_configs_by_brand = {}
570 for signer_config in program.device_signer_configs:
571 design_id = signer_config.design_id.value
572 brand_id = signer_config.brand_id.value
573 if design_id:
574 signer_configs_by_design[design_id] = signer_config
575 elif brand_id:
576 signer_configs_by_brand[brand_id] = signer_config
577 else:
578 raise Exception('No ID found for signer config: %s' % signer_config)
579
C Shapiro2f0bb5d2020-04-14 10:07:47 -0500580 device_signer_config = None
C Shapiroadefd7c2020-05-19 16:37:21 -0500581 if signer_configs_by_design or signer_configs_by_brand:
582 design_id = hw_design.id.value
583 brand_id = device_brand.id.value
584 if design_id in signer_configs_by_design:
585 device_signer_config = signer_configs_by_design[design_id]
586 elif brand_id in signer_configs_by_brand:
587 device_signer_config = signer_configs_by_brand[brand_id]
588 else:
589 # Assume that if signer configs are set, every config is setup
Andrew Lamb2413c982020-05-29 12:15:36 -0600590 raise Exception('Signer config missing for design: %s, brand: %s' %
591 (design_id, brand_id))
C Shapiro2f0bb5d2020-04-14 10:07:47 -0500592
Andrew Lambcd33f702020-06-11 10:45:16 -0600593 transformed_config = _transform_build_config(
C Shapiro90fda252020-04-17 14:34:57 -0500594 Config(
595 program=program,
596 hw_design=hw_design,
Andrew Lambcd33f702020-06-11 10:45:16 -0600597 odm=_lookup(hw_design.odm_id, partners),
C Shapiro90fda252020-04-17 14:34:57 -0500598 hw_design_config=hw_design_config,
599 device_brand=device_brand,
600 device_signer_config=device_signer_config,
Andrew Lambcd33f702020-06-11 10:45:16 -0600601 oem=_lookup(device_brand.oem_id, partners),
C Shapiro90fda252020-04-17 14:34:57 -0500602 sw_config=sw_config,
603 brand_config=brand_config,
Andrew Lamb2413c982020-05-29 12:15:36 -0600604 build_target=config.build_targets[0]), config_files)
David Burger7fd1dbe2020-03-26 09:26:55 -0600605
Andrew Lamb2413c982020-05-29 12:15:36 -0600606 config_json = json.dumps(
607 transformed_config,
608 sort_keys=True,
609 indent=2,
610 separators=(',', ': '))
David Burger7fd1dbe2020-03-26 09:26:55 -0600611
612 if config_json not in results:
613 results[config_json] = transformed_config
614
615 return list(results.values())
616
617
Andrew Lambcd33f702020-06-11 10:45:16 -0600618def _transform_build_config(config, config_files):
David Burger7fd1dbe2020-03-26 09:26:55 -0600619 """Transforms Config instance into target platform JSON schema.
620
621 Args:
622 config: Config namedtuple
C Shapiro5bf23a72020-04-24 11:40:17 -0500623 config_files: Map to look up the generated config files.
David Burger7fd1dbe2020-03-26 09:26:55 -0600624
625 Returns:
626 Unique config payload based on the platform JSON schema.
627 """
628 result = {
Andrew Lamb2413c982020-05-29 12:15:36 -0600629 'identity':
Andrew Lambcd33f702020-06-11 10:45:16 -0600630 _build_identity(config.sw_config.id_scan_config, config.program,
631 config.brand_config.scan_config),
Andrew Lamb2413c982020-05-29 12:15:36 -0600632 'name':
633 config.hw_design.name.lower(),
David Burger7fd1dbe2020-03-26 09:26:55 -0600634 }
635
David Burger8ee9b4d2020-06-16 17:40:21 -0600636 _upsert(_build_arc(config, config_files), result, 'arc')
637 _upsert(_build_audio(config), result, 'audio')
David Burger07af5242020-08-11 11:08:25 -0600638 _upsert(_build_bluetooth(config), result, 'bluetooth')
David Burgerec753912020-08-10 12:59:11 -0600639 _upsert(_build_wifi(config), result, 'wifi')
Andrew Lambca279902020-08-06 10:13:42 -0600640 _upsert(config.brand_config.wallpaper, result, 'wallpaper')
David Burger8ee9b4d2020-06-16 17:40:21 -0600641 _upsert(config.device_brand.brand_code, result, 'brand-code')
642 _upsert(
Andrew Lambcd33f702020-06-11 10:45:16 -0600643 _build_camera(config.hw_design_config.hardware_topology), result,
644 'camera')
David Burger8ee9b4d2020-06-16 17:40:21 -0600645 _upsert(_build_firmware(config), result, 'firmware')
646 _upsert(_build_fw_signing(config), result, 'firmware-signing')
647 _upsert(
Andrew Lambcd33f702020-06-11 10:45:16 -0600648 _build_fingerprint(config.hw_design_config.hardware_topology), result,
Andrew Lamb2413c982020-05-29 12:15:36 -0600649 'fingerprint')
Andrew Lamb0d236ab2020-06-30 12:30:20 -0600650 _upsert(_build_ui(config), result, 'ui')
David Burger7fd1dbe2020-03-26 09:26:55 -0600651 power_prefs = config.sw_config.power_config.preferences
652 power_prefs_map = dict(
Andrew Lamb2413c982020-05-29 12:15:36 -0600653 (x.replace('_', '-'), power_prefs[x]) for x in power_prefs)
David Burger8ee9b4d2020-06-16 17:40:21 -0600654 _upsert(power_prefs_map, result, 'power')
655 if config_files.camera_map:
656 camera_file = config_files.camera_map.get(config.hw_design.name, {})
657 _upsert(camera_file, result, 'camera')
David Burger52c9d322020-06-09 07:16:18 -0600658 if config_files.dptf_map:
659 # Prefer design specific if found, if not fall back to project wide config
660 # mapped under the empty string.
661 if config_files.dptf_map.get(config.hw_design.name):
662 dptf_file = config_files.dptf_map[config.hw_design.name]
663 else:
664 dptf_file = config_files.dptf_map.get('')
David Burger8ee9b4d2020-06-16 17:40:21 -0600665 _upsert(dptf_file, result, 'thermal')
666 _upsert(config_files.touch_fw, result, 'touch')
Trent Beginf067ccb2020-08-12 12:33:53 -0600667 _upsert(
668 _build_hardware_properties(config.hw_design_config.hardware_topology),
669 result, 'hardware-properties')
David Burger7fd1dbe2020-03-26 09:26:55 -0600670
671 return result
672
673
Andrew Lambcd33f702020-06-11 10:45:16 -0600674def write_output(configs, output=None):
David Burger7fd1dbe2020-03-26 09:26:55 -0600675 """Writes a list of configs to platform JSON format.
676
677 Args:
678 configs: List of config dicts defined in cros_config_schema.yaml
679 output: Target file output (if None, prints to stdout)
680 """
Andrew Lamb2413c982020-05-29 12:15:36 -0600681 json_output = json.dumps({'chromeos': {
682 'configs': configs,
683 }},
684 sort_keys=True,
685 indent=2,
686 separators=(',', ': '))
David Burger7fd1dbe2020-03-26 09:26:55 -0600687 if output:
688 with open(output, 'w') as output_stream:
689 # Using print function adds proper trailing newline.
690 print(json_output, file=output_stream)
691 else:
692 print(json_output)
693
694
Andrew Lambcd33f702020-06-11 10:45:16 -0600695def _feature(name, present):
C Shapiro5bf23a72020-04-24 11:40:17 -0500696 attrib = {'name': name}
697 if present:
698 return etree.Element('feature', attrib=attrib)
Andrew Lambcd33f702020-06-11 10:45:16 -0600699
700 return etree.Element('unavailable-feature', attrib=attrib)
C Shapiro5bf23a72020-04-24 11:40:17 -0500701
702
Andrew Lambcd33f702020-06-11 10:45:16 -0600703def _any_present(features):
Andrew Lamb2413c982020-05-29 12:15:36 -0600704 return topology_pb2.HardwareFeatures.PRESENT in features
C Shapiro5bf23a72020-04-24 11:40:17 -0500705
706
Andrew Lambcd33f702020-06-11 10:45:16 -0600707def _arc_hardware_feature_id(design_config):
C Shapiro5bf23a72020-04-24 11:40:17 -0500708 return design_config.id.value.lower().replace(':', '_')
709
710
Andrew Lambcd33f702020-06-11 10:45:16 -0600711def _write_arc_hardware_feature_file(output_dir, file_name, config_content):
David Burger77a1d312020-05-23 16:05:45 -0600712 output_dir += '/arc'
713 os.makedirs(output_dir, exist_ok=True)
714 output = '%s/%s' % (output_dir, file_name)
Andrew Lamb2413c982020-05-29 12:15:36 -0600715 file_content = minidom.parseString(config_content).toprettyxml(
716 indent=' ', encoding='utf-8')
C Shapiroea33cff2020-05-11 13:32:05 -0500717
718 with open(output, 'wb') as f:
719 f.write(file_content)
720
721
Andrew Lambcd33f702020-06-11 10:45:16 -0600722def _write_arc_hardware_feature_files(config, output_dir, build_root_dir):
C Shapiro5bf23a72020-04-24 11:40:17 -0500723 """Writes ARC hardware_feature.xml files for each config
724
725 Args:
726 config: Source ConfigBundle to process.
727 output_dir: Path to the generated output.
C Shapiro5c877992020-04-29 12:11:28 -0500728 build_root_path: Path to the config file from portage's perspective.
C Shapiro5bf23a72020-04-24 11:40:17 -0500729 Returns:
730 dict that maps the design_config_id onto the correct file.
731 """
Andrew Lambcd33f702020-06-11 10:45:16 -0600732 # pylint: disable=too-many-locals
C Shapiro5bf23a72020-04-24 11:40:17 -0500733 result = {}
C Shapiroea33cff2020-05-11 13:32:05 -0500734 configs_by_design = {}
Sean McAllisterf66887b2020-08-03 14:00:51 -0600735 for hw_design in config.design_list:
C Shapiro5bf23a72020-04-24 11:40:17 -0500736 for design_config in hw_design.configs:
737 hw_features = design_config.hardware_features
Kazuhiro Inaba76d8bb52020-06-26 10:34:07 +0900738 any_camera = hw_features.camera.count.value > 0
Josie Nordrumadc27a72020-07-08 11:05:26 -0600739 multi_camera = hw_features.camera.count.value > 1
Andrew Lambcd33f702020-06-11 10:45:16 -0600740 touchscreen = _any_present([hw_features.screen.touch_support])
C Shapiro5bf23a72020-04-24 11:40:17 -0500741 acc = hw_features.accelerometer
742 gyro = hw_features.gyroscope
743 compass = hw_features.magnetometer
Andrew Lambcd33f702020-06-11 10:45:16 -0600744 light_sensor = hw_features.light_sensor
C Shapiro5bf23a72020-04-24 11:40:17 -0500745 root = etree.Element('permissions')
746 root.extend([
Andrew Lambcd33f702020-06-11 10:45:16 -0600747 _feature('android.hardware.camera', multi_camera),
748 _feature('android.hardware.camera.autofocus', multi_camera),
Kazuhiro Inaba76d8bb52020-06-26 10:34:07 +0900749 _feature('android.hardware.camera.any', any_camera),
750 _feature('android.hardware.camera.front', any_camera),
Andrew Lambcd33f702020-06-11 10:45:16 -0600751 _feature(
752 'android.hardware.sensor.accelerometer',
753 _any_present([acc.lid_accelerometer, acc.base_accelerometer])),
754 _feature('android.hardware.sensor.gyroscope',
755 _any_present([gyro.lid_gyroscope, gyro.base_gyroscope])),
756 _feature(
Andrew Lamb2413c982020-05-29 12:15:36 -0600757 'android.hardware.sensor.compass',
Andrew Lambcd33f702020-06-11 10:45:16 -0600758 _any_present(
759 [compass.lid_magnetometer, compass.base_magnetometer])),
760 _feature(
761 'android.hardware.sensor.light',
762 _any_present(
763 [light_sensor.lid_lightsensor,
764 light_sensor.base_lightsensor])),
765 _feature('android.hardware.touchscreen', touchscreen),
766 _feature('android.hardware.touchscreen.multitouch', touchscreen),
767 _feature('android.hardware.touchscreen.multitouch.distinct',
Andrew Lamb2413c982020-05-29 12:15:36 -0600768 touchscreen),
Andrew Lambcd33f702020-06-11 10:45:16 -0600769 _feature('android.hardware.touchscreen.multitouch.jazzhand',
Andrew Lamb2413c982020-05-29 12:15:36 -0600770 touchscreen),
C Shapiro5bf23a72020-04-24 11:40:17 -0500771 ])
772
C Shapiroea33cff2020-05-11 13:32:05 -0500773 design_name = hw_design.name.lower()
C Shapiro5bf23a72020-04-24 11:40:17 -0500774
C Shapiroea33cff2020-05-11 13:32:05 -0500775 # Constructs the following map:
776 # design_name -> config -> design_configs
777 # This allows any of the following file naming schemes:
778 # - All configs within a design share config (design_name prefix only)
779 # - Nobody shares (full design_name and config id prefix needed)
780 #
781 # Having shared configs when possible makes code reviews easier around
782 # the configs and makes debugging easier on the platform side.
783 config_content = etree.tostring(root)
784 arc_configs = configs_by_design.get(design_name, {})
785 design_configs = arc_configs.get(config_content, [])
786 design_configs.append(design_config)
787 arc_configs[config_content] = design_configs
788 configs_by_design[design_name] = arc_configs
C Shapiro9a3ac8c2020-04-25 07:49:21 -0500789
C Shapiroea33cff2020-05-11 13:32:05 -0500790 for design_name, unique_configs in configs_by_design.items():
791 for file_content, design_configs in unique_configs.items():
Andrew Lamb2413c982020-05-29 12:15:36 -0600792 file_name = 'hardware_features_%s.xml' % design_name
793 if len(unique_configs) == 1:
Andrew Lambcd33f702020-06-11 10:45:16 -0600794 _write_arc_hardware_feature_file(output_dir, file_name, file_content)
C Shapiro9a3ac8c2020-04-25 07:49:21 -0500795
Andrew Lamb2413c982020-05-29 12:15:36 -0600796 for design_config in design_configs:
Andrew Lambcd33f702020-06-11 10:45:16 -0600797 feature_id = _arc_hardware_feature_id(design_config)
Andrew Lamb2413c982020-05-29 12:15:36 -0600798 if len(unique_configs) > 1:
799 file_name = 'hardware_features_%s.xml' % feature_id
Andrew Lambcd33f702020-06-11 10:45:16 -0600800 _write_arc_hardware_feature_file(output_dir, file_name, file_content)
David Burger40dfe3a2020-06-18 17:09:13 -0600801 result[feature_id] = _file_v2('%s/arc/%s' % (build_root_dir, file_name),
802 '/etc/%s' % file_name)
C Shapiro5bf23a72020-04-24 11:40:17 -0500803 return result
804
805
Andrew Lambcd33f702020-06-11 10:45:16 -0600806def _read_config(path):
David Burgerd4f32962020-05-02 12:07:40 -0600807 """Reads a ConfigBundle proto from a json pb file.
David Burgere6f76222020-04-27 11:08:01 -0600808
809 Args:
David Burgerd4f32962020-05-02 12:07:40 -0600810 path: Path to the file encoding the json pb proto.
David Burgere6f76222020-04-27 11:08:01 -0600811 """
812 config = config_bundle_pb2.ConfigBundle()
813 with open(path, 'r') as f:
814 return json_format.Parse(f.read(), config)
815
816
Andrew Lambcd33f702020-06-11 10:45:16 -0600817def _merge_configs(configs):
David Burger7fd1dbe2020-03-26 09:26:55 -0600818 result = config_bundle_pb2.ConfigBundle()
819 for config in configs:
820 result.MergeFrom(config)
821
822 return result
823
824
David Burger1ba78a22020-06-18 18:42:47 -0600825def _camera_map(configs, project_name):
David Burger8ee9b4d2020-06-16 17:40:21 -0600826 """Produces a camera config map for the given configs.
827
828 Produces a map that maps from the design name to the camera config for that
829 design.
830
831 Args:
832 configs: Source ConfigBundle to process.
David Burger1ba78a22020-06-18 18:42:47 -0600833 project_name: Name of project processing for.
David Burger8ee9b4d2020-06-16 17:40:21 -0600834
835 Returns:
836 map from design name to camera config.
837 """
838 result = {}
Sean McAllisterf66887b2020-08-03 14:00:51 -0600839 for design in configs.design_list:
David Burger8ee9b4d2020-06-16 17:40:21 -0600840 design_name = design.name
David Burger0d9e8462020-06-19 14:12:37 -0600841 config_path = CAMERA_CONFIG_SOURCE_PATH_TEMPLATE.format(design_name.lower())
David Burger8ee9b4d2020-06-16 17:40:21 -0600842 if os.path.exists(config_path):
David Burger0d9e8462020-06-19 14:12:37 -0600843 destination = CAMERA_CONFIG_DEST_PATH_TEMPLATE.format(design_name.lower())
David Burger8ee9b4d2020-06-16 17:40:21 -0600844 result[design_name] = {
David Burger1ba78a22020-06-18 18:42:47 -0600845 'config-path':
846 destination,
847 'config-file':
848 _file_v2(os.path.join(project_name, config_path), destination),
David Burger8ee9b4d2020-06-16 17:40:21 -0600849 }
850 return result
851
852
David Burger3abda442020-08-06 16:15:59 -0600853def _config_map(configs, project_name, config_dir, config_file, system_dir):
David Burger2f0d9522020-07-30 10:52:28 -0600854 """Produces a config map for the given configs.
855
856 Produces a map that maps from design name to the config file for that
857 design. It looks for the config files at:
858 config_dir + '/' + config_file
859 for a project wide config, that it maps under the empty string, and at:
860 config_dir + '/' + design_name + '/' + config_file
861 for design specific configs that it maps under the design name.
862
863 Args:
864 configs: Source ConfigBundle to process.
David Burger3abda442020-08-06 16:15:59 -0600865 project_name: Name of project processing for.
David Burger2f0d9522020-07-30 10:52:28 -0600866 config_dir: Path to the directory containing configuration files.
867 config_file: Name of the configuration files.
868 system_dir: Base directory for the output system path.
869
870 Returns:
871 map from design name or empty string (project wide), to config.
872 """
873 result = {}
874 # Looking at top level for project wide, and then for each design name
875 # for design specific.
Sean McAllistera3f7df42020-08-04 18:24:02 -0600876 dirs = [""] + [d.name for d in configs.design_list]
David Burger2f0d9522020-07-30 10:52:28 -0600877 for directory in dirs:
878 design = directory.lower()
David Burger3abda442020-08-06 16:15:59 -0600879 config_file_path = os.path.join(config_dir, design, config_file)
880 if os.path.exists(config_file_path):
881 build_path = os.path.join(project_name, config_file_path)
David Burger2f0d9522020-07-30 10:52:28 -0600882 if design:
883 system_file = config_file.replace('.', '_{}.'.format(design))
884 else:
885 system_file = config_file
David Burger3abda442020-08-06 16:15:59 -0600886 system_path = os.path.join(system_dir, project_name, system_file)
David Burger2f0d9522020-07-30 10:52:28 -0600887 result[directory] = _file_v2(build_path, system_path)
888 return result
889
890
David Burger52c9d322020-06-09 07:16:18 -0600891def _dptf_map(configs, project_name):
892 """Produces a dptf map for the given configs.
893
894 Produces a map that maps from design name to the dptf file config for that
895 design. It looks for the dptf files at:
David Burger2f0d9522020-07-30 10:52:28 -0600896 DPTF_PATH + '/' + DPTF_FILE
David Burger52c9d322020-06-09 07:16:18 -0600897 for a project wide config, that it maps under the empty string, and at:
David Burger2f0d9522020-07-30 10:52:28 -0600898 DPTF_PATH + '/' + design_name + '/' + DPTF_FILE
David Burger52c9d322020-06-09 07:16:18 -0600899 for design specific configs that it maps under the design name.
900
901 Args:
902 configs: Source ConfigBundle to process.
903 project_name: Name of project processing for.
904
905 Returns:
David Burger8ee9b4d2020-06-16 17:40:21 -0600906 map from design name or empty string (project wide), to dptf config.
David Burger52c9d322020-06-09 07:16:18 -0600907 """
908 result = {}
David Burger52c9d322020-06-09 07:16:18 -0600909 # Looking at top level for project wide, and then for each design name
910 # for design specific.
Sean McAllisterf66887b2020-08-03 14:00:51 -0600911 dirs = [""] + [d.name for d in configs.design_list]
David Burger52c9d322020-06-09 07:16:18 -0600912 for directory in dirs:
David Burgera2252762020-07-09 15:09:49 -0600913 design = directory.lower()
914 if os.path.exists(os.path.join(DPTF_PATH, design, DPTF_FILE)):
David Burger2f0d9522020-07-30 10:52:28 -0600915 project_dptf_path = os.path.join(project_name, design, DPTF_FILE)
David Burger52c9d322020-06-09 07:16:18 -0600916 dptf_file = {
917 'dptf-dv':
918 project_dptf_path,
919 'files': [
920 _file(
David Burgera2252762020-07-09 15:09:49 -0600921 os.path.join(project_name, DPTF_PATH, design, DPTF_FILE),
David Burger52c9d322020-06-09 07:16:18 -0600922 os.path.join('/etc/dptf', project_dptf_path))
923 ]
924 }
925 result[directory] = dptf_file
926 return result
927
928
Andrew Lambcd33f702020-06-11 10:45:16 -0600929def Main(project_configs, program_config, output): # pylint: disable=invalid-name
David Burger7fd1dbe2020-03-26 09:26:55 -0600930 """Transforms source proto config into platform JSON.
931
932 Args:
933 project_configs: List of source project configs to transform.
934 program_config: Program config for the given set of projects.
935 output: Output file that will be generated by the transform.
936 """
Andrew Lambcd33f702020-06-11 10:45:16 -0600937 configs = _merge_configs([_read_config(program_config)] +
938 [_read_config(config) for config in project_configs])
C Shapiro5bf23a72020-04-24 11:40:17 -0500939 arc_hw_feature_files = {}
C Shapiro2b6d5332020-05-06 17:51:35 -0500940 touch_fw = {}
David Burger2f0d9522020-07-30 10:52:28 -0600941 arc_camera_map = {}
David Burger52c9d322020-06-09 07:16:18 -0600942 dptf_map = {}
David Burger8ee9b4d2020-06-16 17:40:21 -0600943 camera_map = {}
C Shapiro5bf23a72020-04-24 11:40:17 -0500944 output_dir = os.path.dirname(output)
C Shapiro5c877992020-04-29 12:11:28 -0500945 build_root_dir = output_dir
C Shapiro5c877992020-04-29 12:11:28 -0500946 if 'sw_build_config' in output_dir:
947 full_path = os.path.realpath(output)
Andrew Lamb2413c982020-05-29 12:15:36 -0600948 project_name = re.match(r'.*/(\w*)/sw_build_config/.*',
949 full_path).groups(1)[0]
C Shapiro5c877992020-04-29 12:11:28 -0500950 # Projects don't know about each other until they are integrated into the
951 # build system. When this happens, the files need to be able to co-exist
952 # without any collisions. This prefixes the project name (which is how
953 # portage maps in the project), so project files co-exist and can be
954 # installed together.
955 # This is necessary to allow projects to share files at the program level
956 # without having portage file installation collisions.
957 build_root_dir = os.path.join(project_name, output_dir)
C Shapiro6830e6c2020-04-29 13:29:56 -0500958
David Burger3abda442020-08-06 16:15:59 -0600959 arc_camera_map = _config_map(configs, project_name, ARC_CONFIG_PATH,
David Burger2f0d9522020-07-30 10:52:28 -0600960 ARC_CAMERA_CHARACTERISTICS_FILE, '/etc/arc')
David Burger1ba78a22020-06-18 18:42:47 -0600961 camera_map = _camera_map(configs, project_name)
David Burger52c9d322020-06-09 07:16:18 -0600962 dptf_map = _dptf_map(configs, project_name)
963
C Shapiro2b6d5332020-05-06 17:51:35 -0500964 if os.path.exists(TOUCH_PATH):
Andrew Lambcd33f702020-06-11 10:45:16 -0600965 touch_fw = _build_touch_file_config(configs, project_name)
Andrew Lambcd33f702020-06-11 10:45:16 -0600966 arc_hw_feature_files = _write_arc_hardware_feature_files(
967 configs, output_dir, build_root_dir)
C Shapiro5bf23a72020-04-24 11:40:17 -0500968 config_files = ConfigFiles(
C Shapiro5bf23a72020-04-24 11:40:17 -0500969 arc_hw_features=arc_hw_feature_files,
C Shapiro2b6d5332020-05-06 17:51:35 -0500970 touch_fw=touch_fw,
David Burger8ee9b4d2020-06-16 17:40:21 -0600971 dptf_map=dptf_map,
David Burger2f0d9522020-07-30 10:52:28 -0600972 camera_map=camera_map,
973 arc_camera_map=arc_camera_map)
Andrew Lambcd33f702020-06-11 10:45:16 -0600974 write_output(_transform_build_configs(configs, config_files), output)
David Burger7fd1dbe2020-03-26 09:26:55 -0600975
976
977def main(argv=None):
978 """Main program which parses args and runs
979
980 Args:
981 argv: List of command line arguments, if None uses sys.argv.
982 """
983 if argv is None:
984 argv = sys.argv[1:]
Andrew Lambcd33f702020-06-11 10:45:16 -0600985 opts = parse_args(argv)
David Burger7fd1dbe2020-03-26 09:26:55 -0600986 Main(opts.project_configs, opts.program_config, opts.output)
987
988
989if __name__ == '__main__':
990 sys.exit(main(sys.argv[1:]))