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