Skip to content

Commit

Permalink
Pull request #136: Credentials examples improved.
Browse files Browse the repository at this point in the history
Merge in WMN_TOOLS/matter from matter_credentials_example_improvements to silabs

Squashed commit of the following:

commit c891db301502f2fa42d55fe74fcbef1748c7f242
Author: Ricardo Casallas <[email protected]>
Date:   Wed Oct 26 07:41:14 2022 -0400

    Credentials example: Review comments addressed.

commit 7236e5d186172d565e00723daf5a637e47f4888b
Author: Ricardo Casallas <[email protected]>
Date:   Tue Oct 25 14:52:57 2022 -0400

    Credentials example: MGM24 support added.

commit c99a6be38a500510a0c3967d35869833b1682cd5
Author: Ricardo Casallas <[email protected]>
Date:   Fri Oct 14 08:22:24 2022 -0400

    Credentials example: Examples improved. Boards support added.
  • Loading branch information
rcasallas-silabs authored and jmartinez-silabs committed May 25, 2023
1 parent b7bda1c commit a7733c9
Show file tree
Hide file tree
Showing 704 changed files with 78,690 additions and 88,102 deletions.
9 changes: 7 additions & 2 deletions silabs_examples/credentials/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ Simplicity Commander.
2. Set the environment:

> `export BASE_SDK_PATH=<Gecko SDK path>`<br>
> `export BASE_SDK_PATH=../../third_party/silabs/gecko_sdk`<br>
> `export ARM_GCC_DIR=<ARM GNU toolchain path>`<br>
Example:
Expand All @@ -90,7 +90,12 @@ Example:
3. Execute the setup script

> `python3 ./creds.py -p /dev/tty.usbmodem0004402663301 -S 440266330 -B brd4186c -C pai_cert.pem -K pai_priv.pem -D cd.bin`
On Linux, the serial port should have the form `/dev/tty*`, for instance:
> `python3 ./creds.py -p /dev/ttyACM0 -S 440266330 -B brd4186c -C pai_cert.pem -K pai_priv.pem -D cd.bin`
On macOS, use `/dev/cu.*` instead of `/dev/tty.*`, for instance:
> `python3 ./creds.py -p /dev/cu.usbmodem0004402663301 -S 440266330 -B brd4186c -C pai_cert.pem -K pai_priv.pem -D cd.bin`

This script:

Expand Down
11 changes: 0 additions & 11 deletions silabs_examples/credentials/common/creds.c

This file was deleted.

44 changes: 0 additions & 44 deletions silabs_examples/credentials/common/creds.h

This file was deleted.

85 changes: 58 additions & 27 deletions silabs_examples/credentials/creds.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,15 @@
_header_template = 'efr32_creds.tmpl'


class PageInfo:
def __init__(self, address, size):
self.address = address
self.size = size


def printUsage():
print('\nUSAGE:\n\tcert.py -p <serial port> -S <serial num> -T <mg12 | mg24> -D <CD file> -C <PAI cert> -K <PAI key> [-R <CSR file>]')
print('Usage:')
print('\tpython3 ./creds.py -p <serial_port> -S <serial_number> -B <board_num> -N <common_name> -V <vendor_id> -P <product_id> -C <pai_cert_pem> -K <pai_key_pem> -D <cert_declaration>')


def roundNearest(n, multiple):
Expand All @@ -22,13 +29,23 @@ def roundNearest(n, multiple):
return n


def baseAddress(board):
if 'brd4164a' == board or'brd4166a' == board:
return 0x000FF800
if 'brd4186a' == board or 'brd4186c' == board:
return 0x0817E000
def pageInfo(board):
# MG12
if (('brd4161a' == board) or ('brd4162a' == board) or ('brd4163a' == board) or
('brd4164a' == board) or ('brd4166a' == board) or ('brd4170a' == board) or
('brd4304a' == board)):
return PageInfo(0x000FF800, 0x800)
# MG24
if (('brd2601b' == board) or ('brd2703a' == board) or ('brd4186a' == board) or
('brd4186c' == board) or ('brd4187a' == board) or ('brd4187c' == board) or
('brd4304a' == board)):
return PageInfo(0x0817E000, 0x2000)
# MGM24
if ('brd4316a' == board) or ('brd4317a' == board) or ('brd4319a' == board):
return PageInfo(0x0817E000, 0x2000)
return None


def execute(desc, args):
print("\n{}\n {}\n".format(desc, ' '.join(args)))
complete = subprocess.run(args)
Expand All @@ -55,13 +72,13 @@ def prepareApps(board, serial_num):
execute('Building host app', ["make", "-C", "host/app/", "-f", "host-creds.Makefile" ])


