Failure in mgmt while trying to upgrade the app using external flash as secondary slot

Hi 
I am using the nrf52832 with zephyr os , NCS 1.9.1, also using the external flash as a secondary slot, using a nor flash 
I tried to upgrade my app but i an error occurred in img_mgmt.c  --function img_mgmt_read_info  in the NCS while checking the magic number 

else if (hdr.ih_magic == erased_val_32) {
printf("MGMT_ERR_ENOENT 5 \n"); // print for debug only
return MGMT_ERR_ENOENT;

Is there any modification i have to do to make this work also I checked and the failure is in slot 1 which is the secondary slot and slot 0 erased without problem 

int
img_mgmt_read_info(int image_slot, struct image_version *ver, uint8_t *hash,
				   uint32_t *flags)
{
	printf("img_mgmt_read_info \n");
#if IMG_MGMT_DUMMY_HDR
	uint8_t dummy_hash[] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x00, 0x11, 0x22,
				0x33, 0x44, 0x55, 0x66, 0x77};

	if (!hash && !ver && !flags) {
		return 0;
	}

	if (hash) {
		memcpy(hash, dummy_hash, IMG_MGMT_HASH_LEN);
	}

	if (ver) {
		memset(ver, 0xff, sizeof(*ver));
	}

	if (flags) {
		*flags = 0;
	}

	return 0;
#endif

	struct image_header hdr;
	struct image_tlv tlv;
	size_t data_off;
	size_t data_end;
	bool hash_found;
	uint8_t erased_val;
	uint32_t erased_val_32;
	int rc;

	rc = img_mgmt_impl_erased_val(image_slot, &erased_val);
	if (rc != 0) {
		return MGMT_ERR_EUNKNOWN;
	}
	printf("img_mgmt_impl_erased_val image_slot = %d \n", image_slot);
	rc = img_mgmt_impl_read(image_slot, 0, &hdr, sizeof(hdr));
	if (rc != 0) {
		return MGMT_ERR_EUNKNOWN;
	}
	printf("img_mgmt_impl_read \n");
	
	if (ver != NULL) {
		printf("ver null\n");
		memset(ver, erased_val, sizeof(*ver));
	}
	erased_val_32 = ERASED_VAL_32(erased_val);
	if (hdr.ih_magic == IMAGE_MAGIC) {
		if (ver != NULL) {
			memcpy(ver, &hdr.ih_ver, sizeof(*ver));
		}
	} else if (hdr.ih_magic == erased_val_32) {
		printf("MGMT_ERR_ENOENT 5 \n");
		return MGMT_ERR_ENOENT;
	} else {
		printf("MGMT_ERR_EUNKNOWN 6 \n");
		return MGMT_ERR_EUNKNOWN;
	}

	if (flags != NULL) {
		*flags = hdr.ih_flags;
	}
	printf("img_mgmt_find_tlvs before 1\n");
	/* Read the image's TLVs. We first try to find the protected TLVs, if the protected
	 * TLV does not exist, we try to find non-protected TLV which also contains the hash
	 * TLV. All images are required to have a hash TLV.  If the hash is missing, the image
	 * is considered invalid.
	 */
	data_off = hdr.ih_hdr_size + hdr.ih_img_size;

	rc = img_mgmt_find_tlvs(image_slot, &data_off, &data_end, IMAGE_TLV_PROT_INFO_MAGIC);
	if (!rc) {
		/* The data offset should start after the header bytes after the end of
		 * the protected TLV, if one exists.
		 */
		data_off = data_end - sizeof(struct image_tlv_info);
	}
	
	rc = img_mgmt_find_tlvs(image_slot, &data_off, &data_end, IMAGE_TLV_INFO_MAGIC);
	if (rc != 0) {
		return MGMT_ERR_EUNKNOWN;
	}
	printf("img_mgmt_find_tlvs 2 \n");
	hash_found = false;
	while (data_off + sizeof(tlv) <= data_end) {
		rc = img_mgmt_impl_read(image_slot, data_off, &tlv, sizeof(tlv));
		if (rc != 0) {
			return MGMT_ERR_EUNKNOWN;
		}
		printf("img_mgmt_impl_read  1\n");
		if (tlv.it_type == 0xff && tlv.it_len == 0xffff) {
			return MGMT_ERR_EUNKNOWN;
		}
		if (tlv.it_type != IMAGE_TLV_SHA256 || tlv.it_len != IMAGE_HASH_LEN) {
			/* Non-hash TLV.  Skip it. */
			data_off += sizeof(tlv) + tlv.it_len;
			continue;
		}

		if (hash_found) {
			/* More than one hash. */
			return MGMT_ERR_EUNKNOWN;
		}
		hash_found = true;

		data_off += sizeof(tlv);
		if (hash != NULL) {
			if (data_off + IMAGE_HASH_LEN > data_end) {
				printf("MGMT_ERR_EUNKNOWN 1 \n");
				return MGMT_ERR_EUNKNOWN;
			}
			rc = img_mgmt_impl_read(image_slot, data_off, hash,
									IMAGE_HASH_LEN);
			printf("img_mgmt_impl_read 2\n");
			if (rc != 0) {
				printf("MGMT_ERR_EUNKNOWN 2 \n");
				return MGMT_ERR_EUNKNOWN;
			}
		}
	}

	if (!hash_found) {
		printf("MGMT_ERR_EUNKNOWN \n");
		return MGMT_ERR_EUNKNOWN;
	}

	return 0;
}

In the end i am getting an mpu fail 
Any suggestion ? 
Thanks a lot 
Ibrahim

Related