Tammo Spalink | 9a96b8a | 2012-04-03 11:10:41 +0800 | [diff] [blame] | 1 | #!/usr/bin/python |
Jon Salz | e60307f | 2014-08-05 16:20:00 +0800 | [diff] [blame] | 2 | # -*- coding: utf-8 -*- |
| 3 | # Copyright 2014 The Chromium OS Authors. All rights reserved. |
Tammo Spalink | 9a96b8a | 2012-04-03 11:10:41 +0800 | [diff] [blame] | 4 | # Use of this source code is governed by a BSD-style license that can be |
| 5 | # found in the LICENSE file. |
| 6 | |
Jon Salz | e60307f | 2014-08-05 16:20:00 +0800 | [diff] [blame] | 7 | # pylint: disable=E1101 |
| 8 | |
Tammo Spalink | 9a96b8a | 2012-04-03 11:10:41 +0800 | [diff] [blame] | 9 | """Google Factory Tool. |
| 10 | |
| 11 | This tool is indended to be used on factory assembly lines. It |
| 12 | provides all of the Google required test functionality and must be run |
| 13 | on each device as part of the assembly process. |
| 14 | """ |
| 15 | |
Ricky Liang | 5b4568d | 2013-04-23 17:15:23 +0800 | [diff] [blame] | 16 | import collections |
Tammo Spalink | 9a96b8a | 2012-04-03 11:10:41 +0800 | [diff] [blame] | 17 | import logging |
| 18 | import os |
Jon Salz | 6526643 | 2012-07-30 19:02:49 +0800 | [diff] [blame] | 19 | import pipes |
Tammo Spalink | 9a96b8a | 2012-04-03 11:10:41 +0800 | [diff] [blame] | 20 | import re |
| 21 | import sys |
Cheng-Yi Chiang | 9fc121c | 2014-01-27 11:23:22 +0800 | [diff] [blame] | 22 | import threading |
Hung-Te Lin | 6bd1647 | 2012-06-20 16:26:47 +0800 | [diff] [blame] | 23 | import time |
Jon Salz | a88b83b | 2013-05-27 20:00:35 +0800 | [diff] [blame] | 24 | import xmlrpclib |
Ricky Liang | 7905f27 | 2013-03-16 01:57:10 +0800 | [diff] [blame] | 25 | import yaml |
Tammo Spalink | 9a96b8a | 2012-04-03 11:10:41 +0800 | [diff] [blame] | 26 | |
Andy Cheng | 2582d29 | 2012-12-04 17:38:28 +0800 | [diff] [blame] | 27 | from tempfile import gettempdir |
Tammo Spalink | 86a61c6 | 2012-05-25 15:10:35 +0800 | [diff] [blame] | 28 | |
Tammo Spalink | a40293e | 2012-07-04 14:58:56 +0800 | [diff] [blame] | 29 | import factory_common # pylint: disable=W0611 |
Tammo Spalink | 9a96b8a | 2012-04-03 11:10:41 +0800 | [diff] [blame] | 30 | |
Ricky Liang | 5339023 | 2013-03-08 15:37:57 +0800 | [diff] [blame] | 31 | from cros.factory.gooftool import crosfw |
Tammo Spalink | 01e1172 | 2012-07-24 10:17:54 -0700 | [diff] [blame] | 32 | from cros.factory.gooftool import report_upload |
Hung-Te Lin | 84c77fa | 2016-03-02 17:43:30 +0800 | [diff] [blame] | 33 | from cros.factory.gooftool.core import Gooftool |
Wei-Han Chen | 0a3320e | 2016-04-23 01:32:07 +0800 | [diff] [blame] | 34 | from cros.factory.gooftool.common import ExecFactoryPar |
Hung-Te Lin | 0e0f936 | 2015-11-18 18:18:05 +0800 | [diff] [blame] | 35 | from cros.factory.gooftool.common import Shell |
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 |
Hung-Te Lin | 604e0c2 | 2015-11-24 15:17:07 +0800 | [diff] [blame] | 40 | from cros.factory.hwid.v2 import hwid_tool |
| 41 | from cros.factory.hwid.v2.yaml_datastore import YamlWrite |
| 42 | from cros.factory.hwid.v3 import common |
| 43 | from cros.factory.hwid.v3 import hwid_utils |
Hung-Te Lin | 91492a1 | 2014-11-25 18:56:30 +0800 | [diff] [blame] | 44 | from cros.factory.test import event_log |
Wei-Han Chen | 2ebb92d | 2016-01-12 14:51:41 +0800 | [diff] [blame] | 45 | from cros.factory.test.env import paths |
Wei-Han Chen | aff5623 | 2016-04-16 09:17:59 +0800 | [diff] [blame] | 46 | from cros.factory.test.rules import phase |
Hung-Te Lin | 3f09684 | 2016-01-13 17:37:06 +0800 | [diff] [blame] | 47 | from cros.factory.test.rules.privacy import FilterDict |
Hung-Te Lin | 03bf7ab | 2016-06-16 17:26:19 +0800 | [diff] [blame] | 48 | from cros.factory.utils.argparse_utils import CmdArg |
Hung-Te Lin | 03bf7ab | 2016-06-16 17:26:19 +0800 | [diff] [blame] | 49 | from cros.factory.utils.argparse_utils import ParseCmdline |
| 50 | from cros.factory.utils.argparse_utils import verbosity_cmd_arg |
Ting Shen | 18a0638 | 2016-08-30 16:18:21 +0800 | [diff] [blame^] | 51 | from cros.factory.utils import argparse_utils |
Jon Salz | 40b9f82 | 2014-07-25 16:39:55 +0800 | [diff] [blame] | 52 | from cros.factory.utils import file_utils |
Wei-Han Chen | a5c01a0 | 2016-04-23 19:27:19 +0800 | [diff] [blame] | 53 | from cros.factory.utils import sys_utils |
Chun-Ta Lin | 53cbbd5 | 2016-06-08 21:42:19 +0800 | [diff] [blame] | 54 | from cros.factory.utils import time_utils |
Hung-Te Lin | 0e0f936 | 2015-11-18 18:18:05 +0800 | [diff] [blame] | 55 | from cros.factory.utils.debug_utils import SetupLogging |
Jon Salz | ff88c02 | 2012-11-03 12:19:58 +0800 | [diff] [blame] | 56 | from cros.factory.utils.process_utils import Spawn |
Joel Kitching | d3bc266 | 2014-12-16 16:03:32 -0800 | [diff] [blame] | 57 | from cros.factory.utils.type_utils import Error |
Tammo Spalink | 86a61c6 | 2012-05-25 15:10:35 +0800 | [diff] [blame] | 58 | |
Tammo Spalink | 5c69983 | 2012-07-03 17:50:39 +0800 | [diff] [blame] | 59 | |
Tammo Spalink | 5c69983 | 2012-07-03 17:50:39 +0800 | [diff] [blame] | 60 | # TODO(tammo): Replace calls to sys.exit with raise Exit, and maybe |
| 61 | # treat that specially (as a smoot exit, as opposed to the more |
| 62 | # verbose output for generic Error). |
| 63 | |
Cheng-Yi Chiang | 9fc121c | 2014-01-27 11:23:22 +0800 | [diff] [blame] | 64 | _global_gooftool = None |
| 65 | _gooftool_lock = threading.Lock() |
Tammo Spalink | 5c69983 | 2012-07-03 17:50:39 +0800 | [diff] [blame] | 66 | |
Hung-Te Lin | 56b1840 | 2015-01-16 14:52:30 +0800 | [diff] [blame] | 67 | |
Ricky Liang | a70a120 | 2013-03-15 15:03:17 +0800 | [diff] [blame] | 68 | def GetGooftool(options): |
Cheng-Yi Chiang | 9fc121c | 2014-01-27 11:23:22 +0800 | [diff] [blame] | 69 | global _global_gooftool # pylint: disable=W0603 |
Ricky Liang | a70a120 | 2013-03-15 15:03:17 +0800 | [diff] [blame] | 70 | |
Cheng-Yi Chiang | 9fc121c | 2014-01-27 11:23:22 +0800 | [diff] [blame] | 71 | if _global_gooftool is None: |
| 72 | with _gooftool_lock: |
Ricky Liang | 43b879b | 2014-02-24 11:36:55 +0800 | [diff] [blame] | 73 | hwid_version = getattr(options, 'hwid_version', 3) |
| 74 | if hwid_version == 2: |
Cheng-Yi Chiang | 9fc121c | 2014-01-27 11:23:22 +0800 | [diff] [blame] | 75 | hwdb_path = getattr(options, 'hwdb_path', None) |
| 76 | component_db = ( |
| 77 | hwid_tool.HardwareDb(options.hwdb_path).comp_db if hwdb_path |
| 78 | else None) |
| 79 | _global_gooftool = Gooftool(hwid_version=2, component_db=component_db) |
Ricky Liang | 43b879b | 2014-02-24 11:36:55 +0800 | [diff] [blame] | 80 | elif hwid_version == 3: |
Cheng-Yi Chiang | 9fc121c | 2014-01-27 11:23:22 +0800 | [diff] [blame] | 81 | board = getattr(options, 'board', None) |
| 82 | hwdb_path = getattr(options, 'hwdb_path', None) |
| 83 | _global_gooftool = Gooftool(hwid_version=3, board=board, |
| 84 | hwdb_path=hwdb_path) |
| 85 | else: |
| 86 | raise Error, 'Invalid HWID version: %r' % options.hwid_version |
| 87 | |
| 88 | return _global_gooftool |
Ricky Liang | a70a120 | 2013-03-15 15:03:17 +0800 | [diff] [blame] | 89 | |
Hung-Te Lin | 56b1840 | 2015-01-16 14:52:30 +0800 | [diff] [blame] | 90 | |
Ting Shen | 18a0638 | 2016-08-30 16:18:21 +0800 | [diff] [blame^] | 91 | def Command(cmd_name, *args, **kwargs): |
| 92 | """ Decorator for commands in gooftool. |
| 93 | |
| 94 | This is similar to argparse_utils.Command, but all gooftool commands |
| 95 | can be waived during `gooftool finalize` or `gooftool verify` using |
| 96 | --waive_list option. |
| 97 | """ |
| 98 | def Decorate(fun): |
| 99 | def CommandWithWaiveCheck(options): |
| 100 | waive_list = vars(options).get('waive_list', []) |
| 101 | if phase.GetPhase() >= phase.PVT_DOGFOOD and waive_list != []: |
| 102 | raise Error( |
| 103 | 'waive_list should be empty for phase %s' % phase.GetPhase()) |
| 104 | |
| 105 | try: |
| 106 | fun(options) |
| 107 | except Exception as e: |
| 108 | if cmd_name in waive_list: |
| 109 | logging.exception(e) |
| 110 | else: |
| 111 | raise |
| 112 | |
| 113 | return argparse_utils.Command(cmd_name, *args, **kwargs)( |
| 114 | CommandWithWaiveCheck) |
| 115 | return Decorate |
| 116 | |
| 117 | |
Tammo Spalink | 9a96b8a | 2012-04-03 11:10:41 +0800 | [diff] [blame] | 118 | @Command('write_hwid', |
| 119 | CmdArg('hwid', metavar='HWID', help='HWID string')) |
Andy Cheng | c92e6f9 | 2012-11-20 16:55:53 +0800 | [diff] [blame] | 120 | def WriteHWID(options): |
Tammo Spalink | 9a96b8a | 2012-04-03 11:10:41 +0800 | [diff] [blame] | 121 | """Write specified HWID value into the system BB.""" |
Andy Cheng | 7a76cb8 | 2012-11-19 18:08:19 +0800 | [diff] [blame] | 122 | |
Tammo Spalink | 95c4373 | 2012-07-25 15:57:14 -0700 | [diff] [blame] | 123 | logging.info('writing hwid string %r', options.hwid) |
Ricky Liang | a70a120 | 2013-03-15 15:03:17 +0800 | [diff] [blame] | 124 | GetGooftool(options).WriteHWID(options.hwid) |
Andy Cheng | 0465d13 | 2013-03-20 12:12:06 +0800 | [diff] [blame] | 125 | event_log.Log('write_hwid', hwid=options.hwid) |
Tammo Spalink | 95c4373 | 2012-07-25 15:57:14 -0700 | [diff] [blame] | 126 | print 'Wrote HWID: %r' % options.hwid |
Tammo Spalink | 9a96b8a | 2012-04-03 11:10:41 +0800 | [diff] [blame] | 127 | |
| 128 | |
Ricky Liang | 5339023 | 2013-03-08 15:37:57 +0800 | [diff] [blame] | 129 | _board_cmd_arg = CmdArg( |
| 130 | '--board', metavar='BOARD', |
| 131 | default=None, help='Board name to test.') |
| 132 | |
Tammo Spalink | 8fab531 | 2012-05-28 18:33:30 +0800 | [diff] [blame] | 133 | _hwdb_path_cmd_arg = CmdArg( |
| 134 | '--hwdb_path', metavar='PATH', |
Ricky Liang | eede792 | 2013-06-19 10:18:41 +0800 | [diff] [blame] | 135 | default=common.DEFAULT_HWID_DATA_PATH, |
Tammo Spalink | 8fab531 | 2012-05-28 18:33:30 +0800 | [diff] [blame] | 136 | help='Path to the HWID database.') |
| 137 | |
Tammo Spalink | 95c4373 | 2012-07-25 15:57:14 -0700 | [diff] [blame] | 138 | _hwid_status_list_cmd_arg = CmdArg( |
Hung-Te Lin | 56b1840 | 2015-01-16 14:52:30 +0800 | [diff] [blame] | 139 | '--status', nargs='*', default=['supported'], |
| 140 | help='allow only HWIDs with these status values') |
Tammo Spalink | 95c4373 | 2012-07-25 15:57:14 -0700 | [diff] [blame] | 141 | |
Jon Salz | ce124fb | 2012-10-02 17:42:03 +0800 | [diff] [blame] | 142 | _probe_results_cmd_arg = CmdArg( |
| 143 | '--probe_results', metavar='RESULTS.yaml', |
Ricky Liang | c662be3 | 2013-12-24 11:50:23 +0800 | [diff] [blame] | 144 | help=('Output from "gooftool probe --include_vpd" (used instead of ' |
Jon Salz | ce124fb | 2012-10-02 17:42:03 +0800 | [diff] [blame] | 145 | 'probing this system).')) |
| 146 | |
Ricky Liang | 5339023 | 2013-03-08 15:37:57 +0800 | [diff] [blame] | 147 | _device_info_cmd_arg = CmdArg( |
Ricky Liang | f89f73a | 2013-03-19 05:00:24 +0800 | [diff] [blame] | 148 | '--device_info', metavar='DEVICE_INFO.yaml', default=None, |
Ricky Liang | 5339023 | 2013-03-08 15:37:57 +0800 | [diff] [blame] | 149 | help='A dict of device info to use instead of fetching from shopfllor ' |
| 150 | 'server.') |
| 151 | |
Jon Salz | ce124fb | 2012-10-02 17:42:03 +0800 | [diff] [blame] | 152 | _hwid_cmd_arg = CmdArg( |
| 153 | '--hwid', metavar='HWID', |
Ricky Liang | a70a120 | 2013-03-15 15:03:17 +0800 | [diff] [blame] | 154 | 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] | 155 | |
Bernie Thompson | 3c11c87 | 2013-07-22 18:22:45 -0700 | [diff] [blame] | 156 | _rma_mode_cmd_arg = CmdArg( |
| 157 | '--rma_mode', action='store_true', |
| 158 | help='Enable RMA mode, do not check for deprecated components.') |
Tammo Spalink | 95c4373 | 2012-07-25 15:57:14 -0700 | [diff] [blame] | 159 | |
Chih-Yu Huang | 714dbc4 | 2015-07-21 16:42:16 +0800 | [diff] [blame] | 160 | _cros_core_cmd_arg = CmdArg( |
| 161 | '--cros_core', action='store_true', |
| 162 | help='Finalize for ChromeOS Core devices (may add or remove few test ' |
| 163 | 'items. For example, branding verification or firmware bitmap ' |
| 164 | 'locale settings).') |
| 165 | |
Ricky Liang | 43b879b | 2014-02-24 11:36:55 +0800 | [diff] [blame] | 166 | _hwid_version_cmd_arg = CmdArg( |
| 167 | '-i', '--hwid-version', default=3, choices=(2, 3), type=int, |
| 168 | help='Version of HWID to operate on. (default: %(default)s)') |
| 169 | |
bowgotsai | 13820f4 | 2015-09-10 23:18:04 +0800 | [diff] [blame] | 170 | _enforced_release_channels_cmd_arg = CmdArg( |
| 171 | '--enforced_release_channels', nargs='*', default=None, |
| 172 | help='Enforced release image channels.') |
| 173 | |
Hung-Te Lin | cdb9652 | 2016-04-15 16:51:10 +0800 | [diff] [blame] | 174 | _release_rootfs_cmd_arg = CmdArg( |
| 175 | '--release_rootfs', help='Location of release image rootfs partition.') |
| 176 | |
| 177 | _firmware_path_cmd_arg = CmdArg( |
| 178 | '--firmware_path', help='Location of firmware image partition.') |
Ricky Liang | 43b879b | 2014-02-24 11:36:55 +0800 | [diff] [blame] | 179 | |
Wei-Han Chen | be1355a | 2016-04-24 19:31:03 +0800 | [diff] [blame] | 180 | _cutoff_args_cmd_arg = CmdArg( |
| 181 | '--cutoff_args', |
| 182 | help='Battery cutoff arguments to be passed to battery_cutoff.sh ' |
| 183 | 'after wiping. Should be the following format: ' |
| 184 | '[--method shutdown|reboot|battery_cutoff] ' |
| 185 | '[--check-ac connect_ac|remove_ac] ' |
| 186 | '[--min-battery-percent <minimum battery percentage>] ' |
| 187 | '[--max-battery-percent <maximum battery percentage>] ' |
| 188 | '[--min-battery-voltage <minimum battery voltage>] ' |
| 189 | '[--max-battery-voltage <maximum battery voltage>]') |
| 190 | |
| 191 | _shopfloor_url_args_cmd_arg = CmdArg( |
| 192 | '--shopfloor_url', |
| 193 | help='Shopfloor server url to be informed when in-place wipe is done. ' |
| 194 | 'After in-place wipe, a XML-RPC request will be sent to the ' |
| 195 | 'given url to indicate the completion of wipe.') |
| 196 | |
| 197 | _station_ip_cmd_arg = CmdArg( |
| 198 | '--station_ip', |
| 199 | help='IP of remote station') |
| 200 | |
| 201 | _station_port_cmd_arg = CmdArg( |
| 202 | '--station_port', |
| 203 | help='Port on remote station') |
| 204 | |
| 205 | _wipe_finish_token_cmd_arg = CmdArg( |
| 206 | '--wipe_finish_token', |
| 207 | help='Required token when notifying station after wipe finished') |
| 208 | |
Ting Shen | 18a0638 | 2016-08-30 16:18:21 +0800 | [diff] [blame^] | 209 | _waive_list_cmd_arg = CmdArg( |
| 210 | '--waive_list', nargs='*', default=[], metavar='SUBCMD', |
| 211 | help='A list of waived checks, serperated by whitespace.' |
| 212 | 'Each item should be a sub-command of gooftool.' |
| 213 | 'e.g. "gooftool verify --waive_list verify_tpm clear_gbb_flags".') |
| 214 | |
Tammo Spalink | 95c4373 | 2012-07-25 15:57:14 -0700 | [diff] [blame] | 215 | @Command('best_match_hwids', |
Tammo Spalink | 8fab531 | 2012-05-28 18:33:30 +0800 | [diff] [blame] | 216 | _hwdb_path_cmd_arg, |
| 217 | CmdArg('-b', '--board', metavar='BOARD', |
Tammo Spalink | 95c4373 | 2012-07-25 15:57:14 -0700 | [diff] [blame] | 218 | help='optional BOARD name, needed only if data is present ' |
| 219 | 'for more than one'), |
Tammo Spalink | 8fab531 | 2012-05-28 18:33:30 +0800 | [diff] [blame] | 220 | CmdArg('--bom', metavar='BOM', help='BOM name'), |
| 221 | CmdArg('--variant', metavar='VARIANT', help='VARIANT code'), |
Tammo Spalink | 5c69983 | 2012-07-03 17:50:39 +0800 | [diff] [blame] | 222 | CmdArg('--optimistic', action='store_true', |
| 223 | help='do not probe; assume singletons match'), |
Tammo Spalink | 95c4373 | 2012-07-25 15:57:14 -0700 | [diff] [blame] | 224 | CmdArg('--comps', nargs='*', default=[], |
| 225 | help='list of canonical component names'), |
| 226 | CmdArg('--missing', nargs='*', default=[], |
| 227 | help='list component classes to be assumed missing'), |
Tammo Spalink | 5c69983 | 2012-07-03 17:50:39 +0800 | [diff] [blame] | 228 | CmdArg('--status', nargs='*', default=['supported'], |
Tammo Spalink | 95c4373 | 2012-07-25 15:57:14 -0700 | [diff] [blame] | 229 | help='consider only HWIDs within this list of status values')) |
| 230 | def BestMatchHwids(options): |
Ricky Liang | c662be3 | 2013-12-24 11:50:23 +0800 | [diff] [blame] | 231 | """Determine a list of possible HWIDs using provided args and probing. |
Tammo Spalink | 8fab531 | 2012-05-28 18:33:30 +0800 | [diff] [blame] | 232 | |
| 233 | VOLATILE can always be determined by probing. To get a unique |
| 234 | result, VARIANT must be specified for all cases where the matching |
| 235 | BOM has more than one associated variant code, otherwise all HWID |
| 236 | variants will be returned. Both VARIANT and BOM information can |
Tammo Spalink | 95c4373 | 2012-07-25 15:57:14 -0700 | [diff] [blame] | 237 | alternatively be specified using the --stdin_comps argument, which |
| 238 | allows specifying a list of canonical names (one per line) on stdin, |
| 239 | one per line. Based on what is known from BOM and stdin_comps, |
| 240 | determine a list of components to probe for, and use those probe |
| 241 | results to resolve a list of matching HWIDs. If no boms, |
| 242 | components, or variant codes are specified, then a list of all HWIDs |
Andy Cheng | 8ece738 | 2012-08-22 16:25:42 +0800 | [diff] [blame] | 243 | that match probeable components will be returned. |
Tammo Spalink | 8fab531 | 2012-05-28 18:33:30 +0800 | [diff] [blame] | 244 | |
| 245 | Returns (on stdout): A list of HWIDs that match the available probe |
| 246 | results and argument contraints, one per line. |
Tammo Spalink | 70b48a5 | 2012-08-08 16:54:51 -0700 | [diff] [blame] | 247 | |
| 248 | Example: |
| 249 | |
| 250 | // Three ways to specify a keyboard (assuming it is a variant component) |
| 251 | gooftool best_match_hwids --missing keyboard |
| 252 | gooftool best_match_hwids --variant A or |
| 253 | gooftool best_match_hwids --comps us_kbd |
Tammo Spalink | 8fab531 | 2012-05-28 18:33:30 +0800 | [diff] [blame] | 254 | """ |
Andy Cheng | 7a76cb8 | 2012-11-19 18:08:19 +0800 | [diff] [blame] | 255 | |
Tammo Spalink | 5c69983 | 2012-07-03 17:50:39 +0800 | [diff] [blame] | 256 | map(hwid_tool.Validate.Status, options.status) |
Tammo Spalink | 95c4373 | 2012-07-25 15:57:14 -0700 | [diff] [blame] | 257 | hw_db = hwid_tool.HardwareDb(options.hwdb_path) |
Tammo Spalink | 5c69983 | 2012-07-03 17:50:39 +0800 | [diff] [blame] | 258 | comp_db = hw_db.comp_db |
Tammo Spalink | 3a7e902 | 2012-06-27 14:08:40 +0800 | [diff] [blame] | 259 | device = hw_db.GetDevice(options.board) |
Tammo Spalink | 5c69983 | 2012-07-03 17:50:39 +0800 | [diff] [blame] | 260 | component_spec = hwid_tool.ComponentSpec.New() |
Tammo Spalink | 8fab531 | 2012-05-28 18:33:30 +0800 | [diff] [blame] | 261 | if options.bom: |
Tammo Spalink | 5c69983 | 2012-07-03 17:50:39 +0800 | [diff] [blame] | 262 | device.BomExists(options.bom) |
Tammo Spalink | 01e1172 | 2012-07-24 10:17:54 -0700 | [diff] [blame] | 263 | component_spec = hwid_tool.CombineComponentSpecs( |
Hung-Te Lin | 56b1840 | 2015-01-16 14:52:30 +0800 | [diff] [blame] | 264 | component_spec, device.boms[options.bom].primary) |
Tammo Spalink | 8fab531 | 2012-05-28 18:33:30 +0800 | [diff] [blame] | 265 | if options.variant: |
Tammo Spalink | 5c69983 | 2012-07-03 17:50:39 +0800 | [diff] [blame] | 266 | device.VariantExists(options.variant) |
| 267 | variant_spec = device.variants[options.variant] |
Tammo Spalink | 01e1172 | 2012-07-24 10:17:54 -0700 | [diff] [blame] | 268 | if hwid_tool.ComponentSpecsConflict(component_spec, variant_spec): |
Tammo Spalink | 95c4373 | 2012-07-25 15:57:14 -0700 | [diff] [blame] | 269 | sys.exit('ERROR: multiple specifications for these components:\n%s' |
Tammo Spalink | 394e449 | 2012-08-01 10:20:30 -0700 | [diff] [blame] | 270 | % YamlWrite(sorted( |
| 271 | hwid_tool.ComponentSpecClasses(component_spec) & |
| 272 | hwid_tool.ComponentSpecClasses(variant_spec)))) |
Tammo Spalink | 01e1172 | 2012-07-24 10:17:54 -0700 | [diff] [blame] | 273 | component_spec = hwid_tool.CombineComponentSpecs( |
Hung-Te Lin | 56b1840 | 2015-01-16 14:52:30 +0800 | [diff] [blame] | 274 | component_spec, variant_spec) |
Tammo Spalink | 95c4373 | 2012-07-25 15:57:14 -0700 | [diff] [blame] | 275 | if options.comps or options.missing: |
| 276 | map(comp_db.CompExists, options.comps) |
| 277 | map(comp_db.CompClassExists, options.missing) |
| 278 | extra_comp_spec = comp_db.CreateComponentSpec( |
Hung-Te Lin | 56b1840 | 2015-01-16 14:52:30 +0800 | [diff] [blame] | 279 | components=options.comps, |
| 280 | missing=options.missing) |
Tammo Spalink | 95c4373 | 2012-07-25 15:57:14 -0700 | [diff] [blame] | 281 | print 'cmdline asserted components:\n%s' % extra_comp_spec.Encode() |
| 282 | if hwid_tool.ComponentSpecsConflict(component_spec, extra_comp_spec): |
| 283 | sys.exit('ERROR: multiple specifications for these components:\n%s' |
Tammo Spalink | 394e449 | 2012-08-01 10:20:30 -0700 | [diff] [blame] | 284 | % YamlWrite(sorted( |
| 285 | hwid_tool.ComponentSpecClasses(component_spec) & |
| 286 | hwid_tool.ComponentSpecClasses(extra_comp_spec)))) |
Tammo Spalink | 95c4373 | 2012-07-25 15:57:14 -0700 | [diff] [blame] | 287 | component_spec = hwid_tool.CombineComponentSpecs( |
Hung-Te Lin | 56b1840 | 2015-01-16 14:52:30 +0800 | [diff] [blame] | 288 | component_spec, extra_comp_spec) |
Tammo Spalink | 01e1172 | 2012-07-24 10:17:54 -0700 | [diff] [blame] | 289 | spec_classes = hwid_tool.ComponentSpecClasses(component_spec) |
Tammo Spalink | 95c4373 | 2012-07-25 15:57:14 -0700 | [diff] [blame] | 290 | missing_classes = set(comp_db.all_comp_classes) - spec_classes |
Tammo Spalink | 5c69983 | 2012-07-03 17:50:39 +0800 | [diff] [blame] | 291 | if missing_classes and not options.optimistic: |
Andy Cheng | 8ece738 | 2012-08-22 16:25:42 +0800 | [diff] [blame] | 292 | non_probeable_missing = missing_classes - PROBEABLE_COMPONENT_CLASSES |
| 293 | if non_probeable_missing: |
Tammo Spalink | 95c4373 | 2012-07-25 15:57:14 -0700 | [diff] [blame] | 294 | sys.exit('FAILURE: these classes are necessary, were not specified ' |
Tammo Spalink | 70b48a5 | 2012-08-08 16:54:51 -0700 | [diff] [blame] | 295 | 'as inputs, and cannot be probed for:\n%s' |
| 296 | 'This problem can often be addressed by specifying all of ' |
| 297 | 'the missing components on the command line (see the command ' |
Andy Cheng | 8ece738 | 2012-08-22 16:25:42 +0800 | [diff] [blame] | 298 | 'help).' % YamlWrite(list(non_probeable_missing))) |
Tammo Spalink | 95c4373 | 2012-07-25 15:57:14 -0700 | [diff] [blame] | 299 | print 'probing for missing classes:' |
| 300 | print YamlWrite(list(missing_classes)) |
| 301 | probe_results = Probe(target_comp_classes=list(missing_classes), |
| 302 | probe_volatile=False, probe_initial_config=False) |
| 303 | cooked_components = comp_db.MatchComponentProbeValues( |
Hung-Te Lin | 56b1840 | 2015-01-16 14:52:30 +0800 | [diff] [blame] | 304 | probe_results.found_probe_value_map) |
Tammo Spalink | 95c4373 | 2012-07-25 15:57:14 -0700 | [diff] [blame] | 305 | if cooked_components.unmatched: |
| 306 | sys.exit('ERROR: some probed components are unrecognized:\n%s' |
| 307 | % YamlWrite(cooked_components.unmatched)) |
| 308 | probed_comp_spec = comp_db.CreateComponentSpec( |
Hung-Te Lin | 56b1840 | 2015-01-16 14:52:30 +0800 | [diff] [blame] | 309 | components=cooked_components.matched, |
| 310 | missing=probe_results.missing_component_classes) |
Tammo Spalink | 95c4373 | 2012-07-25 15:57:14 -0700 | [diff] [blame] | 311 | component_spec = hwid_tool.CombineComponentSpecs( |
Hung-Te Lin | 56b1840 | 2015-01-16 14:52:30 +0800 | [diff] [blame] | 312 | component_spec, probed_comp_spec) |
Tammo Spalink | 95c4373 | 2012-07-25 15:57:14 -0700 | [diff] [blame] | 313 | print YamlWrite({'component data used for matching': { |
Hung-Te Lin | 56b1840 | 2015-01-16 14:52:30 +0800 | [diff] [blame] | 314 | 'missing component classes': component_spec.classes_missing, |
| 315 | 'found components': component_spec.components}}) |
Tammo Spalink | 95c4373 | 2012-07-25 15:57:14 -0700 | [diff] [blame] | 316 | component_data = hwid_tool.ComponentData( |
Hung-Te Lin | 56b1840 | 2015-01-16 14:52:30 +0800 | [diff] [blame] | 317 | extant_components=hwid_tool.ComponentSpecCompClassMap( |
| 318 | component_spec).keys(), |
| 319 | classes_missing=component_spec.classes_missing) |
Tammo Spalink | 95c4373 | 2012-07-25 15:57:14 -0700 | [diff] [blame] | 320 | match_tree = device.BuildMatchTree(component_data) |
| 321 | if not match_tree: |
| 322 | sys.exit('FAILURE: NO matching BOMs found') |
| 323 | print 'potential BOMs/VARIANTs:' |
| 324 | potential_variants = set() |
| 325 | potential_volatiles = set() |
| 326 | for bom_name, variant_tree in match_tree.items(): |
| 327 | print ' BOM: %-8s VARIANTS: %s' % ( |
Hung-Te Lin | 56b1840 | 2015-01-16 14:52:30 +0800 | [diff] [blame] | 328 | bom_name, ', '.join(sorted(variant_tree))) |
Tammo Spalink | 95c4373 | 2012-07-25 15:57:14 -0700 | [diff] [blame] | 329 | for variant_code in variant_tree: |
| 330 | potential_variants.add(variant_code) |
| 331 | for volatile_code in device.volatiles: |
| 332 | status = device.GetHwidStatus(bom_name, variant_code, volatile_code) |
| 333 | if status in options.status: |
| 334 | potential_volatiles.add(volatile_code) |
| 335 | print '' |
| 336 | if len(potential_variants) == 0: |
| 337 | sys.exit('FAILURE: no matching VARIANTs found') |
| 338 | if len(potential_volatiles) == 0: |
| 339 | sys.exit('FAILURE: no VOLATILEs found for potential matching BOMs/VARIANTS ' |
| 340 | '(with specified status)') |
Tammo Spalink | 394e449 | 2012-08-01 10:20:30 -0700 | [diff] [blame] | 341 | if (options.optimistic and |
| 342 | len(match_tree) == 1 and |
| 343 | len(potential_variants) == 1 and |
| 344 | len(potential_volatiles) == 1): |
| 345 | print ('MATCHING HWID: %s' % device.FmtHwid(match_tree.keys().pop(), |
| 346 | potential_variants.pop(), |
| 347 | potential_volatiles.pop())) |
Tammo Spalink | 5c69983 | 2012-07-03 17:50:39 +0800 | [diff] [blame] | 348 | return |
Tammo Spalink | 95c4373 | 2012-07-25 15:57:14 -0700 | [diff] [blame] | 349 | print ('probing VOLATILEs to resolve potential matches: %s\n' % |
| 350 | ', '.join(sorted(potential_volatiles))) |
| 351 | vol_probe_results = Probe( |
Hung-Te Lin | 56b1840 | 2015-01-16 14:52:30 +0800 | [diff] [blame] | 352 | target_comp_classes=[], |
| 353 | probe_volatile=True, |
| 354 | probe_initial_config=False) |
Tammo Spalink | 95c4373 | 2012-07-25 15:57:14 -0700 | [diff] [blame] | 355 | cooked_volatiles = device.MatchVolatileValues( |
Hung-Te Lin | 56b1840 | 2015-01-16 14:52:30 +0800 | [diff] [blame] | 356 | vol_probe_results.found_volatile_values) |
Tammo Spalink | 95c4373 | 2012-07-25 15:57:14 -0700 | [diff] [blame] | 357 | match_tree = device.BuildMatchTree( |
Hung-Te Lin | 56b1840 | 2015-01-16 14:52:30 +0800 | [diff] [blame] | 358 | component_data, cooked_volatiles.matched_tags) |
Tammo Spalink | 95c4373 | 2012-07-25 15:57:14 -0700 | [diff] [blame] | 359 | matched_hwids = device.GetMatchTreeHwids(match_tree) |
| 360 | if matched_hwids: |
| 361 | for hwid in matched_hwids: |
Ricky Liang | 02f3bd7 | 2012-09-21 14:33:01 +0800 | [diff] [blame] | 362 | if matched_hwids[hwid] in options.status: |
| 363 | print 'MATCHING HWID: %s' % hwid |
Tammo Spalink | 95c4373 | 2012-07-25 15:57:14 -0700 | [diff] [blame] | 364 | return |
| 365 | print 'exact HWID matching failed, but the following BOMs match: %s' % ( |
Hung-Te Lin | 56b1840 | 2015-01-16 14:52:30 +0800 | [diff] [blame] | 366 | ', '.join(sorted(match_tree))) |
Tammo Spalink | 95c4373 | 2012-07-25 15:57:14 -0700 | [diff] [blame] | 367 | if options.optimistic and len(match_tree) == 1: |
| 368 | bom_name = set(match_tree).pop() |
Tammo Spalink | 5c69983 | 2012-07-03 17:50:39 +0800 | [diff] [blame] | 369 | bom = device.boms[bom_name] |
Tammo Spalink | 95c4373 | 2012-07-25 15:57:14 -0700 | [diff] [blame] | 370 | variant_matches = match_tree[bom_name] |
Tammo Spalink | 5c69983 | 2012-07-03 17:50:39 +0800 | [diff] [blame] | 371 | if len(variant_matches) == 1: |
| 372 | var_code = set(variant_matches).pop() |
| 373 | elif len(bom.variants) == 1: |
| 374 | var_code = set(bom.variants).pop() |
| 375 | else: |
Tammo Spalink | 95c4373 | 2012-07-25 15:57:14 -0700 | [diff] [blame] | 376 | sys.exit('FAILURE: NO matching HWIDs found; optimistic matching failed ' |
| 377 | 'because there were too many variants to choose from for BOM %r' |
| 378 | % bom_name) |
| 379 | hwids = [device.FmtHwid(bom_name, var_code, vol_code) |
| 380 | for vol_code in device.volatiles |
| 381 | if device.GetHwidStatus(bom_name, var_code, vol_code) |
| 382 | in options.status] |
| 383 | for hwid in hwids: |
| 384 | print 'MATCHING HWID: %s' % hwid |
Tammo Spalink | 5c69983 | 2012-07-03 17:50:39 +0800 | [diff] [blame] | 385 | return |
| 386 | else: |
Tammo Spalink | 95c4373 | 2012-07-25 15:57:14 -0700 | [diff] [blame] | 387 | print ('optimistic matching not attempted because either it was ' |
| 388 | 'not requested, or because the number of BOMs was <> 1\n') |
| 389 | sys.exit('FAILURE: NO matching HWIDs found') |
Tammo Spalink | 8fab531 | 2012-05-28 18:33:30 +0800 | [diff] [blame] | 390 | |
| 391 | |
Tammo Spalink | 9a96b8a | 2012-04-03 11:10:41 +0800 | [diff] [blame] | 392 | @Command('probe', |
| 393 | CmdArg('--comps', nargs='*', |
| 394 | help='List of keys from the component_db registry.'), |
Ricky Liang | b30da67 | 2013-06-14 12:36:34 +0800 | [diff] [blame] | 395 | CmdArg('--fast_fw_probe', action='store_true', |
| 396 | help='Do a fast probe for EC and main firmware versions only. ' |
| 397 | 'This implies --no_vol and --no_ic.'), |
Tammo Spalink | 9a96b8a | 2012-04-03 11:10:41 +0800 | [diff] [blame] | 398 | CmdArg('--no_vol', action='store_true', |
| 399 | help='Do not probe volatile data.'), |
| 400 | CmdArg('--no_ic', action='store_true', |
Jon Salz | 0f8a684 | 2012-09-25 11:28:22 +0800 | [diff] [blame] | 401 | help='Do not probe initial_config data.'), |
| 402 | CmdArg('--include_vpd', action='store_true', |
| 403 | help='Include VPD data in volatiles.')) |
Tammo Spalink | 9a96b8a | 2012-04-03 11:10:41 +0800 | [diff] [blame] | 404 | def RunProbe(options): |
| 405 | """Print yaml-formatted breakdown of probed device properties.""" |
Ricky Liang | a70a120 | 2013-03-15 15:03:17 +0800 | [diff] [blame] | 406 | print GetGooftool(options).Probe( |
| 407 | target_comp_classes=options.comps, |
Ricky Liang | b30da67 | 2013-06-14 12:36:34 +0800 | [diff] [blame] | 408 | fast_fw_probe=options.fast_fw_probe, |
Ricky Liang | a70a120 | 2013-03-15 15:03:17 +0800 | [diff] [blame] | 409 | probe_volatile=not options.no_vol, |
| 410 | probe_initial_config=not options.no_ic, |
| 411 | probe_vpd=options.include_vpd).Encode() |
Ricky Liang | 5339023 | 2013-03-08 15:37:57 +0800 | [diff] [blame] | 412 | |
Tammo Spalink | 9a96b8a | 2012-04-03 11:10:41 +0800 | [diff] [blame] | 413 | |
Tammo Spalink | 214caf4 | 2012-05-28 10:45:00 +0800 | [diff] [blame] | 414 | @Command('verify_components', |
| 415 | _hwdb_path_cmd_arg, |
Tammo Spalink | 5c69983 | 2012-07-03 17:50:39 +0800 | [diff] [blame] | 416 | CmdArg('target_comps', nargs='*')) |
Tammo Spalink | 214caf4 | 2012-05-28 10:45:00 +0800 | [diff] [blame] | 417 | def VerifyComponents(options): |
Andy Cheng | 8ece738 | 2012-08-22 16:25:42 +0800 | [diff] [blame] | 418 | """Verify that probeable components all match entries in the component_db. |
Tammo Spalink | 214caf4 | 2012-05-28 10:45:00 +0800 | [diff] [blame] | 419 | |
Tammo Spalink | 5c69983 | 2012-07-03 17:50:39 +0800 | [diff] [blame] | 420 | Probe for each component class in the target_comps and verify |
Tammo Spalink | 214caf4 | 2012-05-28 10:45:00 +0800 | [diff] [blame] | 421 | that a corresponding match exists in the component_db -- make sure |
| 422 | that these components are present, that they have been approved, but |
| 423 | do not check against any specific BOM/HWID configurations. |
| 424 | """ |
Andy Cheng | 7a76cb8 | 2012-11-19 18:08:19 +0800 | [diff] [blame] | 425 | |
Andy Cheng | c531e2f | 2012-10-15 19:09:17 +0800 | [diff] [blame] | 426 | try: |
Ricky Liang | a70a120 | 2013-03-15 15:03:17 +0800 | [diff] [blame] | 427 | result = GetGooftool(options).VerifyComponents( |
Andy Cheng | c531e2f | 2012-10-15 19:09:17 +0800 | [diff] [blame] | 428 | options.target_comps) |
| 429 | except ValueError, e: |
| 430 | sys.exit(e) |
| 431 | |
Ricky Liang | 5339023 | 2013-03-08 15:37:57 +0800 | [diff] [blame] | 432 | PrintVerifyComponentsResults(result) |
| 433 | |
| 434 | |
| 435 | def PrintVerifyComponentsResults(result): |
| 436 | """Prints out the results of VerifyComponents method call. |
| 437 | |
| 438 | Groups the results into two groups: 'matches' and 'errors', and prints out |
| 439 | their values. |
| 440 | """ |
Andy Cheng | c531e2f | 2012-10-15 19:09:17 +0800 | [diff] [blame] | 441 | # group by matches and errors |
Tammo Spalink | 214caf4 | 2012-05-28 10:45:00 +0800 | [diff] [blame] | 442 | matches = [] |
Andy Cheng | c531e2f | 2012-10-15 19:09:17 +0800 | [diff] [blame] | 443 | errors = [] |
| 444 | for result_list in result.values(): |
| 445 | for component_name, _, error in result_list: |
| 446 | if component_name: |
| 447 | matches.append(component_name) |
Tammo Spalink | 214caf4 | 2012-05-28 10:45:00 +0800 | [diff] [blame] | 448 | else: |
Andy Cheng | c531e2f | 2012-10-15 19:09:17 +0800 | [diff] [blame] | 449 | errors.append(error) |
| 450 | |
Andy Cheng | 228a8c9 | 2012-08-27 10:53:57 +0800 | [diff] [blame] | 451 | if matches: |
| 452 | print 'found probeable components:\n %s' % '\n '.join(matches) |
Tammo Spalink | 214caf4 | 2012-05-28 10:45:00 +0800 | [diff] [blame] | 453 | if errors: |
Andy Cheng | 228a8c9 | 2012-08-27 10:53:57 +0800 | [diff] [blame] | 454 | print '\nerrors:\n %s' % '\n '.join(errors) |
| 455 | sys.exit('\ncomponent verification FAILURE') |
Tammo Spalink | 214caf4 | 2012-05-28 10:45:00 +0800 | [diff] [blame] | 456 | else: |
Hung-Te Lin | 56b1840 | 2015-01-16 14:52:30 +0800 | [diff] [blame] | 457 | print '\ncomponent verification SUCCESS' |
Tammo Spalink | 214caf4 | 2012-05-28 10:45:00 +0800 | [diff] [blame] | 458 | |
| 459 | |
Ricky Liang | 43b879b | 2014-02-24 11:36:55 +0800 | [diff] [blame] | 460 | @Command('verify_hwid_v2', |
Tammo Spalink | 95c4373 | 2012-07-25 15:57:14 -0700 | [diff] [blame] | 461 | _hwid_status_list_cmd_arg, |
Jon Salz | 0f8a684 | 2012-09-25 11:28:22 +0800 | [diff] [blame] | 462 | _hwdb_path_cmd_arg, |
Jon Salz | ce124fb | 2012-10-02 17:42:03 +0800 | [diff] [blame] | 463 | _probe_results_cmd_arg, |
| 464 | _hwid_cmd_arg) |
Ricky Liang | 43b879b | 2014-02-24 11:36:55 +0800 | [diff] [blame] | 465 | def VerifyHWIDv2(options): |
Tammo Spalink | 9a96b8a | 2012-04-03 11:10:41 +0800 | [diff] [blame] | 466 | """Verify system HWID properties match probed device properties. |
| 467 | |
| 468 | First probe components, volatile and initial_config parameters for |
| 469 | the DUT. Then use the available device data to produce a list of |
| 470 | candidate HWIDs. Then verify the HWID from the DUT is present in |
| 471 | that list. Then verify that the DUT initial config values match |
| 472 | those specified for its HWID. Finally, verify that VPD contains all |
| 473 | the necessary fields as specified by the board data, and when |
| 474 | possible verify that values are legitimate. |
| 475 | """ |
Ricky Liang | f7857c1 | 2012-09-17 13:34:41 +0800 | [diff] [blame] | 476 | def VerifyVpd(ro_vpd_keys, rw_vpd_keys): |
Tammo Spalink | 5c69983 | 2012-07-03 17:50:39 +0800 | [diff] [blame] | 477 | for key in ro_vpd_keys: |
| 478 | if key not in ro_vpd: |
Ricky Liang | f7857c1 | 2012-09-17 13:34:41 +0800 | [diff] [blame] | 479 | sys.exit('Missing required RO VPD field: %s' % key) |
Tammo Spalink | 01e1172 | 2012-07-24 10:17:54 -0700 | [diff] [blame] | 480 | known_valid_values = KNOWN_VPD_FIELD_DATA.get(key, None) |
Tammo Spalink | 5c69983 | 2012-07-03 17:50:39 +0800 | [diff] [blame] | 481 | value = ro_vpd[key] |
Ricky Liang | f7857c1 | 2012-09-17 13:34:41 +0800 | [diff] [blame] | 482 | if (known_valid_values is not None) and (value not in known_valid_values): |
| 483 | sys.exit('Invalid RO VPD entry : key %r, value %r' % (key, value)) |
Ricky Liang | f7857c1 | 2012-09-17 13:34:41 +0800 | [diff] [blame] | 484 | for key in rw_vpd_keys: |
| 485 | if key not in rw_vpd: |
| 486 | sys.exit('Missing required RW VPD field: %s' % key) |
| 487 | known_valid_values = KNOWN_VPD_FIELD_DATA.get(key, None) |
| 488 | value = rw_vpd[key] |
| 489 | if (known_valid_values is not None) and (value not in known_valid_values): |
| 490 | sys.exit('Invalid RW VPD entry : key %r, value %r' % (key, value)) |
Andy Cheng | 0465d13 | 2013-03-20 12:12:06 +0800 | [diff] [blame] | 491 | 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] | 492 | map(hwid_tool.Validate.Status, options.status) |
Jon Salz | 0f8a684 | 2012-09-25 11:28:22 +0800 | [diff] [blame] | 493 | |
Jon Salz | 0f8a684 | 2012-09-25 11:28:22 +0800 | [diff] [blame] | 494 | if options.hwid: |
| 495 | hwid_str = options.hwid |
| 496 | else: |
Hung-Te Lin | 74346d7 | 2016-01-07 22:16:57 +0800 | [diff] [blame] | 497 | main_fw_file = crosfw.LoadMainFirmware().GetFileName() |
Jon Salz | 0f8a684 | 2012-09-25 11:28:22 +0800 | [diff] [blame] | 498 | gbb_result = Shell('gbb_utility -g --hwid %s' % main_fw_file).stdout |
| 499 | hwid_str = re.findall(r'hardware_id:(.*)', gbb_result)[0].strip() |
Tammo Spalink | 95c4373 | 2012-07-25 15:57:14 -0700 | [diff] [blame] | 500 | hwid = hwid_tool.ParseHwid(hwid_str) |
Tammo Spalink | 5c69983 | 2012-07-03 17:50:39 +0800 | [diff] [blame] | 501 | hw_db = hwid_tool.HardwareDb(options.hwdb_path) |
Jon Salz | 0f8a684 | 2012-09-25 11:28:22 +0800 | [diff] [blame] | 502 | print 'Verifying HWID: %r\n' % hwid.hwid |
Tammo Spalink | 95c4373 | 2012-07-25 15:57:14 -0700 | [diff] [blame] | 503 | device = hw_db.GetDevice(hwid.board) |
| 504 | hwid_status = device.GetHwidStatus(hwid.bom, hwid.variant, hwid.volatile) |
| 505 | if hwid_status not in options.status: |
Tammo Spalink | 5c69983 | 2012-07-03 17:50:39 +0800 | [diff] [blame] | 506 | sys.exit('HWID status must be one of [%s], found %r' % |
Hung-Te Lin | 4f5beb1 | 2012-08-20 14:48:06 +0800 | [diff] [blame] | 507 | (', '.join(options.status), hwid_status)) |
Jon Salz | 0f8a684 | 2012-09-25 11:28:22 +0800 | [diff] [blame] | 508 | if options.probe_results: |
| 509 | # Pull in probe results (including VPD data) from the given file |
| 510 | # rather than probing the current system. |
| 511 | probe_results = hwid_tool.ProbeResults.Decode( |
| 512 | open(options.probe_results).read()) |
| 513 | ro_vpd = {} |
| 514 | rw_vpd = {} |
| 515 | for k, v in probe_results.found_volatile_values.items(): |
Hung-Te Lin | 56b1840 | 2015-01-16 14:52:30 +0800 | [diff] [blame] | 516 | match = re.match(r'^vpd\.(ro|rw)\.(\w+)$', k) |
Jon Salz | 0f8a684 | 2012-09-25 11:28:22 +0800 | [diff] [blame] | 517 | if match: |
| 518 | del probe_results.found_volatile_values[k] |
| 519 | (ro_vpd if match.group(1) == 'ro' else rw_vpd)[match.group(2)] = v |
| 520 | else: |
| 521 | probe_results = Probe() |
Hung-Te Lin | 74346d7 | 2016-01-07 22:16:57 +0800 | [diff] [blame] | 522 | ro_vpd = ReadRoVpd() |
| 523 | rw_vpd = ReadRwVpd() |
Tammo Spalink | 95c4373 | 2012-07-25 15:57:14 -0700 | [diff] [blame] | 524 | cooked_components = hw_db.comp_db.MatchComponentProbeValues( |
Hung-Te Lin | 56b1840 | 2015-01-16 14:52:30 +0800 | [diff] [blame] | 525 | probe_results.found_probe_value_map) |
Tammo Spalink | 95c4373 | 2012-07-25 15:57:14 -0700 | [diff] [blame] | 526 | cooked_volatiles = device.MatchVolatileValues( |
Hung-Te Lin | 56b1840 | 2015-01-16 14:52:30 +0800 | [diff] [blame] | 527 | probe_results.found_volatile_values) |
Tammo Spalink | 95c4373 | 2012-07-25 15:57:14 -0700 | [diff] [blame] | 528 | cooked_initial_configs = device.MatchInitialConfigValues( |
Hung-Te Lin | 56b1840 | 2015-01-16 14:52:30 +0800 | [diff] [blame] | 529 | probe_results.initial_configs) |
Tammo Spalink | 95c4373 | 2012-07-25 15:57:14 -0700 | [diff] [blame] | 530 | component_data = hwid_tool.ComponentData( |
Hung-Te Lin | 56b1840 | 2015-01-16 14:52:30 +0800 | [diff] [blame] | 531 | extant_components=cooked_components.matched, |
| 532 | classes_missing=probe_results.missing_component_classes) |
Tammo Spalink | 95c4373 | 2012-07-25 15:57:14 -0700 | [diff] [blame] | 533 | match_tree = device.BuildMatchTree( |
Hung-Te Lin | 56b1840 | 2015-01-16 14:52:30 +0800 | [diff] [blame] | 534 | component_data, cooked_volatiles.matched_tags) |
Tammo Spalink | 95c4373 | 2012-07-25 15:57:14 -0700 | [diff] [blame] | 535 | matched_hwids = device.GetMatchTreeHwids(match_tree) |
| 536 | print 'HWID status: %s\n' % hwid_status |
| 537 | print 'probed system components:' |
| 538 | print YamlWrite(cooked_components.__dict__) |
| 539 | print 'missing component classes:' |
| 540 | print YamlWrite(probe_results.missing_component_classes) |
| 541 | print 'probed volatiles:' |
| 542 | print YamlWrite(cooked_volatiles.__dict__) |
| 543 | print 'probed initial_configs:' |
| 544 | print YamlWrite(cooked_initial_configs) |
| 545 | print 'hwid match tree:' |
| 546 | print YamlWrite(match_tree) |
Andy Cheng | 0465d13 | 2013-03-20 12:12:06 +0800 | [diff] [blame] | 547 | event_log.Log( |
Hung-Te Lin | 56b1840 | 2015-01-16 14:52:30 +0800 | [diff] [blame] | 548 | 'probe', |
| 549 | found_components=cooked_components.__dict__, |
| 550 | missing_component_classes=probe_results.missing_component_classes, |
| 551 | volatiles=cooked_volatiles.__dict__, |
| 552 | initial_configs=cooked_initial_configs) |
Tammo Spalink | 95c4373 | 2012-07-25 15:57:14 -0700 | [diff] [blame] | 553 | if hwid.hwid not in matched_hwids: |
Tammo Spalink | 5c69983 | 2012-07-03 17:50:39 +0800 | [diff] [blame] | 554 | err_msg = 'HWID verification FAILED.\n' |
Tammo Spalink | 95c4373 | 2012-07-25 15:57:14 -0700 | [diff] [blame] | 555 | if cooked_components.unmatched: |
Tammo Spalink | 5c69983 | 2012-07-03 17:50:39 +0800 | [diff] [blame] | 556 | sys.exit(err_msg + 'some components could not be indentified:\n%s' % |
Tammo Spalink | 95c4373 | 2012-07-25 15:57:14 -0700 | [diff] [blame] | 557 | YamlWrite(cooked_components.unmatched)) |
| 558 | if not match_tree: |
Tammo Spalink | 5c69983 | 2012-07-03 17:50:39 +0800 | [diff] [blame] | 559 | 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] | 560 | component_data.Encode()) |
| 561 | if hwid.bom not in match_tree: |
Tammo Spalink | 5c69983 | 2012-07-03 17:50:39 +0800 | [diff] [blame] | 562 | 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] | 563 | (', '.join(sorted(match_tree)), hwid.bom)) |
| 564 | err_msg += 'target bom %r matches components' % hwid.bom |
| 565 | if hwid.bom not in device.IntersectBomsAndInitialConfigs( |
Hung-Te Lin | 56b1840 | 2015-01-16 14:52:30 +0800 | [diff] [blame] | 566 | cooked_initial_configs): |
Tammo Spalink | 5c69983 | 2012-07-03 17:50:39 +0800 | [diff] [blame] | 567 | sys.exit(err_msg + ', but failed initial config verification') |
Tammo Spalink | 95c4373 | 2012-07-25 15:57:14 -0700 | [diff] [blame] | 568 | matched_variants = match_tree.get(hwid.bom, {}) |
| 569 | if hwid.variant not in matched_variants: |
Tammo Spalink | 5c69983 | 2012-07-03 17:50:39 +0800 | [diff] [blame] | 570 | sys.exit(err_msg + ', but target variant_code %r did not match' % |
Tammo Spalink | 95c4373 | 2012-07-25 15:57:14 -0700 | [diff] [blame] | 571 | hwid.variant) |
| 572 | matched_volatiles = matched_variants.get(hwid.variant, {}) |
| 573 | if hwid.volatile not in matched_volatiles: |
Tammo Spalink | 5c69983 | 2012-07-03 17:50:39 +0800 | [diff] [blame] | 574 | sys.exit(err_msg + ', but target volatile_code %r did not match' % |
Tammo Spalink | 95c4373 | 2012-07-25 15:57:14 -0700 | [diff] [blame] | 575 | hwid.volatile) |
| 576 | found_status = matched_volatiles.get(hwid.volatile, None) |
Tammo Spalink | 5c69983 | 2012-07-03 17:50:39 +0800 | [diff] [blame] | 577 | sys.exit(err_msg + ', but hwid status %r was unacceptable' % found_status) |
Ricky Liang | f7857c1 | 2012-09-17 13:34:41 +0800 | [diff] [blame] | 578 | VerifyVpd(device.vpd_ro_fields, device.vpd_rw_fields) |
Andy Cheng | 0465d13 | 2013-03-20 12:12:06 +0800 | [diff] [blame] | 579 | event_log.Log('verified_hwid', hwid=hwid) |
Tammo Spalink | 95c4373 | 2012-07-25 15:57:14 -0700 | [diff] [blame] | 580 | print 'Verification SUCCESS!' |
Tammo Spalink | 9a96b8a | 2012-04-03 11:10:41 +0800 | [diff] [blame] | 581 | |
| 582 | |
Hung-Te Lin | e1d80f6 | 2016-03-31 14:58:13 +0800 | [diff] [blame] | 583 | @Command('verify_keys', |
Hung-Te Lin | cdb9652 | 2016-04-15 16:51:10 +0800 | [diff] [blame] | 584 | _release_rootfs_cmd_arg, |
| 585 | _firmware_path_cmd_arg) |
Tammo Spalink | 01e1172 | 2012-07-24 10:17:54 -0700 | [diff] [blame] | 586 | def VerifyKeys(options): # pylint: disable=W0613 |
Tammo Spalink | 9a96b8a | 2012-04-03 11:10:41 +0800 | [diff] [blame] | 587 | """Verify keys in firmware and SSD match.""" |
Hung-Te Lin | e1d80f6 | 2016-03-31 14:58:13 +0800 | [diff] [blame] | 588 | return GetGooftool(options).VerifyKeys( |
Hung-Te Lin | cdb9652 | 2016-04-15 16:51:10 +0800 | [diff] [blame] | 589 | options.release_rootfs, options.firmware_path) |
Tammo Spalink | 9a96b8a | 2012-04-03 11:10:41 +0800 | [diff] [blame] | 590 | |
| 591 | |
| 592 | @Command('set_fw_bitmap_locale') |
Tammo Spalink | 01e1172 | 2012-07-24 10:17:54 -0700 | [diff] [blame] | 593 | def SetFirmwareBitmapLocale(options): # pylint: disable=W0613 |
Tammo Spalink | 9a96b8a | 2012-04-03 11:10:41 +0800 | [diff] [blame] | 594 | """Use VPD locale value to set firmware bitmap default language.""" |
Andy Cheng | 7a76cb8 | 2012-11-19 18:08:19 +0800 | [diff] [blame] | 595 | |
Ricky Liang | a70a120 | 2013-03-15 15:03:17 +0800 | [diff] [blame] | 596 | (index, locale) = GetGooftool(options).SetFirmwareBitmapLocale() |
Andy Cheng | 2582d29 | 2012-12-04 17:38:28 +0800 | [diff] [blame] | 597 | logging.info('Firmware bitmap initial locale set to %d (%s).', |
| 598 | index, locale) |
Tammo Spalink | 9a96b8a | 2012-04-03 11:10:41 +0800 | [diff] [blame] | 599 | |
| 600 | |
Hung-Te Lin | e1d80f6 | 2016-03-31 14:58:13 +0800 | [diff] [blame] | 601 | @Command('verify_system_time', |
Hung-Te Lin | cdb9652 | 2016-04-15 16:51:10 +0800 | [diff] [blame] | 602 | _release_rootfs_cmd_arg) |
Tammo Spalink | 01e1172 | 2012-07-24 10:17:54 -0700 | [diff] [blame] | 603 | def VerifySystemTime(options): # pylint: disable=W0613 |
Tammo Spalink | 9a96b8a | 2012-04-03 11:10:41 +0800 | [diff] [blame] | 604 | """Verify system time is later than release filesystem creation time.""" |
Andy Cheng | 7a76cb8 | 2012-11-19 18:08:19 +0800 | [diff] [blame] | 605 | |
Hung-Te Lin | cdb9652 | 2016-04-15 16:51:10 +0800 | [diff] [blame] | 606 | return GetGooftool(options).VerifySystemTime(options.release_rootfs) |
Tammo Spalink | 9a96b8a | 2012-04-03 11:10:41 +0800 | [diff] [blame] | 607 | |
| 608 | |
Hung-Te Lin | e1d80f6 | 2016-03-31 14:58:13 +0800 | [diff] [blame] | 609 | @Command('verify_rootfs', |
Hung-Te Lin | cdb9652 | 2016-04-15 16:51:10 +0800 | [diff] [blame] | 610 | _release_rootfs_cmd_arg) |
Tammo Spalink | 01e1172 | 2012-07-24 10:17:54 -0700 | [diff] [blame] | 611 | def VerifyRootFs(options): # pylint: disable=W0613 |
Tammo Spalink | 9a96b8a | 2012-04-03 11:10:41 +0800 | [diff] [blame] | 612 | """Verify rootfs on SSD is valid by checking hash.""" |
Andy Cheng | 7a76cb8 | 2012-11-19 18:08:19 +0800 | [diff] [blame] | 613 | |
Hung-Te Lin | e1d80f6 | 2016-03-31 14:58:13 +0800 | [diff] [blame] | 614 | return GetGooftool(options).VerifyRootFs(options.release_rootfs) |
Tammo Spalink | 9a96b8a | 2012-04-03 11:10:41 +0800 | [diff] [blame] | 615 | |
Hung-Te Lin | 56b1840 | 2015-01-16 14:52:30 +0800 | [diff] [blame] | 616 | |
Cheng-Yi Chiang | 676b529 | 2013-06-18 12:05:33 +0800 | [diff] [blame] | 617 | @Command('verify_tpm') |
| 618 | def VerifyTPM(options): # pylint: disable=W0613 |
| 619 | """Verify TPM is cleared.""" |
| 620 | |
| 621 | return GetGooftool(options).VerifyTPM() |
Tammo Spalink | 9a96b8a | 2012-04-03 11:10:41 +0800 | [diff] [blame] | 622 | |
Hung-Te Lin | 56b1840 | 2015-01-16 14:52:30 +0800 | [diff] [blame] | 623 | |
Hung-Te Lin | dd708d4 | 2014-07-11 17:05:01 +0800 | [diff] [blame] | 624 | @Command('verify_me_locked') |
| 625 | def VerifyManagementEngineLocked(options): # pylint: disable=W0613 |
| 626 | """Verify Managment Engine is locked.""" |
| 627 | |
| 628 | return GetGooftool(options).VerifyManagementEngineLocked() |
| 629 | |
Hung-Te Lin | 56b1840 | 2015-01-16 14:52:30 +0800 | [diff] [blame] | 630 | |
Tammo Spalink | 9a96b8a | 2012-04-03 11:10:41 +0800 | [diff] [blame] | 631 | @Command('verify_switch_wp') |
Andy Cheng | c92e6f9 | 2012-11-20 16:55:53 +0800 | [diff] [blame] | 632 | def VerifyWPSwitch(options): # pylint: disable=W0613 |
Tammo Spalink | 9a96b8a | 2012-04-03 11:10:41 +0800 | [diff] [blame] | 633 | """Verify hardware write protection switch is enabled.""" |
Andy Cheng | 7a76cb8 | 2012-11-19 18:08:19 +0800 | [diff] [blame] | 634 | |
Ricky Liang | a70a120 | 2013-03-15 15:03:17 +0800 | [diff] [blame] | 635 | GetGooftool(options).VerifyWPSwitch() |
Tammo Spalink | 9a96b8a | 2012-04-03 11:10:41 +0800 | [diff] [blame] | 636 | |
| 637 | |
| 638 | @Command('verify_switch_dev') |
Tammo Spalink | 01e1172 | 2012-07-24 10:17:54 -0700 | [diff] [blame] | 639 | def VerifyDevSwitch(options): # pylint: disable=W0613 |
Tammo Spalink | 9a96b8a | 2012-04-03 11:10:41 +0800 | [diff] [blame] | 640 | """Verify developer switch is disabled.""" |
Andy Cheng | 7a76cb8 | 2012-11-19 18:08:19 +0800 | [diff] [blame] | 641 | |
Ricky Liang | a70a120 | 2013-03-15 15:03:17 +0800 | [diff] [blame] | 642 | if GetGooftool(options).CheckDevSwitchForDisabling(): |
Hung-Te Lin | d7d3472 | 2012-07-26 16:48:35 +0800 | [diff] [blame] | 643 | logging.warn('VerifyDevSwitch: No physical switch.') |
Andy Cheng | 0465d13 | 2013-03-20 12:12:06 +0800 | [diff] [blame] | 644 | event_log.Log('switch_dev', type='virtual switch') |
Tammo Spalink | 9a96b8a | 2012-04-03 11:10:41 +0800 | [diff] [blame] | 645 | |
| 646 | |
Jon Salz | add90d3 | 2014-04-29 16:16:27 +0800 | [diff] [blame] | 647 | @Command('verify_branding') |
| 648 | def VerifyBranding(options): # pylint: disable=W0613 |
| 649 | """Verify that branding fields are properly set. |
| 650 | |
| 651 | customization_id, if set in the RO VPD, must be of the correct format. |
| 652 | |
| 653 | rlz_brand_code must be set either in the RO VPD or OEM partition, and must |
| 654 | be of the correct format. |
| 655 | """ |
| 656 | return GetGooftool(options).VerifyBranding() |
| 657 | |
| 658 | |
bowgotsai | 13820f4 | 2015-09-10 23:18:04 +0800 | [diff] [blame] | 659 | @Command('verify_release_channel', |
| 660 | _enforced_release_channels_cmd_arg) |
bowgotsai | 529139c | 2015-05-30 01:39:49 +0800 | [diff] [blame] | 661 | def VerifyReleaseChannel(options): # pylint: disable=W0613 |
| 662 | """Verify that release image channel is correct. |
| 663 | |
| 664 | ChromeOS has four channels: canary, dev, beta and stable. |
| 665 | The last three channels support image auto-updates, checks |
| 666 | that release image channel is one of them. |
| 667 | """ |
bowgotsai | 13820f4 | 2015-09-10 23:18:04 +0800 | [diff] [blame] | 668 | return GetGooftool(options).VerifyReleaseChannel( |
| 669 | options.enforced_release_channels) |
bowgotsai | 529139c | 2015-05-30 01:39:49 +0800 | [diff] [blame] | 670 | |
| 671 | |
Tammo Spalink | 9a96b8a | 2012-04-03 11:10:41 +0800 | [diff] [blame] | 672 | @Command('write_protect') |
Tammo Spalink | 01e1172 | 2012-07-24 10:17:54 -0700 | [diff] [blame] | 673 | def EnableFwWp(options): # pylint: disable=W0613 |
Tammo Spalink | 9a96b8a | 2012-04-03 11:10:41 +0800 | [diff] [blame] | 674 | """Enable then verify firmware write protection.""" |
| 675 | |
Hung-Te Lin | b21c668 | 2012-08-01 13:53:57 +0800 | [diff] [blame] | 676 | def CalculateLegacyRange(fw_type, length, section_data, |
| 677 | section_name): |
Hung-Te Lin | 7ea39e8 | 2012-07-31 18:39:33 +0800 | [diff] [blame] | 678 | ro_size = length / 2 |
| 679 | ro_a = int(section_data[0] / ro_size) |
| 680 | ro_b = int((section_data[0] + section_data[1] - 1) / ro_size) |
| 681 | if ro_a != ro_b: |
Hung-Te Lin | 56b1840 | 2015-01-16 14:52:30 +0800 | [diff] [blame] | 682 | raise Error('%s firmware section %s has illegal size' % |
Hung-Te Lin | b21c668 | 2012-08-01 13:53:57 +0800 | [diff] [blame] | 683 | (fw_type, section_name)) |
Hung-Te Lin | 7ea39e8 | 2012-07-31 18:39:33 +0800 | [diff] [blame] | 684 | ro_offset = ro_a * ro_size |
| 685 | return (ro_offset, ro_size) |
| 686 | |
| 687 | def WriteProtect(fw_file_path, fw_type, legacy_section): |
Tammo Spalink | 9a96b8a | 2012-04-03 11:10:41 +0800 | [diff] [blame] | 688 | """Calculate protection size, then invoke flashrom. |
| 689 | |
| 690 | Our supported chips only allow write protecting half their total |
| 691 | size, so we parition the flash chipset space accordingly. |
| 692 | """ |
Andy Cheng | 7a76cb8 | 2012-11-19 18:08:19 +0800 | [diff] [blame] | 693 | |
Tammo Spalink | 9a96b8a | 2012-04-03 11:10:41 +0800 | [diff] [blame] | 694 | raw_image = open(fw_file_path, 'rb').read() |
Hung-Te Lin | 7ea39e8 | 2012-07-31 18:39:33 +0800 | [diff] [blame] | 695 | wp_section = 'WP_RO' |
Tammo Spalink | 9a96b8a | 2012-04-03 11:10:41 +0800 | [diff] [blame] | 696 | image = crosfw.FirmwareImage(raw_image) |
Hung-Te Lin | 7ea39e8 | 2012-07-31 18:39:33 +0800 | [diff] [blame] | 697 | if image.has_section(wp_section): |
| 698 | section_data = image.get_section_area(wp_section) |
| 699 | ro_offset = section_data[0] |
| 700 | ro_size = section_data[1] |
| 701 | elif image.has_section(legacy_section): |
| 702 | section_data = image.get_section_area(legacy_section) |
| 703 | (ro_offset, ro_size) = CalculateLegacyRange( |
Hung-Te Lin | b21c668 | 2012-08-01 13:53:57 +0800 | [diff] [blame] | 704 | fw_type, len(raw_image), section_data, legacy_section) |
Hung-Te Lin | 7ea39e8 | 2012-07-31 18:39:33 +0800 | [diff] [blame] | 705 | else: |
| 706 | raise Error('could not find %s firmware section %s or %s' % |
| 707 | (fw_type, wp_section, legacy_section)) |
| 708 | |
| 709 | logging.debug('write protecting %s [off=%x size=%x]', fw_type, |
| 710 | ro_offset, ro_size) |
| 711 | crosfw.Flashrom(fw_type).EnableWriteProtection(ro_offset, ro_size) |
Tammo Spalink | 9a96b8a | 2012-04-03 11:10:41 +0800 | [diff] [blame] | 712 | |
| 713 | WriteProtect(crosfw.LoadMainFirmware().GetFileName(), 'main', 'RO_SECTION') |
Andy Cheng | 0465d13 | 2013-03-20 12:12:06 +0800 | [diff] [blame] | 714 | event_log.Log('wp', fw='main') |
Tammo Spalink | 9a96b8a | 2012-04-03 11:10:41 +0800 | [diff] [blame] | 715 | ec_fw_file = crosfw.LoadEcFirmware().GetFileName() |
| 716 | if ec_fw_file is not None: |
| 717 | WriteProtect(ec_fw_file, 'ec', 'EC_RO') |
Andy Cheng | 0465d13 | 2013-03-20 12:12:06 +0800 | [diff] [blame] | 718 | event_log.Log('wp', fw='ec') |
Tammo Spalink | 9a96b8a | 2012-04-03 11:10:41 +0800 | [diff] [blame] | 719 | else: |
| 720 | logging.warning('EC not write protected (seems there is no EC flash).') |
| 721 | |
| 722 | |
| 723 | @Command('clear_gbb_flags') |
Andy Cheng | c92e6f9 | 2012-11-20 16:55:53 +0800 | [diff] [blame] | 724 | def ClearGBBFlags(options): # pylint: disable=W0613 |
Tammo Spalink | 9a96b8a | 2012-04-03 11:10:41 +0800 | [diff] [blame] | 725 | """Zero out the GBB flags, in preparation for transition to release state. |
| 726 | |
| 727 | No GBB flags are set in release/shipping state, but they are useful |
| 728 | for factory/development. See "gbb_utility --flags" for details. |
| 729 | """ |
Andy Cheng | 7a76cb8 | 2012-11-19 18:08:19 +0800 | [diff] [blame] | 730 | |
Ricky Liang | a70a120 | 2013-03-15 15:03:17 +0800 | [diff] [blame] | 731 | GetGooftool(options).ClearGBBFlags() |
Andy Cheng | 0465d13 | 2013-03-20 12:12:06 +0800 | [diff] [blame] | 732 | event_log.Log('clear_gbb_flags') |
Tammo Spalink | 9a96b8a | 2012-04-03 11:10:41 +0800 | [diff] [blame] | 733 | |
| 734 | |
Jon Salz | aa3a30e | 2013-05-15 15:56:28 +0800 | [diff] [blame] | 735 | @Command('clear_factory_vpd_entries') |
| 736 | def ClearFactoryVPDEntries(options): # pylint: disable=W0613 |
| 737 | """Clears factory.* items in the RW VPD.""" |
| 738 | entries = GetGooftool(options).ClearFactoryVPDEntries() |
| 739 | event_log.Log('clear_factory_vpd_entries', entries=FilterDict(entries)) |
| 740 | |
| 741 | |
Mattias Nissler | cca761b | 2015-04-15 21:53:04 +0200 | [diff] [blame] | 742 | @Command('generate_stable_device_secret') |
| 743 | def GenerateStableDeviceSecret(options): # pylint: disable=W0613 |
| 744 | """Generates a a fresh stable device secret and stores it in the RO VPD.""" |
| 745 | GetGooftool(options).GenerateStableDeviceSecret() |
| 746 | event_log.Log('generate_stable_device_secret') |
| 747 | |
Wei-Han Chen | be1355a | 2016-04-24 19:31:03 +0800 | [diff] [blame] | 748 | |
Shun-Hsing Ou | cdc64e1 | 2015-01-14 22:07:33 +0800 | [diff] [blame] | 749 | @Command('wipe_in_place', |
| 750 | CmdArg('--fast', action='store_true', |
Shun-Hsing Ou | 8d3c40a | 2015-10-08 18:16:08 +0800 | [diff] [blame] | 751 | help='use non-secure but faster wipe method.'), |
Shun-Hsing Ou | db407d6 | 2015-11-11 11:03:59 +0800 | [diff] [blame] | 752 | _cutoff_args_cmd_arg, |
Wei-Han Chen | be1355a | 2016-04-24 19:31:03 +0800 | [diff] [blame] | 753 | _shopfloor_url_args_cmd_arg, |
| 754 | _station_ip_cmd_arg, |
| 755 | _station_port_cmd_arg, |
| 756 | _wipe_finish_token_cmd_arg) |
Shun-Hsing Ou | cdc64e1 | 2015-01-14 22:07:33 +0800 | [diff] [blame] | 757 | def WipeInPlace(options): |
| 758 | """Start factory wipe directly without reboot.""" |
| 759 | |
Wei-Han Chen | be1355a | 2016-04-24 19:31:03 +0800 | [diff] [blame] | 760 | GetGooftool(options).WipeInPlace(options.fast, |
| 761 | options.cutoff_args, |
| 762 | options.shopfloor_url, |
| 763 | options.station_ip, |
| 764 | options.station_port, |
| 765 | options.wipe_finish_token) |
Mattias Nissler | cca761b | 2015-04-15 21:53:04 +0200 | [diff] [blame] | 766 | |
Wei-Han Chen | 7dc6d13 | 2016-04-06 11:11:53 +0800 | [diff] [blame] | 767 | @Command('wipe_init', |
Wei-Han Chen | 0a3320e | 2016-04-23 01:32:07 +0800 | [diff] [blame] | 768 | CmdArg('--wipe_args', help='arguments for clobber-state'), |
| 769 | CmdArg('--state_dev', help='path to stateful partition device'), |
| 770 | CmdArg('--root_disk', help='path to primary device'), |
| 771 | CmdArg('--old_root', help='path to old root'), |
| 772 | _cutoff_args_cmd_arg, |
| 773 | _shopfloor_url_args_cmd_arg, |
Wei-Han Chen | be1355a | 2016-04-24 19:31:03 +0800 | [diff] [blame] | 774 | _release_rootfs_cmd_arg, |
| 775 | _station_ip_cmd_arg, |
| 776 | _station_port_cmd_arg, |
| 777 | _wipe_finish_token_cmd_arg) |
Wei-Han Chen | 7dc6d13 | 2016-04-06 11:11:53 +0800 | [diff] [blame] | 778 | def WipeInit(options): |
Wei-Han Chen | 0a3320e | 2016-04-23 01:32:07 +0800 | [diff] [blame] | 779 | GetGooftool(options).WipeInit(options.wipe_args, |
| 780 | options.cutoff_args, |
| 781 | options.shopfloor_url, |
| 782 | options.state_dev, |
| 783 | options.release_rootfs, |
| 784 | options.root_disk, |
Wei-Han Chen | be1355a | 2016-04-24 19:31:03 +0800 | [diff] [blame] | 785 | options.old_root, |
| 786 | options.station_ip, |
| 787 | options.station_port, |
| 788 | options.wipe_finish_token) |
Wei-Han Chen | 7dc6d13 | 2016-04-06 11:11:53 +0800 | [diff] [blame] | 789 | |
Tammo Spalink | 9a96b8a | 2012-04-03 11:10:41 +0800 | [diff] [blame] | 790 | @Command('prepare_wipe', |
| 791 | CmdArg('--fast', action='store_true', |
| 792 | help='use non-secure but faster wipe method.')) |
| 793 | def PrepareWipe(options): |
| 794 | """Prepare system for transition to release state in next reboot.""" |
Tammo Spalink | 9a96b8a | 2012-04-03 11:10:41 +0800 | [diff] [blame] | 795 | |
Ricky Liang | a70a120 | 2013-03-15 15:03:17 +0800 | [diff] [blame] | 796 | GetGooftool(options).PrepareWipe(options.fast) |
Tammo Spalink | 9a96b8a | 2012-04-03 11:10:41 +0800 | [diff] [blame] | 797 | |
Hung-Te Lin | 56b1840 | 2015-01-16 14:52:30 +0800 | [diff] [blame] | 798 | |
Tammo Spalink | 9a96b8a | 2012-04-03 11:10:41 +0800 | [diff] [blame] | 799 | @Command('verify', |
Hung-Te Lin | 6d82754 | 2012-07-19 11:50:41 +0800 | [diff] [blame] | 800 | CmdArg('--no_write_protect', action='store_true', |
| 801 | help='Do not check write protection switch state.'), |
Ricky Liang | 43b879b | 2014-02-24 11:36:55 +0800 | [diff] [blame] | 802 | _hwid_version_cmd_arg, |
Tammo Spalink | 95c4373 | 2012-07-25 15:57:14 -0700 | [diff] [blame] | 803 | _hwid_status_list_cmd_arg, |
Jon Salz | ce124fb | 2012-10-02 17:42:03 +0800 | [diff] [blame] | 804 | _hwdb_path_cmd_arg, |
Ricky Liang | a70a120 | 2013-03-15 15:03:17 +0800 | [diff] [blame] | 805 | _board_cmd_arg, |
Jon Salz | ce124fb | 2012-10-02 17:42:03 +0800 | [diff] [blame] | 806 | _probe_results_cmd_arg, |
Cheng-Yi Chiang | 406ad91 | 2013-11-14 16:51:33 +0800 | [diff] [blame] | 807 | _hwid_cmd_arg, |
Chih-Yu Huang | 714dbc4 | 2015-07-21 16:42:16 +0800 | [diff] [blame] | 808 | _rma_mode_cmd_arg, |
bowgotsai | 13820f4 | 2015-09-10 23:18:04 +0800 | [diff] [blame] | 809 | _cros_core_cmd_arg, |
Hung-Te Lin | cdb9652 | 2016-04-15 16:51:10 +0800 | [diff] [blame] | 810 | _release_rootfs_cmd_arg, |
| 811 | _firmware_path_cmd_arg, |
Ting Shen | 18a0638 | 2016-08-30 16:18:21 +0800 | [diff] [blame^] | 812 | _enforced_release_channels_cmd_arg, |
| 813 | _waive_list_cmd_arg) |
Tammo Spalink | 9a96b8a | 2012-04-03 11:10:41 +0800 | [diff] [blame] | 814 | def Verify(options): |
| 815 | """Verifies if whole factory process is ready for finalization. |
| 816 | |
| 817 | This routine performs all the necessary checks to make sure the |
| 818 | device is ready to be finalized, but does not modify state. These |
| 819 | checks include dev switch, firmware write protection switch, hwid, |
| 820 | system time, keys, and root file system. |
| 821 | """ |
Andy Cheng | 7a76cb8 | 2012-11-19 18:08:19 +0800 | [diff] [blame] | 822 | |
Hung-Te Lin | 6d82754 | 2012-07-19 11:50:41 +0800 | [diff] [blame] | 823 | if not options.no_write_protect: |
Ricky Liang | a70a120 | 2013-03-15 15:03:17 +0800 | [diff] [blame] | 824 | VerifyWPSwitch(options) |
Hung-Te Lin | dd708d4 | 2014-07-11 17:05:01 +0800 | [diff] [blame] | 825 | VerifyManagementEngineLocked(options) |
Ricky Liang | a70a120 | 2013-03-15 15:03:17 +0800 | [diff] [blame] | 826 | VerifyDevSwitch(options) |
| 827 | if options.hwid_version == 2: |
Ricky Liang | 43b879b | 2014-02-24 11:36:55 +0800 | [diff] [blame] | 828 | VerifyHWIDv2(options) |
Ricky Liang | a70a120 | 2013-03-15 15:03:17 +0800 | [diff] [blame] | 829 | elif options.hwid_version == 3: |
Ricky Liang | c662be3 | 2013-12-24 11:50:23 +0800 | [diff] [blame] | 830 | VerifyHWIDv3(options) |
Ricky Liang | a70a120 | 2013-03-15 15:03:17 +0800 | [diff] [blame] | 831 | else: |
| 832 | raise Error, 'Invalid HWID version: %r' % options.hwid_version |
| 833 | VerifySystemTime(options) |
| 834 | VerifyKeys(options) |
| 835 | VerifyRootFs(options) |
Cheng-Yi Chiang | 676b529 | 2013-06-18 12:05:33 +0800 | [diff] [blame] | 836 | VerifyTPM(options) |
Chih-Yu Huang | 714dbc4 | 2015-07-21 16:42:16 +0800 | [diff] [blame] | 837 | if options.cros_core: |
| 838 | logging.info('VerifyBranding is skipped for ChromeOS Core device.') |
| 839 | else: |
| 840 | VerifyBranding(options) |
bowgotsai | 529139c | 2015-05-30 01:39:49 +0800 | [diff] [blame] | 841 | VerifyReleaseChannel(options) |
Tammo Spalink | 9a96b8a | 2012-04-03 11:10:41 +0800 | [diff] [blame] | 842 | |
Hung-Te Lin | 56b1840 | 2015-01-16 14:52:30 +0800 | [diff] [blame] | 843 | |
Jon Salz | fe9036f | 2014-01-16 14:11:23 +0800 | [diff] [blame] | 844 | @Command('untar_stateful_files') |
Hung-Te Lin | 388bce2 | 2014-06-03 19:56:40 +0800 | [diff] [blame] | 845 | def UntarStatefulFiles(unused_options): |
Jon Salz | fe9036f | 2014-01-16 14:11:23 +0800 | [diff] [blame] | 846 | """Untars stateful files from stateful_files.tar.xz on stateful partition. |
| 847 | |
| 848 | If that file does not exist (which should only be R30 and earlier), |
| 849 | this is a no-op. |
| 850 | """ |
Hung-Te Lin | 2333f3f | 2016-08-24 17:56:48 +0800 | [diff] [blame] | 851 | # Path to stateful partition on device. |
| 852 | device_stateful_path = '/mnt/stateful_partition' |
| 853 | tar_file = os.path.join(device_stateful_path, 'stateful_files.tar.xz') |
Jon Salz | fe9036f | 2014-01-16 14:11:23 +0800 | [diff] [blame] | 854 | if os.path.exists(tar_file): |
Hung-Te Lin | 2333f3f | 2016-08-24 17:56:48 +0800 | [diff] [blame] | 855 | Spawn(['tar', 'xf', tar_file], cwd=device_stateful_path, |
Jon Salz | fe9036f | 2014-01-16 14:11:23 +0800 | [diff] [blame] | 856 | log=True, check_call=True) |
| 857 | else: |
| 858 | logging.warning('No stateful files at %s', tar_file) |
Tammo Spalink | 9a96b8a | 2012-04-03 11:10:41 +0800 | [diff] [blame] | 859 | |
Jon Salz | 40b9f82 | 2014-07-25 16:39:55 +0800 | [diff] [blame] | 860 | |
| 861 | @Command('log_source_hashes') |
| 862 | def LogSourceHashes(options): # pylint: disable=W0613 |
| 863 | """Logs hashes of source files in the factory toolkit.""" |
Jon Salz | e60307f | 2014-08-05 16:20:00 +0800 | [diff] [blame] | 864 | # WARNING: The following line is necessary to validate the integrity |
| 865 | # of the factory software. Do not remove or modify it. |
| 866 | # |
| 867 | # 警告:此行会验证工厂软件的完整性,禁止删除或修改。 |
Wei-Han Chen | a5c01a0 | 2016-04-23 19:27:19 +0800 | [diff] [blame] | 868 | factory_par = sys_utils.GetRunningFactoryPythonArchivePath() |
| 869 | if factory_par: |
| 870 | event_log.Log( |
| 871 | 'source_hashes', |
| 872 | **file_utils.HashPythonArchive(factory_par)) |
| 873 | else: |
| 874 | event_log.Log( |
| 875 | 'source_hashes', |
| 876 | **file_utils.HashSourceTree(os.path.join(paths.FACTORY_PATH, 'py'))) |
Jon Salz | 40b9f82 | 2014-07-25 16:39:55 +0800 | [diff] [blame] | 877 | |
| 878 | |
Tammo Spalink | 86a61c6 | 2012-05-25 15:10:35 +0800 | [diff] [blame] | 879 | @Command('log_system_details') |
Tammo Spalink | 01e1172 | 2012-07-24 10:17:54 -0700 | [diff] [blame] | 880 | def LogSystemDetails(options): # pylint: disable=W0613 |
Tammo Spalink | 86a61c6 | 2012-05-25 15:10:35 +0800 | [diff] [blame] | 881 | """Write miscellaneous system details to the event log.""" |
Andy Cheng | 7a76cb8 | 2012-11-19 18:08:19 +0800 | [diff] [blame] | 882 | |
Ricky Liang | 43b879b | 2014-02-24 11:36:55 +0800 | [diff] [blame] | 883 | event_log.Log('system_details', **GetGooftool(options).GetSystemDetails()) |
Tammo Spalink | 86a61c6 | 2012-05-25 15:10:35 +0800 | [diff] [blame] | 884 | |
| 885 | |
Jon Salz | a88b83b | 2013-05-27 20:00:35 +0800 | [diff] [blame] | 886 | def CreateReportArchiveBlob(*args, **kwargs): |
| 887 | """Creates a report archive and returns it as a blob. |
Tammo Spalink | 9a96b8a | 2012-04-03 11:10:41 +0800 | [diff] [blame] | 888 | |
Jon Salz | a88b83b | 2013-05-27 20:00:35 +0800 | [diff] [blame] | 889 | Args: |
| 890 | See CreateReportArchive. |
Andy Cheng | 7a76cb8 | 2012-11-19 18:08:19 +0800 | [diff] [blame] | 891 | |
Jon Salz | a88b83b | 2013-05-27 20:00:35 +0800 | [diff] [blame] | 892 | Returns: |
| 893 | An xmlrpclib.Binary object containing a .tar.xz file. |
| 894 | """ |
| 895 | with open(CreateReportArchive(*args, **kwargs)) as f: |
| 896 | return xmlrpclib.Binary(f.read()) |
| 897 | |
| 898 | |
| 899 | def CreateReportArchive(device_sn=None, add_file=None): |
| 900 | """Creates a report archive in a temporary directory. |
| 901 | |
| 902 | Args: |
| 903 | device_sn: The device serial number (optional). |
| 904 | add_file: A list of files to add (optional). |
| 905 | |
| 906 | Returns: |
| 907 | Path to the archive. |
| 908 | """ |
Hung-Te Lin | 6bd1647 | 2012-06-20 16:26:47 +0800 | [diff] [blame] | 909 | def NormalizeAsFileName(token): |
| 910 | return re.sub(r'\W+', '', token).strip() |
Jon Salz | a88b83b | 2013-05-27 20:00:35 +0800 | [diff] [blame] | 911 | |
| 912 | target_name = '%s%s.tar.xz' % ( |
| 913 | time.strftime('%Y%m%dT%H%M%SZ', |
| 914 | time.gmtime()), |
Hung-Te Lin | 56b1840 | 2015-01-16 14:52:30 +0800 | [diff] [blame] | 915 | ('' if device_sn is None else |
| 916 | '_' + NormalizeAsFileName(device_sn))) |
Tammo Spalink | 86a61c6 | 2012-05-25 15:10:35 +0800 | [diff] [blame] | 917 | target_path = os.path.join(gettempdir(), target_name) |
Jon Salz | a88b83b | 2013-05-27 20:00:35 +0800 | [diff] [blame] | 918 | |
Tammo Spalink | 86a61c6 | 2012-05-25 15:10:35 +0800 | [diff] [blame] | 919 | # Intentionally ignoring dotfiles in EVENT_LOG_DIR. |
Andy Cheng | 0465d13 | 2013-03-20 12:12:06 +0800 | [diff] [blame] | 920 | tar_cmd = 'cd %s ; tar cJf %s *' % (event_log.EVENT_LOG_DIR, target_path) |
Joel Kitching | 4b1b6c0 | 2016-05-10 14:34:30 +0800 | [diff] [blame] | 921 | tar_cmd += ' --add-file %s' % paths.GetFactoryLogPath() |
Jon Salz | a88b83b | 2013-05-27 20:00:35 +0800 | [diff] [blame] | 922 | if add_file: |
| 923 | for f in add_file: |
Jon Salz | 6526643 | 2012-07-30 19:02:49 +0800 | [diff] [blame] | 924 | # Require absolute paths since the tar command may change the |
| 925 | # directory. |
| 926 | if not f.startswith('/'): |
| 927 | raise Error('Not an absolute path: %s' % f) |
| 928 | if not os.path.exists(f): |
| 929 | raise Error('File does not exist: %s' % f) |
| 930 | tar_cmd += ' --add-file %s' % pipes.quote(f) |
Tammo Spalink | 86a61c6 | 2012-05-25 15:10:35 +0800 | [diff] [blame] | 931 | cmd_result = Shell(tar_cmd) |
Jon Salz | ff88c02 | 2012-11-03 12:19:58 +0800 | [diff] [blame] | 932 | |
| 933 | if ((cmd_result.status == 1) and |
| 934 | all((x == '' or |
| 935 | 'file changed as we read it' in x or |
| 936 | "Removing leading `/' from member names" in x) |
| 937 | for x in cmd_result.stderr.split('\n'))): |
| 938 | # That's OK. Make sure it's valid though. |
Vic Yang | 85199e7 | 2013-01-28 14:33:11 +0800 | [diff] [blame] | 939 | Spawn(['tar', 'tfJ', target_path], check_call=True, log=True, |
Jon Salz | ff88c02 | 2012-11-03 12:19:58 +0800 | [diff] [blame] | 940 | ignore_stdout=True) |
| 941 | elif not cmd_result.success: |
Tammo Spalink | 86a61c6 | 2012-05-25 15:10:35 +0800 | [diff] [blame] | 942 | raise Error('unable to tar event logs, cmd %r failed, stderr: %r' % |
| 943 | (tar_cmd, cmd_result.stderr)) |
Jon Salz | ff88c02 | 2012-11-03 12:19:58 +0800 | [diff] [blame] | 944 | |
Jon Salz | a88b83b | 2013-05-27 20:00:35 +0800 | [diff] [blame] | 945 | return target_path |
| 946 | |
| 947 | _upload_method_cmd_arg = CmdArg( |
| 948 | '--upload_method', metavar='METHOD:PARAM', |
| 949 | help=('How to perform the upload. METHOD should be one of ' |
| 950 | '{ftp, shopfloor, ftps, cpfe}.')) |
| 951 | _add_file_cmd_arg = CmdArg( |
| 952 | '--add_file', metavar='FILE', action='append', |
| 953 | help='Extra file to include in report (must be an absolute path)') |
| 954 | |
Hung-Te Lin | 56b1840 | 2015-01-16 14:52:30 +0800 | [diff] [blame] | 955 | |
Jon Salz | a88b83b | 2013-05-27 20:00:35 +0800 | [diff] [blame] | 956 | @Command('upload_report', |
| 957 | _upload_method_cmd_arg, |
| 958 | _add_file_cmd_arg) |
| 959 | def UploadReport(options): |
| 960 | """Create a report containing key device details.""" |
Hung-Te Lin | 74346d7 | 2016-01-07 22:16:57 +0800 | [diff] [blame] | 961 | ro_vpd = ReadRoVpd() |
Jon Salz | a88b83b | 2013-05-27 20:00:35 +0800 | [diff] [blame] | 962 | device_sn = ro_vpd.get('serial_number', None) |
| 963 | if device_sn is None: |
| 964 | logging.warning('RO_VPD missing device serial number') |
Chun-Ta Lin | 53cbbd5 | 2016-06-08 21:42:19 +0800 | [diff] [blame] | 965 | device_sn = 'MISSING_SN_' + time_utils.TimedUUID() |
Jon Salz | a88b83b | 2013-05-27 20:00:35 +0800 | [diff] [blame] | 966 | target_path = CreateReportArchive(device_sn) |
| 967 | |
Tammo Spalink | 86a61c6 | 2012-05-25 15:10:35 +0800 | [diff] [blame] | 968 | if options.upload_method is None or options.upload_method == 'none': |
| 969 | logging.warning('REPORT UPLOAD SKIPPED (report left at %s)', target_path) |
| 970 | return |
| 971 | method, param = options.upload_method.split(':', 1) |
| 972 | if method == 'shopfloor': |
| 973 | report_upload.ShopFloorUpload(target_path, param) |
| 974 | elif method == 'ftp': |
Jay Kim | 360c1dd | 2012-06-25 10:58:11 -0700 | [diff] [blame] | 975 | report_upload.FtpUpload(target_path, 'ftp:' + param) |
Tammo Spalink | 86a61c6 | 2012-05-25 15:10:35 +0800 | [diff] [blame] | 976 | elif method == 'ftps': |
| 977 | report_upload.CurlUrlUpload(target_path, '--ftp-ssl-reqd ftp:%s' % param) |
| 978 | elif method == 'cpfe': |
Shawn Nematbakhsh | 3404a09 | 2013-01-28 16:49:09 -0800 | [diff] [blame] | 979 | report_upload.CpfeUpload(target_path, pipes.quote(param)) |
Tammo Spalink | 86a61c6 | 2012-05-25 15:10:35 +0800 | [diff] [blame] | 980 | else: |
| 981 | raise Error('unknown report upload method %r', method) |
Tammo Spalink | 9a96b8a | 2012-04-03 11:10:41 +0800 | [diff] [blame] | 982 | |
| 983 | |
| 984 | @Command('finalize', |
Hung-Te Lin | 6d82754 | 2012-07-19 11:50:41 +0800 | [diff] [blame] | 985 | CmdArg('--no_write_protect', action='store_true', |
| 986 | help='Do not enable firmware write protection.'), |
Tammo Spalink | 9a96b8a | 2012-04-03 11:10:41 +0800 | [diff] [blame] | 987 | CmdArg('--fast', action='store_true', |
| 988 | help='use non-secure but faster wipe method.'), |
Shun-Hsing Ou | cdc64e1 | 2015-01-14 22:07:33 +0800 | [diff] [blame] | 989 | CmdArg('--wipe_in_place', action='store_true', |
| 990 | help='Start factory wiping in place without reboot.'), |
Shun-Hsing Ou | 8d3c40a | 2015-10-08 18:16:08 +0800 | [diff] [blame] | 991 | _cutoff_args_cmd_arg, |
Shun-Hsing Ou | db407d6 | 2015-11-11 11:03:59 +0800 | [diff] [blame] | 992 | _shopfloor_url_args_cmd_arg, |
Ricky Liang | 43b879b | 2014-02-24 11:36:55 +0800 | [diff] [blame] | 993 | _hwid_version_cmd_arg, |
Tammo Spalink | 9a96b8a | 2012-04-03 11:10:41 +0800 | [diff] [blame] | 994 | _hwdb_path_cmd_arg, |
Tammo Spalink | 95c4373 | 2012-07-25 15:57:14 -0700 | [diff] [blame] | 995 | _hwid_status_list_cmd_arg, |
Jon Salz | 6526643 | 2012-07-30 19:02:49 +0800 | [diff] [blame] | 996 | _upload_method_cmd_arg, |
Jon Salz | ce124fb | 2012-10-02 17:42:03 +0800 | [diff] [blame] | 997 | _add_file_cmd_arg, |
Ricky Liang | a70a120 | 2013-03-15 15:03:17 +0800 | [diff] [blame] | 998 | _board_cmd_arg, |
Jon Salz | ce124fb | 2012-10-02 17:42:03 +0800 | [diff] [blame] | 999 | _probe_results_cmd_arg, |
Cheng-Yi Chiang | 406ad91 | 2013-11-14 16:51:33 +0800 | [diff] [blame] | 1000 | _hwid_cmd_arg, |
Chih-Yu Huang | 714dbc4 | 2015-07-21 16:42:16 +0800 | [diff] [blame] | 1001 | _rma_mode_cmd_arg, |
bowgotsai | 13820f4 | 2015-09-10 23:18:04 +0800 | [diff] [blame] | 1002 | _cros_core_cmd_arg, |
Hung-Te Lin | cdb9652 | 2016-04-15 16:51:10 +0800 | [diff] [blame] | 1003 | _release_rootfs_cmd_arg, |
| 1004 | _firmware_path_cmd_arg, |
Wei-Han Chen | be1355a | 2016-04-24 19:31:03 +0800 | [diff] [blame] | 1005 | _enforced_release_channels_cmd_arg, |
| 1006 | _station_ip_cmd_arg, |
| 1007 | _station_port_cmd_arg, |
Ting Shen | 18a0638 | 2016-08-30 16:18:21 +0800 | [diff] [blame^] | 1008 | _wipe_finish_token_cmd_arg, |
| 1009 | _waive_list_cmd_arg) |
Tammo Spalink | 9a96b8a | 2012-04-03 11:10:41 +0800 | [diff] [blame] | 1010 | def Finalize(options): |
| 1011 | """Verify system readiness and trigger transition into release state. |
| 1012 | |
Jon Salz | aa3a30e | 2013-05-15 15:56:28 +0800 | [diff] [blame] | 1013 | This routine does the following: |
| 1014 | - Verifies system state (see verify command) |
Jon Salz | fe9036f | 2014-01-16 14:11:23 +0800 | [diff] [blame] | 1015 | - Untars stateful_files.tar.xz, if it exists, in the stateful partition, to |
| 1016 | initialize files such as the CRX cache |
Jon Salz | aa3a30e | 2013-05-15 15:56:28 +0800 | [diff] [blame] | 1017 | - Modifies firmware bitmaps to match locale |
| 1018 | - Clears all factory-friendly flags from the GBB |
| 1019 | - Removes factory-specific entries from RW_VPD (factory.*) |
| 1020 | - Enables firmware write protection (cannot rollback after this) |
| 1021 | - Uploads system logs & reports |
| 1022 | - Sets the necessary boot flags to cause wipe of the factory image on the |
| 1023 | next boot. |
Tammo Spalink | 9a96b8a | 2012-04-03 11:10:41 +0800 | [diff] [blame] | 1024 | """ |
Tammo Spalink | 9a96b8a | 2012-04-03 11:10:41 +0800 | [diff] [blame] | 1025 | Verify(options) |
Jon Salz | 40b9f82 | 2014-07-25 16:39:55 +0800 | [diff] [blame] | 1026 | LogSourceHashes(options) |
Jon Salz | fe9036f | 2014-01-16 14:11:23 +0800 | [diff] [blame] | 1027 | UntarStatefulFiles(options) |
Chih-Yu Huang | 714dbc4 | 2015-07-21 16:42:16 +0800 | [diff] [blame] | 1028 | if options.cros_core: |
| 1029 | logging.info('SetFirmwareBitmapLocale is skipped for ChromeOS Core device.') |
| 1030 | else: |
| 1031 | SetFirmwareBitmapLocale(options) |
Ricky Liang | a70a120 | 2013-03-15 15:03:17 +0800 | [diff] [blame] | 1032 | ClearGBBFlags(options) |
Jon Salz | aa3a30e | 2013-05-15 15:56:28 +0800 | [diff] [blame] | 1033 | ClearFactoryVPDEntries(options) |
Mattias Nissler | cca761b | 2015-04-15 21:53:04 +0200 | [diff] [blame] | 1034 | GenerateStableDeviceSecret(options) |
Hung-Te Lin | 6d82754 | 2012-07-19 11:50:41 +0800 | [diff] [blame] | 1035 | if options.no_write_protect: |
| 1036 | logging.warn('WARNING: Firmware Write Protection is SKIPPED.') |
Andy Cheng | 0465d13 | 2013-03-20 12:12:06 +0800 | [diff] [blame] | 1037 | event_log.Log('wp', fw='both', status='skipped') |
Hung-Te Lin | 6d82754 | 2012-07-19 11:50:41 +0800 | [diff] [blame] | 1038 | else: |
Tammo Spalink | 9a96b8a | 2012-04-03 11:10:41 +0800 | [diff] [blame] | 1039 | EnableFwWp({}) |
Jon Salz | a0f58e0 | 2012-05-29 19:33:39 +0800 | [diff] [blame] | 1040 | LogSystemDetails(options) |
Tammo Spalink | 9a96b8a | 2012-04-03 11:10:41 +0800 | [diff] [blame] | 1041 | UploadReport(options) |
Shun-Hsing Ou | cdc64e1 | 2015-01-14 22:07:33 +0800 | [diff] [blame] | 1042 | if options.wipe_in_place: |
| 1043 | event_log.Log('wipe_in_place') |
Wei-Han Chen | 0a3320e | 2016-04-23 01:32:07 +0800 | [diff] [blame] | 1044 | # WipeInPlace(options) |
| 1045 | wipe_args = [] |
Wei-Han Chen | 1f02f8f | 2016-05-31 11:05:24 +0800 | [diff] [blame] | 1046 | if options.cutoff_args: |
| 1047 | wipe_args += ['--cutoff_args', options.cutoff_args] |
| 1048 | if options.shopfloor_url: |
| 1049 | wipe_args += ['--shopfloor_url', options.shopfloor_url] |
Wei-Han Chen | 0a3320e | 2016-04-23 01:32:07 +0800 | [diff] [blame] | 1050 | if options.fast: |
| 1051 | wipe_args += ['--fast'] |
Wei-Han Chen | be1355a | 2016-04-24 19:31:03 +0800 | [diff] [blame] | 1052 | if options.station_ip: |
| 1053 | wipe_args += ['--station_ip', options.station_ip] |
| 1054 | if options.station_port: |
| 1055 | wipe_args += ['--station_port', options.station_port] |
| 1056 | if options.wipe_finish_token: |
| 1057 | wipe_args += ['--wipe_finish_token', options.wipe_finish_token] |
Wei-Han Chen | 0a3320e | 2016-04-23 01:32:07 +0800 | [diff] [blame] | 1058 | ExecFactoryPar('gooftool', 'wipe_in_place', *wipe_args) |
Shun-Hsing Ou | cdc64e1 | 2015-01-14 22:07:33 +0800 | [diff] [blame] | 1059 | else: |
| 1060 | PrepareWipe(options) |
Tammo Spalink | 9a96b8a | 2012-04-03 11:10:41 +0800 | [diff] [blame] | 1061 | |
| 1062 | |
Ricky Liang | c662be3 | 2013-12-24 11:50:23 +0800 | [diff] [blame] | 1063 | def VerifyHWIDv3(options): |
| 1064 | """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] | 1065 | |
Ricky Liang | c662be3 | 2013-12-24 11:50:23 +0800 | [diff] [blame] | 1066 | This is mainly for Gooftool to verify v3 HWID during finalize. For testing |
| 1067 | and development purposes, please use `hwid` command. |
Ricky Liang | 5339023 | 2013-03-08 15:37:57 +0800 | [diff] [blame] | 1068 | """ |
Ricky Liang | c662be3 | 2013-12-24 11:50:23 +0800 | [diff] [blame] | 1069 | db = GetGooftool(options).db |
| 1070 | encoded_string = options.hwid or hwid_utils.GetHWIDString() |
Ricky Liang | 7905f27 | 2013-03-16 01:57:10 +0800 | [diff] [blame] | 1071 | if options.probe_results: |
Ricky Liang | c662be3 | 2013-12-24 11:50:23 +0800 | [diff] [blame] | 1072 | probed_results = yaml.load(open(options.probe_results).read()) |
Ricky Liang | 7905f27 | 2013-03-16 01:57:10 +0800 | [diff] [blame] | 1073 | else: |
Ricky Liang | c662be3 | 2013-12-24 11:50:23 +0800 | [diff] [blame] | 1074 | probed_results = yaml.load(Probe(probe_vpd=True).Encode()) |
| 1075 | vpd = hwid_utils.GetVPD(probed_results) |
Ricky Liang | 5339023 | 2013-03-08 15:37:57 +0800 | [diff] [blame] | 1076 | |
Hung-Te Lin | 1105295 | 2015-03-18 13:48:59 +0800 | [diff] [blame] | 1077 | event_log.Log('probed_results', probed_results=FilterDict(probed_results)) |
| 1078 | event_log.Log('vpd', vpd=FilterDict(vpd)) |
Ricky Liang | 5339023 | 2013-03-08 15:37:57 +0800 | [diff] [blame] | 1079 | |
Ricky Liang | c662be3 | 2013-12-24 11:50:23 +0800 | [diff] [blame] | 1080 | hwid_utils.VerifyHWID(db, encoded_string, probed_results, vpd, |
Hung-Te Lin | 56b1840 | 2015-01-16 14:52:30 +0800 | [diff] [blame] | 1081 | rma_mode=options.rma_mode) |
Ricky Liang | 5339023 | 2013-03-08 15:37:57 +0800 | [diff] [blame] | 1082 | |
Ricky Liang | c662be3 | 2013-12-24 11:50:23 +0800 | [diff] [blame] | 1083 | event_log.Log('verified_hwid', hwid=encoded_string) |
Ricky Liang | 5339023 | 2013-03-08 15:37:57 +0800 | [diff] [blame] | 1084 | |
| 1085 | |
Ricky Liang | 59611a6 | 2013-06-11 13:47:33 +0800 | [diff] [blame] | 1086 | def ParseDecodedHWID(hwid): |
| 1087 | """Parse the HWID object into a more compact dict. |
| 1088 | |
| 1089 | Args: |
| 1090 | hwid: A decoded HWID object. |
| 1091 | |
| 1092 | Returns: |
| 1093 | A dict containing the board name, the binary string, and the list of |
| 1094 | components. |
| 1095 | """ |
| 1096 | results = {} |
| 1097 | results['board'] = hwid.database.board |
| 1098 | results['binary_string'] = hwid.binary_string |
| 1099 | results['components'] = collections.defaultdict(list) |
| 1100 | components = hwid.bom.components |
| 1101 | for comp_cls in sorted(components): |
| 1102 | for (comp_name, probed_values, _) in sorted(components[comp_cls]): |
| 1103 | if not probed_values: |
| 1104 | db_components = hwid.database.components |
| 1105 | probed_values = db_components.GetComponentAttributes( |
| 1106 | comp_cls, comp_name).get('values') |
| 1107 | results['components'][comp_cls].append( |
| 1108 | {comp_name: probed_values if probed_values else None}) |
| 1109 | # Convert defaultdict to dict. |
| 1110 | results['components'] = dict(results['components']) |
| 1111 | return results |
| 1112 | |
| 1113 | |
henryhsu | 44d793a | 2013-07-20 00:07:38 +0800 | [diff] [blame] | 1114 | @Command('get_firmware_hash', |
| 1115 | CmdArg('--file', metavar='FILE', help='Firmware File.')) |
| 1116 | def GetFirmwareHash(options): |
henryhsu | f6f835c | 2013-07-20 20:49:25 +0800 | [diff] [blame] | 1117 | """Get firmware hash from a file""" |
henryhsu | 44d793a | 2013-07-20 00:07:38 +0800 | [diff] [blame] | 1118 | if os.path.exists(options.file): |
| 1119 | hashes = CalculateFirmwareHashes(options.file) |
| 1120 | for section, value_dict in hashes.iteritems(): |
Hung-Te Lin | 56b1840 | 2015-01-16 14:52:30 +0800 | [diff] [blame] | 1121 | print '%s:' % section |
henryhsu | 44d793a | 2013-07-20 00:07:38 +0800 | [diff] [blame] | 1122 | for key, value in value_dict.iteritems(): |
Hung-Te Lin | 56b1840 | 2015-01-16 14:52:30 +0800 | [diff] [blame] | 1123 | print ' %s: %s' % (key, value) |
henryhsu | 44d793a | 2013-07-20 00:07:38 +0800 | [diff] [blame] | 1124 | else: |
| 1125 | raise Error('File does not exist: %s' % options.file) |
| 1126 | |
henryhsu | f6f835c | 2013-07-20 20:49:25 +0800 | [diff] [blame] | 1127 | |
Tammo Spalink | 9a96b8a | 2012-04-03 11:10:41 +0800 | [diff] [blame] | 1128 | def Main(): |
| 1129 | """Run sub-command specified by the command line args.""" |
Andy Cheng | 7a76cb8 | 2012-11-19 18:08:19 +0800 | [diff] [blame] | 1130 | |
Tammo Spalink | 9a96b8a | 2012-04-03 11:10:41 +0800 | [diff] [blame] | 1131 | options = ParseCmdline( |
Ricky Liang | c662be3 | 2013-12-24 11:50:23 +0800 | [diff] [blame] | 1132 | ('Perform Google required factory tests. All the HWID-related functions ' |
| 1133 | 'provided here are mainly for the deprecated HWID v2. To access HWID ' |
| 1134 | 'v3-related utilities, please use `hwid` command.'), |
Tammo Spalink | 9a96b8a | 2012-04-03 11:10:41 +0800 | [diff] [blame] | 1135 | CmdArg('-l', '--log', metavar='PATH', |
| 1136 | help='Write logs to this file.'), |
Jon Salz | a4bea38 | 2012-10-29 13:00:34 +0800 | [diff] [blame] | 1137 | CmdArg('--suppress-event-logs', action='store_true', |
| 1138 | help='Suppress event logging.'), |
Wei-Han Chen | aff5623 | 2016-04-16 09:17:59 +0800 | [diff] [blame] | 1139 | CmdArg('--phase', default=None, |
| 1140 | help=('override phase for phase checking (defaults to the current ' |
| 1141 | 'as returned by the "factory phase" command)')), |
Tammo Spalink | 8fab531 | 2012-05-28 18:33:30 +0800 | [diff] [blame] | 1142 | verbosity_cmd_arg) |
Tammo Spalink | 9a96b8a | 2012-04-03 11:10:41 +0800 | [diff] [blame] | 1143 | SetupLogging(options.verbosity, options.log) |
Andy Cheng | 0465d13 | 2013-03-20 12:12:06 +0800 | [diff] [blame] | 1144 | event_log.SetGlobalLoggerDefaultPrefix('gooftool') |
| 1145 | event_log.GetGlobalLogger().suppress = options.suppress_event_logs |
Tammo Spalink | 9a96b8a | 2012-04-03 11:10:41 +0800 | [diff] [blame] | 1146 | logging.debug('gooftool options: %s', repr(options)) |
Wei-Han Chen | aff5623 | 2016-04-16 09:17:59 +0800 | [diff] [blame] | 1147 | |
| 1148 | phase.OverridePhase(options.phase) |
Tammo Spalink | 9a96b8a | 2012-04-03 11:10:41 +0800 | [diff] [blame] | 1149 | try: |
| 1150 | logging.debug('GOOFTOOL command %r', options.command_name) |
| 1151 | options.command(options) |
| 1152 | logging.info('GOOFTOOL command %r SUCCESS', options.command_name) |
| 1153 | except Error, e: |
| 1154 | logging.exception(e) |
| 1155 | sys.exit('GOOFTOOL command %r ERROR: %s' % (options.command_name, e)) |
| 1156 | except Exception, e: |
| 1157 | logging.exception(e) |
| 1158 | sys.exit('UNCAUGHT RUNTIME EXCEPTION %s' % e) |
| 1159 | |
| 1160 | |
| 1161 | if __name__ == '__main__': |
| 1162 | Main() |