blob: bd17d233cdf6b706931eaa7af690c031ae4520b0 [file] [log] [blame]
Yilin Yang19da6932019-12-10 13:39:28 +08001#!/usr/bin/env python3
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
Yilin Yang71e39412019-09-24 09:26:46 +080010from __future__ import print_function
Jon Salzf10ef792014-04-01 14:25:36 +080011
Jon Salz34008632014-07-08 16:27:40 +080012import base64
Jon Salz1c954152014-04-08 11:48:39 +080013import binascii
Jon Salz2fc97202014-04-21 16:42:59 +080014import logging
Jon Salz1c954152014-04-08 11:48:39 +080015import random
16import sys
17
Yilin Yang8cc5dfb2019-10-22 15:58:53 +080018from six.moves import input
19
Hung-Te Linb6287242016-05-18 14:39:05 +080020from cros.factory.device import device_utils
Yong Hongf6f959e2017-12-26 16:37:49 +080021from cros.factory.hwid.v3 import hwid_utils
Jon Salz1c954152014-04-08 11:48:39 +080022from cros.factory.proto import reg_code_pb2
Hung-Te Lin3f096842016-01-13 17:37:06 +080023from cros.factory.test.rules import registration_codes
24from cros.factory.test.rules.registration_codes import RegistrationCode
Hung-Te Lin03bf7ab2016-06-16 17:26:19 +080025from cros.factory.utils.argparse_utils import CmdArg
26from cros.factory.utils.argparse_utils import Command
27from cros.factory.utils.argparse_utils import ParseCmdline
Peter Shih33fbef72017-08-17 16:14:11 +080028from cros.factory.utils.cros_board_utils import BuildBoard
Hung-Te Lin4e6357c2016-01-08 14:32:00 +080029from cros.factory.utils import sys_utils
Jon Salzf10ef792014-04-01 14:25:36 +080030
31
32@Command('decode',
33 CmdArg('regcode', metavar='REGCODE',
34 help='Encoded registration code string'))
35def Decode(options):
36 reg_code = RegistrationCode(options.regcode)
37 if reg_code.proto:
Yilin Yang71e39412019-09-24 09:26:46 +080038 print(reg_code.proto)
Jon Salzf10ef792014-04-01 14:25:36 +080039 else:
Yilin Yang71e39412019-09-24 09:26:46 +080040 print(reg_code)
Jon Salzf10ef792014-04-01 14:25:36 +080041
42
Jon Salz1c954152014-04-08 11:48:39 +080043@Command(
Hung-Te Lin56b18402015-01-16 14:52:30 +080044 'generate-dummy',
Yong Hong503a2b82017-07-26 18:25:52 +080045 CmdArg('--project', '-p', metavar='PROJECT', required=True,
46 help=('Project to generate codes for. This must be exactly the '
47 'same as the HWID project name, except lowercase.')),
Hung-Te Lin56b18402015-01-16 14:52:30 +080048 CmdArg('--type', '-t', metavar='TYPE', required=True,
49 choices=['unique', 'group'],
50 help='The type of code to generate (choices: %(choices)s)'),
51 CmdArg('--seed', '-s', metavar='INT', type=int, default=None,
52 help='Seed to use for pseudo-random payload; defaults to clock'))
Jon Salz1c954152014-04-08 11:48:39 +080053def GenerateDummy(options):
Yilin Yang44d7f442020-01-06 12:11:42 +080054 print('*** This may be used only to generate a code for testing, '
55 'not for a real device.')
Yilin Yang8cc5dfb2019-10-22 15:58:53 +080056 yes_no = input('*** Are you OK with that? (yes/no) ')
Jon Salz1c954152014-04-08 11:48:39 +080057 if yes_no != 'yes':
Yilin Yang71e39412019-09-24 09:26:46 +080058 print('Aborting.')
Jon Salz1c954152014-04-08 11:48:39 +080059 sys.exit(1)
60
61 random.seed(options.seed)
62 proto = reg_code_pb2.RegCode()
63 proto.content.code_type = (reg_code_pb2.UNIQUE_CODE
64 if options.type == 'unique'
65 else reg_code_pb2.GROUP_CODE)
66
67 # Use this weird magic string for the first 16 characters to make it
68 # obvious that this is a dummy code. (Base64-encoding this string
69 # results in a reg code that looks like
Jon Salz34008632014-07-08 16:27:40 +080070 # '=CiwKIP______TESTING_______'...)
Jon Salz1c954152014-04-08 11:48:39 +080071 proto.content.code = (
Hung-Te Lin56b18402015-01-16 14:52:30 +080072 '\xff\xff\xff\xff\xffLD\x93 \xd1\xbf\xff\xff\xff\xff\xff' + ''.join(
73 chr(random.getrandbits(8))
Yong Hong503a2b82017-07-26 18:25:52 +080074 for i in range(
75 registration_codes.REGISTRATION_CODE_PAYLOAD_BYTES - 16)))
76 proto.content.device = options.project.lower()
Jon Salz1c954152014-04-08 11:48:39 +080077 proto.checksum = (
Hung-Te Lin56b18402015-01-16 14:52:30 +080078 binascii.crc32(proto.content.SerializeToString()) & 0xFFFFFFFF)
Jon Salz1c954152014-04-08 11:48:39 +080079
Jon Salz34008632014-07-08 16:27:40 +080080 encoded_string = '=' + base64.urlsafe_b64encode(
81 proto.SerializeToString()).strip()
Jon Salz1c954152014-04-08 11:48:39 +080082
83 # Make sure the string can be parsed as a sanity check (this will catch,
84 # e.g., invalid device names)
85 reg_code = RegistrationCode(encoded_string)
Yilin Yang71e39412019-09-24 09:26:46 +080086 print('')
87 print(reg_code.proto)
88 print(encoded_string)
Jon Salz1c954152014-04-08 11:48:39 +080089
90
Jon Salz2fc97202014-04-21 16:42:59 +080091@Command(
Hung-Te Lin56b18402015-01-16 14:52:30 +080092 'check',
93 CmdArg(
94 '--unique-code', '-u', metavar='UNIQUE_CODE',
Yong Hong503a2b82017-07-26 18:25:52 +080095 help=('Unique/user code to check (default: ubind_attribute RW VPD '
96 'value)')),
Hung-Te Lin56b18402015-01-16 14:52:30 +080097 CmdArg(
98 '--group-code', '-g', metavar='GROUP_CODE',
99 help='Group code to check (default: gbind_attribute RW VPD value)'),
100 CmdArg(
Yong Hong503a2b82017-07-26 18:25:52 +0800101 '--project', '-b', metavar='PROJECT',
102 help=('Project to check (default: probed project name if run on DUT; '
Wei-Han Chenfddc8842019-01-07 23:19:29 +0800103 'board name in .default_board if in chroot)')),
104 CmdArg(
105 '--allow_dummy', action='store_true',
106 help='Allow dummy regcode (regcode containing "__TESTING__")'))
Jon Salz2fc97202014-04-21 16:42:59 +0800107def Check(options):
Yong Hong503a2b82017-07-26 18:25:52 +0800108 if not options.project:
109 if sys_utils.InChroot():
110 options.project = BuildBoard().short_name
111 else:
Yong Hongf6f959e2017-12-26 16:37:49 +0800112 options.project = hwid_utils.ProbeProject()
Yong Hong503a2b82017-07-26 18:25:52 +0800113 logging.info('Device name: %s', options.project)
Jon Salz2fc97202014-04-21 16:42:59 +0800114
115 rw_vpd = None
116 success = True
Hung-Te Linb6287242016-05-18 14:39:05 +0800117 dut = device_utils.CreateDUTInterface()
Jon Salz2fc97202014-04-21 16:42:59 +0800118
119 for code_type, vpd_attribute, code in (
120 (RegistrationCode.Type.UNIQUE_CODE,
121 'ubind_attribute', options.unique_code),
122 (RegistrationCode.Type.GROUP_CODE,
123 'gbind_attribute', options.group_code)):
124
125 if not code:
126 if rw_vpd is None:
Hung-Te Linf5f2d7f2016-01-08 17:12:46 +0800127 if sys_utils.InChroot():
Jon Salz2fc97202014-04-21 16:42:59 +0800128 sys.stderr.write('error: cannot read VPD from chroot; use -u/-g\n')
129 sys.exit(1)
130
Hung-Te Lincf12e8e2015-11-24 14:13:19 +0800131 rw_vpd = dut.vpd.rw.GetAll()
Guohui Zhouf9687752019-05-24 16:00:52 +0800132 code = rw_vpd.get(vpd_attribute)
Jon Salz2fc97202014-04-21 16:42:59 +0800133 if not code:
134 sys.stderr.write('error: %s is not present in RW VPD\n' %
135 vpd_attribute)
136 sys.exit(1)
137
138 try:
Wei-Han Chenfddc8842019-01-07 23:19:29 +0800139 registration_codes.CheckRegistrationCode(code, code_type, options.project,
140 options.allow_dummy)
Jon Salz2fc97202014-04-21 16:42:59 +0800141 logging.info('%s: success', code_type)
142 except registration_codes.RegistrationCodeException as e:
143 success = False
144 logging.error('%s: failed: %s', code_type, str(e))
145
146 sys.exit(0 if success else 1)
147
148
Jon Salzf10ef792014-04-01 14:25:36 +0800149def main():
Jon Salz2fc97202014-04-21 16:42:59 +0800150 logging.basicConfig(level=logging.INFO)
Jon Salzf10ef792014-04-01 14:25:36 +0800151 options = ParseCmdline('Registration code tool.')
152 options.command(options)
153
154
155if __name__ == '__main__':
156 main()