elf.c

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

// elf.c - minimal ELF32 (little-endian) PT_LOAD loader, used for the ESP32-S3
// mask ROM so the exact ROM code + data sit at their real addresses.
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include "mem.h"
static uint32_t rd32(const uint8_t*p){return p[0]|(p[1]<<8)|(p[2]<<16)|((uint32_t)p[3]<<24);}
static uint16_t rd16(const uint8_t*p){return p[0]|(p[1]<<8);}
void elf_load(const char *path) {
    FILE *f = fopen(path, "rb");
    if (!f) { fprintf(stderr, "cannot open %s\n", path); exit(1); }
    fseek(f, 0, SEEK_END); long sz = ftell(f); fseek(f, 0, SEEK_SET);
    uint8_t *buf = malloc(sz); fread(buf, 1, sz, f); fclose(f);
    if (buf[0]!=0x7f||buf[1]!='E') { fprintf(stderr,"%s: not ELF\n",path); exit(1); }
    uint32_t phoff = rd32(buf+28);
    uint16_t phentsize = rd16(buf+42), phnum = rd16(buf+44);
    int loaded = 0;
    for (int i = 0; i < phnum; i++) {
        const uint8_t *ph = buf + phoff + i*phentsize;
        if (rd32(ph) != 1) continue;              // PT_LOAD
        uint32_t off=rd32(ph+4), vaddr=rd32(ph+8), filesz=rd32(ph+16);
        if (filesz == 0) continue;
        mem_load(vaddr, buf + off, filesz);
        loaded += filesz;
    }
    // The mask ROM's initialised interface tables (rom_cache_internal_table_ptr, the
    // SPI/cache/MMU function-pointer blocks, …) live in .data.interface_* SECTIONS
    // that sit in the zero-filled tail of the first PT_LOAD program header, so the
    // segment pass above misses them. They ARE the ROM's data — load every allocated
    // PROGBITS section straight to its VMA so the ROM starts with its real state.
    uint32_t shoff = rd32(buf+32);
    uint16_t shentsize = rd16(buf+46), shnum = rd16(buf+48);
    for (int i = 0; i < shnum; i++) {
        const uint8_t *sh = buf + shoff + i*shentsize;
        uint32_t type=rd32(sh+4), addr=rd32(sh+12), soff=rd32(sh+16), size=rd32(sh+20);
        // SHT_PROGBITS with a real VMA. (The ROM's .data.interface_* sections carry
        // SHF_WRITE but not SHF_ALLOC, so key off the address, not the flags.)
        if (type != 1 || !addr || !size) continue;
        mem_load(addr, buf + soff, size);
    }
    free(buf);
    printf("ROM %s: loaded %d bytes\n", path, loaded);
}

// The esp-rom debug ELF omits the packed .data source blob (≈0x40057868+) that the
// ResetVector's unpack loop copies into RAM, so those copies would zero the ROM's
// interface tables (rom_cache_internal_table_ptr, the SPI/cache function blocks, …).
// The ELF DOES carry the unpacked VALUES (as .data.interface_* sections, already
// placed at their VMA above) and the 16-byte unpack DESCRIPTORS {dest, dest_end, src,
// flag} in _data_end_btdm_rom.._data_end. Rebuild the missing source from the known
// destination values so the REAL unpack loop reproduces the correct RAM — the ROM code
// runs completely unmodified; we only supply the ROM bytes the ELF left out.
void rom_data_reconstruct(void) {
    for (uint32_t a = 0x40057354; a < 0x400575c4; a += 16) {
        uint32_t ds = mem_r32(a), de = mem_r32(a + 4), src = mem_r32(a + 8);
        for (uint32_t o = 0; ds + o < de; o += 4)
            mem_w32(src + o, mem_r32(ds + o));
    }
}