blob: ddb6980046ed8de9c67b0d7188963afef3a63c5d [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 android.service.persistentdata;
18
Andres Morales396181b2014-08-13 09:25:53 -070019import android.annotation.SystemApi;
Andres Morales74e9b182016-02-22 12:33:33 -080020import android.annotation.IntDef;
Andres Morales68d4acd2014-07-01 19:40:41 -070021import android.os.RemoteException;
22import android.util.Slog;
23
Andres Morales74e9b182016-02-22 12:33:33 -080024import java.lang.annotation.Retention;
25import java.lang.annotation.RetentionPolicy;
26
Andres Morales68d4acd2014-07-01 19:40:41 -070027/**
28 * Interface for reading and writing data blocks to a persistent partition.
29 *
30 * Allows writing one block at a time. Namely, each time
Andres Morales963295e2014-07-10 15:40:24 -070031 * {@link PersistentDataBlockManager#write(byte[])}
Andres Morales68d4acd2014-07-01 19:40:41 -070032 * is called, it will overwite the data that was previously written on the block.
33 *
34 * Clients can query the size of the currently written block via
Andres Morales963295e2014-07-10 15:40:24 -070035 * {@link PersistentDataBlockManager#getDataBlockSize()}.
Andres Morales68d4acd2014-07-01 19:40:41 -070036 *
Andres Morales963295e2014-07-10 15:40:24 -070037 * Clients can query the maximum size for a block via
Andres Morales396181b2014-08-13 09:25:53 -070038 * {@link PersistentDataBlockManager#getMaximumDataBlockSize()}
Andres Morales963295e2014-07-10 15:40:24 -070039 *
40 * Clients can read the currently written block by invoking
41 * {@link PersistentDataBlockManager#read()}.
Andres Morales68d4acd2014-07-01 19:40:41 -070042 *
43 * @hide
44 */
Andres Morales396181b2014-08-13 09:25:53 -070045@SystemApi
Andres Morales68d4acd2014-07-01 19:40:41 -070046public class PersistentDataBlockManager {
47 private static final String TAG = PersistentDataBlockManager.class.getSimpleName();
48 private IPersistentDataBlockService sService;
49
Andres Morales74e9b182016-02-22 12:33:33 -080050 /**
51 * Indicates that the device's bootloader lock state is UNKNOWN.
52 */
53 public static final int FLASH_LOCK_UNKNOWN = -1;
54 /**
55 * Indicates that the device's bootloader is UNLOCKED.
56 */
57 public static final int FLASH_LOCK_UNLOCKED = 0;
58 /**
59 * Indicates that the device's bootloader is LOCKED.
60 */
61 public static final int FLASH_LOCK_LOCKED = 1;
62
63 @IntDef({
64 FLASH_LOCK_UNKNOWN,
65 FLASH_LOCK_LOCKED,
66 FLASH_LOCK_UNLOCKED,
67 })
68 @Retention(RetentionPolicy.SOURCE)
69 public @interface FlashLockState {}
70
Andres Morales68d4acd2014-07-01 19:40:41 -070071 public PersistentDataBlockManager(IPersistentDataBlockService service) {
72 sService = service;
73 }
74
75 /**
76 * Writes {@code data} to the persistent partition. Previously written data
77 * will be overwritten. This data will persist across factory resets.
78 *
Andres Morales963295e2014-07-10 15:40:24 -070079 * Returns the number of bytes written or -1 on error. If the block is too big
80 * to fit on the partition, returns -MAX_BLOCK_SIZE.
81 *
Andres Morales68d4acd2014-07-01 19:40:41 -070082 * @param data the data to write
83 */
Andres Morales963295e2014-07-10 15:40:24 -070084 public int write(byte[] data) {
Andres Morales68d4acd2014-07-01 19:40:41 -070085 try {
Andres Morales963295e2014-07-10 15:40:24 -070086 return sService.write(data);
Andres Morales68d4acd2014-07-01 19:40:41 -070087 } catch (RemoteException e) {
88 onError("writing data");
Andres Morales68d4acd2014-07-01 19:40:41 -070089 return -1;
90 }
91 }
92
93 /**
Andres Morales963295e2014-07-10 15:40:24 -070094 * Returns the data block stored on the persistent partition.
95 */
96 public byte[] read() {
97 try {
98 return sService.read();
99 } catch (RemoteException e) {
100 onError("reading data");
101 return null;
102 }
103 }
104
105 /**
Andres Morales68d4acd2014-07-01 19:40:41 -0700106 * Retrieves the size of the block currently written to the persistent partition.
Andres Morales963295e2014-07-10 15:40:24 -0700107 *
108 * Return -1 on error.
Andres Morales68d4acd2014-07-01 19:40:41 -0700109 */
110 public int getDataBlockSize() {
111 try {
112 return sService.getDataBlockSize();
113 } catch (RemoteException e) {
114 onError("getting data block size");
Andres Morales963295e2014-07-10 15:40:24 -0700115 return -1;
116 }
117 }
118
119 /**
120 * Retrieves the maximum size allowed for a data block.
121 *
122 * Returns -1 on error.
123 */
124 public long getMaximumDataBlockSize() {
125 try {
126 return sService.getMaximumDataBlockSize();
127 } catch (RemoteException e) {
128 onError("getting maximum data block size");
129 return -1;
130 }
131 }
132
133 /**
134 * Zeroes the previously written block in its entirety. Calling this method
135 * will erase all data written to the persistent data partition.
136 */
137 public void wipe() {
138 try {
139 sService.wipe();
140 } catch (RemoteException e) {
141 onError("wiping persistent partition");
Andres Morales68d4acd2014-07-01 19:40:41 -0700142 }
143 }
144
145 /**
Andres Morales68d4acd2014-07-01 19:40:41 -0700146 * Writes a byte enabling or disabling the ability to "OEM unlock" the device.
147 */
148 public void setOemUnlockEnabled(boolean enabled) {
149 try {
150 sService.setOemUnlockEnabled(enabled);
151 } catch (RemoteException e) {
152 onError("setting OEM unlock enabled to " + enabled);
153 }
154 }
155
156 /**
157 * Returns whether or not "OEM unlock" is enabled or disabled on this device.
158 */
159 public boolean getOemUnlockEnabled() {
160 try {
161 return sService.getOemUnlockEnabled();
162 } catch (RemoteException e) {
163 onError("getting OEM unlock enabled bit");
164 return false;
165 }
166 }
167
Andres Morales74e9b182016-02-22 12:33:33 -0800168 /**
169 * Retrieves available information about this device's flash lock state.
170 *
171 * @return FLASH_LOCK_STATE_LOCKED if device bootloader is locked,
172 * FLASH_LOCK_STATE_UNLOCKED if device bootloader is unlocked,
173 * or FLASH_LOCK_STATE unknown if this information cannot be ascertained
174 * on this device.
175 */
176 @FlashLockState
177 public int getFlashLockState() {
178 try {
179 return sService.getFlashLockState();
180 } catch (RemoteException e) {
181 onError("getting flash lock state");
182 return FLASH_LOCK_UNKNOWN;
183 }
184 }
185
Andres Morales68d4acd2014-07-01 19:40:41 -0700186 private void onError(String msg) {
187 Slog.v(TAG, "Remote exception while " + msg);
188 }
189}