Firefly-RK series (eg: RK3288 RK3368) one-click access root privilege tool RootUtils

Keywords: shell Java github Android

Problem description

More and more intelligent devices are using Firefly's development board. http://dev.t-firefly.com/forum.php Sometimes android application development must obtain root privileges (such as restarting devices, silently updating apps). Generally, manufacturers will provide ways to obtain root privileges, but there are always people who do not know how to get root.

Please respect the original, reprint the need to indicate the origin, Big Brother's blog: https://blog.csdn.net/qq137722697

Solution

Firefly Forum has a post about getting root privileges. Portal The premise is that the device can be connected to the computer and operated through adb.
1. The device is connected to the computer and debugged by ADB.

2. Download the attachments root.tar and quick_root.tar, decompress quick_root.tar (terminal running tar xf quick_root.tar) > Best decompress on the computer

3. Open the terminal and run the following commands

adb remount
adb push root.tar system/usr/root.tar
adb push quick_root.sh system/usr/
adb shell 

Then run

root@rk3288:/ # cd system/usr/                                                 
root@rk3288:/system/usr # chmod 777 quick_root.sh                              
root@rk3288:/system/usr # ./quick_root.sh 

It will install and configure related files automatically, and reboot automatically after configuring. After reboot, it will get ROOT privileges.

Exceptional case

There are always special cases, such as a batch of devices I come into contact with without debugging interfaces, so I can not connect to the computer (you may say wireless ADB connection, unfortunately wifi has been banned, only wired) for ADB debugging; you may think that you can run ADB commands on android devices, yes, yes, google provides it. Such a tool, Download Address Portal The tool is used in the same way as adb is used in computers. Ibid.

Ultimate solution

Is there a simpler way to get root privileges by one click? Yes, here's the solution.

One-click access to Root privilege tool RootUtils --> Mode 1: https://fir.im/7pw9
Mode 2: (Scanning 2-D code)

Usage: Click on "Get Root Rights", wait a moment, the device restart is complete, the real one-click access

Now let's talk about how to achieve it. The blogger is really good. The source code has been published, and we don't want to know about it.

Implementation method

Just one line of code, believe it or not.

ShellUtils.execCommand("remount \n push file:///android_asset/root.tar system/usr/root.tar \npush file:///android_asset/quick_root.sh system/usr/\ncd system/usr/\nchmod 777 quick_root.sh\n./quick_root.sh ", false);

You're kidding me. Shell Utils is not a system API. There must be more than one line of code.

Let's take a look at what this method achieves by executing a shell command (each command ends with \ n), copying root.tar and quick_root.sh under the assets folder into the system/usr folder, and then executing the quick_root.sh script to automatically get root.

First step

