cancel
Showing results for 
Search instead for 
Did you mean: 

SBSFU for STM32F407 - Adjusting post/pre build .BAT scripts when creating new projects...

Zaher
Senior II

I have finally managed to get the SBSFU to work on my STM32F4-Discovery board. I've been working on this over the past month until I figured it out and got it to work. For some reason, I couldn't get the 2_Image version to work on my hardware. I believe the FW header was overlapping with one of the Memory Slots' Regions on flash sectors. Will try to get back to this later, however, the 1_Image version worked fine.

Because I wanted to understand the structure of the 3-project based SBSFU lib, I started with new projects from scratch and then added all files, dependencies, IDE configurations, scatter and asm files, and finally modified the .BAT files according to the new location of my projects.

The first two projects worked flawlessly, however, I couldn't imitate the same process for the third project, UserApp, where the customer's firmware merges with the SBSFU core. The project compiles successfully, but the postbuild .BAT script fails to complete the process and no binaries are generated in "Binary" folder. This is only happening in the new project that I have created in a new location. The UserApp project inside the SBSFU demo package compiles and generates the binaries as expected.

I know that the .BAT postbuild script has something to do about it, but no sure what causes the problem as I'm not very familiar with Windows Batch files and their syntax.

The postbuild script throws this error when invoked from within the IDE:

"The syntax of the command is incorrect."

This is also from the output.txt file inside the project:

usage: prepareimage.exe [-h]
                        {keygen,trans,getpub,sign,sha256,conf,enc,header,pack,merge}
                        ...
prepareimage.exe: error: unrecognized arguments: .\UserApp.bin .\\..\\Binary\\\\UserApp.sfu
"C:\Users\admin\STM32_WS\Middlewares\ST\STM32_Secure_Engine\Utilities\KeysAndImages\\win\\prepareimage\\prepareimage.exe enc -k C:\Users\admin\STM32_WS\CubeMX Generated\STM32F407_SBSFU_UserFW\MDK-ARM\\\..\\..\\SECoreBin\\Binary\\OEM_KEY_COMPANY1_key_AES_CBC.bin -i C:\Users\admin\STM32_WS\CubeMX Generated\STM32F407_SBSFU_UserFW\MDK-ARM\\\..\\..\\SECoreBin\\Binary\\iv.bin .\UserApp.bin .\\..\\Binary\\\\UserApp.sfu  > .\output.txt 2>&1 : failed" 

This is also the postbuild .BAT file after modification:

::Post build for SECBOOT_ECCDSA_WITH_AES128_CBC_SHA256
:: arg1 is the build directory
:: arg2 is the elf file path+name
:: arg3 is the bin file path+name
:: arg4 is the version
:: arg5 when present forces "bigelf" generation
@echo off
set "projectdir=%1"
set "execname=%~n3"
set "elf=%2"
set "bin=%3"
set "version=%4"
 
set "SBSFUBootLoader=%~d0%~p0\\..\\.."
::The default installation path of the Cube Programmer tool is: "C:\Program Files\STMicroelectronics\STM32Cube\STM32CubeProgrammer\bin"
::If you installed it in another location, please update the %programmertool% variable below accordingly.
set "programmertool="C:\\Program Files\\STMicroelectronics\\STM32Cube\\STM32CubeProgrammer\\bin\\STM32_Programmer_CLI""
set "userAppBinary=%projectdir%\\..\\Binary\\"
 
set "sfu=%userAppBinary%\\%execname%.sfu"
set "sfb=%userAppBinary%\\%execname%.sfb"
set "sign=%userAppBinary%\\%execname%.sign"
set "headerbin=%userAppBinary%\\%execname%sfuh.bin"
set "bigbinary=%userAppBinary%\\SBSFU_%execname%.bin"
set "elfbackup=%userAppBinary%\\SBSFU_%execname%.elf"
 
