blob: 36699baf28128f46294c4981b3d7fb4b3d3118e8 [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) :
53 mLibrary(library), mPlugin(plugin) {
54 ALOGV("CTOR: mPlugin=%p", mPlugin);
55}
56
57DescramblerImpl::~DescramblerImpl() {
58 ALOGV("DTOR: mPlugin=%p", mPlugin);
59 release();
60}
61
62Return<Status> DescramblerImpl::setMediaCasSession(const HidlCasSessionId& sessionId) {
63 ALOGV("%s: sessionId=%s", __FUNCTION__,
64 sessionIdToString(sessionId).string());
65
66 return toStatus(mPlugin->setMediaCasSession(sessionId));
67}
68
69Return<bool> DescramblerImpl::requiresSecureDecoderComponent(
70 const hidl_string& mime) {
71 return mPlugin->requiresSecureDecoderComponent(String8(mime.c_str()));
72}
73
Chong Zhang16a3cd02017-10-24 22:42:30 -070074static inline bool validateRangeForSize(
75 uint64_t offset, uint64_t length, uint64_t size) {
76 return isInRange<uint64_t, uint64_t>(0, size, offset, length);
77}
78
Chong Zhanga4f67512017-04-24 17:18:25 -070079Return<void> DescramblerImpl::descramble(
80 ScramblingControl scramblingControl,
81 const hidl_vec<SubSample>& subSamples,
82 const SharedBuffer& srcBuffer,
83 uint64_t srcOffset,
84 const DestinationBuffer& dstBuffer,
85 uint64_t dstOffset,
86 descramble_cb _hidl_cb) {
87 ALOGV("%s", __FUNCTION__);
88
89 sp<IMemory> srcMem = mapMemory(srcBuffer.heapBase);
Chong Zhang16a3cd02017-10-24 22:42:30 -070090
91 // Validate if the offset and size in the SharedBuffer is consistent with the
92 // mapped ashmem, since the offset and size is controlled by client.
93 if (srcMem == NULL) {
94 ALOGE("Failed to map src buffer.");
95 _hidl_cb(toStatus(BAD_VALUE), 0, NULL);
96 return Void();
97 }
98 if (!validateRangeForSize(
99 srcBuffer.offset, srcBuffer.size, (uint64_t)srcMem->getSize())) {
100 ALOGE("Invalid src buffer range: offset %llu, size %llu, srcMem size %llu",
101 srcBuffer.offset, srcBuffer.size, (uint64_t)srcMem->getSize());
102 android_errorWriteLog(0x534e4554, "67962232");
103 _hidl_cb(toStatus(BAD_VALUE), 0, NULL);
104 return Void();
105 }
106
107 // use 64-bit here to catch bad subsample size that might be overflowing.
108 uint64_t totalBytesInSubSamples = 0;
109 for (size_t i = 0; i < subSamples.size(); i++) {
110 totalBytesInSubSamples += (uint64_t)subSamples[i].numBytesOfClearData +
111 subSamples[i].numBytesOfEncryptedData;
112 }
113 // Further validate if the specified srcOffset and requested total subsample size
114 // is consistent with the source shared buffer size.
115 if (!validateRangeForSize(srcOffset, totalBytesInSubSamples, srcBuffer.size)) {
116 ALOGE("Invalid srcOffset and subsample size: "
117 "srcOffset %llu, totalBytesInSubSamples %llu, srcBuffer size %llu",
118 srcOffset, totalBytesInSubSamples, srcBuffer.size);
119 android_errorWriteLog(0x534e4554, "67962232");
120 _hidl_cb(toStatus(BAD_VALUE), 0, NULL);
121 return Void();
122 }
123
Chong Zhanga4f67512017-04-24 17:18:25 -0700124 void *srcPtr = (uint8_t *)(void *)srcMem->getPointer() + srcBuffer.offset;
125 void *dstPtr = NULL;
126 if (dstBuffer.type == BufferType::SHARED_MEMORY) {
127 // When using shared memory, src buffer is also used as dst,
128 // we don't map it again here.
129 dstPtr = srcPtr;
Chong Zhang16a3cd02017-10-24 22:42:30 -0700130
131 // In this case the dst and src would be the same buffer, need to validate
132 // dstOffset against the buffer size too.
133 if (!validateRangeForSize(dstOffset, totalBytesInSubSamples, srcBuffer.size)) {
134 ALOGE("Invalid dstOffset and subsample size: "
135 "dstOffset %llu, totalBytesInSubSamples %llu, srcBuffer size %llu",
136 dstOffset, totalBytesInSubSamples, srcBuffer.size);
137 android_errorWriteLog(0x534e4554, "67962232");
138 _hidl_cb(toStatus(BAD_VALUE), 0, NULL);
139 return Void();
140 }
Chong Zhanga4f67512017-04-24 17:18:25 -0700141 } else {
142 native_handle_t *handle = const_cast<native_handle_t *>(
143 dstBuffer.secureMemory.getNativeHandle());
144 dstPtr = static_cast<void *>(handle);
145 }
146 // Casting hidl SubSample to DescramblerPlugin::SubSample, but need
147 // to ensure structs are actually idential
148
149 int32_t result = mPlugin->descramble(
150 dstBuffer.type != BufferType::SHARED_MEMORY,
151 (DescramblerPlugin::ScramblingControl)scramblingControl,
152 subSamples.size(),
153 (DescramblerPlugin::SubSample*)subSamples.data(),
154 srcPtr,
155 srcOffset,
156 dstPtr,
157 dstOffset,
158 NULL);
159
160 _hidl_cb(toStatus(result >= 0 ? OK : result), result, NULL);
161 return Void();
162}
163
164Return<Status> DescramblerImpl::release() {
165 ALOGV("%s: mPlugin=%p", __FUNCTION__, mPlugin);
166
167 if (mPlugin != NULL) {
168 delete mPlugin;
169 mPlugin = NULL;
170 }
171 return Status::OK;
172}
173
174} // namespace implementation
175} // namespace V1_0
176} // namespace cas
177} // namespace hardware
178} // namespace android