Skip to content

Commit

Permalink
Bugfix: Provision: CSR mode fixed.
Browse files Browse the repository at this point in the history
  • Loading branch information
rcasallas-silabs committed Apr 29, 2024
1 parent 42abf1e commit 5627ee1
Show file tree
Hide file tree
Showing 17 changed files with 42,767 additions and 42,818 deletions.
6 changes: 4 additions & 2 deletions examples/platform/silabs/provision/ProvisionProtocolV2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -299,8 +299,6 @@ struct CsrCommand: public Command
{
CsrCommand(Storage & store): Command(kCommand_Init, store)
{
// Always return CSR file
_feedback_list.Add(Parameters::kCsrFile, Type_Binary);
}

CHIP_ERROR ProcessIncoming(Argument &arg) override
Expand All @@ -314,6 +312,10 @@ struct CsrCommand: public Command
case Parameters::kCommonName:
return mStore.Set(arg.id, arg.value.b, arg.size);

case Parameters::kCsrFile:
ReturnErrorOnFailure(_feedback_list.Add(arg.id, arg.type));
return CHIP_NO_ERROR;

default:
return CHIP_ERROR_UNKNOWN_RESOURCE_ID;
}
Expand Down
10,448 changes: 5,224 additions & 5,224 deletions provision/images/efr32mg12_psa123_nvm3k2.s37

Large diffs are not rendered by default.

10,306 changes: 5,153 additions & 5,153 deletions provision/images/efr32mg12_psa12_nvm3k1.s37

Large diffs are not rendered by default.

10,306 changes: 5,153 additions & 5,153 deletions provision/images/efr32mg12_psa12_nvm3k2.s37

Large diffs are not rendered by default.

11,002 changes: 5,500 additions & 5,502 deletions provision/images/efr32mg12_psa3_nvm3k2.s37

Large diffs are not rendered by default.

11,520 changes: 5,759 additions & 5,761 deletions provision/images/efr32mg24_psa123_nvm3k2.s37

Large diffs are not rendered by default.

10,626 changes: 5,313 additions & 5,313 deletions provision/images/efr32mg24_psa12_nvm3k1.s37

Large diffs are not rendered by default.

10,626 changes: 5,313 additions & 5,313 deletions provision/images/efr32mg24_psa12_nvm3k2.s37

Large diffs are not rendered by default.

10,630 changes: 5,315 additions & 5,315 deletions provision/images/efr32mg24_psa3_nvm3k2.s37

Large diffs are not rendered by default.

60 changes: 1 addition & 59 deletions provision/modules/arguments.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ def compileCommandInputs(self, versions, file_ver, params_path):
# Load parsed values into existing arguments
for k, v in inputs.items():
if v is not None:
arg = self.find(k)
arg = self.find(k, True)
arg.set(v, arg.default)
arg.is_user_input = True
# Create export formatter
Expand All @@ -189,7 +189,6 @@ def parseCommandLine(self, params):
# Configure parser
parser = argparse.ArgumentParser(add_help=False, conflict_handler='resolve')
for k, p in params.ids.items():
# print("{}~ {}".format(_util.MARGIN, p))
# Fixed arguments
if p.fixed:
parser.add_argument(p.name, nargs='?', default=p.default)
Expand Down Expand Up @@ -217,60 +216,3 @@ def print(self):
content += "\n{}+ {}".format(_util.MARGIN, a)
if len(content) > 0:
print("* {}({}/{}):{}\n".format(n, count, len(g), content))


class CommonArguments:
PARAMS_PATH = 'modules/parameters.yaml'

def __init__(self, paths) -> None:
super().__init__(paths)
self.configure(paths.base(CommonArguments.PARAMS_PATH))
self.channel = None

