3030#include <unistd.h>
3131#include <errno.h>
3232
33- /* Define a generic max OTP size to appease otp_keystore.h */
34- #ifndef OTP_SIZE
33+ /* Define a generic max OTP size to appease otp_keystore.h when no target is set. */
34+ #if !defined( OTP_SIZE ) && !defined( TARGET_stm32h7 ) && !defined( TARGET_stm32h5 )
3535#define OTP_SIZE 4096
3636#endif
3737
@@ -51,6 +51,11 @@ int main(void)
5151 uint32_t tot_len ;
5252 int ofd ;
5353 int slot_size ;
54+ uint8_t * otp_buf = NULL ;
55+ uint8_t uds [OTP_UDS_LEN ];
56+ size_t offset ;
57+ int rand_fd ;
58+ ssize_t rlen ;
5459
5560 memcpy (hdr .keystore_hdr_magic , KEYSTORE_HDR_MAGIC , 8 );
5661 hdr .item_count = n_keys ;
@@ -68,31 +73,65 @@ int main(void)
6873 slot_size += KEYSTORE_HDR_SIZE ;
6974 fprintf (stderr , "Slot size: %d\n" , slot_size );
7075 fprintf (stderr , "Number of slots: %d\n" , n_keys );
71- fprintf (stderr , "%s size: %d\n" , outfile , (slot_size * n_keys ) +
72- (int )sizeof (struct wolfBoot_otp_hdr ));
76+ tot_len = (uint32_t )sizeof (struct wolfBoot_otp_hdr ) +
77+ (uint32_t )(slot_size * n_keys );
78+ fprintf (stderr , "%s keystore size: %u\n" , outfile , tot_len );
79+ if (tot_len > OTP_UDS_OFFSET ) {
80+ fprintf (stderr ,
81+ "Error: keystore size %u exceeds OTP UDS offset %u\n" ,
82+ tot_len , (unsigned )OTP_UDS_OFFSET );
83+ exit (1 );
84+ }
85+
86+ otp_buf = (uint8_t * )malloc (OTP_SIZE );
87+ if (otp_buf == NULL ) {
88+ fprintf (stderr , "Error: out of memory allocating OTP buffer\n" );
89+ exit (1 );
90+ }
91+ memset (otp_buf , 0xFF , OTP_SIZE );
92+
93+ memcpy (otp_buf , & hdr , sizeof (hdr ));
7394
7495 ofd = open (outfile , O_WRONLY |O_CREAT |O_TRUNC , 0600 );
7596 if (ofd < 0 ) {
7697 perror ("opening output file" );
98+ free (otp_buf );
7799 exit (2 );
78100 }
79101
80- /* Write the header to the beginning of the OTP binary file */
81- if (write (ofd , & hdr , sizeof (hdr )) != sizeof (hdr )) {
82- fprintf (stderr , "Error writing to %s: %s\n" , outfile , strerror (errno ));
83- }
84-
85102 for (i = 0 ; i < n_keys ; i ++ ) {
86103 /* Write each public key to its slot in OTP */
87- if (write (ofd , & PubKeys [i ],
88- slot_size ) < 0 ) {
89- fprintf (stderr , "Error adding key %d to %s: %s\n" , i , outfile ,
90- strerror (errno ));
91- exit (3 );
92- }
104+ offset = sizeof (hdr ) + (size_t )i * (size_t )slot_size ;
105+ memcpy (otp_buf + offset , & PubKeys [i ], (size_t )slot_size );
106+ }
107+
108+ rand_fd = open ("/dev/urandom" , O_RDONLY );
109+ if (rand_fd < 0 ) {
110+ perror ("opening /dev/urandom" );
111+ close (ofd );
112+ free (otp_buf );
113+ exit (4 );
114+ }
115+ rlen = read (rand_fd , uds , sizeof (uds ));
116+ close (rand_fd );
117+ if (rlen != (ssize_t )sizeof (uds )) {
118+ fprintf (stderr , "Error: failed to read random UDS (%zd)\n" , rlen );
119+ close (ofd );
120+ free (otp_buf );
121+ exit (5 );
122+ }
123+
124+ memcpy (otp_buf + OTP_UDS_OFFSET , uds , sizeof (uds ));
125+
126+ if (write (ofd , otp_buf , OTP_SIZE ) != OTP_SIZE ) {
127+ fprintf (stderr , "Error writing to %s: %s\n" , outfile , strerror (errno ));
128+ close (ofd );
129+ free (otp_buf );
130+ exit (3 );
93131 }
94132 fprintf (stderr , "%s successfully created.\nGoodbye.\n" , outfile );
95133 close (ofd );
134+ free (otp_buf );
96135
97136 return 0 ;
98137}
0 commit comments