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