Skip to content

About

Single-stage bootloader for STM32MP135

Resources

Stars

4 stars

Watchers

1 watching

Forks

Latest commit

 

History

105 Commits

Folders and files

Repository files navigation

USB MSC Bootloader for STM32MP135

This is a tiny, single-stage USB Mass Storage bootloader for the STM32MP135. When the board powers on, it can enumerate as a flash drive on your computer, so you can write SD-card images directly with simple tools like dd--no TF-A, U-Boot, or complex setup required. Programs can then be loaded and executed immediately, making it easy to experiment, modify, or add new functionality without navigating a multi-stage boot chain. It works on the eval board as well as my simpler custom board.

Features include:

  • Boot Linux: Load kernel and DTB into RAM and execute it
  • Write to SD card from USB via Mass Storage Class, like a flash drive
  • Load any other program from SD card to DDR and execute it
  • Set image or solid color on LCD display; control backlight; read touch (CTP)
  • Send Ethernet frame and check on PHY & link status
  • Command line with tab completion and "up arrow" history
  • Super simple, hackable code: <3k lines of code plus ST HAL
  • Single stage, small (<160 kB), fits into SYSRAM/SRAM
  • All features optional and easily removed

Hardware setup

  1. Insert the SD card into the slot.

  2. Connect CN12 (PWR_IN), the USB-C port to the right of the screen, to a powered USB hub. (Consult this photo for connector labels.)

  3. Connect CN10, the Micro USB left of the screen, to a desktop computer, which will enumerate as a serial port (e.g. /dev/ttyACM0 on Linux, or COM20 on Windows).

  4. Connect CN7, the USB-C port on the lower left corner of the screen, to a host computer. This port will be used to transfer the data via Mass Storage.

Getting Started

  1. To compile the program, just run Make:

    cd msc_boot
    make CFLAGS_EXTRA=-DEVB
    

    If everything goes well, it should print which binaries were included in the generated SD card image:

    File                      LBA      Size       Blocks
    -------------------------------------------------------
    main.stm32                128      124928     244
    blink.bin                 372      12800      25
    
  2. The initial boot can be done with USB-C with DIP switch set to BOOT = 000 (see below for initial boot alternatives):

    STM32_Programmer_CLI.exe -c port=usb1 -w scripts/flash.tsv
    
  3. In the serial console (115200 baud, no parity), the prompt (> ) should be displayed. Write help and press Enter to get a list of available commands.

Using the Bootloader

The bootloader enumerates as a USB flash drive. Use dd (on Windows, get it from here) to write the SD card image:

dd if=build/sdcard.img of=/dev/sdc # on Linux
dd if=build/sdcard.img of=\\.\E:   # on Windows

WARNING WARNING: This will erase all data on the target device, so double-check that it is the newly-enumerated SD card and contains no important files.

After writing the SD card, open the serial console (115200 baud) and load the blink program into DDR using the two command, then execute it with jump:

> two
> jump

This runs the blink program: it just blinks the red led, and prints a message to UART4. Success! (The two commands loads the first partition to 0xC2000000 and the second to 0xC4000000.)

To run other programs, generate an SD card image containing the bootloader and the program. For example, the blink SD image was created with:

python3 scripts/sdimage.py build/sdcard.img \
   build/main.stm32 \
   --partition build/blink.bin

The script prints a table of where each program is installed on the SD card. Use the shown block number and size with load_sd and jump to load and run any program.

Booting Linux

Running Linux on 32-bit Arm is no different from other "bare-metal" programs: you load it into memory together with any optional parameters and jump to it. Do not allow yourself to be misled into thinking it's some kind of a difficult, mystical process---the kernel will run so long as a few conditions are satisfied:

  • DTB is loaded in memory and register r2 points to that address
  • DDR initialized, MMU and caches disabled, interrupts off, SVC mode
  • Allow non-secure access to peripherals used by the kernel (clocks, DDR, etc.)
  • Init program provided, e.g. from a simple filesystem

This bootloader takes care of all of these details. Use the bootloader command two to load both the kernel and DTB into RAM, and jump to execute it.

An example Linux distribution that works with this bootloader is provided in this repository. (Make sure that the Linux kernel does not do any secure monitor calls, since this bootloader is nonsecure only: use the provided patches under test/linux.) See this blog post for a more in-depth explanation on how to put together a complete Linux system that runs on the STM32MP135 evaluation board.

Booting Linux from NAND Flash

When NAND_FLASH is defined, the bootloader uses the FMC NAND controller instead of the SD card for storage. The NAND is partitioned as follows:

