1 /* 2 * Copyright (C) 2008 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 com.android.internal.os; 18 19 import android.os.FileUtils; 20 import android.os.Power; 21 import android.util.Log; 22 23 import java.io.File; 24 import java.io.FileNotFoundException; 25 import java.io.FileWriter; 26 import java.io.IOException; 27 import java.util.Iterator; 28 import java.util.Map; 29 30 /** 31 * Utility class for interacting with the Android recovery partition. 32 * The recovery partition is a small standalone system which can perform 33 * operations that are difficult while the main system is running, like 34 * upgrading system software or reformatting the data partition. 35 * Note that most of these operations must be run as root. 36 * 37 * @hide 38 */ 39 public class RecoverySystem { 40 private static final String TAG = "RecoverySystem"; // for logging 41 42 // Used to communicate with recovery. See commands/recovery/recovery.c. 43 private static File RECOVERY_DIR = new File("/cache/recovery"); 44 private static File COMMAND_FILE = new File(RECOVERY_DIR, "command"); 45 private static File LOG_FILE = new File(RECOVERY_DIR, "log"); 46 47 // Length limits for reading files. 48 private static int LOG_FILE_MAX_LENGTH = 8 * 1024; 49 50 /** 51 * Reboot into the recovery system to install a system update. 52 * @param update package to install (must be in /cache or /data). 53 * @throws IOException if something goes wrong. 54 */ rebootAndUpdate(File update)55 public static void rebootAndUpdate(File update) throws IOException { 56 String path = update.getCanonicalPath(); 57 if (path.startsWith("/cache/")) { 58 path = "CACHE:" + path.substring(7); 59 } else if (path.startsWith("/data/")) { 60 path = "DATA:" + path.substring(6); 61 } else { 62 throw new IllegalArgumentException( 63 "Must start with /cache or /data: " + path); 64 } 65 bootCommand("--update_package=" + path); 66 } 67 68 /** 69 * Reboot into the recovery system to wipe the /data partition. 70 * @param extras to add to the RECOVERY_COMPLETED intent after rebooting. 71 * @throws IOException if something goes wrong. 72 */ rebootAndWipe()73 public static void rebootAndWipe() throws IOException { 74 bootCommand("--wipe_data"); 75 } 76 77 /** 78 * Reboot into the recovery system with the supplied argument. 79 * @param arg to pass to the recovery utility. 80 * @throws IOException if something goes wrong. 81 */ bootCommand(String arg)82 private static void bootCommand(String arg) throws IOException { 83 RECOVERY_DIR.mkdirs(); // In case we need it 84 COMMAND_FILE.delete(); // In case it's not writable 85 LOG_FILE.delete(); 86 87 FileWriter command = new FileWriter(COMMAND_FILE); 88 try { 89 command.write(arg); 90 command.write("\n"); 91 } finally { 92 command.close(); 93 } 94 95 // Having written the command file, go ahead and reboot 96 Power.reboot("recovery"); 97 throw new IOException("Reboot failed (no permissions?)"); 98 } 99 100 /** 101 * Called after booting to process and remove recovery-related files. 102 * @return the log file from recovery, or null if none was found. 103 */ handleAftermath()104 public static String handleAftermath() { 105 // Record the tail of the LOG_FILE 106 String log = null; 107 try { 108 log = FileUtils.readTextFile(LOG_FILE, -LOG_FILE_MAX_LENGTH, "...\n"); 109 } catch (FileNotFoundException e) { 110 Log.i(TAG, "No recovery log file"); 111 } catch (IOException e) { 112 Log.e(TAG, "Error reading recovery log", e); 113 } 114 115 // Delete everything in RECOVERY_DIR 116 String[] names = RECOVERY_DIR.list(); 117 for (int i = 0; names != null && i < names.length; i++) { 118 File f = new File(RECOVERY_DIR, names[i]); 119 if (!f.delete()) { 120 Log.e(TAG, "Can't delete: " + f); 121 } else { 122 Log.i(TAG, "Deleted: " + f); 123 } 124 } 125 126 return log; 127 } 128 } 129