Merge "platform: msm_shared: Add PM8916 hardware subtype support for msm8909 target"
diff --git a/platform/msm_shared/avb/libavb/avb_ops.c b/platform/msm_shared/avb/libavb/avb_ops.c
index 4c1b81c..2199d34 100644
--- a/platform/msm_shared/avb/libavb/avb_ops.c
+++ b/platform/msm_shared/avb/libavb/avb_ops.c
@@ -160,8 +160,9 @@
 	AvbIOResult Result = AVB_IO_RESULT_OK;
 	EFI_STATUS Status = EFI_SUCCESS;
 	VOID *Page = NULL;
-	UINT32 Offset = 0;
+	UINTN Offset = 0;
 	UINTN ptn = 0;
+	UINTN part_size = 0;
 	UINT32 PageSize = 0;
 	UINT32 StartBlock = 0;
 	UINT32 LastBlock = 0;
@@ -178,46 +179,46 @@
 
 	if (!getimage(Buffer, OutNumRead, Partition)) {
 		/* API returns previously loaded Images buffer address and size */
-		dprintf(SPEW, "DEBUG: %s already loadded \n", Partition);
+                dprintf(SPEW, "DEBUG: %s already loaded \n", Partition);
 		return AVB_IO_RESULT_OK;
 	}
-	dprintf(SPEW, "%s Loading image\n", Partition);
+	dprintf(SPEW, "Loading image %s\n", Partition);
 	index = partition_get_index(Partition);
 	ptn = partition_get_offset(index);
+	part_size = partition_get_size(index);
 
 	if (ReadOffset < 0) {
-		if ((-ReadOffset) > (int64_t)ptn) {
+		if ((-ReadOffset) > (int64_t)part_size) {
 			dprintf(CRITICAL,
 			       "Negative Offset outside range.\n");
 			Result = AVB_IO_RESULT_ERROR_RANGE_OUTSIDE_PARTITION;
 			goto out;
 		}
-		Offset = ptn - (-ReadOffset);
+		Offset = part_size - (-ReadOffset);
 		dprintf(DEBUG,
-		       "negative Offset (%lld) converted to (%u) \n", ReadOffset, Offset);
+		       "negative Offset (%lld) converted to (0x%llx) \n", ReadOffset, Offset);
 	} else {
 		// check int64_t to UINT32 converstion?
 		Offset = ReadOffset;
 	}
 
-	if (Offset > ptn) {
+	if (Offset > part_size) {
 		dprintf(CRITICAL, "Offset outside range.\n");
 		Result = AVB_IO_RESULT_ERROR_RANGE_OUTSIDE_PARTITION;
 		goto out;
 	}
 
-	if (NumBytes > ptn - Offset) {
-		NumBytes = ptn - Offset;
+	if (NumBytes > part_size - Offset) {
+		NumBytes = part_size - Offset;
 	}
 
 	dprintf(CRITICAL,
-	       "read from %s, 0x%x bytes at Offset 0x%x, partition size %llu\n",
+	       "read from %s, 0x%x bytes at Offset 0x%llx, partition size %llu\n",
 	       Partition, NumBytes, Offset, ptn);
 
-	/* |NumBytes| and or |Offset| can be unaligned to block size/page size.
+	/* |NumBytes| and or |Offset| can be unaligned to block size.
 	 */
-	//PageSize = mmc_get_device_blocksize();
-	PageSize = get_page_size();
+	PageSize = mmc_get_device_blocksize();
 	Page = avb_malloc(PageSize);
 	if (Page == NULL) {
 		dprintf(CRITICAL, "Allocate for partial read failed!");
@@ -256,7 +257,7 @@
 			goto out;
 		}
 
-		Status = mmc_read(ptn, Page, PageSize);
+		Status = mmc_read(ptn + (StartBlock * PageSize), Page, PageSize);
 		if (Status == EFI_SUCCESS) {
 			avb_memcpy(Buffer, Page + StartPageReadOffset, StartPageReadSize);
 			*OutNumRead += StartPageReadSize;
diff --git a/platform/msm_shared/avb/libavb/avb_slot_verify.c b/platform/msm_shared/avb/libavb/avb_slot_verify.c
index 42ee9be..31b4f1d 100644
--- a/platform/msm_shared/avb/libavb/avb_slot_verify.c
+++ b/platform/msm_shared/avb/libavb/avb_slot_verify.c
@@ -369,7 +369,7 @@
   char full_partition_name[PART_NAME_MAX_SIZE];
   AvbSlotVerifyResult ret;
   AvbIOResult io_ret;
-  size_t vbmeta_offset;
+  UINTN vbmeta_offset;
   size_t vbmeta_size;
   uint8_t* vbmeta_buf = NULL;
   size_t vbmeta_num_read;
@@ -461,17 +461,30 @@
       ret = AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA;
       goto out;
     }
-
     vbmeta_offset = footer.vbmeta_offset;
     vbmeta_size = footer.vbmeta_size;
   }
 
-  io_ret = ops->read_from_partition(ops,
+  if (avb_strcmp(full_partition_name, "vbmeta") == 0) {
+    io_ret = ops->read_from_partition(ops,
                                     full_partition_name,
                                     vbmeta_offset,
                                     vbmeta_size,
                                     &vbmeta_buf,
                                     &vbmeta_num_read);
+  } else { // for chain partitions
+    vbmeta_buf = avb_malloc(vbmeta_size);
+    if (vbmeta_buf == NULL) {
+        ret = AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
+        goto out;
+    }
+    io_ret = ops->read_from_partition(ops,
+                                    full_partition_name,
+                                    vbmeta_offset,
+                                    vbmeta_size,
+                                    vbmeta_buf,
+                                    &vbmeta_num_read);
+  }
   if (vbmeta_buf == NULL) {
     ret = AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
     goto out;