Merge "Fix a rare failure for imgdiff when random data equals gzip header" am: b87a166a16 am: 8542502f1f
am: 233f4b0b4b

Change-Id: If7e81b8e794f34d374d385eb5603353cdfec524f
diff --git a/Android.mk b/Android.mk
index dc066dd..3eed7a6 100644
--- a/Android.mk
+++ b/Android.mk
@@ -85,9 +85,11 @@
 
 LOCAL_FORCE_STATIC_EXECUTABLE := true
 
+LOCAL_REQUIRED_MODULES := e2fsdroid_static mke2fs_static mke2fs.conf
+
 ifeq ($(TARGET_USERIMAGES_USE_F2FS),true)
 ifeq ($(HOST_OS),linux)
-LOCAL_REQUIRED_MODULES := mkfs.f2fs
+LOCAL_REQUIRED_MODULES += mkfs.f2fs
 endif
 endif
 
@@ -120,6 +122,7 @@
     libverifier \
     libbatterymonitor \
     libbootloader_message \
+    libfs_mgr \
     libext4_utils \
     libsparse \
     libziparchive \
@@ -130,7 +133,6 @@
     libfusesideload \
     libminui \
     libpng \
-    libfs_mgr \
     libcrypto_utils \
     libcrypto \
     libvintf_recovery \
@@ -159,7 +161,7 @@
 endif
 
 ifeq ($(BOARD_CACHEIMAGE_PARTITION_SIZE),)
-LOCAL_REQUIRED_MODULES := recovery-persist recovery-refresh
+LOCAL_REQUIRED_MODULES += recovery-persist recovery-refresh
 endif
 
 include $(BUILD_EXECUTABLE)
diff --git a/roots.cpp b/roots.cpp
index 9b42702..e98dfd4 100644
--- a/roots.cpp
+++ b/roots.cpp
@@ -27,7 +27,8 @@
 #include <fcntl.h>
 
 #include <android-base/logging.h>
-#include <ext4_utils/make_ext4fs.h>
+#include <android-base/properties.h>
+#include <android-base/stringprintf.h>
 #include <ext4_utils/wipe.h>
 #include <fs_mgr.h>
 
@@ -215,11 +216,60 @@
         }
         int result;
         if (strcmp(v->fs_type, "ext4") == 0) {
-            if (v->erase_blk_size != 0 && v->logical_blk_size != 0) {
-                result = make_ext4fs_directory_align(v->blk_device, length, volume, sehandle,
-                        directory, v->erase_blk_size, v->logical_blk_size);
-            } else {
-                result = make_ext4fs_directory(v->blk_device, length, volume, sehandle, directory);
+          static constexpr int block_size = 4096;
+          int raid_stride = v->logical_blk_size / block_size;
+          int raid_stripe_width = v->erase_blk_size / block_size;
+
+          // stride should be the max of 8kb and logical block size
+          if (v->logical_blk_size != 0 && v->logical_blk_size < 8192) {
+            raid_stride = 8192 / block_size;
+          }
+
+          const char* mke2fs_argv[] = { "/sbin/mke2fs_static",
+                                        "-F",
+                                        "-t",
+                                        "ext4",
+                                        "-b",
+                                        nullptr,
+                                        nullptr,
+                                        nullptr,
+                                        nullptr,
+                                        nullptr,
+                                        nullptr };
+
+          int i = 5;
+          std::string block_size_str = std::to_string(block_size);
+          mke2fs_argv[i++] = block_size_str.c_str();
+
+          std::string ext_args;
+          if (v->erase_blk_size != 0 && v->logical_blk_size != 0) {
+            ext_args = android::base::StringPrintf("stride=%d,stripe-width=%d", raid_stride,
+                                                   raid_stripe_width);
+            mke2fs_argv[i++] = "-E";
+            mke2fs_argv[i++] = ext_args.c_str();
+          }
+
+          mke2fs_argv[i++] = v->blk_device;
+
+          std::string size_str = std::to_string(length / block_size);
+          if (length != 0) {
+            mke2fs_argv[i++] = size_str.c_str();
+          }
+
+          result = exec_cmd(mke2fs_argv[0], const_cast<char**>(mke2fs_argv));
+          if (result == 0 && directory != nullptr) {
+            const char* e2fsdroid_argv[] = { "/sbin/e2fsdroid_static",
+                                             "-e",
+                                             "-S",
+                                             "/file_contexts",
+                                             "-f",
+                                             directory,
+                                             "-a",
+                                             volume,
+                                             v->blk_device,
+                                             nullptr };
+
+            result = exec_cmd(e2fsdroid_argv[0], const_cast<char**>(e2fsdroid_argv));
             }
         } else {   /* Has to be f2fs because we checked earlier. */
             if (v->key_loc != NULL && strcmp(v->key_loc, "footer") == 0 && length < 0) {
diff --git a/tests/component/verifier_test.cpp b/tests/component/verifier_test.cpp
index e520f50..2ef3828 100644
--- a/tests/component/verifier_test.cpp
+++ b/tests/component/verifier_test.cpp
@@ -117,21 +117,6 @@
   ASSERT_FALSE(load_keys(key_file5.path, certs));
 }
 
-TEST(VerifierTest, BadPackage_SignatureStartOutOfBounds) {
-  std::string testkey_v3;
-  ASSERT_TRUE(android::base::ReadFileToString(from_testdata_base("testkey_v3.txt"), &testkey_v3));
-
-  TemporaryFile key_file;
-  ASSERT_TRUE(android::base::WriteStringToFile(testkey_v3, key_file.path));
-  std::vector<Certificate> certs;
-  ASSERT_TRUE(load_keys(key_file.path, certs));
-
-  // Signature start is 65535 (0xffff) while comment size is 0 (Bug: 31914369).
-  std::string package = "\x50\x4b\x05\x06"s + std::string(12, '\0') + "\xff\xff\xff\xff\x00\x00"s;
-  ASSERT_EQ(VERIFY_FAILURE, verify_file(reinterpret_cast<const unsigned char*>(package.data()),
-                                        package.size(), certs));
-}
-
 TEST(VerifierTest, BadPackage_AlteredFooter) {
   std::string testkey_v3;
   ASSERT_TRUE(android::base::ReadFileToString(from_testdata_base("testkey_v3.txt"), &testkey_v3));
@@ -177,6 +162,21 @@
                         certs));
 }
 
