Tammo Spalink | 9a96b8a | 2012-04-03 11:10:41 +0800 | [diff] [blame] | 1 | #!/usr/bin/python |
Tammo Spalink | 01e1172 | 2012-07-24 10:17:54 -0700 | [diff] [blame] | 2 | # pylint: disable=E1101 |
Tammo Spalink | 9a96b8a | 2012-04-03 11:10:41 +0800 | [diff] [blame] | 3 | # |
| 4 | # Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
| 5 | # Use of this source code is governed by a BSD-style license that can be |
| 6 | # found in the LICENSE file. |
| 7 | |
| 8 | """Google Factory Tool. |
| 9 | |
| 10 | This tool is indended to be used on factory assembly lines. It |
| 11 | provides all of the Google required test functionality and must be run |
| 12 | on each device as part of the assembly process. |
| 13 | """ |
| 14 | |
Ricky Liang | 5b4568d | 2013-04-23 17:15:23 +0800 | [diff] [blame] | 15 | import collections |
Tammo Spalink | 9a96b8a | 2012-04-03 11:10:41 +0800 | [diff] [blame] | 16 | import logging |
| 17 | import os |
Jon Salz | 6526643 | 2012-07-30 19:02:49 +0800 | [diff] [blame] | 18 | import pipes |
Tammo Spalink | 9a96b8a | 2012-04-03 11:10:41 +0800 | [diff] [blame] | 19 | import re |
| 20 | import sys |
Hung-Te Lin | 6bd1647 | 2012-06-20 16:26:47 +0800 | [diff] [blame] | 21 | import time |
Ricky Liang | 7905f27 | 2013-03-16 01:57:10 +0800 | [diff] [blame] | 22 | import yaml |
Tammo Spalink | 9a96b8a | 2012-04-03 11:10:41 +0800 | [diff] [blame] | 23 | |
Andy Cheng | 2582d29 | 2012-12-04 17:38:28 +0800 | [diff] [blame] | 24 | from tempfile import gettempdir |
Tammo Spalink | 86a61c6 | 2012-05-25 15:10:35 +0800 | [diff] [blame] | 25 | |
Tammo Spalink | a40293e | 2012-07-04 14:58:56 +0800 | [diff] [blame] | 26 | import factory_common # pylint: disable=W0611 |
Tammo Spalink | 9a96b8a | 2012-04-03 11:10:41 +0800 | [diff] [blame] | 27 | |
Andy Cheng | 0465d13 | 2013-03-20 12:12:06 +0800 | [diff] [blame] | 28 | from cros.factory import event_log |
Jon Salz | 0f8a684 | 2012-09-25 11:28:22 +0800 | [diff] [blame] | 29 | from cros.factory.common import Error, SetupLogging, Shell |
Tammo Spalink | 394e449 | 2012-08-01 10:20:30 -0700 | [diff] [blame] | 30 | from cros.factory.common import YamlWrite |
Andy Cheng | c531e2f | 2012-10-15 19:09:17 +0800 | [diff] [blame] | 31 | from cros.factory.gooftool import Gooftool |
Ricky Liang | 5339023 | 2013-03-08 15:37:57 +0800 | [diff] [blame] | 32 | from cros.factory.gooftool import crosfw |
Tammo Spalink | 01e1172 | 2012-07-24 10:17:54 -0700 | [diff] [blame] | 33 | from cros.factory.gooftool import report_upload |
Andy Cheng | 8ece738 | 2012-08-22 16:25:42 +0800 | [diff] [blame] | 34 | from cros.factory.gooftool.probe import Probe, PROBEABLE_COMPONENT_CLASSES |
Jon Salz | 0f8a684 | 2012-09-25 11:28:22 +0800 | [diff] [blame] | 35 | from cros.factory.gooftool.probe import ReadRoVpd, ReadRwVpd |
Jon Salz | 193d7c6 | 2013-03-07 13:40:19 +0800 | [diff] [blame] | 36 | from cros.factory.gooftool.vpd_data import KNOWN_VPD_FIELD_DATA |
Tammo Spalink | a40293e | 2012-07-04 14:58:56 +0800 | [diff] [blame] | 37 | from cros.factory.hacked_argparse import CmdArg, Command, ParseCmdline |
| 38 | from cros.factory.hacked_argparse import verbosity_cmd_arg |
Tammo Spalink | 01e1172 | 2012-07-24 10:17:54 -0700 | [diff] [blame] | 39 | from cros.factory.hwdb import hwid_tool |
cychiang | 7fe0937 | 2012-07-04 14:31:23 +0800 | [diff] [blame] | 40 | from cros.factory.test.factory import FACTORY_LOG_PATH |
Jon Salz | ff88c02 | 2012-11-03 12:19:58 +0800 | [diff] [blame] | 41 | from cros.factory.utils.process_utils import Spawn |
Jon Salz | 8baad8b | 2013-03-11 20:01:45 +0800 | [diff] [blame] | 42 | from cros.factory.privacy import FilterDict |
Tammo Spalink | 86a61c6 | 2012-05-25 15:10:35 +0800 | [diff] [blame] | 43 | |
Tammo Spalink | 5c69983 | 2012-07-03 17:50:39 +0800 | [diff] [blame] | 44 | |
Tammo Spalink | 5c69983 | 2012-07-03 17:50:39 +0800 | [diff] [blame] | 45 | # TODO(tammo): Replace calls to sys.exit with raise Exit, and maybe |
| 46 | # treat that specially (as a smoot exit, as opposed to the more |
| 47 | # verbose output for generic Error). |
| 48 | |
| 49 | |
Ricky Liang | a70a120 | 2013-03-15 15:03:17 +0800 | [diff] [blame] | 50 | def GetGooftool(options): |
| 51 | if options.hwid_version == 2: |
| 52 | hwdb_path = getattr(options, 'hwdb_path', None) |
| 53 | component_db = ( |
| 54 | hwid_tool.HardwareDb(options.hwdb_path).comp_db if hwdb_path else None) |
| 55 | return Gooftool(hwid_version=2, component_db=component_db) |
| 56 | elif options.hwid_version == 3: |
| 57 | board = getattr(options, 'board', None) |
| 58 | hwdb_path = getattr(options, 'hwdb_path', None) |
| 59 | return Gooftool(hwid_version=3, board=board, hwdb_path=hwdb_path) |
| 60 | else: |
| 61 | raise Error, 'Invalid HWID version: %r' % options.hwid_version |
| 62 | |
| 63 | |
Tammo Spalink | 9a96b8a | 2012-04-03 11:10:41 +0800 | [diff] [blame] | 64 | @Command('write_hwid', |
| 65 | CmdArg('hwid', metavar='HWID', help='HWID string')) |
Andy Cheng | c92e6f9 | 2012-11-20 16:55:53 +0800 | [diff] [blame] | 66 | def WriteHWID(options): |
Tammo Spalink | 9a96b8a | 2012-04-03 11:10:41 +0800 | [diff] [blame] | 67 | """Write specified HWID value into the system BB.""" |
Andy Cheng | 7a76cb8 | 2012-11-19 18:08:19 +0800 | [diff] [blame] | 68 | |
Tammo Spalink | 95c4373 | 2012-07-25 15:57:14 -0700 | [diff] [blame] | 69 | logging.info('writing hwid string %r', options.hwid) |
Ricky Liang | a70a120 | 2013-03-15 15:03:17 +0800 | [diff] [blame] | 70 | GetGooftool(options).WriteHWID(options.hwid) |
Andy Cheng | 0465d13 | 2013-03-20 12:12:06 +0800 | [diff] [blame] | 71 | event_log.Log('write_hwid', hwid=options.hwid) |
Tammo Spalink | 95c4373 | 2012-07-25 15:57:14 -0700 | [diff] [blame] | 72 | print 'Wrote HWID: %r' % options.hwid |
Tammo Spalink | 9a96b8a | 2012-04-03 11:10:41 +0800 | [diff] [blame] | 73 | |
| 74 | |
Ricky Liang | 5339023 | 2013-03-08 15:37:57 +0800 | [diff] [blame] | 75 | _board_cmd_arg = CmdArg( |
| 76 | '--board', metavar='BOARD', |
| 77 | default=None, help='Board name to test.') |
| 78 | |
Tammo Spalink | 8fab531 | 2012-05-28 18:33:30 +0800 | [diff] [blame] | 79 | _hwdb_path_cmd_arg = CmdArg( |
| 80 | '--hwdb_path', metavar='PATH', |
| 81 | default=hwid_tool.DEFAULT_HWID_DATA_PATH, |
| 82 | help='Path to the HWID database.') |
| 83 | |
Tammo Spalink | 95c4373 | 2012-07-25 15:57:14 -0700 | [diff] [blame] | 84 | _hwid_status_list_cmd_arg = CmdArg( |
| 85 | '--status', nargs='*', default=['supported'], |
| 86 | help='allow only HWIDs with these status values') |
| 87 | |
Jon Salz | ce124fb | 2012-10-02 17:42:03 +0800 | [diff] [blame] | 88 | _probe_results_cmd_arg = CmdArg( |
| 89 | '--probe_results', metavar='RESULTS.yaml', |
| 90 | help=('Output from "gooftool probe" (used instead of ' |
| 91 | 'probing this system).')) |
| 92 | |
Ricky Liang | 5339023 | 2013-03-08 15:37:57 +0800 | [diff] [blame] | 93 | _device_info_cmd_arg = CmdArg( |
Ricky Liang | f89f73a | 2013-03-19 05:00:24 +0800 | [diff] [blame] | 94 | '--device_info', metavar='DEVICE_INFO.yaml', default=None, |
Ricky Liang | 5339023 | 2013-03-08 15:37:57 +0800 | [diff] [blame] | 95 | help='A dict of device info to use instead of fetching from shopfllor ' |
| 96 | 'server.') |
| 97 | |
Jon Salz | ce124fb | 2012-10-02 17:42:03 +0800 | [diff] [blame] | 98 | _hwid_cmd_arg = CmdArg( |
| 99 | '--hwid', metavar='HWID', |
Ricky Liang | a70a120 | 2013-03-15 15:03:17 +0800 | [diff] [blame] | 100 | help='HWID to verify (instead of the currently set HWID of this system).') |
Jon Salz | ce124fb | 2012-10-02 17:42:03 +0800 | [diff] [blame] | 101 | |
Tammo Spalink | 95c4373 | 2012-07-25 15:57:14 -0700 | [diff] [blame] | 102 | |
| 103 | @Command('best_match_hwids', |
Tammo Spalink | 8fab531 | 2012-05-28 18:33:30 +0800 | [diff] [blame] | 104 | _hwdb_path_cmd_arg, |
| 105 | CmdArg('-b', '--board', metavar='BOARD', |
Tammo Spalink | 95c4373 | 2012-07-25 15:57:14 -0700 | [diff] [blame] | 106 | help='optional BOARD name, needed only if data is present ' |
| 107 | 'for more than one'), |
Tammo Spalink | 8fab531 | 2012-05-28 18:33:30 +0800 | [diff] [blame] | 108 | CmdArg('--bom', metavar='BOM', help='BOM name'), |
| 109 | CmdArg('--variant', metavar='VARIANT', help='VARIANT code'), |
Tammo Spalink | 5c69983 | 2012-07-03 17:50:39 +0800 | [diff] [blame] | 110 | CmdArg('--optimistic', action='store_true', |
| 111 | help='do not probe; assume singletons match'), |
Tammo Spalink | 95c4373 | 2012-07-25 15:57:14 -0700 | [diff] [blame] | 112 | CmdArg('--comps', nargs='*', default=[], |
| 113 | help='list of canonical component names'), |
| 114 | CmdArg('--missing', nargs='*', default=[], |
| 115 | help='list component classes to be assumed missing'), |
Tammo Spalink | 5c69983 | 2012-07-03 17:50:39 +0800 | [diff] [blame] | 116 | CmdArg('--status', nargs='*', default=['supported'], |
Tammo Spalink | 95c4373 | 2012-07-25 15:57:14 -0700 | [diff] [blame] | 117 | help='consider only HWIDs within this list of status values')) |
| 118 | def BestMatchHwids(options): |
Tammo Spalink | 8fab531 | 2012-05-28 18:33:30 +0800 | [diff] [blame] | 119 | """Determine a list of possible HWIDs using provided args and probeing. |
| 120 | |
| 121 | VOLATILE can always be determined by probing. To get a unique |
| 122 | result, VARIANT must be specified for all cases where the matching |
| 123 | BOM has more than one associated variant code, otherwise all HWID |
| 124 | variants will be returned. Both VARIANT and BOM information can |
Tammo Spalink | 95c4373 | 2012-07-25 15:57:14 -0700 | [diff] [blame] | 125 | alternatively be specified using the --stdin_comps argument, which |
| 126 | allows specifying a list of canonical names (one per line) on stdin, |
| 127 | one per line. Based on what is known from BOM and stdin_comps, |
| 128 | determine a list of components to probe for, and use those probe |
| 129 | results to resolve a list of matching HWIDs. If no boms, |
| 130 | components, or variant codes are specified, then a list of all HWIDs |
Andy Cheng | 8ece738 | 2012-08-22 16:25:42 +0800 | [diff] [blame] | 131 | that match probeable components will be returned. |
Tammo Spalink | 8fab531 | 2012-05-28 18:33:30 +0800 | [diff] [blame] | 132 | |
| 133 | Returns (on stdout): A list of HWIDs that match the available probe |
| 134 | results and argument contraints, one per line. |
Tammo Spalink | 70b48a5 | 2012-08-08 16:54:51 -0700 | [diff] [blame] | 135 | |
| 136 | Example: |
| 137 | |
| 138 | // Three ways to specify a keyboard (assuming it is a variant component) |
| 139 | gooftool best_match_hwids --missing keyboard |
| 140 | gooftool best_match_hwids --variant A or |
| 141 | gooftool best_match_hwids --comps us_kbd |
Tammo Spalink | 8fab531 | 2012-05-28 18:33:30 +0800 | [diff] [blame] | 142 | """ |
Andy Cheng | 7a76cb8 | 2012-11-19 18:08:19 +0800 | [diff] [blame] | 143 | |
Tammo Spalink | 5c69983 | 2012-07-03 17:50:39 +0800 | [diff] [blame] | 144 | map(hwid_tool.Validate.Status, options.status) |
Tammo Spalink | 95c4373 | 2012-07-25 15:57:14 -0700 | [diff] [blame] | 145 | hw_db = hwid_tool.HardwareDb(options.hwdb_path) |
Tammo Spalink | 5c69983 | 2012-07-03 17:50:39 +0800 | [diff] [blame] | 146 | comp_db = hw_db.comp_db |
Tammo Spalink | 3a7e902 | 2012-06-27 14:08:40 +0800 | [diff] [blame] | 147 | device = hw_db.GetDevice(options.board) |
Tammo Spalink | 5c69983 | 2012-07-03 17:50:39 +0800 | [diff] [blame] | 148 | component_spec = hwid_tool.ComponentSpec.New() |
Tammo Spalink | 8fab531 | 2012-05-28 18:33:30 +0800 | [diff] [blame] | 149 | if options.bom: |
Tammo Spalink | 5c69983 | 2012-07-03 17:50:39 +0800 | [diff] [blame] | 150 | device.BomExists(options.bom) |
Tammo Spalink | 01e1172 | 2012-07-24 10:17:54 -0700 | [diff] [blame] | 151 | component_spec = hwid_tool.CombineComponentSpecs( |
Tammo Spalink | 5c69983 | 2012-07-03 17:50:39 +0800 | [diff] [blame] | 152 | component_spec, device.boms[options.bom].primary) |
Tammo Spalink | 8fab531 | 2012-05-28 18:33:30 +0800 | [diff] [blame] | 153 | if options.variant: |
Tammo Spalink | 5c69983 | 2012-07-03 17:50:39 +0800 | [diff] [blame] | 154 | device.VariantExists(options.variant) |
| 155 | variant_spec = device.variants[options.variant] |
Tammo Spalink | 01e1172 | 2012-07-24 10:17:54 -0700 | [diff] [blame] | 156 | if hwid_tool.ComponentSpecsConflict(component_spec, variant_spec): |
Tammo Spalink | 95c4373 | 2012-07-25 15:57:14 -0700 | [diff] [blame] | 157 | sys.exit('ERROR: multiple specifications for these components:\n%s' |
Tammo Spalink | 394e449 | 2012-08-01 10:20:30 -0700 | [diff] [blame] | 158 | % YamlWrite(sorted( |
| 159 | hwid_tool.ComponentSpecClasses(component_spec) & |
| 160 | hwid_tool.ComponentSpecClasses(variant_spec)))) |
Tammo Spalink | 01e1172 | 2012-07-24 10:17:54 -0700 | [diff] [blame] | 161 | component_spec = hwid_tool.CombineComponentSpecs( |
| 162 | component_spec, variant_spec) |
Tammo Spalink | 95c4373 | 2012-07-25 15:57:14 -0700 | [diff] [blame] | 163 | if options.comps or options.missing: |
| 164 | map(comp_db.CompExists, options.comps) |
| 165 | map(comp_db.CompClassExists, options.missing) |
| 166 | extra_comp_spec = comp_db.CreateComponentSpec( |
| 167 | components=options.comps, |
| 168 | missing=options.missing) |
| 169 | print 'cmdline asserted components:\n%s' % extra_comp_spec.Encode() |
| 170 | if hwid_tool.ComponentSpecsConflict(component_spec, extra_comp_spec): |
| 171 | sys.exit('ERROR: multiple specifications for these components:\n%s' |
Tammo Spalink | 394e449 | 2012-08-01 10:20:30 -0700 | [diff] [blame] | 172 | % YamlWrite(sorted( |
| 173 | hwid_tool.ComponentSpecClasses(component_spec) & |
| 174 | hwid_tool.ComponentSpecClasses(extra_comp_spec)))) |
Tammo Spalink | 95c4373 | 2012-07-25 15:57:14 -0700 | [diff] [blame] | 175 | component_spec = hwid_tool.CombineComponentSpecs( |
| 176 | component_spec, extra_comp_spec) |
Tammo Spalink | 01e1172 | 2012-07-24 10:17:54 -0700 | [diff] [blame] | 177 | spec_classes = hwid_tool.ComponentSpecClasses(component_spec) |
Tammo Spalink | 95c4373 | 2012-07-25 15:57:14 -0700 | [diff] [blame] | 178 | missing_classes = set(comp_db.all_comp_classes) - spec_classes |
Tammo Spalink | 5c69983 | 2012-07-03 17:50:39 +0800 | [diff] [blame] | 179 | if missing_classes and not options.optimistic: |
Andy Cheng | 8ece738 | 2012-08-22 16:25:42 +0800 | [diff] [blame] | 180 | non_probeable_missing = missing_classes - PROBEABLE_COMPONENT_CLASSES |
| 181 | if non_probeable_missing: |
Tammo Spalink | 95c4373 | 2012-07-25 15:57:14 -0700 | [diff] [blame] | 182 | sys.exit('FAILURE: these classes are necessary, were not specified ' |
Tammo Spalink | 70b48a5 | 2012-08-08 16:54:51 -0700 | [diff] [blame] | 183 | 'as inputs, and cannot be probed for:\n%s' |
| 184 | 'This problem can often be addressed by specifying all of ' |
| 185 | 'the missing components on the command line (see the command ' |
Andy Cheng | 8ece738 | 2012-08-22 16:25:42 +0800 | [diff] [blame] | 186 | 'help).' % YamlWrite(list(non_probeable_missing))) |
Tammo Spalink | 95c4373 | 2012-07-25 15:57:14 -0700 | [diff] [blame] | 187 | print 'probing for missing classes:' |
| 188 | print YamlWrite(list(missing_classes)) |
| 189 | probe_results = Probe(target_comp_classes=list(missing_classes), |
| 190 | probe_volatile=False, probe_initial_config=False) |
| 191 | cooked_components = comp_db.MatchComponentProbeValues( |
| 192 | probe_results.found_probe_value_map) |
| 193 | if cooked_components.unmatched: |
| 194 | sys.exit('ERROR: some probed components are unrecognized:\n%s' |
| 195 | % YamlWrite(cooked_components.unmatched)) |
| 196 | probed_comp_spec = comp_db.CreateComponentSpec( |
| 197 | components=cooked_components.matched, |
| 198 | missing=probe_results.missing_component_classes) |
| 199 | component_spec = hwid_tool.CombineComponentSpecs( |
| 200 | component_spec, probed_comp_spec) |
| 201 | print YamlWrite({'component data used for matching': { |
| 202 | 'missing component classes': component_spec.classes_missing, |
| 203 | 'found components': component_spec.components}}) |
| 204 | component_data = hwid_tool.ComponentData( |
| 205 | extant_components=hwid_tool.ComponentSpecCompClassMap( |
| 206 | component_spec).keys(), |
| 207 | classes_missing=component_spec.classes_missing) |
| 208 | match_tree = device.BuildMatchTree(component_data) |
| 209 | if not match_tree: |
| 210 | sys.exit('FAILURE: NO matching BOMs found') |
| 211 | print 'potential BOMs/VARIANTs:' |
| 212 | potential_variants = set() |
| 213 | potential_volatiles = set() |
| 214 | for bom_name, variant_tree in match_tree.items(): |
| 215 | print ' BOM: %-8s VARIANTS: %s' % ( |
| 216 | bom_name, ', '.join(sorted(variant_tree))) |
| 217 | for variant_code in variant_tree: |
| 218 | potential_variants.add(variant_code) |
| 219 | for volatile_code in device.volatiles: |
| 220 | status = device.GetHwidStatus(bom_name, variant_code, volatile_code) |
| 221 | if status in options.status: |
| 222 | potential_volatiles.add(volatile_code) |
| 223 | print '' |
| 224 | if len(potential_variants) == 0: |
| 225 | sys.exit('FAILURE: no matching VARIANTs found') |
| 226 | if len(potential_volatiles) == 0: |
| 227 | sys.exit('FAILURE: no VOLATILEs found for potential matching BOMs/VARIANTS ' |
| 228 | '(with specified status)') |
Tammo Spalink | 394e449 | 2012-08-01 10:20:30 -0700 | [diff] [blame] | 229 | if (options.optimistic and |
| 230 | len(match_tree) == 1 and |
| 231 | len(potential_variants) == 1 and |
| 232 | len(potential_volatiles) == 1): |
| 233 | print ('MATCHING HWID: %s' % device.FmtHwid(match_tree.keys().pop(), |
| 234 | potential_variants.pop(), |
| 235 | potential_volatiles.pop())) |
Tammo Spalink | 5c69983 | 2012-07-03 17:50:39 +0800 | [diff] [blame] | 236 | return |
Tammo Spalink | 95c4373 | 2012-07-25 15:57:14 -0700 | [diff] [blame] | 237 | print ('probing VOLATILEs to resolve potential matches: %s\n' % |
| 238 | ', '.join(sorted(potential_volatiles))) |
| 239 | vol_probe_results = Probe( |
| 240 | target_comp_classes=[], |
| 241 | probe_volatile=True, |
| 242 | probe_initial_config=False) |
| 243 | cooked_volatiles = device.MatchVolatileValues( |
| 244 | vol_probe_results.found_volatile_values) |
| 245 | match_tree = device.BuildMatchTree( |
| 246 | component_data, cooked_volatiles.matched_tags) |
| 247 | matched_hwids = device.GetMatchTreeHwids(match_tree) |
| 248 | if matched_hwids: |
| 249 | for hwid in matched_hwids: |
Ricky Liang | 02f3bd7 | 2012-09-21 14:33:01 +0800 | [diff] [blame] | 250 | if matched_hwids[hwid] in options.status: |
| 251 | print 'MATCHING HWID: %s' % hwid |
Tammo Spalink | 95c4373 | 2012-07-25 15:57:14 -0700 | [diff] [blame] | 252 | return |
| 253 | print 'exact HWID matching failed, but the following BOMs match: %s' % ( |
| 254 | ', '.join(sorted(match_tree))) |
| 255 | if options.optimistic and len(match_tree) == 1: |
| 256 | bom_name = set(match_tree).pop() |
Tammo Spalink | 5c69983 | 2012-07-03 17:50:39 +0800 | [diff] [blame] | 257 | bom = device.boms[bom_name] |
Tammo Spalink | 95c4373 | 2012-07-25 15:57:14 -0700 | [diff] [blame] | 258 | variant_matches = match_tree[bom_name] |
Tammo Spalink | 5c69983 | 2012-07-03 17:50:39 +0800 | [diff] [blame] | 259 | if len(variant_matches) == 1: |
| 260 | var_code = set(variant_matches).pop() |
| 261 | elif len(bom.variants) == 1: |
| 262 | var_code = set(bom.variants).pop() |
| 263 | else: |
Tammo Spalink | 95c4373 | 2012-07-25 15:57:14 -0700 | [diff] [blame] | 264 | sys.exit('FAILURE: NO matching HWIDs found; optimistic matching failed ' |
| 265 | 'because there were too many variants to choose from for BOM %r' |
| 266 | % bom_name) |
| 267 | hwids = [device.FmtHwid(bom_name, var_code, vol_code) |
| 268 | for vol_code in device.volatiles |
| 269 | if device.GetHwidStatus(bom_name, var_code, vol_code) |
| 270 | in options.status] |
| 271 | for hwid in hwids: |
| 272 | print 'MATCHING HWID: %s' % hwid |
Tammo Spalink | 5c69983 | 2012-07-03 17:50:39 +0800 | [diff] [blame] | 273 | return |
| 274 | else: |
Tammo Spalink | 95c4373 | 2012-07-25 15:57:14 -0700 | [diff] [blame] | 275 | print ('optimistic matching not attempted because either it was ' |
| 276 | 'not requested, or because the number of BOMs was <> 1\n') |
| 277 | sys.exit('FAILURE: NO matching HWIDs found') |
Tammo Spalink | 8fab531 | 2012-05-28 18:33:30 +0800 | [diff] [blame] | 278 | |
| 279 | |
Tammo Spalink | 9a96b8a | 2012-04-03 11:10:41 +0800 | [diff] [blame] | 280 | @Command('probe', |
| 281 | CmdArg('--comps', nargs='*', |
| 282 | help='List of keys from the component_db registry.'), |
| 283 | CmdArg('--no_vol', action='store_true', |
| 284 | help='Do not probe volatile data.'), |
| 285 | CmdArg('--no_ic', action='store_true', |
Jon Salz | 0f8a684 | 2012-09-25 11:28:22 +0800 | [diff] [blame] | 286 | help='Do not probe initial_config data.'), |
| 287 | CmdArg('--include_vpd', action='store_true', |
| 288 | help='Include VPD data in volatiles.')) |
Tammo Spalink | 9a96b8a | 2012-04-03 11:10:41 +0800 | [diff] [blame] | 289 | def RunProbe(options): |
| 290 | """Print yaml-formatted breakdown of probed device properties.""" |
Ricky Liang | a70a120 | 2013-03-15 15:03:17 +0800 | [diff] [blame] | 291 | print GetGooftool(options).Probe( |
| 292 | target_comp_classes=options.comps, |
| 293 | probe_volatile=not options.no_vol, |
| 294 | probe_initial_config=not options.no_ic, |
| 295 | probe_vpd=options.include_vpd).Encode() |
Ricky Liang | 5339023 | 2013-03-08 15:37:57 +0800 | [diff] [blame] | 296 | |
Tammo Spalink | 9a96b8a | 2012-04-03 11:10:41 +0800 | [diff] [blame] | 297 | |
Tammo Spalink | 214caf4 | 2012-05-28 10:45:00 +0800 | [diff] [blame] | 298 | @Command('verify_components', |
| 299 | _hwdb_path_cmd_arg, |
Tammo Spalink | 5c69983 | 2012-07-03 17:50:39 +0800 | [diff] [blame] | 300 | CmdArg('target_comps', nargs='*')) |
Tammo Spalink | 214caf4 | 2012-05-28 10:45:00 +0800 | [diff] [blame] | 301 | def VerifyComponents(options): |
Andy Cheng | 8ece738 | 2012-08-22 16:25:42 +0800 | [diff] [blame] | 302 | """Verify that probeable components all match entries in the component_db. |
Tammo Spalink | 214caf4 | 2012-05-28 10:45:00 +0800 | [diff] [blame] | 303 | |
Tammo Spalink | 5c69983 | 2012-07-03 17:50:39 +0800 | [diff] [blame] | 304 | Probe for each component class in the target_comps and verify |
Tammo Spalink | 214caf4 | 2012-05-28 10:45:00 +0800 | [diff] [blame] | 305 | that a corresponding match exists in the component_db -- make sure |
| 306 | that these components are present, that they have been approved, but |
| 307 | do not check against any specific BOM/HWID configurations. |
| 308 | """ |
Andy Cheng | 7a76cb8 | 2012-11-19 18:08:19 +0800 | [diff] [blame] | 309 | |
Andy Cheng | c531e2f | 2012-10-15 19:09:17 +0800 | [diff] [blame] | 310 | try: |
Ricky Liang | a70a120 | 2013-03-15 15:03:17 +0800 | [diff] [blame] | 311 | result = GetGooftool(options).VerifyComponents( |
Andy Cheng | c531e2f | 2012-10-15 19:09:17 +0800 | [diff] [blame] | 312 | options.target_comps) |
| 313 | except ValueError, e: |
| 314 | sys.exit(e) |
| 315 | |
Ricky Liang | 5339023 | 2013-03-08 15:37:57 +0800 | [diff] [blame] | 316 | PrintVerifyComponentsResults(result) |
| 317 | |
| 318 | |
| 319 | def PrintVerifyComponentsResults(result): |
| 320 | """Prints out the results of VerifyComponents method call. |
| 321 | |
| 322 | Groups the results into two groups: 'matches' and 'errors', and prints out |
| 323 | their values. |
| 324 | """ |
Andy Cheng | c531e2f | 2012-10-15 19:09:17 +0800 | [diff] [blame] | 325 | # group by matches and errors |
Tammo Spalink | 214caf4 | 2012-05-28 10:45:00 +0800 | [diff] [blame] | 326 | matches = [] |
Andy Cheng | c531e2f | 2012-10-15 19:09:17 +0800 | [diff] [blame] | 327 | errors = [] |
| 328 | for result_list in result.values(): |
| 329 | for component_name, _, error in result_list: |
| 330 | if component_name: |
| 331 | matches.append(component_name) |
Tammo Spalink | 214caf4 | 2012-05-28 10:45:00 +0800 | [diff] [blame] | 332 | else: |
Andy Cheng | c531e2f | 2012-10-15 19:09:17 +0800 | [diff] [blame] | 333 | errors.append(error) |
| 334 | |
Andy Cheng | 228a8c9 | 2012-08-27 10:53:57 +0800 | [diff] [blame] | 335 | if matches: |
| 336 | print 'found probeable components:\n %s' % '\n '.join(matches) |
Tammo Spalink | 214caf4 | 2012-05-28 10:45:00 +0800 | [diff] [blame] | 337 | if errors: |
Andy Cheng | 228a8c9 | 2012-08-27 10:53:57 +0800 | [diff] [blame] | 338 | print '\nerrors:\n %s' % '\n '.join(errors) |
| 339 | sys.exit('\ncomponent verification FAILURE') |
Tammo Spalink | 214caf4 | 2012-05-28 10:45:00 +0800 | [diff] [blame] | 340 | else: |
Andy Cheng | 228a8c9 | 2012-08-27 10:53:57 +0800 | [diff] [blame] | 341 | print "\ncomponent verification SUCCESS" |
Tammo Spalink | 214caf4 | 2012-05-28 10:45:00 +0800 | [diff] [blame] | 342 | |
| 343 | |
Tammo Spalink | 9a96b8a | 2012-04-03 11:10:41 +0800 | [diff] [blame] | 344 | @Command('verify_hwid', |
Tammo Spalink | 95c4373 | 2012-07-25 15:57:14 -0700 | [diff] [blame] | 345 | _hwid_status_list_cmd_arg, |
Jon Salz | 0f8a684 | 2012-09-25 11:28:22 +0800 | [diff] [blame] | 346 | _hwdb_path_cmd_arg, |
Jon Salz | ce124fb | 2012-10-02 17:42:03 +0800 | [diff] [blame] | 347 | _probe_results_cmd_arg, |
| 348 | _hwid_cmd_arg) |
Tammo Spalink | 9a96b8a | 2012-04-03 11:10:41 +0800 | [diff] [blame] | 349 | def VerifyHwid(options): |
| 350 | """Verify system HWID properties match probed device properties. |
| 351 | |
| 352 | First probe components, volatile and initial_config parameters for |
| 353 | the DUT. Then use the available device data to produce a list of |
| 354 | candidate HWIDs. Then verify the HWID from the DUT is present in |
| 355 | that list. Then verify that the DUT initial config values match |
| 356 | those specified for its HWID. Finally, verify that VPD contains all |
| 357 | the necessary fields as specified by the board data, and when |
| 358 | possible verify that values are legitimate. |
| 359 | """ |
Andy Cheng | 7a76cb8 | 2012-11-19 18:08:19 +0800 | [diff] [blame] | 360 | |
Ricky Liang | f7857c1 | 2012-09-17 13:34:41 +0800 | [diff] [blame] | 361 | def VerifyVpd(ro_vpd_keys, rw_vpd_keys): |
Tammo Spalink | 5c69983 | 2012-07-03 17:50:39 +0800 | [diff] [blame] | 362 | for key in ro_vpd_keys: |
| 363 | if key not in ro_vpd: |
Ricky Liang | f7857c1 | 2012-09-17 13:34:41 +0800 | [diff] [blame] | 364 | sys.exit('Missing required RO VPD field: %s' % key) |
Tammo Spalink | 01e1172 | 2012-07-24 10:17:54 -0700 | [diff] [blame] | 365 | known_valid_values = KNOWN_VPD_FIELD_DATA.get(key, None) |
Tammo Spalink | 5c69983 | 2012-07-03 17:50:39 +0800 | [diff] [blame] | 366 | value = ro_vpd[key] |
Ricky Liang | f7857c1 | 2012-09-17 13:34:41 +0800 | [diff] [blame] | 367 | if (known_valid_values is not None) and (value not in known_valid_values): |
| 368 | sys.exit('Invalid RO VPD entry : key %r, value %r' % (key, value)) |
Ricky Liang | f7857c1 | 2012-09-17 13:34:41 +0800 | [diff] [blame] | 369 | for key in rw_vpd_keys: |
| 370 | if key not in rw_vpd: |
| 371 | sys.exit('Missing required RW VPD field: %s' % key) |
| 372 | known_valid_values = KNOWN_VPD_FIELD_DATA.get(key, None) |
| 373 | value = rw_vpd[key] |
| 374 | if (known_valid_values is not None) and (value not in known_valid_values): |
| 375 | sys.exit('Invalid RW VPD entry : key %r, value %r' % (key, value)) |
Andy Cheng | 0465d13 | 2013-03-20 12:12:06 +0800 | [diff] [blame] | 376 | event_log.Log('vpd', ro_vpd=FilterDict(ro_vpd), rw_vpd=FilterDict(rw_vpd)) |
Tammo Spalink | 5c69983 | 2012-07-03 17:50:39 +0800 | [diff] [blame] | 377 | map(hwid_tool.Validate.Status, options.status) |
Jon Salz | 0f8a684 | 2012-09-25 11:28:22 +0800 | [diff] [blame] | 378 | |
Jon Salz | 8135081 | 2012-10-11 16:13:22 +0800 | [diff] [blame] | 379 | if not options.hwid or not options.probe_results: |
| 380 | main_fw_file = crosfw.LoadMainFirmware().GetFileName() |
Jon Salz | 0f8a684 | 2012-09-25 11:28:22 +0800 | [diff] [blame] | 381 | |
| 382 | if options.hwid: |
| 383 | hwid_str = options.hwid |
| 384 | else: |
| 385 | gbb_result = Shell('gbb_utility -g --hwid %s' % main_fw_file).stdout |
| 386 | hwid_str = re.findall(r'hardware_id:(.*)', gbb_result)[0].strip() |
Tammo Spalink | 95c4373 | 2012-07-25 15:57:14 -0700 | [diff] [blame] | 387 | hwid = hwid_tool.ParseHwid(hwid_str) |
Tammo Spalink | 5c69983 | 2012-07-03 17:50:39 +0800 | [diff] [blame] | 388 | hw_db = hwid_tool.HardwareDb(options.hwdb_path) |
Jon Salz | 0f8a684 | 2012-09-25 11:28:22 +0800 | [diff] [blame] | 389 | print 'Verifying HWID: %r\n' % hwid.hwid |
Tammo Spalink | 95c4373 | 2012-07-25 15:57:14 -0700 | [diff] [blame] | 390 | device = hw_db.GetDevice(hwid.board) |
| 391 | hwid_status = device.GetHwidStatus(hwid.bom, hwid.variant, hwid.volatile) |
| 392 | if hwid_status not in options.status: |
Tammo Spalink | 5c69983 | 2012-07-03 17:50:39 +0800 | [diff] [blame] | 393 | sys.exit('HWID status must be one of [%s], found %r' % |
Hung-Te Lin | 4f5beb1 | 2012-08-20 14:48:06 +0800 | [diff] [blame] | 394 | (', '.join(options.status), hwid_status)) |
Jon Salz | 0f8a684 | 2012-09-25 11:28:22 +0800 | [diff] [blame] | 395 | if options.probe_results: |
| 396 | # Pull in probe results (including VPD data) from the given file |
| 397 | # rather than probing the current system. |
| 398 | probe_results = hwid_tool.ProbeResults.Decode( |
| 399 | open(options.probe_results).read()) |
| 400 | ro_vpd = {} |
| 401 | rw_vpd = {} |
| 402 | for k, v in probe_results.found_volatile_values.items(): |
| 403 | match = re.match('^vpd\.(ro|rw)\.(\w+)$', k) |
| 404 | if match: |
| 405 | del probe_results.found_volatile_values[k] |
| 406 | (ro_vpd if match.group(1) == 'ro' else rw_vpd)[match.group(2)] = v |
| 407 | else: |
| 408 | probe_results = Probe() |
| 409 | ro_vpd = ReadRoVpd(main_fw_file) |
| 410 | rw_vpd = ReadRwVpd(main_fw_file) |
Tammo Spalink | 95c4373 | 2012-07-25 15:57:14 -0700 | [diff] [blame] | 411 | cooked_components = hw_db.comp_db.MatchComponentProbeValues( |
| 412 | probe_results.found_probe_value_map) |
| 413 | cooked_volatiles = device.MatchVolatileValues( |
| 414 | probe_results.found_volatile_values) |
| 415 | cooked_initial_configs = device.MatchInitialConfigValues( |
| 416 | probe_results.initial_configs) |
| 417 | component_data = hwid_tool.ComponentData( |
| 418 | extant_components=cooked_components.matched, |
| 419 | classes_missing=probe_results.missing_component_classes) |
| 420 | match_tree = device.BuildMatchTree( |
| 421 | component_data, cooked_volatiles.matched_tags) |
| 422 | matched_hwids = device.GetMatchTreeHwids(match_tree) |
| 423 | print 'HWID status: %s\n' % hwid_status |
| 424 | print 'probed system components:' |
| 425 | print YamlWrite(cooked_components.__dict__) |
| 426 | print 'missing component classes:' |
| 427 | print YamlWrite(probe_results.missing_component_classes) |
| 428 | print 'probed volatiles:' |
| 429 | print YamlWrite(cooked_volatiles.__dict__) |
| 430 | print 'probed initial_configs:' |
| 431 | print YamlWrite(cooked_initial_configs) |
| 432 | print 'hwid match tree:' |
| 433 | print YamlWrite(match_tree) |
Andy Cheng | 0465d13 | 2013-03-20 12:12:06 +0800 | [diff] [blame] | 434 | event_log.Log( |
Tammo Spalink | 5c69983 | 2012-07-03 17:50:39 +0800 | [diff] [blame] | 435 | 'probe', |
Tammo Spalink | 95c4373 | 2012-07-25 15:57:14 -0700 | [diff] [blame] | 436 | found_components=cooked_components.__dict__, |
| 437 | missing_component_classes=probe_results.missing_component_classes, |
| 438 | volatiles=cooked_volatiles.__dict__, |
| 439 | initial_configs=cooked_initial_configs) |
| 440 | if hwid.hwid not in matched_hwids: |
Tammo Spalink | 5c69983 | 2012-07-03 17:50:39 +0800 | [diff] [blame] | 441 | err_msg = 'HWID verification FAILED.\n' |
Tammo Spalink | 95c4373 | 2012-07-25 15:57:14 -0700 | [diff] [blame] | 442 | if cooked_components.unmatched: |
Tammo Spalink | 5c69983 | 2012-07-03 17:50:39 +0800 | [diff] [blame] | 443 | sys.exit(err_msg + 'some components could not be indentified:\n%s' % |
Tammo Spalink | 95c4373 | 2012-07-25 15:57:14 -0700 | [diff] [blame] | 444 | YamlWrite(cooked_components.unmatched)) |
| 445 | if not match_tree: |
Tammo Spalink | 5c69983 | 2012-07-03 17:50:39 +0800 | [diff] [blame] | 446 | sys.exit(err_msg + 'no matching boms were found for components:\n%s' % |
Tammo Spalink | 95c4373 | 2012-07-25 15:57:14 -0700 | [diff] [blame] | 447 | component_data.Encode()) |
| 448 | if hwid.bom not in match_tree: |
Tammo Spalink | 5c69983 | 2012-07-03 17:50:39 +0800 | [diff] [blame] | 449 | sys.exit(err_msg + 'matching boms [%s] do not include target bom %r' % |
Tammo Spalink | 95c4373 | 2012-07-25 15:57:14 -0700 | [diff] [blame] | 450 | (', '.join(sorted(match_tree)), hwid.bom)) |
| 451 | err_msg += 'target bom %r matches components' % hwid.bom |
| 452 | if hwid.bom not in device.IntersectBomsAndInitialConfigs( |
| 453 | cooked_initial_configs): |
Tammo Spalink | 5c69983 | 2012-07-03 17:50:39 +0800 | [diff] [blame] | 454 | sys.exit(err_msg + ', but failed initial config verification') |
Tammo Spalink | 95c4373 | 2012-07-25 15:57:14 -0700 | [diff] [blame] | 455 | matched_variants = match_tree.get(hwid.bom, {}) |
| 456 | if hwid.variant not in matched_variants: |
Tammo Spalink | 5c69983 | 2012-07-03 17:50:39 +0800 | [diff] [blame] | 457 | sys.exit(err_msg + ', but target variant_code %r did not match' % |
Tammo Spalink | 95c4373 | 2012-07-25 15:57:14 -0700 | [diff] [blame] | 458 | hwid.variant) |
| 459 | matched_volatiles = matched_variants.get(hwid.variant, {}) |
| 460 | if hwid.volatile not in matched_volatiles: |
Tammo Spalink | 5c69983 | 2012-07-03 17:50:39 +0800 | [diff] [blame] | 461 | sys.exit(err_msg + ', but target volatile_code %r did not match' % |
Tammo Spalink | 95c4373 | 2012-07-25 15:57:14 -0700 | [diff] [blame] | 462 | hwid.volatile) |
| 463 | found_status = matched_volatiles.get(hwid.volatile, None) |
Tammo Spalink | 5c69983 | 2012-07-03 17:50:39 +0800 | [diff] [blame] | 464 | sys.exit(err_msg + ', but hwid status %r was unacceptable' % found_status) |
Ricky Liang | f7857c1 | 2012-09-17 13:34:41 +0800 | [diff] [blame] | 465 | VerifyVpd(device.vpd_ro_fields, device.vpd_rw_fields) |
Andy Cheng | 0465d13 | 2013-03-20 12:12:06 +0800 | [diff] [blame] | 466 | event_log.Log('verified_hwid', hwid=hwid) |
Tammo Spalink | 95c4373 | 2012-07-25 15:57:14 -0700 | [diff] [blame] | 467 | print 'Verification SUCCESS!' |
Tammo Spalink | 9a96b8a | 2012-04-03 11:10:41 +0800 | [diff] [blame] | 468 | |
| 469 | |
| 470 | @Command('verify_keys') |
Tammo Spalink | 01e1172 | 2012-07-24 10:17:54 -0700 | [diff] [blame] | 471 | def VerifyKeys(options): # pylint: disable=W0613 |
Tammo Spalink | 9a96b8a | 2012-04-03 11:10:41 +0800 | [diff] [blame] | 472 | """Verify keys in firmware and SSD match.""" |
Andy Cheng | 7a76cb8 | 2012-11-19 18:08:19 +0800 | [diff] [blame] | 473 | |
Ricky Liang | a70a120 | 2013-03-15 15:03:17 +0800 | [diff] [blame] | 474 | return GetGooftool(options).VerifyKeys() |
Tammo Spalink | 9a96b8a | 2012-04-03 11:10:41 +0800 | [diff] [blame] | 475 | |
| 476 | |
| 477 | @Command('set_fw_bitmap_locale') |
Tammo Spalink | 01e1172 | 2012-07-24 10:17:54 -0700 | [diff] [blame] | 478 | def SetFirmwareBitmapLocale(options): # pylint: disable=W0613 |
Tammo Spalink | 9a96b8a | 2012-04-03 11:10:41 +0800 | [diff] [blame] | 479 | """Use VPD locale value to set firmware bitmap default language.""" |
Andy Cheng | 7a76cb8 | 2012-11-19 18:08:19 +0800 | [diff] [blame] | 480 | |
Ricky Liang | a70a120 | 2013-03-15 15:03:17 +0800 | [diff] [blame] | 481 | (index, locale) = GetGooftool(options).SetFirmwareBitmapLocale() |
Andy Cheng | 2582d29 | 2012-12-04 17:38:28 +0800 | [diff] [blame] | 482 | logging.info('Firmware bitmap initial locale set to %d (%s).', |
| 483 | index, locale) |
Tammo Spalink | 9a96b8a | 2012-04-03 11:10:41 +0800 | [diff] [blame] | 484 | |
| 485 | |
| 486 | @Command('verify_system_time') |
Tammo Spalink | 01e1172 | 2012-07-24 10:17:54 -0700 | [diff] [blame] | 487 | def VerifySystemTime(options): # pylint: disable=W0613 |
Tammo Spalink | 9a96b8a | 2012-04-03 11:10:41 +0800 | [diff] [blame] | 488 | """Verify system time is later than release filesystem creation time.""" |
Andy Cheng | 7a76cb8 | 2012-11-19 18:08:19 +0800 | [diff] [blame] | 489 | |
Ricky Liang | a70a120 | 2013-03-15 15:03:17 +0800 | [diff] [blame] | 490 | return GetGooftool(options).VerifySystemTime() |
Tammo Spalink | 9a96b8a | 2012-04-03 11:10:41 +0800 | [diff] [blame] | 491 | |
| 492 | |
| 493 | @Command('verify_rootfs') |
Tammo Spalink | 01e1172 | 2012-07-24 10:17:54 -0700 | [diff] [blame] | 494 | def VerifyRootFs(options): # pylint: disable=W0613 |
Tammo Spalink | 9a96b8a | 2012-04-03 11:10:41 +0800 | [diff] [blame] | 495 | """Verify rootfs on SSD is valid by checking hash.""" |
Andy Cheng | 7a76cb8 | 2012-11-19 18:08:19 +0800 | [diff] [blame] | 496 | |
Ricky Liang | a70a120 | 2013-03-15 15:03:17 +0800 | [diff] [blame] | 497 | return GetGooftool(options).VerifyRootFs() |
Tammo Spalink | 9a96b8a | 2012-04-03 11:10:41 +0800 | [diff] [blame] | 498 | |
| 499 | |
| 500 | @Command('verify_switch_wp') |
Andy Cheng | c92e6f9 | 2012-11-20 16:55:53 +0800 | [diff] [blame] | 501 | def VerifyWPSwitch(options): # pylint: disable=W0613 |
Tammo Spalink | 9a96b8a | 2012-04-03 11:10:41 +0800 | [diff] [blame] | 502 | """Verify hardware write protection switch is enabled.""" |
Andy Cheng | 7a76cb8 | 2012-11-19 18:08:19 +0800 | [diff] [blame] | 503 | |
Ricky Liang | a70a120 | 2013-03-15 15:03:17 +0800 | [diff] [blame] | 504 | GetGooftool(options).VerifyWPSwitch() |
Tammo Spalink | 9a96b8a | 2012-04-03 11:10:41 +0800 | [diff] [blame] | 505 | |
| 506 | |
| 507 | @Command('verify_switch_dev') |
Tammo Spalink | 01e1172 | 2012-07-24 10:17:54 -0700 | [diff] [blame] | 508 | def VerifyDevSwitch(options): # pylint: disable=W0613 |
Tammo Spalink | 9a96b8a | 2012-04-03 11:10:41 +0800 | [diff] [blame] | 509 | """Verify developer switch is disabled.""" |
Andy Cheng | 7a76cb8 | 2012-11-19 18:08:19 +0800 | [diff] [blame] | 510 | |
Ricky Liang | a70a120 | 2013-03-15 15:03:17 +0800 | [diff] [blame] | 511 | if GetGooftool(options).CheckDevSwitchForDisabling(): |
Hung-Te Lin | d7d3472 | 2012-07-26 16:48:35 +0800 | [diff] [blame] | 512 | logging.warn('VerifyDevSwitch: No physical switch.') |
Andy Cheng | 0465d13 | 2013-03-20 12:12:06 +0800 | [diff] [blame] | 513 | event_log.Log('switch_dev', type='virtual switch') |
Tammo Spalink | 9a96b8a | 2012-04-03 11:10:41 +0800 | [diff] [blame] | 514 | |
| 515 | |
| 516 | @Command('write_protect') |
Tammo Spalink | 01e1172 | 2012-07-24 10:17:54 -0700 | [diff] [blame] | 517 | def EnableFwWp(options): # pylint: disable=W0613 |
Tammo Spalink | 9a96b8a | 2012-04-03 11:10:41 +0800 | [diff] [blame] | 518 | """Enable then verify firmware write protection.""" |
| 519 | |
Hung-Te Lin | b21c668 | 2012-08-01 13:53:57 +0800 | [diff] [blame] | 520 | def CalculateLegacyRange(fw_type, length, section_data, |
| 521 | section_name): |
Hung-Te Lin | 7ea39e8 | 2012-07-31 18:39:33 +0800 | [diff] [blame] | 522 | ro_size = length / 2 |
| 523 | ro_a = int(section_data[0] / ro_size) |
| 524 | ro_b = int((section_data[0] + section_data[1] - 1) / ro_size) |
| 525 | if ro_a != ro_b: |
| 526 | raise Error("%s firmware section %s has illegal size" % |
Hung-Te Lin | b21c668 | 2012-08-01 13:53:57 +0800 | [diff] [blame] | 527 | (fw_type, section_name)) |
Hung-Te Lin | 7ea39e8 | 2012-07-31 18:39:33 +0800 | [diff] [blame] | 528 | ro_offset = ro_a * ro_size |
| 529 | return (ro_offset, ro_size) |
| 530 | |
| 531 | def WriteProtect(fw_file_path, fw_type, legacy_section): |
Tammo Spalink | 9a96b8a | 2012-04-03 11:10:41 +0800 | [diff] [blame] | 532 | """Calculate protection size, then invoke flashrom. |
| 533 | |
| 534 | Our supported chips only allow write protecting half their total |
| 535 | size, so we parition the flash chipset space accordingly. |
| 536 | """ |
Andy Cheng | 7a76cb8 | 2012-11-19 18:08:19 +0800 | [diff] [blame] | 537 | |
Tammo Spalink | 9a96b8a | 2012-04-03 11:10:41 +0800 | [diff] [blame] | 538 | raw_image = open(fw_file_path, 'rb').read() |
Hung-Te Lin | 7ea39e8 | 2012-07-31 18:39:33 +0800 | [diff] [blame] | 539 | wp_section = 'WP_RO' |
Tammo Spalink | 9a96b8a | 2012-04-03 11:10:41 +0800 | [diff] [blame] | 540 | image = crosfw.FirmwareImage(raw_image) |
Hung-Te Lin | 7ea39e8 | 2012-07-31 18:39:33 +0800 | [diff] [blame] | 541 | if image.has_section(wp_section): |
| 542 | section_data = image.get_section_area(wp_section) |
| 543 | ro_offset = section_data[0] |
| 544 | ro_size = section_data[1] |
| 545 | elif image.has_section(legacy_section): |
| 546 | section_data = image.get_section_area(legacy_section) |
| 547 | (ro_offset, ro_size) = CalculateLegacyRange( |
Hung-Te Lin | b21c668 | 2012-08-01 13:53:57 +0800 | [diff] [blame] | 548 | fw_type, len(raw_image), section_data, legacy_section) |
Hung-Te Lin | 7ea39e8 | 2012-07-31 18:39:33 +0800 | [diff] [blame] | 549 | else: |
| 550 | raise Error('could not find %s firmware section %s or %s' % |
| 551 | (fw_type, wp_section, legacy_section)) |
| 552 | |
| 553 | logging.debug('write protecting %s [off=%x size=%x]', fw_type, |
| 554 | ro_offset, ro_size) |
| 555 | crosfw.Flashrom(fw_type).EnableWriteProtection(ro_offset, ro_size) |
Tammo Spalink | 9a96b8a | 2012-04-03 11:10:41 +0800 | [diff] [blame] | 556 | |
| 557 | WriteProtect(crosfw.LoadMainFirmware().GetFileName(), 'main', 'RO_SECTION') |
Andy Cheng | 0465d13 | 2013-03-20 12:12:06 +0800 | [diff] [blame] | 558 | event_log.Log('wp', fw='main') |
Tammo Spalink | 9a96b8a | 2012-04-03 11:10:41 +0800 | [diff] [blame] | 559 | ec_fw_file = crosfw.LoadEcFirmware().GetFileName() |
| 560 | if ec_fw_file is not None: |
| 561 | WriteProtect(ec_fw_file, 'ec', 'EC_RO') |
Andy Cheng | 0465d13 | 2013-03-20 12:12:06 +0800 | [diff] [blame] | 562 | event_log.Log('wp', fw='ec') |
Tammo Spalink | 9a96b8a | 2012-04-03 11:10:41 +0800 | [diff] [blame] | 563 | else: |
| 564 | logging.warning('EC not write protected (seems there is no EC flash).') |
| 565 | |
| 566 | |
| 567 | @Command('clear_gbb_flags') |
Andy Cheng | c92e6f9 | 2012-11-20 16:55:53 +0800 | [diff] [blame] | 568 | def ClearGBBFlags(options): # pylint: disable=W0613 |
Tammo Spalink | 9a96b8a | 2012-04-03 11:10:41 +0800 | [diff] [blame] | 569 | """Zero out the GBB flags, in preparation for transition to release state. |
| 570 | |
| 571 | No GBB flags are set in release/shipping state, but they are useful |
| 572 | for factory/development. See "gbb_utility --flags" for details. |
| 573 | """ |
Andy Cheng | 7a76cb8 | 2012-11-19 18:08:19 +0800 | [diff] [blame] | 574 | |
Ricky Liang | a70a120 | 2013-03-15 15:03:17 +0800 | [diff] [blame] | 575 | GetGooftool(options).ClearGBBFlags() |
Andy Cheng | 0465d13 | 2013-03-20 12:12:06 +0800 | [diff] [blame] | 576 | event_log.Log('clear_gbb_flags') |
Tammo Spalink | 9a96b8a | 2012-04-03 11:10:41 +0800 | [diff] [blame] | 577 | |
| 578 | |
Jon Salz | aa3a30e | 2013-05-15 15:56:28 +0800 | [diff] [blame] | 579 | @Command('clear_factory_vpd_entries') |
| 580 | def ClearFactoryVPDEntries(options): # pylint: disable=W0613 |
| 581 | """Clears factory.* items in the RW VPD.""" |
| 582 | entries = GetGooftool(options).ClearFactoryVPDEntries() |
| 583 | event_log.Log('clear_factory_vpd_entries', entries=FilterDict(entries)) |
| 584 | |
| 585 | |
Tammo Spalink | 9a96b8a | 2012-04-03 11:10:41 +0800 | [diff] [blame] | 586 | @Command('prepare_wipe', |
| 587 | CmdArg('--fast', action='store_true', |
| 588 | help='use non-secure but faster wipe method.')) |
| 589 | def PrepareWipe(options): |
| 590 | """Prepare system for transition to release state in next reboot.""" |
Tammo Spalink | 9a96b8a | 2012-04-03 11:10:41 +0800 | [diff] [blame] | 591 | |
Ricky Liang | a70a120 | 2013-03-15 15:03:17 +0800 | [diff] [blame] | 592 | GetGooftool(options).PrepareWipe(options.fast) |
Tammo Spalink | 9a96b8a | 2012-04-03 11:10:41 +0800 | [diff] [blame] | 593 | |
| 594 | @Command('verify', |
Hung-Te Lin | 6d82754 | 2012-07-19 11:50:41 +0800 | [diff] [blame] | 595 | CmdArg('--no_write_protect', action='store_true', |
| 596 | help='Do not check write protection switch state.'), |
Tammo Spalink | 95c4373 | 2012-07-25 15:57:14 -0700 | [diff] [blame] | 597 | _hwid_status_list_cmd_arg, |
Jon Salz | ce124fb | 2012-10-02 17:42:03 +0800 | [diff] [blame] | 598 | _hwdb_path_cmd_arg, |
Ricky Liang | a70a120 | 2013-03-15 15:03:17 +0800 | [diff] [blame] | 599 | _board_cmd_arg, |
Jon Salz | ce124fb | 2012-10-02 17:42:03 +0800 | [diff] [blame] | 600 | _probe_results_cmd_arg, |
| 601 | _hwid_cmd_arg) |
Tammo Spalink | 9a96b8a | 2012-04-03 11:10:41 +0800 | [diff] [blame] | 602 | def Verify(options): |
| 603 | """Verifies if whole factory process is ready for finalization. |
| 604 | |
| 605 | This routine performs all the necessary checks to make sure the |
| 606 | device is ready to be finalized, but does not modify state. These |
| 607 | checks include dev switch, firmware write protection switch, hwid, |
| 608 | system time, keys, and root file system. |
| 609 | """ |
Andy Cheng | 7a76cb8 | 2012-11-19 18:08:19 +0800 | [diff] [blame] | 610 | |
Hung-Te Lin | 6d82754 | 2012-07-19 11:50:41 +0800 | [diff] [blame] | 611 | if not options.no_write_protect: |
Ricky Liang | a70a120 | 2013-03-15 15:03:17 +0800 | [diff] [blame] | 612 | VerifyWPSwitch(options) |
| 613 | VerifyDevSwitch(options) |
| 614 | if options.hwid_version == 2: |
| 615 | VerifyHwid(options) |
| 616 | elif options.hwid_version == 3: |
| 617 | VerifyHwidV3(options) |
| 618 | else: |
| 619 | raise Error, 'Invalid HWID version: %r' % options.hwid_version |
| 620 | VerifySystemTime(options) |
| 621 | VerifyKeys(options) |
| 622 | VerifyRootFs(options) |
Tammo Spalink | 9a96b8a | 2012-04-03 11:10:41 +0800 | [diff] [blame] | 623 | |
| 624 | |
Tammo Spalink | 86a61c6 | 2012-05-25 15:10:35 +0800 | [diff] [blame] | 625 | @Command('log_system_details') |
Tammo Spalink | 01e1172 | 2012-07-24 10:17:54 -0700 | [diff] [blame] | 626 | def LogSystemDetails(options): # pylint: disable=W0613 |
Tammo Spalink | 86a61c6 | 2012-05-25 15:10:35 +0800 | [diff] [blame] | 627 | """Write miscellaneous system details to the event log.""" |
Andy Cheng | 7a76cb8 | 2012-11-19 18:08:19 +0800 | [diff] [blame] | 628 | |
Andy Cheng | 0465d13 | 2013-03-20 12:12:06 +0800 | [diff] [blame] | 629 | event_log.Log('system_details', **Gooftool( |
Ricky Liang | a70a120 | 2013-03-15 15:03:17 +0800 | [diff] [blame] | 630 | hwid_version=options.hwid_version).GetSystemDetails()) |
Tammo Spalink | 86a61c6 | 2012-05-25 15:10:35 +0800 | [diff] [blame] | 631 | |
| 632 | |
Tammo Spalink | 9a96b8a | 2012-04-03 11:10:41 +0800 | [diff] [blame] | 633 | _upload_method_cmd_arg = CmdArg( |
| 634 | '--upload_method', metavar='METHOD:PARAM', |
| 635 | help=('How to perform the upload. METHOD should be one of ' |
cychiang | 3b15bd5 | 2012-09-08 13:58:18 +0800 | [diff] [blame] | 636 | '{ftp, shopfloor, ftps, cpfe}.')) |
Jon Salz | 6526643 | 2012-07-30 19:02:49 +0800 | [diff] [blame] | 637 | _add_file_cmd_arg = CmdArg( |
| 638 | '--add_file', metavar='FILE', action='append', |
| 639 | help='Extra file to include in report (must be an absolute path)') |
Tammo Spalink | 9a96b8a | 2012-04-03 11:10:41 +0800 | [diff] [blame] | 640 | |
| 641 | @Command('upload_report', |
Jon Salz | 6526643 | 2012-07-30 19:02:49 +0800 | [diff] [blame] | 642 | _upload_method_cmd_arg, |
| 643 | _add_file_cmd_arg) |
Tammo Spalink | 9a96b8a | 2012-04-03 11:10:41 +0800 | [diff] [blame] | 644 | def UploadReport(options): |
| 645 | """Create and a report containing key device details.""" |
Andy Cheng | 7a76cb8 | 2012-11-19 18:08:19 +0800 | [diff] [blame] | 646 | |
Hung-Te Lin | 6bd1647 | 2012-06-20 16:26:47 +0800 | [diff] [blame] | 647 | def NormalizeAsFileName(token): |
| 648 | return re.sub(r'\W+', '', token).strip() |
Tammo Spalink | 86a61c6 | 2012-05-25 15:10:35 +0800 | [diff] [blame] | 649 | ro_vpd = ReadRoVpd(crosfw.LoadMainFirmware().GetFileName()) |
| 650 | device_sn = ro_vpd.get('serial_number', None) |
| 651 | if device_sn is None: |
| 652 | logging.warning('RO_VPD missing device serial number') |
Andy Cheng | 0465d13 | 2013-03-20 12:12:06 +0800 | [diff] [blame] | 653 | device_sn = 'MISSING_SN_' + event_log.TimedUuid() |
Vic Yang | 85199e7 | 2013-01-28 14:33:11 +0800 | [diff] [blame] | 654 | target_name = '%s_%s.tar.xz' % (time.strftime('%Y%m%dT%H%M%SZ', |
| 655 | time.gmtime()), |
| 656 | NormalizeAsFileName(device_sn)) |
Tammo Spalink | 86a61c6 | 2012-05-25 15:10:35 +0800 | [diff] [blame] | 657 | target_path = os.path.join(gettempdir(), target_name) |
| 658 | # Intentionally ignoring dotfiles in EVENT_LOG_DIR. |
Andy Cheng | 0465d13 | 2013-03-20 12:12:06 +0800 | [diff] [blame] | 659 | tar_cmd = 'cd %s ; tar cJf %s *' % (event_log.EVENT_LOG_DIR, target_path) |
Tammo Spalink | 86a61c6 | 2012-05-25 15:10:35 +0800 | [diff] [blame] | 660 | tar_cmd += ' --add-file %s' % FACTORY_LOG_PATH |
Jon Salz | 6526643 | 2012-07-30 19:02:49 +0800 | [diff] [blame] | 661 | if options.add_file: |
| 662 | for f in options.add_file: |
| 663 | # Require absolute paths since the tar command may change the |
| 664 | # directory. |
| 665 | if not f.startswith('/'): |
| 666 | raise Error('Not an absolute path: %s' % f) |
| 667 | if not os.path.exists(f): |
| 668 | raise Error('File does not exist: %s' % f) |
| 669 | tar_cmd += ' --add-file %s' % pipes.quote(f) |
Tammo Spalink | 86a61c6 | 2012-05-25 15:10:35 +0800 | [diff] [blame] | 670 | cmd_result = Shell(tar_cmd) |
Jon Salz | ff88c02 | 2012-11-03 12:19:58 +0800 | [diff] [blame] | 671 | |
| 672 | if ((cmd_result.status == 1) and |
| 673 | all((x == '' or |
| 674 | 'file changed as we read it' in x or |
| 675 | "Removing leading `/' from member names" in x) |
| 676 | for x in cmd_result.stderr.split('\n'))): |
| 677 | # That's OK. Make sure it's valid though. |
Vic Yang | 85199e7 | 2013-01-28 14:33:11 +0800 | [diff] [blame] | 678 | Spawn(['tar', 'tfJ', target_path], check_call=True, log=True, |
Jon Salz | ff88c02 | 2012-11-03 12:19:58 +0800 | [diff] [blame] | 679 | ignore_stdout=True) |
| 680 | elif not cmd_result.success: |
Tammo Spalink | 86a61c6 | 2012-05-25 15:10:35 +0800 | [diff] [blame] | 681 | raise Error('unable to tar event logs, cmd %r failed, stderr: %r' % |
| 682 | (tar_cmd, cmd_result.stderr)) |
Jon Salz | ff88c02 | 2012-11-03 12:19:58 +0800 | [diff] [blame] | 683 | |
Tammo Spalink | 86a61c6 | 2012-05-25 15:10:35 +0800 | [diff] [blame] | 684 | if options.upload_method is None or options.upload_method == 'none': |
| 685 | logging.warning('REPORT UPLOAD SKIPPED (report left at %s)', target_path) |
| 686 | return |
| 687 | method, param = options.upload_method.split(':', 1) |
| 688 | if method == 'shopfloor': |
| 689 | report_upload.ShopFloorUpload(target_path, param) |
| 690 | elif method == 'ftp': |
Jay Kim | 360c1dd | 2012-06-25 10:58:11 -0700 | [diff] [blame] | 691 | report_upload.FtpUpload(target_path, 'ftp:' + param) |
Tammo Spalink | 86a61c6 | 2012-05-25 15:10:35 +0800 | [diff] [blame] | 692 | elif method == 'ftps': |
| 693 | report_upload.CurlUrlUpload(target_path, '--ftp-ssl-reqd ftp:%s' % param) |
| 694 | elif method == 'cpfe': |
Shawn Nematbakhsh | 3404a09 | 2013-01-28 16:49:09 -0800 | [diff] [blame] | 695 | report_upload.CpfeUpload(target_path, pipes.quote(param)) |
Tammo Spalink | 86a61c6 | 2012-05-25 15:10:35 +0800 | [diff] [blame] | 696 | else: |
| 697 | raise Error('unknown report upload method %r', method) |
Tammo Spalink | 9a96b8a | 2012-04-03 11:10:41 +0800 | [diff] [blame] | 698 | |
| 699 | |
| 700 | @Command('finalize', |
Hung-Te Lin | 6d82754 | 2012-07-19 11:50:41 +0800 | [diff] [blame] | 701 | CmdArg('--no_write_protect', action='store_true', |
| 702 | help='Do not enable firmware write protection.'), |
Tammo Spalink | 9a96b8a | 2012-04-03 11:10:41 +0800 | [diff] [blame] | 703 | CmdArg('--fast', action='store_true', |
| 704 | help='use non-secure but faster wipe method.'), |
| 705 | _hwdb_path_cmd_arg, |
Tammo Spalink | 95c4373 | 2012-07-25 15:57:14 -0700 | [diff] [blame] | 706 | _hwid_status_list_cmd_arg, |
Jon Salz | 6526643 | 2012-07-30 19:02:49 +0800 | [diff] [blame] | 707 | _upload_method_cmd_arg, |
Jon Salz | ce124fb | 2012-10-02 17:42:03 +0800 | [diff] [blame] | 708 | _add_file_cmd_arg, |
Ricky Liang | a70a120 | 2013-03-15 15:03:17 +0800 | [diff] [blame] | 709 | _board_cmd_arg, |
Jon Salz | ce124fb | 2012-10-02 17:42:03 +0800 | [diff] [blame] | 710 | _probe_results_cmd_arg, |
| 711 | _hwid_cmd_arg) |
Tammo Spalink | 9a96b8a | 2012-04-03 11:10:41 +0800 | [diff] [blame] | 712 | def Finalize(options): |
| 713 | """Verify system readiness and trigger transition into release state. |
| 714 | |
Jon Salz | aa3a30e | 2013-05-15 15:56:28 +0800 | [diff] [blame] | 715 | This routine does the following: |
| 716 | - Verifies system state (see verify command) |
| 717 | - Modifies firmware bitmaps to match locale |
| 718 | - Clears all factory-friendly flags from the GBB |
| 719 | - Removes factory-specific entries from RW_VPD (factory.*) |
| 720 | - Enables firmware write protection (cannot rollback after this) |
| 721 | - Uploads system logs & reports |
| 722 | - Sets the necessary boot flags to cause wipe of the factory image on the |
| 723 | next boot. |
Tammo Spalink | 9a96b8a | 2012-04-03 11:10:41 +0800 | [diff] [blame] | 724 | """ |
Tammo Spalink | 9a96b8a | 2012-04-03 11:10:41 +0800 | [diff] [blame] | 725 | Verify(options) |
Ricky Liang | a70a120 | 2013-03-15 15:03:17 +0800 | [diff] [blame] | 726 | SetFirmwareBitmapLocale(options) |
| 727 | ClearGBBFlags(options) |
Jon Salz | aa3a30e | 2013-05-15 15:56:28 +0800 | [diff] [blame] | 728 | ClearFactoryVPDEntries(options) |
Hung-Te Lin | 6d82754 | 2012-07-19 11:50:41 +0800 | [diff] [blame] | 729 | if options.no_write_protect: |
| 730 | logging.warn('WARNING: Firmware Write Protection is SKIPPED.') |
Andy Cheng | 0465d13 | 2013-03-20 12:12:06 +0800 | [diff] [blame] | 731 | event_log.Log('wp', fw='both', status='skipped') |
Hung-Te Lin | 6d82754 | 2012-07-19 11:50:41 +0800 | [diff] [blame] | 732 | else: |
Tammo Spalink | 9a96b8a | 2012-04-03 11:10:41 +0800 | [diff] [blame] | 733 | EnableFwWp({}) |
Jon Salz | a0f58e0 | 2012-05-29 19:33:39 +0800 | [diff] [blame] | 734 | LogSystemDetails(options) |
Tammo Spalink | 9a96b8a | 2012-04-03 11:10:41 +0800 | [diff] [blame] | 735 | UploadReport(options) |
| 736 | PrepareWipe(options) |
| 737 | |
| 738 | |
Ricky Liang | 5339023 | 2013-03-08 15:37:57 +0800 | [diff] [blame] | 739 | @Command('verify_components_v3', |
| 740 | _board_cmd_arg, |
| 741 | _hwdb_path_cmd_arg, |
| 742 | CmdArg('target_comps', nargs='*')) |
| 743 | def VerifyComponentsV3(options): |
| 744 | """Verify that probeable components all match entries in the component_db. |
| 745 | |
| 746 | This method uses the HWIDv3 component database to verify components. |
| 747 | |
| 748 | Probe for each component class in the target_comps and verify |
| 749 | that a corresponding match exists in the component_db -- make sure |
| 750 | that these components are present, that they have been approved, but |
| 751 | do not check against any specific BOM/HWID configurations. |
| 752 | """ |
| 753 | |
Ricky Liang | a70a120 | 2013-03-15 15:03:17 +0800 | [diff] [blame] | 754 | result = GetGooftool(options).VerifyComponentsV3(options.target_comps) |
Ricky Liang | 5339023 | 2013-03-08 15:37:57 +0800 | [diff] [blame] | 755 | PrintVerifyComponentsResults(result) |
| 756 | |
| 757 | |
| 758 | @Command('generate_hwid_v3', |
| 759 | _board_cmd_arg, |
| 760 | _hwdb_path_cmd_arg, |
Ricky Liang | 7905f27 | 2013-03-16 01:57:10 +0800 | [diff] [blame] | 761 | _probe_results_cmd_arg, |
Ricky Liang | 5339023 | 2013-03-08 15:37:57 +0800 | [diff] [blame] | 762 | _device_info_cmd_arg) |
| 763 | def GenerateHwidV3(options): |
| 764 | """Generates the HWID of the DUT. |
| 765 | |
| 766 | The HWID is generated based on the given device info and the probe results |
| 767 | retrieved by probing the DUT. If there are conflits of component information |
| 768 | between device info and probe result, priority is given to device info. |
| 769 | """ |
| 770 | try: |
Ricky Liang | 7905f27 | 2013-03-16 01:57:10 +0800 | [diff] [blame] | 771 | with open(options.device_info) as f: |
| 772 | device_info = yaml.load(f.read()) |
Ricky Liang | 5339023 | 2013-03-08 15:37:57 +0800 | [diff] [blame] | 773 | except Exception, e: |
| 774 | raise Error, 'Invalid device_info: %s' % e |
Ricky Liang | 7905f27 | 2013-03-16 01:57:10 +0800 | [diff] [blame] | 775 | if options.probe_results: |
| 776 | with open(options.probe_results) as f: |
| 777 | probe_results = hwid_tool.ProbeResults.Decode(f.read()) |
| 778 | else: |
| 779 | probe_results = Probe() |
Ricky Liang | 5339023 | 2013-03-08 15:37:57 +0800 | [diff] [blame] | 780 | print 'device_info:' |
| 781 | print device_info |
Ricky Liang | 5b4568d | 2013-04-23 17:15:23 +0800 | [diff] [blame] | 782 | print 'probe results:' |
Ricky Liang | 5339023 | 2013-03-08 15:37:57 +0800 | [diff] [blame] | 783 | print probe_results.Encode() |
| 784 | |
| 785 | # Do not log device_info for now until we're sure that it does not contain |
| 786 | # any sensitive infomation. |
| 787 | # TODO(jcliang): Add logging for device_info when appropriate. |
| 788 | |
Andy Cheng | 0465d13 | 2013-03-20 12:12:06 +0800 | [diff] [blame] | 789 | event_log.Log( |
Ricky Liang | 5339023 | 2013-03-08 15:37:57 +0800 | [diff] [blame] | 790 | 'probe', |
| 791 | found_components=probe_results.found_probe_value_map, |
| 792 | missing_component_classes=probe_results.missing_component_classes, |
| 793 | volatiles=probe_results.found_volatile_values, |
| 794 | initial_configs=probe_results.initial_configs) |
| 795 | |
Ricky Liang | a70a120 | 2013-03-15 15:03:17 +0800 | [diff] [blame] | 796 | hwid_object = GetGooftool(options).GenerateHwidV3( |
Ricky Liang | ab9d0b8 | 2013-03-19 02:15:32 +0800 | [diff] [blame] | 797 | device_info, probe_results) |
Ricky Liang | 5339023 | 2013-03-08 15:37:57 +0800 | [diff] [blame] | 798 | |
| 799 | final_bom = {} |
| 800 | for component_class, component_values in ( |
| 801 | hwid_object.bom.components.iteritems()): |
Ricky Liang | 5b4568d | 2013-04-23 17:15:23 +0800 | [diff] [blame] | 802 | final_bom[component_class] = [v.probed_values for v in component_values] |
Andy Cheng | 0465d13 | 2013-03-20 12:12:06 +0800 | [diff] [blame] | 803 | event_log.Log( |
Ricky Liang | 5339023 | 2013-03-08 15:37:57 +0800 | [diff] [blame] | 804 | 'final_bom', |
| 805 | final_bom=final_bom) |
Andy Cheng | 0465d13 | 2013-03-20 12:12:06 +0800 | [diff] [blame] | 806 | event_log.Log( |
Ricky Liang | 5339023 | 2013-03-08 15:37:57 +0800 | [diff] [blame] | 807 | 'generated_hwid', |
| 808 | encoded_string=hwid_object.encoded_string, |
| 809 | binary_string=hwid_object.binary_string) |
| 810 | print 'Encoded HWID string:', hwid_object.encoded_string |
| 811 | print 'Binary HWID string:', hwid_object.binary_string |
| 812 | |
| 813 | |
| 814 | @Command('verify_hwid_v3', |
| 815 | _board_cmd_arg, |
| 816 | _hwdb_path_cmd_arg, |
Ricky Liang | 7905f27 | 2013-03-16 01:57:10 +0800 | [diff] [blame] | 817 | _probe_results_cmd_arg, |
Ricky Liang | 5339023 | 2013-03-08 15:37:57 +0800 | [diff] [blame] | 818 | _hwid_cmd_arg) |
| 819 | def VerifyHwidV3(options): |
| 820 | """Verify system HWID properties match probed device properties. |
| 821 | |
| 822 | First probe components, volatile and initial_config parameters for |
| 823 | the DUT. Then use the available device data to produce a list of |
| 824 | candidate HWIDs. Then verify the HWID from the DUT is present in |
| 825 | that list. Then verify that the DUT initial config values match |
| 826 | those specified for its HWID. Finally, verify that VPD contains all |
| 827 | the necessary fields as specified by the board data, and when |
| 828 | possible verify that values are legitimate. |
| 829 | """ |
Ricky Liang | 7905f27 | 2013-03-16 01:57:10 +0800 | [diff] [blame] | 830 | if not options.probe_results: |
| 831 | main_fw_file = crosfw.LoadMainFirmware().GetFileName() |
Ricky Liang | 5339023 | 2013-03-08 15:37:57 +0800 | [diff] [blame] | 832 | if options.hwid: |
| 833 | hwid_str = options.hwid |
| 834 | else: |
| 835 | gbb_result = Shell('gbb_utility -g --hwid %s' % main_fw_file).stdout |
| 836 | hwid_str = re.findall(r'hardware_id:(.*)', gbb_result)[0].strip() |
| 837 | print 'Verifying HWID: %r\n' % hwid_str |
Ricky Liang | 7905f27 | 2013-03-16 01:57:10 +0800 | [diff] [blame] | 838 | if options.probe_results: |
| 839 | # Pull in probe results (including VPD data) from the given file |
| 840 | # rather than probing the current system. |
| 841 | with open(options.probe_results) as f: |
| 842 | probe_results = hwid_tool.ProbeResults.Decode(f.read()) |
| 843 | probed_ro_vpd = {} |
| 844 | probed_rw_vpd = {} |
| 845 | for k, v in probe_results.found_volatile_values.items(): |
| 846 | # Use items(), not iteritems(), since we will be modifying the dict in the |
| 847 | # loop. |
| 848 | match = re.match('^vpd\.(ro|rw)\.(\w+)$', k) |
| 849 | if match: |
| 850 | del probe_results.found_volatile_values[k] |
| 851 | (probed_ro_vpd if match.group(1) == 'ro' |
| 852 | else probed_rw_vpd)[match.group(2)] = v |
| 853 | else: |
| 854 | probe_results = Probe() |
| 855 | probed_ro_vpd = ReadRoVpd(main_fw_file) |
| 856 | probed_rw_vpd = ReadRwVpd(main_fw_file) |
Ricky Liang | 5339023 | 2013-03-08 15:37:57 +0800 | [diff] [blame] | 857 | print 'probe result:' |
| 858 | print probe_results.Encode() |
Andy Cheng | 0465d13 | 2013-03-20 12:12:06 +0800 | [diff] [blame] | 859 | event_log.Log( |
Ricky Liang | 5339023 | 2013-03-08 15:37:57 +0800 | [diff] [blame] | 860 | 'probe', |
| 861 | found_components=probe_results.found_probe_value_map, |
| 862 | missing_component_classes=probe_results.missing_component_classes, |
| 863 | volatiles=probe_results.found_volatile_values, |
| 864 | initial_configs=probe_results.initial_configs) |
Andy Cheng | 0465d13 | 2013-03-20 12:12:06 +0800 | [diff] [blame] | 865 | event_log.Log('vpd', probed_ro_vpd=FilterDict(probed_ro_vpd), |
Ricky Liang | 5339023 | 2013-03-08 15:37:57 +0800 | [diff] [blame] | 866 | probed_rw_vpd=FilterDict(probed_rw_vpd)) |
| 867 | |
Ricky Liang | a70a120 | 2013-03-15 15:03:17 +0800 | [diff] [blame] | 868 | GetGooftool(options).VerifyHwidV3( |
Ricky Liang | ab9d0b8 | 2013-03-19 02:15:32 +0800 | [diff] [blame] | 869 | hwid_str, probe_results, probed_ro_vpd, probed_rw_vpd) |
Ricky Liang | 5339023 | 2013-03-08 15:37:57 +0800 | [diff] [blame] | 870 | |
Andy Cheng | 0465d13 | 2013-03-20 12:12:06 +0800 | [diff] [blame] | 871 | event_log.Log('verified_hwid', hwid=hwid_str) |
Ricky Liang | 5339023 | 2013-03-08 15:37:57 +0800 | [diff] [blame] | 872 | print 'Verification SUCCESS!' |
| 873 | |
| 874 | |
Ricky Liang | 7905f27 | 2013-03-16 01:57:10 +0800 | [diff] [blame] | 875 | @Command('decode_hwid_v3', |
| 876 | _board_cmd_arg, |
| 877 | _hwdb_path_cmd_arg, |
| 878 | _hwid_cmd_arg) |
| 879 | def DecodeHwidV3(options): |
| 880 | """Decodes the given v3 HWID and prints out decoded information. |
| 881 | |
| 882 | If no HWID is given, the HWID stored on the device will be loaded and used |
| 883 | instead. |
| 884 | """ |
| 885 | decoded_hwid_context = Gooftool(hwid_version=3, board=options.board, |
| 886 | hwdb_path=options.hwdb_path).DecodeHwidV3( |
| 887 | options.hwid) |
| 888 | |
Ricky Liang | 5b4568d | 2013-04-23 17:15:23 +0800 | [diff] [blame] | 889 | results = {} |
| 890 | results['board'] = decoded_hwid_context.database.board |
| 891 | results['binary_string'] = decoded_hwid_context.binary_string |
| 892 | results['components'] = collections.defaultdict(list) |
Ricky Liang | 7905f27 | 2013-03-16 01:57:10 +0800 | [diff] [blame] | 893 | components = decoded_hwid_context.bom.components |
| 894 | for comp_cls in sorted(components): |
Ricky Liang | 5b4568d | 2013-04-23 17:15:23 +0800 | [diff] [blame] | 895 | for (comp_name, probed_values, _) in sorted(components[comp_cls]): |
| 896 | if not probed_values: |
Ricky Liang | 7905f27 | 2013-03-16 01:57:10 +0800 | [diff] [blame] | 897 | db_components = decoded_hwid_context.database.components |
Ricky Liang | 5b4568d | 2013-04-23 17:15:23 +0800 | [diff] [blame] | 898 | probed_values = db_components.GetComponentAttributes( |
| 899 | comp_cls, comp_name)['values'] |
| 900 | results['components'][comp_cls].append( |
| 901 | {comp_name: probed_values if probed_values else None}) |
| 902 | # Convert defaultdict to dict. |
| 903 | results['components'] = dict(results['components']) |
| 904 | print yaml.dump(results, default_flow_style=False) |
Ricky Liang | 7905f27 | 2013-03-16 01:57:10 +0800 | [diff] [blame] | 905 | |
| 906 | |
Tammo Spalink | 9a96b8a | 2012-04-03 11:10:41 +0800 | [diff] [blame] | 907 | def Main(): |
| 908 | """Run sub-command specified by the command line args.""" |
Andy Cheng | 7a76cb8 | 2012-11-19 18:08:19 +0800 | [diff] [blame] | 909 | |
Tammo Spalink | 9a96b8a | 2012-04-03 11:10:41 +0800 | [diff] [blame] | 910 | options = ParseCmdline( |
| 911 | 'Perform Google required factory tests.', |
| 912 | CmdArg('-l', '--log', metavar='PATH', |
| 913 | help='Write logs to this file.'), |
Jon Salz | a4bea38 | 2012-10-29 13:00:34 +0800 | [diff] [blame] | 914 | CmdArg('--suppress-event-logs', action='store_true', |
| 915 | help='Suppress event logging.'), |
Jon Salz | fb61b1d | 2013-05-15 16:04:13 +0800 | [diff] [blame] | 916 | CmdArg('-i', '--hwid-version', default=3, choices=[2, 3], type=int, |
Ricky Liang | a70a120 | 2013-03-15 15:03:17 +0800 | [diff] [blame] | 917 | help='Version of HWID to operate on.'), |
Tammo Spalink | 8fab531 | 2012-05-28 18:33:30 +0800 | [diff] [blame] | 918 | verbosity_cmd_arg) |
Tammo Spalink | 9a96b8a | 2012-04-03 11:10:41 +0800 | [diff] [blame] | 919 | SetupLogging(options.verbosity, options.log) |
Andy Cheng | 0465d13 | 2013-03-20 12:12:06 +0800 | [diff] [blame] | 920 | event_log.SetGlobalLoggerDefaultPrefix('gooftool') |
| 921 | event_log.GetGlobalLogger().suppress = options.suppress_event_logs |
Tammo Spalink | 9a96b8a | 2012-04-03 11:10:41 +0800 | [diff] [blame] | 922 | logging.debug('gooftool options: %s', repr(options)) |
| 923 | try: |
| 924 | logging.debug('GOOFTOOL command %r', options.command_name) |
| 925 | options.command(options) |
| 926 | logging.info('GOOFTOOL command %r SUCCESS', options.command_name) |
| 927 | except Error, e: |
| 928 | logging.exception(e) |
| 929 | sys.exit('GOOFTOOL command %r ERROR: %s' % (options.command_name, e)) |
| 930 | except Exception, e: |
| 931 | logging.exception(e) |
| 932 | sys.exit('UNCAUGHT RUNTIME EXCEPTION %s' % e) |
| 933 | |
| 934 | |
| 935 | if __name__ == '__main__': |
| 936 | Main() |