blob: bd9e3a9bcd02c3ea77fbb2cd25dd6d4e16b4cfd2 [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.
David Burgerec753912020-08-10 12:59:11 -06006# pylint: disable=too-many-lines
David Burger7fd1dbe2020-03-26 09:26:55 -06007"""Transforms config from /config/proto/api proto format to platform JSON."""
8
9import argparse
10import json
11import pprint
C Shapiro90fda252020-04-17 14:34:57 -050012import os
David Burger7fd1dbe2020-03-26 09:26:55 -060013import sys
C Shapiro90fda252020-04-17 14:34:57 -050014import re
C Shapiro5bf23a72020-04-24 11:40:17 -050015import xml.etree.ElementTree as etree
C Shapiro9a3ac8c2020-04-25 07:49:21 -050016import xml.dom.minidom as minidom
David Burger7fd1dbe2020-03-26 09:26:55 -060017
Andrew Lamb319cc922020-06-15 10:45:46 -060018from typing import List
19
David Burger7fd1dbe2020-03-26 09:26:55 -060020from collections import namedtuple
21
Andrew Lambcd33f702020-06-11 10:45:16 -060022from google.protobuf import json_format
23
Prathmesh Prabhu72f8a002020-04-10 09:57:53 -070024from chromiumos.config.api import device_brand_pb2
David Burger92609a32020-04-23 10:38:50 -060025from chromiumos.config.api import topology_pb2
C Shapiro5bf23a72020-04-24 11:40:17 -050026from chromiumos.config.payload import config_bundle_pb2
Prathmesh Prabhu72f8a002020-04-10 09:57:53 -070027from chromiumos.config.api.software import brand_config_pb2
David Burger7fd1dbe2020-03-26 09:26:55 -060028
Andrew Lamb2413c982020-05-29 12:15:36 -060029Config = namedtuple('Config', [
30 'program', 'hw_design', 'odm', 'hw_design_config', 'device_brand',
31 'device_signer_config', 'oem', 'sw_config', 'brand_config', 'build_target'
32])
David Burger7fd1dbe2020-03-26 09:26:55 -060033
David Burger2f0d9522020-07-30 10:52:28 -060034ConfigFiles = namedtuple('ConfigFiles', [
35 'bluetooth', 'arc_hw_features', 'touch_fw', 'dptf_map', 'camera_map',
36 'arc_camera_map'
37])
38
39ARC_CONFIG_PATH = 'sw_build_config/platform/chromeos-config/arc'
40ARC_CAMERA_CHARACTERISTICS_FILE = 'camera_characteristics.conf'
David Burger8ee9b4d2020-06-16 17:40:21 -060041
42CAMERA_CONFIG_DEST_PATH_TEMPLATE = '/etc/camera/camera_config_{}.json'
43CAMERA_CONFIG_SOURCE_PATH_TEMPLATE = (
44 'sw_build_config/platform/chromeos-config/camera/camera_config_{}.json')
C Shapiro5bf23a72020-04-24 11:40:17 -050045
David Burger52c9d322020-06-09 07:16:18 -060046DPTF_PATH = 'sw_build_config/platform/chromeos-config/thermal'
47DPTF_FILE = 'dptf.dv'
David Burger2f0d9522020-07-30 10:52:28 -060048
C Shapiro2b6d5332020-05-06 17:51:35 -050049TOUCH_PATH = 'sw_build_config/platform/chromeos-config/touch'
Andrew Lamb6c42efc2020-06-16 10:40:43 -060050WALLPAPER_BASE_PATH = '/usr/share/chromeos-assets/wallpaper'
David Burger7fd1dbe2020-03-26 09:26:55 -060051
Andrew Lamb2413c982020-05-29 12:15:36 -060052
Andrew Lambcd33f702020-06-11 10:45:16 -060053def parse_args(argv):
David Burger7fd1dbe2020-03-26 09:26:55 -060054 """Parse the available arguments.
55
56 Invalid arguments or -h cause this function to print a message and exit.
57
58 Args:
59 argv: List of string arguments (excluding program name / argv[0])
60
61 Returns:
62 argparse.Namespace object containing the attributes.
63 """
64 parser = argparse.ArgumentParser(
65 description='Converts source proto config into platform JSON config.')
66 parser.add_argument(
67 '-c',
68 '--project_configs',
69 nargs='+',
70 type=str,
71 help='Space delimited list of source protobinary project config files.')
72 parser.add_argument(
73 '-p',
74 '--program_config',
75 type=str,
76 help='Path to the source program-level protobinary file')
77 parser.add_argument(
Andrew Lamb2413c982020-05-29 12:15:36 -060078 '-o', '--output', type=str, help='Output file that will be generated')
David Burger7fd1dbe2020-03-26 09:26:55 -060079 return parser.parse_args(argv)
80
81
David Burger8ee9b4d2020-06-16 17:40:21 -060082def _upsert(field, target, target_name):
83 """Updates or inserts `field` within `target`.
84
85 If `target_name` already exists within `target` an update is performed,
86 otherwise, an insert is performed.
87 """
Sam McNally9a873f72020-06-05 19:47:22 +100088 if field or field == 0:
David Burger8ee9b4d2020-06-16 17:40:21 -060089 if target_name in target:
90 target[target_name].update(field)
91 else:
92 target[target_name] = field
David Burger7fd1dbe2020-03-26 09:26:55 -060093
94
Andrew Lambcd33f702020-06-11 10:45:16 -060095def _build_arc(config, config_files):
96 if not config.build_target.arc:
97 return None
98
99 build_properties = {
100 'device': config.build_target.arc.device,
101 'first-api-level': config.build_target.arc.first_api_level,
102 'marketing-name': config.device_brand.brand_name,
103 'metrics-tag': config.hw_design.name.lower(),
104 'product': config.build_target.id.value,
105 }
106 if config.oem:
107 build_properties['oem'] = config.oem.name
108 result = {'build-properties': build_properties}
109 feature_id = _arc_hardware_feature_id(config.hw_design_config)
110 if feature_id in config_files.arc_hw_features:
111 result['hardware-features'] = config_files.arc_hw_features[feature_id]
112 topology = config.hw_design_config.hardware_topology
113 ppi = topology.screen.hardware_feature.screen.panel_properties.pixels_per_in
114 # Only set for high resolution displays
115 if ppi and ppi > 250:
116 result['scale'] = ppi
David Burger2f0d9522020-07-30 10:52:28 -0600117
118 if config_files.arc_camera_map:
119 # Prefer design specific if found, if not fall back to project wide config
120 # mapped under the empty string.
121 if config.hw_design.name in config_files.arc_camera_map:
122 camera_characteristics = config_files.arc_camera_map[
123 config.hw_design.name]
124 else:
125 camera_characteristics = config_files.arc_camera_map.get('')
126 result['camera-characteristics'] = camera_characteristics
127
Andrew Lambcd33f702020-06-11 10:45:16 -0600128 return result
David Burger7fd1dbe2020-03-26 09:26:55 -0600129
Andrew Lamb2413c982020-05-29 12:15:36 -0600130
Andrew Lamb319cc922020-06-15 10:45:46 -0600131def _build_ash_flags(config: Config) -> List[str]:
132 """Returns a list of Ash flags for config.
133
134 Ash is the window manager and system UI for ChromeOS, see
135 https://chromium.googlesource.com/chromium/src/+/refs/heads/master/ash/.
136 """
137 # A map from flag name -> value. Value may be None for boolean flags.
138 flags = {}
139
140 hw_features = config.hw_design_config.hardware_features
141 if hw_features.stylus.stylus == topology_pb2.HardwareFeatures.Stylus.INTERNAL:
Andrew Lamb2e641e22020-06-15 12:30:41 -0600142 flags['has-internal-stylus'] = None
Andrew Lamb319cc922020-06-15 10:45:46 -0600143
Andrew Lamb2e641e22020-06-15 12:30:41 -0600144 fp_loc = hw_features.fingerprint.location
145 if fp_loc and fp_loc != topology_pb2.HardwareFeatures.Fingerprint.NOT_PRESENT:
146 loc_name = topology_pb2.HardwareFeatures.Fingerprint.Location.Name(fp_loc)
147 flags['fingerprint-sensor-location'] = loc_name.lower().replace('_', '-')
148
Andrew Lamb6c42efc2020-06-16 10:40:43 -0600149 wallpaper = config.brand_config.wallpaper
150 # If a wallpaper is set, the 'default-wallpaper-is-oem' flag needs to be set.
151 # If a wallpaper is not set, the 'default_[large|small].jpg' wallpapers
152 # should still be set.
153 if wallpaper:
154 flags['default-wallpaper-is-oem'] = None
155 else:
156 wallpaper = 'default'
157
158 for size in ('small', 'large'):
159 flags[f'default-wallpaper-{size}'] = (
160 f'{WALLPAPER_BASE_PATH}/{wallpaper}_{size}.jpg')
161
162 # For each size, also install 'guest' and 'child' wallpapers.
163 for wallpaper_type in ('guest', 'child'):
164 flags[f'{wallpaper_type}-wallpaper-{size}'] = (
165 f'{WALLPAPER_BASE_PATH}/{wallpaper_type}_{size}.jpg')
166
Andrew Lamb72d41362020-06-17 09:19:02 -0600167 flags['arc-build-properties'] = json_format.MessageToDict(
168 config.build_target.arc)
169
Andrew Lamb90b168c2020-06-22 10:42:30 -0600170 power_button = hw_features.power_button
171 if power_button.edge:
172 flags['ash-power-button-position'] = json.dumps({
173 'edge':
174 topology_pb2.HardwareFeatures.Button.Edge.Name(power_button.edge
175 ).lower(),
176 # Starlark sometimes represents float literals strangely, e.g. changing
177 # 0.9 to 0.899999. Round to two digits here.
178 'position':
179 round(power_button.position, 2)
180 })
181
182 volume_button = hw_features.volume_button
183 if volume_button.edge:
184 flags['ash-side-volume-button-position'] = json.dumps({
185 'region':
186 topology_pb2.HardwareFeatures.Button.Region.Name(
187 volume_button.region).lower(),
188 'edge':
189 topology_pb2.HardwareFeatures.Button.Edge.Name(volume_button.edge
190 ).lower(),
191 })
192
Andrew Lamb2e641e22020-06-15 12:30:41 -0600193 return sorted([f'--{k}={v}' if v else f'--{k}' for k, v in flags.items()])
Andrew Lamb319cc922020-06-15 10:45:46 -0600194
195
196def _build_ui(config: Config) -> dict:
197 """Builds the 'ui' property from cros_config_schema."""
198 return {'extra-ash-flags': _build_ash_flags(config)}
199
200
Andrew Lambcd33f702020-06-11 10:45:16 -0600201def _build_bluetooth(config, bluetooth_files):
C Shapiro90fda252020-04-17 14:34:57 -0500202 bt_flags = config.sw_config.bluetooth_config.flags
203 # Convert to native map (from proto wrapper)
204 bt_flags_map = dict(bt_flags)
205 result = {}
206 if bt_flags_map:
207 result['flags'] = bt_flags_map
C Shapiro74da76e2020-05-04 13:02:20 -0500208 bt_comp = config.hw_design_config.hardware_features.bluetooth.component.usb
C Shapiro90fda252020-04-17 14:34:57 -0500209 if bt_comp.vendor_id:
Andrew Lambcd33f702020-06-11 10:45:16 -0600210 bt_id = _bluetooth_id(config.hw_design.name.lower(), bt_comp)
C Shapiro90fda252020-04-17 14:34:57 -0500211 if bt_id in bluetooth_files:
212 result['config'] = bluetooth_files[bt_id]
213 return result
214
David Burger7fd1dbe2020-03-26 09:26:55 -0600215
David Burgerec753912020-08-10 12:59:11 -0600216def _build_wifi(config):
217 result = {}
218 if config.sw_config.wifi_config.HasField('ath10k_config'):
219 ath10k_config = config.sw_config.wifi_config.ath10k_config
220
221 def power_chain(power):
222 return {
223 'limit-2g': power.limit_2g,
224 'limit-5g': power.limit_5g,
225 }
226
227 result['tablet-mode-power-table-ath10k'] = power_chain(
228 ath10k_config.tablet_mode_power_table)
229 result['non-tablet-mode-power-table-ath10k'] = power_chain(
230 ath10k_config.non_tablet_mode_power_table)
231 elif config.sw_config.wifi_config.HasField('rtw88_config'):
232 rtw88_config = config.sw_config.wifi_config.rtw88_config
233
234 def power_chain(power):
235 return {
236 'limit-2g': power.limit_2g,
237 'limit-5g-1': power.limit_5g_1,
238 'limit-5g-3': power.limit_5g_3,
239 'limit-5g-4': power.limit_5g_4,
240 }
241
242 result['tablet-mode-power-table-rtw'] = power_chain(
243 rtw88_config.tablet_mode_power_table)
244 result['non-tablet-mode-power-table-rtw'] = power_chain(
245 rtw88_config.non_tablet_mode_power_table)
246
247 def offsets(offset):
248 return {
249 'offset-2g': offset.offset_2g,
250 'offset-5g': offset.offset_5g,
251 }
252
253 result['geo-offsets-fcc'] = offsets(rtw88_config.offset_fcc)
254 result['geo-offsets-eu'] = offsets(rtw88_config.offset_eu)
255 result['geo-offsets-rest-of-world'] = offsets(rtw88_config.offset_other)
256 return result
257
258
Andrew Lambcd33f702020-06-11 10:45:16 -0600259def _build_fingerprint(hw_topology):
260 if not hw_topology.HasField('fingerprint'):
261 return None
262
263 fp = hw_topology.fingerprint.hardware_feature.fingerprint
264 result = {}
265 if fp.location != topology_pb2.HardwareFeatures.Fingerprint.NOT_PRESENT:
266 location = fp.Location.DESCRIPTOR.values_by_number[fp.location].name
267 result['sensor-location'] = location.lower().replace('_', '-')
268 if fp.board:
269 result['board'] = fp.board
Tom Hughesdfc35402020-06-29 16:02:09 -0700270 if fp.ro_version:
271 result['ro-version'] = fp.ro_version
272
Andrew Lambcd33f702020-06-11 10:45:16 -0600273 return result
David Burger7fd1dbe2020-03-26 09:26:55 -0600274
275
Andrew Lambcd33f702020-06-11 10:45:16 -0600276def _fw_bcs_path(payload):
David Burger7fd1dbe2020-03-26 09:26:55 -0600277 if payload and payload.firmware_image_name:
Andrew Lamb2413c982020-05-29 12:15:36 -0600278 return 'bcs://%s.%d.%d.0.tbz2' % (payload.firmware_image_name,
279 payload.version.major,
280 payload.version.minor)
David Burger7fd1dbe2020-03-26 09:26:55 -0600281
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 _fw_build_target(payload):
David Burger7fd1dbe2020-03-26 09:26:55 -0600286 if payload:
287 return payload.build_target_name
288
Andrew Lambcd33f702020-06-11 10:45:16 -0600289 return None
David Burger7fd1dbe2020-03-26 09:26:55 -0600290
Andrew Lambcd33f702020-06-11 10:45:16 -0600291
292def _build_firmware(config):
David Burgerb70b6762020-05-21 12:14:59 -0600293 """Returns firmware config, or None if no build targets."""
Andrew Lamb3da156d2020-04-16 16:00:56 -0600294 fw_payload_config = config.sw_config.firmware
295 fw_build_config = config.sw_config.firmware_build_config
296 main_ro = fw_payload_config.main_ro_payload
297 main_rw = fw_payload_config.main_rw_payload
298 ec_ro = fw_payload_config.ec_ro_payload
299 pd_ro = fw_payload_config.pd_ro_payload
David Burger7fd1dbe2020-03-26 09:26:55 -0600300
301 build_targets = {}
Andrew Lamb3da156d2020-04-16 16:00:56 -0600302
David Burger8ee9b4d2020-06-16 17:40:21 -0600303 _upsert(fw_build_config.build_targets.depthcharge, build_targets,
304 'depthcharge')
305 _upsert(fw_build_config.build_targets.coreboot, build_targets, 'coreboot')
306 _upsert(fw_build_config.build_targets.ec, build_targets, 'ec')
307 _upsert(
Andrew Lambf8954ee2020-04-21 10:24:40 -0600308 list(fw_build_config.build_targets.ec_extras), build_targets, 'ec_extras')
David Burger8ee9b4d2020-06-16 17:40:21 -0600309 _upsert(fw_build_config.build_targets.libpayload, build_targets, 'libpayload')
David Burger7fd1dbe2020-03-26 09:26:55 -0600310
David Burgerb70b6762020-05-21 12:14:59 -0600311 if not build_targets:
312 return None
313
David Burger7fd1dbe2020-03-26 09:26:55 -0600314 result = {
315 'bcs-overlay': config.build_target.overlay_name,
316 'build-targets': build_targets,
David Burger7fd1dbe2020-03-26 09:26:55 -0600317 }
Andrew Lamb883fa042020-04-06 11:37:22 -0600318
David Burger8ee9b4d2020-06-16 17:40:21 -0600319 _upsert(main_ro.firmware_image_name.lower(), result, 'image-name')
Andrew Lamb883fa042020-04-06 11:37:22 -0600320
David Burger8ee9b4d2020-06-16 17:40:21 -0600321 _upsert(_fw_bcs_path(main_ro), result, 'main-ro-image')
322 _upsert(_fw_bcs_path(main_rw), result, 'main-rw-image')
323 _upsert(_fw_bcs_path(ec_ro), result, 'ec-ro-image')
324 _upsert(_fw_bcs_path(pd_ro), result, 'pd-ro-image')
David Burger7fd1dbe2020-03-26 09:26:55 -0600325
David Burger8ee9b4d2020-06-16 17:40:21 -0600326 _upsert(
Andrew Lambf39fbe82020-04-13 16:14:33 -0600327 config.hw_design_config.hardware_features.fw_config.value,
328 result,
329 'firmware-config',
330 )
331
David Burger7fd1dbe2020-03-26 09:26:55 -0600332 return result
333
334
Andrew Lambcd33f702020-06-11 10:45:16 -0600335def _build_fw_signing(config):
C Shapiro2f0bb5d2020-04-14 10:07:47 -0500336 if config.sw_config.firmware and config.device_signer_config:
David Burger68e0d142020-05-15 17:29:33 -0600337 hw_design = config.hw_design.name.lower()
Sam McNally2fc807f2020-07-16 18:13:53 +1000338 brand_scan_config = config.brand_config.scan_config
339 if brand_scan_config and brand_scan_config.whitelabel_tag:
340 signature_id = '%s-%s' % (hw_design, brand_scan_config.whitelabel_tag)
341 else:
342 signature_id = hw_design
343
C Shapiro2f0bb5d2020-04-14 10:07:47 -0500344 return {
345 'key-id': config.device_signer_config.key_id,
Sam McNally2fc807f2020-07-16 18:13:53 +1000346 'signature-id': signature_id,
C Shapiro2f0bb5d2020-04-14 10:07:47 -0500347 }
348 return {}
David Burger7fd1dbe2020-03-26 09:26:55 -0600349
350
Andrew Lambcd33f702020-06-11 10:45:16 -0600351def _file(source, destination):
Andrew Lamb2413c982020-05-29 12:15:36 -0600352 return {'destination': destination, 'source': source}
David Burger7fd1dbe2020-03-26 09:26:55 -0600353
354
David Burger40dfe3a2020-06-18 17:09:13 -0600355def _file_v2(build_path, system_path):
356 return {'build-path': build_path, 'system-path': system_path}
357
358
Andrew Lambcd33f702020-06-11 10:45:16 -0600359def _build_audio(config):
David Burger178f3ef2020-06-26 12:11:57 -0600360 if not config.sw_config.audio_configs:
361 return {}
David Burger7fd1dbe2020-03-26 09:26:55 -0600362 alsa_path = '/usr/share/alsa/ucm'
363 cras_path = '/etc/cras'
364 project_name = config.hw_design.name.lower()
David Burger43250662020-05-07 11:21:50 -0600365 program_name = config.program.name.lower()
David Burger7fd1dbe2020-03-26 09:26:55 -0600366 files = []
David Burger178f3ef2020-06-26 12:11:57 -0600367 ucm_suffix = None
368 for audio in config.sw_config.audio_configs:
369 card = audio.card_name
370 card_with_suffix = audio.card_name
371 if audio.ucm_suffix:
372 # TODO: last ucm_suffix wins.
373 ucm_suffix = audio.ucm_suffix
374 card_with_suffix += '.' + audio.ucm_suffix
375 if audio.ucm_file:
376 files.append(
377 _file(audio.ucm_file,
378 '%s/%s/HiFi.conf' % (alsa_path, card_with_suffix)))
379 if audio.ucm_master_file:
380 files.append(
381 _file(
382 audio.ucm_master_file, '%s/%s/%s.conf' %
Andrew Lamb2413c982020-05-29 12:15:36 -0600383 (alsa_path, card_with_suffix, card_with_suffix)))
David Burger178f3ef2020-06-26 12:11:57 -0600384 if audio.card_config_file:
385 files.append(
386 _file(audio.card_config_file,
387 '%s/%s/%s' % (cras_path, project_name, card)))
388 if audio.dsp_file:
389 files.append(
390 _file(audio.dsp_file, '%s/%s/dsp.ini' % (cras_path, project_name)))
391 if audio.module_file:
392 files.append(
393 _file(audio.module_file,
394 '/etc/modprobe.d/alsa-%s.conf' % program_name))
395 if audio.board_file:
396 files.append(
397 _file(audio.board_file,
398 '%s/%s/board.ini' % (cras_path, project_name)))
David Burger599ff7b2020-04-06 16:29:31 -0600399
400 result = {
David Burger7fd1dbe2020-03-26 09:26:55 -0600401 'main': {
402 'cras-config-dir': project_name,
403 'files': files,
404 }
405 }
David Burger178f3ef2020-06-26 12:11:57 -0600406
407 if ucm_suffix:
408 result['main']['ucm-suffix'] = ucm_suffix
David Burger599ff7b2020-04-06 16:29:31 -0600409
410 return result
David Burger7fd1dbe2020-03-26 09:26:55 -0600411
412
Andrew Lambcd33f702020-06-11 10:45:16 -0600413def _build_camera(hw_topology):
David Burger8aa8fa32020-04-14 08:30:34 -0600414 if hw_topology.HasField('camera'):
415 camera = hw_topology.camera.hardware_feature.camera
416 result = {}
417 if camera.count.value:
418 result['count'] = camera.count.value
419 return result
420
Andrew Lambcd33f702020-06-11 10:45:16 -0600421 return None
David Burger8aa8fa32020-04-14 08:30:34 -0600422
Andrew Lambcd33f702020-06-11 10:45:16 -0600423
424def _build_identity(hw_scan_config, program, brand_scan_config=None):
David Burger7fd1dbe2020-03-26 09:26:55 -0600425 identity = {}
David Burger8ee9b4d2020-06-16 17:40:21 -0600426 _upsert(hw_scan_config.firmware_sku, identity, 'sku-id')
427 _upsert(hw_scan_config.smbios_name_match, identity, 'smbios-name-match')
Andrew Lamb7806ce92020-04-07 10:22:17 -0600428 # 'platform-name' is needed to support 'mosys platform name'. Clients should
429 # longer require platform name, but set it here for backwards compatibility.
David Burger8ee9b4d2020-06-16 17:40:21 -0600430 _upsert(program.name, identity, 'platform-name')
David Burger7fd1dbe2020-03-26 09:26:55 -0600431 # ARM architecture
David Burger8ee9b4d2020-06-16 17:40:21 -0600432 _upsert(hw_scan_config.device_tree_compatible_match, identity,
433 'device-tree-compatible-match')
David Burger7fd1dbe2020-03-26 09:26:55 -0600434
435 if brand_scan_config:
David Burger8ee9b4d2020-06-16 17:40:21 -0600436 _upsert(brand_scan_config.whitelabel_tag, identity, 'whitelabel-tag')
David Burger7fd1dbe2020-03-26 09:26:55 -0600437
438 return identity
439
440
Andrew Lambcd33f702020-06-11 10:45:16 -0600441def _lookup(id_value, id_map):
442 if not id_value.value:
443 return None
444
445 key = id_value.value
446 if key in id_map:
447 return id_map[id_value.value]
448 error = 'Failed to lookup %s with value: %s' % (
449 id_value.__class__.__name__.replace('Id', ''), key)
450 print(error)
451 print('Check the config contents provided:')
452 printer = pprint.PrettyPrinter(indent=4)
453 printer.pprint(id_map)
454 raise Exception(error)
David Burger7fd1dbe2020-03-26 09:26:55 -0600455
456
Andrew Lambcd33f702020-06-11 10:45:16 -0600457def _build_touch_file_config(config, project_name):
Sean McAllistereaf10b72020-08-03 13:41:06 -0600458 partners = {x.id.value: x for x in config.partner_list}
C Shapiro2b6d5332020-05-06 17:51:35 -0500459 files = []
460 for comp in config.components:
C Shapiro4813be62020-05-13 17:31:58 -0500461 touch = comp.touchscreen
462 # Everything is the same for Touch screen/pad, except different fields
463 if comp.HasField('touchpad'):
464 touch = comp.touchpad
465 if touch.product_id:
Andrew Lambcd33f702020-06-11 10:45:16 -0600466 vendor = _lookup(comp.manufacturer_id, partners)
C Shapiro2b6d5332020-05-06 17:51:35 -0500467 if not vendor:
Andrew Lamb2413c982020-05-29 12:15:36 -0600468 raise Exception("Manufacturer must be set for touch device %s" %
469 comp.id.value)
C Shapiro2b6d5332020-05-06 17:51:35 -0500470
C Shapiro4813be62020-05-13 17:31:58 -0500471 product_id = touch.product_id
472 fw_version = touch.fw_version
C Shapiro2b6d5332020-05-06 17:51:35 -0500473
C Shapiro2b6d5332020-05-06 17:51:35 -0500474 file_name = "%s_%s.bin" % (product_id, fw_version)
475 fw_file_path = os.path.join(TOUCH_PATH, vendor.name, file_name)
476
477 if not os.path.exists(fw_file_path):
Andrew Lamb2413c982020-05-29 12:15:36 -0600478 raise Exception("Touchscreen fw bin file doesn't exist at: %s" %
479 fw_file_path)
C Shapiro2b6d5332020-05-06 17:51:35 -0500480
C Shapiro303cece2020-07-22 07:15:21 -0500481 touch_vendor = vendor.touch_vendor
482 sym_link = touch_vendor.symlink_file_format.format(
483 vendor_name=vendor.name,
484 vendor_id=touch_vendor.vendor_id,
485 product_id=product_id,
486 fw_version=fw_version,
487 product_series=touch.product_series)
488
489 dest = "%s_%s" % (vendor.name, file_name)
490 if touch_vendor.destination_file_format:
491 dest = touch_vendor.destination_file_format.format(
492 vendor_name=vendor.name,
493 vendor_id=touch_vendor.vendor_id,
494 product_id=product_id,
495 fw_version=fw_version,
496 product_series=touch.product_series)
497
C Shapiro2b6d5332020-05-06 17:51:35 -0500498 files.append({
C Shapiro303cece2020-07-22 07:15:21 -0500499 "destination": os.path.join("/opt/google/touch/firmware", dest),
YH Lin9160fc52020-07-22 16:35:28 -0700500 "source": os.path.join(project_name, fw_file_path),
501 "symlink": os.path.join("/lib/firmware", sym_link),
C Shapiro2b6d5332020-05-06 17:51:35 -0500502 })
503
504 result = {}
David Burger8ee9b4d2020-06-16 17:40:21 -0600505 _upsert(files, result, 'files')
C Shapiro2b6d5332020-05-06 17:51:35 -0500506 return result
507
508
David Burger8ee9b4d2020-06-16 17:40:21 -0600509def _transform_build_configs(config,
David Burger2f0d9522020-07-30 10:52:28 -0600510 config_files=ConfigFiles({}, {}, {}, {}, {}, {})):
Andrew Lambcd33f702020-06-11 10:45:16 -0600511 # pylint: disable=too-many-locals,too-many-branches
Sean McAllistereaf10b72020-08-03 13:41:06 -0600512 partners = {x.id.value: x for x in config.partner_list}
Sean McAllisterf38d1e92020-08-03 13:57:53 -0600513 programs = {x.id.value: x for x in config.program_list}
David Burger7fd1dbe2020-03-26 09:26:55 -0600514 sw_configs = list(config.software_configs)
Andrew Lambcd33f702020-06-11 10:45:16 -0600515 brand_configs = {x.brand_id.value: x for x in config.brand_configs}
David Burger7fd1dbe2020-03-26 09:26:55 -0600516
C Shapiroa0b766c2020-03-31 08:35:28 -0500517 if len(config.build_targets) != 1:
518 # Artifact of sharing the config_bundle for analysis and transforms.
519 # Integrated analysis of multiple programs/projects it the only time
520 # having multiple build targets would be valid.
521 raise Exception('Single build_target required for transform')
522
David Burger7fd1dbe2020-03-26 09:26:55 -0600523 results = {}
Sean McAllisterf66887b2020-08-03 14:00:51 -0600524 for hw_design in config.design_list:
Sean McAllister6cbb0ec2020-08-03 14:03:37 -0600525 if config.device_brand_list:
Andrew Lamb2413c982020-05-29 12:15:36 -0600526 device_brands = [
Sean McAllister6cbb0ec2020-08-03 14:03:37 -0600527 x for x in config.device_brand_list
Andrew Lamb2413c982020-05-29 12:15:36 -0600528 if x.design_id.value == hw_design.id.value
529 ]
David Burger7fd1dbe2020-03-26 09:26:55 -0600530 else:
531 device_brands = [device_brand_pb2.DeviceBrand()]
532
533 for device_brand in device_brands:
534 # Brand config can be empty since platform JSON config allows it
535 brand_config = brand_config_pb2.BrandConfig()
536 if device_brand.id.value in brand_configs:
537 brand_config = brand_configs[device_brand.id.value]
538
539 for hw_design_config in hw_design.configs:
540 design_id = hw_design_config.id.value
Andrew Lamb2413c982020-05-29 12:15:36 -0600541 sw_config_matches = [
542 x for x in sw_configs if x.design_config_id.value == design_id
543 ]
David Burger7fd1dbe2020-03-26 09:26:55 -0600544 if len(sw_config_matches) == 1:
545 sw_config = sw_config_matches[0]
546 elif len(sw_config_matches) > 1:
547 raise Exception('Multiple software configs found for: %s' % design_id)
548 else:
549 raise Exception('Software config is required for: %s' % design_id)
550
Andrew Lambcd33f702020-06-11 10:45:16 -0600551 program = _lookup(hw_design.program_id, programs)
C Shapiroadefd7c2020-05-19 16:37:21 -0500552 signer_configs_by_design = {}
553 signer_configs_by_brand = {}
554 for signer_config in program.device_signer_configs:
555 design_id = signer_config.design_id.value
556 brand_id = signer_config.brand_id.value
557 if design_id:
558 signer_configs_by_design[design_id] = signer_config
559 elif brand_id:
560 signer_configs_by_brand[brand_id] = signer_config
561 else:
562 raise Exception('No ID found for signer config: %s' % signer_config)
563
C Shapiro2f0bb5d2020-04-14 10:07:47 -0500564 device_signer_config = None
C Shapiroadefd7c2020-05-19 16:37:21 -0500565 if signer_configs_by_design or signer_configs_by_brand:
566 design_id = hw_design.id.value
567 brand_id = device_brand.id.value
568 if design_id in signer_configs_by_design:
569 device_signer_config = signer_configs_by_design[design_id]
570 elif brand_id in signer_configs_by_brand:
571 device_signer_config = signer_configs_by_brand[brand_id]
572 else:
573 # Assume that if signer configs are set, every config is setup
Andrew Lamb2413c982020-05-29 12:15:36 -0600574 raise Exception('Signer config missing for design: %s, brand: %s' %
575 (design_id, brand_id))
C Shapiro2f0bb5d2020-04-14 10:07:47 -0500576
Andrew Lambcd33f702020-06-11 10:45:16 -0600577 transformed_config = _transform_build_config(
C Shapiro90fda252020-04-17 14:34:57 -0500578 Config(
579 program=program,
580 hw_design=hw_design,
Andrew Lambcd33f702020-06-11 10:45:16 -0600581 odm=_lookup(hw_design.odm_id, partners),
C Shapiro90fda252020-04-17 14:34:57 -0500582 hw_design_config=hw_design_config,
583 device_brand=device_brand,
584 device_signer_config=device_signer_config,
Andrew Lambcd33f702020-06-11 10:45:16 -0600585 oem=_lookup(device_brand.oem_id, partners),
C Shapiro90fda252020-04-17 14:34:57 -0500586 sw_config=sw_config,
587 brand_config=brand_config,
Andrew Lamb2413c982020-05-29 12:15:36 -0600588 build_target=config.build_targets[0]), config_files)
David Burger7fd1dbe2020-03-26 09:26:55 -0600589
Andrew Lamb2413c982020-05-29 12:15:36 -0600590 config_json = json.dumps(
591 transformed_config,
592 sort_keys=True,
593 indent=2,
594 separators=(',', ': '))
David Burger7fd1dbe2020-03-26 09:26:55 -0600595
596 if config_json not in results:
597 results[config_json] = transformed_config
598
599 return list(results.values())
600
601
Andrew Lambcd33f702020-06-11 10:45:16 -0600602def _transform_build_config(config, config_files):
David Burger7fd1dbe2020-03-26 09:26:55 -0600603 """Transforms Config instance into target platform JSON schema.
604
605 Args:
606 config: Config namedtuple
C Shapiro5bf23a72020-04-24 11:40:17 -0500607 config_files: Map to look up the generated config files.
David Burger7fd1dbe2020-03-26 09:26:55 -0600608
609 Returns:
610 Unique config payload based on the platform JSON schema.
611 """
612 result = {
Andrew Lamb2413c982020-05-29 12:15:36 -0600613 'identity':
Andrew Lambcd33f702020-06-11 10:45:16 -0600614 _build_identity(config.sw_config.id_scan_config, config.program,
615 config.brand_config.scan_config),
Andrew Lamb2413c982020-05-29 12:15:36 -0600616 'name':
617 config.hw_design.name.lower(),
David Burger7fd1dbe2020-03-26 09:26:55 -0600618 }
619
David Burger8ee9b4d2020-06-16 17:40:21 -0600620 _upsert(_build_arc(config, config_files), result, 'arc')
621 _upsert(_build_audio(config), result, 'audio')
622 _upsert(_build_bluetooth(config, config_files.bluetooth), result, 'bluetooth')
David Burgerec753912020-08-10 12:59:11 -0600623 _upsert(_build_wifi(config), result, 'wifi')
Andrew Lambca279902020-08-06 10:13:42 -0600624 _upsert(config.brand_config.wallpaper, result, 'wallpaper')
David Burger8ee9b4d2020-06-16 17:40:21 -0600625 _upsert(config.device_brand.brand_code, result, 'brand-code')
626 _upsert(
Andrew Lambcd33f702020-06-11 10:45:16 -0600627 _build_camera(config.hw_design_config.hardware_topology), result,
628 'camera')
David Burger8ee9b4d2020-06-16 17:40:21 -0600629 _upsert(_build_firmware(config), result, 'firmware')
630 _upsert(_build_fw_signing(config), result, 'firmware-signing')
631 _upsert(
Andrew Lambcd33f702020-06-11 10:45:16 -0600632 _build_fingerprint(config.hw_design_config.hardware_topology), result,
Andrew Lamb2413c982020-05-29 12:15:36 -0600633 'fingerprint')
Andrew Lamb0d236ab2020-06-30 12:30:20 -0600634 _upsert(_build_ui(config), result, 'ui')
David Burger7fd1dbe2020-03-26 09:26:55 -0600635 power_prefs = config.sw_config.power_config.preferences
636 power_prefs_map = dict(
Andrew Lamb2413c982020-05-29 12:15:36 -0600637 (x.replace('_', '-'), power_prefs[x]) for x in power_prefs)
David Burger8ee9b4d2020-06-16 17:40:21 -0600638 _upsert(power_prefs_map, result, 'power')
639 if config_files.camera_map:
640 camera_file = config_files.camera_map.get(config.hw_design.name, {})
641 _upsert(camera_file, result, 'camera')
David Burger52c9d322020-06-09 07:16:18 -0600642 if config_files.dptf_map:
643 # Prefer design specific if found, if not fall back to project wide config
644 # mapped under the empty string.
645 if config_files.dptf_map.get(config.hw_design.name):
646 dptf_file = config_files.dptf_map[config.hw_design.name]
647 else:
648 dptf_file = config_files.dptf_map.get('')
David Burger8ee9b4d2020-06-16 17:40:21 -0600649 _upsert(dptf_file, result, 'thermal')
650 _upsert(config_files.touch_fw, result, 'touch')
David Burger7fd1dbe2020-03-26 09:26:55 -0600651
652 return result
653
654
Andrew Lambcd33f702020-06-11 10:45:16 -0600655def write_output(configs, output=None):
David Burger7fd1dbe2020-03-26 09:26:55 -0600656 """Writes a list of configs to platform JSON format.
657
658 Args:
659 configs: List of config dicts defined in cros_config_schema.yaml
660 output: Target file output (if None, prints to stdout)
661 """
Andrew Lamb2413c982020-05-29 12:15:36 -0600662 json_output = json.dumps({'chromeos': {
663 'configs': configs,
664 }},
665 sort_keys=True,
666 indent=2,
667 separators=(',', ': '))
David Burger7fd1dbe2020-03-26 09:26:55 -0600668 if output:
669 with open(output, 'w') as output_stream:
670 # Using print function adds proper trailing newline.
671 print(json_output, file=output_stream)
672 else:
673 print(json_output)
674
675
Andrew Lambcd33f702020-06-11 10:45:16 -0600676def _bluetooth_id(project_name, bt_comp):
Andrew Lamb2413c982020-05-29 12:15:36 -0600677 return '_'.join(
678 [project_name, bt_comp.vendor_id, bt_comp.product_id, bt_comp.bcd_device])
C Shapiro90fda252020-04-17 14:34:57 -0500679
680
Andrew Lambcd33f702020-06-11 10:45:16 -0600681def _feature(name, present):
C Shapiro5bf23a72020-04-24 11:40:17 -0500682 attrib = {'name': name}
683 if present:
684 return etree.Element('feature', attrib=attrib)
Andrew Lambcd33f702020-06-11 10:45:16 -0600685
686 return etree.Element('unavailable-feature', attrib=attrib)
C Shapiro5bf23a72020-04-24 11:40:17 -0500687
688
Andrew Lambcd33f702020-06-11 10:45:16 -0600689def _any_present(features):
Andrew Lamb2413c982020-05-29 12:15:36 -0600690 return topology_pb2.HardwareFeatures.PRESENT in features
C Shapiro5bf23a72020-04-24 11:40:17 -0500691
692
Andrew Lambcd33f702020-06-11 10:45:16 -0600693def _arc_hardware_feature_id(design_config):
C Shapiro5bf23a72020-04-24 11:40:17 -0500694 return design_config.id.value.lower().replace(':', '_')
695
696
Andrew Lambcd33f702020-06-11 10:45:16 -0600697def _write_arc_hardware_feature_file(output_dir, file_name, config_content):
David Burger77a1d312020-05-23 16:05:45 -0600698 output_dir += '/arc'
699 os.makedirs(output_dir, exist_ok=True)
700 output = '%s/%s' % (output_dir, file_name)
Andrew Lamb2413c982020-05-29 12:15:36 -0600701 file_content = minidom.parseString(config_content).toprettyxml(
702 indent=' ', encoding='utf-8')
C Shapiroea33cff2020-05-11 13:32:05 -0500703
704 with open(output, 'wb') as f:
705 f.write(file_content)
706
707
Andrew Lambcd33f702020-06-11 10:45:16 -0600708def _write_arc_hardware_feature_files(config, output_dir, build_root_dir):
C Shapiro5bf23a72020-04-24 11:40:17 -0500709 """Writes ARC hardware_feature.xml files for each config
710
711 Args:
712 config: Source ConfigBundle to process.
713 output_dir: Path to the generated output.
C Shapiro5c877992020-04-29 12:11:28 -0500714 build_root_path: Path to the config file from portage's perspective.
C Shapiro5bf23a72020-04-24 11:40:17 -0500715 Returns:
716 dict that maps the design_config_id onto the correct file.
717 """
Andrew Lambcd33f702020-06-11 10:45:16 -0600718 # pylint: disable=too-many-locals
C Shapiro5bf23a72020-04-24 11:40:17 -0500719 result = {}
C Shapiroea33cff2020-05-11 13:32:05 -0500720 configs_by_design = {}
Sean McAllisterf66887b2020-08-03 14:00:51 -0600721 for hw_design in config.design_list:
C Shapiro5bf23a72020-04-24 11:40:17 -0500722 for design_config in hw_design.configs:
723 hw_features = design_config.hardware_features
Kazuhiro Inaba76d8bb52020-06-26 10:34:07 +0900724 any_camera = hw_features.camera.count.value > 0
Josie Nordrumadc27a72020-07-08 11:05:26 -0600725 multi_camera = hw_features.camera.count.value > 1
Andrew Lambcd33f702020-06-11 10:45:16 -0600726 touchscreen = _any_present([hw_features.screen.touch_support])
C Shapiro5bf23a72020-04-24 11:40:17 -0500727 acc = hw_features.accelerometer
728 gyro = hw_features.gyroscope
729 compass = hw_features.magnetometer
Andrew Lambcd33f702020-06-11 10:45:16 -0600730 light_sensor = hw_features.light_sensor
C Shapiro5bf23a72020-04-24 11:40:17 -0500731 root = etree.Element('permissions')
732 root.extend([
Andrew Lambcd33f702020-06-11 10:45:16 -0600733 _feature('android.hardware.camera', multi_camera),
734 _feature('android.hardware.camera.autofocus', multi_camera),
Kazuhiro Inaba76d8bb52020-06-26 10:34:07 +0900735 _feature('android.hardware.camera.any', any_camera),
736 _feature('android.hardware.camera.front', any_camera),
Andrew Lambcd33f702020-06-11 10:45:16 -0600737 _feature(
738 'android.hardware.sensor.accelerometer',
739 _any_present([acc.lid_accelerometer, acc.base_accelerometer])),
740 _feature('android.hardware.sensor.gyroscope',
741 _any_present([gyro.lid_gyroscope, gyro.base_gyroscope])),
742 _feature(
Andrew Lamb2413c982020-05-29 12:15:36 -0600743 'android.hardware.sensor.compass',
Andrew Lambcd33f702020-06-11 10:45:16 -0600744 _any_present(
745 [compass.lid_magnetometer, compass.base_magnetometer])),
746 _feature(
747 'android.hardware.sensor.light',
748 _any_present(
749 [light_sensor.lid_lightsensor,
750 light_sensor.base_lightsensor])),
751 _feature('android.hardware.touchscreen', touchscreen),
752 _feature('android.hardware.touchscreen.multitouch', touchscreen),
753 _feature('android.hardware.touchscreen.multitouch.distinct',
Andrew Lamb2413c982020-05-29 12:15:36 -0600754 touchscreen),
Andrew Lambcd33f702020-06-11 10:45:16 -0600755 _feature('android.hardware.touchscreen.multitouch.jazzhand',
Andrew Lamb2413c982020-05-29 12:15:36 -0600756 touchscreen),
C Shapiro5bf23a72020-04-24 11:40:17 -0500757 ])
758
C Shapiroea33cff2020-05-11 13:32:05 -0500759 design_name = hw_design.name.lower()
C Shapiro5bf23a72020-04-24 11:40:17 -0500760
C Shapiroea33cff2020-05-11 13:32:05 -0500761 # Constructs the following map:
762 # design_name -> config -> design_configs
763 # This allows any of the following file naming schemes:
764 # - All configs within a design share config (design_name prefix only)
765 # - Nobody shares (full design_name and config id prefix needed)
766 #
767 # Having shared configs when possible makes code reviews easier around
768 # the configs and makes debugging easier on the platform side.
769 config_content = etree.tostring(root)
770 arc_configs = configs_by_design.get(design_name, {})
771 design_configs = arc_configs.get(config_content, [])
772 design_configs.append(design_config)
773 arc_configs[config_content] = design_configs
774 configs_by_design[design_name] = arc_configs
C Shapiro9a3ac8c2020-04-25 07:49:21 -0500775
C Shapiroea33cff2020-05-11 13:32:05 -0500776 for design_name, unique_configs in configs_by_design.items():
777 for file_content, design_configs in unique_configs.items():
Andrew Lamb2413c982020-05-29 12:15:36 -0600778 file_name = 'hardware_features_%s.xml' % design_name
779 if len(unique_configs) == 1:
Andrew Lambcd33f702020-06-11 10:45:16 -0600780 _write_arc_hardware_feature_file(output_dir, file_name, file_content)
C Shapiro9a3ac8c2020-04-25 07:49:21 -0500781
Andrew Lamb2413c982020-05-29 12:15:36 -0600782 for design_config in design_configs:
Andrew Lambcd33f702020-06-11 10:45:16 -0600783 feature_id = _arc_hardware_feature_id(design_config)
Andrew Lamb2413c982020-05-29 12:15:36 -0600784 if len(unique_configs) > 1:
785 file_name = 'hardware_features_%s.xml' % feature_id
Andrew Lambcd33f702020-06-11 10:45:16 -0600786 _write_arc_hardware_feature_file(output_dir, file_name, file_content)
David Burger40dfe3a2020-06-18 17:09:13 -0600787 result[feature_id] = _file_v2('%s/arc/%s' % (build_root_dir, file_name),
788 '/etc/%s' % file_name)
C Shapiro5bf23a72020-04-24 11:40:17 -0500789 return result
790
791
Andrew Lambcd33f702020-06-11 10:45:16 -0600792def _write_bluetooth_config_files(config, output_dir, build_root_path):
C Shapiro90fda252020-04-17 14:34:57 -0500793 """Writes bluetooth conf files for every unique bluetooth chip.
794
795 Args:
796 config: Source ConfigBundle to process.
797 output_dir: Path to the generated output.
C Shapiro5c877992020-04-29 12:11:28 -0500798 build_root_path: Path to the config file from portage's perspective.
C Shapiro90fda252020-04-17 14:34:57 -0500799 Returns:
800 dict that maps the bluetooth component id onto the file config.
801 """
David Burger77a1d312020-05-23 16:05:45 -0600802 output_dir += '/bluetooth'
C Shapiro90fda252020-04-17 14:34:57 -0500803 result = {}
Sean McAllisterf66887b2020-08-03 14:00:51 -0600804 for hw_design in config.design_list:
C Shapiro90fda252020-04-17 14:34:57 -0500805 project_name = hw_design.name.lower()
806 for design_config in hw_design.configs:
C Shapiro74da76e2020-05-04 13:02:20 -0500807 bt_comp = design_config.hardware_features.bluetooth.component.usb
C Shapiro90fda252020-04-17 14:34:57 -0500808 if bt_comp.vendor_id:
Andrew Lambcd33f702020-06-11 10:45:16 -0600809 bt_id = _bluetooth_id(project_name, bt_comp)
David Burger40dfe3a2020-06-18 17:09:13 -0600810 result[bt_id] = _file_v2(
811 '%s/bluetooth/%s.conf' % (build_root_path, bt_id),
812 '/etc/bluetooth/%s/main.conf' % bt_id)
C Shapiro90fda252020-04-17 14:34:57 -0500813 bt_content = '''[General]
Andrew Lamb2413c982020-05-29 12:15:36 -0600814DeviceID = bluetooth:%s:%s:%s''' % (bt_comp.vendor_id, bt_comp.product_id,
C Shapiro90fda252020-04-17 14:34:57 -0500815 bt_comp.bcd_device)
816
David Burger77a1d312020-05-23 16:05:45 -0600817 os.makedirs(output_dir, exist_ok=True)
818 output = '%s/%s.conf' % (output_dir, bt_id)
C Shapiro90fda252020-04-17 14:34:57 -0500819 with open(output, 'w') as output_stream:
820 # Using print function adds proper trailing newline.
821 print(bt_content, file=output_stream)
822 return result
823
824
Andrew Lambcd33f702020-06-11 10:45:16 -0600825def _read_config(path):
David Burgerd4f32962020-05-02 12:07:40 -0600826 """Reads a ConfigBundle proto from a json pb file.
David Burgere6f76222020-04-27 11:08:01 -0600827
828 Args:
David Burgerd4f32962020-05-02 12:07:40 -0600829 path: Path to the file encoding the json pb proto.
David Burgere6f76222020-04-27 11:08:01 -0600830 """
831 config = config_bundle_pb2.ConfigBundle()
832 with open(path, 'r') as f:
833 return json_format.Parse(f.read(), config)
834
835
Andrew Lambcd33f702020-06-11 10:45:16 -0600836def _merge_configs(configs):
David Burger7fd1dbe2020-03-26 09:26:55 -0600837 result = config_bundle_pb2.ConfigBundle()
838 for config in configs:
839 result.MergeFrom(config)
840
841 return result
842
843
David Burger1ba78a22020-06-18 18:42:47 -0600844def _camera_map(configs, project_name):
David Burger8ee9b4d2020-06-16 17:40:21 -0600845 """Produces a camera config map for the given configs.
846
847 Produces a map that maps from the design name to the camera config for that
848 design.
849
850 Args:
851 configs: Source ConfigBundle to process.
David Burger1ba78a22020-06-18 18:42:47 -0600852 project_name: Name of project processing for.
David Burger8ee9b4d2020-06-16 17:40:21 -0600853
854 Returns:
855 map from design name to camera config.
856 """
857 result = {}
Sean McAllisterf66887b2020-08-03 14:00:51 -0600858 for design in configs.design_list:
David Burger8ee9b4d2020-06-16 17:40:21 -0600859 design_name = design.name
David Burger0d9e8462020-06-19 14:12:37 -0600860 config_path = CAMERA_CONFIG_SOURCE_PATH_TEMPLATE.format(design_name.lower())
David Burger8ee9b4d2020-06-16 17:40:21 -0600861 if os.path.exists(config_path):
David Burger0d9e8462020-06-19 14:12:37 -0600862 destination = CAMERA_CONFIG_DEST_PATH_TEMPLATE.format(design_name.lower())
David Burger8ee9b4d2020-06-16 17:40:21 -0600863 result[design_name] = {
David Burger1ba78a22020-06-18 18:42:47 -0600864 'config-path':
865 destination,
866 'config-file':
867 _file_v2(os.path.join(project_name, config_path), destination),
David Burger8ee9b4d2020-06-16 17:40:21 -0600868 }
869 return result
870
871
David Burger3abda442020-08-06 16:15:59 -0600872def _config_map(configs, project_name, config_dir, config_file, system_dir):
David Burger2f0d9522020-07-30 10:52:28 -0600873 """Produces a config map for the given configs.
874
875 Produces a map that maps from design name to the config file for that
876 design. It looks for the config files at:
877 config_dir + '/' + config_file
878 for a project wide config, that it maps under the empty string, and at:
879 config_dir + '/' + design_name + '/' + config_file
880 for design specific configs that it maps under the design name.
881
882 Args:
883 configs: Source ConfigBundle to process.
David Burger3abda442020-08-06 16:15:59 -0600884 project_name: Name of project processing for.
David Burger2f0d9522020-07-30 10:52:28 -0600885 config_dir: Path to the directory containing configuration files.
886 config_file: Name of the configuration files.
887 system_dir: Base directory for the output system path.
888
889 Returns:
890 map from design name or empty string (project wide), to config.
891 """
892 result = {}
893 # Looking at top level for project wide, and then for each design name
894 # for design specific.
Sean McAllistera3f7df42020-08-04 18:24:02 -0600895 dirs = [""] + [d.name for d in configs.design_list]
David Burger2f0d9522020-07-30 10:52:28 -0600896 for directory in dirs:
897 design = directory.lower()
David Burger3abda442020-08-06 16:15:59 -0600898 config_file_path = os.path.join(config_dir, design, config_file)
899 if os.path.exists(config_file_path):
900 build_path = os.path.join(project_name, config_file_path)
David Burger2f0d9522020-07-30 10:52:28 -0600901 if design:
902 system_file = config_file.replace('.', '_{}.'.format(design))
903 else:
904 system_file = config_file
David Burger3abda442020-08-06 16:15:59 -0600905 system_path = os.path.join(system_dir, project_name, system_file)
David Burger2f0d9522020-07-30 10:52:28 -0600906 result[directory] = _file_v2(build_path, system_path)
907 return result
908
909
David Burger52c9d322020-06-09 07:16:18 -0600910def _dptf_map(configs, project_name):
911 """Produces a dptf map for the given configs.
912
913 Produces a map that maps from design name to the dptf file config for that
914 design. It looks for the dptf files at:
David Burger2f0d9522020-07-30 10:52:28 -0600915 DPTF_PATH + '/' + DPTF_FILE
David Burger52c9d322020-06-09 07:16:18 -0600916 for a project wide config, that it maps under the empty string, and at:
David Burger2f0d9522020-07-30 10:52:28 -0600917 DPTF_PATH + '/' + design_name + '/' + DPTF_FILE
David Burger52c9d322020-06-09 07:16:18 -0600918 for design specific configs that it maps under the design name.
919
920 Args:
921 configs: Source ConfigBundle to process.
922 project_name: Name of project processing for.
923
924 Returns:
David Burger8ee9b4d2020-06-16 17:40:21 -0600925 map from design name or empty string (project wide), to dptf config.
David Burger52c9d322020-06-09 07:16:18 -0600926 """
927 result = {}
David Burger52c9d322020-06-09 07:16:18 -0600928 # Looking at top level for project wide, and then for each design name
929 # for design specific.
Sean McAllisterf66887b2020-08-03 14:00:51 -0600930 dirs = [""] + [d.name for d in configs.design_list]
David Burger52c9d322020-06-09 07:16:18 -0600931 for directory in dirs:
David Burgera2252762020-07-09 15:09:49 -0600932 design = directory.lower()
933 if os.path.exists(os.path.join(DPTF_PATH, design, DPTF_FILE)):
David Burger2f0d9522020-07-30 10:52:28 -0600934 project_dptf_path = os.path.join(project_name, design, DPTF_FILE)
David Burger52c9d322020-06-09 07:16:18 -0600935 dptf_file = {
936 'dptf-dv':
937 project_dptf_path,
938 'files': [
939 _file(
David Burgera2252762020-07-09 15:09:49 -0600940 os.path.join(project_name, DPTF_PATH, design, DPTF_FILE),
David Burger52c9d322020-06-09 07:16:18 -0600941 os.path.join('/etc/dptf', project_dptf_path))
942 ]
943 }
944 result[directory] = dptf_file
945 return result
946
947
Andrew Lambcd33f702020-06-11 10:45:16 -0600948def Main(project_configs, program_config, output): # pylint: disable=invalid-name
David Burger7fd1dbe2020-03-26 09:26:55 -0600949 """Transforms source proto config into platform JSON.
950
951 Args:
952 project_configs: List of source project configs to transform.
953 program_config: Program config for the given set of projects.
954 output: Output file that will be generated by the transform.
955 """
Andrew Lambcd33f702020-06-11 10:45:16 -0600956 configs = _merge_configs([_read_config(program_config)] +
957 [_read_config(config) for config in project_configs])
C Shapiro5bf23a72020-04-24 11:40:17 -0500958 bluetooth_files = {}
959 arc_hw_feature_files = {}
C Shapiro2b6d5332020-05-06 17:51:35 -0500960 touch_fw = {}
David Burger2f0d9522020-07-30 10:52:28 -0600961 arc_camera_map = {}
David Burger52c9d322020-06-09 07:16:18 -0600962 dptf_map = {}
David Burger8ee9b4d2020-06-16 17:40:21 -0600963 camera_map = {}
C Shapiro5bf23a72020-04-24 11:40:17 -0500964 output_dir = os.path.dirname(output)
C Shapiro5c877992020-04-29 12:11:28 -0500965 build_root_dir = output_dir
C Shapiro5c877992020-04-29 12:11:28 -0500966 if 'sw_build_config' in output_dir:
967 full_path = os.path.realpath(output)
Andrew Lamb2413c982020-05-29 12:15:36 -0600968 project_name = re.match(r'.*/(\w*)/sw_build_config/.*',
969 full_path).groups(1)[0]
C Shapiro5c877992020-04-29 12:11:28 -0500970 # Projects don't know about each other until they are integrated into the
971 # build system. When this happens, the files need to be able to co-exist
972 # without any collisions. This prefixes the project name (which is how
973 # portage maps in the project), so project files co-exist and can be
974 # installed together.
975 # This is necessary to allow projects to share files at the program level
976 # without having portage file installation collisions.
977 build_root_dir = os.path.join(project_name, output_dir)
C Shapiro6830e6c2020-04-29 13:29:56 -0500978
David Burger3abda442020-08-06 16:15:59 -0600979 arc_camera_map = _config_map(configs, project_name, ARC_CONFIG_PATH,
David Burger2f0d9522020-07-30 10:52:28 -0600980 ARC_CAMERA_CHARACTERISTICS_FILE, '/etc/arc')
David Burger1ba78a22020-06-18 18:42:47 -0600981 camera_map = _camera_map(configs, project_name)
David Burger52c9d322020-06-09 07:16:18 -0600982 dptf_map = _dptf_map(configs, project_name)
983
C Shapiro2b6d5332020-05-06 17:51:35 -0500984 if os.path.exists(TOUCH_PATH):
Andrew Lambcd33f702020-06-11 10:45:16 -0600985 touch_fw = _build_touch_file_config(configs, project_name)
986 bluetooth_files = _write_bluetooth_config_files(configs, output_dir,
987 build_root_dir)
988 arc_hw_feature_files = _write_arc_hardware_feature_files(
989 configs, output_dir, build_root_dir)
C Shapiro5bf23a72020-04-24 11:40:17 -0500990 config_files = ConfigFiles(
991 bluetooth=bluetooth_files,
992 arc_hw_features=arc_hw_feature_files,
C Shapiro2b6d5332020-05-06 17:51:35 -0500993 touch_fw=touch_fw,
David Burger8ee9b4d2020-06-16 17:40:21 -0600994 dptf_map=dptf_map,
David Burger2f0d9522020-07-30 10:52:28 -0600995 camera_map=camera_map,
996 arc_camera_map=arc_camera_map)
Andrew Lambcd33f702020-06-11 10:45:16 -0600997 write_output(_transform_build_configs(configs, config_files), output)
David Burger7fd1dbe2020-03-26 09:26:55 -0600998
999
1000def main(argv=None):
1001 """Main program which parses args and runs
1002
1003 Args:
1004 argv: List of command line arguments, if None uses sys.argv.
1005 """
1006 if argv is None:
1007 argv = sys.argv[1:]
Andrew Lambcd33f702020-06-11 10:45:16 -06001008 opts = parse_args(argv)
David Burger7fd1dbe2020-03-26 09:26:55 -06001009 Main(opts.project_configs, opts.program_config, opts.output)
1010
1011
1012if __name__ == '__main__':
1013 sys.exit(main(sys.argv[1:]))