def generateFiles(port, serial_num, pai_pem, pai_key):
def generateFiles(port, serial_num, common_name, vendor_id, product_id, pai_pem, pai_key):

# Create temporary dir
execute('Creating temp dir', ["mkdir", "-p", _temp_dir ])

# Generate CSR
execute('Requesting CSR', ["./host/app/build/debug/host-creds", "-p", port, "-R", _csr_pem ])
execute('Requesting CSR', ["./host/app/build/debug/host-creds", "-p", port, "-f", _csr_pem, "-N", common_name, "-V", vendor_id, "-P", product_id ])
subprocess.run([ "cat", _csr_pem])

# Generate DAC
Expand All @@ -79,7 +96,7 @@ def generateFiles(port, serial_num, pai_pem, pai_key):
execute('Parsing PAI', ["openssl", "x509", "-outform", "der", "-in", pai_pem, "-out", _pai_der])


def writeCredentials(serial_num, base_addr, cd_file):
def writeCredentials(serial_num, page, cd_file):

# Calculate offsets

Expand All @@ -88,8 +105,8 @@ def writeCredentials(serial_num, base_addr, cd_file):
cd_stats = os.stat(cd_file)

pai_offset = 0
dac_offset = roundNearest(pai_offset + pai_stats.st_size, 32)
cd_offset = roundNearest(dac_offset + dac_stats.st_size, 32)
dac_offset = roundNearest(pai_offset + pai_stats.st_size, 64)
cd_offset = roundNearest(dac_offset + dac_stats.st_size, 64)
end_offset = roundNearest(cd_offset + cd_stats.st_size, 1024)

# Generate header
Expand All @@ -108,19 +125,21 @@ def writeCredentials(serial_num, base_addr, cd_file):

# Flash

cd_address = base_addr + cd_offset
pai_address = base_addr + pai_offset
dac_address = base_addr + dac_offset
print("PAI:\t{} + {}\t\t= {} ({})".format(hex(base_addr), hex(pai_offset), hex(pai_address), pai_stats.st_size))
print("DAC:\t{} + {}\t= {} ({})".format(hex(base_addr), hex(dac_offset), hex(dac_address), dac_stats.st_size))
print("DC:\t{} + {}\t= {} ({})".format(hex(base_addr), hex(cd_offset), hex(cd_address), cd_stats.st_size))
cd_address = page.address + cd_offset
pai_address = page.address + pai_offset
dac_address = page.address + dac_offset
print("PAI:\t{} + {}\t\t= {} ({})".format(hex(page.address), hex(pai_offset), hex(pai_address), pai_stats.st_size))
print("DAC:\t{} + {}\t= {} ({})".format(hex(page.address), hex(dac_offset), hex(dac_address), dac_stats.st_size))
print("DC:\t{} + {}\t= {} ({})".format(hex(page.address), hex(cd_offset), hex(cd_address), cd_stats.st_size))

execute('Flashing PAI', ["commander", "flash", _pai_der, "--binary", "--address", hex(base_addr + pai_offset), "--serialno", serial_num])
execute('Flashing DAC', ["commander", "flash", _dac_der, "--binary", "--address", hex(base_addr + dac_offset), "--serialno", serial_num])
execute('Flashing CD', ["commander", "flash", cd_file, "--binary", "--address", hex(base_addr + cd_offset), "--serialno", serial_num])

execute('Erasing Page', ["commander", "device", "pageerase", "--range", "{}:+{}".format(hex(page.address), page.size), "--serialno", serial_num])
execute('Flashing PAI', ["commander", "flash", _pai_der, "--binary", "--address", hex(page.address + pai_offset), "--serialno", serial_num])
execute('Flashing DAC', ["commander", "flash", _dac_der, "--binary", "--address", hex(page.address + dac_offset), "--serialno", serial_num])
execute('Flashing CD', ["commander", "flash", cd_file, "--binary", "--address", hex(page.address + cd_offset), "--serialno", serial_num])

# Print
subprocess.run(["commander", "readmem", "--range", "{}:+{}".format(hex(base_addr), end_offset), "--serialno", serial_num])
subprocess.run(["commander", "readmem", "--range", "{}:+{}".format(hex(page.address), end_offset), "--serialno", serial_num])


def main(argv):
Expand All @@ -129,12 +148,15 @@ def main(argv):
board = None
cert_file = None
key_file = None
common_name = None
vendor_id = None
product_id = None
cd_file = None

# Parse arguments

