blob: 8ba02037977bfa093fcc4d463fe34c43a0d69006 [file] [log] [blame]
aimitakeshi27ed8ad2010-07-29 10:12:27 +09001/*
2 * Copyright (C) 2010 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
Takeshi Aimi2272ee22010-09-20 23:40:41 +090017//#define LOG_NDEBUG 0
aimitakeshi27ed8ad2010-07-29 10:12:27 +090018#define LOG_TAG "DrmManagerService(Native)"
19#include <utils/Log.h>
20
Takeshi Aimi34738462010-11-16 13:56:11 +090021#include <private/android_filesystem_config.h>
James Dong8635b7b2011-03-14 17:01:38 -070022#include <media/MemoryLeakTrackUtil.h>
Takeshi Aimi34738462010-11-16 13:56:11 +090023
aimitakeshi27ed8ad2010-07-29 10:12:27 +090024#include <errno.h>
25#include <utils/threads.h>
26#include <binder/IServiceManager.h>
Takeshi Aimi34738462010-11-16 13:56:11 +090027#include <binder/IPCThreadState.h>
aimitakeshi27ed8ad2010-07-29 10:12:27 +090028#include <sys/stat.h>
29#include "DrmManagerService.h"
30#include "DrmManager.h"
31
32using namespace android;
33
Takeshi Aimi34738462010-11-16 13:56:11 +090034static Vector<uid_t> trustedUids;
35
36static bool isProtectedCallAllowed() {
37 // TODO
38 // Following implementation is just for reference.
39 // Each OEM manufacturer should implement/replace with their own solutions.
40 bool result = false;
41
42 IPCThreadState* ipcState = IPCThreadState::self();
43 uid_t uid = ipcState->getCallingUid();
44
45 for (unsigned int i = 0; i < trustedUids.size(); ++i) {
46 if (trustedUids[i] == uid) {
47 result = true;
48 break;
49 }
50 }
51 return result;
52}
53
aimitakeshi27ed8ad2010-07-29 10:12:27 +090054void DrmManagerService::instantiate() {
Steve Block3856b092011-10-20 11:56:00 +010055 ALOGV("instantiate");
Takeshi Aimie943f842010-10-08 23:05:49 +090056 defaultServiceManager()->addService(String16("drm.drmManager"), new DrmManagerService());
Takeshi Aimi34738462010-11-16 13:56:11 +090057
58 if (0 >= trustedUids.size()) {
59 // TODO
60 // Following implementation is just for reference.
61 // Each OEM manufacturer should implement/replace with their own solutions.
62
63 // Add trusted uids here
64 trustedUids.push(AID_MEDIA);
65 }
aimitakeshi27ed8ad2010-07-29 10:12:27 +090066}
67
Takeshi Aimie943f842010-10-08 23:05:49 +090068DrmManagerService::DrmManagerService() :
69 mDrmManager(NULL) {
Steve Block3856b092011-10-20 11:56:00 +010070 ALOGV("created");
aimitakeshi27ed8ad2010-07-29 10:12:27 +090071 mDrmManager = new DrmManager();
Takeshi Aimie943f842010-10-08 23:05:49 +090072 mDrmManager->loadPlugIns();
aimitakeshi27ed8ad2010-07-29 10:12:27 +090073}
74
75DrmManagerService::~DrmManagerService() {
Steve Block3856b092011-10-20 11:56:00 +010076 ALOGV("Destroyed");
Takeshi Aimie943f842010-10-08 23:05:49 +090077 mDrmManager->unloadPlugIns();
aimitakeshi27ed8ad2010-07-29 10:12:27 +090078 delete mDrmManager; mDrmManager = NULL;
79}
80
Gloria Wang8f001512011-07-21 15:10:22 -070081int DrmManagerService::addUniqueId(bool isNative) {
82 return mDrmManager->addUniqueId(isNative);
Takeshi Aimi2272ee22010-09-20 23:40:41 +090083}
84
85void DrmManagerService::removeUniqueId(int uniqueId) {
86 mDrmManager->removeUniqueId(uniqueId);
87}
88
Takeshi Aimie943f842010-10-08 23:05:49 +090089void DrmManagerService::addClient(int uniqueId) {
90 mDrmManager->addClient(uniqueId);
aimitakeshi27ed8ad2010-07-29 10:12:27 +090091}
92
Takeshi Aimie943f842010-10-08 23:05:49 +090093void DrmManagerService::removeClient(int uniqueId) {
94 mDrmManager->removeClient(uniqueId);
aimitakeshi27ed8ad2010-07-29 10:12:27 +090095}
96
97status_t DrmManagerService::setDrmServiceListener(
98 int uniqueId, const sp<IDrmServiceListener>& drmServiceListener) {
Steve Block3856b092011-10-20 11:56:00 +010099 ALOGV("Entering setDrmServiceListener");
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900100 mDrmManager->setDrmServiceListener(uniqueId, drmServiceListener);
101 return DRM_NO_ERROR;
102}
103
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900104status_t DrmManagerService::installDrmEngine(int uniqueId, const String8& drmEngineFile) {
Steve Block3856b092011-10-20 11:56:00 +0100105 ALOGV("Entering installDrmEngine");
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900106 return mDrmManager->installDrmEngine(uniqueId, drmEngineFile);
107}
108
109DrmConstraints* DrmManagerService::getConstraints(
110 int uniqueId, const String8* path, const int action) {
Steve Block3856b092011-10-20 11:56:00 +0100111 ALOGV("Entering getConstraints from content");
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900112 return mDrmManager->getConstraints(uniqueId, path, action);
113}
114
Takeshi Aimi34738462010-11-16 13:56:11 +0900115DrmMetadata* DrmManagerService::getMetadata(int uniqueId, const String8* path) {
Steve Block3856b092011-10-20 11:56:00 +0100116 ALOGV("Entering getMetadata from content");
Takeshi Aimi34738462010-11-16 13:56:11 +0900117 return mDrmManager->getMetadata(uniqueId, path);
118}
119
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900120bool DrmManagerService::canHandle(int uniqueId, const String8& path, const String8& mimeType) {
Steve Block3856b092011-10-20 11:56:00 +0100121 ALOGV("Entering canHandle");
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900122 return mDrmManager->canHandle(uniqueId, path, mimeType);
123}
124
125DrmInfoStatus* DrmManagerService::processDrmInfo(int uniqueId, const DrmInfo* drmInfo) {
Steve Block3856b092011-10-20 11:56:00 +0100126 ALOGV("Entering processDrmInfo");
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900127 return mDrmManager->processDrmInfo(uniqueId, drmInfo);
128}
129
130DrmInfo* DrmManagerService::acquireDrmInfo(int uniqueId, const DrmInfoRequest* drmInfoRequest) {
Steve Block3856b092011-10-20 11:56:00 +0100131 ALOGV("Entering acquireDrmInfo");
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900132 return mDrmManager->acquireDrmInfo(uniqueId, drmInfoRequest);
133}
134
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900135status_t DrmManagerService::saveRights(
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900136 int uniqueId, const DrmRights& drmRights,
137 const String8& rightsPath, const String8& contentPath) {
Steve Block3856b092011-10-20 11:56:00 +0100138 ALOGV("Entering saveRights");
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900139 return mDrmManager->saveRights(uniqueId, drmRights, rightsPath, contentPath);
140}
141
142String8 DrmManagerService::getOriginalMimeType(int uniqueId, const String8& path) {
Steve Block3856b092011-10-20 11:56:00 +0100143 ALOGV("Entering getOriginalMimeType");
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900144 return mDrmManager->getOriginalMimeType(uniqueId, path);
145}
146
147int DrmManagerService::getDrmObjectType(
148 int uniqueId, const String8& path, const String8& mimeType) {
Steve Block3856b092011-10-20 11:56:00 +0100149 ALOGV("Entering getDrmObjectType");
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900150 return mDrmManager->getDrmObjectType(uniqueId, path, mimeType);
151}
152
153int DrmManagerService::checkRightsStatus(
154 int uniqueId, const String8& path, int action) {
Steve Block3856b092011-10-20 11:56:00 +0100155 ALOGV("Entering checkRightsStatus");
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900156 return mDrmManager->checkRightsStatus(uniqueId, path, action);
157}
158
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900159status_t DrmManagerService::consumeRights(
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900160 int uniqueId, DecryptHandle* decryptHandle, int action, bool reserve) {
Steve Block3856b092011-10-20 11:56:00 +0100161 ALOGV("Entering consumeRights");
James Dong328745b2012-02-28 13:55:55 -0800162 if (!isProtectedCallAllowed()) {
163 return DRM_ERROR_NO_PERMISSION;
164 }
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900165 return mDrmManager->consumeRights(uniqueId, decryptHandle, action, reserve);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900166}
167
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900168status_t DrmManagerService::setPlaybackStatus(
Gloria Wanga2cd44c2010-11-19 15:19:36 -0800169 int uniqueId, DecryptHandle* decryptHandle, int playbackStatus, int64_t position) {
Steve Block3856b092011-10-20 11:56:00 +0100170 ALOGV("Entering setPlaybackStatus");
James Dong328745b2012-02-28 13:55:55 -0800171 if (!isProtectedCallAllowed()) {
172 return DRM_ERROR_NO_PERMISSION;
173 }
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900174 return mDrmManager->setPlaybackStatus(uniqueId, decryptHandle, playbackStatus, position);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900175}
176
177bool DrmManagerService::validateAction(
178 int uniqueId, const String8& path,
179 int action, const ActionDescription& description) {
Steve Block3856b092011-10-20 11:56:00 +0100180 ALOGV("Entering validateAction");
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900181 return mDrmManager->validateAction(uniqueId, path, action, description);
182}
183
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900184status_t DrmManagerService::removeRights(int uniqueId, const String8& path) {
Steve Block3856b092011-10-20 11:56:00 +0100185 ALOGV("Entering removeRights");
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900186 return mDrmManager->removeRights(uniqueId, path);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900187}
188
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900189status_t DrmManagerService::removeAllRights(int uniqueId) {
Steve Block3856b092011-10-20 11:56:00 +0100190 ALOGV("Entering removeAllRights");
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900191 return mDrmManager->removeAllRights(uniqueId);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900192}
193
194int DrmManagerService::openConvertSession(int uniqueId, const String8& mimeType) {
Steve Block3856b092011-10-20 11:56:00 +0100195 ALOGV("Entering openConvertSession");
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900196 return mDrmManager->openConvertSession(uniqueId, mimeType);
197}
198
199DrmConvertedStatus* DrmManagerService::convertData(
200 int uniqueId, int convertId, const DrmBuffer* inputData) {
Steve Block3856b092011-10-20 11:56:00 +0100201 ALOGV("Entering convertData");
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900202 return mDrmManager->convertData(uniqueId, convertId, inputData);
203}
204
205DrmConvertedStatus* DrmManagerService::closeConvertSession(int uniqueId, int convertId) {
Steve Block3856b092011-10-20 11:56:00 +0100206 ALOGV("Entering closeConvertSession");
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900207 return mDrmManager->closeConvertSession(uniqueId, convertId);
208}
209
210status_t DrmManagerService::getAllSupportInfo(
211 int uniqueId, int* length, DrmSupportInfo** drmSupportInfoArray) {
Steve Block3856b092011-10-20 11:56:00 +0100212 ALOGV("Entering getAllSupportInfo");
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900213 return mDrmManager->getAllSupportInfo(uniqueId, length, drmSupportInfoArray);
214}
215
216DecryptHandle* DrmManagerService::openDecryptSession(
James Dong9d2f3862012-01-10 08:24:37 -0800217 int uniqueId, int fd, off64_t offset, off64_t length, const char* mime) {
Steve Block3856b092011-10-20 11:56:00 +0100218 ALOGV("Entering DrmManagerService::openDecryptSession");
Takeshi Aimi34738462010-11-16 13:56:11 +0900219 if (isProtectedCallAllowed()) {
James Dong9d2f3862012-01-10 08:24:37 -0800220 return mDrmManager->openDecryptSession(uniqueId, fd, offset, length, mime);
Takeshi Aimi34738462010-11-16 13:56:11 +0900221 }
222
223 return NULL;
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900224}
225
Takeshi Aimie943f842010-10-08 23:05:49 +0900226DecryptHandle* DrmManagerService::openDecryptSession(
James Dong9d2f3862012-01-10 08:24:37 -0800227 int uniqueId, const char* uri, const char* mime) {
Steve Block3856b092011-10-20 11:56:00 +0100228 ALOGV("Entering DrmManagerService::openDecryptSession with uri");
Takeshi Aimi34738462010-11-16 13:56:11 +0900229 if (isProtectedCallAllowed()) {
James Dong9d2f3862012-01-10 08:24:37 -0800230 return mDrmManager->openDecryptSession(uniqueId, uri, mime);
Takeshi Aimi34738462010-11-16 13:56:11 +0900231 }
232
233 return NULL;
Takeshi Aimie943f842010-10-08 23:05:49 +0900234}
235
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900236status_t DrmManagerService::closeDecryptSession(int uniqueId, DecryptHandle* decryptHandle) {
Steve Block3856b092011-10-20 11:56:00 +0100237 ALOGV("Entering closeDecryptSession");
James Dong328745b2012-02-28 13:55:55 -0800238 if (!isProtectedCallAllowed()) {
239 return DRM_ERROR_NO_PERMISSION;
240 }
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900241 return mDrmManager->closeDecryptSession(uniqueId, decryptHandle);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900242}
243
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900244status_t DrmManagerService::initializeDecryptUnit(int uniqueId, DecryptHandle* decryptHandle,
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900245 int decryptUnitId, const DrmBuffer* headerInfo) {
Steve Block3856b092011-10-20 11:56:00 +0100246 ALOGV("Entering initializeDecryptUnit");
James Dong328745b2012-02-28 13:55:55 -0800247 if (!isProtectedCallAllowed()) {
248 return DRM_ERROR_NO_PERMISSION;
249 }
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900250 return mDrmManager->initializeDecryptUnit(uniqueId,decryptHandle, decryptUnitId, headerInfo);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900251}
252
253status_t DrmManagerService::decrypt(
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900254 int uniqueId, DecryptHandle* decryptHandle, int decryptUnitId,
255 const DrmBuffer* encBuffer, DrmBuffer** decBuffer, DrmBuffer* IV) {
Steve Block3856b092011-10-20 11:56:00 +0100256 ALOGV("Entering decrypt");
James Dong328745b2012-02-28 13:55:55 -0800257 if (!isProtectedCallAllowed()) {
258 return DRM_ERROR_NO_PERMISSION;
259 }
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900260 return mDrmManager->decrypt(uniqueId, decryptHandle, decryptUnitId, encBuffer, decBuffer, IV);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900261}
262
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900263status_t DrmManagerService::finalizeDecryptUnit(
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900264 int uniqueId, DecryptHandle* decryptHandle, int decryptUnitId) {
Steve Block3856b092011-10-20 11:56:00 +0100265 ALOGV("Entering finalizeDecryptUnit");
James Dong328745b2012-02-28 13:55:55 -0800266 if (!isProtectedCallAllowed()) {
267 return DRM_ERROR_NO_PERMISSION;
268 }
Takeshi Aimi2272ee22010-09-20 23:40:41 +0900269 return mDrmManager->finalizeDecryptUnit(uniqueId, decryptHandle, decryptUnitId);
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900270}
271
272ssize_t DrmManagerService::pread(int uniqueId, DecryptHandle* decryptHandle,
Gloria Wanga2cd44c2010-11-19 15:19:36 -0800273 void* buffer, ssize_t numBytes, off64_t offset) {
Steve Block3856b092011-10-20 11:56:00 +0100274 ALOGV("Entering pread");
James Dong328745b2012-02-28 13:55:55 -0800275 if (!isProtectedCallAllowed()) {
276 return DRM_ERROR_NO_PERMISSION;
277 }
aimitakeshi27ed8ad2010-07-29 10:12:27 +0900278 return mDrmManager->pread(uniqueId, decryptHandle, buffer, numBytes, offset);
279}
280
James Dong8635b7b2011-03-14 17:01:38 -0700281status_t DrmManagerService::dump(int fd, const Vector<String16>& args)
282{
283 const size_t SIZE = 256;
284 char buffer[SIZE];
285 String8 result;
286 if (checkCallingPermission(String16("android.permission.DUMP")) == false) {
287 snprintf(buffer, SIZE, "Permission Denial: "
288 "can't dump DrmManagerService from pid=%d, uid=%d\n",
289 IPCThreadState::self()->getCallingPid(),
290 IPCThreadState::self()->getCallingUid());
291 result.append(buffer);
292 } else {
293#if DRM_MEMORY_LEAK_TRACK
294 bool dumpMem = false;
295 for (size_t i = 0; i < args.size(); i++) {
296 if (args[i] == String16("-m")) {
297 dumpMem = true;
298 }
299 }
300 if (dumpMem) {
301 dumpMemoryAddresses(fd);
302 }
303#endif
304 }
305 write(fd, result.string(), result.size());
306 return NO_ERROR;
307}
308