Add support for flashing PKC secured board
v3 cover Add support for flashing PKC secured board
V3:
1. Updated man page by adding all newly defined options.
2. Clean up some cosmetic issues
V2:
1. Redefined sign and download command line parameters as below:
generate signed messages: --gen-signed-msgs --signed-msgs-file <msgs.bin>
download signed messages: --download-signed-msgs --signed-msgs-file <msgs.bin>2. Added option --soc=<soc#> to specify tegra soc model number so that generating signed messages can be proceeded without accessing hardware.
3. Re-enabled cmac_hash when generating signed messages so that signed messages can also run on an unsecured board.
4. Download bootloader's pkc signature only when the board is in secured pkc mode.
5. Added error check on key length.
6. Added option --usb-timeout to allow user to specify usb transfer timeout value.
V1:
1. Use option "--pkc" to sign and download bootloader. This option is designed for developer. Patch 0001-Add-option-pkc.patch was originally submitted by Alban Bedel.
2. Use option "--ml_rcm" and "--pkc" to sign rcm messages and bootloader. This signing only feature is intended for production where signging is done at secured server and flashing can be done at different stage and site without requiring keyfile being present.
3. Use option "--signed" to specify and download signed rcm messages. This option is used for flashing on fused board. ie, a system with security mode enabled.
Alban Bedel (1):
Add support for production devices secured with PKC
Jimmy Zhang (3):
Add option --gen-signed-msgs and --signed-msgs-file to generate signed
blobs
Add option --download-signed-msgs to download signed blobs Add option --usb-timeout=<value>
src/Makefile.am | 2 + src/main.c | 541 ++++++++++++++++++++++++++++++++++++++++++++++++------ src/rcm.c | 18 +- src/rcm.h | 15 +- src/rsa-pss.cpp | 163 ++++++++++++++++ src/rsa-pss.h | 46 +++++ src/tegrarcm.1.in | 118 +++++++++++- src/usb.c | 7 +- 8 files changed, 830 insertions(+), 80 deletions(-) create mode 100644 src/rsa-pss.cpp create mode 100644 src/rsa-pss.h
Signature · 1 line
1.9.1
PATCH v3 1/4 Add support for production devices secured with PKC
In reply to a message not in the archiveFrom: Alban Bedel <[email protected]>
Add the support code needed to sign the RCM messages with RSA-PSS as needed to communicate with secured production devices. This mode is enabled by passing a key via the --pkc command line argument. If such a key is set the RCM messages will be signed with it as well as the bootloader.
---
Changelog:
V2: * Download bl sig only when op_mode is SECURE_PKC
* Generate cmac_hash even when --pkc option is present so that an unfused board can still run with --pkc option.
* Added Error Check on either key modulus length or key length
v1: * Added the missing bootloader signature
* Added the ODM secure PKC mode to the supported operating modes
* Renamed the --key option to --pkc to avoid potential ambiguities if SKB mode is ever supported.
* Added a copyright header to the new files
--- src/Makefile.am | 2 + src/main.c | 81 ++++++++++++++++++++++++---- src/rcm.c | 18 ++++++- src/rcm.h | 15 +++--- src/rsa-pss.cpp | 163 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/rsa-pss.h | 46 ++++++++++++++++ 6 files changed, 307 insertions(+), 18 deletions(-) create mode 100644 src/rsa-pss.cpp create mode 100644 src/rsa-pss.h
src/Makefile.am +2 -0
@@ -8,6 +8,8 @@ tegrarcm_SOURCES = \ nv3p.c \ debug.c \ rcm.c \ + rsa-pss.cpp \ + rsa-pss.h \ aes-cmac.cpp \ aes-cmac.h \ debug.h \
src/main.c +71 -10
194 lines not on this page · raw message
src/rcm.c +16 -2
@@ -1,5 +1,5 @@/* -* Copyright (c) 2011, NVIDIA CORPORATION +* Copyright (c) 2011-2016, NVIDIA CORPORATION * All rights reserved. * * Redistribution and use in source and binary forms, with or without
@@ -32,6 +32,7 @@#include <errno.h> #include "rcm.h" #include "aes-cmac.h" +#include "rsa-pss.h" static int rcm_sign_msg (uint8_t *buf);static int rcm1_sign_msg (uint8_t *buf);
@@ -72,8 +73,9 @@ static uint32_t rcm_get_msg_buf_len(uint32_t payload_len);static uint32_t rcm_version =0 ;static uint32_t message_size =0 ; +static const char *rcm_keyfile =NULL ; -int rcm_init (uint32_t version) +int rcm_init (uint32_t version,const char *keyfile) {int ret = -EINVAL ;
@@ -92,6 +94,9 @@ int rcm_init(uint32_t version) message_size =sizeof (rcm40_msg_t); ret =0 ; } + + rcm_keyfile = keyfile; +return ret; }
@@ -198,6 +203,11 @@ static int rcm35_sign_msg(uint8_t *buf) }cmac_hash (msg->reserved, crypto_len, msg->object_sig.cmac_hash); + +if (rcm_keyfile) +rsa_pss_sign (rcm_keyfile, msg->reserved, crypto_len, + msg->object_sig.rsa_pss_sig, msg->modulus); +return 0 ; }
@@ -218,6 +228,10 @@ static int rcm40_sign_msg(uint8_t *buf) }cmac_hash (msg->reserved, crypto_len, msg->object_sig.cmac_hash); +if (rcm_keyfile) +rsa_pss_sign (rcm_keyfile, msg->reserved, crypto_len, + msg->object_sig.rsa_pss_sig, msg->modulus); +return 0 ; }
src/rcm.h +9 -6
@@ -1,5 +1,5 @@/* -* Copyright (c) 2011, NVIDIA CORPORATION +* Copyright (c) 2011-2016, NVIDIA CORPORATION * All rights reserved. * * Redistribution and use in source and binary forms, with or without
@@ -50,6 +50,8 @@// AES block size in bytes #define RCM_AES_BLOCK_SIZE (128 / 8) +#define RCM_RSA_MODULUS_SIZE (2048 / 8) +#define RCM_RSA_SIG_SIZE RCM_RSA_MODULUS_SIZE/* * Defines the header for RCM messages from the host.
@@ -72,10 +74,10 @@ typedef struct { typedef struct { uint32_t len_insecure; // 000-003 - uint8_t modulus[2048 / 8]; // 004-103 + uint8_t modulus[RCM_RSA_MODULUS_SIZE]; // 004-103 union { uint8_t cmac_hash[RCM_AES_BLOCK_SIZE]; - uint8_t rsa_pss_sig[2048 / 8]; + uint8_t rsa_pss_sig[RCM_RSA_SIG_SIZE]; } object_sig; // 104-203 uint8_t reserved[16]; // 204-213 uint32_t ecid[4]; // 214-223
@@ -89,10 +91,10 @@ typedef struct { typedef struct { uint32_t len_insecure; // 000-003 - uint8_t modulus[2048 / 8]; // 004-103 + uint8_t modulus[RCM_RSA_MODULUS_SIZE]; // 004-103 struct { uint8_t cmac_hash[RCM_AES_BLOCK_SIZE]; - uint8_t rsa_pss_sig[2048 / 8]; + uint8_t rsa_pss_sig[RCM_RSA_SIG_SIZE]; } object_sig; // 104-213 uint8_t reserved[16]; // 214-223 uint32_t ecid[4]; // 224-233
@@ -109,8 +111,9 @@ typedef struct {#define RCM_OP_MODE_DEVEL 0x3#define RCM_OP_MODE_ODM_SECURE 0x4#define RCM_OP_MODE_ODM_OPEN 0x5 +#define RCM_OP_MODE_ODM_SECURE_PKC 0x6 -int rcm_init (uint32_t version); +int rcm_init (uint32_t version,const char *keyfile);uint32_t rcm_get_msg_len (uint8_t *msg); int rcm_create_msg( uint32_t opcode,
src/rsa-pss.cpp new file +163 -0
163 lines not on this page · raw message
src/rsa-pss.h new file +46 -0
46 lines not on this page · raw message
Signature · 1 line
1.9.1
PATCH v3 2/4 Add option --gen-signed-msgs and --signed-msgs-file to generate signed blobs
In reply to a message not in the archiveThis feature allows generation of signed blobs that can later be used to communicate with a PKC-enabled Tegra device without access to the PKC. Option --bootloader, --soc and --pkc are also required when generating the blob.
Example:
tegrarcm --gen-signed-msgs --signed-msgs-file rel_1001.bin \
--bootloader u-boot.bin --loadaddr 0x83d88000 --soc 124 \
--pkc rsa_priv.derWhere generated signed message files are:
a) rel_1001.bin.qry
b) rel_1001.bin.ml
c) rel_1001.bin.bl
--- src/main.c | 259 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 240 insertions(+), 19 deletions(-)
src/main.c +240 -19
@@ -61,10 +61,14 @@// tegra124 miniloader #include "miniloader/tegra124-miniloader.h" -static int initialize_rcm (uint16_t devid,usb_device_t *usb,const char *pkc_keyfile); -static int initialize_miniloader (uint16_t devid,usb_device_t *usb,char *mlfile,uint32_t mlentry); +static int initialize_rcm (uint16_t devid,usb_device_t *usb, +const char *pkc_keyfile,const char *signed_msgs_file); +static int initialize_miniloader (uint16_t devid,usb_device_t *usb, +char *mlfile,uint32_t mlentry,const char *signed_msgs_file);static int wait_status (nv3p_handle_t h3p);static int send_file (nv3p_handle_t h3p,const char *filename); +static int create_miniloader_rcm (uint8_t *miniloader,uint32_t size, +uint32_t entry,const char *signed_msgs_file);static int download_miniloader (usb_device_t *usb,uint8_t *miniloader,uint32_t size,uint32_t entry);static void dump_platform_info (nv3p_platform_info_t *info);
@@ -73,6 +77,8 @@ static int download_bootloader(nv3p_handle_t h3p, char *filename, uint32_t entry, uint32_t loadaddr, const char *pkc_keyfile);static int read_bct (nv3p_handle_t h3p,char *filename); +static int sign_blob (const char *blob_filename,const char *pkc_keyfile, +const char *signed_msgs_file);static void set_platform_info (nv3p_platform_info_t *info);static uint32_t get_op_mode (void );
@@ -92,6 +98,9 @@ enum cmdline_opts {#ifdef HAVE_USB_PORT_MATCH OPT_USBPORTPATH , #endif +OPT_SIGN_MSGS , +OPT_SIGNED_MSGS_FILE , +OPT_SOC , OPT_END, };
@@ -134,7 +143,12 @@ static void usage(char *progname)fprintf (stderr,"\t--pkc=<key.ber>\n" );fprintf (stderr,"\t\tSpecify the key file for secured devices. The private key should be\n" );fprintf (stderr,"\t\tin DER format\n" ); - +fprintf (stderr,"\t--gen-signed-msgs\n" ); +fprintf (stderr,"\t\tGenerate signed messages for pkc secured devices\n" ); +fprintf (stderr,"\t--signed-msgs-file=<msg_file_prefix>\n" ); +fprintf (stderr,"\t\tSpecify message files prefix\n" ); +fprintf (stderr,"\t--soc=<tegra soc #>\n" ); +fprintf (stderr,"\t\tSpecify Tegra SoC chip model number, ie, 124.\n" );fprintf (stderr,"\n" ); }
@@ -168,6 +182,29 @@ static void parse_usb_port_path(char *argv0, char *path, uint8_t *match_bus, }#endif +#define ARRAY_SIZE (a) (sizeof(a) / sizeof((a)[0])) +static bool is_soc_supported_for_signed_msgs (uint32_t soc,uint16_t *devid) +{ +struct { +uint32_t soc; +uint16_t usb_devid; + } soc_to_devid[] = { + {114 ,USB_DEVID_NVIDIA_TEGRA114 }, + {124 ,USB_DEVID_NVIDIA_TEGRA124 }, + }; + +uint32_t i; + +for (i =0 ; i <ARRAY_SIZE (soc_to_devid); ++i) { +if (soc_to_devid[i].soc == soc) { + *devid = soc_to_devid[i].usb_devid; +return true; + } + } + +return false; +} +int main (int argc,char **argv) {// discover devices
@@ -194,6 +231,9 @@ int main(int argc, char **argv)uint8_t match_ports[PORT_MATCH_MAX_PORTS ];int match_ports_len;#endif +bool sign_msgs = false; +char *signed_msgs_file =NULL ; +uint32_t soc =0 ;static struct option long_options[] = { [OPT_BCT ] = {"bct" ,1 ,0 ,0 },
@@ -208,9 +248,11 @@ int main(int argc, char **argv) #ifdef HAVE_USB_PORT_MATCH [OPT_USBPORTPATH ] = {"usb-port-path" ,1 ,0 ,0 }, #endif + [OPT_SIGN_MSGS] = {"gen-signed-msgs", 0, 0, 0}, + [OPT_SIGNED_MSGS_FILE] = {"signed-msgs-file", 1, 0, 0}, + [OPT_SOC] = {"soc", 1, 0, 0}, [OPT_END ] = {0 ,0 ,0 ,0 } }; -// parse command line args while (1 ) { c = getopt_long(argc, argv, "h0",
@@ -254,6 +296,15 @@ int main(int argc, char **argv) match_port = true;break ;#endif +case OPT_SIGN_MSGS : + sign_msgs = true; +break ; +case OPT_SIGNED_MSGS_FILE : + signed_msgs_file = optarg; +break ; +case OPT_SOC : + soc =strtoul (optarg,NULL ,0 ); +break ;case OPT_HELP :default :usage (argv[0 ]);
@@ -279,12 +330,47 @@ int main(int argc, char **argv) optind++; } -if (bctfile ==NULL ) { +/* error check */ +if (sign_msgs == true) { +/* must have pkc option */ +if (pkc_keyfile ==NULL ) { +fprintf (stderr,"PKC key file must be specified\n" ); + goto usage_exit; + } + +/* must have signed_msgs_file option */ +if (signed_msgs_file ==NULL ) { +fprintf (stderr,"signed msgs filename must be" +" specified\n" ); + goto usage_exit; + } + +/* must have --soc option */ +if (soc ==0 ) { +fprintf (stderr,"soc model number must be" +" specified\n" ); + goto usage_exit; + } + } + +/* +* option --signed-msgs-file needs to work with option +* --gen-signed-msgs +*/ +if (signed_msgs_file && (sign_msgs == false)) { +fprintf (stderr,"missing option --gen-signed-msgs\n" ); + goto usage_exit; + } + +/* specify bct file if no sign_msgs option */ +if ((bctfile ==NULL ) && (sign_msgs == false)) {fprintf (stderr,"BCT file must be specified\n" );usage (argv[0 ]);exit (EXIT_FAILURE ); } -printf ("bct file: %s\n" , bctfile); + +if (bctfile) +printf ("bct file: %s\n" , bctfile);if (!do_read) {if (blfile ==NULL ) {
@@ -305,6 +391,33 @@ int main(int argc, char **argv)printf ("entry addr 0x%x\n" , entryaddr); } +/* if sign_msgs, generate signed msgs, then exit */ +if (sign_msgs == true) { +/* +* validate SoC value +* currently only verified soc is T124 +*/ +if (!is_soc_supported_for_signed_msgs (soc, &devid)) { +fprintf (stderr,"Unrecognized soc: %u\n" , soc); + goto usage_exit; + } + +// init and create signed query version rcm +if (initialize_rcm (devid,NULL , pkc_keyfile, signed_msgs_file)) +error (1 , errno,"error initializing RCM protocol" ); + +// create signed download miniloader rcm +if (initialize_miniloader (devid,NULL , mlfile, mlentry, + signed_msgs_file)) +error (1 , errno,"error initializing miniloader" ); + +// create bl signature +sign_blob (blfile, pkc_keyfile, signed_msgs_file); + +exit (0 ); + } + +/* start nv3p protocol */ usb =usb_open (USB_VENID_NVIDIA , &devid #ifdef HAVE_USB_PORT_MATCH , &match_port, &match_bus, match_ports, &match_ports_len
@@ -325,17 +438,20 @@ int main(int argc, char **argv)error (1 , errno,"USB read truncated" );// initialize rcm - ret2 =initialize_rcm (devid, usb, pkc_keyfile); + ret2 =initialize_rcm (devid, usb, pkc_keyfile, + signed_msgs_file);if (ret2)error (1 , errno,"error initializing RCM protocol" ); -// download the miniloader to start nv3p - ret2 =initialize_miniloader (devid, usb, mlfile, mlentry); +// download the miniloader to start nv3p or create ml rcm file + ret2 =initialize_miniloader (devid, usb, mlfile, mlentry, + signed_msgs_file);if (ret2)error (1 , errno,"error initializing miniloader" );// device may have re-enumerated, so reopen USB usb_close (usb); + usb =usb_open (USB_VENID_NVIDIA , &devid #ifdef HAVE_USB_PORT_MATCH , &match_port, &match_bus, match_ports, &match_ports_len
@@ -391,18 +507,53 @@ int main(int argc, char **argv)nv3p_close (h3p);usb_close (usb); +return 0 ; + +usage_exit: +usage (argv[0 ]); +exit (EXIT_FAILURE ); +} + +static int create_name_string (char *out,const char *in,const char *ext) +{ +if ((strlen (in) +strlen (ext) +1 ) >PATH_MAX ) { +fprintf (stderr,"error: name length %zu bytes exceed " +"limits for file %s\n" , +strlen (in) +strlen (ext) +1 -PATH_MAX , in); +return -1 ; + } +snprintf (out,PATH_MAX ,"%s%s" , in, ext); +return 0 ; +} + +static int save_to_file (const char *filename,const uint8_t *msg_buff, +const uint32_t length) +{ +FILE *fp; + +printf ("Create file %s...\n" , filename); + + fp =fopen (filename,"wb" ); +if (fp ==NULL ) { +fprintf (stderr,"Error opening raw file %s.\n" , filename); +return -1 ; + } + +fwrite (msg_buff,1 , length, fp); +fclose (fp);return 0 ; }static int initialize_rcm (uint16_t devid,usb_device_t *usb, -const char *pkc_keyfile) +const char *pkc_keyfile,const char *signed_msgs_file) { -int ret; +int ret =0 ;uint8_t *msg_buff;int msg_len;uint32_t status;int actual_len; +char query_version_rcm_filename[PATH_MAX ];// initialize RCM if ((devid & 0xff) == USB_DEVID_NVIDIA_TEGRA20 ||
@@ -427,6 +578,18 @@ static int initialize_rcm(uint16_t devid, usb_device_t *usb,// create query version message rcm_create_msg (RCM_CMD_QUERY_RCM_VERSION ,NULL ,0 ,NULL ,0 , &msg_buff); +/* save query version rcm blob */ +if (signed_msgs_file) { + ret =create_name_string (query_version_rcm_filename, + signed_msgs_file,".qry" ); +if (ret) + goto done; + + ret =save_to_file (query_version_rcm_filename, msg_buff, +rcm_get_msg_len (msg_buff)); + goto done; + } +// write query version message to device msg_len =rcm_get_msg_len (msg_buff);if (msg_len ==0 ) {
@@ -436,28 +599,31 @@ static int initialize_rcm(uint16_t devid, usb_device_t *usb, ret =usb_write (usb, msg_buff, msg_len);if (ret) {fprintf (stderr,"write RCM query version: USB transfer failure\n" ); -return ret; + goto done; } -free (msg_buff); - msg_buff =NULL ;// read response ret =usb_read (usb, (uint8_t *)&status,sizeof (status), &actual_len);if (ret) {fprintf (stderr,"read RCM query version: USB transfer failure\n" ); -return ret; + goto done; }if (actual_len <sizeof (status)) {fprintf (stderr,"read RCM query version: USB read truncated\n" ); -return EIO ; + ret =EIO ; + goto done; }printf ("RCM version: %d.%d\n" ,RCM_VERSION_MAJOR (status),RCM_VERSION_MINOR (status)); -return 0 ; +done: +if (msg_buff) +free (msg_buff); +return ret; } -static int initialize_miniloader (uint16_t devid,usb_device_t *usb,char *mlfile,uint32_t mlentry) +static int initialize_miniloader (uint16_t devid,usb_device_t *usb, +char *mlfile,uint32_t mlentry,const char *signed_msgs_file) {int fd;struct stat sb;
@@ -466,7 +632,7 @@ static int initialize_miniloader(uint16_t devid, usb_device_t *usb, char *mlfileuint32_t miniloader_size;uint32_t miniloader_entry; -// use prebuilt miniloader if not loading from a file +// if using miniloader from an external file if (mlfile) { fd =open (mlfile,O_RDONLY ,0 );if (fd <0 ) {
@@ -511,6 +677,12 @@ static int initialize_miniloader(uint16_t devid, usb_device_t *usb, char *mlfilereturn ENODEV ; } } + +if (signed_msgs_file) { +return create_miniloader_rcm (miniloader, miniloader_size, + miniloader_entry, signed_msgs_file); + } +printf ("downloading miniloader to target at address 0x%x (%d bytes)...\n" , miniloader_entry, miniloader_size); ret = download_miniloader(usb, miniloader, miniloader_size,
@@ -636,6 +808,30 @@ fail:return ret; } +static int create_miniloader_rcm (uint8_t *miniloader,uint32_t size, +uint32_t entry,const char *signed_msgs_file) +{ +uint8_t *msg_buff; +int ret =0 ; +char ml_rcm_filename[PATH_MAX ]; + +// create RCM_CMD_DL_MINILOADER blob +rcm_create_msg (RCM_CMD_DL_MINILOADER , (uint8_t *)&entry,sizeof (entry), + miniloader, size, &msg_buff); + + ret =create_name_string (ml_rcm_filename, signed_msgs_file,".ml" ); +if (ret) + goto done; + +// write to binary file +dprintf ("Write miniloader rcm to %s\n" , ml_rcm_filename); + + ret =save_to_file (ml_rcm_filename, msg_buff, +rcm_get_msg_len (msg_buff)); +done: +free (msg_buff); +return ret; +}static int download_miniloader (usb_device_t *usb,uint8_t *miniloader,uint32_t size,uint32_t entry)
@@ -906,6 +1102,31 @@ static int download_bootloader(nv3p_handle_t h3p, char *filename,return 0 ; } +static int sign_blob (const char *blob_filename,const char *pkc_keyfile, +const char *signed_msgs_file) +{ +int ret; +uint8_t rsa_pss_sig[RCM_RSA_SIG_SIZE ]; + +char signature_filename[PATH_MAX ]; + + ret =rsa_pss_sign_file (pkc_keyfile, blob_filename, rsa_pss_sig); +if (ret) { +fprintf (stderr,"error signing %s with %s\n" , + blob_filename, pkc_keyfile); +return ret; + } + +/* save signature to signed_msgs_file.bl */ + ret =create_name_string (signature_filename, signed_msgs_file,".bl" ); +if (ret) +return ret; + + ret =save_to_file (signature_filename, rsa_pss_sig, +sizeof (rsa_pss_sig)); +return ret; +} +static void set_platform_info (nv3p_platform_info_t *info) { g_platform_info = info;
Signature · 1 line
1.9.1
PATCH v3 3/4 Add option --download-signed-msgs to download signed blobs
In reply to a message not in the archiveThis feature allows user to download signed messages to devices secured with PKC.
Example:
tegrarcm --download-signed-msgs --signed-msgs-file rel_1001.bin \
--bct=jetson-tk1-bct.bct --bootloader=u-boot.bin --loadaddr=0x83d88000
Where the following blob files are downloaded to device sequentially:
a) rel_1001.bin.qry
b) rel_1001.bin.ml
c) rel_1001.bin.bl
--- src/main.c | 261 ++++++++++++++++++++++++++++++++++++++++-------------- src/tegrarcm.1.in | 118 ++++++++++++++++++++++-- 2 files changed, 305 insertions(+), 74 deletions(-)
src/main.c +196 -65
@@ -62,20 +62,23 @@#include "miniloader/tegra124-miniloader.h" static int initialize_rcm (uint16_t devid,usb_device_t *usb, -const char *pkc_keyfile,const char *signed_msgs_file); +const char *pkc_keyfile,const char *signed_msgs_file, +bool download_signed_msgs);static int initialize_miniloader (uint16_t devid,usb_device_t *usb, -char *mlfile,uint32_t mlentry,const char *signed_msgs_file); +char *mlfile,uint32_t mlentry,const char *signed_msgs_file, +bool download_signed_msgs);static int wait_status (nv3p_handle_t h3p);static int send_file (nv3p_handle_t h3p,const char *filename);static int create_miniloader_rcm (uint8_t *miniloader,uint32_t size,uint32_t entry,const char *signed_msgs_file);static int download_miniloader (usb_device_t *usb,uint8_t *miniloader, -uint32_t size,uint32_t entry); +uint32_t size,uint32_t entry, +bool download_signed_msgs);static void dump_platform_info (nv3p_platform_info_t *info);static int download_bct (nv3p_handle_t h3p,char *filename);static int download_bootloader (nv3p_handle_t h3p,char *filename, -uint32_t entry,uint32_t loadaddr, -const char *pkc_keyfile); +uint32_t entry,uint32_t loadaddr, +const char *pkc_keyfile,const char *signed_msgs_file);static int read_bct (nv3p_handle_t h3p,char *filename);static int sign_blob (const char *blob_filename,const char *pkc_keyfile,const char *signed_msgs_file);
@@ -101,6 +104,7 @@ enum cmdline_opts {OPT_SIGN_MSGS ,OPT_SIGNED_MSGS_FILE ,OPT_SOC , +OPT_DOWNLOAD_SIGNED_MSGS , OPT_END, };
@@ -149,6 +153,8 @@ static void usage(char *progname)fprintf (stderr,"\t\tSpecify message files prefix\n" );fprintf (stderr,"\t--soc=<tegra soc #>\n" );fprintf (stderr,"\t\tSpecify Tegra SoC chip model number, ie, 124.\n" ); +fprintf (stderr,"\t--download-signed-msgs\n" ); +fprintf (stderr,"\t\tDownload signed messages\n" );fprintf (stderr,"\n" ); }
@@ -234,6 +240,7 @@ int main(int argc, char **argv)bool sign_msgs = false;char *signed_msgs_file =NULL ;uint32_t soc =0 ; +bool download_signed_msgs = false;static struct option long_options[] = { [OPT_BCT ] = {"bct" ,1 ,0 ,0 },
@@ -251,6 +258,7 @@ int main(int argc, char **argv) [OPT_SIGN_MSGS] = {"gen-signed-msgs", 0, 0, 0}, [OPT_SIGNED_MSGS_FILE] = {"signed-msgs-file", 1, 0, 0}, [OPT_SOC] = {"soc", 1, 0, 0}, + [OPT_DOWNLOAD_SIGNED_MSGS] = {"download-signed-msgs", 0, 0, 0}, [OPT_END ] = {0 ,0 ,0 ,0 } };// parse command line args
@@ -305,6 +313,9 @@ int main(int argc, char **argv)case OPT_SOC : soc =strtoul (optarg,NULL ,0 );break ; +case OPT_DOWNLOAD_SIGNED_MSGS : + download_signed_msgs = true; +break ;case OPT_HELP :default :usage (argv[0 ]);
@@ -353,12 +364,36 @@ int main(int argc, char **argv) } } +/* error check */ +if (download_signed_msgs == true) { +/* must have signed_msgs_file option */ +if (signed_msgs_file ==NULL ) { +fprintf (stderr,"signed msgs filename must be" +" specified\n" ); + goto usage_exit; + } + +/* can not have pkc keyfile */ +if (pkc_keyfile) { +fprintf (stderr,"No pkc keyfile is needed\n" ); + goto usage_exit; + } + +/* can not load in unsigned ml */ +if (mlfile) { +fprintf (stderr,"can not have option --miniloader\n" ); + goto usage_exit; + } + } +/* * option --signed-msgs-file needs to work with option -* --gen-signed-msgs +* either --gen-signed-msgs or --download-signed-msgs */ -if (signed_msgs_file && (sign_msgs == false)) { -fprintf (stderr,"missing option --gen-signed-msgs\n" ); +if (signed_msgs_file && (sign_msgs == false) && + (download_signed_msgs == false)) { +fprintf (stderr,"missing option either --gen-signed-msgs or" +" --download-signed-msgs\n" ); goto usage_exit; }
@@ -403,12 +438,13 @@ int main(int argc, char **argv) }// init and create signed query version rcm -if (initialize_rcm (devid,NULL , pkc_keyfile, signed_msgs_file)) +if (initialize_rcm (devid,NULL , pkc_keyfile, signed_msgs_file, + download_signed_msgs))error (1 , errno,"error initializing RCM protocol" );// create signed download miniloader rcm if (initialize_miniloader (devid,NULL , mlfile, mlentry, - signed_msgs_file)) + signed_msgs_file, download_signed_msgs))error (1 , errno,"error initializing miniloader" );// create bl signature
@@ -439,13 +475,13 @@ int main(int argc, char **argv)// initialize rcm ret2 =initialize_rcm (devid, usb, pkc_keyfile, - signed_msgs_file); + signed_msgs_file, download_signed_msgs);if (ret2)error (1 , errno,"error initializing RCM protocol" );// download the miniloader to start nv3p or create ml rcm file ret2 =initialize_miniloader (devid, usb, mlfile, mlentry, - signed_msgs_file); + signed_msgs_file, download_signed_msgs);if (ret2)error (1 , errno,"error initializing miniloader" );
@@ -501,7 +537,8 @@ int main(int argc, char **argv) }// download the bootloader - ret =download_bootloader (h3p, blfile, entryaddr, loadaddr, pkc_keyfile); + ret =download_bootloader (h3p, blfile, entryaddr, loadaddr, + pkc_keyfile, signed_msgs_file);if (ret)error (1 , ret,"error downloading bootloader: %s" , blfile);
@@ -546,7 +583,8 @@ static int save_to_file(const char *filename, const uint8_t *msg_buff, }static int initialize_rcm (uint16_t devid,usb_device_t *usb, -const char *pkc_keyfile,const char *signed_msgs_file) +const char *pkc_keyfile,const char *signed_msgs_file, +bool download_signed_msgs) {int ret =0 ;uint8_t *msg_buff;
@@ -578,16 +616,55 @@ static int initialize_rcm(uint16_t devid, usb_device_t *usb,// create query version message rcm_create_msg (RCM_CMD_QUERY_RCM_VERSION ,NULL ,0 ,NULL ,0 , &msg_buff); -/* save query version rcm blob */ +/* if signed_msgs_file is given */ if (signed_msgs_file) { +int fd; +struct stat sb; + ret =create_name_string (query_version_rcm_filename, signed_msgs_file,".qry" );if (ret) goto done; - ret =save_to_file (query_version_rcm_filename, msg_buff, +/* either save query version rcm blob */ +if (download_signed_msgs == false) { + ret =save_to_file (query_version_rcm_filename, msg_buff,rcm_get_msg_len (msg_buff)); - goto done; + goto done; + } + +/* or download signed query version rcm blob */ +printf ("download signed query version rcm from file %s\n" , + query_version_rcm_filename); + +free (msg_buff);// release memory allocated by rcm_create_msg + msg_buff =NULL ; + + fd =open (query_version_rcm_filename,O_RDONLY ,0 ); +if (fd <0 ) { +fprintf (stderr,"error opening %s\n" , + query_version_rcm_filename); +return errno; + } + + ret =fstat (fd, &sb); +if (ret) { +fprintf (stderr,"error on fstat of %s\n" , + query_version_rcm_filename); +return ret; + } + msg_buff = (uint8_t *)malloc (sb.st_size); +if (!msg_buff) { +fprintf (stderr,"error allocating %zd bytes for query" +" rcm\n" , sb.st_size); +return errno; + } +if (read (fd, msg_buff, sb.st_size) != sb.st_size) { +fprintf (stderr,"error reading from %s\n" , + query_version_rcm_filename); +free (msg_buff); +return errno; + } }// write query version message to device
@@ -623,25 +700,68 @@ done: }static int initialize_miniloader (uint16_t devid,usb_device_t *usb, -char *mlfile,uint32_t mlentry,const char *signed_msgs_file) +char *mlfile,uint32_t mlentry,const char *signed_msgs_file, +bool download_signed_msgs) {int fd;struct stat sb;int ret; -uint8_t *miniloader; +uint8_t *miniloader =NULL ; +bool ml_buffer_alloc = false;uint32_t miniloader_size;uint32_t miniloader_entry; +char ml_rcm_filename[PATH_MAX ]; +char *filename =NULL ; + +// use prebuilt miniloader if not loading from a file +if ((devid &0xff ) ==USB_DEVID_NVIDIA_TEGRA20 ) { + miniloader = miniloader_tegra20; + miniloader_size =sizeof (miniloader_tegra20); + miniloader_entry =TEGRA20_MINILOADER_ENTRY ; + }else if ((devid &0xff ) ==USB_DEVID_NVIDIA_TEGRA30 ) { + miniloader = miniloader_tegra30; + miniloader_size =sizeof (miniloader_tegra30); + miniloader_entry =TEGRA30_MINILOADER_ENTRY ; + }else if ((devid &0xff ) ==USB_DEVID_NVIDIA_TEGRA114 ) { + miniloader = miniloader_tegra114; + miniloader_size =sizeof (miniloader_tegra114); + miniloader_entry =TEGRA114_MINILOADER_ENTRY ; + }else if ((devid &0xff ) ==USB_DEVID_NVIDIA_TEGRA124 ) { + miniloader = miniloader_tegra124; + miniloader_size =sizeof (miniloader_tegra124); + miniloader_entry =TEGRA124_MINILOADER_ENTRY ; + }else { +fprintf (stderr,"unknown tegra device: 0x%x\n" , devid); +return ENODEV ; + } + +// if loading unsigned ml from a file +if (mlfile) + filename = mlfile; + +// if loading signed ml rcm file +if (signed_msgs_file && (download_signed_msgs == true)) { + filename = ml_rcm_filename; + ret =create_name_string (ml_rcm_filename, signed_msgs_file, +".ml" ); +if (ret) + goto done; -// if using miniloader from an external file -if (mlfile) { - fd =open (mlfile,O_RDONLY ,0 ); +/* download signed ml rcm blob */ +printf ("download signed miniloader rcm from file %s\n" , + ml_rcm_filename); + } + +// loading ml or signed ml rcm from a file +if (filename) { + fd =open (filename,O_RDONLY ,0 );if (fd <0 ) { -dprintf ("error opening %s for reading\n" , mlfile); +dprintf ("error opening %s\n" , filename);return errno; } ret =fstat (fd, &sb);if (ret) { -dprintf ("error on fstat of %s\n" , mlfile); +dprintf ("error on fstat of %s\n" , filename);return ret; } miniloader_size = sb.st_size;
@@ -650,50 +770,37 @@ static int initialize_miniloader(uint16_t devid, usb_device_t *usb,dprintf ("error allocating %d bytes for miniloader\n" , miniloader_size);return errno; } + ml_buffer_alloc = true;if (read (fd, miniloader, miniloader_size) != miniloader_size) {dprintf ("error reading from miniloader file" );return errno; } - miniloader_entry = mlentry; - }else { -if ((devid &0xff ) ==USB_DEVID_NVIDIA_TEGRA20 ) { - miniloader = miniloader_tegra20; - miniloader_size =sizeof (miniloader_tegra20); - miniloader_entry =TEGRA20_MINILOADER_ENTRY ; - }else if ((devid &0xff ) ==USB_DEVID_NVIDIA_TEGRA30 ) { - miniloader = miniloader_tegra30; - miniloader_size =sizeof (miniloader_tegra30); - miniloader_entry =TEGRA30_MINILOADER_ENTRY ; - }else if ((devid &0xff ) ==USB_DEVID_NVIDIA_TEGRA114 ) { - miniloader = miniloader_tegra114; - miniloader_size =sizeof (miniloader_tegra114); - miniloader_entry =TEGRA114_MINILOADER_ENTRY ; - }else if ((devid &0xff ) ==USB_DEVID_NVIDIA_TEGRA124 ) { - miniloader = miniloader_tegra124; - miniloader_size =sizeof (miniloader_tegra124); - miniloader_entry =TEGRA124_MINILOADER_ENTRY ; - }else { -fprintf (stderr,"unknown tegra device: 0x%x\n" , devid); -return ENODEV ; - } } -if (signed_msgs_file) { -return create_miniloader_rcm (miniloader, miniloader_size, +// if entry is specified +if (mlentry) + miniloader_entry = mlentry; + +// if generating ml rcm file +if (signed_msgs_file && (download_signed_msgs == false)) { + ret =create_miniloader_rcm (miniloader, miniloader_size, miniloader_entry, signed_msgs_file); + goto done; }printf ("downloading miniloader to target at address 0x%x (%d bytes)...\n" , miniloader_entry, miniloader_size); ret =download_miniloader (usb, miniloader, miniloader_size, - miniloader_entry); + miniloader_entry, download_signed_msgs);if (ret) {fprintf (stderr,"Error downloading miniloader\n" );return ret; }printf ("miniloader downloaded successfully\n" ); - -return 0 ; +done: +if ((ml_buffer_alloc == true) && miniloader) +free (miniloader); +return ret; }static int wait_status (nv3p_handle_t h3p)
@@ -834,17 +941,24 @@ done: }static int download_miniloader (usb_device_t *usb,uint8_t *miniloader, -uint32_t size,uint32_t entry) +uint32_t size,uint32_t entry, +bool download_signed_msgs) { -uint8_t *msg_buff; +uint8_t *msg_buff =NULL ; +bool msg_buff_alloc = false;int ret;uint32_t status;int actual_len;// download the miniloader to the bootrom -rcm_create_msg (RCM_CMD_DL_MINILOADER , - (uint8_t *)&entry,sizeof (entry), miniloader, size, - &msg_buff); +if (download_signed_msgs == true)/* signed ml with rcm header */ + msg_buff = miniloader; +else { +rcm_create_msg (RCM_CMD_DL_MINILOADER , (uint8_t *)&entry, +sizeof (entry), miniloader, size, &msg_buff); + msg_buff_alloc = true; + } + ret =usb_write (usb, msg_buff,rcm_get_msg_len (msg_buff));if (ret) goto fail;
@@ -862,7 +976,8 @@ static int download_miniloader(usb_device_t *usb, uint8_t *miniloader, ret =0 ; fail: -free (msg_buff); +if ((msg_buff_alloc == true) && msg_buff) +free (msg_buff);return ret; }
@@ -1030,8 +1145,8 @@ out: }static int download_bootloader (nv3p_handle_t h3p,char *filename, -uint32_t entry,uint32_t loadaddr, -const char *pkc_keyfile) +uint32_t entry,uint32_t loadaddr, +const char *pkc_keyfile,const char *signed_msgs_file) {int ret;nv3p_cmd_dl_bl_t arg;
@@ -1069,8 +1184,8 @@ static int download_bootloader(nv3p_handle_t h3p, char *filename,// For fused board, the bootloader hash must be sent first if (get_op_mode () ==RCM_OP_MODE_ODM_SECURE_PKC ) { -/* sign and download */ if (pkc_keyfile) { +/* sign and download with --pkc option */ uint8_t rsa_pss_sig[RCM_RSA_SIG_SIZE ]; ret =rsa_pss_sign_file (pkc_keyfile, filename, rsa_pss_sig);
@@ -1081,15 +1196,31 @@ static int download_bootloader(nv3p_handle_t h3p, char *filename, } ret =nv3p_data_send (h3p, rsa_pss_sig,sizeof (rsa_pss_sig)); -if (ret) { -dprintf ("error sending bootloader signature\n" ); + + }else if (signed_msgs_file) { +/* download bl's signature */ +char signature_filename[PATH_MAX ]; + + ret =create_name_string (signature_filename, + signed_msgs_file,".bl" ); +if (ret)return ret; - } + +// send the bootloader's signature file + ret =send_file (h3p, signature_filename); + }else { -fprintf (stderr,"Error: missing pkc keyfile to sign" -" bootloader\n" ); +/* missing both options */ +dprintf ("error: missing both pkc keyfile and" +" bootloader's signature file\n" );return -1 ; } + +// check transfer status +if (ret) { +dprintf ("error sending bootloader signature\n" ); +return ret; + } }// send the bootloader file
src/tegrarcm.1.in +109 -9
148 lines not on this page · raw message
Signature · 1 line
1.9.1
PATCH v3 4/4 Add option --usb-timeout=<value>
In reply to a message not in the archiveRCM communication sometimes fails if the USB timeout is short. The default timeout is 1000ms. Increasing the timeout value may avoid this issue. The exact cause is not yet diagnosed. Value 0 means unlimited timeout.
Example:
$ sudo tegrarcm --bct=jetson-tk1-bct.bct --bootloader=u-boot.bin \ --loadaddr=0x83d88000 --pkc=rsa_priv.der --usb-timeout=5000
--- src/main.c | 8 ++++++++ src/usb.c | 7 ++++--- 2 files changed, 12 insertions(+), 3 deletions(-)
src/main.c +8 -0
@@ -87,6 +87,7 @@ static void set_platform_info(nv3p_platform_info_t *info);static uint32_t get_op_mode (void );static nv3p_platform_info_t *g_platform_info =NULL ; +extern uint32_t usb_timeout;enum cmdline_opts {OPT_BCT ,
@@ -105,6 +106,7 @@ enum cmdline_opts {OPT_SIGNED_MSGS_FILE ,OPT_SOC ,OPT_DOWNLOAD_SIGNED_MSGS , +OPT_USB_TIMEOUT , OPT_END, };
@@ -155,6 +157,8 @@ static void usage(char *progname)fprintf (stderr,"\t\tSpecify Tegra SoC chip model number, ie, 124.\n" );fprintf (stderr,"\t--download-signed-msgs\n" );fprintf (stderr,"\t\tDownload signed messages\n" ); +fprintf (stderr,"\t--usb-timeout=<timeout_ms>\n" ); +fprintf (stderr,"\t\tSpecify usb transfer timeout value in ms, 0 for unlimited timeout\n" );fprintf (stderr,"\n" ); }
@@ -259,6 +263,7 @@ int main(int argc, char **argv) [OPT_SIGNED_MSGS_FILE] = {"signed-msgs-file", 1, 0, 0}, [OPT_SOC] = {"soc", 1, 0, 0}, [OPT_DOWNLOAD_SIGNED_MSGS] = {"download-signed-msgs", 0, 0, 0}, + [OPT_USB_TIMEOUT] = {"usb-timeout", 1, 0, 0}, [OPT_END ] = {0 ,0 ,0 ,0 } };// parse command line args
@@ -316,6 +321,9 @@ int main(int argc, char **argv)case OPT_DOWNLOAD_SIGNED_MSGS : download_signed_msgs = true;break ; +case OPT_USB_TIMEOUT : + usb_timeout =strtoul (optarg,NULL ,0 ); +break ;case OPT_HELP :default :usage (argv[0 ]);
src/usb.c +4 -3
@@ -1,5 +1,5 @@/* -* Copyright (c) 2011, NVIDIA CORPORATION +* Copyright (c) 2011-2016, NVIDIA CORPORATION * All rights reserved. * * Redistribution and use in source and binary forms, with or without
@@ -39,6 +39,7 @@#define USB_XFER_MAX 4096 +uint32_t usb_timeout =USB_TIMEOUT ;// // returns 1 if the specified usb device matches the vendor id //
@@ -307,7 +308,7 @@ int usb_write(usb_device_t *usb, uint8_t *buf, int len)while (len) { chunk_size =MIN (len,USB_XFER_MAX ); ret =libusb_bulk_transfer (usb->handle, usb->endpt_out, buf, - chunk_size, &actual_chunk,USB_TIMEOUT ); + chunk_size, &actual_chunk, usb_timeout);if (ret !=LIBUSB_SUCCESS ) {dprintf ("libusb write failure: %d: %s\n" , ret,libusb_error_name (ret));return EIO ;
@@ -334,7 +335,7 @@ int usb_read(usb_device_t *usb, uint8_t *buf, int len, int *actual_len)while (len) { chunk_size =MIN (len,USB_XFER_MAX ); ret =libusb_bulk_transfer (usb->handle, usb->endpt_in, buf, - chunk_size, &actual_chunk,USB_TIMEOUT ); + chunk_size, &actual_chunk, usb_timeout);if (ret !=LIBUSB_SUCCESS ) {dprintf ("libusb read failure: %d: %s\n" , ret,libusb_error_name (ret));return EIO ;
Signature · 1 line
1.9.1
Quoted 8 lines · Jimmy Zhang
Alban Bedel (1):
Add support for production devices secured with PKCJimmy Zhang (3):
Add option --gen-signed-msgs and --signed-msgs-file to generate signed
blobs
Add option --download-signed-msgs to download signed blobs Add option --usb-timeout=<value>
I've applied these now, and will push out a little later.