Android 關機(reboot)流程

來源:互聯網
上載者:User

標籤:kernel   android   reboot   

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.javareboot(){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,recoveryint do_powerctl(int nargs, char **args)     
=>return android_reboot(cmd, 0, reboot_target);cmd = ANDROID_RB_RESTART2 //在android_reboot.h中#define ANDROID_RB_RESTART2 0xDEAD0003reboot_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)

=》最後會進入Kernelkernel/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.cvoid 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 關機(reboot)流程

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.