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