blob: 2dcf74ba3ab7260c01a1c61f6d003698e6670f31 [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
Andrew Lamb2413c982020-05-29 12:15:36 -060033ConfigFiles = namedtuple(
David Burger52c9d322020-06-09 07:16:18 -060034 'ConfigFiles', ['bluetooth', 'arc_hw_features', 'touch_fw', 'dptf_map'])
C Shapiro5bf23a72020-04-24 11:40:17 -050035
David Burger52c9d322020-06-09 07:16:18 -060036DPTF_PATH = 'sw_build_config/platform/chromeos-config/thermal'
37DPTF_FILE = 'dptf.dv'
C Shapiro2b6d5332020-05-06 17:51:35 -050038TOUCH_PATH = 'sw_build_config/platform/chromeos-config/touch'
David Burger7fd1dbe2020-03-26 09:26:55 -060039
Andrew Lamb2413c982020-05-29 12:15:36 -060040
Andrew Lambcd33f702020-06-11 10:45:16 -060041def parse_args(argv):
David Burger7fd1dbe2020-03-26 09:26:55 -060042 """Parse the available arguments.
43
44 Invalid arguments or -h cause this function to print a message and exit.
45
46 Args:
47 argv: List of string arguments (excluding program name / argv[0])
48
49 Returns:
50 argparse.Namespace object containing the attributes.
51 """
52 parser = argparse.ArgumentParser(
53 description='Converts source proto config into platform JSON config.')
54 parser.add_argument(
55 '-c',
56 '--project_configs',
57 nargs='+',
58 type=str,
59 help='Space delimited list of source protobinary project config files.')
60 parser.add_argument(
61 '-p',
62 '--program_config',
63 type=str,
64 help='Path to the source program-level protobinary file')
65 parser.add_argument(
Andrew Lamb2413c982020-05-29 12:15:36 -060066 '-o', '--output', type=str, help='Output file that will be generated')
David Burger7fd1dbe2020-03-26 09:26:55 -060067 return parser.parse_args(argv)
68
69
Andrew Lambcd33f702020-06-11 10:45:16 -060070def _set(field, target, target_name):
Sam McNally9a873f72020-06-05 19:47:22 +100071 if field or field == 0:
David Burger7fd1dbe2020-03-26 09:26:55 -060072 target[target_name] = field
73
74
Andrew Lambcd33f702020-06-11 10:45:16 -060075def _build_arc(config, config_files):
76 if not config.build_target.arc:
77 return None
78
79 build_properties = {
80 'device': config.build_target.arc.device,
81 'first-api-level': config.build_target.arc.first_api_level,
82 'marketing-name': config.device_brand.brand_name,
83 'metrics-tag': config.hw_design.name.lower(),
84 'product': config.build_target.id.value,
85 }
86 if config.oem:
87 build_properties['oem'] = config.oem.name
88 result = {'build-properties': build_properties}
89 feature_id = _arc_hardware_feature_id(config.hw_design_config)
90 if feature_id in config_files.arc_hw_features:
91 result['hardware-features'] = config_files.arc_hw_features[feature_id]
92 topology = config.hw_design_config.hardware_topology
93 ppi = topology.screen.hardware_feature.screen.panel_properties.pixels_per_in
94 # Only set for high resolution displays
95 if ppi and ppi > 250:
96 result['scale'] = ppi
97 return result
David Burger7fd1dbe2020-03-26 09:26:55 -060098
Andrew Lamb2413c982020-05-29 12:15:36 -060099
Andrew Lamb319cc922020-06-15 10:45:46 -0600100def _build_ash_flags(config: Config) -> List[str]:
101 """Returns a list of Ash flags for config.
102
103 Ash is the window manager and system UI for ChromeOS, see
104 https://chromium.googlesource.com/chromium/src/+/refs/heads/master/ash/.
105 """
106 # A map from flag name -> value. Value may be None for boolean flags.
107 flags = {}
108
109 hw_features = config.hw_design_config.hardware_features
110 if hw_features.stylus.stylus == topology_pb2.HardwareFeatures.Stylus.INTERNAL:
Andrew Lamb2e641e22020-06-15 12:30:41 -0600111 flags['has-internal-stylus'] = None
Andrew Lamb319cc922020-06-15 10:45:46 -0600112
Andrew Lamb2e641e22020-06-15 12:30:41 -0600113 fp_loc = hw_features.fingerprint.location
114 if fp_loc and fp_loc != topology_pb2.HardwareFeatures.Fingerprint.NOT_PRESENT:
115 loc_name = topology_pb2.HardwareFeatures.Fingerprint.Location.Name(fp_loc)
116 flags['fingerprint-sensor-location'] = loc_name.lower().replace('_', '-')
117
118 return sorted([f'--{k}={v}' if v else f'--{k}' for k, v in flags.items()])
Andrew Lamb319cc922020-06-15 10:45:46 -0600119
120
121def _build_ui(config: Config) -> dict:
122 """Builds the 'ui' property from cros_config_schema."""
123 return {'extra-ash-flags': _build_ash_flags(config)}
124
125
Andrew Lambcd33f702020-06-11 10:45:16 -0600126def _build_bluetooth(config, bluetooth_files):
C Shapiro90fda252020-04-17 14:34:57 -0500127 bt_flags = config.sw_config.bluetooth_config.flags
128 # Convert to native map (from proto wrapper)
129 bt_flags_map = dict(bt_flags)
130 result = {}
131 if bt_flags_map:
132 result['flags'] = bt_flags_map
C Shapiro74da76e2020-05-04 13:02:20 -0500133 bt_comp = config.hw_design_config.hardware_features.bluetooth.component.usb
C Shapiro90fda252020-04-17 14:34:57 -0500134 if bt_comp.vendor_id:
Andrew Lambcd33f702020-06-11 10:45:16 -0600135 bt_id = _bluetooth_id(config.hw_design.name.lower(), bt_comp)
C Shapiro90fda252020-04-17 14:34:57 -0500136 if bt_id in bluetooth_files:
137 result['config'] = bluetooth_files[bt_id]
138 return result
139
David Burger7fd1dbe2020-03-26 09:26:55 -0600140
Andrew Lambcd33f702020-06-11 10:45:16 -0600141def _build_fingerprint(hw_topology):
142 if not hw_topology.HasField('fingerprint'):
143 return None
144
145 fp = hw_topology.fingerprint.hardware_feature.fingerprint
146 result = {}
147 if fp.location != topology_pb2.HardwareFeatures.Fingerprint.NOT_PRESENT:
148 location = fp.Location.DESCRIPTOR.values_by_number[fp.location].name
149 result['sensor-location'] = location.lower().replace('_', '-')
150 if fp.board:
151 result['board'] = fp.board
152 return result
David Burger7fd1dbe2020-03-26 09:26:55 -0600153
154
Andrew Lambcd33f702020-06-11 10:45:16 -0600155def _fw_bcs_path(payload):
David Burger7fd1dbe2020-03-26 09:26:55 -0600156 if payload and payload.firmware_image_name:
Andrew Lamb2413c982020-05-29 12:15:36 -0600157 return 'bcs://%s.%d.%d.0.tbz2' % (payload.firmware_image_name,
158 payload.version.major,
159 payload.version.minor)
David Burger7fd1dbe2020-03-26 09:26:55 -0600160
Andrew Lambcd33f702020-06-11 10:45:16 -0600161 return None
David Burger7fd1dbe2020-03-26 09:26:55 -0600162
Andrew Lambcd33f702020-06-11 10:45:16 -0600163
164def _fw_build_target(payload):
David Burger7fd1dbe2020-03-26 09:26:55 -0600165 if payload:
166 return payload.build_target_name
167
Andrew Lambcd33f702020-06-11 10:45:16 -0600168 return None
David Burger7fd1dbe2020-03-26 09:26:55 -0600169
Andrew Lambcd33f702020-06-11 10:45:16 -0600170
171def _build_firmware(config):
David Burgerb70b6762020-05-21 12:14:59 -0600172 """Returns firmware config, or None if no build targets."""
Andrew Lamb3da156d2020-04-16 16:00:56 -0600173 fw_payload_config = config.sw_config.firmware
174 fw_build_config = config.sw_config.firmware_build_config
175 main_ro = fw_payload_config.main_ro_payload
176 main_rw = fw_payload_config.main_rw_payload
177 ec_ro = fw_payload_config.ec_ro_payload
178 pd_ro = fw_payload_config.pd_ro_payload
David Burger7fd1dbe2020-03-26 09:26:55 -0600179
180 build_targets = {}
Andrew Lamb3da156d2020-04-16 16:00:56 -0600181
Andrew Lambcd33f702020-06-11 10:45:16 -0600182 _set(fw_build_config.build_targets.depthcharge, build_targets, 'depthcharge')
183 _set(fw_build_config.build_targets.coreboot, build_targets, 'coreboot')
184 _set(fw_build_config.build_targets.ec, build_targets, 'ec')
185 _set(
Andrew Lambf8954ee2020-04-21 10:24:40 -0600186 list(fw_build_config.build_targets.ec_extras), build_targets, 'ec_extras')
Andrew Lambcd33f702020-06-11 10:45:16 -0600187 _set(fw_build_config.build_targets.libpayload, build_targets, 'libpayload')
David Burger7fd1dbe2020-03-26 09:26:55 -0600188
David Burgerb70b6762020-05-21 12:14:59 -0600189 if not build_targets:
190 return None
191
David Burger7fd1dbe2020-03-26 09:26:55 -0600192 result = {
193 'bcs-overlay': config.build_target.overlay_name,
194 'build-targets': build_targets,
David Burger7fd1dbe2020-03-26 09:26:55 -0600195 }
Andrew Lamb883fa042020-04-06 11:37:22 -0600196
Andrew Lambcd33f702020-06-11 10:45:16 -0600197 _set(main_ro.firmware_image_name.lower(), result, 'image-name')
Andrew Lamb883fa042020-04-06 11:37:22 -0600198
Andrew Lambcd33f702020-06-11 10:45:16 -0600199 _set(_fw_bcs_path(main_ro), result, 'main-ro-image')
200 _set(_fw_bcs_path(main_rw), result, 'main-rw-image')
201 _set(_fw_bcs_path(ec_ro), result, 'ec-ro-image')
202 _set(_fw_bcs_path(pd_ro), result, 'pd-ro-image')
David Burger7fd1dbe2020-03-26 09:26:55 -0600203
Andrew Lambcd33f702020-06-11 10:45:16 -0600204 _set(
Andrew Lambf39fbe82020-04-13 16:14:33 -0600205 config.hw_design_config.hardware_features.fw_config.value,
206 result,
207 'firmware-config',
208 )
209
David Burger7fd1dbe2020-03-26 09:26:55 -0600210 return result
211
212
Andrew Lambcd33f702020-06-11 10:45:16 -0600213def _build_fw_signing(config):
C Shapiro2f0bb5d2020-04-14 10:07:47 -0500214 if config.sw_config.firmware and config.device_signer_config:
David Burger68e0d142020-05-15 17:29:33 -0600215 hw_design = config.hw_design.name.lower()
C Shapiro2f0bb5d2020-04-14 10:07:47 -0500216 return {
217 'key-id': config.device_signer_config.key_id,
C Shapiro10e9a612020-05-19 17:06:43 -0500218 # TODO(shapiroc): Need to fix for whitelabel.
219 # Whitelabel will collide on unique signature-id values.
David Burger68e0d142020-05-15 17:29:33 -0600220 'signature-id': hw_design,
C Shapiro2f0bb5d2020-04-14 10:07:47 -0500221 }
222 return {}
David Burger7fd1dbe2020-03-26 09:26:55 -0600223
224
Andrew Lambcd33f702020-06-11 10:45:16 -0600225def _file(source, destination):
Andrew Lamb2413c982020-05-29 12:15:36 -0600226 return {'destination': destination, 'source': source}
David Burger7fd1dbe2020-03-26 09:26:55 -0600227
228
Andrew Lambcd33f702020-06-11 10:45:16 -0600229def _build_audio(config):
David Burger7fd1dbe2020-03-26 09:26:55 -0600230 alsa_path = '/usr/share/alsa/ucm'
231 cras_path = '/etc/cras'
232 project_name = config.hw_design.name.lower()
David Burger43250662020-05-07 11:21:50 -0600233 program_name = config.program.name.lower()
Andrew Lamb7d536782020-04-07 10:23:55 -0600234 if not config.sw_config.HasField('audio_config'):
David Burger7fd1dbe2020-03-26 09:26:55 -0600235 return {}
236 audio = config.sw_config.audio_config
237 card = audio.card_name
David Burger599ff7b2020-04-06 16:29:31 -0600238 card_with_suffix = audio.card_name
239 if audio.ucm_suffix:
240 card_with_suffix += '.' + audio.ucm_suffix
David Burger7fd1dbe2020-03-26 09:26:55 -0600241 files = []
242 if audio.ucm_file:
Andrew Lamb2413c982020-05-29 12:15:36 -0600243 files.append(
Andrew Lambcd33f702020-06-11 10:45:16 -0600244 _file(audio.ucm_file,
Andrew Lamb2413c982020-05-29 12:15:36 -0600245 '%s/%s/HiFi.conf' % (alsa_path, card_with_suffix)))
David Burger7fd1dbe2020-03-26 09:26:55 -0600246 if audio.ucm_master_file:
Andrew Lamb2413c982020-05-29 12:15:36 -0600247 files.append(
Andrew Lambcd33f702020-06-11 10:45:16 -0600248 _file(audio.ucm_master_file, '%s/%s/%s.conf' %
Andrew Lamb2413c982020-05-29 12:15:36 -0600249 (alsa_path, card_with_suffix, card_with_suffix)))
David Burger7fd1dbe2020-03-26 09:26:55 -0600250 if audio.card_config_file:
Andrew Lamb2413c982020-05-29 12:15:36 -0600251 files.append(
Andrew Lambcd33f702020-06-11 10:45:16 -0600252 _file(audio.card_config_file,
Andrew Lamb2413c982020-05-29 12:15:36 -0600253 '%s/%s/%s' % (cras_path, project_name, card)))
David Burger7fd1dbe2020-03-26 09:26:55 -0600254 if audio.dsp_file:
255 files.append(
Andrew Lambcd33f702020-06-11 10:45:16 -0600256 _file(audio.dsp_file, '%s/%s/dsp.ini' % (cras_path, project_name)))
David Burgere1a37492020-05-06 09:29:24 -0600257 if audio.module_file:
258 files.append(
Andrew Lambcd33f702020-06-11 10:45:16 -0600259 _file(audio.module_file, '/etc/modprobe.d/alsa-%s.conf' % program_name))
David Burgere1a37492020-05-06 09:29:24 -0600260 if audio.board_file:
261 files.append(
Andrew Lambcd33f702020-06-11 10:45:16 -0600262 _file(audio.board_file, '%s/%s/board.ini' % (cras_path, project_name)))
David Burger599ff7b2020-04-06 16:29:31 -0600263
264 result = {
David Burger7fd1dbe2020-03-26 09:26:55 -0600265 'main': {
266 'cras-config-dir': project_name,
267 'files': files,
268 }
269 }
David Burger599ff7b2020-04-06 16:29:31 -0600270 if audio.ucm_suffix:
David Burger03cdcbd2020-04-13 13:54:48 -0600271 result['main']['ucm-suffix'] = audio.ucm_suffix
David Burger599ff7b2020-04-06 16:29:31 -0600272
273 return result
David Burger7fd1dbe2020-03-26 09:26:55 -0600274
275
Andrew Lambcd33f702020-06-11 10:45:16 -0600276def _build_camera(hw_topology):
David Burger8aa8fa32020-04-14 08:30:34 -0600277 if hw_topology.HasField('camera'):
278 camera = hw_topology.camera.hardware_feature.camera
279 result = {}
280 if camera.count.value:
281 result['count'] = camera.count.value
282 return result
283
Andrew Lambcd33f702020-06-11 10:45:16 -0600284 return None
David Burger8aa8fa32020-04-14 08:30:34 -0600285
Andrew Lambcd33f702020-06-11 10:45:16 -0600286
287def _build_identity(hw_scan_config, program, brand_scan_config=None):
David Burger7fd1dbe2020-03-26 09:26:55 -0600288 identity = {}
Andrew Lambcd33f702020-06-11 10:45:16 -0600289 _set(hw_scan_config.firmware_sku, identity, 'sku-id')
290 _set(hw_scan_config.smbios_name_match, identity, 'smbios-name-match')
Andrew Lamb7806ce92020-04-07 10:22:17 -0600291 # 'platform-name' is needed to support 'mosys platform name'. Clients should
292 # longer require platform name, but set it here for backwards compatibility.
Andrew Lambcd33f702020-06-11 10:45:16 -0600293 _set(program.name, identity, 'platform-name')
David Burger7fd1dbe2020-03-26 09:26:55 -0600294 # ARM architecture
Andrew Lambcd33f702020-06-11 10:45:16 -0600295 _set(hw_scan_config.device_tree_compatible_match, identity,
David Burger7fd1dbe2020-03-26 09:26:55 -0600296 'device-tree-compatible-match')
297
298 if brand_scan_config:
Andrew Lambcd33f702020-06-11 10:45:16 -0600299 _set(brand_scan_config.whitelabel_tag, identity, 'whitelabel-tag')
David Burger7fd1dbe2020-03-26 09:26:55 -0600300
301 return identity
302
303
Andrew Lambcd33f702020-06-11 10:45:16 -0600304def _lookup(id_value, id_map):
305 if not id_value.value:
306 return None
307
308 key = id_value.value
309 if key in id_map:
310 return id_map[id_value.value]
311 error = 'Failed to lookup %s with value: %s' % (
312 id_value.__class__.__name__.replace('Id', ''), key)
313 print(error)
314 print('Check the config contents provided:')
315 printer = pprint.PrettyPrinter(indent=4)
316 printer.pprint(id_map)
317 raise Exception(error)
David Burger7fd1dbe2020-03-26 09:26:55 -0600318
319
Andrew Lambcd33f702020-06-11 10:45:16 -0600320def _build_touch_file_config(config, project_name):
321 partners = {x.id.value: x for x in config.partners.value}
C Shapiro2b6d5332020-05-06 17:51:35 -0500322 files = []
323 for comp in config.components:
C Shapiro4813be62020-05-13 17:31:58 -0500324 touch = comp.touchscreen
325 # Everything is the same for Touch screen/pad, except different fields
326 if comp.HasField('touchpad'):
327 touch = comp.touchpad
328 if touch.product_id:
Andrew Lambcd33f702020-06-11 10:45:16 -0600329 vendor = _lookup(comp.manufacturer_id, partners)
C Shapiro2b6d5332020-05-06 17:51:35 -0500330 if not vendor:
Andrew Lamb2413c982020-05-29 12:15:36 -0600331 raise Exception("Manufacturer must be set for touch device %s" %
332 comp.id.value)
C Shapiro2b6d5332020-05-06 17:51:35 -0500333
C Shapiro4813be62020-05-13 17:31:58 -0500334 product_id = touch.product_id
335 fw_version = touch.fw_version
C Shapiro2b6d5332020-05-06 17:51:35 -0500336
C Shapiro5c6fc212020-05-13 16:32:09 -0500337 touch_vendor = vendor.touch_vendor
338 sym_link = touch_vendor.fw_file_format.format(
Andrew Lamb2413c982020-05-29 12:15:36 -0600339 vendor_name=vendor.name,
340 vendor_id=touch_vendor.vendor_id,
341 product_id=product_id,
342 fw_version=fw_version,
343 product_series=touch.product_series)
C Shapiro2b6d5332020-05-06 17:51:35 -0500344
345 file_name = "%s_%s.bin" % (product_id, fw_version)
346 fw_file_path = os.path.join(TOUCH_PATH, vendor.name, file_name)
347
348 if not os.path.exists(fw_file_path):
Andrew Lamb2413c982020-05-29 12:15:36 -0600349 raise Exception("Touchscreen fw bin file doesn't exist at: %s" %
350 fw_file_path)
C Shapiro2b6d5332020-05-06 17:51:35 -0500351
352 files.append({
Andrew Lamb2413c982020-05-29 12:15:36 -0600353 "destination":
354 "/opt/google/touch/firmware/%s_%s" % (vendor.name, file_name),
355 "source":
356 os.path.join(project_name, fw_file_path),
357 "symlink":
358 os.path.join("/lib/firmware", sym_link),
C Shapiro2b6d5332020-05-06 17:51:35 -0500359 })
360
361 result = {}
Andrew Lambcd33f702020-06-11 10:45:16 -0600362 _set(files, result, 'files')
C Shapiro2b6d5332020-05-06 17:51:35 -0500363 return result
364
365
Andrew Lambcd33f702020-06-11 10:45:16 -0600366def _transform_build_configs(config, config_files=ConfigFiles({}, {}, {},
367 None)):
368 # pylint: disable=too-many-locals,too-many-branches
369 partners = {x.id.value: x for x in config.partners.value}
370 programs = {x.id.value: x for x in config.programs.value}
David Burger7fd1dbe2020-03-26 09:26:55 -0600371 sw_configs = list(config.software_configs)
Andrew Lambcd33f702020-06-11 10:45:16 -0600372 brand_configs = {x.brand_id.value: x for x in config.brand_configs}
David Burger7fd1dbe2020-03-26 09:26:55 -0600373
C Shapiroa0b766c2020-03-31 08:35:28 -0500374 if len(config.build_targets) != 1:
375 # Artifact of sharing the config_bundle for analysis and transforms.
376 # Integrated analysis of multiple programs/projects it the only time
377 # having multiple build targets would be valid.
378 raise Exception('Single build_target required for transform')
379
David Burger7fd1dbe2020-03-26 09:26:55 -0600380 results = {}
381 for hw_design in config.designs.value:
382 if config.device_brands.value:
Andrew Lamb2413c982020-05-29 12:15:36 -0600383 device_brands = [
384 x for x in config.device_brands.value
385 if x.design_id.value == hw_design.id.value
386 ]
David Burger7fd1dbe2020-03-26 09:26:55 -0600387 else:
388 device_brands = [device_brand_pb2.DeviceBrand()]
389
390 for device_brand in device_brands:
391 # Brand config can be empty since platform JSON config allows it
392 brand_config = brand_config_pb2.BrandConfig()
393 if device_brand.id.value in brand_configs:
394 brand_config = brand_configs[device_brand.id.value]
395
396 for hw_design_config in hw_design.configs:
397 design_id = hw_design_config.id.value
Andrew Lamb2413c982020-05-29 12:15:36 -0600398 sw_config_matches = [
399 x for x in sw_configs if x.design_config_id.value == design_id
400 ]
David Burger7fd1dbe2020-03-26 09:26:55 -0600401 if len(sw_config_matches) == 1:
402 sw_config = sw_config_matches[0]
403 elif len(sw_config_matches) > 1:
404 raise Exception('Multiple software configs found for: %s' % design_id)
405 else:
406 raise Exception('Software config is required for: %s' % design_id)
407
Andrew Lambcd33f702020-06-11 10:45:16 -0600408 program = _lookup(hw_design.program_id, programs)
C Shapiroadefd7c2020-05-19 16:37:21 -0500409 signer_configs_by_design = {}
410 signer_configs_by_brand = {}
411 for signer_config in program.device_signer_configs:
412 design_id = signer_config.design_id.value
413 brand_id = signer_config.brand_id.value
414 if design_id:
415 signer_configs_by_design[design_id] = signer_config
416 elif brand_id:
417 signer_configs_by_brand[brand_id] = signer_config
418 else:
419 raise Exception('No ID found for signer config: %s' % signer_config)
420
C Shapiro2f0bb5d2020-04-14 10:07:47 -0500421 device_signer_config = None
C Shapiroadefd7c2020-05-19 16:37:21 -0500422 if signer_configs_by_design or signer_configs_by_brand:
423 design_id = hw_design.id.value
424 brand_id = device_brand.id.value
425 if design_id in signer_configs_by_design:
426 device_signer_config = signer_configs_by_design[design_id]
427 elif brand_id in signer_configs_by_brand:
428 device_signer_config = signer_configs_by_brand[brand_id]
429 else:
430 # Assume that if signer configs are set, every config is setup
Andrew Lamb2413c982020-05-29 12:15:36 -0600431 raise Exception('Signer config missing for design: %s, brand: %s' %
432 (design_id, brand_id))
C Shapiro2f0bb5d2020-04-14 10:07:47 -0500433
Andrew Lambcd33f702020-06-11 10:45:16 -0600434 transformed_config = _transform_build_config(
C Shapiro90fda252020-04-17 14:34:57 -0500435 Config(
436 program=program,
437 hw_design=hw_design,
Andrew Lambcd33f702020-06-11 10:45:16 -0600438 odm=_lookup(hw_design.odm_id, partners),
C Shapiro90fda252020-04-17 14:34:57 -0500439 hw_design_config=hw_design_config,
440 device_brand=device_brand,
441 device_signer_config=device_signer_config,
Andrew Lambcd33f702020-06-11 10:45:16 -0600442 oem=_lookup(device_brand.oem_id, partners),
C Shapiro90fda252020-04-17 14:34:57 -0500443 sw_config=sw_config,
444 brand_config=brand_config,
Andrew Lamb2413c982020-05-29 12:15:36 -0600445 build_target=config.build_targets[0]), config_files)
David Burger7fd1dbe2020-03-26 09:26:55 -0600446
Andrew Lamb2413c982020-05-29 12:15:36 -0600447 config_json = json.dumps(
448 transformed_config,
449 sort_keys=True,
450 indent=2,
451 separators=(',', ': '))
David Burger7fd1dbe2020-03-26 09:26:55 -0600452
453 if config_json not in results:
454 results[config_json] = transformed_config
455
456 return list(results.values())
457
458
Andrew Lambcd33f702020-06-11 10:45:16 -0600459def _transform_build_config(config, config_files):
David Burger7fd1dbe2020-03-26 09:26:55 -0600460 """Transforms Config instance into target platform JSON schema.
461
462 Args:
463 config: Config namedtuple
C Shapiro5bf23a72020-04-24 11:40:17 -0500464 config_files: Map to look up the generated config files.
David Burger7fd1dbe2020-03-26 09:26:55 -0600465
466 Returns:
467 Unique config payload based on the platform JSON schema.
468 """
469 result = {
Andrew Lamb2413c982020-05-29 12:15:36 -0600470 'identity':
Andrew Lambcd33f702020-06-11 10:45:16 -0600471 _build_identity(config.sw_config.id_scan_config, config.program,
472 config.brand_config.scan_config),
Andrew Lamb2413c982020-05-29 12:15:36 -0600473 'name':
474 config.hw_design.name.lower(),
David Burger7fd1dbe2020-03-26 09:26:55 -0600475 }
476
Andrew Lambcd33f702020-06-11 10:45:16 -0600477 _set(_build_arc(config, config_files), result, 'arc')
478 _set(_build_audio(config), result, 'audio')
479 _set(_build_bluetooth(config, config_files.bluetooth), result, 'bluetooth')
480 _set(config.device_brand.brand_code, result, 'brand-code')
481 _set(
482 _build_camera(config.hw_design_config.hardware_topology), result,
483 'camera')
484 _set(_build_firmware(config), result, 'firmware')
485 _set(_build_fw_signing(config), result, 'firmware-signing')
486 _set(
487 _build_fingerprint(config.hw_design_config.hardware_topology), result,
Andrew Lamb2413c982020-05-29 12:15:36 -0600488 'fingerprint')
Andrew Lamb319cc922020-06-15 10:45:46 -0600489
490 # TODO(crbug.com/1093837): Enable _build_ui for real programs once ready.
491 if config.program.id.value == "FAKE_PROGRAM":
492 _set(_build_ui(config), result, 'ui')
David Burger7fd1dbe2020-03-26 09:26:55 -0600493 power_prefs = config.sw_config.power_config.preferences
494 power_prefs_map = dict(
Andrew Lamb2413c982020-05-29 12:15:36 -0600495 (x.replace('_', '-'), power_prefs[x]) for x in power_prefs)
Andrew Lambcd33f702020-06-11 10:45:16 -0600496 _set(power_prefs_map, result, 'power')
David Burger52c9d322020-06-09 07:16:18 -0600497 if config_files.dptf_map:
498 # Prefer design specific if found, if not fall back to project wide config
499 # mapped under the empty string.
500 if config_files.dptf_map.get(config.hw_design.name):
501 dptf_file = config_files.dptf_map[config.hw_design.name]
502 else:
503 dptf_file = config_files.dptf_map.get('')
504 _set(dptf_file, result, 'thermal')
Andrew Lambcd33f702020-06-11 10:45:16 -0600505 _set(config_files.touch_fw, result, 'touch')
David Burger7fd1dbe2020-03-26 09:26:55 -0600506
507 return result
508
509
Andrew Lambcd33f702020-06-11 10:45:16 -0600510def write_output(configs, output=None):
David Burger7fd1dbe2020-03-26 09:26:55 -0600511 """Writes a list of configs to platform JSON format.
512
513 Args:
514 configs: List of config dicts defined in cros_config_schema.yaml
515 output: Target file output (if None, prints to stdout)
516 """
Andrew Lamb2413c982020-05-29 12:15:36 -0600517 json_output = json.dumps({'chromeos': {
518 'configs': configs,
519 }},
520 sort_keys=True,
521 indent=2,
522 separators=(',', ': '))
David Burger7fd1dbe2020-03-26 09:26:55 -0600523 if output:
524 with open(output, 'w') as output_stream:
525 # Using print function adds proper trailing newline.
526 print(json_output, file=output_stream)
527 else:
528 print(json_output)
529
530
Andrew Lambcd33f702020-06-11 10:45:16 -0600531def _bluetooth_id(project_name, bt_comp):
Andrew Lamb2413c982020-05-29 12:15:36 -0600532 return '_'.join(
533 [project_name, bt_comp.vendor_id, bt_comp.product_id, bt_comp.bcd_device])
C Shapiro90fda252020-04-17 14:34:57 -0500534
535
Andrew Lambcd33f702020-06-11 10:45:16 -0600536def _feature(name, present):
C Shapiro5bf23a72020-04-24 11:40:17 -0500537 attrib = {'name': name}
538 if present:
539 return etree.Element('feature', attrib=attrib)
Andrew Lambcd33f702020-06-11 10:45:16 -0600540
541 return etree.Element('unavailable-feature', attrib=attrib)
C Shapiro5bf23a72020-04-24 11:40:17 -0500542
543
Andrew Lambcd33f702020-06-11 10:45:16 -0600544def _any_present(features):
Andrew Lamb2413c982020-05-29 12:15:36 -0600545 return topology_pb2.HardwareFeatures.PRESENT in features
C Shapiro5bf23a72020-04-24 11:40:17 -0500546
547
Andrew Lambcd33f702020-06-11 10:45:16 -0600548def _arc_hardware_feature_id(design_config):
C Shapiro5bf23a72020-04-24 11:40:17 -0500549 return design_config.id.value.lower().replace(':', '_')
550
551
Andrew Lambcd33f702020-06-11 10:45:16 -0600552def _write_arc_hardware_feature_file(output_dir, file_name, config_content):
David Burger77a1d312020-05-23 16:05:45 -0600553 output_dir += '/arc'
554 os.makedirs(output_dir, exist_ok=True)
555 output = '%s/%s' % (output_dir, file_name)
Andrew Lamb2413c982020-05-29 12:15:36 -0600556 file_content = minidom.parseString(config_content).toprettyxml(
557 indent=' ', encoding='utf-8')
C Shapiroea33cff2020-05-11 13:32:05 -0500558
559 with open(output, 'wb') as f:
560 f.write(file_content)
561
562
Andrew Lambcd33f702020-06-11 10:45:16 -0600563def _write_arc_hardware_feature_files(config, output_dir, build_root_dir):
C Shapiro5bf23a72020-04-24 11:40:17 -0500564 """Writes ARC hardware_feature.xml files for each config
565
566 Args:
567 config: Source ConfigBundle to process.
568 output_dir: Path to the generated output.
C Shapiro5c877992020-04-29 12:11:28 -0500569 build_root_path: Path to the config file from portage's perspective.
C Shapiro5bf23a72020-04-24 11:40:17 -0500570 Returns:
571 dict that maps the design_config_id onto the correct file.
572 """
Andrew Lambcd33f702020-06-11 10:45:16 -0600573 # pylint: disable=too-many-locals
C Shapiro5bf23a72020-04-24 11:40:17 -0500574 result = {}
C Shapiroea33cff2020-05-11 13:32:05 -0500575 configs_by_design = {}
C Shapiro5bf23a72020-04-24 11:40:17 -0500576 for hw_design in config.designs.value:
577 for design_config in hw_design.configs:
578 hw_features = design_config.hardware_features
579 multi_camera = hw_features.camera.count == 2
Andrew Lambcd33f702020-06-11 10:45:16 -0600580 touchscreen = _any_present([hw_features.screen.touch_support])
C Shapiro5bf23a72020-04-24 11:40:17 -0500581 acc = hw_features.accelerometer
582 gyro = hw_features.gyroscope
583 compass = hw_features.magnetometer
Andrew Lambcd33f702020-06-11 10:45:16 -0600584 light_sensor = hw_features.light_sensor
C Shapiro5bf23a72020-04-24 11:40:17 -0500585 root = etree.Element('permissions')
586 root.extend([
Andrew Lambcd33f702020-06-11 10:45:16 -0600587 _feature('android.hardware.camera', multi_camera),
588 _feature('android.hardware.camera.autofocus', multi_camera),
589 _feature(
590 'android.hardware.sensor.accelerometer',
591 _any_present([acc.lid_accelerometer, acc.base_accelerometer])),
592 _feature('android.hardware.sensor.gyroscope',
593 _any_present([gyro.lid_gyroscope, gyro.base_gyroscope])),
594 _feature(
Andrew Lamb2413c982020-05-29 12:15:36 -0600595 'android.hardware.sensor.compass',
Andrew Lambcd33f702020-06-11 10:45:16 -0600596 _any_present(
597 [compass.lid_magnetometer, compass.base_magnetometer])),
598 _feature(
599 'android.hardware.sensor.light',
600 _any_present(
601 [light_sensor.lid_lightsensor,
602 light_sensor.base_lightsensor])),
603 _feature('android.hardware.touchscreen', touchscreen),
604 _feature('android.hardware.touchscreen.multitouch', touchscreen),
605 _feature('android.hardware.touchscreen.multitouch.distinct',
Andrew Lamb2413c982020-05-29 12:15:36 -0600606 touchscreen),
Andrew Lambcd33f702020-06-11 10:45:16 -0600607 _feature('android.hardware.touchscreen.multitouch.jazzhand',
Andrew Lamb2413c982020-05-29 12:15:36 -0600608 touchscreen),
C Shapiro5bf23a72020-04-24 11:40:17 -0500609 ])
610
C Shapiroea33cff2020-05-11 13:32:05 -0500611 design_name = hw_design.name.lower()
C Shapiro5bf23a72020-04-24 11:40:17 -0500612
C Shapiroea33cff2020-05-11 13:32:05 -0500613 # Constructs the following map:
614 # design_name -> config -> design_configs
615 # This allows any of the following file naming schemes:
616 # - All configs within a design share config (design_name prefix only)
617 # - Nobody shares (full design_name and config id prefix needed)
618 #
619 # Having shared configs when possible makes code reviews easier around
620 # the configs and makes debugging easier on the platform side.
621 config_content = etree.tostring(root)
622 arc_configs = configs_by_design.get(design_name, {})
623 design_configs = arc_configs.get(config_content, [])
624 design_configs.append(design_config)
625 arc_configs[config_content] = design_configs
626 configs_by_design[design_name] = arc_configs
C Shapiro9a3ac8c2020-04-25 07:49:21 -0500627
C Shapiroea33cff2020-05-11 13:32:05 -0500628 for design_name, unique_configs in configs_by_design.items():
629 for file_content, design_configs in unique_configs.items():
Andrew Lamb2413c982020-05-29 12:15:36 -0600630 file_name = 'hardware_features_%s.xml' % design_name
631 if len(unique_configs) == 1:
Andrew Lambcd33f702020-06-11 10:45:16 -0600632 _write_arc_hardware_feature_file(output_dir, file_name, file_content)
C Shapiro9a3ac8c2020-04-25 07:49:21 -0500633
Andrew Lamb2413c982020-05-29 12:15:36 -0600634 for design_config in design_configs:
Andrew Lambcd33f702020-06-11 10:45:16 -0600635 feature_id = _arc_hardware_feature_id(design_config)
Andrew Lamb2413c982020-05-29 12:15:36 -0600636 if len(unique_configs) > 1:
637 file_name = 'hardware_features_%s.xml' % feature_id
Andrew Lambcd33f702020-06-11 10:45:16 -0600638 _write_arc_hardware_feature_file(output_dir, file_name, file_content)
Andrew Lamb2413c982020-05-29 12:15:36 -0600639 result[feature_id] = {
640 'build-path': '%s/arc/%s' % (build_root_dir, file_name),
641 'system-path': '/etc/%s' % file_name,
642 }
C Shapiro5bf23a72020-04-24 11:40:17 -0500643 return result
644
645
Andrew Lambcd33f702020-06-11 10:45:16 -0600646def _write_bluetooth_config_files(config, output_dir, build_root_path):
C Shapiro90fda252020-04-17 14:34:57 -0500647 """Writes bluetooth conf files for every unique bluetooth chip.
648
649 Args:
650 config: Source ConfigBundle to process.
651 output_dir: Path to the generated output.
C Shapiro5c877992020-04-29 12:11:28 -0500652 build_root_path: Path to the config file from portage's perspective.
C Shapiro90fda252020-04-17 14:34:57 -0500653 Returns:
654 dict that maps the bluetooth component id onto the file config.
655 """
David Burger77a1d312020-05-23 16:05:45 -0600656 output_dir += '/bluetooth'
C Shapiro90fda252020-04-17 14:34:57 -0500657 result = {}
658 for hw_design in config.designs.value:
659 project_name = hw_design.name.lower()
660 for design_config in hw_design.configs:
C Shapiro74da76e2020-05-04 13:02:20 -0500661 bt_comp = design_config.hardware_features.bluetooth.component.usb
C Shapiro90fda252020-04-17 14:34:57 -0500662 if bt_comp.vendor_id:
Andrew Lambcd33f702020-06-11 10:45:16 -0600663 bt_id = _bluetooth_id(project_name, bt_comp)
C Shapiro90fda252020-04-17 14:34:57 -0500664 result[bt_id] = {
C Shapiro5c877992020-04-29 12:11:28 -0500665 'build-path': '%s/bluetooth/%s.conf' % (build_root_path, bt_id),
C Shapiro90fda252020-04-17 14:34:57 -0500666 'system-path': '/etc/bluetooth/%s/main.conf' % bt_id,
667 }
668 bt_content = '''[General]
Andrew Lamb2413c982020-05-29 12:15:36 -0600669DeviceID = bluetooth:%s:%s:%s''' % (bt_comp.vendor_id, bt_comp.product_id,
C Shapiro90fda252020-04-17 14:34:57 -0500670 bt_comp.bcd_device)
671
David Burger77a1d312020-05-23 16:05:45 -0600672 os.makedirs(output_dir, exist_ok=True)
673 output = '%s/%s.conf' % (output_dir, bt_id)
C Shapiro90fda252020-04-17 14:34:57 -0500674 with open(output, 'w') as output_stream:
675 # Using print function adds proper trailing newline.
676 print(bt_content, file=output_stream)
677 return result
678
679
Andrew Lambcd33f702020-06-11 10:45:16 -0600680def _read_config(path):
David Burgerd4f32962020-05-02 12:07:40 -0600681 """Reads a ConfigBundle proto from a json pb file.
David Burgere6f76222020-04-27 11:08:01 -0600682
683 Args:
David Burgerd4f32962020-05-02 12:07:40 -0600684 path: Path to the file encoding the json pb proto.
David Burgere6f76222020-04-27 11:08:01 -0600685 """
686 config = config_bundle_pb2.ConfigBundle()
687 with open(path, 'r') as f:
688 return json_format.Parse(f.read(), config)
689
690
Andrew Lambcd33f702020-06-11 10:45:16 -0600691def _merge_configs(configs):
David Burger7fd1dbe2020-03-26 09:26:55 -0600692 result = config_bundle_pb2.ConfigBundle()
693 for config in configs:
694 result.MergeFrom(config)
695
696 return result
697
698
David Burger52c9d322020-06-09 07:16:18 -0600699def _dptf_map(configs, project_name):
700 """Produces a dptf map for the given configs.
701
702 Produces a map that maps from design name to the dptf file config for that
703 design. It looks for the dptf files at:
704 DPTF_PATH + DPTF_FILE
705 for a project wide config, that it maps under the empty string, and at:
706 DPTF_PATH + design_name + DPTF_FILE
707 for design specific configs that it maps under the design name.
708
709 Args:
710 configs: Source ConfigBundle to process.
711 project_name: Name of project processing for.
712
713 Returns:
714 map from design name or empty string (project wide), to dptf config
715 """
716 result = {}
717 project_dptf_path = os.path.join(project_name, 'dptf.dv')
718 # Looking at top level for project wide, and then for each design name
719 # for design specific.
720 dirs = [""] + [d.name for d in configs.designs.value]
721 for directory in dirs:
722 if os.path.exists(os.path.join(DPTF_PATH, directory, DPTF_FILE)):
723 dptf_file = {
724 'dptf-dv':
725 project_dptf_path,
726 'files': [
727 _file(
728 os.path.join(project_name, DPTF_PATH, directory, DPTF_FILE),
729 os.path.join('/etc/dptf', project_dptf_path))
730 ]
731 }
732 result[directory] = dptf_file
733 return result
734
735
Andrew Lambcd33f702020-06-11 10:45:16 -0600736def Main(project_configs, program_config, output): # pylint: disable=invalid-name
David Burger7fd1dbe2020-03-26 09:26:55 -0600737 """Transforms source proto config into platform JSON.
738
739 Args:
740 project_configs: List of source project configs to transform.
741 program_config: Program config for the given set of projects.
742 output: Output file that will be generated by the transform.
743 """
Andrew Lambcd33f702020-06-11 10:45:16 -0600744 configs = _merge_configs([_read_config(program_config)] +
745 [_read_config(config) for config in project_configs])
C Shapiro5bf23a72020-04-24 11:40:17 -0500746 bluetooth_files = {}
747 arc_hw_feature_files = {}
C Shapiro2b6d5332020-05-06 17:51:35 -0500748 touch_fw = {}
David Burger52c9d322020-06-09 07:16:18 -0600749 dptf_map = {}
C Shapiro5bf23a72020-04-24 11:40:17 -0500750 output_dir = os.path.dirname(output)
C Shapiro5c877992020-04-29 12:11:28 -0500751 build_root_dir = output_dir
C Shapiro5c877992020-04-29 12:11:28 -0500752 if 'sw_build_config' in output_dir:
753 full_path = os.path.realpath(output)
Andrew Lamb2413c982020-05-29 12:15:36 -0600754 project_name = re.match(r'.*/(\w*)/sw_build_config/.*',
755 full_path).groups(1)[0]
C Shapiro5c877992020-04-29 12:11:28 -0500756 # Projects don't know about each other until they are integrated into the
757 # build system. When this happens, the files need to be able to co-exist
758 # without any collisions. This prefixes the project name (which is how
759 # portage maps in the project), so project files co-exist and can be
760 # installed together.
761 # This is necessary to allow projects to share files at the program level
762 # without having portage file installation collisions.
763 build_root_dir = os.path.join(project_name, output_dir)
C Shapiro6830e6c2020-04-29 13:29:56 -0500764
David Burger52c9d322020-06-09 07:16:18 -0600765 dptf_map = _dptf_map(configs, project_name)
766
C Shapiro2b6d5332020-05-06 17:51:35 -0500767 if os.path.exists(TOUCH_PATH):
Andrew Lambcd33f702020-06-11 10:45:16 -0600768 touch_fw = _build_touch_file_config(configs, project_name)
769 bluetooth_files = _write_bluetooth_config_files(configs, output_dir,
770 build_root_dir)
771 arc_hw_feature_files = _write_arc_hardware_feature_files(
772 configs, output_dir, build_root_dir)
C Shapiro5bf23a72020-04-24 11:40:17 -0500773 config_files = ConfigFiles(
774 bluetooth=bluetooth_files,
775 arc_hw_features=arc_hw_feature_files,
C Shapiro2b6d5332020-05-06 17:51:35 -0500776 touch_fw=touch_fw,
David Burger52c9d322020-06-09 07:16:18 -0600777 dptf_map=dptf_map)
Andrew Lambcd33f702020-06-11 10:45:16 -0600778 write_output(_transform_build_configs(configs, config_files), output)
David Burger7fd1dbe2020-03-26 09:26:55 -0600779
780
781def main(argv=None):
782 """Main program which parses args and runs
783
784 Args:
785 argv: List of command line arguments, if None uses sys.argv.
786 """
787 if argv is None:
788 argv = sys.argv[1:]
Andrew Lambcd33f702020-06-11 10:45:16 -0600789 opts = parse_args(argv)
David Burger7fd1dbe2020-03-26 09:26:55 -0600790 Main(opts.project_configs, opts.program_config, opts.output)
791
792
793if __name__ == '__main__':
794 sys.exit(main(sys.argv[1:]))