- char buf[4];
- fread(buf, 1, 4, f);
- if (memcmp(buf, "\x7F" "ELF", 4) == 0) {
- int i;
- fseek(f, 0, SEEK_SET);
- Elf32_Ehdr header;
- fread(&header, 1, sizeof(header), f);
- int count = header.e_phnum;
- printf("count=%d phoff=%x\n", count, header.e_phoff);
- Elf32_Phdr phdr;
-
- for (i=0; i<header.e_phnum; i++) {
- fseek(f, header.e_phoff + i * sizeof(Elf32_Phdr), SEEK_SET);
- fread(&phdr, 1, sizeof(phdr), f);
- printf("type=%d offset=%x paddr=%x vaddr=%x filesize=%x memsize=%x flags=%x align=%x\n",
- phdr.p_type, phdr.p_offset, phdr.p_paddr, phdr.p_vaddr, phdr.p_filesz, phdr.p_memsz, phdr.p_flags, phdr.p_align);
- if (phdr.p_type == PT_LOAD && phdr.p_filesz > 0 && phdr.p_paddr >= PHYSICAL_FLASH_START
- && (phdr.p_paddr + phdr.p_filesz) < PHYSICAL_FLASH_END) {
- printf("flashing offset=%x paddr=%x size=%x\n", phdr.p_offset, phdr.p_paddr, phdr.p_filesz);
- if (phdr.p_offset == 0) {
- printf("skipping forward 0x2000 because of strange linker thing\n");
- phdr.p_offset += 0x2000;
- phdr.p_paddr += 0x2000;
- phdr.p_filesz -= 0x2000;
- phdr.p_memsz -= 0x2000;
- }
+ fprintf(stderr, "Loading usable ELF segments:\n");
+ for (int i = 0; i < num_phdrs; i++) {
+ if (le32(phdr->p_type) != PT_LOAD) {
+ phdr++;
+ continue;
+ }
+ uint32_t vaddr = le32(phdr->p_vaddr);
+ uint32_t paddr = le32(phdr->p_paddr);
+ uint32_t filesz = le32(phdr->p_filesz);
+ uint32_t memsz = le32(phdr->p_memsz);
+ uint32_t offset = le32(phdr->p_offset);
+ uint32_t flags = le32(phdr->p_flags);
+ if (!filesz) {
+ phdr++;
+ continue;
+ }
+ fprintf(stderr, "%d: V 0x%08x P 0x%08x (0x%08x->0x%08x) [%c%c%c] @0x%x\n",
+ i, vaddr, paddr, filesz, memsz,
+ flags & PF_R ? 'R' : ' ',
+ flags & PF_W ? 'W' : ' ',
+ flags & PF_X ? 'X' : ' ',
+ offset);
+ if (filesz != memsz) {
+ fprintf(stderr, "Error: PHDR file size does not equal memory size\n"
+ "(DATA+BSS PHDRs do not make sense on ROM platforms!)\n");
+ return -1;
+ }
+ if (paddr < last_end) {
+ fprintf(stderr, "Error: PHDRs not sorted or overlap\n");
+ return -1;
+ }
+ if (paddr < FLASH_START || (paddr+filesz) > FLASH_END) {
+ fprintf(stderr, "Error: PHDR is not contained in Flash\n");
+ return -1;
+ }
+ if (vaddr >= FLASH_START && vaddr < FLASH_END && (flags & PF_W)) {
+ fprintf(stderr, "Error: Flash VMA segment is writable\n");
+ return -1;
+ }
+
+ uint8_t *data;
+ // make extra space if we need to move the data forward
+ data = malloc(filesz + BLOCK_SIZE);
+ if (!data) {
+ fprintf(stderr, "Out of memory\n");
+ return -1;
+ }
+ if (fseek(fd, offset, SEEK_SET) < 0 || fread(data, 1, filesz, fd) != filesz) {
+ fprintf(stderr, "Error while reading PHDR payload\n");
+ free(data);
+ return -1;
+ }
+
+ uint32_t block_offset = paddr & (BLOCK_SIZE-1);
+ if (block_offset) {
+ if (ctx->num_segs) {
+ flash_seg_t *prev_seg = seg - 1;
+ uint32_t this_end = paddr + filesz;
+ uint32_t this_firstblock = paddr & ~(BLOCK_SIZE-1);
+ uint32_t prev_lastblock = (last_end - 1) & ~(BLOCK_SIZE-1);