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