blob: 1291ab695764411e645d3837e8f4ec0b92858923 [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
Jon Salz34008632014-07-08 16:27:40 +080010import base64
Jon Salz1c954152014-04-08 11:48:39 +080011import binascii
Jon Salz2fc97202014-04-21 16:42:59 +080012import logging
Jon Salz1c954152014-04-08 11:48:39 +080013import random
14import sys
15
Hung-Te Linb6287242016-05-18 14:39:05 +080016from cros.factory.device import device_utils
Yong Hongf6f959e2017-12-26 16:37:49 +080017from cros.factory.hwid.v3 import hwid_utils
Jon Salz1c954152014-04-08 11:48:39 +080018from cros.factory.proto import reg_code_pb2
Hung-Te Lin3f096842016-01-13 17:37:06 +080019from cros.factory.test.rules import registration_codes
20from cros.factory.test.rules.registration_codes import RegistrationCode
Hung-Te Lin03bf7ab2016-06-16 17:26:19 +080021from cros.factory.utils.argparse_utils import CmdArg
22from cros.factory.utils.argparse_utils import Command
23from cros.factory.utils.argparse_utils import ParseCmdline
Peter Shih33fbef72017-08-17 16:14:11 +080024from cros.factory.utils.cros_board_utils import BuildBoard
Hung-Te Lin4e6357c2016-01-08 14:32:00 +080025from cros.factory.utils import sys_utils
Jon Salzf10ef792014-04-01 14:25:36 +080026
27
28@Command('decode',
29 CmdArg('regcode', metavar='REGCODE',
30 help='Encoded registration code string'))
31def Decode(options):
32 reg_code = RegistrationCode(options.regcode)
33 if reg_code.proto:
Yilin Yang71e39412019-09-24 09:26:46 +080034 print(reg_code.proto)
Jon Salzf10ef792014-04-01 14:25:36 +080035 else:
Yilin Yang71e39412019-09-24 09:26:46 +080036 print(reg_code)
Jon Salzf10ef792014-04-01 14:25:36 +080037
38
Jon Salz1c954152014-04-08 11:48:39 +080039@Command(
Hung-Te Lin56b18402015-01-16 14:52:30 +080040 'generate-dummy',
Yong Hong503a2b82017-07-26 18:25:52 +080041 CmdArg('--project', '-p', metavar='PROJECT', required=True,
42 help=('Project to generate codes for. This must be exactly the '
43 'same as the HWID project name, except lowercase.')),
Hung-Te Lin56b18402015-01-16 14:52:30 +080044 CmdArg('--type', '-t', metavar='TYPE', required=True,
45 choices=['unique', 'group'],
46 help='The type of code to generate (choices: %(choices)s)'),
47 CmdArg('--seed', '-s', metavar='INT', type=int, default=None,
48 help='Seed to use for pseudo-random payload; defaults to clock'))
Jon Salz1c954152014-04-08 11:48:39 +080049def GenerateDummy(options):
Yilin Yang44d7f442020-01-06 12:11:42 +080050 print('*** This may be used only to generate a code for testing, '
51 'not for a real device.')
Yilin Yang8cc5dfb2019-10-22 15:58:53 +080052 yes_no = input('*** Are you OK with that? (yes/no) ')
Jon Salz1c954152014-04-08 11:48:39 +080053 if yes_no != 'yes':
Yilin Yang71e39412019-09-24 09:26:46 +080054 print('Aborting.')
Jon Salz1c954152014-04-08 11:48:39 +080055 sys.exit(1)
56
57 random.seed(options.seed)
58 proto = reg_code_pb2.RegCode()
59 proto.content.code_type = (reg_code_pb2.UNIQUE_CODE
60 if options.type == 'unique'
61 else reg_code_pb2.GROUP_CODE)
62
63 # Use this weird magic string for the first 16 characters to make it
64 # obvious that this is a dummy code. (Base64-encoding this string
65 # results in a reg code that looks like
Jon Salz34008632014-07-08 16:27:40 +080066 # '=CiwKIP______TESTING_______'...)
Jon Salz1c954152014-04-08 11:48:39 +080067 proto.content.code = (
Yilin Yangf951d8b2020-05-06 15:56:20 +080068 b'\xff\xff\xff\xff\xffLD\x93 \xd1\xbf\xff\xff\xff\xff\xff' + b''.join(
69 bytes([random.getrandbits(8)])
Yong Hong503a2b82017-07-26 18:25:52 +080070 for i in range(
71 registration_codes.REGISTRATION_CODE_PAYLOAD_BYTES - 16)))
72 proto.content.device = options.project.lower()
Jon Salz1c954152014-04-08 11:48:39 +080073 proto.checksum = (
Hung-Te Lin56b18402015-01-16 14:52:30 +080074 binascii.crc32(proto.content.SerializeToString()) & 0xFFFFFFFF)
Jon Salz1c954152014-04-08 11:48:39 +080075
Jon Salz34008632014-07-08 16:27:40 +080076 encoded_string = '=' + base64.urlsafe_b64encode(
Yilin Yangf951d8b2020-05-06 15:56:20 +080077 proto.SerializeToString()).strip().decode('utf-8')
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)
Yilin Yang71e39412019-09-24 09:26:46 +080082 print('')
83 print(reg_code.proto)
84 print(encoded_string)
Jon Salz1c954152014-04-08 11:48:39 +080085
86
Jon Salz2fc97202014-04-21 16:42:59 +080087@Command(
Hung-Te Lin56b18402015-01-16 14:52:30 +080088 'check',
89 CmdArg(
90 '--unique-code', '-u', metavar='UNIQUE_CODE',
Yong Hong503a2b82017-07-26 18:25:52 +080091 help=('Unique/user code to check (default: ubind_attribute RW VPD '
92 'value)')),
Hung-Te Lin56b18402015-01-16 14:52:30 +080093 CmdArg(
94 '--group-code', '-g', metavar='GROUP_CODE',
95 help='Group code to check (default: gbind_attribute RW VPD value)'),
96 CmdArg(
Yong Hong503a2b82017-07-26 18:25:52 +080097 '--project', '-b', metavar='PROJECT',
98 help=('Project to check (default: probed project name if run on DUT; '
Wei-Han Chenfddc8842019-01-07 23:19:29 +080099 'board name in .default_board if in chroot)')),
100 CmdArg(
101 '--allow_dummy', action='store_true',
102 help='Allow dummy regcode (regcode containing "__TESTING__")'))
Jon Salz2fc97202014-04-21 16:42:59 +0800103def Check(options):
Yong Hong503a2b82017-07-26 18:25:52 +0800104 if not options.project:
105 if sys_utils.InChroot():
106 options.project = BuildBoard().short_name
107 else:
Yong Hongf6f959e2017-12-26 16:37:49 +0800108 options.project = hwid_utils.ProbeProject()
Yong Hong503a2b82017-07-26 18:25:52 +0800109 logging.info('Device name: %s', options.project)
Jon Salz2fc97202014-04-21 16:42:59 +0800110
111 rw_vpd = None
112 success = True
Hung-Te Linb6287242016-05-18 14:39:05 +0800113 dut = device_utils.CreateDUTInterface()
Jon Salz2fc97202014-04-21 16:42:59 +0800114
115 for code_type, vpd_attribute, code in (
116 (RegistrationCode.Type.UNIQUE_CODE,
117 'ubind_attribute', options.unique_code),
118 (RegistrationCode.Type.GROUP_CODE,
119 'gbind_attribute', options.group_code)):
120
121 if not code:
122 if rw_vpd is None:
Hung-Te Linf5f2d7f2016-01-08 17:12:46 +0800123 if sys_utils.InChroot():
Jon Salz2fc97202014-04-21 16:42:59 +0800124 sys.stderr.write('error: cannot read VPD from chroot; use -u/-g\n')
125 sys.exit(1)
126
Hung-Te Lincf12e8e2015-11-24 14:13:19 +0800127 rw_vpd = dut.vpd.rw.GetAll()
Guohui Zhouf9687752019-05-24 16:00:52 +0800128 code = rw_vpd.get(vpd_attribute)
Jon Salz2fc97202014-04-21 16:42:59 +0800129 if not code:
130 sys.stderr.write('error: %s is not present in RW VPD\n' %
131 vpd_attribute)
132 sys.exit(1)
133
134 try:
Wei-Han Chenfddc8842019-01-07 23:19:29 +0800135 registration_codes.CheckRegistrationCode(code, code_type, options.project,
136 options.allow_dummy)
Jon Salz2fc97202014-04-21 16:42:59 +0800137 logging.info('%s: success', code_type)
138 except registration_codes.RegistrationCodeException as e:
139 success = False
140 logging.error('%s: failed: %s', code_type, str(e))
141
142 sys.exit(0 if success else 1)
143
144
Jon Salzf10ef792014-04-01 14:25:36 +0800145def main():
Jon Salz2fc97202014-04-21 16:42:59 +0800146 logging.basicConfig(level=logging.INFO)
Jon Salzf10ef792014-04-01 14:25:36 +0800147 options = ParseCmdline('Registration code tool.')
148 options.command(options)
149
150
151if __name__ == '__main__':
152 main()