image.c

C source for Emulating a round-screen ESP32 board, 79 lines. Raw file

// image.c - load an UNCHANGED ESP32-S3 app image (the exact <app>.bin that is
// flashed to the device) into emulated memory. Format: esp_image_header_t (24B),
// then segment_count x [load_addr u32, data_len u32, data...]. Returns the entry.
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include "mem.h"

// ---- FAITHFUL boot: lay out the raw flash exactly as `esptool write_flash` does,
// then start at the mask-ROM reset vector so the real 1st-stage ROM loader, the real
// 2nd-stage bootloader, and the app all run from flash with no shortcuts. ----
static long load_raw(const char *path, uint32_t off, int required) {
    FILE *f = fopen(path, "rb");
    if (!f) { if (required) { fprintf(stderr, "cannot open %s\n", path); exit(1); } return -1; }
    fseek(f, 0, SEEK_END); long n = ftell(f); fseek(f, 0, SEEK_SET);
    uint8_t *dst = mem_flash(off);
    if (!dst || off + (uint32_t)n > mem_flash_size()) { fprintf(stderr, "flash OOB %s\n", path); exit(1); }
    if (fread(dst, 1, n, f) != (size_t)n) { fprintf(stderr, "short read %s\n", path); exit(1); }
    fclose(f);
    printf("flash: %-42s -> 0x%06x (%ld bytes)\n", path, off, n);
    return n;
}

// Load a complete 32 MB flash dump (bootloader@0, partition-table@0x8000,
// app@0x110000, SPIFFS/NVS above) straight into the emulated flash, then start
// at the ROM reset vector. This is the exact image `esptool write-flash` burns,
// so the device boots here bit-for-bit as it does on silicon.
uint32_t flash_image_load(const char *path) {
    load_raw(path, 0x0, 1);
    return 0x40000400;               // _ResetVector_text_start
}

// Given the app .bin path (…/build/<app>.bin) find its sibling bootloader.bin and
// partition-table.bin and place all three at their real flash offsets, then return
// the ESP32-S3 ROM reset vector (0x40000400) where the CPU actually starts on reset.
uint32_t flash_boot_load(const char *app_path) {
    char dir[1024]; strncpy(dir, app_path, sizeof dir - 1); dir[sizeof dir - 1] = 0;
    char *slash = strrchr(dir, '/'); if (slash) *slash = 0; else strcpy(dir, ".");
    char boot[1200], part[1200];
    snprintf(boot, sizeof boot, "%s/bootloader/bootloader.bin", dir);
    snprintf(part, sizeof part, "%s/partition_table/partition-table.bin", dir);
    load_raw(boot, 0x0000, 1);       // 2nd-stage bootloader
    load_raw(part, 0x8000, 1);       // partition table
    load_raw(app_path, 0x10000, 1);  // factory app
    return 0x40000400;               // _ResetVector_text_start
}

uint32_t image_load(const char *path) {
    FILE *f = fopen(path, "rb");
    if (!f) { fprintf(stderr, "cannot open %s\n", path); exit(1); }
    uint8_t hdr[24];
    if (fread(hdr, 1, 24, f) != 24 || hdr[0] != 0xE9) {
        fprintf(stderr, "%s: not an ESP app image (magic %02X)\n", path, hdr[0]);
        exit(1);
    }
    int segs = hdr[1];
    uint32_t entry = hdr[4] | (hdr[5]<<8) | (hdr[6]<<16) | ((uint32_t)hdr[7]<<24);
    printf("image %s: %d segments, entry %08X\n", path, segs, entry);

    for (int s = 0; s < segs; s++) {
        uint8_t sh[8];
        if (fread(sh, 1, 8, f) != 8) { fprintf(stderr, "short segment header\n"); exit(1); }
        uint32_t addr = sh[0] | (sh[1]<<8) | (sh[2]<<16) | ((uint32_t)sh[3]<<24);
        uint32_t len  = sh[4] | (sh[5]<<8) | (sh[6]<<16) | ((uint32_t)sh[7]<<24);
        uint8_t *buf = malloc(len);
        if (fread(buf, 1, len, f) != len) { fprintf(stderr, "short segment data\n"); exit(1); }
        mem_load(addr, buf, len);
        // The first (DROM) segment sits at _rodata_reserved_start; cpu_start reads the
        // app image header through the cache at (that - 24 - 8). On real HW the image
        // header and this segment's header lie just before it in the same flash
        // mapping, so reproduce that 32-byte prefix so the MAGIC check passes.
        if (s == 0) { mem_load(addr - 32, hdr, 24); mem_load(addr - 8, sh, 8); }
        printf("  seg %d: %08X len %u\n", s, addr, len);
        free(buf);
    }
    fclose(f);
    return entry;
}