Snap for 11224086 from 525e9339a9a78b0f9b3452c0720a2d27efe6daa5 to mainline-tzdata5-release

Change-Id: Ibe0f4057c241de4fcbf6d801c653ab234fd1d86d
diff --git a/abi/src/com/android/tests/abi/KernelAbilistTest.java b/abi/src/com/android/tests/abi/KernelAbilistTest.java
index 8fba83d..315b12a 100644
--- a/abi/src/com/android/tests/abi/KernelAbilistTest.java
+++ b/abi/src/com/android/tests/abi/KernelAbilistTest.java
@@ -28,6 +28,9 @@
 
 @RunWith(DeviceJUnit4ClassRunner.class)
 public class KernelAbilistTest extends BaseHostJUnit4Test {
+    private static final String FEATURE_LEANBACK = "android.software.leanback";
+    private static final String FEATURE_TV = "android.hardware.type.television";
+
     @VsrTest(requirements = {"VSR-3.12-002"})
     @RequiresDevice
     @Test
@@ -38,6 +41,11 @@
             return;
         }
 
+        // Exclude VSR-3.12 for Android TV
+        if (hasDeviceFeature(FEATURE_LEANBACK) || hasDeviceFeature(FEATURE_TV)) {
+            return;
+        }
+
         // ro.vendor.api_level is the VSR requirement API level
         // calculated from ro.product.first_api_level, ro.board.api_level,
         // and ro.board.first_api_level.
diff --git a/encryption/file_based_encryption_tests.cpp b/encryption/file_based_encryption_tests.cpp
index 94b076e..58c656d 100644
--- a/encryption/file_based_encryption_tests.cpp
+++ b/encryption/file_based_encryption_tests.cpp
@@ -228,18 +228,26 @@
         fd_ = fd;
         return;
       }
-      if (errno == EBUSY) {
-        // Filesystem is already frozen, perhaps by a concurrent execution of
-        // this same test.  Since we don't have control over exactly when
-        // another process unfreezes the filesystem, we don't continue on with
-        // the test but rather just keep retrying the freeze until it works.
+      if (errno == EBUSY || errno == EINVAL) {
+        // EBUSY means the filesystem is already frozen, perhaps by a concurrent
+        // execution of this same test.  Since we don't have control over
+        // exactly when another process unfreezes the filesystem, we don't
+        // continue on with the test but rather just keep retrying the freeze
+        // until it works.
+        //
+        // Very rarely, on f2fs FIFREEZE fails with EINVAL (b/255800104).
+        // Unfortunately, the reason for this is still unknown.  Enter the retry
+        // loop in this case too, in the hope that it helps.
+        //
+        // Both of these errors are rare, so this sleep should not normally be
+        // executed.
         std::this_thread::sleep_for(std::chrono::milliseconds(100));
         continue;
       }
       ADD_FAILURE() << "Failed to freeze filesystem" << Errno();
       return;
     } while (std::chrono::steady_clock::now() - start <
-             std::chrono::seconds(10));
+             std::chrono::seconds(20));
     ADD_FAILURE() << "Timed out while waiting to freeze filesystem";
   }
 
