Part Number: AM2612-Q1
Other Parts Discussed in Thread: SYSCONFIG
Hi expert,
Please provide ways the SBL Image size can be reduced.
Applies to: AM263Px, AM261x, AM263x - MCU+ SDK sbl_ospi_multicore_elf (the same approach applies to other SBL examples that embed the HSM runtime)
Q1. My SBL .tiimage is around 300 KB, but most of it is zeros. Is this expected?
It is not a limitation of the image-generation tools, and it is not a problem with your build setup. It comes from the memory layout used in the example's SysConfig.
The SBL embeds the HSM runtime (HSMRT) as read-only data (section .rodata.hsmrt) so it can be handed to the HSM at boot. In the default layout, this section is placed far above the rest of the SBL:
Device HSMRT size HSMRT placed at
AM263Px 43,124 B 0x70042000
AM261x 28,003 B 0x70052100
When the .out file is converted to a .bin, objcopy writes one continuous block from the lowest loaded address to the highest. ROM copies this file byte for byte to 0x70002000, so a byte's position in the file is its address in RAM. Any address range between two loaded sections is therefore written as zeros, even if it only holds uninitialized data (.bss, stacks, heap).
Default layout (AM263Px, release build):
0x70002000 0x700141E0 0x70042000 0x7004C874
| vectors + code/data/ | .bss / stacks / heap | unused | HSM runtime |
| rodata (~72 KB) | (~91 KB) | (~92 KB) | (~42 KB) |
|<---------------------- written to .bin (~298 KB, ~64% zeros) ---------------->|
|<-------- 187,936 B of zero fill ------->|
Because the HSM runtime is at a fixed address, the image size is almost the same for debug and release builds. A smaller build only means a larger share of zeros.
Q2. How do I confirm this on my own build?
1. Open <sbl>.map and go to the "SEGMENT ALLOCATION MAP".
2. Find .rodata.hsmrt and note its address.
3. Find where .rodata ends (the last segment with a non-zero "init length" below the HSM runtime).
4. Everything between those two addresses is written to the .bin as zeros. Segments with "init length" 0 (.bss, .stack, .sysmem, *stack) are not loaded data.
The .bin size equals (end of .rodata.hsmrt - 0x70002000).
Q3. How do I reduce the image size?
Place the HSM runtime directly after the vectors, and the main SBL region after it. All loaded content is then contiguous, and the uninitialized sections come after the last loaded byte, so they are not part of the image.
Reordered layout (AM263Px, release build):
0x70002000 0x70002100 0x7000CA00 0x7001EAE0 0x70035820
| vectors | HSM runtime | code / data / rodata | .bss / stacks / heap |
| | (~42 KB) | (~72 KB) | (not loaded - omitted)|
|<------------- written to .bin (~115 KB) -------->|
Changes in example.syscfg (Memory Configurator -> Memory Region):
AM263Px:
region1.memory_region[4].$name = "MSRAM_0";
region1.memory_region[4].auto = false;
region1.memory_region[4].manualStartAddress = 0x7000CA00;
region1.memory_region[4].size = 0x33600;
region1.memory_region[5].$name = "MSRAM_HSMRT";
region1.memory_region[5].auto = false;
region1.memory_region[5].manualStartAddress = 0x70002100;
region1.memory_region[5].size = 0xA900;
AM261x:
region1.memory_region[4].$name = "MSRAM_0";
region1.memory_region[4].auto = false;
region1.memory_region[4].manualStartAddress = 0x70009100;
region1.memory_region[4].size = 0x36F00;
region1.memory_region[5].$name = "MSRAM_HSMRT";
region1.memory_region[5].auto = false;
region1.memory_region[5].manualStartAddress = 0x70002100;
region1.memory_region[5].size = 0x7000;
No changes to main.c or the linker sections are needed. The .rodata.hsmrt section is already assigned to MSRAM_HSMRT.
Q4. What does "auto = false" do, and why is it required?
"auto" is the "Calculate Start Address" checkbox of a memory region in SysConfig.
auto = true (default, box ticked) : SysConfig ignores manualStartAddress and places the region in the smallest free gap that fits it.
auto = false (box unticked) : SysConfig places the region exactly at manualStartAddress.
If "auto = false" is missing, the start address you enter has no effect. In the GUI, the Start Address field only becomes editable after you untick "Calculate Start Address".
Also note that SysConfig places manual regions first. A manual region that overlaps another region cannot be placed and causes an error. This is why MSRAM_0 has to be moved and resized together with MSRAM_HSMRT.
Q5. How do I choose the addresses and sizes?
1. HSM runtime size: HSMRT_IMG_SIZE_IN_BYTES in
source/security/security_common/drivers/hsmclient/soc/<device>/hsmRtImg.h
2. MSRAM_HSMRT: start at 0x70002100 (right after MSRAM_VECS). Size = HSMRT size rounded up (e.g. to 0x100). Any space left unused here becomes zero padding in the image, so keep it tight.
3. MSRAM_0: start at the end of MSRAM_HSMRT, end at 0x70040000. Applications start at 0x70040000 by default, and the SBL's code, data, .bss and stacks must not overlap memory the application loads into while the SBL is still running.
4. After building, check the map file: the last SBL segment (usually .undefinedstack) must end below 0x70040000.
5. If the HSM runtime image changes in a future SDK, check the region size again.
Q6. What is gPHTBuffer, and can I reduce it?
gPHTBuffer (source/drivers/bootloader/bootloader.c) holds the program header table (PHT) of the application ELF while the SBL loads and authenticates it. Each PHT entry describes one segment to be loaded.
The buffer is sized for the worst case that the parser accepts:
ELF_MAX_SEGMENTS x ELF_P_HEADER_MAX_SIZE = 1024 x 56 B = 57,344 B (~56 KB)
The size actually needed is:
(number of program headers) x 32 B for 32-bit R5 images
Typical images need far less. For example:
Application Program headers PHT size
hello_world (single core) 4 128 B
ipc_notify_echo (multicore) 6 192 B
To check your own application, run on the final .mcelf:
tiarmreadelf -h <app>.mcelf
and look at "Number of program headers" and "Size of program headers".
To reduce the buffer, change in source/drivers/bootloader/bootloader_elf.h:
#define ELF_MAX_SEGMENTS (32U) /* or 64U */
then rebuild the drivers library (both debug and release profiles) and the SBL. The value is compiled into the library, so changing it only in the SBL project has no effect.
Leave margin for future application updates. If an application has more program headers than ELF_MAX_SEGMENTS, the SBL rejects it and it will not boot.
Q7. I reduced gPHTBuffer, but the image size did not change. Why?
gPHTBuffer is uninitialized data (.bss). With the reordered layout (Q3), .bss comes after the last loaded byte and is not part of the image.
In the default layout it does not reduce the image either, because the image size is set by the position of the HSM runtime (Q1).
Sample AM263P CC Project with modified Sysconfig
https://e2e.ti.com/cfs-file/__key/communityserver-discussions-components-files/908/4863.example.syscfg
Sample AM261 LP Project with modified Sysconfig
https://e2e.ti.com/cfs-file/__key/communityserver-discussions-components-files/908/4863.example.syscfg