恢復出廠設定 Factory Reset 呼叫過程

kv110發表於2014-09-17

過程分兩大步

1. Main system 往/cache/recovery/command寫命令wipe-data,重啟手機
2. bootloader引導到recovery分割槽,執行wipe-data, 重啟手機

1. Main system 往/cache/recovery/command寫命令wipe-data,重啟手機
packages/apps/Settings/src/com/android/settings/MasterClearConfirm.java
   getActivity().sendBroadcast(new Intent("android.intent.action.MASTER_CLEAR"));

packages/apps/Settings/src/com/android/settings/MasterClear.java
frameworks/base/services/java/com/android/server/MasterClearReceiver.java
   new Thread("Reboot")
   RecoverySystem.rebootWipeUserData(context)

frameworks/base/core/java/android/os/RecoverySystem.java
 bootCommand(context, "--wipe_data\n--locale=" + Locale.getDefault().toString());
 bootCommand
   write command wipe-data to /cache/recovery/command
   pm.reboot("recovery");
 
frameworks/base/core/java/android/os/PowerManager.java
frameworks/base/services/java/com/android/server/power/PowerManagerService.java
frameworks/base/services/java/com/android/server/power/ShutdownThread.java
   PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
   pm.reboot("recovery");
      mService.reboot(false, reason, true)
         shutdownOrRebootInternal(false, confirm, reason, wait);
            ShutdownThread.shutdown(mContext, confirm); (shutdown)
            ShutdownThread.reboot(mContext, reason, confirm);  (reboot)
              shutdownInner(context, confirm);
                 beginShutdownSequence(context)
                   setprop sys.shutdown.requested recovery
                   rebootOrShutdown(mReboot, mRebootReason);
                     PowerManagerService.lowLevelReboot(reason)
                        SystemProperties.set("sys.powerctl", "reboot," + reason);  Thread.sleep(20000);
                        
system/core/rootdir/init.rc                        
on property:sys.powerctl=*       
    powerctl ${sys.powerctl}
        KEYWORD(powerctl,    COMMAND, 1, do_powerctl)   system/core/init/keywords.h
          do_powerctl()   system/core/init/builtins.c
              do_reboot(cmd, 0, reboot_target);   cmd = ANDROID_RB_RESTART2;   system/core/init/reboot.c
                __reboot(LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2,LINUX_REBOOT_CMD_RESTART2, arg);
                  __reboot bionic/libc/arch-arm/syscalls/__reboot.S
               

kernel/kernel/sys.c
SYSCALL_DEFINE4(reboot, int, magic1, int, magic2, unsigned int, cmd,void __user *, arg)
   kernel_restart(char *cmd)
     machine_restart(char *cmd)  kernel/arch/arm/kernel/process.c
        machine_shutdown()
        arm_pm_restart(reboot_mode,cmd)   kernel/arch/arm/kernel/process.c
          pm_power_off = msm_pm_power_off;
          arm_pm_restart = msm_pm_restart;              /kernel/arch/arm/mach-msm/restart_7k.c
             setup_mm_for_reboot   kernel/arch/unicore32/mm/mmu.c
             msm_proc_comm   /kernel/arch/arm/mach-msm/proc_msm.c

2. bootloader引導到recovery分割槽,執行wipe-data, 重啟手機
bootable/recovery/etc/init.rc
  service recovery /sbin/recovery

 bootable/recovery/recovery.c
 讀取/cache/recovery/command,判定哪種命令,如果是wipe-data,
  erase_volume("/data");
  erase_volume("/cache");
  reboot();
 check master reset log:  /cache/recovery/log
 

相關文章