Block(s) Label Size Description
0-1 bootloader 512 KB Primary and redundant copies
2 ptable 256 KB Partition table (nand_pt_t struct)
3 dtb 256 KB Device tree blob
4-67 kernel 16 MB Kernel image (64 blocks reserved)
68+ rootfs ~495 MB UBI/UBIFS root filesystem

This layout is fixed: rootfs always starts at block 68 regardless of actual kernel size, so MTD partition definitions in the DTB never need updating when the kernel changes.

Building a NAND image -- use scripts/nandimage.py:

python3 scripts/nandimage.py nand.img \
    --boot  build/main.stm32 \
    --dtb   linux/arch/arm/boot/dts/board.dtb \
    --kernel linux/arch/arm/boot/zImage \
    --rootfs buildroot/output/images/rootfs.ubi

The script prints a layout table and embeds a partition table (block 2) that fmc_flush reads to know how many blocks to write.

Flashing -- copy nand.img to the USB MSC flash drive exposed by the bootloader, then in the serial console:

> fmc_flush

USB is blocked during the flush. Ctrl-C cancels after the next 2-second progress report.

Booting -- after flashing, fmc_bload loads the kernel and DTB from their NAND partitions into DDR, then jump executes the kernel:

> fmc_bload
> jump

Autoboot does both steps automatically after the key-press timeout.

Rootfs geometry -- build the UBI image with these parameters (matching the MX30LF4G28AD chip):

PEB size  = 262144  (256 KB)
LEB size  = 253952  (256 KB - 2 x 4 KB OOB pages)
min I/O   = 4096
sub-page  = 4096

In Buildroot:

BR2_TARGET_ROOTFS_UBI=y
BR2_TARGET_ROOTFS_UBI_PEBSIZE=0x40000
BR2_TARGET_ROOTFS_UBIFS_LEBSIZE=0x3e000
BR2_TARGET_ROOTFS_UBIFS_MINIOSIZE=0x1000
BR2_TARGET_ROOTFS_UBI_SUBSIZE=4096
BR2_TARGET_ROOTFS_UBI_MAXLEBCNT=1978

Configuration Flags

To customize the behavior of the bootloader for a new board or application, consult src/board.h. The whole header is essentially a giant if/else, defining the pin configurations of these boards:

Enable the eval board by passing CFLAGS_EXTRA=-DEVB, otherwise the bootloader will be built for the custom board. If adding support for a new board, add it to list above and into board.h as another else statement.

To enable/disable application specific functionality as desired, add these flags to the build command line (or directly to board.h):

  • BOOT_NOPROMPT, when not defined, asks for any key to be pressed to stop autoboot; when defined, we proceed to autoboot immediately
  • REG_PRINTOUT defines a command to print out the register values for RCC and all TIMx, GPIOx blocks
  • ETHERNET defines a command to send an Ethernet frame
  • LCD_DISPLAY enables the LCD and CTP touch controller; when not defined, lcd_init() is a no-op and the backlight/color commands are omitted
  • NAND_FLASH changes the USB MSC and bootloading code to use NAND flash (SD card is used by default when NAND_FLASH not defined)

Other features can be disabled just by removing them from the main() function:

  • Remove the blinking by removing blink().
  • Remove USB support by removing usb_init().
  • Remove SD card support by removing sd_init().

Note that if too much functionality is enabled at once, it may not all fit into SRAM anymore!

Initial Boot Alternatives

The initial download as explained above requires the STM32CubeProgrammer, which implements the custom DFU (Device Firmware Upgrade) protocol used by ST. There are other ways to load this program onto the STM32MP135 that do not require the STM32CubeProgrammer:

  • Use a separate computer to copy the image (build/sdcard.img) directly to the beginning of the SD card, re-insert the card into the board, set BOOT = 101 (SD card boot), and press the reset button.

  • Alternatively: Use the UART bootloader (BOOT = 000, "serial boot") with the provided Python script to write the bootloader directly into SYSRAM:

    python3 scripts/uart_boot.py -c COM20 -f build/main.stm32
    
  • Or even: Use JTAG (BOOT = 100, "engineering boot"):

    JLinkGDBServer.exe -device STM32MP135F -if swd -port 2330
    arm-none-eabi-gdb.exe -q -x scripts/load.gdb
    

References

Contributing

Questions, bug reports, and bring-up notes are welcome--feel free to open a GitHub issue if something isn't clear or if you run into hardware issues on your own STM32MP135 board. Even simple questions help improve the documentation.

Small, focused pull requests are also appreciated. Please follow the general style of the existing code and keep features optional so the bootloader stays small and easy to understand. Run static code analysis via make check before committing changes.

Author

Jakob Kastelic (Stanford Research Systems)

About

Single-stage bootloader for STM32MP135

Resources

Stars

4 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages