blob: 6a0ba8d9d7f90207db0b590cd4fe27c1fd4a9d19 [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
Jon Salz1c954152014-04-08 11:48:39 +080018from cros.factory.proto import reg_code_pb2
Hung-Te Lincf12e8e2015-11-24 14:13:19 +080019from cros.factory.test import dut as dut_module
Hung-Te Lin3f096842016-01-13 17:37:06 +080020from cros.factory.test.rules import registration_codes
21from cros.factory.test.rules.registration_codes import RegistrationCode
Jon Salz2fc97202014-04-21 16:42:59 +080022from cros.factory.tools.build_board import BuildBoard
Hung-Te Lin03bf7ab2016-06-16 17:26:19 +080023from cros.factory.utils.argparse_utils import CmdArg
24from cros.factory.utils.argparse_utils import Command
25from cros.factory.utils.argparse_utils import ParseCmdline
Hung-Te Lin4e6357c2016-01-08 14:32:00 +080026from cros.factory.utils import sys_utils
Jon Salzf10ef792014-04-01 14:25:36 +080027
28
29@Command('decode',
30 CmdArg('regcode', metavar='REGCODE',
31 help='Encoded registration code string'))
32def 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 Salz1c954152014-04-08 11:48:39 +080040@Command(
Hung-Te Lin56b18402015-01-16 14:52:30 +080041 '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 Salz1c954152014-04-08 11:48:39 +080052def 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 Salz34008632014-07-08 16:27:40 +080069 # '=CiwKIP______TESTING_______'...)
Jon Salz1c954152014-04-08 11:48:39 +080070 proto.content.code = (
Hung-Te Lin56b18402015-01-16 14:52:30 +080071 '\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 Salz1c954152014-04-08 11:48:39 +080074 proto.content.device = options.board.lower()
75 proto.checksum = (
Hung-Te Lin56b18402015-01-16 14:52:30 +080076 binascii.crc32(proto.content.SerializeToString()) & 0xFFFFFFFF)
Jon Salz1c954152014-04-08 11:48:39 +080077
Jon Salz34008632014-07-08 16:27:40 +080078 encoded_string = '=' + base64.urlsafe_b64encode(
79 proto.SerializeToString()).strip()
Jon Salz1c954152014-04-08 11:48:39 +080080
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 Salz2fc97202014-04-21 16:42:59 +080089@Command(
Hung-Te Lin56b18402015-01-16 14:52:30 +080090 '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 Salz2fc97202014-04-21 16:42:59 +0800100def 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 Lincf12e8e2015-11-24 14:13:19 +0800107 dut = dut_module.Create()
Jon Salz2fc97202014-04-21 16:42:59 +0800108
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 Linf5f2d7f2016-01-08 17:12:46 +0800117 if sys_utils.InChroot():
Jon Salz2fc97202014-04-21 16:42:59 +0800118 sys.stderr.write('error: cannot read VPD from chroot; use -u/-g\n')
119 sys.exit(1)
120
Hung-Te Lincf12e8e2015-11-24 14:13:19 +0800121 rw_vpd = dut.vpd.rw.GetAll()
Jon Salz2fc97202014-04-21 16:42:59 +0800122 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 Salzf10ef792014-04-01 14:25:36 +0800138def main():
Jon Salz2fc97202014-04-21 16:42:59 +0800139 logging.basicConfig(level=logging.INFO)
Jon Salzf10ef792014-04-01 14:25:36 +0800140 options = ParseCmdline('Registration code tool.')
141 options.command(options)
142
143
144if __name__ == '__main__':
145 main()