1#!/usr/bin/env python
2#
3# Copyright (C) 2019 The Android Open Source Project
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9#      http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
17"""
18Check VINTF compatibility from a target files package.
19
20Usage: check_target_files_vintf target_files
21
22target_files can be a ZIP file or an extracted target files directory.
23"""
24
25import logging
26import subprocess
27import sys
28import os
29import zipfile
30
31import common
32
33logger = logging.getLogger(__name__)
34
35OPTIONS = common.OPTIONS
36
37# Keys are paths that VINTF searches. Must keep in sync with libvintf's search
38# paths (VintfObject.cpp).
39# These paths are stored in different directories in target files package, so
40# we have to search for the correct path and tell checkvintf to remap them.
41# Look for TARGET_COPY_OUT_* variables in board_config.mk for possible paths for
42# each partition.
43DIR_SEARCH_PATHS = {
44    '/system': ('SYSTEM',),
45    '/vendor': ('VENDOR', 'SYSTEM/vendor'),
46    '/product': ('PRODUCT', 'SYSTEM/product'),
47    '/odm': ('ODM', 'VENDOR/odm', 'SYSTEM/vendor/odm'),
48    '/system_ext': ('SYSTEM_EXT', 'SYSTEM/system_ext'),
49    # vendor_dlkm and odm_dlkm does not have VINTF files.
50}
51
52UNZIP_PATTERN = ['META/*', '*/build.prop']
53
54
55def GetDirmap(input_tmp):
56  dirmap = {}
57  for device_path, target_files_rel_paths in DIR_SEARCH_PATHS.items():
58    for target_files_rel_path in target_files_rel_paths:
59      target_files_path = os.path.join(input_tmp, target_files_rel_path)
60      if os.path.isdir(target_files_path):
61        dirmap[device_path] = target_files_path
62        break
63    if device_path not in dirmap:
64      raise ValueError("Can't determine path for device path " + device_path +
65                       ". Searched the following:" +
66                       ("\n".join(target_files_rel_paths)))
67  return dirmap
68
69
70def GetArgsForSkus(info_dict):
71  odm_skus = info_dict.get('vintf_odm_manifest_skus', '').strip().split()
72  if info_dict.get('vintf_include_empty_odm_sku', '') == "true" or not odm_skus:
73    odm_skus += ['']
74
75  vendor_skus = info_dict.get('vintf_vendor_manifest_skus', '').strip().split()
76  if info_dict.get('vintf_include_empty_vendor_sku', '') == "true" or \
77      not vendor_skus:
78    vendor_skus += ['']
79
80  return [['--property', 'ro.boot.product.hardware.sku=' + odm_sku,
81           '--property', 'ro.boot.product.vendor.sku=' + vendor_sku]
82          for odm_sku in odm_skus for vendor_sku in vendor_skus]
83
84
85def GetArgsForShippingApiLevel(info_dict):
86  shipping_api_level = info_dict['vendor.build.prop'].GetProp(
87      'ro.product.first_api_level')
88  if not shipping_api_level:
89    logger.warning('Cannot determine ro.product.first_api_level')
90    return []
91  return ['--property', 'ro.product.first_api_level=' + shipping_api_level]
92
93
94def GetArgsForKernel(input_tmp):
95  version_path = os.path.join(input_tmp, 'META/kernel_version.txt')
96  config_path = os.path.join(input_tmp, 'META/kernel_configs.txt')
97
98  if not os.path.isfile(version_path) or not os.path.isfile(config_path):
99    logger.info('Skipping kernel config checks because '
100                'PRODUCT_OTA_ENFORCE_VINTF_KERNEL_REQUIREMENTS is not set')
101    return []
102
103  with open(version_path) as f:
104    version = f.read().strip()
105
106  return ['--kernel', '{}:{}'.format(version, config_path)]
107
108
109def CheckVintfFromExtractedTargetFiles(input_tmp, info_dict=None):
110  """
111  Checks VINTF metadata of an extracted target files directory.
112
113  Args:
114    inp: path to the directory that contains the extracted target files archive.
115    info_dict: The build-time info dict. If None, it will be loaded from inp.
116
117  Returns:
118    True if VINTF check is skipped or compatible, False if incompatible. Raise
119    a RuntimeError if any error occurs.
120  """
121
122  if info_dict is None:
123    info_dict = common.LoadInfoDict(input_tmp)
124
125  if info_dict.get('vintf_enforce') != 'true':
126    logger.warning('PRODUCT_ENFORCE_VINTF_MANIFEST is not set, skipping checks')
127    return True
128
129  dirmap = GetDirmap(input_tmp)
130  args_for_skus = GetArgsForSkus(info_dict)
131  shipping_api_level_args = GetArgsForShippingApiLevel(info_dict)
132  kernel_args = GetArgsForKernel(input_tmp)
133
134  common_command = [
135      'checkvintf',
136      '--check-compat',
137  ]
138  for device_path, real_path in dirmap.items():
139    common_command += ['--dirmap', '{}:{}'.format(device_path, real_path)]
140  common_command += kernel_args
141  common_command += shipping_api_level_args
142
143  success = True
144  for sku_args in args_for_skus:
145    command = common_command + sku_args
146    proc = common.Run(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
147    out, err = proc.communicate()
148    if proc.returncode == 0:
149      logger.info("Command `%s` returns 'compatible'", ' '.join(command))
150    elif out.strip() == "INCOMPATIBLE":
151      logger.info("Command `%s` returns 'incompatible'", ' '.join(command))
152      success = False
153    else:
154      raise common.ExternalError(
155          "Failed to run command '{}' (exit code {}):\nstdout:{}\nstderr:{}"
156          .format(' '.join(command), proc.returncode, out, err))
157    logger.info("stdout: %s", out)
158    logger.info("stderr: %s", err)
159
160  return success
161
162
163def GetVintfFileList():
164  """
165  Returns a list of VINTF metadata files that should be read from a target files
166  package before executing checkvintf.
167  """
168  def PathToPatterns(path):
169    if path[-1] == '/':
170      path += '*'
171    for device_path, target_files_rel_paths in DIR_SEARCH_PATHS.items():
172      if path.startswith(device_path):
173        suffix = path[len(device_path):]
174        return [rel_path + suffix for rel_path in target_files_rel_paths]
175    raise RuntimeError('Unrecognized path from checkvintf --dump-file-list: ' +
176                       path)
177
178  out = common.RunAndCheckOutput(['checkvintf', '--dump-file-list'])
179  paths = out.strip().split('\n')
180  paths = sum((PathToPatterns(path) for path in paths if path), [])
181  return paths
182
183
184def CheckVintfFromTargetFiles(inp, info_dict=None):
185  """
186  Checks VINTF metadata of a target files zip.
187
188  Args:
189    inp: path to the target files archive.
190    info_dict: The build-time info dict. If None, it will be loaded from inp.
191
192  Returns:
193    True if VINTF check is skipped or compatible, False if incompatible. Raise
194    a RuntimeError if any error occurs.
195  """
196  input_tmp = common.UnzipTemp(inp, GetVintfFileList() + UNZIP_PATTERN)
197  return CheckVintfFromExtractedTargetFiles(input_tmp, info_dict)
198
199
200def CheckVintf(inp, info_dict=None):
201  """
202  Checks VINTF metadata of a target files zip or extracted target files
203  directory.
204
205  Args:
206    inp: path to the (possibly extracted) target files archive.
207    info_dict: The build-time info dict. If None, it will be loaded from inp.
208
209  Returns:
210    True if VINTF check is skipped or compatible, False if incompatible. Raise
211    a RuntimeError if any error occurs.
212  """
213  if os.path.isdir(inp):
214    logger.info('Checking VINTF compatibility extracted target files...')
215    return CheckVintfFromExtractedTargetFiles(inp, info_dict)
216
217  if zipfile.is_zipfile(inp):
218    logger.info('Checking VINTF compatibility target files...')
219    return CheckVintfFromTargetFiles(inp, info_dict)
220
221  raise ValueError('{} is not a valid directory or zip file'.format(inp))
222
223
224def main(argv):
225  args = common.ParseOptions(argv, __doc__)
226  if len(args) != 1:
227    common.Usage(__doc__)
228    sys.exit(1)
229  common.InitLogging()
230  if not CheckVintf(args[0]):
231    sys.exit(1)
232
233
234if __name__ == '__main__':
235  try:
236    common.CloseInheritedPipes()
237    main(sys.argv[1:])
238  except common.ExternalError:
239    logger.exception('\n   ERROR:\n')
240    sys.exit(1)
241  finally:
242    common.Cleanup()
243