- ExpectedAddr = start_addr;
-
- FILE *f = fopen(file, "r");
- if(!f) {
- printf("couldn't open file\n");
- exit(-1);
- }
-
- char line[512];
- while(fgets(line, sizeof(line), f)) {
- if(memcmp(line, "S3", 2)==0) {
- char *s = line + 2;
- int len = HexByte(s) - 5;
- s += 2;
-
- char addrStr[9];
- memcpy(addrStr, s, 8);
- addrStr[8] = '\0';
- uint32_t addr;
- sscanf(addrStr, "%x", &addr);
- s += 8;
-
- /* Accept files that are located at PHYSICAL_FLASH_START, and files that are located at 0 */
- if(addr < PHYSICAL_FLASH_START)
- addr += PHYSICAL_FLASH_START;
-
- int i;
- for(i = 0; i < len; i++) {
- while((addr+i) > ExpectedAddr) {
- GotByte(ExpectedAddr, 0xff, start_addr, end_addr, translate);
- }
- GotByte(addr+i, HexByte(s), start_addr, end_addr, translate);
- s += 2;
- }
- }
- }
-
- if(!AllWritten) FlushPrevious(translate);
-
- fclose(f);
- printf("\ndone.\n");
+ FILE *f = fopen(file, "rb");
+ if (!f) {
+ printf("couldn't open file\n");
+ exit(-1);
+ }
+
+ char buf[4];
+ fread(buf, 1, 4, f);
+ if (memcmp(buf, "\x7F" "ELF", 4) == 0) {
+ 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 (int 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;
+ }
+
+ fseek(f, phdr.p_offset, SEEK_SET);
+ ExpectedAddr = phdr.p_paddr;
+ while (ExpectedAddr < (phdr.p_paddr + phdr.p_filesz)) {
+ unsigned int bytes_to_read = phdr.p_paddr + phdr.p_filesz - ExpectedAddr;
+ if (bytes_to_read > 256)
+ bytes_to_read=256;
+ else
+ memset(QueuedToSend, 0xFF, 256);
+ fread(QueuedToSend, 1, bytes_to_read, f);
+ printf("WriteBlock(%x, %d, %02x %02x %02x %02x %02x %02x %02x %02x ... %02x %02x %02x %02x %02x %02x %02x %02x)\n", ExpectedAddr, bytes_to_read,
+ QueuedToSend[0], QueuedToSend[1], QueuedToSend[2], QueuedToSend[3],
+ QueuedToSend[4], QueuedToSend[5], QueuedToSend[6], QueuedToSend[7],
+ QueuedToSend[248], QueuedToSend[249], QueuedToSend[250], QueuedToSend[251],
+ QueuedToSend[252], QueuedToSend[253], QueuedToSend[254], QueuedToSend[255]);
+ WriteBlock(ExpectedAddr, 256, QueuedToSend);
+ ExpectedAddr += bytes_to_read;
+ }
+ }
+ }
+
+ fclose(f);
+ printf("\ndone.\n");
+ return;
+ } else printf("Bad file format\n");