def formatOutput(self, main = {}):
# Version
self.insert(main, 'version')
# Options
main['options'] = options = {}
self.insert(options, 'stop')
self.insert(options, 'params', 'parameters')
self.insert(options, 'inputs')
self.insert(options, 'output')
self.insert(options, 'temp')
self.insert(options, 'device')
self.insert(options, 'channel')
self.insert(options, 'generate')
self.insert(options, 'csr')
self.insert(options, 'gen_fw')
self.insert(options, 'prod_fw')
self.insert(options, 'cert_tool')
self.insert(options, 'pylink_lib')
if len(options) > 0: main['options'] = options
# Matter
main['matter'] = matter = {}
# Custom
self.insertCustom(main)
return main


def collectInputs(self, cin, fin = {}):
# Version
self.collect(fin, cin, 'version')
self.collect(fin, cin, 'action')
self.collect(fin, cin, 'extra')
# Options
options = ('options' in fin) and fin['options'] or None
self.collect(options, cin, 'stop')
self.collect(options, cin, 'parameters', 'params')
self.collect(options, cin, 'output')
self.collect(options, cin, 'temp')
self.collect(options, cin, 'device')
self.collect(options, cin, 'channel')
self.collect(options, cin, 'generate')
self.collect(options, cin, 'csr')
self.collect(options, cin, 'gen_fw')
self.collect(options, cin, 'prod_fw')
self.collect(options, cin, 'cert_tool')
self.collect(options, cin, 'pylink_lib')
# Custom
self.collectCustom(cin, fin)
9 changes: 4 additions & 5 deletions provision/modules/credentials.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,11 @@ def collect(self):
# Collect PKCS#12 bundle
self.collectPKCS12(pkcs12, pai_cert, dac_cert, dac_key)

#Calculate offsets sizes here and generate the silabs_cred.h header file

self.generateLegacyHeader(dac_cert, pai_cert, cd)


def generateLegacyHeader(self, dac_cert, pai_cert, cd):
def generateLegacyHeader(self):
cd = self.args.get(ID.kCertification)
pai_cert = self.args.get(ID.kPaiCert)
dac_cert = self.args.get(ID.kDacCert)
# Calculate offsets
dac_stats = os.stat(dac_cert.str())
pai_stats = os.stat(pai_cert.str())
Expand Down
4 changes: 2 additions & 2 deletions provision/modules/formatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def parse(self, main):
self.extract(options, 'device')
self.extract(options, 'channel')
self.extract(options, 'generate')
self.extract(options, 'csr')
self.extract(options, 'csr_mode')
self.extract(options, 'gen_fw')
self.extract(options, 'prod_fw')
self.extract(options, 'cert_tool')
Expand All @@ -72,7 +72,7 @@ def format(self, main = {}):
self.insert(options, 'device')
self.insert(options, 'channel')
self.insert(options, 'generate')
self.insert(options, 'csr')
self.insert(options, 'csr_mode')
self.insert(options, 'gen_fw')
self.insert(options, 'prod_fw')
self.insert(options, 'cert_tool')
Expand Down
5 changes: 5 additions & 0 deletions provision/modules/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ def execute(self, paths, args):
paths.setTemp(args.str(ID.kTemporaryDir))

# Compute defaults
creds = _creds.Credentials(paths, args)
if ('auto' == action) or ('binary' == action):
creds.collect()
self.computeDefaults(paths, args)

# Stop
Expand All @@ -68,6 +70,9 @@ def execute(self, paths, args):
# Export arguments (including returned values)
args.export()

# Generate legacy header (silabs_cred.h) with credentials offsets and sizes
creds.generateLegacyHeader()

# Production Firmware
if _chan.Channel.BLE != chan.type:
self.writeProductionFirmware(args, conn)
Expand Down
12 changes: 9 additions & 3 deletions provision/modules/parameters.py
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,7 @@ class ParameterList:
def __init__(self, paths, custom_path = None) -> None:
self.paths = paths
self.names = {}
self.longs = {}
self.ids = {}
self.groups = {}
self.custom = {}
Expand Down Expand Up @@ -370,6 +371,7 @@ def add(self, id, y):
self.ids[id] = p
p.parse(y)
self.names[p.name] = p
self.longs[p.long] = p
return p

def create(self, y):
Expand All @@ -380,9 +382,13 @@ def get(self, k):
return self.ids[k]
raise ValueError("Unknown parameter 0x{:02x}".format(k))

