Andres Morales | 963295e | 2014-07-10 15:40:24 -0700 | [diff] [blame] | 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 | |
Andres Morales | 68d4acd | 2014-07-01 19:40:41 -0700 | [diff] [blame] | 17 | package android.service.persistentdata; |
| 18 | |
Andres Morales | 396181b | 2014-08-13 09:25:53 -0700 | [diff] [blame] | 19 | import android.annotation.SystemApi; |
Andres Morales | 74e9b18 | 2016-02-22 12:33:33 -0800 | [diff] [blame] | 20 | import android.annotation.IntDef; |
Andres Morales | 68d4acd | 2014-07-01 19:40:41 -0700 | [diff] [blame] | 21 | import android.os.RemoteException; |
| 22 | import android.util.Slog; |
| 23 | |
Andres Morales | 74e9b18 | 2016-02-22 12:33:33 -0800 | [diff] [blame] | 24 | import java.lang.annotation.Retention; |
| 25 | import java.lang.annotation.RetentionPolicy; |
| 26 | |
Andres Morales | 68d4acd | 2014-07-01 19:40:41 -0700 | [diff] [blame] | 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 |
Andres Morales | 963295e | 2014-07-10 15:40:24 -0700 | [diff] [blame] | 31 | * {@link PersistentDataBlockManager#write(byte[])} |
Andres Morales | 68d4acd | 2014-07-01 19:40:41 -0700 | [diff] [blame] | 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 |
Andres Morales | 963295e | 2014-07-10 15:40:24 -0700 | [diff] [blame] | 35 | * {@link PersistentDataBlockManager#getDataBlockSize()}. |
Andres Morales | 68d4acd | 2014-07-01 19:40:41 -0700 | [diff] [blame] | 36 | * |
Andres Morales | 963295e | 2014-07-10 15:40:24 -0700 | [diff] [blame] | 37 | * Clients can query the maximum size for a block via |
Andres Morales | 396181b | 2014-08-13 09:25:53 -0700 | [diff] [blame] | 38 | * {@link PersistentDataBlockManager#getMaximumDataBlockSize()} |
Andres Morales | 963295e | 2014-07-10 15:40:24 -0700 | [diff] [blame] | 39 | * |
| 40 | * Clients can read the currently written block by invoking |
| 41 | * {@link PersistentDataBlockManager#read()}. |
Andres Morales | 68d4acd | 2014-07-01 19:40:41 -0700 | [diff] [blame] | 42 | * |
| 43 | * @hide |
| 44 | */ |
Andres Morales | 396181b | 2014-08-13 09:25:53 -0700 | [diff] [blame] | 45 | @SystemApi |
Andres Morales | 68d4acd | 2014-07-01 19:40:41 -0700 | [diff] [blame] | 46 | public class PersistentDataBlockManager { |
| 47 | private static final String TAG = PersistentDataBlockManager.class.getSimpleName(); |
| 48 | private IPersistentDataBlockService sService; |
| 49 | |
Andres Morales | 74e9b18 | 2016-02-22 12:33:33 -0800 | [diff] [blame] | 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 | |
Andres Morales | 68d4acd | 2014-07-01 19:40:41 -0700 | [diff] [blame] | 71 | 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 Morales | 963295e | 2014-07-10 15:40:24 -0700 | [diff] [blame] | 79 | * 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 Morales | 68d4acd | 2014-07-01 19:40:41 -0700 | [diff] [blame] | 82 | * @param data the data to write |
| 83 | */ |
Andres Morales | 963295e | 2014-07-10 15:40:24 -0700 | [diff] [blame] | 84 | public int write(byte[] data) { |
Andres Morales | 68d4acd | 2014-07-01 19:40:41 -0700 | [diff] [blame] | 85 | try { |
Andres Morales | 963295e | 2014-07-10 15:40:24 -0700 | [diff] [blame] | 86 | return sService.write(data); |
Andres Morales | 68d4acd | 2014-07-01 19:40:41 -0700 | [diff] [blame] | 87 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 88 | throw e.rethrowFromSystemServer(); |
Andres Morales | 68d4acd | 2014-07-01 19:40:41 -0700 | [diff] [blame] | 89 | } |
| 90 | } |
| 91 | |
| 92 | /** |
Andres Morales | 963295e | 2014-07-10 15:40:24 -0700 | [diff] [blame] | 93 | * Returns the data block stored on the persistent partition. |
| 94 | */ |
| 95 | public byte[] read() { |
| 96 | try { |
| 97 | return sService.read(); |
| 98 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 99 | throw e.rethrowFromSystemServer(); |
Andres Morales | 963295e | 2014-07-10 15:40:24 -0700 | [diff] [blame] | 100 | } |
| 101 | } |
| 102 | |
| 103 | /** |
Andres Morales | 68d4acd | 2014-07-01 19:40:41 -0700 | [diff] [blame] | 104 | * Retrieves the size of the block currently written to the persistent partition. |
Andres Morales | 963295e | 2014-07-10 15:40:24 -0700 | [diff] [blame] | 105 | * |
| 106 | * Return -1 on error. |
Andres Morales | 68d4acd | 2014-07-01 19:40:41 -0700 | [diff] [blame] | 107 | */ |
| 108 | public int getDataBlockSize() { |
| 109 | try { |
| 110 | return sService.getDataBlockSize(); |
| 111 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 112 | throw e.rethrowFromSystemServer(); |
Andres Morales | 963295e | 2014-07-10 15:40:24 -0700 | [diff] [blame] | 113 | } |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * Retrieves the maximum size allowed for a data block. |
| 118 | * |
| 119 | * Returns -1 on error. |
| 120 | */ |
| 121 | public long getMaximumDataBlockSize() { |
| 122 | try { |
| 123 | return sService.getMaximumDataBlockSize(); |
| 124 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 125 | throw e.rethrowFromSystemServer(); |
Andres Morales | 963295e | 2014-07-10 15:40:24 -0700 | [diff] [blame] | 126 | } |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * Zeroes the previously written block in its entirety. Calling this method |
| 131 | * will erase all data written to the persistent data partition. |
| 132 | */ |
| 133 | public void wipe() { |
| 134 | try { |
| 135 | sService.wipe(); |
| 136 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 137 | throw e.rethrowFromSystemServer(); |
Andres Morales | 68d4acd | 2014-07-01 19:40:41 -0700 | [diff] [blame] | 138 | } |
| 139 | } |
| 140 | |
| 141 | /** |
Andres Morales | 68d4acd | 2014-07-01 19:40:41 -0700 | [diff] [blame] | 142 | * Writes a byte enabling or disabling the ability to "OEM unlock" the device. |
| 143 | */ |
| 144 | public void setOemUnlockEnabled(boolean enabled) { |
| 145 | try { |
| 146 | sService.setOemUnlockEnabled(enabled); |
| 147 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 148 | throw e.rethrowFromSystemServer(); |
Andres Morales | 68d4acd | 2014-07-01 19:40:41 -0700 | [diff] [blame] | 149 | } |
| 150 | } |
| 151 | |
| 152 | /** |
| 153 | * Returns whether or not "OEM unlock" is enabled or disabled on this device. |
| 154 | */ |
| 155 | public boolean getOemUnlockEnabled() { |
| 156 | try { |
| 157 | return sService.getOemUnlockEnabled(); |
| 158 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 159 | throw e.rethrowFromSystemServer(); |
Andres Morales | 68d4acd | 2014-07-01 19:40:41 -0700 | [diff] [blame] | 160 | } |
| 161 | } |
| 162 | |
Andres Morales | 74e9b18 | 2016-02-22 12:33:33 -0800 | [diff] [blame] | 163 | /** |
| 164 | * Retrieves available information about this device's flash lock state. |
| 165 | * |
| 166 | * @return FLASH_LOCK_STATE_LOCKED if device bootloader is locked, |
| 167 | * FLASH_LOCK_STATE_UNLOCKED if device bootloader is unlocked, |
| 168 | * or FLASH_LOCK_STATE unknown if this information cannot be ascertained |
| 169 | * on this device. |
| 170 | */ |
| 171 | @FlashLockState |
| 172 | public int getFlashLockState() { |
| 173 | try { |
| 174 | return sService.getFlashLockState(); |
| 175 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 176 | throw e.rethrowFromSystemServer(); |
Andres Morales | 74e9b18 | 2016-02-22 12:33:33 -0800 | [diff] [blame] | 177 | } |
| 178 | } |
Andres Morales | 68d4acd | 2014-07-01 19:40:41 -0700 | [diff] [blame] | 179 | } |