Implement services for crypto+drm HALs

Test: service runs, basic gtests pass

bug:32815560
Change-Id: I9642f20d11805c56c46ddede64e776df5314f089
diff --git a/drm/crypto/1.0/default/Android.mk b/drm/crypto/1.0/default/Android.mk
index 83794ac..27fca98 100644
--- a/drm/crypto/1.0/default/Android.mk
+++ b/drm/crypto/1.0/default/Android.mk
@@ -32,10 +32,15 @@
     libmediadrm \
     libstagefright_foundation \
     android.hardware.drm.crypto@1.0 \
-    android.hidl.memory@1.0
+    android.hidl.memory@1.0 \
 
 LOCAL_C_INCLUDES := \
     frameworks/native/include \
     frameworks/av/include
 
+# TODO: The legacy DRM plugins only support 32-bit. They need
+# to be migrated to 64-bit (b/18948909)
+LOCAL_32_BIT_ONLY := true
+
+
 include $(BUILD_SHARED_LIBRARY)
diff --git a/drm/crypto/1.0/default/CryptoFactory.cpp b/drm/crypto/1.0/default/CryptoFactory.cpp
index 187d564..9519d01 100644
--- a/drm/crypto/1.0/default/CryptoFactory.cpp
+++ b/drm/crypto/1.0/default/CryptoFactory.cpp
@@ -27,7 +27,8 @@
 namespace implementation {
 
     CryptoFactory::CryptoFactory() :
-        loader("/vendor/lib/mediadrm", "createCryptoFactory") {}
+        loader("/vendor/lib/mediadrm", "createCryptoFactory") {
+    }
 
     // Methods from ::android::hardware::drm::crypto::V1_0::ICryptoFactory follow.
     Return<bool> CryptoFactory::isCryptoSchemeSupported(
@@ -42,7 +43,6 @@
 
     Return<void> CryptoFactory::createPlugin(const hidl_array<uint8_t, 16>& uuid,
             const hidl_vec<uint8_t>& initData, createPlugin_cb _hidl_cb) {
-
         for (size_t i = 0; i < loader.factoryCount(); i++) {
             if (loader.getFactory(i)->isCryptoSchemeSupported(uuid.data())) {
                 android::CryptoPlugin *legacyPlugin = NULL;
@@ -62,7 +62,7 @@
         return Void();
     }
 
-    ICryptoFactory* HIDL_FETCH_ICryptoFactory(const char /* *name */) {
+    ICryptoFactory* HIDL_FETCH_ICryptoFactory(const char* /* name */) {
         return new CryptoFactory();
     }
 
diff --git a/drm/crypto/1.0/default/CryptoPlugin.cpp b/drm/crypto/1.0/default/CryptoPlugin.cpp
index 9173d5b..6056f81 100644
--- a/drm/crypto/1.0/default/CryptoPlugin.cpp
+++ b/drm/crypto/1.0/default/CryptoPlugin.cpp
@@ -21,6 +21,7 @@
 
 #include <hidlmemory/mapping.h>
 #include <android/hidl/memory/1.0/IMemory.h>
+#include <utils/Log.h>
 
 using android::hidl::memory::V1_0::IMemory;
 
@@ -53,7 +54,7 @@
             const hidl_array<uint8_t, 16>& keyId,
             const hidl_array<uint8_t, 16>& iv, Mode mode,
             const Pattern& pattern, const hidl_vec<SubSample>& subSamples,
-            const hidl_memory &source, const DestinationBuffer& destination,
+            const hidl_memory& source, const DestinationBuffer& destination,
             decrypt_cb _hidl_cb) {
 
         android::CryptoPlugin::Mode legacyMode;
@@ -88,24 +89,26 @@
         AString detailMessage;
 
         void *destPtr = NULL;
-        sp<IMemory> sharedMemory;
+        sp<IMemory> sharedSourceMemory = mapMemory(source);
+        sp<IMemory> sharedDestinationMemory;
 
         if (destination.type == BufferType::SHARED_MEMORY) {
-            sharedMemory = mapMemory(source);
-            destPtr = sharedMemory->getPointer();
-            sharedMemory->update();
+            sharedDestinationMemory = mapMemory(destination.nonsecureMemory);
+            sharedDestinationMemory->update();
+            destPtr = sharedDestinationMemory->getPointer();
         } else if (destination.type == BufferType::NATIVE_HANDLE) {
             native_handle_t *handle = const_cast<native_handle_t *>(
                     destination.secureMemory.getNativeHandle());
             destPtr = static_cast<void *>(handle);
         }
         ssize_t result = mLegacyPlugin->decrypt(secure, keyId.data(), iv.data(),
-                legacyMode, legacyPattern, sharedMemory->getPointer(),
+                legacyMode, legacyPattern, sharedSourceMemory->getPointer(),
                 legacySubSamples, subSamples.size(), destPtr, &detailMessage);
 
         if (destination.type == BufferType::SHARED_MEMORY) {
-            sharedMemory->commit();
+            sharedDestinationMemory->commit();
         }
+
         delete[] legacySubSamples;
 
         uint32_t status;
diff --git a/drm/crypto/1.0/default/CryptoPlugin.h b/drm/crypto/1.0/default/CryptoPlugin.h
index b17dade..2c3032b 100644
--- a/drm/crypto/1.0/default/CryptoPlugin.h
+++ b/drm/crypto/1.0/default/CryptoPlugin.h
@@ -42,6 +42,7 @@
 
 struct CryptoPlugin : public ICryptoPlugin {
     CryptoPlugin(android::CryptoPlugin *plugin) : mLegacyPlugin(plugin) {}
+
     ~CryptoPlugin() {delete mLegacyPlugin;}
 
     // Methods from ::android::hardware::drm::crypto::V1_0::ICryptoPlugin