def find(self, name):
if name in self.names:
return self.names[name]
def find(self, name, use_long = False):
if use_long:
if name in self.longs:
return self.longs[name]
else:
if name in self.names:
return self.names[name]
raise ValueError("Unknown parameter \"{}\"".format(name))

def findList(self, names):
Expand Down
8 changes: 5 additions & 3 deletions provision/modules/parameters.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,10 @@ internal:

- id: 0x0105
desc: 'Certificate Signing Request'
name: '_csr_'
name: 'csr'
long: 'csr_file'
short: '_i5'
type: 'string'
type: 'path'
hidden: true

options:
Expand Down Expand Up @@ -111,7 +112,8 @@ options:

- id: 0x0134
desc: 'Certificate Signing Request Mode'
name: 'csr'
name: 'csr_mode'
long: 'csr'
short: 'r'
type: 'flag'

Expand Down
4 changes: 2 additions & 2 deletions provision/modules/v1_0/protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ def decode(self):
pass

def execute(self, chan):
print("{}:".format(self.name))
print("\n{}:".format(self.name))
# Encode
self.addInt8u(self.id)
self.encode()
Expand Down Expand Up @@ -188,7 +188,7 @@ def encode(self):
def decode(self):
kid = self.getInt32u()
csr = self.getString()
print("{}+ vendor_id:{:04x}, product_id:{:04x}, key:{}, req({}):\n{}".format(_util.MARGIN, self.vendor_id, self.product_id, kid, len(csr), csr))
print("{}+ vendor_id:{:04x}, product_id:{:04x}, key:{}, csr({})\n".format(_util.MARGIN, self.vendor_id, self.product_id, kid, len(csr)))
# Write CSR to file
_util.File(self.csr_path).write(csr)

Expand Down
13 changes: 5 additions & 8 deletions provision/modules/v2_0/protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,24 +30,23 @@ def execute(self, paths, args, chan):
return e.export()
# Init
init = InitCommand(paths, args)
init.execute(chan)
if 'auto' == action:
# CSR
if args.bool(ID.kCsrMode):
self.csr(paths, args, chan)
# Write all
write = AutoCommand(paths, args)
init.execute(chan)
write.execute(chan)
write = FinishCommand(paths, args)
write.execute(chan)
elif 'write' == action:
# Write non-nulls
write = WriteCommand(paths, args)
init.execute(chan)
write.execute(chan)
elif 'read' == action:
# Read
read = ReadCommand(paths, args, args.str(ID.kExtra))
init.execute(chan)
read.execute(chan)
else:
raise ValueError("Action not supported: \"{}\"".format(action))
Expand All @@ -73,10 +72,8 @@ def csr(self, paths, args, chan):
# CSR
cmd = CsrCommand(paths, args)
cmd.execute(chan)
# Write CSR to file
csr_path = paths.temp('csr.pem')
_util.File(csr_path).write(args.value(ID.kCsrFile))
# Sign
csr_path = args.value(ID.kCsrFile)
signer = _pki.SigningServer(base_dir, csr_path, pai_cert_path, pai_key_path, dac_path)
signer.sign()

Expand Down Expand Up @@ -222,7 +219,7 @@ def receivePackage(self, chan, counter, req):
self.fail(req, res, e)

def encodeValue(self, a):
if not self.send_values:
if (not self.send_values) or (a.value is None):
return None
elif (Types.BINARY == a.type) and (Formats.PATH == a.format) and os.path.isfile(a.value):
return _util.BinaryFile(a.value).read()
Expand All @@ -247,7 +244,7 @@ def processIncoming(self, ctx, value):
if Formats.PATH == arg.format:
if arg.value is None:
# Use temporary file
arg.set(self.paths.temp("{}.bin".format(arg.name)))
arg.set(self.paths.temp("{}.bin".format(arg.name)), None, False)
_util.BinaryFile(arg.value).write(value)
else:
arg.set(value, None, False)
Expand Down

0 comments on commit 5627ee1

Please sign in to comment.