资源描述:
《andriod关机&重启分析》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、Andriod关机&重启分析1、jni层frameworks/base/core/jni/android_os_Power.cppstaticvoidandroid_os_Power_shutdown(JNIEnv*env,jobjectclazz){sync();#ifdefHAVE_ANDROID_OSreboot(RB_POWER_OFF);#endif}staticvoidandroid_os_Power_reboot(JNIEnv*env,jobjectclazz,jstringreason){sync(
2、);#ifdefHAVE_ANDROID_OSif(reason==NULL){reboot(RB_AUTOBOOT);}else{constchar*chars=env->GetStringUTFChars(reason,NULL);__reboot(LINUX_REBOOT_MAGIC1,LINUX_REBOOT_MAGIC2,LINUX_REBOOT_CMD_RESTART2,(char*)chars);env->ReleaseStringUTFChars(reason,chars);//Incaseitfa
3、ils.}jniThrowIOException(env,errno);#endif}staticJNINativeMethodmethod_table[]={…...{"shutdown","()V",(void*)android_os_Power_shutdown},{"rebootNative","(Ljava/lang/String;)V",(void*)android_os_Power_reboot},};其中,RB_POWER_OFF及RB_AUTOBOOT定义bionic/libc/include/s
4、ys/reboot.h中。#defineRB_AUTOBOOTLINUX_REBOOT_CMD_RESTART#defineRB_HALT_SYSTEMLINUX_REBOOT_CMD_HALT#defineRB_POWER_OFFLINUX_REBOOT_CMD_POWER_OFF…...2、通过jni层,最后会执行系统调用sys_reboot,即reboot。以imx51平台为例,reboot定义在kernel_imx/kernel/sys.c…...if((cmd==LINUX_REBOOT_CMD_POWE
5、R_OFF)&&!pm_power_off)cmd=LINUX_REBOOT_CMD_HALT;lock_kernel();switch(cmd){caseLINUX_REBOOT_CMD_RESTART:kernel_restart(NULL);break;…...caseLINUX_REBOOT_CMD_HALT:kernel_halt();unlock_kernel();do_exit(0);break;caseLINUX_REBOOT_CMD_POWER_OFF:kernel_power_off();unl
6、ock_kernel();do_exit(0);break;…...其中,函数指针pm_power_off是与平台相关的指针,以imx51平台为例,其定义于kernel_imx/arch/arm/mach-mx5/mx51_baggage.c中。staticvoidmxc_power_off(void){….../*Setthepowergatebitstopowerdown*/pmic_write_reg(REG_POWER_MISC,(PWGT1SPIEN
7、PWGT2SPIEN),(PWGT1SPIEN
8、PWG
9、T2SPIEN));}另外,在staticvoid__initmxc_board_init(void)函数中,有pm_power_off=mxc_power_off;另外,在pmic中,也定义了pm_power_off=pmic_power_off;这是因为imx51支持2中关机方式,具体要看硬件支持。3、sys_reboot分析当pm_power_off为空时,由上层传入的命令LINUX_REBOOT_CMD_POWER_OFF会变为LINUX_REBOOT_CMD_HALT,从而执行kernel_halt()函
10、数。在该函数中,会执行函数machine_halt(),其定义在kernel_imx/arch/arm/kernel/process.c中。voidmachine_halt(void){}由于该函数为空,因此,当pm_power_off指针为空时,执行关机操作会失败。当pm_power_off不为空时,上层命令LINUX_REBOOT_CMD_POWER_O