歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android 關機(reboot)流程

Android 關機(reboot)流程

日期:2017/3/1 9:26:04   编辑:Linux編程

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是什麼時候讀。這就可以實現一些關機之後的狀態保存。

更多Android相關信息見Android 專題頁面 http://www.linuxidc.com/topicnews.aspx?tid=11

Copyright © Linux教程網 All Rights Reserved