Skip to main content
Visitor II
November 30, 2024
Solved

Create Factory Image "openblt bootloader and user applicaiton"

  • November 30, 2024
  • 1 reply
  • 3150 views

Hello, 

i am struggling with creating an working image of my openblt bootloader and my user application.

When i just flash the bootloader, and upload the user application via bootloader pipeline everything works fine.

But when i try to use srec_cat  or other merge tools the application will not start.

because the bootloader does not accecpt the application. I think i need to calculate the crc of the application and merge that into the factory image.

But i have so success.

Anyone hwo did this before?


Thanks so much

max

    This topic has been closed for replies.
    Best answer by Pavel A.

    If the application fits in the internal  flash, this actually should be very easy. Prefer plain binary files. Other formats (srec, hex) can leave holes with undefined content (0's or FF's or whatever). Then combine them easily with python:

     

     

    APP_SIZE=0x10000
    APP_OFFSET=0x2000
    BOOTLOADER_SIZE=APP_OFFSET
    TOTAL_SIZE = BOOTLOADER_SIZE + APP_SIZE
    buf = bytearray(b'\xFF' * TOTAL_SIZE)
    with open('bootloader.bin', 'rb') as f:
     len_boot = f.readinto(buf)
    with open('app.bin', 'rb') as f:
     len_app=f.readinto(memoryview(buf)[APP_OFFSET:])
    
    # Here buf is your combined image.
    # Calculate the checksum and poke it into the buffer...
    #.......
    with open('all.bin', 'wb') as f:
     len_total = f.write(buf)
    assert len_total == TOTAL_SIZE

     

     

    1 reply

    Pavel A.Answer
    Super User
    November 30, 2024

    If the application fits in the internal  flash, this actually should be very easy. Prefer plain binary files. Other formats (srec, hex) can leave holes with undefined content (0's or FF's or whatever). Then combine them easily with python:

     

     

    APP_SIZE=0x10000
    APP_OFFSET=0x2000
    BOOTLOADER_SIZE=APP_OFFSET
    TOTAL_SIZE = BOOTLOADER_SIZE + APP_SIZE
    buf = bytearray(b'\xFF' * TOTAL_SIZE)
    with open('bootloader.bin', 'rb') as f:
     len_boot = f.readinto(buf)
    with open('app.bin', 'rb') as f:
     len_app=f.readinto(memoryview(buf)[APP_OFFSET:])
    
    # Here buf is your combined image.
    # Calculate the checksum and poke it into the buffer...
    #.......
    with open('all.bin', 'wb') as f:
     len_total = f.write(buf)
    assert len_total == TOTAL_SIZE

     

     

    Visitor II
    December 1, 2024

    Thank you so much,

    maybe it helps other *** people like me:)

     

    import os
    
    # Define constants
    APP_OFFSET = 0x4000 # Application starts at 0x8004000
    BOOTLOADER_SIZE = APP_OFFSET # Bootloader size matches the offset
    TOTAL_SIZE = 0x10000 + APP_OFFSET # Maximum size: bootloader + application
    BOOT_FLASH_VECTOR_TABLE_CS_OFFSET = 0x188 # Offset for checksum in application
    
    # Create buffer filled with 0xFF (to simulate erased flash memory)
    buf = bytearray(b'\xFF' * TOTAL_SIZE)
    
    # Load the bootloader binary into the buffer
    try:
     with open('openblt_bootloader.bin', 'rb') as f:
     len_boot = f.readinto(buf)
     print(f"Bootloader size: {len_boot} bytes")
    except FileNotFoundError:
     raise FileNotFoundError("Bootloader binary 'openblt_bootloader.bin' not found.")
    
    # Load the application binary into the buffer at the correct offset
    try:
     with open('NGC_BLT_Test_APP.bin', 'rb') as f:
     len_app = f.readinto(memoryview(buf)[APP_OFFSET:])
     print(f"Application size: {len_app} bytes")
    except FileNotFoundError:
     raise FileNotFoundError("Application binary 'NGC_BLT_Test_APP.bin' not found.")
    
    # Ensure application fits in the allocated space
    if len_app + APP_OFFSET > TOTAL_SIZE:
     raise ValueError(f"Application size exceeds the allocated space. "
     f"Max allowed: {TOTAL_SIZE - APP_OFFSET}, Actual: {len_app}")
    
    # Calculate checksum based on the application vector table
    def calculate_checksum(buffer, app_offset):
     checksum = 0
     for i in range(0, 0x1C, 4): # First 7 exception addresses (0x00 to 0x18)
     entry = int.from_bytes(buffer[app_offset + i:app_offset + i + 4], byteorder='little')
     checksum += entry
     checksum = ~checksum & 0xFFFFFFFF # One's complement
     checksum += 1 # Two's complement
     return checksum
    
    # Calculate the checksum
    checksum = calculate_checksum(buf, APP_OFFSET)
    
    # Write the checksum into the buffer at APP_OFFSET + BOOT_FLASH_VECTOR_TABLE_CS_OFFSET
    checksum_offset = APP_OFFSET + BOOT_FLASH_VECTOR_TABLE_CS_OFFSET
    buf[checksum_offset:checksum_offset + 4] = checksum.to_bytes(4, byteorder='little')
    
    # Save the combined image to a file
    output_file = 'all.bin'
    with open(output_file, 'wb') as f:
     len_total = f.write(buf)
    
    # Verify the final size of the written file matches the total size
    if len_total != TOTAL_SIZE:
     raise IOError(f"Error writing combined image. Expected {TOTAL_SIZE} bytes, wrote {len_total} bytes.")
    
    print(f"Combined image created successfully: {output_file}")
    print(f"Bootloader size: {len_boot} bytes")
    print(f"Application size: {len_app} bytes")
    print(f"Checksum: 0x{checksum:08X} written at offset 0x{checksum_offset:08X}")
    

    Thats the working python code. It works with  my openblt bootloader and custom application. 

    Again, thank you so much.