So the first step is to put root.tar and quick_root.sh under the assets folder (it doesn't matter where you put them, as long as app s can read them).

The second step

Let's see how Shell Utils is implemented (this kind comes from the Internet, thanks to the author)

package com.hdl.rootutils;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.List;

/**
 * Shell Tool class
 * Created by HDL on 2018/8/6.
 */

public class ShellUtils {
    public static final String COMMAND_SU = "su";
    public static final String COMMAND_SH = "sh";
    public static final String COMMAND_EXIT = "exit\n";
    public static final String COMMAND_LINE_END = "\n";

    private ShellUtils() {
        throw new AssertionError();
    }

    /**
     * Check to see if there is root permission
     *
     * @return
     */
    public static boolean checkRootPermission() {
        return execCommand("echo root", true, false).result == 0;
    }


    /**
     * Execute the shell command and return the result by default
     *
     * @param command command
     * @return
     * @see ShellUtils#execCommand(String[], boolean, boolean)
     */
    public static CommandResult execCommand(String command, boolean isRoot) {
        return execCommand(new String[]{command}, isRoot, true);
    }


    /**
     * Execute the shell command and return the result by default
     *
     * @param commands command list
     * @return
     * @see ShellUtils#execCommand(String[], boolean, boolean)
     */

    public static CommandResult execCommand(List<String> commands, boolean isRoot) {
        return execCommand(commands == null ? null : commands.toArray(new String[]{}), isRoot, true);
    }


    /**
     * Execute the shell command and return the result by default
     *
     * @param commands command array
     * @return
     * @see ShellUtils#execCommand(String[], boolean, boolean)
     */

    public static CommandResult execCommand(String[] commands, boolean isRoot) {
        return execCommand(commands, isRoot, true);
    }


    /**
     * execute shell command
     *
     * @param command         command
     * @param isNeedResultMsg whether need result msg
     * @return
     * @see ShellUtils#execCommand(String[], boolean, boolean)
     */
    public static CommandResult execCommand(String command, boolean isRoot, boolean isNeedResultMsg) {
        return execCommand(new String[]{command}, isRoot, isNeedResultMsg);
    }


    /**
     * execute shell commands
     *
     * @param commands command list
     * @return
     * @see ShellUtils#execCommand(String[], boolean, boolean)
     */
    public static CommandResult execCommand(List<String> commands, boolean isRoot, boolean isNeedResultMsg) {

        return execCommand(commands == null ? null : commands.toArray(new String[]{}), isRoot, isNeedResultMsg);
    }


    /**
     * execute shell commands
     */
    public static CommandResult execCommand(String[] commands, boolean isRoot, boolean isNeedResultMsg) {
        int result = -1;
        if (commands == null || commands.length == 0) {
            return new CommandResult(result, null, null);
        }
        Process process = null;
        BufferedReader successResult = null;
        BufferedReader errorResult = null;
        StringBuilder successMsg = null;
        StringBuilder errorMsg = null;
        DataOutputStream os = null;
        try {
            process = Runtime.getRuntime().exec(isRoot ? COMMAND_SU : COMMAND_SH);
            os = new DataOutputStream(process.getOutputStream());
            for (String command : commands) {
                if (command == null) {
                    continue;
                }
                // donnot use os.writeBytes(commmand), avoid chinese charset
                // error
                os.write(command.getBytes());
                os.writeBytes(COMMAND_LINE_END);
                os.flush();
            }
            os.writeBytes(COMMAND_EXIT);
            os.flush();
            result = process.waitFor();
            // get command result
            if (isNeedResultMsg) {
                successMsg = new StringBuilder();
                errorMsg = new StringBuilder();
                successResult = new BufferedReader(new InputStreamReader(process.getInputStream()));
                errorResult = new BufferedReader(new InputStreamReader(process.getErrorStream()));
                String s;
                while ((s = successResult.readLine()) != null) {
                    successMsg.append(s);
                }
                while ((s = errorResult.readLine()) != null) {
                    errorMsg.append(s);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (os != null) {
                    os.close();
                }
                if (successResult != null) {
                    successResult.close();
                }
                if (errorResult != null) {
                    errorResult.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            if (process != null) {
                process.destroy();
            }
        }
        return new CommandResult(result, successMsg == null ? null : successMsg.toString(), errorMsg == null ? null : errorMsg.toString());
    }

    public static class CommandResult {
        /**
         * Operation result
         **/
        public int result;
        /**
         * Successful results
         **/
        public String successMsg;
        /**
         * Running Failure Result
         **/
        public String errorMsg;

        public CommandResult(int result) {
            this.result = result;
        }

        public CommandResult(int result, String successMsg, String errorMsg) {
            this.result = result;
            this.successMsg = successMsg;
            this.errorMsg = errorMsg;
        }
    }
}

Here's GIthub's address. If you think it's helpful, star t it. You're more welcome to add more practical features to fork.

https://github.com/huangdali/RootUtils

Please respect the original, reprint the need to indicate the origin, Big Brother's blog: https://blog.csdn.net/qq137722697

Posted by ednark on Wed, 12 Dec 2018 03:27:06 -0800