您的位置:首页 > 移动开发 > Android开发

Android 关机(reboot)流程 -- "sys.powerctl"

2016-07-06 16:48 411 查看
Base On
Android 4.2

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)

=》最后会进入Kernel

kernel/sys.c

    case LINUX_REBOOT_CMD_RESTART2:

          if (strncpy_from_user(&buffer[0], arg, sizeof(buffer) - 1) < 0) {

              ret = -EFAULT;

              break;

          }

          buffer[sizeof(buffer) - 1] = '\0';

          kernel_restart(buffer);  //buffer = arg= recovery

          break;

=>machine_restart()

=>arch/arm/kernel/process.c

void machine_restart(char *cmd)

{

    local_irq_disable();

    smp_send_stop();

    /* Flush the console to make sure all the relevant messages make it

    * out to the console drivers */

    arm_machine_flush_console();

    arm_pm_restart(reboot_mode, cmd);

    /* Give a grace period for failure to restart of 1s */

    mdelay(1000); // 1s之内没有restart完成就reboot失败。

    /* Whoops - the platform was unable to reboot. Tell the user! */

    printk("Reboot failed -- System halted\n");

    local_irq_disable();

    while (1);

}

=》

void (*arm_pm_restart)(char str, const char *cmd) = arm_machine_restart;

=》

void arm_machine_restart(char mode, const char *cmd)

{

...

aml_write_reg32(P_AO_RTI_STATUS_REG1, reboot_reason); //这一个标志寄存器

...

arch_reset(mode, cmd); // mode = 'h' cmd = "recovery"

}

差不多了,尝试就如此吧。

那么有必要了解这个寄存器P_AO_RTI_STATUS_REG1在下次启动的时候,uboot是什么时候读。这就可以实现一些关机之后的状态保存。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: