- 论坛徽章:
- 0
|
reboot 进入busybox, busybox-1.01/init/reboot.c
- extern int reboot_main(int argc, char **argv)
- {
- char *delay; /* delay in seconds before rebooting */
- if(bb_getopt_ulflags(argc, argv, "d:", &delay)) {
- sleep(atoi(delay));
- }
- #ifndef CONFIG_INIT
- #ifndef RB_AUTOBOOT
- #define RB_AUTOBOOT 0x01234567 /*与kernel 中定义相同*/
- #endif
- return(bb_shutdown_system(RB_AUTOBOOT));
- #else
- return kill_init(SIGTERM);
- #endif
- }
复制代码
busybox-1.01/init/init_shared.c,向init或linuxrc进程发送SIGTRRM,
- extern int kill_init(int sig)
- {
- #ifdef CONFIG_FEATURE_INITRD
- /* don't assume init's pid == 1 */
- long *pid = find_pid_by_name("init");
- if (!pid || *pid<=0) {
- pid = find_pid_by_name("linuxrc");
- if (!pid || *pid<=0)
- bb_error_msg_and_die("no process killed");
- }
- return(kill(*pid, sig));
- #else
- return(kill(1, sig));
- #endif
- }
复制代码
init_main()中得到信号:
-
- extern int init_main(int argc, char **argv)
- {
- ...
- signal(SIGTERM, reboot_signal);
- ...
- }
复制代码- static void reboot_signal(int sig)
- {
- shutdown_system();
- message(CONSOLE | LOG, "Please stand by while rebooting the system.");
- sync();
- /* allow time for last message to reach serial console */
- sleep(2);
- init_reboot(RB_AUTOBOOT);
- loop_forever();
- }
复制代码- static void init_reboot(unsigned long magic)
- {
- pid_t pid;
- /* We have to fork here, since the kernel calls do_exit(0) in
- * linux/kernel/sys.c, which can cause the machine to panic when
- * the init process is killed.... */
- if ((pid = fork()) == 0) {
- reboot(magic);
- _exit(0);
- }
- waitpid (pid, NULL, 0);
- }
复制代码
reboot的定义在库里面,用的是交叉编程库为uclinux,在其库中找到linux/common/reboot.c,
- #include "syscalls.h"
- #include <sys/reboot.h>
- #define __NR__reboot __NR_reboot
- static inline _syscall3(int, _reboot, int, magic, int, magic2, int, flag);
- int reboot(int flag)
- {
- return (_reboot((int) 0xfee1dead, 672274793, flag));
- }
复制代码
include/linux/reboot.h 中定义:
- #define LINUX_REBOOT_CMD_RESTART 0x01234567
复制代码
进入kernel/sys.c,
-
- asmlinkage long sys_reboot(int magic1, int magic2, unsigned int cmd, void __user * arg)
- {
- char buffer[256];
- /* We only trust the superuser with rebooting the system. */
- if (!capable(CAP_SYS_BOOT))
- return -EPERM;
- /* For safety, we require "magic" arguments. */
- if (magic1 != LINUX_REBOOT_MAGIC1 ||
- (magic2 != LINUX_REBOOT_MAGIC2 &&
- magic2 != LINUX_REBOOT_MAGIC2A &&
- magic2 != LINUX_REBOOT_MAGIC2B &&
- magic2 != LINUX_REBOOT_MAGIC2C))
- return -EINVAL;
- lock_kernel();
- switch (cmd) {
- case LINUX_REBOOT_CMD_RESTART:
- kernel_restart(NULL);
- break;
- case LINUX_REBOOT_CMD_CAD_ON:
- C_A_D = 1;
- break;
- case LINUX_REBOOT_CMD_CAD_OFF:
- C_A_D = 0;
- break;
- case LINUX_REBOOT_CMD_HALT:
- kernel_halt();
- unlock_kernel();
- do_exit(0);
- break;
- case LINUX_REBOOT_CMD_POWER_OFF:
- kernel_power_off();
- unlock_kernel();
- do_exit(0);
- break;
- case LINUX_REBOOT_CMD_RESTART2:
- if (strncpy_from_user(&buffer[0], arg, sizeof(buffer) - 1) < 0) {
- unlock_kernel();
- return -EFAULT;
- }
- buffer[sizeof(buffer) - 1] = '\0';
- kernel_restart(buffer);
- break;
- case LINUX_REBOOT_CMD_KEXEC:
- kernel_kexec();
- unlock_kernel();
- return -EINVAL;
- #ifdef CONFIG_SOFTWARE_SUSPEND
- case LINUX_REBOOT_CMD_SW_SUSPEND:
- {
- int ret = software_suspend();
- unlock_kernel();
- return ret;
- }
- #endif
- default:
- unlock_kernel();
- return -EINVAL;
- }
- unlock_kernel();
- return 0;
- }
复制代码
系统重启:
- void kernel_restart(char *cmd)
- {
- kernel_restart_prepare(cmd);
- if (!cmd) {
- printk(KERN_EMERG "Restarting system.\n");
- } else {
- printk(KERN_EMERG "Restarting system with command '%s'.\n", cmd);
- }
- printk(".\n");
- machine_restart(cmd);
- }
复制代码
进入arch/mips/kernel/reset.c, _machine_restart为一个函数指针:
- void (*_machine_restart)(char *command);
- void machine_restart(char *command)
- {
- _machine_restart(command);
- }
复制代码
用的cpu为ar7240,在arch/mips/ar7240下的指定
- void __init plat_setup(void)
- {
- .....
- _machine_restart = ar7240_restart;
- _machine_halt = ar7240_halt;
- _machine_power_off = ar7240_power_off;
- ....
- }
复制代码
对cpu reset reg进行写值,操作完成
- void
- ar7240_restart(char *command)
- {
- for(;;) {
- ar7240_reg_wr(AR7240_RESET, AR7240_RESET_FULL_CHIP);
- }
- }
复制代码
[ 本帖最后由 s.t_seeyou 于 2009-12-14 17:13 编辑 ] |
评分
-
查看全部评分
|