blob: f45a9dfd663f3d84514f0b14c3a189425b9e7132 [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
17from collections import namedtuple
18
Andrew Lambcd33f702020-06-11 10:45:16 -060019from google.protobuf import json_format
20
Prathmesh Prabhu72f8a002020-04-10 09:57:53 -070021from chromiumos.config.api import device_brand_pb2
David Burger92609a32020-04-23 10:38:50 -060022from chromiumos.config.api import topology_pb2
C Shapiro5bf23a72020-04-24 11:40:17 -050023from chromiumos.config.payload import config_bundle_pb2
Prathmesh Prabhu72f8a002020-04-10 09:57:53 -070024from chromiumos.config.api.software import brand_config_pb2
David Burger7fd1dbe2020-03-26 09:26:55 -060025
Andrew Lamb2413c982020-05-29 12:15:36 -060026Config = namedtuple('Config', [
27 'program', 'hw_design', 'odm', 'hw_design_config', 'device_brand',
28 'device_signer_config', 'oem', 'sw_config', 'brand_config', 'build_target'
29])
David Burger7fd1dbe2020-03-26 09:26:55 -060030
Andrew Lamb2413c982020-05-29 12:15:36 -060031ConfigFiles = namedtuple(
32 'ConfigFiles', ['bluetooth', 'arc_hw_features', 'touch_fw', 'dptf_file'])
C Shapiro5bf23a72020-04-24 11:40:17 -050033
C Shapiro6830e6c2020-04-29 13:29:56 -050034DPTF_PATH = 'sw_build_config/platform/chromeos-config/thermal/dptf.dv'
C Shapiro2b6d5332020-05-06 17:51:35 -050035TOUCH_PATH = 'sw_build_config/platform/chromeos-config/touch'
David Burger7fd1dbe2020-03-26 09:26:55 -060036
Andrew Lamb2413c982020-05-29 12:15:36 -060037
Andrew Lambcd33f702020-06-11 10:45:16 -060038def parse_args(argv):
David Burger7fd1dbe2020-03-26 09:26:55 -060039 """Parse the available arguments.
40
41 Invalid arguments or -h cause this function to print a message and exit.
42
43 Args:
44 argv: List of string arguments (excluding program name / argv[0])
45
46 Returns:
47 argparse.Namespace object containing the attributes.
48 """
49 parser = argparse.ArgumentParser(
50 description='Converts source proto config into platform JSON config.')
51 parser.add_argument(
52 '-c',
53 '--project_configs',
54 nargs='+',
55 type=str,
56 help='Space delimited list of source protobinary project config files.')
57 parser.add_argument(
58 '-p',
59 '--program_config',
60 type=str,
61 help='Path to the source program-level protobinary file')
62 parser.add_argument(
Andrew Lamb2413c982020-05-29 12:15:36 -060063 '-o', '--output', type=str, help='Output file that will be generated')
David Burger7fd1dbe2020-03-26 09:26:55 -060064 return parser.parse_args(argv)
65
66
Andrew Lambcd33f702020-06-11 10:45:16 -060067def _set(field, target, target_name):
Sam McNally9a873f72020-06-05 19:47:22 +100068 if field or field == 0:
David Burger7fd1dbe2020-03-26 09:26:55 -060069 target[target_name] = field
70
71
Andrew Lambcd33f702020-06-11 10:45:16 -060072def _build_arc(config, config_files):
73 if not config.build_target.arc:
74 return None
75
76 build_properties = {
77 'device': config.build_target.arc.device,
78 'first-api-level': config.build_target.arc.first_api_level,
79 'marketing-name': config.device_brand.brand_name,
80 'metrics-tag': config.hw_design.name.lower(),
81 'product': config.build_target.id.value,
82 }
83 if config.oem:
84 build_properties['oem'] = config.oem.name
85 result = {'build-properties': build_properties}
86 feature_id = _arc_hardware_feature_id(config.hw_design_config)
87 if feature_id in config_files.arc_hw_features:
88 result['hardware-features'] = config_files.arc_hw_features[feature_id]
89 topology = config.hw_design_config.hardware_topology
90 ppi = topology.screen.hardware_feature.screen.panel_properties.pixels_per_in
91 # Only set for high resolution displays
92 if ppi and ppi > 250:
93 result['scale'] = ppi
94 return result
David Burger7fd1dbe2020-03-26 09:26:55 -060095
Andrew Lamb2413c982020-05-29 12:15:36 -060096
Andrew Lambcd33f702020-06-11 10:45:16 -060097def _build_bluetooth(config, bluetooth_files):
C Shapiro90fda252020-04-17 14:34:57 -050098 bt_flags = config.sw_config.bluetooth_config.flags
99 # Convert to native map (from proto wrapper)
100 bt_flags_map = dict(bt_flags)
101 result = {}
102 if bt_flags_map:
103 result['flags'] = bt_flags_map
C Shapiro74da76e2020-05-04 13:02:20 -0500104 bt_comp = config.hw_design_config.hardware_features.bluetooth.component.usb
C Shapiro90fda252020-04-17 14:34:57 -0500105 if bt_comp.vendor_id:
Andrew Lambcd33f702020-06-11 10:45:16 -0600106 bt_id = _bluetooth_id(config.hw_design.name.lower(), bt_comp)
C Shapiro90fda252020-04-17 14:34:57 -0500107 if bt_id in bluetooth_files:
108 result['config'] = bluetooth_files[bt_id]
109 return result
110
David Burger7fd1dbe2020-03-26 09:26:55 -0600111
Andrew Lambcd33f702020-06-11 10:45:16 -0600112def _build_fingerprint(hw_topology):
113 if not hw_topology.HasField('fingerprint'):
114 return None
115
116 fp = hw_topology.fingerprint.hardware_feature.fingerprint
117 result = {}
118 if fp.location != topology_pb2.HardwareFeatures.Fingerprint.NOT_PRESENT:
119 location = fp.Location.DESCRIPTOR.values_by_number[fp.location].name
120 result['sensor-location'] = location.lower().replace('_', '-')
121 if fp.board:
122 result['board'] = fp.board
123 return result
David Burger7fd1dbe2020-03-26 09:26:55 -0600124
125
Andrew Lambcd33f702020-06-11 10:45:16 -0600126def _fw_bcs_path(payload):
David Burger7fd1dbe2020-03-26 09:26:55 -0600127 if payload and payload.firmware_image_name:
Andrew Lamb2413c982020-05-29 12:15:36 -0600128 return 'bcs://%s.%d.%d.0.tbz2' % (payload.firmware_image_name,
129 payload.version.major,
130 payload.version.minor)
David Burger7fd1dbe2020-03-26 09:26:55 -0600131
Andrew Lambcd33f702020-06-11 10:45:16 -0600132 return None
David Burger7fd1dbe2020-03-26 09:26:55 -0600133
Andrew Lambcd33f702020-06-11 10:45:16 -0600134
135def _fw_build_target(payload):
David Burger7fd1dbe2020-03-26 09:26:55 -0600136 if payload:
137 return payload.build_target_name
138
Andrew Lambcd33f702020-06-11 10:45:16 -0600139 return None
David Burger7fd1dbe2020-03-26 09:26:55 -0600140
Andrew Lambcd33f702020-06-11 10:45:16 -0600141
142def _build_firmware(config):
David Burgerb70b6762020-05-21 12:14:59 -0600143 """Returns firmware config, or None if no build targets."""
Andrew Lamb3da156d2020-04-16 16:00:56 -0600144 fw_payload_config = config.sw_config.firmware
145 fw_build_config = config.sw_config.firmware_build_config
146 main_ro = fw_payload_config.main_ro_payload
147 main_rw = fw_payload_config.main_rw_payload
148 ec_ro = fw_payload_config.ec_ro_payload
149 pd_ro = fw_payload_config.pd_ro_payload
David Burger7fd1dbe2020-03-26 09:26:55 -0600150
151 build_targets = {}
Andrew Lamb3da156d2020-04-16 16:00:56 -0600152
Andrew Lambcd33f702020-06-11 10:45:16 -0600153 _set(fw_build_config.build_targets.depthcharge, build_targets, 'depthcharge')
154 _set(fw_build_config.build_targets.coreboot, build_targets, 'coreboot')
155 _set(fw_build_config.build_targets.ec, build_targets, 'ec')
156 _set(
Andrew Lambf8954ee2020-04-21 10:24:40 -0600157 list(fw_build_config.build_targets.ec_extras), build_targets, 'ec_extras')
Andrew Lambcd33f702020-06-11 10:45:16 -0600158 _set(fw_build_config.build_targets.libpayload, build_targets, 'libpayload')
David Burger7fd1dbe2020-03-26 09:26:55 -0600159
David Burgerb70b6762020-05-21 12:14:59 -0600160 if not build_targets:
161 return None
162
David Burger7fd1dbe2020-03-26 09:26:55 -0600163 result = {
164 'bcs-overlay': config.build_target.overlay_name,
165 'build-targets': build_targets,
David Burger7fd1dbe2020-03-26 09:26:55 -0600166 }
Andrew Lamb883fa042020-04-06 11:37:22 -0600167
Andrew Lambcd33f702020-06-11 10:45:16 -0600168 _set(main_ro.firmware_image_name.lower(), result, 'image-name')
Andrew Lamb883fa042020-04-06 11:37:22 -0600169
Andrew Lambcd33f702020-06-11 10:45:16 -0600170 _set(_fw_bcs_path(main_ro), result, 'main-ro-image')
171 _set(_fw_bcs_path(main_rw), result, 'main-rw-image')
172 _set(_fw_bcs_path(ec_ro), result, 'ec-ro-image')
173 _set(_fw_bcs_path(pd_ro), result, 'pd-ro-image')
David Burger7fd1dbe2020-03-26 09:26:55 -0600174
Andrew Lambcd33f702020-06-11 10:45:16 -0600175 _set(
Andrew Lambf39fbe82020-04-13 16:14:33 -0600176 config.hw_design_config.hardware_features.fw_config.value,
177 result,
178 'firmware-config',
179 )
180
David Burger7fd1dbe2020-03-26 09:26:55 -0600181 return result
182
183
Andrew Lambcd33f702020-06-11 10:45:16 -0600184def _build_fw_signing(config):
C Shapiro2f0bb5d2020-04-14 10:07:47 -0500185 if config.sw_config.firmware and config.device_signer_config:
David Burger68e0d142020-05-15 17:29:33 -0600186 hw_design = config.hw_design.name.lower()
C Shapiro2f0bb5d2020-04-14 10:07:47 -0500187 return {
188 'key-id': config.device_signer_config.key_id,
C Shapiro10e9a612020-05-19 17:06:43 -0500189 # TODO(shapiroc): Need to fix for whitelabel.
190 # Whitelabel will collide on unique signature-id values.
David Burger68e0d142020-05-15 17:29:33 -0600191 'signature-id': hw_design,
C Shapiro2f0bb5d2020-04-14 10:07:47 -0500192 }
193 return {}
David Burger7fd1dbe2020-03-26 09:26:55 -0600194
195
Andrew Lambcd33f702020-06-11 10:45:16 -0600196def _file(source, destination):
Andrew Lamb2413c982020-05-29 12:15:36 -0600197 return {'destination': destination, 'source': source}
David Burger7fd1dbe2020-03-26 09:26:55 -0600198
199
Andrew Lambcd33f702020-06-11 10:45:16 -0600200def _build_audio(config):
David Burger7fd1dbe2020-03-26 09:26:55 -0600201 alsa_path = '/usr/share/alsa/ucm'
202 cras_path = '/etc/cras'
203 project_name = config.hw_design.name.lower()
David Burger43250662020-05-07 11:21:50 -0600204 program_name = config.program.name.lower()
Andrew Lamb7d536782020-04-07 10:23:55 -0600205 if not config.sw_config.HasField('audio_config'):
David Burger7fd1dbe2020-03-26 09:26:55 -0600206 return {}
207 audio = config.sw_config.audio_config
208 card = audio.card_name
David Burger599ff7b2020-04-06 16:29:31 -0600209 card_with_suffix = audio.card_name
210 if audio.ucm_suffix:
211 card_with_suffix += '.' + audio.ucm_suffix
David Burger7fd1dbe2020-03-26 09:26:55 -0600212 files = []
213 if audio.ucm_file:
Andrew Lamb2413c982020-05-29 12:15:36 -0600214 files.append(
Andrew Lambcd33f702020-06-11 10:45:16 -0600215 _file(audio.ucm_file,
Andrew Lamb2413c982020-05-29 12:15:36 -0600216 '%s/%s/HiFi.conf' % (alsa_path, card_with_suffix)))
David Burger7fd1dbe2020-03-26 09:26:55 -0600217 if audio.ucm_master_file:
Andrew Lamb2413c982020-05-29 12:15:36 -0600218 files.append(
Andrew Lambcd33f702020-06-11 10:45:16 -0600219 _file(audio.ucm_master_file, '%s/%s/%s.conf' %
Andrew Lamb2413c982020-05-29 12:15:36 -0600220 (alsa_path, card_with_suffix, card_with_suffix)))
David Burger7fd1dbe2020-03-26 09:26:55 -0600221 if audio.card_config_file:
Andrew Lamb2413c982020-05-29 12:15:36 -0600222 files.append(
Andrew Lambcd33f702020-06-11 10:45:16 -0600223 _file(audio.card_config_file,
Andrew Lamb2413c982020-05-29 12:15:36 -0600224 '%s/%s/%s' % (cras_path, project_name, card)))
David Burger7fd1dbe2020-03-26 09:26:55 -0600225 if audio.dsp_file:
226 files.append(
Andrew Lambcd33f702020-06-11 10:45:16 -0600227 _file(audio.dsp_file, '%s/%s/dsp.ini' % (cras_path, project_name)))
David Burgere1a37492020-05-06 09:29:24 -0600228 if audio.module_file:
229 files.append(
Andrew Lambcd33f702020-06-11 10:45:16 -0600230 _file(audio.module_file, '/etc/modprobe.d/alsa-%s.conf' % program_name))
David Burgere1a37492020-05-06 09:29:24 -0600231 if audio.board_file:
232 files.append(
Andrew Lambcd33f702020-06-11 10:45:16 -0600233 _file(audio.board_file, '%s/%s/board.ini' % (cras_path, project_name)))
David Burger599ff7b2020-04-06 16:29:31 -0600234
235 result = {
David Burger7fd1dbe2020-03-26 09:26:55 -0600236 'main': {
237 'cras-config-dir': project_name,
238 'files': files,
239 }
240 }
David Burger599ff7b2020-04-06 16:29:31 -0600241 if audio.ucm_suffix:
David Burger03cdcbd2020-04-13 13:54:48 -0600242 result['main']['ucm-suffix'] = audio.ucm_suffix
David Burger599ff7b2020-04-06 16:29:31 -0600243
244 return result
David Burger7fd1dbe2020-03-26 09:26:55 -0600245
246
Andrew Lambcd33f702020-06-11 10:45:16 -0600247def _build_camera(hw_topology):
David Burger8aa8fa32020-04-14 08:30:34 -0600248 if hw_topology.HasField('camera'):
249 camera = hw_topology.camera.hardware_feature.camera
250 result = {}
251 if camera.count.value:
252 result['count'] = camera.count.value
253 return result
254
Andrew Lambcd33f702020-06-11 10:45:16 -0600255 return None
David Burger8aa8fa32020-04-14 08:30:34 -0600256
Andrew Lambcd33f702020-06-11 10:45:16 -0600257
258def _build_identity(hw_scan_config, program, brand_scan_config=None):
David Burger7fd1dbe2020-03-26 09:26:55 -0600259 identity = {}
Andrew Lambcd33f702020-06-11 10:45:16 -0600260 _set(hw_scan_config.firmware_sku, identity, 'sku-id')
261 _set(hw_scan_config.smbios_name_match, identity, 'smbios-name-match')
Andrew Lamb7806ce92020-04-07 10:22:17 -0600262 # 'platform-name' is needed to support 'mosys platform name'. Clients should
263 # longer require platform name, but set it here for backwards compatibility.
Andrew Lambcd33f702020-06-11 10:45:16 -0600264 _set(program.name, identity, 'platform-name')
David Burger7fd1dbe2020-03-26 09:26:55 -0600265 # ARM architecture
Andrew Lambcd33f702020-06-11 10:45:16 -0600266 _set(hw_scan_config.device_tree_compatible_match, identity,
David Burger7fd1dbe2020-03-26 09:26:55 -0600267 'device-tree-compatible-match')
268
269 if brand_scan_config:
Andrew Lambcd33f702020-06-11 10:45:16 -0600270 _set(brand_scan_config.whitelabel_tag, identity, 'whitelabel-tag')
David Burger7fd1dbe2020-03-26 09:26:55 -0600271
272 return identity
273
274
Andrew Lambcd33f702020-06-11 10:45:16 -0600275def _lookup(id_value, id_map):
276 if not id_value.value:
277 return None
278
279 key = id_value.value
280 if key in id_map:
281 return id_map[id_value.value]
282 error = 'Failed to lookup %s with value: %s' % (
283 id_value.__class__.__name__.replace('Id', ''), key)
284 print(error)
285 print('Check the config contents provided:')
286 printer = pprint.PrettyPrinter(indent=4)
287 printer.pprint(id_map)
288 raise Exception(error)
David Burger7fd1dbe2020-03-26 09:26:55 -0600289
290
Andrew Lambcd33f702020-06-11 10:45:16 -0600291def _build_touch_file_config(config, project_name):
292 partners = {x.id.value: x for x in config.partners.value}
C Shapiro2b6d5332020-05-06 17:51:35 -0500293 files = []
294 for comp in config.components:
C Shapiro4813be62020-05-13 17:31:58 -0500295 touch = comp.touchscreen
296 # Everything is the same for Touch screen/pad, except different fields
297 if comp.HasField('touchpad'):
298 touch = comp.touchpad
299 if touch.product_id:
Andrew Lambcd33f702020-06-11 10:45:16 -0600300 vendor = _lookup(comp.manufacturer_id, partners)
C Shapiro2b6d5332020-05-06 17:51:35 -0500301 if not vendor:
Andrew Lamb2413c982020-05-29 12:15:36 -0600302 raise Exception("Manufacturer must be set for touch device %s" %
303 comp.id.value)
C Shapiro2b6d5332020-05-06 17:51:35 -0500304
C Shapiro4813be62020-05-13 17:31:58 -0500305 product_id = touch.product_id
306 fw_version = touch.fw_version
C Shapiro2b6d5332020-05-06 17:51:35 -0500307
C Shapiro5c6fc212020-05-13 16:32:09 -0500308 touch_vendor = vendor.touch_vendor
309 sym_link = touch_vendor.fw_file_format.format(
Andrew Lamb2413c982020-05-29 12:15:36 -0600310 vendor_name=vendor.name,
311 vendor_id=touch_vendor.vendor_id,
312 product_id=product_id,
313 fw_version=fw_version,
314 product_series=touch.product_series)
C Shapiro2b6d5332020-05-06 17:51:35 -0500315
316 file_name = "%s_%s.bin" % (product_id, fw_version)
317 fw_file_path = os.path.join(TOUCH_PATH, vendor.name, file_name)
318
319 if not os.path.exists(fw_file_path):
Andrew Lamb2413c982020-05-29 12:15:36 -0600320 raise Exception("Touchscreen fw bin file doesn't exist at: %s" %
321 fw_file_path)
C Shapiro2b6d5332020-05-06 17:51:35 -0500322
323 files.append({
Andrew Lamb2413c982020-05-29 12:15:36 -0600324 "destination":
325 "/opt/google/touch/firmware/%s_%s" % (vendor.name, file_name),
326 "source":
327 os.path.join(project_name, fw_file_path),
328 "symlink":
329 os.path.join("/lib/firmware", sym_link),
C Shapiro2b6d5332020-05-06 17:51:35 -0500330 })
331
332 result = {}
Andrew Lambcd33f702020-06-11 10:45:16 -0600333 _set(files, result, 'files')
C Shapiro2b6d5332020-05-06 17:51:35 -0500334 return result
335
336
Andrew Lambcd33f702020-06-11 10:45:16 -0600337def _transform_build_configs(config, config_files=ConfigFiles({}, {}, {},
338 None)):
339 # pylint: disable=too-many-locals,too-many-branches
340 partners = {x.id.value: x for x in config.partners.value}
341 programs = {x.id.value: x for x in config.programs.value}
David Burger7fd1dbe2020-03-26 09:26:55 -0600342 sw_configs = list(config.software_configs)
Andrew Lambcd33f702020-06-11 10:45:16 -0600343 brand_configs = {x.brand_id.value: x for x in config.brand_configs}
David Burger7fd1dbe2020-03-26 09:26:55 -0600344
C Shapiroa0b766c2020-03-31 08:35:28 -0500345 if len(config.build_targets) != 1:
346 # Artifact of sharing the config_bundle for analysis and transforms.
347 # Integrated analysis of multiple programs/projects it the only time
348 # having multiple build targets would be valid.
349 raise Exception('Single build_target required for transform')
350
David Burger7fd1dbe2020-03-26 09:26:55 -0600351 results = {}
352 for hw_design in config.designs.value:
353 if config.device_brands.value:
Andrew Lamb2413c982020-05-29 12:15:36 -0600354 device_brands = [
355 x for x in config.device_brands.value
356 if x.design_id.value == hw_design.id.value
357 ]
David Burger7fd1dbe2020-03-26 09:26:55 -0600358 else:
359 device_brands = [device_brand_pb2.DeviceBrand()]
360
361 for device_brand in device_brands:
362 # Brand config can be empty since platform JSON config allows it
363 brand_config = brand_config_pb2.BrandConfig()
364 if device_brand.id.value in brand_configs:
365 brand_config = brand_configs[device_brand.id.value]
366
367 for hw_design_config in hw_design.configs:
368 design_id = hw_design_config.id.value
Andrew Lamb2413c982020-05-29 12:15:36 -0600369 sw_config_matches = [
370 x for x in sw_configs if x.design_config_id.value == design_id
371 ]
David Burger7fd1dbe2020-03-26 09:26:55 -0600372 if len(sw_config_matches) == 1:
373 sw_config = sw_config_matches[0]
374 elif len(sw_config_matches) > 1:
375 raise Exception('Multiple software configs found for: %s' % design_id)
376 else:
377 raise Exception('Software config is required for: %s' % design_id)
378
Andrew Lambcd33f702020-06-11 10:45:16 -0600379 program = _lookup(hw_design.program_id, programs)
C Shapiroadefd7c2020-05-19 16:37:21 -0500380 signer_configs_by_design = {}
381 signer_configs_by_brand = {}
382 for signer_config in program.device_signer_configs:
383 design_id = signer_config.design_id.value
384 brand_id = signer_config.brand_id.value
385 if design_id:
386 signer_configs_by_design[design_id] = signer_config
387 elif brand_id:
388 signer_configs_by_brand[brand_id] = signer_config
389 else:
390 raise Exception('No ID found for signer config: %s' % signer_config)
391
C Shapiro2f0bb5d2020-04-14 10:07:47 -0500392 device_signer_config = None
C Shapiroadefd7c2020-05-19 16:37:21 -0500393 if signer_configs_by_design or signer_configs_by_brand:
394 design_id = hw_design.id.value
395 brand_id = device_brand.id.value
396 if design_id in signer_configs_by_design:
397 device_signer_config = signer_configs_by_design[design_id]
398 elif brand_id in signer_configs_by_brand:
399 device_signer_config = signer_configs_by_brand[brand_id]
400 else:
401 # Assume that if signer configs are set, every config is setup
Andrew Lamb2413c982020-05-29 12:15:36 -0600402 raise Exception('Signer config missing for design: %s, brand: %s' %
403 (design_id, brand_id))
C Shapiro2f0bb5d2020-04-14 10:07:47 -0500404
Andrew Lambcd33f702020-06-11 10:45:16 -0600405 transformed_config = _transform_build_config(
C Shapiro90fda252020-04-17 14:34:57 -0500406 Config(
407 program=program,
408 hw_design=hw_design,
Andrew Lambcd33f702020-06-11 10:45:16 -0600409 odm=_lookup(hw_design.odm_id, partners),
C Shapiro90fda252020-04-17 14:34:57 -0500410 hw_design_config=hw_design_config,
411 device_brand=device_brand,
412 device_signer_config=device_signer_config,
Andrew Lambcd33f702020-06-11 10:45:16 -0600413 oem=_lookup(device_brand.oem_id, partners),
C Shapiro90fda252020-04-17 14:34:57 -0500414 sw_config=sw_config,
415 brand_config=brand_config,
Andrew Lamb2413c982020-05-29 12:15:36 -0600416 build_target=config.build_targets[0]), config_files)
David Burger7fd1dbe2020-03-26 09:26:55 -0600417
Andrew Lamb2413c982020-05-29 12:15:36 -0600418 config_json = json.dumps(
419 transformed_config,
420 sort_keys=True,
421 indent=2,
422 separators=(',', ': '))
David Burger7fd1dbe2020-03-26 09:26:55 -0600423
424 if config_json not in results:
425 results[config_json] = transformed_config
426
427 return list(results.values())
428
429
Andrew Lambcd33f702020-06-11 10:45:16 -0600430def _transform_build_config(config, config_files):
David Burger7fd1dbe2020-03-26 09:26:55 -0600431 """Transforms Config instance into target platform JSON schema.
432
433 Args:
434 config: Config namedtuple
C Shapiro5bf23a72020-04-24 11:40:17 -0500435 config_files: Map to look up the generated config files.
David Burger7fd1dbe2020-03-26 09:26:55 -0600436
437 Returns:
438 Unique config payload based on the platform JSON schema.
439 """
440 result = {
Andrew Lamb2413c982020-05-29 12:15:36 -0600441 'identity':
Andrew Lambcd33f702020-06-11 10:45:16 -0600442 _build_identity(config.sw_config.id_scan_config, config.program,
443 config.brand_config.scan_config),
Andrew Lamb2413c982020-05-29 12:15:36 -0600444 'name':
445 config.hw_design.name.lower(),
David Burger7fd1dbe2020-03-26 09:26:55 -0600446 }
447
Andrew Lambcd33f702020-06-11 10:45:16 -0600448 _set(_build_arc(config, config_files), result, 'arc')
449 _set(_build_audio(config), result, 'audio')
450 _set(_build_bluetooth(config, config_files.bluetooth), result, 'bluetooth')
451 _set(config.device_brand.brand_code, result, 'brand-code')
452 _set(
453 _build_camera(config.hw_design_config.hardware_topology), result,
454 'camera')
455 _set(_build_firmware(config), result, 'firmware')
456 _set(_build_fw_signing(config), result, 'firmware-signing')
457 _set(
458 _build_fingerprint(config.hw_design_config.hardware_topology), result,
Andrew Lamb2413c982020-05-29 12:15:36 -0600459 'fingerprint')
David Burger7fd1dbe2020-03-26 09:26:55 -0600460 power_prefs = config.sw_config.power_config.preferences
461 power_prefs_map = dict(
Andrew Lamb2413c982020-05-29 12:15:36 -0600462 (x.replace('_', '-'), power_prefs[x]) for x in power_prefs)
Andrew Lambcd33f702020-06-11 10:45:16 -0600463 _set(power_prefs_map, result, 'power')
464 _set(config_files.dptf_file, result, 'thermal')
465 _set(config_files.touch_fw, result, 'touch')
David Burger7fd1dbe2020-03-26 09:26:55 -0600466
467 return result
468
469
Andrew Lambcd33f702020-06-11 10:45:16 -0600470def write_output(configs, output=None):
David Burger7fd1dbe2020-03-26 09:26:55 -0600471 """Writes a list of configs to platform JSON format.
472
473 Args:
474 configs: List of config dicts defined in cros_config_schema.yaml
475 output: Target file output (if None, prints to stdout)
476 """
Andrew Lamb2413c982020-05-29 12:15:36 -0600477 json_output = json.dumps({'chromeos': {
478 'configs': configs,
479 }},
480 sort_keys=True,
481 indent=2,
482 separators=(',', ': '))
David Burger7fd1dbe2020-03-26 09:26:55 -0600483 if output:
484 with open(output, 'w') as output_stream:
485 # Using print function adds proper trailing newline.
486 print(json_output, file=output_stream)
487 else:
488 print(json_output)
489
490
Andrew Lambcd33f702020-06-11 10:45:16 -0600491def _bluetooth_id(project_name, bt_comp):
Andrew Lamb2413c982020-05-29 12:15:36 -0600492 return '_'.join(
493 [project_name, bt_comp.vendor_id, bt_comp.product_id, bt_comp.bcd_device])
C Shapiro90fda252020-04-17 14:34:57 -0500494
495
Andrew Lambcd33f702020-06-11 10:45:16 -0600496def _feature(name, present):
C Shapiro5bf23a72020-04-24 11:40:17 -0500497 attrib = {'name': name}
498 if present:
499 return etree.Element('feature', attrib=attrib)
Andrew Lambcd33f702020-06-11 10:45:16 -0600500
501 return etree.Element('unavailable-feature', attrib=attrib)
C Shapiro5bf23a72020-04-24 11:40:17 -0500502
503
Andrew Lambcd33f702020-06-11 10:45:16 -0600504def _any_present(features):
Andrew Lamb2413c982020-05-29 12:15:36 -0600505 return topology_pb2.HardwareFeatures.PRESENT in features
C Shapiro5bf23a72020-04-24 11:40:17 -0500506
507
Andrew Lambcd33f702020-06-11 10:45:16 -0600508def _arc_hardware_feature_id(design_config):
C Shapiro5bf23a72020-04-24 11:40:17 -0500509 return design_config.id.value.lower().replace(':', '_')
510
511
Andrew Lambcd33f702020-06-11 10:45:16 -0600512def _write_arc_hardware_feature_file(output_dir, file_name, config_content):
David Burger77a1d312020-05-23 16:05:45 -0600513 output_dir += '/arc'
514 os.makedirs(output_dir, exist_ok=True)
515 output = '%s/%s' % (output_dir, file_name)
Andrew Lamb2413c982020-05-29 12:15:36 -0600516 file_content = minidom.parseString(config_content).toprettyxml(
517 indent=' ', encoding='utf-8')
C Shapiroea33cff2020-05-11 13:32:05 -0500518
519 with open(output, 'wb') as f:
520 f.write(file_content)
521
522
Andrew Lambcd33f702020-06-11 10:45:16 -0600523def _write_arc_hardware_feature_files(config, output_dir, build_root_dir):
C Shapiro5bf23a72020-04-24 11:40:17 -0500524 """Writes ARC hardware_feature.xml files for each config
525
526 Args:
527 config: Source ConfigBundle to process.
528 output_dir: Path to the generated output.
C Shapiro5c877992020-04-29 12:11:28 -0500529 build_root_path: Path to the config file from portage's perspective.
C Shapiro5bf23a72020-04-24 11:40:17 -0500530 Returns:
531 dict that maps the design_config_id onto the correct file.
532 """
Andrew Lambcd33f702020-06-11 10:45:16 -0600533 # pylint: disable=too-many-locals
C Shapiro5bf23a72020-04-24 11:40:17 -0500534 result = {}
C Shapiroea33cff2020-05-11 13:32:05 -0500535 configs_by_design = {}
C Shapiro5bf23a72020-04-24 11:40:17 -0500536 for hw_design in config.designs.value:
537 for design_config in hw_design.configs:
538 hw_features = design_config.hardware_features
539 multi_camera = hw_features.camera.count == 2
Andrew Lambcd33f702020-06-11 10:45:16 -0600540 touchscreen = _any_present([hw_features.screen.touch_support])
C Shapiro5bf23a72020-04-24 11:40:17 -0500541 acc = hw_features.accelerometer
542 gyro = hw_features.gyroscope
543 compass = hw_features.magnetometer
Andrew Lambcd33f702020-06-11 10:45:16 -0600544 light_sensor = hw_features.light_sensor
C Shapiro5bf23a72020-04-24 11:40:17 -0500545 root = etree.Element('permissions')
546 root.extend([
Andrew Lambcd33f702020-06-11 10:45:16 -0600547 _feature('android.hardware.camera', multi_camera),
548 _feature('android.hardware.camera.autofocus', multi_camera),
549 _feature(
550 'android.hardware.sensor.accelerometer',
551 _any_present([acc.lid_accelerometer, acc.base_accelerometer])),
552 _feature('android.hardware.sensor.gyroscope',
553 _any_present([gyro.lid_gyroscope, gyro.base_gyroscope])),
554 _feature(
Andrew Lamb2413c982020-05-29 12:15:36 -0600555 'android.hardware.sensor.compass',
Andrew Lambcd33f702020-06-11 10:45:16 -0600556 _any_present(
557 [compass.lid_magnetometer, compass.base_magnetometer])),
558 _feature(
559 'android.hardware.sensor.light',
560 _any_present(
561 [light_sensor.lid_lightsensor,
562 light_sensor.base_lightsensor])),
563 _feature('android.hardware.touchscreen', touchscreen),
564 _feature('android.hardware.touchscreen.multitouch', touchscreen),
565 _feature('android.hardware.touchscreen.multitouch.distinct',
Andrew Lamb2413c982020-05-29 12:15:36 -0600566 touchscreen),
Andrew Lambcd33f702020-06-11 10:45:16 -0600567 _feature('android.hardware.touchscreen.multitouch.jazzhand',
Andrew Lamb2413c982020-05-29 12:15:36 -0600568 touchscreen),
C Shapiro5bf23a72020-04-24 11:40:17 -0500569 ])
570
C Shapiroea33cff2020-05-11 13:32:05 -0500571 design_name = hw_design.name.lower()
C Shapiro5bf23a72020-04-24 11:40:17 -0500572
C Shapiroea33cff2020-05-11 13:32:05 -0500573 # Constructs the following map:
574 # design_name -> config -> design_configs
575 # This allows any of the following file naming schemes:
576 # - All configs within a design share config (design_name prefix only)
577 # - Nobody shares (full design_name and config id prefix needed)
578 #
579 # Having shared configs when possible makes code reviews easier around
580 # the configs and makes debugging easier on the platform side.
581 config_content = etree.tostring(root)
582 arc_configs = configs_by_design.get(design_name, {})
583 design_configs = arc_configs.get(config_content, [])
584 design_configs.append(design_config)
585 arc_configs[config_content] = design_configs
586 configs_by_design[design_name] = arc_configs
C Shapiro9a3ac8c2020-04-25 07:49:21 -0500587
C Shapiroea33cff2020-05-11 13:32:05 -0500588 for design_name, unique_configs in configs_by_design.items():
589 for file_content, design_configs in unique_configs.items():
Andrew Lamb2413c982020-05-29 12:15:36 -0600590 file_name = 'hardware_features_%s.xml' % design_name
591 if len(unique_configs) == 1:
Andrew Lambcd33f702020-06-11 10:45:16 -0600592 _write_arc_hardware_feature_file(output_dir, file_name, file_content)
C Shapiro9a3ac8c2020-04-25 07:49:21 -0500593
Andrew Lamb2413c982020-05-29 12:15:36 -0600594 for design_config in design_configs:
Andrew Lambcd33f702020-06-11 10:45:16 -0600595 feature_id = _arc_hardware_feature_id(design_config)
Andrew Lamb2413c982020-05-29 12:15:36 -0600596 if len(unique_configs) > 1:
597 file_name = 'hardware_features_%s.xml' % feature_id
Andrew Lambcd33f702020-06-11 10:45:16 -0600598 _write_arc_hardware_feature_file(output_dir, file_name, file_content)
Andrew Lamb2413c982020-05-29 12:15:36 -0600599 result[feature_id] = {
600 'build-path': '%s/arc/%s' % (build_root_dir, file_name),
601 'system-path': '/etc/%s' % file_name,
602 }
C Shapiro5bf23a72020-04-24 11:40:17 -0500603 return result
604
605
Andrew Lambcd33f702020-06-11 10:45:16 -0600606def _write_bluetooth_config_files(config, output_dir, build_root_path):
C Shapiro90fda252020-04-17 14:34:57 -0500607 """Writes bluetooth conf files for every unique bluetooth chip.
608
609 Args:
610 config: Source ConfigBundle to process.
611 output_dir: Path to the generated output.
C Shapiro5c877992020-04-29 12:11:28 -0500612 build_root_path: Path to the config file from portage's perspective.
C Shapiro90fda252020-04-17 14:34:57 -0500613 Returns:
614 dict that maps the bluetooth component id onto the file config.
615 """
David Burger77a1d312020-05-23 16:05:45 -0600616 output_dir += '/bluetooth'
C Shapiro90fda252020-04-17 14:34:57 -0500617 result = {}
618 for hw_design in config.designs.value:
619 project_name = hw_design.name.lower()
620 for design_config in hw_design.configs:
C Shapiro74da76e2020-05-04 13:02:20 -0500621 bt_comp = design_config.hardware_features.bluetooth.component.usb
C Shapiro90fda252020-04-17 14:34:57 -0500622 if bt_comp.vendor_id:
Andrew Lambcd33f702020-06-11 10:45:16 -0600623 bt_id = _bluetooth_id(project_name, bt_comp)
C Shapiro90fda252020-04-17 14:34:57 -0500624 result[bt_id] = {
C Shapiro5c877992020-04-29 12:11:28 -0500625 'build-path': '%s/bluetooth/%s.conf' % (build_root_path, bt_id),
C Shapiro90fda252020-04-17 14:34:57 -0500626 'system-path': '/etc/bluetooth/%s/main.conf' % bt_id,
627 }
628 bt_content = '''[General]
Andrew Lamb2413c982020-05-29 12:15:36 -0600629DeviceID = bluetooth:%s:%s:%s''' % (bt_comp.vendor_id, bt_comp.product_id,
C Shapiro90fda252020-04-17 14:34:57 -0500630 bt_comp.bcd_device)
631
David Burger77a1d312020-05-23 16:05:45 -0600632 os.makedirs(output_dir, exist_ok=True)
633 output = '%s/%s.conf' % (output_dir, bt_id)
C Shapiro90fda252020-04-17 14:34:57 -0500634 with open(output, 'w') as output_stream:
635 # Using print function adds proper trailing newline.
636 print(bt_content, file=output_stream)
637 return result
638
639
Andrew Lambcd33f702020-06-11 10:45:16 -0600640def _read_config(path):
David Burgerd4f32962020-05-02 12:07:40 -0600641 """Reads a ConfigBundle proto from a json pb file.
David Burgere6f76222020-04-27 11:08:01 -0600642
643 Args:
David Burgerd4f32962020-05-02 12:07:40 -0600644 path: Path to the file encoding the json pb proto.
David Burgere6f76222020-04-27 11:08:01 -0600645 """
646 config = config_bundle_pb2.ConfigBundle()
647 with open(path, 'r') as f:
648 return json_format.Parse(f.read(), config)
649
650
Andrew Lambcd33f702020-06-11 10:45:16 -0600651def _merge_configs(configs):
David Burger7fd1dbe2020-03-26 09:26:55 -0600652 result = config_bundle_pb2.ConfigBundle()
653 for config in configs:
654 result.MergeFrom(config)
655
656 return result
657
658
Andrew Lambcd33f702020-06-11 10:45:16 -0600659def Main(project_configs, program_config, output): # pylint: disable=invalid-name
David Burger7fd1dbe2020-03-26 09:26:55 -0600660 """Transforms source proto config into platform JSON.
661
662 Args:
663 project_configs: List of source project configs to transform.
664 program_config: Program config for the given set of projects.
665 output: Output file that will be generated by the transform.
666 """
Andrew Lambcd33f702020-06-11 10:45:16 -0600667 configs = _merge_configs([_read_config(program_config)] +
668 [_read_config(config) for config in project_configs])
C Shapiro5bf23a72020-04-24 11:40:17 -0500669 bluetooth_files = {}
670 arc_hw_feature_files = {}
C Shapiro2b6d5332020-05-06 17:51:35 -0500671 touch_fw = {}
C Shapiro6830e6c2020-04-29 13:29:56 -0500672 dptf_file = None
C Shapiro5bf23a72020-04-24 11:40:17 -0500673 output_dir = os.path.dirname(output)
C Shapiro5c877992020-04-29 12:11:28 -0500674 build_root_dir = output_dir
C Shapiro5c877992020-04-29 12:11:28 -0500675 if 'sw_build_config' in output_dir:
676 full_path = os.path.realpath(output)
Andrew Lamb2413c982020-05-29 12:15:36 -0600677 project_name = re.match(r'.*/(\w*)/sw_build_config/.*',
678 full_path).groups(1)[0]
C Shapiro5c877992020-04-29 12:11:28 -0500679 # Projects don't know about each other until they are integrated into the
680 # build system. When this happens, the files need to be able to co-exist
681 # without any collisions. This prefixes the project name (which is how
682 # portage maps in the project), so project files co-exist and can be
683 # installed together.
684 # This is necessary to allow projects to share files at the program level
685 # without having portage file installation collisions.
686 build_root_dir = os.path.join(project_name, output_dir)
C Shapiro6830e6c2020-04-29 13:29:56 -0500687
C Shapiro7356bd62020-05-02 05:21:33 -0500688 if os.path.exists(DPTF_PATH):
689 project_dptf_path = os.path.join(project_name, 'dptf.dv')
690 dptf_file = {
Andrew Lamb2413c982020-05-29 12:15:36 -0600691 'dptf-dv':
692 project_dptf_path,
693 'files': [
Andrew Lambcd33f702020-06-11 10:45:16 -0600694 _file(
Andrew Lamb2413c982020-05-29 12:15:36 -0600695 os.path.join(project_name, DPTF_PATH),
696 os.path.join('/etc/dptf', project_dptf_path))
697 ]
C Shapiro7356bd62020-05-02 05:21:33 -0500698 }
C Shapiro2b6d5332020-05-06 17:51:35 -0500699 if os.path.exists(TOUCH_PATH):
Andrew Lambcd33f702020-06-11 10:45:16 -0600700 touch_fw = _build_touch_file_config(configs, project_name)
701 bluetooth_files = _write_bluetooth_config_files(configs, output_dir,
702 build_root_dir)
703 arc_hw_feature_files = _write_arc_hardware_feature_files(
704 configs, output_dir, build_root_dir)
C Shapiro5bf23a72020-04-24 11:40:17 -0500705 config_files = ConfigFiles(
706 bluetooth=bluetooth_files,
707 arc_hw_features=arc_hw_feature_files,
C Shapiro2b6d5332020-05-06 17:51:35 -0500708 touch_fw=touch_fw,
Andrew Lamb2413c982020-05-29 12:15:36 -0600709 dptf_file=dptf_file)
Andrew Lambcd33f702020-06-11 10:45:16 -0600710 write_output(_transform_build_configs(configs, config_files), output)
David Burger7fd1dbe2020-03-26 09:26:55 -0600711
712
713def main(argv=None):
714 """Main program which parses args and runs
715
716 Args:
717 argv: List of command line arguments, if None uses sys.argv.
718 """
719 if argv is None:
720 argv = sys.argv[1:]
Andrew Lambcd33f702020-06-11 10:45:16 -0600721 opts = parse_args(argv)
David Burger7fd1dbe2020-03-26 09:26:55 -0600722 Main(opts.project_configs, opts.program_config, opts.output)
723
724
725if __name__ == '__main__':
726 sys.exit(main(sys.argv[1:]))