﻿*************************************
How to use the scripts 
*************************************
1. build_key_from_txt_gcc.bat

   This script can be used to generate the execute only binary code that will load certain
   AES 128 key to the CRYP HW. 
   
   The content of the AES 128 key is provided as an input file 
   in text format presentating the 16 bytes of AES key in hex.
	
   For example, if the AES key is 7fddb57453c241d03efbed3ac44e371c in hex 
   The content of the input key text file is just the same string 7fddb57453c241d03efbed3ac44e371c
   
   One parameters to be given when running the script in windows command console

   param: <AES key text file>
   
   Example of usage: 
   
	> build_key_from_txt_gcc.bat test_keys\aes_128_test_key.txt

	You will see some log as follows in the console. For H7, choose option 3 to let the script 
	build the code for M7 core. 
	After execution, a key binary ready to be programmed to the flash is generated, 	
	e.g. aes_128_test_key_prog.bin. 
	
	=================================
	Core type options
	1) M0+
	2) M4
	3) M7
	=================================
	Please enter your choice[1,2 or 3]: 3
	Convert aes-128 key into CM7 executable binary
	Please wait...
	Convert txt key file to binary format...
	Convert key binary to instructions...
	Input Length = 102
	Output Length = 42
	CertUtil: -decodehex command completed successfully.
	Done.
	Generated binary file to be programmed:
	C:\MMS\MMS-MCD\LAT\H7\H7KeyProt\Utils\test_keys\\aes_128_test_key_prog.bin
	00000000: 3eb4 4bf2 7451 c7f6 dd71 44f2 d012 c5f2  >.K.tQ...qD.....
	00000010: c232 4ef6 3a53 c3f6 fb63 43f2 1c74 ccf2  .2N.:S...cC..t..
	00000020: 4e44 80e8 1e00 3ebc 7047                 ND....>.pG
	Press any key to continue . . .
    

2. prog_key_binary.bat 

   This script can be used to generate the combined binary of Boot and key binary into 
   a single hex and program to the 1 block of H7 internal flsh
   
   Two parameters to be given to the script:
   param1: <Boot hex file>
   param2: <Generated Key binary file>

   Example of usage: 
		> prog_key_binary.bat ..\Projects\Boot\EWARM\STM32H755ZI_Nucleo_Boot\Exe\Boot.hex test_keys\aes_128_test_key_prog.bin

   Log on console will be something like follows
   
	prog_key_binary.bat ..\Projects\Boot\EWARM\STM32H755ZI_Nucleo_Boot\Exe\Boot.hex test_keys\aes_128_test_key_prog.bin
ExtTools\srec_cat.exe ..\Projects\Boot\EWARM\STM32H755ZI_Nucleo_Boot\Exe\Boot.hex -intel test_keys\aes_128_test_key_prog.bin -binary -offset 0x08008000 -o "combined.hex" -intel
"Write binary "combined.hex" to address 0x08000000"
      -------------------------------------------------------------------
                       STM32CubeProgrammer v2.7.0
      -------------------------------------------------------------------

ST-LINK SN  : 0047001E3137510C39383538
ST-LINK FW  : V3J7M3
Board       : NUCLEO-H755ZI-Q
Voltage     : 3.31V
SWD freq    : 24000 KHz
Connect mode: Under Reset
Reset mode  : Hardware reset
Device ID   : 0x450
Revision ID : Rev V
Device name : STM32H7xx
Flash size  : 2 MBytes
Device type : MCU
Device CPU  : Cortex-M7/M4



Warning: Write address is ignored for hex, Srec and elf files


Memory Programming ...
Opening and parsing file: combined.hex
  File          : combined.hex
  Size          : 25619 Bytes
  Address       : 0x08000000


Erasing memory corresponding to segment 0:
Erasing internal memory sector 0
Erasing memory corresponding to segment 1:
Erasing internal memory sector 0
Download in Progress:
██████████████████████████████████████████████████ 100%

File download complete
Time elapsed during download operation: 00:00:01.805



Verifying ...


Read progress:
██████████████████████████████████████████████████ 100%

Download verified successfully


      -------------------------------------------------------------------
                       STM32CubeProgrammer v2.7.0
      -------------------------------------------------------------------

ST-LINK SN  : 0047001E3137510C39383538
ST-LINK FW  : V3J7M3
Board       : NUCLEO-H755ZI-Q
Voltage     : 3.31V
SWD freq    : 24000 KHz
Connect mode: Under Reset
Reset mode  : Hardware reset
Device ID   : 0x450
Revision ID : Rev V
Device name : STM32H7xx
Flash size  : 2 MBytes
Device type : MCU
Device CPU  : Cortex-M7/M4


Reading 32-bit memory content
  Size          : 256 Bytes
  Address:      : 0x08000000

0x08000000 : 20008210 0800610D 08005DF9 08005DFB
0x08000010 : 08005DFD 08005DFF 08005E01 00000000
0x08000020 : 00000000 00000000 00000000 08005E03
0x08000030 : 08005E05 00000000 08005E07 08005E09
0x08000040 : 08006155 08006159 0800615D 08006161
0x08000050 : 08006165 08006169 0800616D 08006171
0x08000060 : 08006175 08006179 0800617D 08006181
0x08000070 : 08006185 08006189 0800618D 08006191
0x08000080 : 08006195 08006199 0800619D 080061A1
0x08000090 : 080061A5 080061A9 080061AD 080061B1
0x080000A0 : 080061B5 080061B9 080061BD 080061C1
0x080000B0 : 080061C5 080061C9 080061CD 080061D1
0x080000C0 : 080061D5 080061D9 080061DD 080061E1
0x080000D0 : 080061E5 080061E9 080061ED 080061F1
0x080000E0 : 08005E11 080061F5 00000000 080061F9
0x080000F0 : 080061FD 08006201 08006205 08006209
      -------------------------------------------------------------------
                       STM32CubeProgrammer v2.7.0
      -------------------------------------------------------------------

ST-LINK SN  : 0047001E3137510C39383538
ST-LINK FW  : V3J7M3
Board       : NUCLEO-H755ZI-Q
Voltage     : 3.31V
SWD freq    : 24000 KHz
Connect mode: Under Reset
Reset mode  : Hardware reset
Device ID   : 0x450
Revision ID : Rev V
Device name : STM32H7xx
Flash size  : 2 MBytes
Device type : MCU
Device CPU  : Cortex-M7/M4


Reading 32-bit memory content
  Size          : 256 Bytes
  Address:      : 0x08008000

0x08008000 : F24BB43E F6C75174 F24471DD F2C512D0
0x08008010 : F64E32C2 F6C3533A F24363FB F2CC741C
0x08008020 : E880444E BC3E001E FFFF4770 FFFFFFFF
0x08008030 : FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFF
0x08008040 : FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFF
0x08008050 : FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFF
0x08008060 : FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFF
0x08008070 : FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFF
0x08008080 : FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFF
0x08008090 : FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFF
0x080080A0 : FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFF
0x080080B0 : FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFF
0x080080C0 : FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFF
0x080080D0 : FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFF
0x080080E0 : FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFF
0x080080F0 : FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFF
Press any key to continue . . .
