blob: e7d808a82ce2bcbf260fe8a2a49267906201ceee [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; '
Wei-Han Chenfddc8842019-01-07 23:19:29 +0800101 'board name in .default_board if in chroot)')),
102 CmdArg(
103 '--allow_dummy', action='store_true',
104 help='Allow dummy regcode (regcode containing "__TESTING__")'))
Jon Salz2fc97202014-04-21 16:42:59 +0800105def Check(options):
Yong Hong503a2b82017-07-26 18:25:52 +0800106 if not options.project:
107 if sys_utils.InChroot():
108 options.project = BuildBoard().short_name
109 else:
Yong Hongf6f959e2017-12-26 16:37:49 +0800110 options.project = hwid_utils.ProbeProject()
Yong Hong503a2b82017-07-26 18:25:52 +0800111 logging.info('Device name: %s', options.project)
Jon Salz2fc97202014-04-21 16:42:59 +0800112
113 rw_vpd = None
114 success = True
Hung-Te Linb6287242016-05-18 14:39:05 +0800115 dut = device_utils.CreateDUTInterface()
Jon Salz2fc97202014-04-21 16:42:59 +0800116
117 for code_type, vpd_attribute, code in (
118 (RegistrationCode.Type.UNIQUE_CODE,
119 'ubind_attribute', options.unique_code),
120 (RegistrationCode.Type.GROUP_CODE,
121 'gbind_attribute', options.group_code)):
122
123 if not code:
124 if rw_vpd is None:
Hung-Te Linf5f2d7f2016-01-08 17:12:46 +0800125 if sys_utils.InChroot():
Jon Salz2fc97202014-04-21 16:42:59 +0800126 sys.stderr.write('error: cannot read VPD from chroot; use -u/-g\n')
127 sys.exit(1)
128
Hung-Te Lincf12e8e2015-11-24 14:13:19 +0800129 rw_vpd = dut.vpd.rw.GetAll()
Jon Salz2fc97202014-04-21 16:42:59 +0800130 code = rw_vpd.get(vpd_attribute)
131 if not code:
132 sys.stderr.write('error: %s is not present in RW VPD\n' %
133 vpd_attribute)
134 sys.exit(1)
135
136 try:
Wei-Han Chenfddc8842019-01-07 23:19:29 +0800137 registration_codes.CheckRegistrationCode(code, code_type, options.project,
138 options.allow_dummy)
Jon Salz2fc97202014-04-21 16:42:59 +0800139 logging.info('%s: success', code_type)
140 except registration_codes.RegistrationCodeException as e:
141 success = False
142 logging.error('%s: failed: %s', code_type, str(e))
143
144 sys.exit(0 if success else 1)
145
146
Jon Salzf10ef792014-04-01 14:25:36 +0800147def main():
Jon Salz2fc97202014-04-21 16:42:59 +0800148 logging.basicConfig(level=logging.INFO)
Jon Salzf10ef792014-04-01 14:25:36 +0800149 options = ParseCmdline('Registration code tool.')
150 options.command(options)
151
152
153if __name__ == '__main__':
154 main()