blob: 29af9da713f6abe5dec7e2fd7da62ff29b0c5f83 [file] [log] [blame]
Jon Salzf10ef792014-04-01 14:25:36 +08001#!/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 Salz34008632014-07-08 16:27:40 +080011import base64
Jon Salz1c954152014-04-08 11:48:39 +080012import binascii
Jon Salz2fc97202014-04-21 16:42:59 +080013import logging
Jon Salz1c954152014-04-08 11:48:39 +080014import random
15import sys
16
Jon Salzf10ef792014-04-01 14:25:36 +080017import factory_common # pylint: disable=W0611
18from cros.factory.hacked_argparse import CmdArg, Command, ParseCmdline
Jon Salz1c954152014-04-08 11:48:39 +080019from cros.factory.proto import reg_code_pb2
Jon Salz2fc97202014-04-21 16:42:59 +080020from cros.factory.system import vpd
Jon Salz1c954152014-04-08 11:48:39 +080021from cros.factory.test import registration_codes
Jon Salz2fc97202014-04-21 16:42:59 +080022from cros.factory.test import utils
Jon Salzf10ef792014-04-01 14:25:36 +080023from cros.factory.test.registration_codes import RegistrationCode
Jon Salz2fc97202014-04-21 16:42:59 +080024from cros.factory.tools.build_board import BuildBoard
Jon Salzf10ef792014-04-01 14:25:36 +080025
26
27@Command('decode',
28 CmdArg('regcode', metavar='REGCODE',
29 help='Encoded registration code string'))
30def 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 Salz1c954152014-04-08 11:48:39 +080038@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'))
50def 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 Salz34008632014-07-08 16:27:40 +080067 # '=CiwKIP______TESTING_______'...)
Jon Salz1c954152014-04-08 11:48:39 +080068 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 Salz34008632014-07-08 16:27:40 +080076 encoded_string = '=' + base64.urlsafe_b64encode(
77 proto.SerializeToString()).strip()
Jon Salz1c954152014-04-08 11:48:39 +080078
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 Salz2fc97202014-04-21 16:42:59 +080087@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)'))
98def 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 Salzf10ef792014-04-01 14:25:36 +0800135def main():
Jon Salz2fc97202014-04-21 16:42:59 +0800136 logging.basicConfig(level=logging.INFO)
Jon Salzf10ef792014-04-01 14:25:36 +0800137 options = ParseCmdline('Registration code tool.')
138 options.command(options)
139
140
141if __name__ == '__main__':
142 main()