Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pull changes to allow roms that require it to alter bootstrap's behavior #1

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 18 additions & 6 deletions src/com/koushikdutta/droidx/bootstrap/BootService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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());
}
Expand Down
49 changes: 33 additions & 16 deletions src/com/koushikdutta/droidx/bootstrap/Bootstrap.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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);
Expand Down
67 changes: 67 additions & 0 deletions src/com/koushikdutta/droidx/bootstrap/ROMBootstrapSettings.java
Original file line number Diff line number Diff line change
@@ -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; }
}