- 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;
+ 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);
+
+ if (this_firstblock == prev_lastblock) {
+ uint32_t new_length = this_end - prev_seg->start;
+ uint32_t this_offset = paddr - prev_seg->start;
+ uint32_t hole = this_offset - prev_seg->length;
+ uint8_t *new_data = malloc(new_length);
+ if (!new_data) {
+ fprintf(stderr, "Out of memory\n");
+ free(data);
+ return -1;
+ }
+ memset(new_data, 0xff, new_length);
+ memcpy(new_data, prev_seg->data, prev_seg->length);
+ memcpy(new_data + this_offset, data, filesz);
+ fprintf(stderr, "Note: Extending previous segment from 0x%x to 0x%x bytes\n",
+ prev_seg->length, new_length);
+ if (hole)
+ fprintf(stderr, "Note: 0x%x-byte hole created\n", hole);
+ free(data);
+ free(prev_seg->data);
+ prev_seg->data = new_data;
+ prev_seg->length = new_length;
+ last_end = this_end;
+ phdr++;
+ continue;