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