set "iv=%SBSFUBootLoader%\\SECoreBin\\Binary\\iv.bin"
set "oemkey=%SBSFUBootLoader%\\SECoreBin\\Binary\\OEM_KEY_COMPANY1_key_AES_CBC.bin"
set "ecckey=%SBSFUBootLoader%\\SECoreBin\\Binary\\ECCKEY.txt"
set "sbsfuelf=%SBSFUBootLoader%\\SBSFU\\SBSFU.axf"
 
::comment this line to force python
::python is used if windows executeable not found
pushd %projectdir%\..\..\..\Middlewares\ST\STM32_Secure_Engine\Utilities\KeysAndImages
set basedir=%cd%
popd
goto exe:
goto py:
:exe
::line for window executeable
echo Postbuild with windows executable
set "prepareimage=%basedir%\\win\\prepareimage\\prepareimage.exe"
set "python="
if exist %prepareimage% (
goto postbuild
)
:py
::line for python
echo Postbuild with python script
set "prepareimage=%basedir%\\prepareimage.py"
set "python=python "
:postbuild
 
::Make sure we have a Binary sub-folder in UserApp folder
if not exist "%userAppBinary%" (
mkdir "%userAppBinary%"
)
 
::PostBuild is fired if elf has been regenerated from last run, so elf is different from backup elf
if exist %elfbackup% (
fc %elfbackup% %elf% >NUL && (goto nothingtodo) || echo "elf has been modified, processing required"
)
 
set "command=%python%%prepareimage% enc -k %oemkey% -i %iv% %bin% %sfu%  > %projectdir%\output.txt 2>&1"
%command%
IF %ERRORLEVEL% NEQ 0 goto :error
 
set "command=%python%%prepareimage% sha256 %bin% %sign% >> %projectdir%\output.txt 2>&1"
%command%
IF %ERRORLEVEL% NEQ 0 goto :error
 
set "command=%python%%prepareimage% pack -k %ecckey%  -r 4 -v %version% -i %iv% -f %sfu% -t %sign% %sfb% -o 512 >> %projectdir%\output.txt 2>&1"
%command%
IF %ERRORLEVEL% NEQ 0 goto :error
 
set "command=%python%%prepareimage% header -k  %ecckey% -r 4 -v %version%  -i %iv% -f %sfu% -t %sign% -o 512 %headerbin% >> %projectdir%\output.txt 2>&1"
%command%
IF %ERRORLEVEL% NEQ 0 goto :error
 
set "command=%python%%prepareimage% merge -i %headerbin% -s %sbsfuelf% %elf% %bigbinary% >> %projectdir%\output.txt 2>&1"
%command%
IF %ERRORLEVEL% NEQ 0 goto :error
 
IF  "%~5"=="" goto :finish
echo "Generating the global elf file (SBSFU and userApp)"
echo "Generating the global elf file (SBSFU and userApp)" >> %projectdir%\output.txt
set "command=%programmertool% -ms %elf% %headerbin% %sbsfuelf% >> %projectdir%\output.txt 2>&1"
%command%
IF %ERRORLEVEL% NEQ 0 goto :error
set "command=copy %elf% %elfbackup%  >> %projectdir%\output.txt 2>&1"
%command%
IF %ERRORLEVEL% NEQ 0 goto :error
 
:finish
::backup and clean up the intermediate file
del %sign%
del %sfu%
del %headerbin%
 
exit 0
 
:error
echo "%command% : failed" >> %projectdir%\\output.txt
:: remove the elf to force the regeneration
if exist %elf%(
  del %elf%
)
if exist %elfbackup%(
  del %elfbackup%
)
echo %command% : failed
 
pause
exit 1
 
:nothingtodo
exit 0

By the way, I have created SBSFU folder in the root directory of the project (where the postbuild script resides) and copied the SBSFU.axf file from the SBSFU project there, and also created SECoreBin/Binary folder and copied all of the ECCKEY, iv.bin, and OEM_Key files inside it as well.

Hopefully someone can help on this!

Thanks

0 REPLIES 0