+ 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);
+ }
+ } else if (record_type == 5) { /* Start Linear Address Record */
+ uint32_t start_address;
+
+ sscanf(&lpszLine[bytes_read], "%8" SCNx32, &start_address);
+ cal_checksum += (uint8_t)(start_address >> 24);
+ cal_checksum += (uint8_t)(start_address >> 16);
+ cal_checksum += (uint8_t)(start_address >> 8);
+ cal_checksum += (uint8_t)start_address;
+ bytes_read += 8;
+
+ image->start_address_set = 1;
+ image->start_address = be_to_h_u32((uint8_t *)&start_address);
+ } else {
+ LOG_ERROR("unhandled IHEX record type: %i", (int)record_type);
+ return ERROR_IMAGE_FORMAT_ERROR;