+ 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;
+ }
+ }
+ fprintf(stderr, "Warning: segment does not begin on a block boundary, will pad\n");
+ memmove(data + block_offset, data, filesz);
+ memset(data, 0xFF, block_offset);
+ filesz += block_offset;
+ paddr -= block_offset;
+ }
+
+ seg->data = data;
+ seg->start = paddr;
+ seg->length = filesz;
+ seg++;
+ ctx->num_segs++;
+
+ last_end = paddr + filesz;
+ phdr++;
+ }
+ return 0;