2727#include "hal.h"
2828#include "otp_keystore.h"
2929
30+ #ifdef NO_FLASH_OTP_KEYSTORE_TARGET
31+ /* See otp_keystore.h */
32+ #ifndef _MSC_VER
33+ #warning "No device target defined and no TARGET_sim"
34+ #else
35+ #pragma message("Warning: No device target defined and no TARGET_sim")
36+ #endif
37+ #endif
38+
39+ #ifdef TARGET_sim
40+ #include <stdio.h>
41+ #define SIM_PRINTF (...) \
42+ do { \
43+ printf(__VA_ARGS__); \
44+ fflush(stdout); \
45+ } while (0)
46+ #else
47+ #define SIM_PRINTF (...) do {} while (0)
48+ #endif
49+
3050extern struct keystore_slot PubKeys [];
3151
3252void main (void )
@@ -37,6 +57,8 @@ void main(void)
3757 uint32_t tot_len ;
3858
3959 hal_init ();
60+ SIM_PRINTF ("[primer] hal_init() done\n" );
61+ SIM_PRINTF ("[primer] detected %d public key(s)\n" , n_keys );
4062
4163 memcpy (hdr .keystore_hdr_magic , KEYSTORE_HDR_MAGIC , 8 );
4264 hdr .item_count = n_keys ;
@@ -45,13 +67,20 @@ void main(void)
4567
4668 /* Sanity check to avoid writing an empty keystore */
4769 if (n_keys < 1 ) {
70+ #ifdef TARGET_sim
71+ SIM_PRINTF ("Error: too few keys (%d), refusing to write\n" , n_keys );
72+ exit (1 );
73+ #else
4874 while (1 )
4975 ;
76+ /* no exit */
77+ #endif
5078 }
5179
5280 /* Write the header to the beginning of the OTP memory */
5381 hal_flash_otp_write (FLASH_OTP_BASE , (uint16_t * )& hdr , sizeof (hdr ));
54-
82+ SIM_PRINTF ("[primer] wrote OTP header at 0x%08lX (size %lu)\n" ,
83+ (unsigned long )FLASH_OTP_BASE , (unsigned long )sizeof (hdr ));
5584 for (i = 0 ; i < n_keys ; i ++ ) {
5685 /* Write each public key to its slot in OTP */
5786 hal_flash_otp_write (FLASH_OTP_BASE +
@@ -67,8 +96,13 @@ void main(void)
6796#endif
6897 (void )tot_len ;
6998
99+ #ifdef TARGET_sim
100+ SIM_PRINTF ("Done!\n" );
101+ exit (0 );
102+ #else
70103 /* Done! */
71104 while (1 )
72105 ;
73-
106+ /* no exit */
107+ #endif
74108}
0 commit comments