blob: 680547ab93656983a907969ac1c3f542635bfe21 [file] [log] [blame]
Andres Morales963295e2014-07-10 15:40:24 -07001/*
2 * Copyright (C) 2014 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
Andres Morales68d4acd2014-07-01 19:40:41 -070017package com.android.server;
18
19import android.Manifest;
Guang Zhu514c5802014-09-12 15:14:00 -070020import android.app.ActivityManager;
Andres Morales68d4acd2014-07-01 19:40:41 -070021import android.content.Context;
22import android.content.pm.PackageManager;
23import android.os.Binder;
24import android.os.IBinder;
25import android.os.RemoteException;
26import android.os.SystemProperties;
Andres Morales6429f312014-08-04 16:35:15 -070027import android.os.UserHandle;
Xiaohui Chenf0660782015-09-02 14:25:19 -070028import android.os.UserManager;
Andres Morales68d4acd2014-07-01 19:40:41 -070029import android.service.persistentdata.IPersistentDataBlockService;
Andres Morales74e9b182016-02-22 12:33:33 -080030import android.service.persistentdata.PersistentDataBlockManager;
Andres Morales963295e2014-07-10 15:40:24 -070031import android.util.Slog;
Guang Zhu514c5802014-09-12 15:14:00 -070032
Andres Morales68d4acd2014-07-01 19:40:41 -070033import com.android.internal.R;
Guang Zhu514c5802014-09-12 15:14:00 -070034
Andres Morales963295e2014-07-10 15:40:24 -070035import libcore.io.IoUtils;
Andres Morales68d4acd2014-07-01 19:40:41 -070036
37import java.io.DataInputStream;
38import java.io.DataOutputStream;
39import java.io.File;
40import java.io.FileInputStream;
41import java.io.FileNotFoundException;
42import java.io.FileOutputStream;
43import java.io.IOException;
44import java.nio.ByteBuffer;
45import java.nio.channels.FileChannel;
Andres Morales28301302014-11-12 07:56:46 -080046import java.security.MessageDigest;
47import java.security.NoSuchAlgorithmException;
48import java.util.Arrays;
Andres Morales68d4acd2014-07-01 19:40:41 -070049
50/**
51 * Service for reading and writing blocks to a persistent partition.
Andres Morales963295e2014-07-10 15:40:24 -070052 * This data will live across factory resets not initiated via the Settings UI.
53 * When a device is factory reset through Settings this data is wiped.
Andres Morales68d4acd2014-07-01 19:40:41 -070054 *
55 * Allows writing one block at a time. Namely, each time
56 * {@link android.service.persistentdata.IPersistentDataBlockService}.write(byte[] data)
57 * is called, it will overwite the data that was previously written on the block.
58 *
59 * Clients can query the size of the currently written block via
60 * {@link android.service.persistentdata.IPersistentDataBlockService}.getTotalDataSize().
61 *
62 * Clients can any number of bytes from the currently written block up to its total size by invoking
63 * {@link android.service.persistentdata.IPersistentDataBlockService}.read(byte[] data)
64 */
65public class PersistentDataBlockService extends SystemService {
66 private static final String TAG = PersistentDataBlockService.class.getSimpleName();
67
68 private static final String PERSISTENT_DATA_BLOCK_PROP = "ro.frp.pst";
69 private static final int HEADER_SIZE = 8;
Andres Morales963295e2014-07-10 15:40:24 -070070 // Magic number to mark block device as adhering to the format consumed by this service
Andres Morales28301302014-11-12 07:56:46 -080071 private static final int PARTITION_TYPE_MARKER = 0x19901873;
Andres Morales963295e2014-07-10 15:40:24 -070072 // Limit to 100k as blocks larger than this might cause strain on Binder.
Andres Morales963295e2014-07-10 15:40:24 -070073 private static final int MAX_DATA_BLOCK_SIZE = 1024 * 100;
Andres Morales28301302014-11-12 07:56:46 -080074 public static final int DIGEST_SIZE_BYTES = 32;
Andres Morales5ca4cc52015-03-19 16:37:54 -070075 private static final String OEM_UNLOCK_PROP = "sys.oem_unlock_allowed";
Andres Morales74e9b182016-02-22 12:33:33 -080076 private static final String FLASH_LOCK_PROP = "ro.boot.flash.locked";
77 private static final String FLASH_LOCK_LOCKED = "1";
78 private static final String FLASH_LOCK_UNLOCKED = "0";
Andres Morales68d4acd2014-07-01 19:40:41 -070079
80 private final Context mContext;
81 private final String mDataBlockFile;
Andres Morales963295e2014-07-10 15:40:24 -070082 private final Object mLock = new Object();
Andres Morales6429f312014-08-04 16:35:15 -070083
Andres Moralesa31c23d2014-10-30 15:31:31 -070084 private int mAllowedUid = -1;
Andres Morales963295e2014-07-10 15:40:24 -070085 private long mBlockDeviceSize;
Andres Morales68d4acd2014-07-01 19:40:41 -070086
87 public PersistentDataBlockService(Context context) {
88 super(context);
89 mContext = context;
90 mDataBlockFile = SystemProperties.get(PERSISTENT_DATA_BLOCK_PROP);
Andres Morales963295e2014-07-10 15:40:24 -070091 mBlockDeviceSize = -1; // Load lazily
Xiaohui Chenf0660782015-09-02 14:25:19 -070092 mAllowedUid = getAllowedUid(UserHandle.USER_SYSTEM);
Andres Morales6429f312014-08-04 16:35:15 -070093 }
94
Andres Moralesa31c23d2014-10-30 15:31:31 -070095 private int getAllowedUid(int userHandle) {
Andres Morales6429f312014-08-04 16:35:15 -070096 String allowedPackage = mContext.getResources()
Andres Morales68d4acd2014-07-01 19:40:41 -070097 .getString(R.string.config_persistentDataPackageName);
98 PackageManager pm = mContext.getPackageManager();
99 int allowedUid = -1;
100 try {
Jeff Sharkeyc5967e92016-01-07 18:50:29 -0700101 allowedUid = pm.getPackageUidAsUser(allowedPackage,
102 PackageManager.MATCH_SYSTEM_ONLY, userHandle);
Andres Morales68d4acd2014-07-01 19:40:41 -0700103 } catch (PackageManager.NameNotFoundException e) {
104 // not expected
Andres Morales963295e2014-07-10 15:40:24 -0700105 Slog.e(TAG, "not able to find package " + allowedPackage, e);
Andres Morales68d4acd2014-07-01 19:40:41 -0700106 }
Andres Moralesa31c23d2014-10-30 15:31:31 -0700107 return allowedUid;
Andres Morales68d4acd2014-07-01 19:40:41 -0700108 }
109
110 @Override
111 public void onStart() {
Andres Morales28301302014-11-12 07:56:46 -0800112 enforceChecksumValidity();
Andres Morales1ce7d172015-01-07 14:24:57 -0800113 formatIfOemUnlockEnabled();
Andres Morales68d4acd2014-07-01 19:40:41 -0700114 publishBinderService(Context.PERSISTENT_DATA_BLOCK_SERVICE, mService);
115 }
116
Andres Morales1ce7d172015-01-07 14:24:57 -0800117 private void formatIfOemUnlockEnabled() {
Andres Morales5ca4cc52015-03-19 16:37:54 -0700118 boolean enabled = doGetOemUnlockEnabled();
119 if (enabled) {
Andres Morales1ce7d172015-01-07 14:24:57 -0800120 synchronized (mLock) {
Andres Moralesc8f952c2015-03-19 08:34:55 -0700121 formatPartitionLocked(true);
Andres Morales1ce7d172015-01-07 14:24:57 -0800122 }
123 }
Andres Morales5ca4cc52015-03-19 16:37:54 -0700124
125 SystemProperties.set(OEM_UNLOCK_PROP, enabled ? "1" : "0");
Andres Morales1ce7d172015-01-07 14:24:57 -0800126 }
127
Amith Yamasanid2b21042016-06-03 10:12:47 -0700128 private void enforceOemUnlockReadPermission() {
129 if (mContext.checkCallingOrSelfPermission(Manifest.permission.READ_OEM_UNLOCK_STATE)
130 == PackageManager.PERMISSION_DENIED
131 && mContext.checkCallingOrSelfPermission(Manifest.permission.OEM_UNLOCK_STATE)
132 == PackageManager.PERMISSION_DENIED) {
133 throw new SecurityException("Can't access OEM unlock state. Requires "
134 + "READ_OEM_UNLOCK_STATE or OEM_UNLOCK_STATE permission.");
135 }
136 }
137
138 private void enforceOemUnlockWritePermission() {
Andres Morales68d4acd2014-07-01 19:40:41 -0700139 mContext.enforceCallingOrSelfPermission(
140 Manifest.permission.OEM_UNLOCK_STATE,
Amith Yamasanid2b21042016-06-03 10:12:47 -0700141 "Can't modify OEM unlock state");
Andres Morales68d4acd2014-07-01 19:40:41 -0700142 }
143
144 private void enforceUid(int callingUid) {
Andres Moralesa31c23d2014-10-30 15:31:31 -0700145 if (callingUid != mAllowedUid) {
Andres Morales68d4acd2014-07-01 19:40:41 -0700146 throw new SecurityException("uid " + callingUid + " not allowed to access PST");
147 }
148 }
149
Xiaohui Chenf0660782015-09-02 14:25:19 -0700150 private void enforceIsAdmin() {
151 final int userId = UserHandle.getCallingUserId();
152 final boolean isAdmin = UserManager.get(mContext).isUserAdmin(userId);
153 if (!isAdmin) {
154 throw new SecurityException(
155 "Only the Admin user is allowed to change OEM unlock state");
Andres Moralesa31c23d2014-10-30 15:31:31 -0700156 }
157 }
Andres Morales963295e2014-07-10 15:40:24 -0700158 private int getTotalDataSizeLocked(DataInputStream inputStream) throws IOException {
Andres Morales28301302014-11-12 07:56:46 -0800159 // skip over checksum
160 inputStream.skipBytes(DIGEST_SIZE_BYTES);
161
Andres Morales68d4acd2014-07-01 19:40:41 -0700162 int totalDataSize;
163 int blockId = inputStream.readInt();
Andres Morales963295e2014-07-10 15:40:24 -0700164 if (blockId == PARTITION_TYPE_MARKER) {
Andres Morales68d4acd2014-07-01 19:40:41 -0700165 totalDataSize = inputStream.readInt();
166 } else {
167 totalDataSize = 0;
168 }
169 return totalDataSize;
170 }
171
Andres Morales963295e2014-07-10 15:40:24 -0700172 private long getBlockDeviceSize() {
173 synchronized (mLock) {
174 if (mBlockDeviceSize == -1) {
175 mBlockDeviceSize = nativeGetBlockDeviceSize(mDataBlockFile);
Andres Morales68d4acd2014-07-01 19:40:41 -0700176 }
177 }
178
179 return mBlockDeviceSize;
180 }
181
Andres Morales28301302014-11-12 07:56:46 -0800182 private boolean enforceChecksumValidity() {
183 byte[] storedDigest = new byte[DIGEST_SIZE_BYTES];
184
185 synchronized (mLock) {
186 byte[] digest = computeDigestLocked(storedDigest);
187 if (digest == null || !Arrays.equals(storedDigest, digest)) {
188 Slog.i(TAG, "Formatting FRP partition...");
Andres Moralesc8f952c2015-03-19 08:34:55 -0700189 formatPartitionLocked(false);
Andres Morales28301302014-11-12 07:56:46 -0800190 return false;
191 }
192 }
193
194 return true;
195 }
196
197 private boolean computeAndWriteDigestLocked() {
198 byte[] digest = computeDigestLocked(null);
199 if (digest != null) {
200 DataOutputStream outputStream;
201 try {
202 outputStream = new DataOutputStream(
203 new FileOutputStream(new File(mDataBlockFile)));
204 } catch (FileNotFoundException e) {
205 Slog.e(TAG, "partition not available?", e);
206 return false;
207 }
208
209 try {
210 outputStream.write(digest, 0, DIGEST_SIZE_BYTES);
211 outputStream.flush();
212 } catch (IOException e) {
213 Slog.e(TAG, "failed to write block checksum", e);
214 return false;
215 } finally {
216 IoUtils.closeQuietly(outputStream);
217 }
218 return true;
219 } else {
220 return false;
221 }
222 }
223
224 private byte[] computeDigestLocked(byte[] storedDigest) {
225 DataInputStream inputStream;
226 try {
227 inputStream = new DataInputStream(new FileInputStream(new File(mDataBlockFile)));
228 } catch (FileNotFoundException e) {
229 Slog.e(TAG, "partition not available?", e);
230 return null;
231 }
232
233 MessageDigest md;
234 try {
235 md = MessageDigest.getInstance("SHA-256");
236 } catch (NoSuchAlgorithmException e) {
237 // won't ever happen -- every implementation is required to support SHA-256
238 Slog.e(TAG, "SHA-256 not supported?", e);
239 IoUtils.closeQuietly(inputStream);
240 return null;
241 }
242
243 try {
244 if (storedDigest != null && storedDigest.length == DIGEST_SIZE_BYTES) {
245 inputStream.read(storedDigest);
246 } else {
247 inputStream.skipBytes(DIGEST_SIZE_BYTES);
248 }
249
250 int read;
251 byte[] data = new byte[1024];
252 md.update(data, 0, DIGEST_SIZE_BYTES); // include 0 checksum in digest
253 while ((read = inputStream.read(data)) != -1) {
254 md.update(data, 0, read);
255 }
256 } catch (IOException e) {
257 Slog.e(TAG, "failed to read partition", e);
258 return null;
259 } finally {
260 IoUtils.closeQuietly(inputStream);
261 }
262
263 return md.digest();
264 }
265
Andres Moralesc8f952c2015-03-19 08:34:55 -0700266 private void formatPartitionLocked(boolean setOemUnlockEnabled) {
Andres Morales28301302014-11-12 07:56:46 -0800267 DataOutputStream outputStream;
268 try {
269 outputStream = new DataOutputStream(new FileOutputStream(new File(mDataBlockFile)));
270 } catch (FileNotFoundException e) {
271 Slog.e(TAG, "partition not available?", e);
272 return;
273 }
274
275 byte[] data = new byte[DIGEST_SIZE_BYTES];
276 try {
277 outputStream.write(data, 0, DIGEST_SIZE_BYTES);
278 outputStream.writeInt(PARTITION_TYPE_MARKER);
279 outputStream.writeInt(0); // data size
280 outputStream.flush();
281 } catch (IOException e) {
282 Slog.e(TAG, "failed to format block", e);
283 return;
284 } finally {
285 IoUtils.closeQuietly(outputStream);
286 }
287
Andres Moralesc8f952c2015-03-19 08:34:55 -0700288 doSetOemUnlockEnabledLocked(setOemUnlockEnabled);
Andres Morales28301302014-11-12 07:56:46 -0800289 computeAndWriteDigestLocked();
290 }
291
292 private void doSetOemUnlockEnabledLocked(boolean enabled) {
293 FileOutputStream outputStream;
294 try {
295 outputStream = new FileOutputStream(new File(mDataBlockFile));
296 } catch (FileNotFoundException e) {
297 Slog.e(TAG, "partition not available", e);
298 return;
299 }
300
301 try {
302 FileChannel channel = outputStream.getChannel();
303
304 channel.position(getBlockDeviceSize() - 1);
305
306 ByteBuffer data = ByteBuffer.allocate(1);
307 data.put(enabled ? (byte) 1 : (byte) 0);
308 data.flip();
309 channel.write(data);
310 outputStream.flush();
311 } catch (IOException e) {
312 Slog.e(TAG, "unable to access persistent partition", e);
313 return;
314 } finally {
Andres Morales5ca4cc52015-03-19 16:37:54 -0700315 SystemProperties.set(OEM_UNLOCK_PROP, enabled ? "1" : "0");
Andres Morales28301302014-11-12 07:56:46 -0800316 IoUtils.closeQuietly(outputStream);
317 }
318 }
319
Andres Morales1ce7d172015-01-07 14:24:57 -0800320 private boolean doGetOemUnlockEnabled() {
321 DataInputStream inputStream;
322 try {
323 inputStream = new DataInputStream(new FileInputStream(new File(mDataBlockFile)));
324 } catch (FileNotFoundException e) {
325 Slog.e(TAG, "partition not available");
326 return false;
327 }
328
329 try {
330 synchronized (mLock) {
331 inputStream.skip(getBlockDeviceSize() - 1);
332 return inputStream.readByte() != 0;
333 }
334 } catch (IOException e) {
335 Slog.e(TAG, "unable to access persistent partition", e);
336 return false;
337 } finally {
338 IoUtils.closeQuietly(inputStream);
339 }
340 }
341
Andres Morales963295e2014-07-10 15:40:24 -0700342 private native long nativeGetBlockDeviceSize(String path);
343 private native int nativeWipe(String path);
Andres Morales68d4acd2014-07-01 19:40:41 -0700344
345 private final IBinder mService = new IPersistentDataBlockService.Stub() {
346 @Override
347 public int write(byte[] data) throws RemoteException {
348 enforceUid(Binder.getCallingUid());
349
350 // Need to ensure we don't write over the last byte
Andres Morales963295e2014-07-10 15:40:24 -0700351 long maxBlockSize = getBlockDeviceSize() - HEADER_SIZE - 1;
352 if (data.length > maxBlockSize) {
353 // partition is ~500k so shouldn't be a problem to downcast
354 return (int) -maxBlockSize;
Andres Morales68d4acd2014-07-01 19:40:41 -0700355 }
356
357 DataOutputStream outputStream;
358 try {
359 outputStream = new DataOutputStream(new FileOutputStream(new File(mDataBlockFile)));
360 } catch (FileNotFoundException e) {
Andres Morales963295e2014-07-10 15:40:24 -0700361 Slog.e(TAG, "partition not available?", e);
Andres Morales68d4acd2014-07-01 19:40:41 -0700362 return -1;
363 }
364
365 ByteBuffer headerAndData = ByteBuffer.allocate(data.length + HEADER_SIZE);
Andres Morales963295e2014-07-10 15:40:24 -0700366 headerAndData.putInt(PARTITION_TYPE_MARKER);
Andres Morales68d4acd2014-07-01 19:40:41 -0700367 headerAndData.putInt(data.length);
368 headerAndData.put(data);
369
Andres Morales28301302014-11-12 07:56:46 -0800370 synchronized (mLock) {
Andres Morales68d4acd2014-07-01 19:40:41 -0700371 try {
Andres Morales28301302014-11-12 07:56:46 -0800372 byte[] checksum = new byte[DIGEST_SIZE_BYTES];
373 outputStream.write(checksum, 0, DIGEST_SIZE_BYTES);
374 outputStream.write(headerAndData.array());
375 outputStream.flush();
Andres Morales68d4acd2014-07-01 19:40:41 -0700376 } catch (IOException e) {
Andres Morales28301302014-11-12 07:56:46 -0800377 Slog.e(TAG, "failed writing to the persistent data block", e);
378 return -1;
379 } finally {
380 IoUtils.closeQuietly(outputStream);
381 }
382
383 if (computeAndWriteDigestLocked()) {
384 return data.length;
385 } else {
386 return -1;
Andres Morales68d4acd2014-07-01 19:40:41 -0700387 }
388 }
389 }
390
391 @Override
Andres Morales963295e2014-07-10 15:40:24 -0700392 public byte[] read() {
Andres Morales68d4acd2014-07-01 19:40:41 -0700393 enforceUid(Binder.getCallingUid());
Andres Morales28301302014-11-12 07:56:46 -0800394 if (!enforceChecksumValidity()) {
395 return new byte[0];
396 }
Andres Morales68d4acd2014-07-01 19:40:41 -0700397
398 DataInputStream inputStream;
399 try {
400 inputStream = new DataInputStream(new FileInputStream(new File(mDataBlockFile)));
401 } catch (FileNotFoundException e) {
Andres Morales963295e2014-07-10 15:40:24 -0700402 Slog.e(TAG, "partition not available?", e);
403 return null;
Andres Morales68d4acd2014-07-01 19:40:41 -0700404 }
405
406 try {
Andres Morales963295e2014-07-10 15:40:24 -0700407 synchronized (mLock) {
408 int totalDataSize = getTotalDataSizeLocked(inputStream);
409
410 if (totalDataSize == 0) {
411 return new byte[0];
412 }
413
414 byte[] data = new byte[totalDataSize];
415 int read = inputStream.read(data, 0, totalDataSize);
416 if (read < totalDataSize) {
417 // something went wrong, not returning potentially corrupt data
418 Slog.e(TAG, "failed to read entire data block. bytes read: " +
419 read + "/" + totalDataSize);
420 return null;
421 }
422 return data;
423 }
Andres Morales68d4acd2014-07-01 19:40:41 -0700424 } catch (IOException e) {
Andres Morales963295e2014-07-10 15:40:24 -0700425 Slog.e(TAG, "failed to read data", e);
426 return null;
Andres Morales68d4acd2014-07-01 19:40:41 -0700427 } finally {
428 try {
429 inputStream.close();
430 } catch (IOException e) {
Andres Morales963295e2014-07-10 15:40:24 -0700431 Slog.e(TAG, "failed to close OutputStream");
432 }
433 }
434 }
435
436 @Override
437 public void wipe() {
Amith Yamasanid2b21042016-06-03 10:12:47 -0700438 enforceOemUnlockWritePermission();
Andres Morales963295e2014-07-10 15:40:24 -0700439
440 synchronized (mLock) {
441 int ret = nativeWipe(mDataBlockFile);
442
443 if (ret < 0) {
444 Slog.e(TAG, "failed to wipe persistent partition");
Andres Morales68d4acd2014-07-01 19:40:41 -0700445 }
446 }
447 }
448
449 @Override
450 public void setOemUnlockEnabled(boolean enabled) {
Guang Zhu514c5802014-09-12 15:14:00 -0700451 // do not allow monkey to flip the flag
452 if (ActivityManager.isUserAMonkey()) {
453 return;
454 }
Amith Yamasanid2b21042016-06-03 10:12:47 -0700455 enforceOemUnlockWritePermission();
Xiaohui Chenf0660782015-09-02 14:25:19 -0700456 enforceIsAdmin();
Andres Morales68d4acd2014-07-01 19:40:41 -0700457
Andres Morales28301302014-11-12 07:56:46 -0800458 synchronized (mLock) {
459 doSetOemUnlockEnabledLocked(enabled);
460 computeAndWriteDigestLocked();
Andres Morales68d4acd2014-07-01 19:40:41 -0700461 }
462 }
463
464 @Override
465 public boolean getOemUnlockEnabled() {
Amith Yamasanid2b21042016-06-03 10:12:47 -0700466 enforceOemUnlockReadPermission();
Andres Morales1ce7d172015-01-07 14:24:57 -0800467 return doGetOemUnlockEnabled();
Andres Morales68d4acd2014-07-01 19:40:41 -0700468 }
469
470 @Override
Andres Morales74e9b182016-02-22 12:33:33 -0800471 public int getFlashLockState() {
Amith Yamasanid2b21042016-06-03 10:12:47 -0700472 enforceOemUnlockReadPermission();
Andres Morales74e9b182016-02-22 12:33:33 -0800473 String locked = SystemProperties.get(FLASH_LOCK_PROP);
474 switch (locked) {
475 case FLASH_LOCK_LOCKED:
476 return PersistentDataBlockManager.FLASH_LOCK_LOCKED;
477 case FLASH_LOCK_UNLOCKED:
478 return PersistentDataBlockManager.FLASH_LOCK_UNLOCKED;
479 default:
480 return PersistentDataBlockManager.FLASH_LOCK_UNKNOWN;
481 }
482 }
483
484 @Override
Andres Morales68d4acd2014-07-01 19:40:41 -0700485 public int getDataBlockSize() {
Craig Lafayette66445a62015-03-27 09:01:43 -0400486 enforcePersistentDataBlockAccess();
Andres Morales68d4acd2014-07-01 19:40:41 -0700487
488 DataInputStream inputStream;
489 try {
490 inputStream = new DataInputStream(new FileInputStream(new File(mDataBlockFile)));
491 } catch (FileNotFoundException e) {
Andres Morales963295e2014-07-10 15:40:24 -0700492 Slog.e(TAG, "partition not available");
Andres Morales68d4acd2014-07-01 19:40:41 -0700493 return 0;
494 }
495
496 try {
Andres Morales963295e2014-07-10 15:40:24 -0700497 synchronized (mLock) {
498 return getTotalDataSizeLocked(inputStream);
499 }
Andres Morales68d4acd2014-07-01 19:40:41 -0700500 } catch (IOException e) {
Andres Morales963295e2014-07-10 15:40:24 -0700501 Slog.e(TAG, "error reading data block size");
Andres Morales68d4acd2014-07-01 19:40:41 -0700502 return 0;
503 } finally {
Andres Morales963295e2014-07-10 15:40:24 -0700504 IoUtils.closeQuietly(inputStream);
Andres Morales68d4acd2014-07-01 19:40:41 -0700505 }
506 }
Andres Morales963295e2014-07-10 15:40:24 -0700507
Craig Lafayette66445a62015-03-27 09:01:43 -0400508 private void enforcePersistentDataBlockAccess() {
509 if (mContext.checkCallingPermission(Manifest.permission.ACCESS_PDB_STATE)
510 != PackageManager.PERMISSION_GRANTED) {
511 enforceUid(Binder.getCallingUid());
512 }
513 }
514
Andres Morales963295e2014-07-10 15:40:24 -0700515 @Override
516 public long getMaximumDataBlockSize() {
517 long actualSize = getBlockDeviceSize() - HEADER_SIZE - 1;
518 return actualSize <= MAX_DATA_BLOCK_SIZE ? actualSize : MAX_DATA_BLOCK_SIZE;
519 }
Andres Morales68d4acd2014-07-01 19:40:41 -0700520 };
521}