Tao Bao | 43cd1bc | 2016-02-08 09:51:38 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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 | |
| 17 | package android.os; |
| 18 | |
| 19 | import android.annotation.SystemApi; |
| 20 | import android.os.IUpdateEngine; |
| 21 | import android.os.IUpdateEngineCallback; |
| 22 | import android.os.RemoteException; |
| 23 | |
Tao Bao | 43cd1bc | 2016-02-08 09:51:38 -0800 | [diff] [blame] | 24 | /** |
| 25 | * UpdateEngine handles calls to the update engine which takes care of A/B OTA |
| 26 | * updates. It wraps up the update engine Binder APIs and exposes them as |
Elliott Hughes | e3ce3e8b | 2017-02-27 09:37:02 -0800 | [diff] [blame] | 27 | * SystemApis, which will be called by the system app responsible for OTAs. |
| 28 | * On a Google device, this will be GmsCore. |
| 29 | * |
| 30 | * The minimal flow is: |
| 31 | * <ol> |
| 32 | * <li>Create a new UpdateEngine instance. |
| 33 | * <li>Call {@link #bind}, optionally providing callbacks. |
| 34 | * <li>Call {@link #applyPayload}. |
| 35 | * </ol> |
| 36 | * |
| 37 | * In addition, methods are provided to {@link #cancel} or |
| 38 | * {@link #suspend}/{@link #resume} application of an update. |
Tao Bao | 43cd1bc | 2016-02-08 09:51:38 -0800 | [diff] [blame] | 39 | * |
| 40 | * The APIs defined in this class and UpdateEngineCallback class must be in |
| 41 | * sync with the ones in |
| 42 | * system/update_engine/binder_bindings/android/os/IUpdateEngine.aidl and |
| 43 | * system/update_engine/binder_bindings/android/os/IUpdateEngineCallback.aidl. |
| 44 | * |
| 45 | * {@hide} |
| 46 | */ |
| 47 | @SystemApi |
| 48 | public class UpdateEngine { |
| 49 | private static final String TAG = "UpdateEngine"; |
| 50 | |
| 51 | private static final String UPDATE_ENGINE_SERVICE = "android.os.UpdateEngineService"; |
| 52 | |
| 53 | /** |
| 54 | * Error code from the update engine. Values must agree with the ones in |
| 55 | * system/update_engine/common/error_code.h. |
| 56 | */ |
Tao Bao | 43cd1bc | 2016-02-08 09:51:38 -0800 | [diff] [blame] | 57 | public static final class ErrorCodeConstants { |
| 58 | public static final int SUCCESS = 0; |
| 59 | public static final int ERROR = 1; |
| 60 | public static final int FILESYSTEM_COPIER_ERROR = 4; |
| 61 | public static final int POST_INSTALL_RUNNER_ERROR = 5; |
| 62 | public static final int PAYLOAD_MISMATCHED_TYPE_ERROR = 6; |
| 63 | public static final int INSTALL_DEVICE_OPEN_ERROR = 7; |
| 64 | public static final int KERNEL_DEVICE_OPEN_ERROR = 8; |
| 65 | public static final int DOWNLOAD_TRANSFER_ERROR = 9; |
| 66 | public static final int PAYLOAD_HASH_MISMATCH_ERROR = 10; |
| 67 | public static final int PAYLOAD_SIZE_MISMATCH_ERROR = 11; |
| 68 | public static final int DOWNLOAD_PAYLOAD_VERIFICATION_ERROR = 12; |
Alex Kershaw | 7bcb2fa | 2019-01-04 15:17:39 +0000 | [diff] [blame] | 69 | public static final int PAYLOAD_TIMESTAMP_ERROR = 51; |
scypher | f5a5365 | 2017-11-06 14:45:12 -0800 | [diff] [blame] | 70 | public static final int UPDATED_BUT_NOT_ACTIVE = 52; |
Tao Bao | 43cd1bc | 2016-02-08 09:51:38 -0800 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Update status code from the update engine. Values must agree with the |
| 75 | * ones in system/update_engine/client_library/include/update_engine/update_status.h. |
| 76 | */ |
Tao Bao | 43cd1bc | 2016-02-08 09:51:38 -0800 | [diff] [blame] | 77 | public static final class UpdateStatusConstants { |
| 78 | public static final int IDLE = 0; |
| 79 | public static final int CHECKING_FOR_UPDATE = 1; |
| 80 | public static final int UPDATE_AVAILABLE = 2; |
| 81 | public static final int DOWNLOADING = 3; |
| 82 | public static final int VERIFYING = 4; |
| 83 | public static final int FINALIZING = 5; |
| 84 | public static final int UPDATED_NEED_REBOOT = 6; |
| 85 | public static final int REPORTING_ERROR_EVENT = 7; |
| 86 | public static final int ATTEMPTING_ROLLBACK = 8; |
| 87 | public static final int DISABLED = 9; |
| 88 | } |
| 89 | |
| 90 | private IUpdateEngine mUpdateEngine; |
Tao Bao | 445c304 | 2017-07-16 11:21:03 -0700 | [diff] [blame] | 91 | private IUpdateEngineCallback mUpdateEngineCallback = null; |
| 92 | private final Object mUpdateEngineCallbackLock = new Object(); |
Tao Bao | 43cd1bc | 2016-02-08 09:51:38 -0800 | [diff] [blame] | 93 | |
Elliott Hughes | e3ce3e8b | 2017-02-27 09:37:02 -0800 | [diff] [blame] | 94 | /** |
| 95 | * Creates a new instance. |
| 96 | */ |
Tao Bao | 43cd1bc | 2016-02-08 09:51:38 -0800 | [diff] [blame] | 97 | public UpdateEngine() { |
| 98 | mUpdateEngine = IUpdateEngine.Stub.asInterface( |
| 99 | ServiceManager.getService(UPDATE_ENGINE_SERVICE)); |
| 100 | } |
| 101 | |
Elliott Hughes | e3ce3e8b | 2017-02-27 09:37:02 -0800 | [diff] [blame] | 102 | /** |
| 103 | * Prepares this instance for use. The callback will be notified on any |
| 104 | * status change, and when the update completes. A handler can be supplied |
| 105 | * to control which thread runs the callback, or null. |
| 106 | */ |
Tao Bao | b7e47ae | 2016-03-22 12:30:23 -0700 | [diff] [blame] | 107 | public boolean bind(final UpdateEngineCallback callback, final Handler handler) { |
Tao Bao | 445c304 | 2017-07-16 11:21:03 -0700 | [diff] [blame] | 108 | synchronized (mUpdateEngineCallbackLock) { |
| 109 | mUpdateEngineCallback = new IUpdateEngineCallback.Stub() { |
| 110 | @Override |
| 111 | public void onStatusUpdate(final int status, final float percent) { |
| 112 | if (handler != null) { |
| 113 | handler.post(new Runnable() { |
| 114 | @Override |
| 115 | public void run() { |
| 116 | callback.onStatusUpdate(status, percent); |
| 117 | } |
| 118 | }); |
| 119 | } else { |
| 120 | callback.onStatusUpdate(status, percent); |
| 121 | } |
Tao Bao | 43cd1bc | 2016-02-08 09:51:38 -0800 | [diff] [blame] | 122 | } |
Tao Bao | 43cd1bc | 2016-02-08 09:51:38 -0800 | [diff] [blame] | 123 | |
Tao Bao | 445c304 | 2017-07-16 11:21:03 -0700 | [diff] [blame] | 124 | @Override |
| 125 | public void onPayloadApplicationComplete(final int errorCode) { |
| 126 | if (handler != null) { |
| 127 | handler.post(new Runnable() { |
| 128 | @Override |
| 129 | public void run() { |
| 130 | callback.onPayloadApplicationComplete(errorCode); |
| 131 | } |
| 132 | }); |
| 133 | } else { |
| 134 | callback.onPayloadApplicationComplete(errorCode); |
| 135 | } |
Tao Bao | 43cd1bc | 2016-02-08 09:51:38 -0800 | [diff] [blame] | 136 | } |
Tao Bao | 445c304 | 2017-07-16 11:21:03 -0700 | [diff] [blame] | 137 | }; |
Tao Bao | 43cd1bc | 2016-02-08 09:51:38 -0800 | [diff] [blame] | 138 | |
Tao Bao | 445c304 | 2017-07-16 11:21:03 -0700 | [diff] [blame] | 139 | try { |
| 140 | return mUpdateEngine.bind(mUpdateEngineCallback); |
| 141 | } catch (RemoteException e) { |
| 142 | throw e.rethrowFromSystemServer(); |
| 143 | } |
Tao Bao | b7e47ae | 2016-03-22 12:30:23 -0700 | [diff] [blame] | 144 | } |
Tao Bao | 43cd1bc | 2016-02-08 09:51:38 -0800 | [diff] [blame] | 145 | } |
| 146 | |
Elliott Hughes | e3ce3e8b | 2017-02-27 09:37:02 -0800 | [diff] [blame] | 147 | /** |
| 148 | * Equivalent to {@code bind(callback, null)}. |
| 149 | */ |
Tao Bao | b7e47ae | 2016-03-22 12:30:23 -0700 | [diff] [blame] | 150 | public boolean bind(final UpdateEngineCallback callback) { |
Tao Bao | 43cd1bc | 2016-02-08 09:51:38 -0800 | [diff] [blame] | 151 | return bind(callback, null); |
| 152 | } |
| 153 | |
Elliott Hughes | e3ce3e8b | 2017-02-27 09:37:02 -0800 | [diff] [blame] | 154 | /** |
| 155 | * Applies the payload found at the given {@code url}. For non-streaming |
| 156 | * updates, the URL can be a local file using the {@code file://} scheme. |
| 157 | * |
| 158 | * <p>The {@code offset} and {@code size} parameters specify the location |
| 159 | * of the payload within the file represented by the URL. This is useful |
| 160 | * if the downloadable package at the URL contains more than just the |
| 161 | * update_engine payload (such as extra metadata). This is true for |
| 162 | * Google's OTA system, where the URL points to a zip file in which the |
| 163 | * payload is stored uncompressed within the zip file alongside other |
| 164 | * data. |
| 165 | * |
| 166 | * <p>The {@code headerKeyValuePairs} parameter is used to pass metadata |
| 167 | * to update_engine. In Google's implementation, this is stored as |
| 168 | * {@code payload_properties.txt} in the zip file. It's generated by the |
| 169 | * script {@code system/update_engine/scripts/brillo_update_payload}. |
| 170 | * The complete list of keys and their documentation is in |
| 171 | * {@code system/update_engine/common/constants.cc}, but an example |
| 172 | * might be: |
| 173 | * <pre> |
| 174 | * String[] pairs = { |
| 175 | * "FILE_HASH=lURPCIkIAjtMOyB/EjQcl8zDzqtD6Ta3tJef6G/+z2k=", |
| 176 | * "FILE_SIZE=871903868", |
| 177 | * "METADATA_HASH=tBvj43QOB0Jn++JojcpVdbRLz0qdAuL+uTkSy7hokaw=", |
| 178 | * "METADATA_SIZE=70604" |
| 179 | * }; |
| 180 | * </pre> |
| 181 | */ |
Tao Bao | b7e47ae | 2016-03-22 12:30:23 -0700 | [diff] [blame] | 182 | public void applyPayload(String url, long offset, long size, String[] headerKeyValuePairs) { |
| 183 | try { |
| 184 | mUpdateEngine.applyPayload(url, offset, size, headerKeyValuePairs); |
| 185 | } catch (RemoteException e) { |
| 186 | throw e.rethrowFromSystemServer(); |
| 187 | } |
Tao Bao | 43cd1bc | 2016-02-08 09:51:38 -0800 | [diff] [blame] | 188 | } |
| 189 | |
Elliott Hughes | e3ce3e8b | 2017-02-27 09:37:02 -0800 | [diff] [blame] | 190 | /** |
| 191 | * Permanently cancels an in-progress update. |
| 192 | * |
| 193 | * <p>See {@link #resetStatus} to undo a finshed update (only available |
| 194 | * before the updated system has been rebooted). |
| 195 | * |
| 196 | * <p>See {@link #suspend} for a way to temporarily stop an in-progress |
| 197 | * update with the ability to resume it later. |
| 198 | */ |
Tao Bao | b7e47ae | 2016-03-22 12:30:23 -0700 | [diff] [blame] | 199 | public void cancel() { |
| 200 | try { |
| 201 | mUpdateEngine.cancel(); |
| 202 | } catch (RemoteException e) { |
| 203 | throw e.rethrowFromSystemServer(); |
| 204 | } |
Tao Bao | 43cd1bc | 2016-02-08 09:51:38 -0800 | [diff] [blame] | 205 | } |
| 206 | |
Elliott Hughes | e3ce3e8b | 2017-02-27 09:37:02 -0800 | [diff] [blame] | 207 | /** |
| 208 | * Suspends an in-progress update. This can be undone by calling |
| 209 | * {@link #resume}. |
| 210 | */ |
Tao Bao | b7e47ae | 2016-03-22 12:30:23 -0700 | [diff] [blame] | 211 | public void suspend() { |
| 212 | try { |
| 213 | mUpdateEngine.suspend(); |
| 214 | } catch (RemoteException e) { |
| 215 | throw e.rethrowFromSystemServer(); |
| 216 | } |
Tao Bao | 43cd1bc | 2016-02-08 09:51:38 -0800 | [diff] [blame] | 217 | } |
| 218 | |
Elliott Hughes | e3ce3e8b | 2017-02-27 09:37:02 -0800 | [diff] [blame] | 219 | /** |
| 220 | * Resumes a suspended update. |
| 221 | */ |
Tao Bao | b7e47ae | 2016-03-22 12:30:23 -0700 | [diff] [blame] | 222 | public void resume() { |
| 223 | try { |
| 224 | mUpdateEngine.resume(); |
| 225 | } catch (RemoteException e) { |
| 226 | throw e.rethrowFromSystemServer(); |
| 227 | } |
| 228 | } |
| 229 | |
Elliott Hughes | e3ce3e8b | 2017-02-27 09:37:02 -0800 | [diff] [blame] | 230 | /** |
| 231 | * Resets the bootable flag on the non-current partition and all internal |
| 232 | * update_engine state. This can be used after an unwanted payload has been |
| 233 | * successfully applied and the device has not yet been rebooted to signal |
| 234 | * that we no longer want to boot into that updated system. After this call |
| 235 | * completes, update_engine will no longer report |
| 236 | * {@code UPDATED_NEED_REBOOT}, so your callback can remove any outstanding |
| 237 | * notification that rebooting into the new system is possible. |
| 238 | */ |
Tao Bao | b7e47ae | 2016-03-22 12:30:23 -0700 | [diff] [blame] | 239 | public void resetStatus() { |
| 240 | try { |
| 241 | mUpdateEngine.resetStatus(); |
| 242 | } catch (RemoteException e) { |
| 243 | throw e.rethrowFromSystemServer(); |
| 244 | } |
Tao Bao | 43cd1bc | 2016-02-08 09:51:38 -0800 | [diff] [blame] | 245 | } |
Tao Bao | 445c304 | 2017-07-16 11:21:03 -0700 | [diff] [blame] | 246 | |
| 247 | /** |
| 248 | * Unbinds the last bound callback function. |
| 249 | */ |
Tao Bao | 445c304 | 2017-07-16 11:21:03 -0700 | [diff] [blame] | 250 | public boolean unbind() { |
| 251 | synchronized (mUpdateEngineCallbackLock) { |
| 252 | if (mUpdateEngineCallback == null) { |
| 253 | return true; |
| 254 | } |
| 255 | try { |
| 256 | boolean result = mUpdateEngine.unbind(mUpdateEngineCallback); |
| 257 | mUpdateEngineCallback = null; |
| 258 | return result; |
| 259 | } catch (RemoteException e) { |
| 260 | throw e.rethrowFromSystemServer(); |
| 261 | } |
| 262 | } |
| 263 | } |
Tao Bao | d27be34 | 2018-01-10 11:40:19 -0800 | [diff] [blame] | 264 | |
| 265 | /** |
| 266 | * Verifies that a payload associated with the given payload metadata |
| 267 | * {@code payloadMetadataFilename} can be safely applied to ths device. |
| 268 | * Returns {@code true} if the update can successfully be applied and |
| 269 | * returns {@code false} otherwise. |
| 270 | * |
| 271 | * @param payloadMetadataFilename the location of the metadata without the |
| 272 | * {@code file://} prefix. |
| 273 | */ |
Tao Bao | d27be34 | 2018-01-10 11:40:19 -0800 | [diff] [blame] | 274 | public boolean verifyPayloadMetadata(String payloadMetadataFilename) { |
| 275 | try { |
| 276 | return mUpdateEngine.verifyPayloadApplicable(payloadMetadataFilename); |
| 277 | } catch (RemoteException e) { |
| 278 | throw e.rethrowFromSystemServer(); |
| 279 | } |
| 280 | } |
Tao Bao | 43cd1bc | 2016-02-08 09:51:38 -0800 | [diff] [blame] | 281 | } |