try:
opts, args = getopt.getopt(argv,"p:S:B:C:K:D:", ["port=", "serial_num=", "board=", "pai_cert=", "pai_key=", "dc="])
opts, args = getopt.getopt(argv,"p:S:B:N:V:P:C:K:D:", ["port=", "serial_num=", "board=", "name=", "vendor=", "product=", "pai_cert=", "pai_key=", "cd="])
except getopt.GetoptError:
printUsage();
sys.exit(2)
Expand All @@ -153,6 +175,15 @@ def main(argv):
elif opt in ("-B", "--board"):
board = arg

elif opt in ("-N", "--name"):
common_name = arg

elif opt in ("-V", "--vendor"):
vendor_id = arg

elif opt in ("-P", "--product"):
product_id = arg

elif opt in ("-C", "--pai_cert"):
cert_file = arg

Expand All @@ -164,7 +195,7 @@ def main(argv):

# Validate arguments

if (board is None) or (port is None) or (serial_num is None) or (cert_file is None) or (key_file is None) or (cd_file is None):
if (board is None) or (port is None) or (serial_num is None) or (common_name is None) or (vendor_id is None) or (product_id is None) or (cert_file is None) or (key_file is None) or (cd_file is None):
printUsage();
sys.exit(2)

Expand All @@ -180,8 +211,8 @@ def main(argv):

# Get flash base address

base_addr = baseAddress(board)
if base_addr is None:
page_info = pageInfo(board)
if page_info is None:
print("ERROR: Board not supported: '{}'".format(board))
exit(1)

Expand All @@ -200,10 +231,10 @@ def main(argv):
prepareApps(board, serial_num)

# Generate files
generateFiles(port, serial_num, cert_file, key_file)
generateFiles(port, serial_num, common_name, vendor_id, product_id, cert_file, key_file)

# Write files
writeCredentials(serial_num, base_addr, cd_file)
writeCredentials(serial_num, page_info, cd_file)


main(sys.argv[1:])
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#CRC Codes for initially generated config files -- do not modify!
emlib_core_debug_config.h=171843933
mbedtls_config.h=-1710232477
nvm3_default_config.h=-1865878923
psa_crypto_config.h=-715200899
sl_board_control_config.h=-1412901556
sl_device_init_dcdc_config.h=467347643
sl_device_init_emu_config.h=1238190016
sl_device_init_hfxo_config.h=-1662355520
sl_device_init_lfxo_config.h=1192628029
sl_iostream_usart_vcom_config.h=-255408175
sl_memory_config.h=-995392215
sl_mx25_flash_shutdown_eusart_config.h=156638707
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,30 @@
/* IcfEditorFile="$TOOLKIT_DIR$\config\ide\IcfEditor\cortex_v1_0.xml" */
/* Version 4.1.1 */

/*-Memory Regions-*/
define symbol __ICFEDIT_region_ROM_start__ = 0x8006000;
define symbol __ICFEDIT_region_ROM_end__ = (0x8006000+0x178000-1);
define symbol __ICFEDIT_region_RAM_start__ = 0x20000000;
define symbol __ICFEDIT_region_RAM_end__ = (0x20000000+0x40000-1);
/*-Memory Regions*/
define symbol __ICFEDIT_region_ROM_start__ = 0x8000000;
define symbol __ICFEDIT_region_ROM_end__ = (0x8000000+0x17e000-1);
define symbol __ICFEDIT_region_RAM_start__ = 0x20000000;
define symbol __ICFEDIT_region_RAM_end__ = (0x20000000+0x40000-1);

/**** End of ICF editor section. ###ICF###*/

define memory mem with size = 4G;
define region ROM_region = mem:[from __ICFEDIT_region_ROM_start__ to __ICFEDIT_region_ROM_end__];
define region RAM_region = mem:[from __ICFEDIT_region_RAM_start__ to __ICFEDIT_region_RAM_end__];
define region MAIN_FLASH_region = mem:[from 0x8000000 to (0x8000000+0x17e000-1)];

define block CSTACK with alignment = 8
{
section .stack
};

/* Note that "expanding size" is not used yet due to failures in 8.30.1 */
define block HEAP with alignment = 8
{
section .heap
};

define block header with alignment = 8
{
section AAT
};

define block nvm with alignment = 8192
{
Expand All @@ -43,10 +41,8 @@ define block storage with alignment = 8192
section INTERNAL_STORAGE,
};
keep { block storage };

define block application with fixed order
{
block header,
readonly section .intvec,
readonly
};
Expand All @@ -67,18 +63,15 @@ do not initialize
section .heap
};

keep {
section .intvec,
section AAT,
block header
};
keep { section .intvec };
"application":
place at start of ROM_region { block application };

"storage_regions":
place at end of ROM_region {
place at end of MAIN_FLASH_region {
block storage,

block nvm,
block storage
};

"application_ram":
Expand Down
Loading

0 comments on commit a7733c9

Please sign in to comment.