blob: d84997df59bef8bea98d6edc8e118bcd9b6824e1 [file] [log] [blame]
You-Cheng Syud5692942018-01-04 14:40:59 +08001#!/usr/bin/env python
Jon Salzf10ef792014-04-01 14:25:36 +08002#
Hung-Te Lin1990b742017-08-09 17:34:57 +08003# Copyright 2014 The Chromium OS Authors. All rights reserved.
Jon Salzf10ef792014-04-01 14:25:36 +08004# 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
Peter Shih67c7c0f2018-02-26 11:23:59 +080017import factory_common # pylint: disable=unused-import
Hung-Te Linb6287242016-05-18 14:39:05 +080018from cros.factory.device import device_utils
Yong Hongf6f959e2017-12-26 16:37:49 +080019from cros.factory.hwid.v3 import hwid_utils
Jon Salz1c954152014-04-08 11:48:39 +080020from cros.factory.proto import reg_code_pb2
Hung-Te Lin3f096842016-01-13 17:37:06 +080021from cros.factory.test.rules import registration_codes
22from cros.factory.test.rules.registration_codes import RegistrationCode
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
Peter Shih33fbef72017-08-17 16:14:11 +080026from cros.factory.utils.cros_board_utils import BuildBoard
Hung-Te Lin4e6357c2016-01-08 14:32:00 +080027from cros.factory.utils import sys_utils
Jon Salzf10ef792014-04-01 14:25:36 +080028
29
30@Command('decode',
31 CmdArg('regcode', metavar='REGCODE',
32 help='Encoded registration code string'))
33def Decode(options):
34 reg_code = RegistrationCode(options.regcode)
35 if reg_code.proto:
36 print reg_code.proto
37 else:
38 print reg_code
39
40
Jon Salz1c954152014-04-08 11:48:39 +080041@Command(
Hung-Te Lin56b18402015-01-16 14:52:30 +080042 'generate-dummy',
Yong Hong503a2b82017-07-26 18:25:52 +080043 CmdArg('--project', '-p', metavar='PROJECT', required=True,
44 help=('Project to generate codes for. This must be exactly the '
45 'same as the HWID project name, except lowercase.')),
Hung-Te Lin56b18402015-01-16 14:52:30 +080046 CmdArg('--type', '-t', metavar='TYPE', required=True,
47 choices=['unique', 'group'],
48 help='The type of code to generate (choices: %(choices)s)'),
49 CmdArg('--seed', '-s', metavar='INT', type=int, default=None,
50 help='Seed to use for pseudo-random payload; defaults to clock'))
Jon Salz1c954152014-04-08 11:48:39 +080051def GenerateDummy(options):
52 print ('*** This may be used only to generate a code for testing, '
53 'not for a real device.')
54 yes_no = raw_input('*** Are you OK with that? (yes/no) ')
55 if yes_no != 'yes':
56 print 'Aborting.'
57 sys.exit(1)
58
59 random.seed(options.seed)
60 proto = reg_code_pb2.RegCode()
61 proto.content.code_type = (reg_code_pb2.UNIQUE_CODE
62 if options.type == 'unique'
63 else reg_code_pb2.GROUP_CODE)
64
65 # Use this weird magic string for the first 16 characters to make it
66 # obvious that this is a dummy code. (Base64-encoding this string
67 # results in a reg code that looks like
Jon Salz34008632014-07-08 16:27:40 +080068 # '=CiwKIP______TESTING_______'...)
Jon Salz1c954152014-04-08 11:48:39 +080069 proto.content.code = (
Hung-Te Lin56b18402015-01-16 14:52:30 +080070 '\xff\xff\xff\xff\xffLD\x93 \xd1\xbf\xff\xff\xff\xff\xff' + ''.join(
71 chr(random.getrandbits(8))
Yong Hong503a2b82017-07-26 18:25:52 +080072 for i in range(
73 registration_codes.REGISTRATION_CODE_PAYLOAD_BYTES - 16)))
74 proto.content.device = options.project.lower()
Jon Salz1c954152014-04-08 11:48:39 +080075 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',
Yong Hong503a2b82017-07-26 18:25:52 +080093 help=('Unique/user code to check (default: ubind_attribute RW VPD '
94 'value)')),
Hung-Te Lin56b18402015-01-16 14:52:30 +080095 CmdArg(
96 '--group-code', '-g', metavar='GROUP_CODE',
97 help='Group code to check (default: gbind_attribute RW VPD value)'),
98 CmdArg(
Yong Hong503a2b82017-07-26 18:25:52 +080099 '--project', '-b', metavar='PROJECT',
100 help=('Project to check (default: probed project name if run on DUT; '
101 'board name in .default_board if in chroot)')))
Jon Salz2fc97202014-04-21 16:42:59 +0800102def Check(options):
Yong Hong503a2b82017-07-26 18:25:52 +0800103 if not options.project:
104 if sys_utils.InChroot():
105 options.project = BuildBoard().short_name
106 else:
Yong Hongf6f959e2017-12-26 16:37:49 +0800107 options.project = hwid_utils.ProbeProject()
Yong Hong503a2b82017-07-26 18:25:52 +0800108 logging.info('Device name: %s', options.project)
Jon Salz2fc97202014-04-21 16:42:59 +0800109
110 rw_vpd = None
111 success = True
Hung-Te Linb6287242016-05-18 14:39:05 +0800112 dut = device_utils.CreateDUTInterface()
Jon Salz2fc97202014-04-21 16:42:59 +0800113
114 for code_type, vpd_attribute, code in (
115 (RegistrationCode.Type.UNIQUE_CODE,
116 'ubind_attribute', options.unique_code),
117 (RegistrationCode.Type.GROUP_CODE,
118 'gbind_attribute', options.group_code)):
119
120 if not code:
121 if rw_vpd is None:
Hung-Te Linf5f2d7f2016-01-08 17:12:46 +0800122 if sys_utils.InChroot():
Jon Salz2fc97202014-04-21 16:42:59 +0800123 sys.stderr.write('error: cannot read VPD from chroot; use -u/-g\n')
124 sys.exit(1)
125
Hung-Te Lincf12e8e2015-11-24 14:13:19 +0800126 rw_vpd = dut.vpd.rw.GetAll()
Jon Salz2fc97202014-04-21 16:42:59 +0800127 code = rw_vpd.get(vpd_attribute)
128 if not code:
129 sys.stderr.write('error: %s is not present in RW VPD\n' %
130 vpd_attribute)
131 sys.exit(1)
132
133 try:
Yong Hong503a2b82017-07-26 18:25:52 +0800134 registration_codes.CheckRegistrationCode(code, code_type, options.project)
Jon Salz2fc97202014-04-21 16:42:59 +0800135 logging.info('%s: success', code_type)
136 except registration_codes.RegistrationCodeException as e:
137 success = False
138 logging.error('%s: failed: %s', code_type, str(e))
139
140 sys.exit(0 if success else 1)
141
142
Jon Salzf10ef792014-04-01 14:25:36 +0800143def main():
Jon Salz2fc97202014-04-21 16:42:59 +0800144 logging.basicConfig(level=logging.INFO)
Jon Salzf10ef792014-04-01 14:25:36 +0800145 options = ParseCmdline('Registration code tool.')
146 options.command(options)
147
148
149if __name__ == '__main__':
150 main()