PersistentDataBlockManager.java revision 3b8b46f3a46ccf35a6bb6a828af0f2d011cc9abe
1/*
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
17package android.service.persistentdata;
18
19import android.annotation.SystemApi;
20import android.annotation.IntDef;
21import android.os.RemoteException;
22import android.util.Slog;
23
24import java.lang.annotation.Retention;
25import java.lang.annotation.RetentionPolicy;
26
27/**
28 * Interface for reading and writing data blocks to a persistent partition.
29 *
30 * Allows writing one block at a time. Namely, each time
31 * {@link PersistentDataBlockManager#write(byte[])}
32 * 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
35 * {@link PersistentDataBlockManager#getDataBlockSize()}.
36 *
37 * Clients can query the maximum size for a block via
38 * {@link PersistentDataBlockManager#getMaximumDataBlockSize()}
39 *
40 * Clients can read the currently written block by invoking
41 * {@link PersistentDataBlockManager#read()}.
42 *
43 * @hide
44 */
45@SystemApi
46public class PersistentDataBlockManager {
47    private static final String TAG = PersistentDataBlockManager.class.getSimpleName();
48    private IPersistentDataBlockService sService;
49
50    /**
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
71    /** @hide */
72    public PersistentDataBlockManager(IPersistentDataBlockService service) {
73        sService = service;
74    }
75
76    /**
77     * Writes {@code data} to the persistent partition. Previously written data
78     * will be overwritten. This data will persist across factory resets.
79     *
80     * Returns the number of bytes written or -1 on error. If the block is too big
81     * to fit on the partition, returns -MAX_BLOCK_SIZE.
82     *
83     * {@link #wipe} will block any further {@link #write} operation until reboot,
84     * in which case -1 will be returned.
85     *
86     * @param data the data to write
87     */
88    public int write(byte[] data) {
89        try {
90            return sService.write(data);
91        } catch (RemoteException e) {
92            throw e.rethrowFromSystemServer();
93        }
94    }
95
96    /**
97     * Returns the data block stored on the persistent partition.
98     */
99    public byte[] read() {
100        try {
101            return sService.read();
102        } catch (RemoteException e) {
103            throw e.rethrowFromSystemServer();
104        }
105    }
106
107    /**
108     * Retrieves the size of the block currently written to the persistent partition.
109     *
110     * Return -1 on error.
111     */
112    public int getDataBlockSize() {
113        try {
114            return sService.getDataBlockSize();
115        } catch (RemoteException e) {
116            throw e.rethrowFromSystemServer();
117        }
118    }
119
120    /**
121     * Retrieves the maximum size allowed for a data block.
122     *
123     * Returns -1 on error.
124     */
125    public long getMaximumDataBlockSize() {
126        try {
127            return sService.getMaximumDataBlockSize();
128        } catch (RemoteException e) {
129            throw e.rethrowFromSystemServer();
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     * It will also prevent any further {@link #write} operation until reboot,
137     * in order to prevent a potential race condition. See b/30352311.
138     */
139    public void wipe() {
140        try {
141            sService.wipe();
142        } catch (RemoteException e) {
143            throw e.rethrowFromSystemServer();
144        }
145    }
146
147    /**
148     * Writes a byte enabling or disabling the ability to "OEM unlock" the device.
149     *
150     * @deprecated use {@link OemLockManager#setOemUnlockAllowedByUser(boolean)} instead.
151     */
152    public void setOemUnlockEnabled(boolean enabled) {
153        try {
154            sService.setOemUnlockEnabled(enabled);
155        } catch (RemoteException e) {
156            throw e.rethrowFromSystemServer();
157        }
158    }
159
160    /**
161     * Returns whether or not "OEM unlock" is enabled or disabled on this device.
162     *
163     * @deprecated use {@link OemLockManager#isOemUnlockAllowedByUser()} instead.
164     */
165    public boolean getOemUnlockEnabled() {
166        try {
167            return sService.getOemUnlockEnabled();
168        } catch (RemoteException e) {
169            throw e.rethrowFromSystemServer();
170        }
171    }
172
173    /**
174     * Retrieves available information about this device's flash lock state.
175     *
176     * @return {@link #FLASH_LOCK_LOCKED} if device bootloader is locked,
177     * {@link #FLASH_LOCK_UNLOCKED} if device bootloader is unlocked, or {@link #FLASH_LOCK_UNKNOWN}
178     * if this information cannot be ascertained on this device.
179     */
180    @FlashLockState
181    public int getFlashLockState() {
182        try {
183            return sService.getFlashLockState();
184        } catch (RemoteException e) {
185            throw e.rethrowFromSystemServer();
186        }
187    }
188}
189