Anoop Jayaram
Embedded Systems

Bootloader Development

Learn how bootloaders work in embedded systems, firmware update mechanisms, memory mapping, UART bootloaders, OTA updates, and secure firmware loading.

Introduction

A bootloader is a small program that executes immediately after the microcontroller resets. Its main role is to initialize the system and load the main application firmware into memory.

Bootloaders are widely used in firmware updates, OTA systems, industrial controllers, automotive ECUs, and IoT devices.

Memory Mapping

In embedded systems, Flash memory is divided into separate regions for bootloader and application firmware.

| Bootloader Section | 0x08000000 |
|--------------------|
| Application Code   | 0x08010000 |
|--------------------|
| Backup Firmware    | 0x08070000 |

Boot Process

  1. System Reset Occurs
  2. Bootloader Starts Execution
  3. Hardware Initialization
  4. Firmware Validation
  5. Jump to Application Firmware

UART Bootloader

UART bootloaders receive firmware through serial communication and write the data into Flash memory.

while(1)
{
   Receive_Firmware();
   Flash_Write();
   Verify_Firmware();
}

Basic ESP32 Bootloader Example

The following example demonstrates a simple ESP32 firmware update mechanism using UART communication. The bootloader receives firmware data and writes it into Flash memory.

#include <stdio.h>
#include <string.h>
#include "esp_system.h"
#include "esp_spi_flash.h"
#include "esp_partition.h"

#define BUFFER_SIZE 256

uint8_t firmware_buffer[BUFFER_SIZE];

void receive_firmware()
{
    printf("Waiting for firmware data...\\n");

    // Example UART receive logic
    for(int i = 0; i < BUFFER_SIZE; i++)
    {
        firmware_buffer[i] = i;
    }

    printf("Firmware received\\n");
}

void write_firmware()
{
    const esp_partition_t *partition =
        esp_partition_find_first(
            ESP_PARTITION_TYPE_APP,
            ESP_PARTITION_SUBTYPE_APP_OTA_0,
            NULL);

    if(partition == NULL)
    {
        printf("Partition not found\\n");
        return;
    }

    esp_partition_erase_range(partition, 0, BUFFER_SIZE);

    esp_partition_write(
        partition,
        0,
        firmware_buffer,
        BUFFER_SIZE);

    printf("Firmware written to flash\\n");
}

void app_main()
{
    printf("ESP32 Bootloader Example\\n");

    receive_firmware();

    write_firmware();

    printf("Firmware update complete\\n");
}

Code Explanation

  • receive_firmware() receives firmware data through UART
  • esp_partition_find_first() locates OTA partition
  • esp_partition_erase_range() clears Flash memory region
  • esp_partition_write() writes firmware into Flash
  • After update completion, ESP32 can reboot into new firmware

OTA Firmware Update

OTA (Over-The-Air) bootloaders allow firmware updates through WiFi, Bluetooth, or Ethernet without physical access to the device.

  • WiFi OTA
  • BLE OTA
  • MQTT Firmware Updates
  • HTTPS Secure Updates

Bootloader Security

Secure boot mechanisms protect embedded systems from unauthorized firmware modifications.

  • Firmware Encryption
  • Digital Signature Verification
  • Checksum Validation
  • Secure Flash Protection