recovery 和 reboot流程
part 1.
reboot recovery流程。
1,RecoverySystem.java中
    private static File RECOVERY_DIR = new File("/cache/recovery");
    private static File COMMAND_FILE = new File(RECOVERY_DIR, "command");
     pm.reboot("recovery");
=》PowerManager.java
     public void reboot(String reason) {
        try {
            mService.reboot(false, reason, true);
        } catch (RemoteException e) {
        }
    } 
mService就是PowerManagerService.java的实例
因为
public final class PowerManagerService extends IPowerManager.Stub
=>PowerManagerService.java
reboot(){
shutdownOrRebootInternal(false, confirm, reason, wait);
}
private void shutdownOrRebootInternal(final boolean shutdown, final boolean confirm,
            final String reason, boolean wait) {
 ShutdownThread.reboot(mContext, reason, confirm);
}
=>
ShutdownThread.java
public static void reboot(final Context context, String reason, boolean confirm) {
        mReboot = true;
        mRebootSafeMode = false;
        mRebootReason = reason;
        shutdownInner(context, confirm);
    }
shutdownInner
-》
beginShutdownSequence
拿两个wake_lock让系统不待机,让屏幕常亮
然后
sInstance.start();这个ShutdwonThread的实例start.即调用其的run方法
在run方法中,关掉am,radio....之后,调用 
rebootOrShutdown
-》
public static void rebootOrShutdown(boolean reboot, String reason)
-》
PowerManagerService.lowLevelReboot(reason);
又调回PowerManagerService了。
->PowerManagerService中
lowLevelReboot()
SystemProperties.set("sys.powerctl", "reboot," + reason); //就传给Kernel进入reboot了
=》
那么接下来,走到哪里去了?
参考 :
“
KK 以后 Google 将关机/重启的最后转移到了init 进程上执行。 其他Process 通过system property sys.powerctl 通知init 执行。
sys.powerctl 的配置在init.rc 当中,可以参考
on property:sys.powerctl=*
   powerctl ${sys.powerctl}
   
然后执行system/core/init/builtins.c#do_powerctl 函数,call android_reboot 重启或者关机。
为何工厂模式下无法adb reboot ? 正常情况下adb reboot 可以重启, 其关键就是因为mediatek/config/{platform}factory_init.rc 中没有定义
on property:sys.powerctl=*
   powerctl ${sys.powerctl}
   
如果要添加此功能,只有在factory_init.rc 中添加这两行即可
”
继续跟
=》在init.rc中和一个相关的on 触发器
396 on property:sys.powerctl=*
397     powerctl ${sys.powerctl} 
=》会调用 builtins.c 的,参考间reboot,recovery
int do_powerctl(int nargs, char **args)    
=>
return android_reboot(cmd, 0, reboot_target);
cmd = ANDROID_RB_RESTART2 //在android_reboot.h中#define ANDROID_RB_RESTART2 0xDEAD0003
reboot_target =第二个参考recovery
=》
/system/core/libcutils/android_reboot.c 中的执行android_reboot();
case ANDROID_RB_RESTART2:                                                                                                                         
121             ret = __reboot(LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2,
122                            LINUX_REBOOT_CMD_RESTART2, arg); //arg = recovery
=>
这个在bionic/libc/include/sys/reboot.h中定义的。说明这是一个标准的系统调用
extern int __reboot(int, int, int, void *);
具体位于bionic/libc/arch-arm/syscalls/__reboot.S
  1 /* autogenerated by gensyscalls.py */
  2 #include <asm/unistd.h>
  3 #include <linux/err.h>
  4 #include <machine/asm.h>
  5                                                                                                                                                           
  6 ENTRY(__reboot)
  7     mov     ip, r7
  8     ldr     r7, =__NR_reboot
  9     swi     #0
10     mov     r7, ip
11     cmn     r0, #(MAX_ERRNO + 1)
12     bxls    lr
13     neg     r0, r0
14     b       __set_errno
15 END(__reboot)

