1# Lint as: python2, python3 2# Copyright 2020 The Chromium OS Authors. All rights reserved. 3# Use of this source code is governed by a BSD-style license that can be 4# found in the LICENSE file. 5 6from autotest_lib.client.common_lib import error 7from autotest_lib.utils.frozen_chromite.lib import retry_util 8 9 10_VPD_BASE_CMD = 'vpd -i %s %s %s' 11_RW = 'RW_VPD' 12_RO = 'RO_VPD' 13 14 15def _check_partition(partition): 16 """ 17 Used to validate input string in other functions. 18 19 @param partition: If this is not 'RO_VPD' or 'RW_VPD', raise a ValueError. 20 21 """ 22 if partition not in [_RW, _RO]: 23 raise ValueError("partition should be 'RW_VPD' or 'RO_VPD'") 24 25 26def dump_vpd_log(host, force=True, retries=3): 27 """ 28 Applies changes to the VPD settings by flushing them to the VPD cache and 29 output files. 30 31 @param host: Host to run the command on. 32 @param force: True to pass in the --force parameter to forcefully dump 33 the log. False to omit it. 34 @param retries: Number of times to try rerunning the command in case of 35 error. 36 37 """ 38 vpd_dump_cmd = 'dump_vpd_log%s' % (' --force' if force else '') 39 retry_util.RetryException(error.AutoservRunError, retries, host.run, 40 vpd_dump_cmd) 41 42 43def vpd_get(host, key, partition='RW_VPD', retries=3): 44 """ 45 Gets the VPD value associated with the input key. 46 47 @param host: Host to run the command on. 48 @param key: Key of the desired VPD value. 49 @param partition: Which partition to access. 'RO_VPD' or 'RW_VPD'. 50 @param retries: Number of times to try rerunning the command in case of 51 error. 52 53 """ 54 _check_partition(partition) 55 get_cmd = _VPD_BASE_CMD % (partition, '-g', key) 56 try: 57 return retry_util.RetryException(error.AutoservRunError, retries, 58 host.run, get_cmd).stdout 59 except error.AutoservRunError as e: 60 if 'was not found' in str(e.result_obj.stderr): 61 return None 62 else: 63 raise e 64 65 66def vpd_set(host, vpd_dict, partition='RW_VPD', dump=False, force_dump=False, 67 retries=3): 68 """ 69 Sets the given key/value pairs in the specified VPD partition. 70 71 @param host: Host to run the command on. 72 @param vpd_dict: Dictionary containing the VPD key/value pairs to set. 73 Dictionary keys should be the VPD key strings, and values 74 should be the desired values to write. 75 @param partition: Which partition to access. 'RO_VPD' or 'RW_VPD'. 76 @param dump: If True, also run dump_vpd_log command after setting the 77 vpd values. 78 @param force_dump: Whether or not to forcefully dump the vpd log. 79 @param retries: Number of times to try rerunning the command in case of 80 error. 81 82 """ 83 _check_partition(partition) 84 for vpd_key in vpd_dict: 85 set_cmd = _VPD_BASE_CMD % (partition, '-s', 86 (vpd_key + '=' + str(vpd_dict[vpd_key]))) 87 retry_util.RetryException(error.AutoservRunError, retries, 88 host.run, set_cmd).stdout 89 90 if dump: 91 dump_vpd_log(host, force=force_dump, retries=retries) 92 93 94def vpd_delete(host, key, partition='RW_VPD', dump=False, force_dump=False, 95 retries=3): 96 """ 97 Deletes the specified key from the specified VPD partition. 98 99 @param host: Host to run the command on. 100 @param key: The VPD value to delete. 101 @param partition: Which partition to access. 'RO_VPD' or 'RW_VPD'. 102 @param dump: If True, also run dump_vpd_log command after deleting the 103 vpd value. 104 @param force_dump: Whether or not to forcefully dump the vpd log. 105 @param retries: Number of times to try rerunning the command in case of 106 error. 107 108 """ 109 _check_partition(partition) 110 if not vpd_get(host, key, partition=partition, retries=retries): 111 return 112 113 del_cmd = _VPD_BASE_CMD % (partition, '-d', key) 114 retry_util.RetryException(error.AutoservRunError, retries, host.run, 115 del_cmd).stdout 116 117 if dump: 118 dump_vpd_log(host, force=force_dump, retries=retries) 119