Fix the identification of vbmeta partition

After dynamic partition, vbmeta_system and vbemta_vendor partitions
are added. Therefore, partitions with 'vbmeta' prefix should be treated
as vbmeta partition.

This makes libavb avoid locating avb footer from the end of vbmeta_*
partitions. And will not emit the following error message, which could
be confusing even if the following read of vbmeta succeeds from the
beginning of the partition.

Run the following test case will show different results after this
change.

$ ./out/host/linux-x86/nativetest64/libavb_host_unittest/libavb_host_unittest
--gtest_filter=AvbSlotVerifyTest.HashDescriptorInOtherVBMetaPartition

Before this change
-------------------------------------------------------------------------------
[ RUN      ] AvbSlotVerifyTest.HashDescriptorInOtherVBMetaPartition
avb_slot_verify.c:678: DEBUG: Loading vbmeta struct from partition 'vbmeta'.
avb_footer.c:41: ERROR: Footer magic is incorrect.
avb_slot_verify.c:650: DEBUG: vbmeta_google: No footer detected.
avb_slot_verify.c:678: DEBUG: Loading vbmeta struct from partition
'vbmeta_google'.
[       OK ] AvbSlotVerifyTest.HashDescriptorInOtherVBMetaPartition (960 ms)

After this change
-------------------------------------------------------------------------------
[ RUN      ] AvbSlotVerifyTest.HashDescriptorInOtherVBMetaPartition
avb_slot_verify.c:683: DEBUG: Loading vbmeta struct from partition 'vbmeta'.
avb_slot_verify.c:683: DEBUG: Loading vbmeta struct from partition 'vbmeta_google'.
[       OK ] AvbSlotVerifyTest.HashDescriptorInOtherVBMetaPartition (961ms)

Bug: b/134989217
Test: device launch, verify vbmeta, vbmeta_system, vbmeta_vendor.
Test: m libavb_host_unittest -j8 && \
      ./out/host/linux-x86/nativetest64/libavb_host_unittest/libavb_host_unittest
Change-Id: Ib0c2cd56197508fef8c12985e8cc0cd79469556c
diff --git a/platform/msm_shared/avb/libavb/avb_slot_verify.c b/platform/msm_shared/avb/libavb/avb_slot_verify.c
index 2b9b7be..f44a145 100644
--- a/platform/msm_shared/avb/libavb/avb_slot_verify.c
+++ b/platform/msm_shared/avb/libavb/avb_slot_verify.c
@@ -367,7 +367,7 @@
   size_t num_descriptors;
   size_t n;
   bool is_main_vbmeta;
-  bool is_vbmeta_partition;
+  bool look_for_vbmeta_footer;
   AvbVBMetaData* vbmeta_image_data = NULL;
 
   ret = AVB_SLOT_VERIFY_RESULT_OK;
@@ -379,7 +379,14 @@
    * vbmeta struct.
    */
   is_main_vbmeta = (rollback_index_location == 0);
-  is_vbmeta_partition = (avb_strcmp(partition_name, "vbmeta") == 0);
+
+  /* Don't use footers for vbmeta partitions ('vbmeta' or
+   * 'vbmeta_<partition_name>').
+   */
+  look_for_vbmeta_footer = true;
+  if (avb_strncmp(partition_name, "vbmeta", avb_strlen("vbmeta")) == 0) {
+    look_for_vbmeta_footer = false;
+  }
 
   if (!avb_validate_utf8((const uint8_t*)partition_name, partition_name_len)) {
     avb_error("Partition name is not valid UTF-8.\n");
@@ -409,7 +416,7 @@
 
   vbmeta_offset = 0;
   vbmeta_size = VBMETA_MAX_SIZE;
-  if (is_vbmeta_partition) {
+  if (is_main_vbmeta) {
     io_ret = ops->read_from_partition(ops,
                                     partition_name,
                                     vbmeta_offset,
@@ -421,40 +428,40 @@
       goto out;
     }
   } else {
-    uint8_t footer_buf[AVB_FOOTER_SIZE];
-    size_t footer_num_read;
-    AvbFooter footer;
+    if (look_for_vbmeta_footer) {
+      uint8_t footer_buf[AVB_FOOTER_SIZE];
+      size_t footer_num_read;
+      AvbFooter footer;
+      io_ret = ops->read_from_partition(ops,
+                                        partition_name,
+                                        -AVB_FOOTER_SIZE,
+                                        AVB_FOOTER_SIZE,
+                                        footer_buf,
+                                        &footer_num_read);
+      if (io_ret == AVB_IO_RESULT_ERROR_OOM) {
+        ret = AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
+        goto out;
+      } else if (io_ret != AVB_IO_RESULT_OK) {
+        avb_errorv(full_partition_name, ": Error loading footer.\n", NULL);
+        ret = AVB_SLOT_VERIFY_RESULT_ERROR_IO;
+        goto out;
+      }
+      avb_assert(footer_num_read == AVB_FOOTER_SIZE);
 
-    io_ret = ops->read_from_partition(ops,
-                                      partition_name,
-                                      -AVB_FOOTER_SIZE,
-                                      AVB_FOOTER_SIZE,
-                                      footer_buf,
-                                      &footer_num_read);
-    if (io_ret == AVB_IO_RESULT_ERROR_OOM) {
-      ret = AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
-      goto out;
-    } else if (io_ret != AVB_IO_RESULT_OK) {
-      avb_errorv(full_partition_name, ": Error loading footer.\n", NULL);
-      ret = AVB_SLOT_VERIFY_RESULT_ERROR_IO;
-      goto out;
-    }
-    avb_assert(footer_num_read == AVB_FOOTER_SIZE);
-
-    if (!avb_footer_validate_and_byteswap((const AvbFooter*)footer_buf,
-                                          &footer)) {
-      avb_errorv(full_partition_name, ": No footer detected.\n", NULL);
-    } else {
-      /* Basic footer sanity check since the data is untrusted. */
-      if (footer.vbmeta_size > VBMETA_MAX_SIZE) {
-        avb_errorv(
-            full_partition_name, ": Invalid vbmeta size in footer.\n", NULL);
+      if (!avb_footer_validate_and_byteswap((const AvbFooter*)footer_buf,
+                                            &footer)) {
+        avb_errorv(full_partition_name, ": No footer detected.\n", NULL);
       } else {
-        vbmeta_offset = footer.vbmeta_offset;
-        vbmeta_size = footer.vbmeta_size;
+        /* Basic footer sanity check since the data is untrusted. */
+        if (footer.vbmeta_size > VBMETA_MAX_SIZE) {
+          avb_errorv(
+              full_partition_name, ": Invalid vbmeta size in footer.\n", NULL);
+        } else {
+          vbmeta_offset = footer.vbmeta_offset;
+          vbmeta_size = footer.vbmeta_size;
+        }
       }
     }
-
     vbmeta_buf = avb_malloc(vbmeta_size); // for chain partitions
     if (vbmeta_buf == NULL) {
         ret = AVB_SLOT_VERIFY_RESULT_ERROR_OOM;
@@ -476,7 +483,7 @@
      * go try to get it from the boot partition instead.
      */
     if (is_main_vbmeta && io_ret == AVB_IO_RESULT_ERROR_NO_SUCH_PARTITION &&
-        is_vbmeta_partition) {
+        !look_for_vbmeta_footer) {
       avb_debugv(full_partition_name,
                  ": No such partition. Trying 'boot' instead.\n",
                  NULL);