diff --git a/encryption/utils.cpp b/encryption/utils.cpp
index da3632c..b7a0b57 100644
--- a/encryption/utils.cpp
+++ b/encryption/utils.cpp
@@ -37,6 +37,36 @@
 
 namespace android {
 namespace kernel {
+// Context in fixed input string comprises of software provided context,
+// padding to eight bytes (if required) and the key policy.
+static const std::vector<std::vector<uint8_t>> HwWrappedEncryptionKeyContexts =
+    {
+        {'i',  'n',  'l',  'i',  'n',  'e',  ' ',  'e',  'n', 'c', 'r', 'y',
+         'p',  't',  'i',  'o',  'n',  ' ',  'k',  'e',  'y', 0x0, 0x0, 0x0,
+         0x00, 0x00, 0x00, 0x02, 0x43, 0x00, 0x82, 0x50, 0x0, 0x0, 0x0, 0x0},
+        // Below for "legacy && kdf tied to Trusted Execution
+        // Environment(TEE)".
+        // Where as above caters ( "all latest targets" || ("legacy && kdf
+        // not tied to TEE)).
+        {'i',  'n',  'l',  'i',  'n',  'e',  ' ',  'e',  'n', 'c', 'r', 'y',
+         'p',  't',  'i',  'o',  'n',  ' ',  'k',  'e',  'y', 0x0, 0x0, 0x0,
+         0x00, 0x00, 0x00, 0x01, 0x43, 0x00, 0x82, 0x18, 0x0, 0x0, 0x0, 0x0},
+};
+
+static bool GetKdfContext(std::vector<uint8_t> *ctx) {
+  std::string kdf =
+      android::base::GetProperty("ro.crypto.hw_wrapped_keys.kdf", "v1");
+  if (kdf == "v1") {
+    *ctx = HwWrappedEncryptionKeyContexts[0];
+    return true;
+  }
+  if (kdf == "legacykdf") {
+    *ctx = HwWrappedEncryptionKeyContexts[1];
+    return true;
+  }
+  ADD_FAILURE() << "Unknown KDF: " << kdf;
+  return false;
+}
 
 // Offset in bytes to the filesystem superblock, relative to the beginning of
 // the block device
@@ -403,17 +433,12 @@
                                   std::vector<uint8_t> *enc_key) {
   std::vector<uint8_t> label{0x00, 0x00, 0x40, 0x00, 0x00, 0x00,
                              0x00, 0x00, 0x00, 0x00, 0x20};
-  // Context in fixed input string comprises of software provided context,
-  // padding to eight bytes (if required) and the key policy.
-  std::vector<uint8_t> context = {
-      'i', 'n', 'l', 'i', 'n', 'e', ' ', 'e',
-      'n', 'c', 'r', 'y', 'p', 't', 'i', 'o',
-      'n', ' ', 'k', 'e', 'y', 0x0, 0x0, 0x0,
-      0x00, 0x00, 0x00, 0x02, 0x43, 0x00, 0x82, 0x50,
-      0x0,  0x0,  0x0,  0x0};
 
-  return AesCmacKdfHelper(master_key, label, context, kAes256XtsKeySize,
-                          enc_key);
+  std::vector<uint8_t> ctx;
+
+  if (!GetKdfContext(&ctx)) return false;
+
+  return AesCmacKdfHelper(master_key, label, ctx, kAes256XtsKeySize, enc_key);
 }
 
 bool DeriveHwWrappedRawSecret(const std::vector<uint8_t> &master_key,
diff --git a/isa/vts_kernel_isa_test.cpp b/isa/vts_kernel_isa_test.cpp
index 8db2ff7..4975057 100644
--- a/isa/vts_kernel_isa_test.cpp
+++ b/isa/vts_kernel_isa_test.cpp
@@ -31,12 +31,43 @@
   return it->second;
 }
 
+// Returns true if the device has the specified feature.
+static bool deviceSupportsFeature(const char* feature) {
+  bool device_supports_feature = false;
+  FILE* p = popen("pm list features", "re");
+  if (p) {
+    char* line = NULL;
+    size_t len = 0;
+    while (getline(&line, &len, p) > 0) {
+      if (strstr(line, feature)) {
+        device_supports_feature = true;
+        break;
+      }
+    }
+    if (line) {
+      free(line);
+      line = NULL;
+    }
+    pclose(p);
+  }
+  return device_supports_feature;
+}
+
+static bool isTV() {
+  return deviceSupportsFeature("android.software.leanback");
+}
+
 /*
  * Tests that the kernel in use is meant to run on CPUs that support a
  * 64-bit Instruction Set Architecture (ISA).
  */
 TEST(KernelISATest, KernelUses64BitISA) {
   /*
+   * Exclude VSR-3.12 from Android TV
+   */
+
+  if (isTV()) GTEST_SKIP() << "Exempt from TV devices";
+  /*
    * ro.vendor.api_level is the VSR API level, which is calculated
    * as:
    *