Merge "is_retrofit_device to work in bootloader" into main
diff --git a/fs_mgr/libsnapshot/libsnapshot_cow/create_cow.cpp b/fs_mgr/libsnapshot/libsnapshot_cow/create_cow.cpp
index 4e07fe3..efb1035 100644
--- a/fs_mgr/libsnapshot/libsnapshot_cow/create_cow.cpp
+++ b/fs_mgr/libsnapshot/libsnapshot_cow/create_cow.cpp
@@ -240,9 +240,11 @@
             SHA256(bufptr, BLOCK_SZ, checksum);
             std::string hash = ToHexString(checksum, sizeof(checksum));
 
-            if (create_snapshot_patch_ && !WriteSnapshot(bufptr, blkindex, hash)) {
-                LOG(ERROR) << "WriteSnapshot failed for block: " << blkindex;
-                return false;
+            if (create_snapshot_patch_) {
+                if (!WriteSnapshot(bufptr, blkindex, hash)) {
+                    LOG(ERROR) << "WriteSnapshot failed for block: " << blkindex;
+                    return false;
+                }
             } else {
                 std::lock_guard<std::mutex> lock(source_block_hash_lock_);
                 {
@@ -256,7 +258,7 @@
             num_blocks -= 1;
         }
 
-        file_offset += (skip_blocks * kBlockSizeToRead);
+        file_offset += (skip_blocks * to_read);
         if (file_offset >= dev_sz) {
             break;
         }
diff --git a/init/first_stage_init.cpp b/init/first_stage_init.cpp
index c6a287a..e48fa15 100644
--- a/init/first_stage_init.cpp
+++ b/init/first_stage_init.cpp
@@ -239,8 +239,12 @@
             module_dirs.emplace_back(entry->d_name);
             break;
         }
-        // Ignore _16k/_64k module dirs on 4K kernels
-        if (GetPageSizeSuffix(entry->d_name) != page_size_suffix) {
+        // Is a directory does not have page size suffix, it does not mean this directory is for 4K
+        // kernels. Certain 16K kernel builds put all modules in /lib/modules/`uname -r` without any
+        // suffix. Therefore, only ignore a directory if it has _16k/_64k suffix and the suffix does
+        // not match system page size.
+        const auto dir_page_size_suffix = GetPageSizeSuffix(entry->d_name);
+        if (!dir_page_size_suffix.empty() && dir_page_size_suffix != page_size_suffix) {
             continue;
         }
         int dir_major = 0, dir_minor = 0;
diff --git a/libcutils/ashmem-dev.cpp b/libcutils/ashmem-dev.cpp
index 5e01da9..410dbfd 100644
--- a/libcutils/ashmem-dev.cpp
+++ b/libcutils/ashmem-dev.cpp
@@ -44,13 +44,6 @@
 #include <android-base/strings.h>
 #include <android-base/unique_fd.h>
 
-/*
- * The minimum vendor API level at and after which it is safe to use memfd.
- * This is to facilitate deprecation of ashmem.
- */
-#define MIN_MEMFD_VENDOR_API_LEVEL 29
-#define MIN_MEMFD_VENDOR_API_LEVEL_CHAR 'Q'
-
 /* ashmem identity */
 static dev_t __ashmem_rdev;
 /*
@@ -88,55 +81,17 @@
 
 /* Determine if vendor processes would be ok with memfd in the system:
  *
- * If VNDK is using older libcutils, don't use memfd. This is so that the
- * same shared memory mechanism is used across binder transactions between
- * vendor partition processes and system partition processes.
+ * Previously this function checked if memfd is supported by checking if
+ * vendor VNDK version is greater than Q. As we can assume all treblelized
+ * device using this code is up to date enough to use memfd, memfd is allowed
+ * if the device is treblelized.
  */
 static bool check_vendor_memfd_allowed() {
-    std::string vndk_version = android::base::GetProperty("ro.vndk.version", "");
+    static bool is_treblelized = android::base::GetBoolProperty("ro.treble.enabled", false);
 
-    if (vndk_version == "") {
-        ALOGE("memfd: ro.vndk.version not defined or invalid (%s), this is mandated since P.\n",
-              vndk_version.c_str());
-        return false;
-    }
-
-    /* No issues if vendor is targetting current Dessert */
-    if (vndk_version == "current") {
-        return false;
-    }
-
-    /* Check if VNDK version is a number and act on it */
-    char* p;
-    long int vers = strtol(vndk_version.c_str(), &p, 10);
-    if (*p == 0) {
-        if (vers < MIN_MEMFD_VENDOR_API_LEVEL) {
-            ALOGI("memfd: device VNDK version (%s) is < Q so using ashmem.\n",
-                  vndk_version.c_str());
-            return false;
-        }
-
-        return true;
-    }
-
-    // Non-numeric should be a single ASCII character. Characters after the
-    // first are ignored.
-    if (tolower(vndk_version[0]) < 'a' || tolower(vndk_version[0]) > 'z') {
-        ALOGE("memfd: ro.vndk.version not defined or invalid (%s), this is mandated since P.\n",
-              vndk_version.c_str());
-        return false;
-    }
-
-    if (tolower(vndk_version[0]) < tolower(MIN_MEMFD_VENDOR_API_LEVEL_CHAR)) {
-        ALOGI("memfd: device is using VNDK version (%s) which is less than Q. Use ashmem only.\n",
-              vndk_version.c_str());
-        return false;
-    }
-
-    return true;
+    return is_treblelized;
 }
 
-
 /* Determine if memfd can be supported. This is just one-time hardwork
  * which will be cached by the caller.
  */
diff --git a/libcutils/fs_config.cpp b/libcutils/fs_config.cpp
index 26ac576..919be2f 100644
--- a/libcutils/fs_config.cpp
+++ b/libcutils/fs_config.cpp
@@ -41,10 +41,6 @@
 
 #include "fs_config.h"
 
-#ifndef O_BINARY
-#define O_BINARY 0
-#endif
-
 using android::base::EndsWith;
 using android::base::StartsWith;
 
@@ -257,12 +253,12 @@
         len = strip(target_out_path, len, "/");
         len = strip(target_out_path, len, "/system");
         if (asprintf(&name, "%.*s%s", (int)len, target_out_path, conf[which][dir]) != -1) {
-            fd = TEMP_FAILURE_RETRY(open(name, O_RDONLY | O_BINARY));
+            fd = TEMP_FAILURE_RETRY(open(name, O_RDONLY));
             free(name);
         }
     }
     if (fd < 0) {
-        fd = TEMP_FAILURE_RETRY(open(conf[which][dir], O_RDONLY | O_BINARY));
+        fd = TEMP_FAILURE_RETRY(open(conf[which][dir], O_RDONLY));
     }
     return fd;
 }
diff --git a/rootdir/init.rc b/rootdir/init.rc
index 487e5da..8eec16c 100644
--- a/rootdir/init.rc
+++ b/rootdir/init.rc
@@ -610,7 +610,6 @@
     restorecon_recursive /metadata/apex
 
     mkdir /metadata/staged-install 0770 root system
-    mkdir /metadata/sepolicy 0700 root root
 on late-fs
     # Ensure that tracefs has the correct permissions.
     # This does not work correctly if it is called in post-fs.