- section[image->num_sections].base_address =
- (full_address & 0xffff) | (upper_address << 4);
- full_address = (full_address & 0xffff) | (upper_address << 4);
- }
- }
- else if (record_type == 4) /* Extended Linear Address Record */
- {
- u16 upper_address;
-
- sscanf(&lpszLine[bytes_read], "%4hx", &upper_address);
- cal_checksum += (u8)(upper_address >> 8);
- cal_checksum += (u8)upper_address;
- bytes_read += 4;
-
- if ((full_address >> 16) != upper_address)
- {
- /* we encountered a nonconsecutive location, create a new section,
- * unless the current section has zero size, in which case this specifies
- * the current section's base address
- */
- if (section[image->num_sections].size != 0)
- {
- image->num_sections++;
- section[image->num_sections].size = 0x0;
- section[image->num_sections].flags = 0;
- section[image->num_sections].private = &ihex->buffer[cooked_bytes];
+ } else if (record_type == 1) { /* End of File Record */
+ /* finish the current section */
+ image->num_sections++;
+
+ /* copy section information */
+ image->sections = malloc(sizeof(struct imagesection) * image->num_sections);
+ for (i = 0; i < image->num_sections; i++) {
+ image->sections[i].private = section[i].private;
+ image->sections[i].base_address = section[i].base_address;
+ image->sections[i].size = section[i].size;
+ image->sections[i].flags = section[i].flags;
+ }
+
+ end_rec = true;
+ break;
+ } else if (record_type == 2) { /* Linear Address Record */
+ uint16_t upper_address;
+
+ sscanf(&lpszLine[bytes_read], "%4hx", &upper_address);
+ cal_checksum += (uint8_t)(upper_address >> 8);
+ cal_checksum += (uint8_t)upper_address;
+ bytes_read += 4;
+
+ if ((full_address >> 4) != upper_address) {
+ /* we encountered a nonconsecutive location, create a new section,
+ * unless the current section has zero size, in which case this specifies
+ * the current section's base address
+ */
+ if (section[image->num_sections].size != 0) {
+ image->num_sections++;
+ if (image->num_sections >= IMAGE_MAX_SECTIONS) {
+ /* too many sections */
+ LOG_ERROR("Too many sections found in IHEX file");
+ return ERROR_IMAGE_FORMAT_ERROR;
+ }
+ section[image->num_sections].size = 0x0;
+ section[image->num_sections].flags = 0;
+ section[image->num_sections].private =
+ &ihex->buffer[cooked_bytes];
+ }
+ section[image->num_sections].base_address =
+ (full_address & 0xffff) | (upper_address << 4);
+ full_address = (full_address & 0xffff) | (upper_address << 4);
+ }
+ } else if (record_type == 3) { /* Start Segment Address Record */
+ uint32_t dummy;
+
+ /* "Start Segment Address Record" will not be supported
+ * but we must consume it, and do not create an error. */
+ while (count-- > 0) {
+ sscanf(&lpszLine[bytes_read], "%2" SCNx32, &dummy);
+ cal_checksum += (uint8_t)dummy;
+ bytes_read += 2;
+ }
+ } else if (record_type == 4) { /* Extended Linear Address Record */
+ uint16_t upper_address;
+
+ sscanf(&lpszLine[bytes_read], "%4hx", &upper_address);
+ cal_checksum += (uint8_t)(upper_address >> 8);
+ cal_checksum += (uint8_t)upper_address;
+ bytes_read += 4;
+
+ if ((full_address >> 16) != upper_address) {
+ /* we encountered a nonconsecutive location, create a new section,
+ * unless the current section has zero size, in which case this specifies
+ * the current section's base address
+ */
+ if (section[image->num_sections].size != 0) {
+ image->num_sections++;
+ if (image->num_sections >= IMAGE_MAX_SECTIONS) {
+ /* too many sections */
+ LOG_ERROR("Too many sections found in IHEX file");
+ return ERROR_IMAGE_FORMAT_ERROR;
+ }
+ section[image->num_sections].size = 0x0;
+ section[image->num_sections].flags = 0;
+ section[image->num_sections].private =
+ &ihex->buffer[cooked_bytes];
+ }
+ section[image->num_sections].base_address =
+ (full_address & 0xffff) | (upper_address << 16);
+ full_address = (full_address & 0xffff) | (upper_address << 16);