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