diff --git a/src/com/koushikdutta/droidx/bootstrap/BootService.java b/src/com/koushikdutta/droidx/bootstrap/BootService.java index 6a72df1..71084f2 100644 --- a/src/com/koushikdutta/droidx/bootstrap/BootService.java +++ b/src/com/koushikdutta/droidx/bootstrap/BootService.java @@ -4,8 +4,11 @@ import android.content.Intent; import android.os.Handler; import android.os.IBinder; +import android.util.Log; public class BootService extends Service { + + private static final String TAG = "DXB/BootService"; @Override public void onStart(Intent intent, int startId) { @@ -22,14 +25,23 @@ public void run() { String adbd = filesDir + "/adbd"; StringBuilder command = new StringBuilder(); + // prevent recovery from booting here + Log.d(TAG, "Removing recovery_mode trigger"); command.append("rm /data/.recovery_mode ; "); - // restart adbd as root - command.append(busybox + " mount -orw,remount / ; "); - command.append("mv /sbin/adbd /sbin/adbd.old ; "); - command.append(busybox + " cp " + adbd + " /sbin/adbd ; "); - command.append(busybox + " mount -oro,remount / ; "); - command.append(busybox + " kill $(ps | " + busybox + " grep adbd) ;"); + + ROMBootstrapSettings settings = new ROMBootstrapSettings(); + + if(settings.restartAdb()) { + // restart adbd as root + Log.d(TAG, "Restarting ADB as Root"); + command.append(busybox + " mount -orw,remount / ; "); + command.append("mv /sbin/adbd /sbin/adbd.old ; "); + command.append(busybox + " cp " + adbd + " /sbin/adbd ; "); + command.append(busybox + " mount -oro,remount / ; "); + command.append(busybox + " kill $(ps | " + busybox + " grep adbd) ;"); + } + try { Helper.runSuCommand(BootService.this, command.toString()); } diff --git a/src/com/koushikdutta/droidx/bootstrap/Bootstrap.java b/src/com/koushikdutta/droidx/bootstrap/Bootstrap.java index e039cfc..8ad8136 100644 --- a/src/com/koushikdutta/droidx/bootstrap/Bootstrap.java +++ b/src/com/koushikdutta/droidx/bootstrap/Bootstrap.java @@ -20,6 +20,9 @@ import android.widget.Button; public class Bootstrap extends Activity { + + private static final String TAG = "DXB/Bootstrap"; + /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { @@ -41,25 +44,39 @@ public void onClick(View v) { String adbd = filesDir + "/adbd"; StringBuilder command = new StringBuilder(); - command.append(busybox + " mount -oremount,rw /system ; "); - command.append(busybox + " cp " + logwrapper + " /system/bin/logwrapper.bin ; "); - command.append(busybox + " cp " + hijack + " /system/bin/hijack ; "); - command.append("cd /system/bin ; rm logwrapper ; ln -s hijack logwrapper ; "); - command.append(busybox + " mount -oremount,ro /system ; "); - command.append(busybox + " mkdir -p /preinstall/recovery ; "); - command.append(busybox + " cp " + updatebinary + " /preinstall/recovery/update-binary ; "); - command.append(busybox + " cp " + recoveryzip + " /preinstall/recovery/recovery.zip ; "); - command.append(busybox + " cp " + hijack + " /preinstall/recovery/hijack ; "); - command.append(busybox + " cp " + logwrapper + " /preinstall/recovery/logwrapper ; "); + + ROMBootstrapSettings settings = new ROMBootstrapSettings(); + + if(settings.installHijack()) { + Log.d(TAG, "Installing hijack"); + command.append(busybox + " mount -oremount,rw /system ; "); + command.append(busybox + " cp " + logwrapper + " /system/bin/logwrapper.bin ; "); + command.append(busybox + " cp " + hijack + " /system/bin/hijack ; "); + command.append("cd /system/bin ; rm logwrapper ; ln -s hijack logwrapper ; "); + command.append(busybox + " mount -oremount,ro /system ; "); + } + + if(settings.installRecovery()) { + Log.d(TAG, "Installing recovery"); + command.append(busybox + " mkdir -p /preinstall/recovery ; "); + command.append(busybox + " cp " + updatebinary + " /preinstall/recovery/update-binary ; "); + command.append(busybox + " cp " + recoveryzip + " /preinstall/recovery/recovery.zip ; "); + command.append(busybox + " cp " + hijack + " /preinstall/recovery/hijack ; "); + command.append(busybox + " cp " + logwrapper + " /preinstall/recovery/logwrapper ; "); + } - // restart adbd as root - command.append(busybox + " mount -orw,remount / ; "); - command.append("mv /sbin/adbd /sbin/adbd.old ; "); - command.append(busybox + " cp " + adbd + " /sbin/adbd ; "); - command.append(busybox + " mount -oro,remount / ; "); - command.append(busybox + " kill $(ps | " + busybox + " grep adbd) ;"); + if(settings.restartAdb()) { + // restart adbd as root + Log.d(TAG, "Restarting ADB as Root"); + command.append(busybox + " mount -orw,remount / ; "); + command.append("mv /sbin/adbd /sbin/adbd.old ; "); + command.append(busybox + " cp " + adbd + " /sbin/adbd ; "); + command.append(busybox + " mount -oro,remount / ; "); + command.append(busybox + " kill $(ps | " + busybox + " grep adbd) ;"); + } // prevent recovery from booting here + Log.d(TAG, "Removing recovery_mode trigger"); command.append("rm /data/.recovery_mode ; "); AlertDialog.Builder builder = new Builder(Bootstrap.this); diff --git a/src/com/koushikdutta/droidx/bootstrap/ROMBootstrapSettings.java b/src/com/koushikdutta/droidx/bootstrap/ROMBootstrapSettings.java new file mode 100644 index 0000000..4a13120 --- /dev/null +++ b/src/com/koushikdutta/droidx/bootstrap/ROMBootstrapSettings.java @@ -0,0 +1,67 @@ +package com.koushikdutta.droidx.bootstrap; + +import java.io.File; +import java.io.FileInputStream; + +import android.util.Log; + +import org.json.JSONObject; + +public class ROMBootstrapSettings { + private static final String TAG = "DXB/ROMBootstrapSettings"; + + private static final File SETTINGS_FILE = new File("/system/etc/DroidXBootstrap.cfg"); + private static final String RESTART_ADB_KEY = "restart_adb"; + private static final String INSTALL_HIJACK_KEY = "install_hijack"; + private static final String INSTALL_RECOVERY_KEY = "install_recovery"; + + private boolean mRestartAdb = true; + private boolean mInstallHijack = true; + private boolean mInstallRecovery = true; + + public ROMBootstrapSettings() { + if(SETTINGS_FILE.exists()) { + Log.d(TAG, "Found settings file, parsing"); + + FileInputStream f = null; + String data = ""; + + try { + // read file into a string + byte[] buffer = new byte[(int) SETTINGS_FILE.length()]; + f = new FileInputStream(SETTINGS_FILE); + f.read(buffer); + data = new String(buffer); + } catch(Exception e) { + Log.e(TAG, "Error reading settings file", e); + } finally { + // ensure the stream is closed + if(f != null) try { f.close(); } catch(Exception ignored) { } + } + + try { + // parse as JSON + JSONObject json = new JSONObject(data); + + if(json.has(RESTART_ADB_KEY)) { + mRestartAdb = json.optBoolean(RESTART_ADB_KEY, true); + Log.d(TAG, "Setting [RestartAdb] to [" + (mRestartAdb ? "true" : "false") + "]"); + } + if(json.has(INSTALL_HIJACK_KEY)) { + mInstallHijack = json.optBoolean(INSTALL_HIJACK_KEY, true); + Log.d(TAG, "Setting [InstallHijack] to [" + (mInstallHijack ? "true" : "false") + "]"); + } + if(json.has(INSTALL_RECOVERY_KEY)) { + mInstallRecovery = json.optBoolean(INSTALL_RECOVERY_KEY, true); + Log.d(TAG, "Setting [InstallRecovery] to [" + (mInstallRecovery ? "true" : "false") + "]"); + } + } catch(Exception e) { + Log.e(TAG, "Error parsing settings file", e); + } + } + } + + public boolean restartAdb() { return mRestartAdb; } + public boolean installHijack() { return mInstallHijack; } + public boolean installRecovery() { return mInstallRecovery; } +}