Jon Salz | f10ef79 | 2014-04-01 14:25:36 +0800 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | # |
| 3 | # Copyright (c) 2014 The Chromium OS Authors. All rights reserved. |
| 4 | # Use of this source code is governed by a BSD-style license that can be |
| 5 | # found in the LICENSE file. |
| 6 | |
| 7 | |
| 8 | """A command-line tool for reg code handling.""" |
| 9 | |
| 10 | |
Jon Salz | 3400863 | 2014-07-08 16:27:40 +0800 | [diff] [blame] | 11 | import base64 |
Jon Salz | 1c95415 | 2014-04-08 11:48:39 +0800 | [diff] [blame] | 12 | import binascii |
Jon Salz | 2fc9720 | 2014-04-21 16:42:59 +0800 | [diff] [blame] | 13 | import logging |
Jon Salz | 1c95415 | 2014-04-08 11:48:39 +0800 | [diff] [blame] | 14 | import random |
| 15 | import sys |
| 16 | |
Jon Salz | f10ef79 | 2014-04-01 14:25:36 +0800 | [diff] [blame] | 17 | import factory_common # pylint: disable=W0611 |
Jon Salz | 1c95415 | 2014-04-08 11:48:39 +0800 | [diff] [blame] | 18 | from cros.factory.proto import reg_code_pb2 |
Hung-Te Lin | cf12e8e | 2015-11-24 14:13:19 +0800 | [diff] [blame] | 19 | from cros.factory.test import dut as dut_module |
Hung-Te Lin | 3f09684 | 2016-01-13 17:37:06 +0800 | [diff] [blame] | 20 | from cros.factory.test.rules import registration_codes |
| 21 | from cros.factory.test.rules.registration_codes import RegistrationCode |
Jon Salz | 2fc9720 | 2014-04-21 16:42:59 +0800 | [diff] [blame] | 22 | from cros.factory.tools.build_board import BuildBoard |
Hung-Te Lin | 03bf7ab | 2016-06-16 17:26:19 +0800 | [diff] [blame^] | 23 | from cros.factory.utils.argparse_utils import CmdArg |
| 24 | from cros.factory.utils.argparse_utils import Command |
| 25 | from cros.factory.utils.argparse_utils import ParseCmdline |
Hung-Te Lin | 4e6357c | 2016-01-08 14:32:00 +0800 | [diff] [blame] | 26 | from cros.factory.utils import sys_utils |
Jon Salz | f10ef79 | 2014-04-01 14:25:36 +0800 | [diff] [blame] | 27 | |
| 28 | |
| 29 | @Command('decode', |
| 30 | CmdArg('regcode', metavar='REGCODE', |
| 31 | help='Encoded registration code string')) |
| 32 | def Decode(options): |
| 33 | reg_code = RegistrationCode(options.regcode) |
| 34 | if reg_code.proto: |
| 35 | print reg_code.proto |
| 36 | else: |
| 37 | print reg_code |
| 38 | |
| 39 | |
Jon Salz | 1c95415 | 2014-04-08 11:48:39 +0800 | [diff] [blame] | 40 | @Command( |
Hung-Te Lin | 56b1840 | 2015-01-16 14:52:30 +0800 | [diff] [blame] | 41 | 'generate-dummy', |
| 42 | CmdArg('--board', '-b', metavar='BOARD', required=True, |
| 43 | help=('Board to generate codes for. This must be exactly the ' |
| 44 | 'same as the HWID board name, except lowercase. For ' |
| 45 | 'boards with variants (like "daisy_spring"), use only ' |
| 46 | 'the variant name ("spring").')), |
| 47 | CmdArg('--type', '-t', metavar='TYPE', required=True, |
| 48 | choices=['unique', 'group'], |
| 49 | help='The type of code to generate (choices: %(choices)s)'), |
| 50 | CmdArg('--seed', '-s', metavar='INT', type=int, default=None, |
| 51 | help='Seed to use for pseudo-random payload; defaults to clock')) |
Jon Salz | 1c95415 | 2014-04-08 11:48:39 +0800 | [diff] [blame] | 52 | def GenerateDummy(options): |
| 53 | print ('*** This may be used only to generate a code for testing, ' |
| 54 | 'not for a real device.') |
| 55 | yes_no = raw_input('*** Are you OK with that? (yes/no) ') |
| 56 | if yes_no != 'yes': |
| 57 | print 'Aborting.' |
| 58 | sys.exit(1) |
| 59 | |
| 60 | random.seed(options.seed) |
| 61 | proto = reg_code_pb2.RegCode() |
| 62 | proto.content.code_type = (reg_code_pb2.UNIQUE_CODE |
| 63 | if options.type == 'unique' |
| 64 | else reg_code_pb2.GROUP_CODE) |
| 65 | |
| 66 | # Use this weird magic string for the first 16 characters to make it |
| 67 | # obvious that this is a dummy code. (Base64-encoding this string |
| 68 | # results in a reg code that looks like |
Jon Salz | 3400863 | 2014-07-08 16:27:40 +0800 | [diff] [blame] | 69 | # '=CiwKIP______TESTING_______'...) |
Jon Salz | 1c95415 | 2014-04-08 11:48:39 +0800 | [diff] [blame] | 70 | proto.content.code = ( |
Hung-Te Lin | 56b1840 | 2015-01-16 14:52:30 +0800 | [diff] [blame] | 71 | '\xff\xff\xff\xff\xffLD\x93 \xd1\xbf\xff\xff\xff\xff\xff' + ''.join( |
| 72 | chr(random.getrandbits(8)) |
| 73 | for i in range(registration_codes.REGISTRATION_CODE_PAYLOAD_BYTES - 16))) |
Jon Salz | 1c95415 | 2014-04-08 11:48:39 +0800 | [diff] [blame] | 74 | proto.content.device = options.board.lower() |
| 75 | proto.checksum = ( |
Hung-Te Lin | 56b1840 | 2015-01-16 14:52:30 +0800 | [diff] [blame] | 76 | binascii.crc32(proto.content.SerializeToString()) & 0xFFFFFFFF) |
Jon Salz | 1c95415 | 2014-04-08 11:48:39 +0800 | [diff] [blame] | 77 | |
Jon Salz | 3400863 | 2014-07-08 16:27:40 +0800 | [diff] [blame] | 78 | encoded_string = '=' + base64.urlsafe_b64encode( |
| 79 | proto.SerializeToString()).strip() |
Jon Salz | 1c95415 | 2014-04-08 11:48:39 +0800 | [diff] [blame] | 80 | |
| 81 | # Make sure the string can be parsed as a sanity check (this will catch, |
| 82 | # e.g., invalid device names) |
| 83 | reg_code = RegistrationCode(encoded_string) |
| 84 | print |
| 85 | print reg_code.proto |
| 86 | print encoded_string |
| 87 | |
| 88 | |
Jon Salz | 2fc9720 | 2014-04-21 16:42:59 +0800 | [diff] [blame] | 89 | @Command( |
Hung-Te Lin | 56b1840 | 2015-01-16 14:52:30 +0800 | [diff] [blame] | 90 | 'check', |
| 91 | CmdArg( |
| 92 | '--unique-code', '-u', metavar='UNIQUE_CODE', |
| 93 | help='Unique/user code to check (default: ubind_attribute RW VPD value)'), |
| 94 | CmdArg( |
| 95 | '--group-code', '-g', metavar='GROUP_CODE', |
| 96 | help='Group code to check (default: gbind_attribute RW VPD value)'), |
| 97 | CmdArg( |
| 98 | '--board', '-b', metavar='BOARD', |
| 99 | help='Board to check (default: from .default_board or lsb-release)')) |
Jon Salz | 2fc9720 | 2014-04-21 16:42:59 +0800 | [diff] [blame] | 100 | def Check(options): |
| 101 | if not options.board: |
| 102 | options.board = BuildBoard().short_name |
| 103 | logging.info('Device name: %s', options.board) |
| 104 | |
| 105 | rw_vpd = None |
| 106 | success = True |
Hung-Te Lin | cf12e8e | 2015-11-24 14:13:19 +0800 | [diff] [blame] | 107 | dut = dut_module.Create() |
Jon Salz | 2fc9720 | 2014-04-21 16:42:59 +0800 | [diff] [blame] | 108 | |
| 109 | for code_type, vpd_attribute, code in ( |
| 110 | (RegistrationCode.Type.UNIQUE_CODE, |
| 111 | 'ubind_attribute', options.unique_code), |
| 112 | (RegistrationCode.Type.GROUP_CODE, |
| 113 | 'gbind_attribute', options.group_code)): |
| 114 | |
| 115 | if not code: |
| 116 | if rw_vpd is None: |
Hung-Te Lin | f5f2d7f | 2016-01-08 17:12:46 +0800 | [diff] [blame] | 117 | if sys_utils.InChroot(): |
Jon Salz | 2fc9720 | 2014-04-21 16:42:59 +0800 | [diff] [blame] | 118 | sys.stderr.write('error: cannot read VPD from chroot; use -u/-g\n') |
| 119 | sys.exit(1) |
| 120 | |
Hung-Te Lin | cf12e8e | 2015-11-24 14:13:19 +0800 | [diff] [blame] | 121 | rw_vpd = dut.vpd.rw.GetAll() |
Jon Salz | 2fc9720 | 2014-04-21 16:42:59 +0800 | [diff] [blame] | 122 | code = rw_vpd.get(vpd_attribute) |
| 123 | if not code: |
| 124 | sys.stderr.write('error: %s is not present in RW VPD\n' % |
| 125 | vpd_attribute) |
| 126 | sys.exit(1) |
| 127 | |
| 128 | try: |
| 129 | registration_codes.CheckRegistrationCode(code, code_type, options.board) |
| 130 | logging.info('%s: success', code_type) |
| 131 | except registration_codes.RegistrationCodeException as e: |
| 132 | success = False |
| 133 | logging.error('%s: failed: %s', code_type, str(e)) |
| 134 | |
| 135 | sys.exit(0 if success else 1) |
| 136 | |
| 137 | |
Jon Salz | f10ef79 | 2014-04-01 14:25:36 +0800 | [diff] [blame] | 138 | def main(): |
Jon Salz | 2fc9720 | 2014-04-21 16:42:59 +0800 | [diff] [blame] | 139 | logging.basicConfig(level=logging.INFO) |
Jon Salz | f10ef79 | 2014-04-01 14:25:36 +0800 | [diff] [blame] | 140 | options = ParseCmdline('Registration code tool.') |
| 141 | options.command(options) |
| 142 | |
| 143 | |
| 144 | if __name__ == '__main__': |
| 145 | main() |