blob: 1f8993379fce62f2947efeee14ee3287f1bccb5c [file] [log] [blame]
Chong Zhanga4f67512017-04-24 17:18:25 -07001/*
2 * Copyright (C) 2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17//#define LOG_NDEBUG 0
18#define LOG_TAG "android.hardware.cas@1.0-DescramblerImpl"
19
20#include <hidlmemory/mapping.h>
Chong Zhanga4f67512017-04-24 17:18:25 -070021#include <media/cas/DescramblerAPI.h>
Chong Zhang16a3cd02017-10-24 22:42:30 -070022#include <media/hardware/CryptoAPI.h>
23#include <media/stagefright/foundation/AUtils.h>
Chong Zhanga4f67512017-04-24 17:18:25 -070024#include <utils/Log.h>
25
26#include "DescramblerImpl.h"
27#include "SharedLibrary.h"
28#include "TypeConvert.h"
29
30namespace android {
31using hidl::memory::V1_0::IMemory;
32
33namespace hardware {
34namespace cas {
35namespace V1_0 {
36namespace implementation {
37
38#define CHECK_SUBSAMPLE_DEF(type) \
39static_assert(sizeof(SubSample) == sizeof(type::SubSample), \
40 "SubSample: size doesn't match"); \
41static_assert(offsetof(SubSample, numBytesOfClearData) \
42 == offsetof(type::SubSample, mNumBytesOfClearData), \
43 "SubSample: numBytesOfClearData offset doesn't match"); \
44static_assert(offsetof(SubSample, numBytesOfEncryptedData) \
45 == offsetof(type::SubSample, mNumBytesOfEncryptedData), \
46 "SubSample: numBytesOfEncryptedData offset doesn't match")
47
48CHECK_SUBSAMPLE_DEF(DescramblerPlugin);
49CHECK_SUBSAMPLE_DEF(CryptoPlugin);
50
51DescramblerImpl::DescramblerImpl(
52 const sp<SharedLibrary>& library, DescramblerPlugin *plugin) :
Chong Zhangaddcb3a2018-03-21 15:52:21 -070053 mLibrary(library), mPluginHolder(plugin) {
54 ALOGV("CTOR: plugin=%p", mPluginHolder.get());
Chong Zhanga4f67512017-04-24 17:18:25 -070055}
56
57DescramblerImpl::~DescramblerImpl() {
Chong Zhangaddcb3a2018-03-21 15:52:21 -070058 ALOGV("DTOR: plugin=%p", mPluginHolder.get());
Chong Zhanga4f67512017-04-24 17:18:25 -070059 release();
60}
61
62Return<Status> DescramblerImpl::setMediaCasSession(const HidlCasSessionId& sessionId) {
63 ALOGV("%s: sessionId=%s", __FUNCTION__,
64 sessionIdToString(sessionId).string());
65
Chong Zhangaddcb3a2018-03-21 15:52:21 -070066 std::shared_ptr<DescramblerPlugin> holder = std::atomic_load(&mPluginHolder);
67 if (holder.get() == nullptr) {
68 return toStatus(INVALID_OPERATION);
69 }
70
71 return toStatus(holder->setMediaCasSession(sessionId));
Chong Zhanga4f67512017-04-24 17:18:25 -070072}
73
74Return<bool> DescramblerImpl::requiresSecureDecoderComponent(
75 const hidl_string& mime) {
Chong Zhangaddcb3a2018-03-21 15:52:21 -070076 std::shared_ptr<DescramblerPlugin> holder = std::atomic_load(&mPluginHolder);
77 if (holder.get() == nullptr) {
78 return false;
79 }
80
81 return holder->requiresSecureDecoderComponent(String8(mime.c_str()));
Chong Zhanga4f67512017-04-24 17:18:25 -070082}
83
Chong Zhang16a3cd02017-10-24 22:42:30 -070084static inline bool validateRangeForSize(
85 uint64_t offset, uint64_t length, uint64_t size) {
86 return isInRange<uint64_t, uint64_t>(0, size, offset, length);
87}
88
Chong Zhanga4f67512017-04-24 17:18:25 -070089Return<void> DescramblerImpl::descramble(
90 ScramblingControl scramblingControl,
91 const hidl_vec<SubSample>& subSamples,
92 const SharedBuffer& srcBuffer,
93 uint64_t srcOffset,
94 const DestinationBuffer& dstBuffer,
95 uint64_t dstOffset,
96 descramble_cb _hidl_cb) {
97 ALOGV("%s", __FUNCTION__);
98
Chong Zhangaddcb3a2018-03-21 15:52:21 -070099 // Get a local copy of the shared_ptr for the plugin. Note that before
100 // calling the HIDL callback, this shared_ptr must be manually reset,
101 // since the client side could proceed as soon as the callback is called
102 // without waiting for this method to go out of scope.
103 std::shared_ptr<DescramblerPlugin> holder = std::atomic_load(&mPluginHolder);
104 if (holder.get() == nullptr) {
105 _hidl_cb(toStatus(INVALID_OPERATION), 0, NULL);
106 return Void();
107 }
108
Chong Zhanga4f67512017-04-24 17:18:25 -0700109 sp<IMemory> srcMem = mapMemory(srcBuffer.heapBase);
Chong Zhang16a3cd02017-10-24 22:42:30 -0700110
111 // Validate if the offset and size in the SharedBuffer is consistent with the
112 // mapped ashmem, since the offset and size is controlled by client.
113 if (srcMem == NULL) {
114 ALOGE("Failed to map src buffer.");
Chong Zhangaddcb3a2018-03-21 15:52:21 -0700115 holder.reset();
Chong Zhang16a3cd02017-10-24 22:42:30 -0700116 _hidl_cb(toStatus(BAD_VALUE), 0, NULL);
117 return Void();
118 }
119 if (!validateRangeForSize(
120 srcBuffer.offset, srcBuffer.size, (uint64_t)srcMem->getSize())) {
121 ALOGE("Invalid src buffer range: offset %llu, size %llu, srcMem size %llu",
122 srcBuffer.offset, srcBuffer.size, (uint64_t)srcMem->getSize());
123 android_errorWriteLog(0x534e4554, "67962232");
Chong Zhangaddcb3a2018-03-21 15:52:21 -0700124 holder.reset();
Chong Zhang16a3cd02017-10-24 22:42:30 -0700125 _hidl_cb(toStatus(BAD_VALUE), 0, NULL);
126 return Void();
127 }
128
129 // use 64-bit here to catch bad subsample size that might be overflowing.
130 uint64_t totalBytesInSubSamples = 0;
131 for (size_t i = 0; i < subSamples.size(); i++) {
132 totalBytesInSubSamples += (uint64_t)subSamples[i].numBytesOfClearData +
133 subSamples[i].numBytesOfEncryptedData;
134 }
135 // Further validate if the specified srcOffset and requested total subsample size
136 // is consistent with the source shared buffer size.
137 if (!validateRangeForSize(srcOffset, totalBytesInSubSamples, srcBuffer.size)) {
138 ALOGE("Invalid srcOffset and subsample size: "
139 "srcOffset %llu, totalBytesInSubSamples %llu, srcBuffer size %llu",
140 srcOffset, totalBytesInSubSamples, srcBuffer.size);
141 android_errorWriteLog(0x534e4554, "67962232");
Chong Zhangaddcb3a2018-03-21 15:52:21 -0700142 holder.reset();
Chong Zhang16a3cd02017-10-24 22:42:30 -0700143 _hidl_cb(toStatus(BAD_VALUE), 0, NULL);
144 return Void();
145 }
146
Chong Zhanga4f67512017-04-24 17:18:25 -0700147 void *srcPtr = (uint8_t *)(void *)srcMem->getPointer() + srcBuffer.offset;
148 void *dstPtr = NULL;
149 if (dstBuffer.type == BufferType::SHARED_MEMORY) {
150 // When using shared memory, src buffer is also used as dst,
151 // we don't map it again here.
152 dstPtr = srcPtr;
Chong Zhang16a3cd02017-10-24 22:42:30 -0700153
154 // In this case the dst and src would be the same buffer, need to validate
155 // dstOffset against the buffer size too.
156 if (!validateRangeForSize(dstOffset, totalBytesInSubSamples, srcBuffer.size)) {
157 ALOGE("Invalid dstOffset and subsample size: "
158 "dstOffset %llu, totalBytesInSubSamples %llu, srcBuffer size %llu",
159 dstOffset, totalBytesInSubSamples, srcBuffer.size);
160 android_errorWriteLog(0x534e4554, "67962232");
Chong Zhangaddcb3a2018-03-21 15:52:21 -0700161 holder.reset();
Chong Zhang16a3cd02017-10-24 22:42:30 -0700162 _hidl_cb(toStatus(BAD_VALUE), 0, NULL);
163 return Void();
164 }
Chong Zhanga4f67512017-04-24 17:18:25 -0700165 } else {
166 native_handle_t *handle = const_cast<native_handle_t *>(
167 dstBuffer.secureMemory.getNativeHandle());
168 dstPtr = static_cast<void *>(handle);
169 }
170 // Casting hidl SubSample to DescramblerPlugin::SubSample, but need
171 // to ensure structs are actually idential
172
Chong Zhangaddcb3a2018-03-21 15:52:21 -0700173 int32_t result = holder->descramble(
Chong Zhanga4f67512017-04-24 17:18:25 -0700174 dstBuffer.type != BufferType::SHARED_MEMORY,
175 (DescramblerPlugin::ScramblingControl)scramblingControl,
176 subSamples.size(),
177 (DescramblerPlugin::SubSample*)subSamples.data(),
178 srcPtr,
179 srcOffset,
180 dstPtr,
181 dstOffset,
182 NULL);
183
Chong Zhangaddcb3a2018-03-21 15:52:21 -0700184 holder.reset();
Chong Zhanga4f67512017-04-24 17:18:25 -0700185 _hidl_cb(toStatus(result >= 0 ? OK : result), result, NULL);
186 return Void();
187}
188
189Return<Status> DescramblerImpl::release() {
Chong Zhangaddcb3a2018-03-21 15:52:21 -0700190 ALOGV("%s: plugin=%p", __FUNCTION__, mPluginHolder.get());
Chong Zhanga4f67512017-04-24 17:18:25 -0700191
Chong Zhangaddcb3a2018-03-21 15:52:21 -0700192 std::shared_ptr<DescramblerPlugin> holder(nullptr);
193 std::atomic_store(&mPluginHolder, holder);
194
Chong Zhanga4f67512017-04-24 17:18:25 -0700195 return Status::OK;
196}
197
198} // namespace implementation
199} // namespace V1_0
200} // namespace cas
201} // namespace hardware
202} // namespace android