+TEST(VerifierTest, BadPackage_SignatureStartOutOfBounds) {
+  std::string testkey_v3;
+  ASSERT_TRUE(android::base::ReadFileToString(from_testdata_base("testkey_v3.txt"), &testkey_v3));
+
+  TemporaryFile key_file;
+  ASSERT_TRUE(android::base::WriteStringToFile(testkey_v3, key_file.path));
+  std::vector<Certificate> certs;
+  ASSERT_TRUE(load_keys(key_file.path, certs));
+
+  // Signature start is 65535 (0xffff) while comment size is 0 (Bug: 31914369).
+  std::string package = "\x50\x4b\x05\x06"s + std::string(12, '\0') + "\xff\xff\xff\xff\x00\x00"s;
+  ASSERT_EQ(VERIFY_FAILURE, verify_file(reinterpret_cast<const unsigned char*>(package.data()),
+                                        package.size(), certs));
+}
+
 TEST_P(VerifierSuccessTest, VerifySucceed) {
   ASSERT_EQ(verify_file(memmap.addr, memmap.length, certs, nullptr), VERIFY_SUCCESS);
 }
diff --git a/updater/install.cpp b/updater/install.cpp
index ff79edc..c9a3a07 100644
--- a/updater/install.cpp
+++ b/updater/install.cpp
@@ -302,9 +302,32 @@
   }
 
   if (fs_type == "ext4") {
-    int status = make_ext4fs(location.c_str(), size, mount_point.c_str(), sehandle);
+    const char* mke2fs_argv[] = { "/sbin/mke2fs_static", "-t",    "ext4", "-b", "4096",
+                                  location.c_str(),      nullptr, nullptr };
+    std::string size_str;
+    if (size != 0) {
+      size_str = std::to_string(size / 4096LL);
+      mke2fs_argv[6] = size_str.c_str();
+    }
+
+    int status = exec_cmd(mke2fs_argv[0], const_cast<char**>(mke2fs_argv));
     if (status != 0) {
-      LOG(ERROR) << name << ": make_ext4fs failed (" << status << ") on " << location;
+      LOG(WARNING) << name << ": mke2fs failed (" << status << ") on " << location
+                   << ", falling back to make_ext4fs";
+      status = make_ext4fs(location.c_str(), size, mount_point.c_str(), sehandle);
+      if (status != 0) {
+        LOG(ERROR) << name << ": make_ext4fs failed (" << status << ") on " << location;
+        return StringValue("");
+      }
+      return StringValue(location);
+    }
+
+    const char* e2fsdroid_argv[] = { "/sbin/e2fsdroid_static", "-e",   "-S",
+                                     "/file_contexts",         "-a",   mount_point.c_str(),
+                                     location.c_str(),         nullptr };
+    status = exec_cmd(e2fsdroid_argv[0], const_cast<char**>(e2fsdroid_argv));
+    if (status != 0) {
+      LOG(ERROR) << name << ": e2fsdroid failed (" << status << ") on " << location;
       return StringValue("");
     }
     return StringValue(location);