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