Merge "libavb: Only query partition GUIDs when the cmdline needs them."
am: 4ef63c8fe5

Change-Id: Ie1c119663eca9063c6d7c358d33409839d712827
diff --git a/libavb/avb_cmdline.c b/libavb/avb_cmdline.c
index 38b0960..bf10865 100644
--- a/libavb/avb_cmdline.c
+++ b/libavb/avb_cmdline.c
@@ -58,6 +58,14 @@
     char part_name[AVB_PART_NAME_MAX_SIZE];
     char guid_buf[37];
 
+    /* Don't attempt to query the partition guid unless its search string is
+     * present in the command line. Note: the original cmdline is used here,
+     * not the replaced one. See b/116010959.
+     */
+    if (avb_strstr(cmdline, replace_str[n]) == NULL) {
+      continue;
+    }
+
     if (!avb_str_concat(part_name,
                         sizeof part_name,
                         part_name_str[n],
diff --git a/test/avb_slot_verify_unittest.cc b/test/avb_slot_verify_unittest.cc
index 274b6d4..749f080 100644
--- a/test/avb_slot_verify_unittest.cc
+++ b/test/avb_slot_verify_unittest.cc
@@ -3318,4 +3318,39 @@
   avb_slot_verify_data_free(slot_data);
 }
 
+TEST_F(AvbSlotVerifyTest, NoSystemPartition) {
+  GenerateVBMetaImage("vbmeta_a.img",
+                      "SHA256_RSA2048",
+                      0,
+                      base::FilePath("test/data/testkey_rsa2048.pem"),
+                      "--internal_release_string \"\"");
+
+  ops_.set_expected_public_key(
+      PublicKeyAVB(base::FilePath("test/data/testkey_rsa2048.pem")));
+  ops_.set_hidden_partitions({"system_a"});
+
+  AvbSlotVerifyData* slot_data = NULL;
+  const char* requested_partitions[] = {"boot", NULL};
+  EXPECT_EQ(AVB_SLOT_VERIFY_RESULT_OK,
+            avb_slot_verify(ops_.avb_ops(),
+                            requested_partitions,
+                            "_a",
+                            AVB_SLOT_VERIFY_FLAGS_NONE,
+                            AVB_HASHTREE_ERROR_MODE_RESTART_AND_INVALIDATE,
+                            &slot_data));
+  EXPECT_NE(nullptr, slot_data);
+  EXPECT_EQ(
+      "androidboot.vbmeta.device=PARTUUID=1234-fake-guid-for:vbmeta_a "
+      "androidboot.vbmeta.avb_version=1.1 "
+      "androidboot.vbmeta.device_state=locked "
+      "androidboot.vbmeta.hash_alg=sha256 androidboot.vbmeta.size=1152 "
+      "androidboot.vbmeta.digest="
+      "4161a7e655eabe16c3fe714de5d43736e7c0a190cf08d36c946d2509ce071e4d "
+      "androidboot.vbmeta.invalidate_on_error=yes "
+      "androidboot.veritymode=enforcing",
+      std::string(slot_data->cmdline));
+
+  avb_slot_verify_data_free(slot_data);
+}
+
 }  // namespace avb
diff --git a/test/fake_avb_ops.cc b/test/fake_avb_ops.cc
index 291527c..2fdba10 100644
--- a/test/fake_avb_ops.cc
+++ b/test/fake_avb_ops.cc
@@ -92,6 +92,10 @@
                                             size_t num_bytes,
                                             void* buffer,
                                             size_t* out_num_read) {
+  if (hidden_partitions_.find(partition) != hidden_partitions_.end()) {
+    return AVB_IO_RESULT_ERROR_NO_SUCH_PARTITION;
+  }
+
   base::FilePath path =
       partition_dir_.Append(std::string(partition)).AddExtension("img");
 
@@ -153,6 +157,9 @@
     size_t num_bytes,
     uint8_t** out_pointer,
     size_t* out_num_bytes_preloaded) {
+  if (hidden_partitions_.find(partition) != hidden_partitions_.end()) {
+    return AVB_IO_RESULT_ERROR_NO_SUCH_PARTITION;
+  }
   std::map<std::string, uint8_t*>::iterator it =
       preloaded_partitions_.find(std::string(partition));
   if (it == preloaded_partitions_.end()) {
@@ -179,6 +186,10 @@
                                            int64_t offset,
                                            size_t num_bytes,
                                            const void* buffer) {
+  if (hidden_partitions_.find(partition) != hidden_partitions_.end()) {
+    return AVB_IO_RESULT_ERROR_NO_SUCH_PARTITION;
+  }
+
   base::FilePath path =
       partition_dir_.Append(std::string(partition)).AddExtension("img");
 
@@ -289,6 +300,9 @@
                                                       const char* partition,
                                                       char* guid_buf,
                                                       size_t guid_buf_size) {
+  if (hidden_partitions_.find(partition) != hidden_partitions_.end()) {
+    return AVB_IO_RESULT_ERROR_NO_SUCH_PARTITION;
+  }
   // This is faking it a bit but makes testing easy. It works
   // because avb_slot_verify.c doesn't check that the returned GUID
   // is wellformed.
@@ -299,6 +313,10 @@
 AvbIOResult FakeAvbOps::get_size_of_partition(AvbOps* ops,
                                               const char* partition,
                                               uint64_t* out_size) {
+  if (hidden_partitions_.find(partition) != hidden_partitions_.end()) {
+    return AVB_IO_RESULT_ERROR_NO_SUCH_PARTITION;
+  }
+
   base::FilePath path =
       partition_dir_.Append(std::string(partition)).AddExtension("img");
 
diff --git a/test/fake_avb_ops.h b/test/fake_avb_ops.h
index 7beead6..935dbf4 100644
--- a/test/fake_avb_ops.h
+++ b/test/fake_avb_ops.h
@@ -180,6 +180,12 @@
     permanent_attributes_hash_ = hash;
   }
 
+  // All AvbOps for partitions in the given set will fail with
+  // AVB_IO_RESULT_ERROR_NO_SUCH_PARTITION.
+  void set_hidden_partitions(const std::set<std::string>& partitions) {
+    hidden_partitions_ = partitions;
+  }
+
   void enable_get_preloaded_partition();
 
   bool preload_partition(const std::string& partition,
@@ -275,6 +281,7 @@
 
   std::set<std::string> partition_names_read_from_;
   std::map<std::string, uint8_t*> preloaded_partitions_;
+  std::set<std::string> hidden_partitions_;
 
   std::map<std::string, std::string> stored_values_;
 };