blob: 0edefac9c1b4b221abd3d8e9700aaa8270c7d309 [file] [log] [blame]
Yifan Honge3ba82c2019-08-21 13:29:30 -07001#!/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.
Yifan Hong2870d1e2019-12-19 13:58:00 -080041# Look for TARGET_COPY_OUT_* variables in board_config.mk for possible paths for
42# each partition.
Yifan Honge3ba82c2019-08-21 13:29:30 -070043DIR_SEARCH_PATHS = {
44 '/system': ('SYSTEM',),
45 '/vendor': ('VENDOR', 'SYSTEM/vendor'),
46 '/product': ('PRODUCT', 'SYSTEM/product'),
Yifan Hong2870d1e2019-12-19 13:58:00 -080047 '/odm': ('ODM', 'VENDOR/odm', 'SYSTEM/vendor/odm'),
Yifan Hong9cbb6242019-12-19 13:56:59 -080048 '/system_ext': ('SYSTEM_EXT', 'SYSTEM/system_ext'),
Yifan Hongf496f1b2020-07-15 16:52:59 -070049 # vendor_dlkm and odm_dlkm does not have VINTF files.
Yifan Honge3ba82c2019-08-21 13:29:30 -070050}
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):
Yifan Hong28ffd732020-03-13 13:11:10 -070071 odm_skus = info_dict.get('vintf_odm_manifest_skus', '').strip().split()
Yifan Hong69430e62020-03-17 15:18:34 -070072 if info_dict.get('vintf_include_empty_odm_sku', '') == "true" or not odm_skus:
Yifan Hong28ffd732020-03-13 13:11:10 -070073 odm_skus += ['']
Yifan Honge3ba82c2019-08-21 13:29:30 -070074
Yifan Hong28ffd732020-03-13 13:11:10 -070075 vendor_skus = info_dict.get('vintf_vendor_manifest_skus', '').strip().split()
Yifan Hong69430e62020-03-17 15:18:34 -070076 if info_dict.get('vintf_include_empty_vendor_sku', '') == "true" or \
77 not vendor_skus:
Yifan Hong28ffd732020-03-13 13:11:10 -070078 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]
Yifan Honge3ba82c2019-08-21 13:29:30 -070083
Tianjie Xu0fde41e2020-05-09 05:24:18 +000084
Yifan Honge3ba82c2019-08-21 13:29:30 -070085def GetArgsForShippingApiLevel(info_dict):
Tianjie Xu0fde41e2020-05-09 05:24:18 +000086 shipping_api_level = info_dict['vendor.build.prop'].GetProp(
Yifan Honge3ba82c2019-08-21 13:29:30 -070087 '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):
Yifan Hong28ffd732020-03-13 13:11:10 -070099 logger.info('Skipping kernel config checks because '
Yifan Honge3ba82c2019-08-21 13:29:30 -0700100 '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
Kelvin Zhangcff4d762020-07-29 16:37:51 -0400223def CheckVintfIfTrebleEnabled(target_files, target_info):
224 """Checks compatibility info of the input target files.
225
226 Metadata used for compatibility verification is retrieved from target_zip.
227
228 Compatibility should only be checked for devices that have enabled
229 Treble support.
230
231 Args:
232 target_files: Path to zip file containing the source files to be included
233 for OTA. Can also be the path to extracted directory.
234 target_info: The BuildInfo instance that holds the target build info.
235 """
236
237 # Will only proceed if the target has enabled the Treble support (as well as
238 # having a /vendor partition).
239 if not HasTrebleEnabled(target_files, target_info):
240 return
241
242 # Skip adding the compatibility package as a workaround for b/114240221. The
243 # compatibility will always fail on devices without qualified kernels.
244 if OPTIONS.skip_compatibility_check:
245 return
246
247 if not CheckVintf(target_files, target_info):
248 raise RuntimeError("VINTF compatibility check failed")
249
250def HasTrebleEnabled(target_files, target_info):
251 def HasVendorPartition(target_files):
252 if os.path.isdir(target_files):
253 return os.path.isdir(os.path.join(target_files, "VENDOR"))
254 if zipfile.is_zipfile(target_files):
255 return HasPartition(zipfile.ZipFile(target_files), "vendor")
256 raise ValueError("Unknown target_files argument")
257
258 return (HasVendorPartition(target_files) and
259 target_info.GetBuildProp("ro.treble.enabled") == "true")
260
261
262def HasPartition(target_files_zip, partition):
263 try:
264 target_files_zip.getinfo(partition.upper() + "/")
265 return True
266 except KeyError:
267 return False
268
Yifan Honge3ba82c2019-08-21 13:29:30 -0700269
270def main(argv):
271 args = common.ParseOptions(argv, __doc__)
272 if len(args) != 1:
273 common.Usage(__doc__)
274 sys.exit(1)
275 common.InitLogging()
276 if not CheckVintf(args[0]):
277 sys.exit(1)
278
279
280if __name__ == '__main__':
281 try:
282 common.CloseInheritedPipes()
283 main(sys.argv[1:])
284 except common.ExternalError:
285 logger.exception('\n ERROR:\n')
286 sys.exit(1)
287 finally:
288 common.Cleanup()