Tian haili @ csdn
After obtaining the OTA update package through the network or local device, you can start the android upgrade through the program. This document describes this process.
After obtaining the OTA update package, you can enable the OTA update directly through Android. OS. recoverysystem. installpackage.
Recoverysystem. installpackage () is added after the API-8 and permission is required to use it: Android. manifest. Permission. Reboot
I. Implementation of recoverysystem # installpackage ()
Recoverysystem. installpackage () is to use Arg "-- update_package = <FILENAME>" [<FILENAME> instead of a specific file] To Call bootcommand.
Implementation of bootcommand:
FileWriter command= new FileWriter(COMMAND_FILE); try { command.write(arg); command.write("\n"); } finally { command.close(); } // Having writtenthe command file, go ahead and reboot PowerManager pm =(PowerManager) context.getSystemService(Context.POWER_SERVICE); pm.reboot("recovery");
Command_file is "/Cache/recovery/command ". Bootcommand () writes "-- update_package = <FILENAME>" to/Cache/recovery/command, and then calls the reboot () of powermanagerservice through binderipc (), and pass "recovery" as the parameter.
II. Implementation of powermanagerservice # reboot
Powermanagerservice. Reboot (reason: string) is implemented by executing shutdownthread. Reboot (mcontext, finalreason, false.
Execution sequence diagram, where values:
-Reason: "recovery ";
-Confirm: false;
-Rebootorshutdown parameters: reboot <-mreboot; reason <-mrebootreason;
The final call of power. Reboot () is implemented by calling its C through JNI.
The JNI implementation in power. Java is in android_ OS _power.cpp.
Iii. Native Implementation of power. Reboot
Power. Reboot () is implemented through android_ OS _power_reboot [in frameworks/base/CORE/JNI/android_ OS _power.cpp]
static void android_os_Power_reboot(JNIEnv *env, jobject clazz,jstring reason){ sync();#ifdef HAVE_ANDROID_OS if (reason == NULL) { reboot(RB_AUTOBOOT); } else { const char *chars =env->GetStringUTFChars(reason, NULL); __reboot(LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2, LINUX_REBOOT_CMD_RESTART2, (char*) chars); env->ReleaseStringUTFChars(reason, chars); // In case it fails. } jniThrowIOException(env,errno);#endif}
The system calls _ reboot to embed the kernel to implement reboot.
In the kernel state, enter the sys_reboot system definition.
Iv. definition and implementation of sys_reboot
The definition and implementation of sys_reboot depends on the code in the kernel.
// kernel/kernel/sys.cSYSCALL_DEFINE4(reboot, int, magic1, int,magic2, unsigned int, cmd, void__user *, arg) //kernel/include/linux/syscalls.h#define __SYSCALL_DEFINEx(x, name, ...) \ asmlinkagelong sys##name(__SC_DECL##x(__VA_ARGS__)) #define SYSCALL_DEFINEx(x, sname, ...) \ __SYSCALL_DEFINEx(x,sname, __VA_ARGS__) #define SYSCALL_DEFINE4(name, ...)SYSCALL_DEFINEx(4, _##name, __VA_ARGS__)
Therefore, in SYS. CSyscall_define4 (reboot ,...)Sys_reboot () is implemented ()
Syscall_define4 (reboot ,...)ModerateMagic1AndMagic2For identification, only magic1 and magic2 will be processed. Otherwise, return directly.
For cmdLinux_reboot_cmd_restart2: Copy the reason of reboot from the user State to the kernel state and call kernel_restart () for execution.
Kernel_restart (char * cmd) first calls kernel_restart_prepare (CMD); then calls machine_restart (CMD );
// kernel/kernel/sys.cvoid kernel_restart_prepare(char *cmd){ blocking_notifier_call_chain(&reboot_notifier_list,SYS_RESTART, cmd); system_state= SYSTEM_RESTART; device_shutdown(); sysdev_shutdown();}
//kernel/arch/arm/kernel/process.cvoid machine_restart(char *cmd){ arm_pm_restart(reboot_mode,cmd);}
Arm_pm_restartThe function pointer pointing to the restart of the machine defined on the specific platform. The prototype is void (* arm_pm_restart) (char STR, const char * cmd ).
The specific implementation is related to the specific platform used.
Summary
This article describes the process of initiating OTA update in Android, from framework to JNI, to the kernel layer, and analyzes the layer-by-layer relationship, it is to write "-- update_package = <FILENAME>" to/Cache/recovery/command, and then transfer it to the kernel state to execute the system call through the system call to restart the machine, complete the OTA update process.
After the restart, the process of entering the recovery mode will be discussed later in the topic.