blob: 0bc42f5c790d4cb3d8aa7e330291da076cd83bf2 [file] [log] [blame]
Mike Frysinger63bb3c72019-09-01 15:16:26 -04001#!/usr/bin/env python2
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
Peter Shih67c7c0f2018-02-26 11:23:59 +080018import factory_common # pylint: disable=unused-import
Hung-Te Linb6287242016-05-18 14:39:05 +080019from cros.factory.device import device_utils
Yong Hongf6f959e2017-12-26 16:37:49 +080020from cros.factory.hwid.v3 import hwid_utils
Jon Salz1c954152014-04-08 11:48:39 +080021from cros.factory.proto import reg_code_pb2
Hung-Te Lin3f096842016-01-13 17:37:06 +080022from cros.factory.test.rules import registration_codes
23from cros.factory.test.rules.registration_codes import RegistrationCode
Hung-Te Lin03bf7ab2016-06-16 17:26:19 +080024from cros.factory.utils.argparse_utils import CmdArg
25from cros.factory.utils.argparse_utils import Command
26from cros.factory.utils.argparse_utils import ParseCmdline
Peter Shih33fbef72017-08-17 16:14:11 +080027from cros.factory.utils.cros_board_utils import BuildBoard
Hung-Te Lin4e6357c2016-01-08 14:32:00 +080028from cros.factory.utils import sys_utils
Jon Salzf10ef792014-04-01 14:25:36 +080029
30
31@Command('decode',
32 CmdArg('regcode', metavar='REGCODE',
33 help='Encoded registration code string'))
34def Decode(options):
35 reg_code = RegistrationCode(options.regcode)
36 if reg_code.proto:
Yilin Yang71e39412019-09-24 09:26:46 +080037 print(reg_code.proto)
Jon Salzf10ef792014-04-01 14:25:36 +080038 else:
Yilin Yang71e39412019-09-24 09:26:46 +080039 print(reg_code)
Jon Salzf10ef792014-04-01 14:25:36 +080040
41
Jon Salz1c954152014-04-08 11:48:39 +080042@Command(
Hung-Te Lin56b18402015-01-16 14:52:30 +080043 'generate-dummy',
Yong Hong503a2b82017-07-26 18:25:52 +080044 CmdArg('--project', '-p', metavar='PROJECT', required=True,
45 help=('Project to generate codes for. This must be exactly the '
46 'same as the HWID project name, except lowercase.')),
Hung-Te Lin56b18402015-01-16 14:52:30 +080047 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':
Yilin Yang71e39412019-09-24 09:26:46 +080057 print('Aborting.')
Jon Salz1c954152014-04-08 11:48:39 +080058 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))
Yong Hong503a2b82017-07-26 18:25:52 +080073 for i in range(
74 registration_codes.REGISTRATION_CODE_PAYLOAD_BYTES - 16)))
75 proto.content.device = options.project.lower()
Jon Salz1c954152014-04-08 11:48:39 +080076 proto.checksum = (
Hung-Te Lin56b18402015-01-16 14:52:30 +080077 binascii.crc32(proto.content.SerializeToString()) & 0xFFFFFFFF)
Jon Salz1c954152014-04-08 11:48:39 +080078
Jon Salz34008632014-07-08 16:27:40 +080079 encoded_string = '=' + base64.urlsafe_b64encode(
80 proto.SerializeToString()).strip()
Jon Salz1c954152014-04-08 11:48:39 +080081
82 # Make sure the string can be parsed as a sanity check (this will catch,
83 # e.g., invalid device names)
84 reg_code = RegistrationCode(encoded_string)
Yilin Yang71e39412019-09-24 09:26:46 +080085 print('')
86 print(reg_code.proto)
87 print(encoded_string)
Jon Salz1c954152014-04-08 11:48:39 +080088
89
Jon Salz2fc97202014-04-21 16:42:59 +080090@Command(
Hung-Te Lin56b18402015-01-16 14:52:30 +080091 'check',
92 CmdArg(
93 '--unique-code', '-u', metavar='UNIQUE_CODE',
Yong Hong503a2b82017-07-26 18:25:52 +080094 help=('Unique/user code to check (default: ubind_attribute RW VPD '
95 'value)')),
Hung-Te Lin56b18402015-01-16 14:52:30 +080096 CmdArg(
97 '--group-code', '-g', metavar='GROUP_CODE',
98 help='Group code to check (default: gbind_attribute RW VPD value)'),
99 CmdArg(
Yong Hong503a2b82017-07-26 18:25:52 +0800100 '--project', '-b', metavar='PROJECT',
101 help=('Project to check (default: probed project name if run on DUT; '
Wei-Han Chenfddc8842019-01-07 23:19:29 +0800102 'board name in .default_board if in chroot)')),
103 CmdArg(
104 '--allow_dummy', action='store_true',
105 help='Allow dummy regcode (regcode containing "__TESTING__")'))
Jon Salz2fc97202014-04-21 16:42:59 +0800106def Check(options):
Yong Hong503a2b82017-07-26 18:25:52 +0800107 if not options.project:
108 if sys_utils.InChroot():
109 options.project = BuildBoard().short_name
110 else:
Yong Hongf6f959e2017-12-26 16:37:49 +0800111 options.project = hwid_utils.ProbeProject()
Yong Hong503a2b82017-07-26 18:25:52 +0800112 logging.info('Device name: %s', options.project)
Jon Salz2fc97202014-04-21 16:42:59 +0800113
114 rw_vpd = None
115 success = True
Hung-Te Linb6287242016-05-18 14:39:05 +0800116 dut = device_utils.CreateDUTInterface()
Jon Salz2fc97202014-04-21 16:42:59 +0800117
118 for code_type, vpd_attribute, code in (
119 (RegistrationCode.Type.UNIQUE_CODE,
120 'ubind_attribute', options.unique_code),
121 (RegistrationCode.Type.GROUP_CODE,
122 'gbind_attribute', options.group_code)):
123
124 if not code:
125 if rw_vpd is None:
Hung-Te Linf5f2d7f2016-01-08 17:12:46 +0800126 if sys_utils.InChroot():
Jon Salz2fc97202014-04-21 16:42:59 +0800127 sys.stderr.write('error: cannot read VPD from chroot; use -u/-g\n')
128 sys.exit(1)
129
Hung-Te Lincf12e8e2015-11-24 14:13:19 +0800130 rw_vpd = dut.vpd.rw.GetAll()
Guohui Zhouf9687752019-05-24 16:00:52 +0800131 code = rw_vpd.get(vpd_attribute)
Jon Salz2fc97202014-04-21 16:42:59 +0800132 if not code:
133 sys.stderr.write('error: %s is not present in RW VPD\n' %
134 vpd_attribute)
135 sys.exit(1)
136
137 try:
Wei-Han Chenfddc8842019-01-07 23:19:29 +0800138 registration_codes.CheckRegistrationCode(code, code_type, options.project,
139 options.allow_dummy)
Jon Salz2fc97202014-04-21 16:42:59 +0800140 logging.info('%s: success', code_type)
141 except registration_codes.RegistrationCodeException as e:
142 success = False
143 logging.error('%s: failed: %s', code_type, str(e))
144
145 sys.exit(0 if success else 1)
146
147
Jon Salzf10ef792014-04-01 14:25:36 +0800148def main():
Jon Salz2fc97202014-04-21 16:42:59 +0800149 logging.basicConfig(level=logging.INFO)
Jon Salzf10ef792014-04-01 14:25:36 +0800150 options = ParseCmdline('Registration code tool.')
151 options.command(options)
152
153
154if __name__ == '__main__':
155 main()