新聞中心

EEPW首頁 > 嵌入式系統(tǒng) > 設(shè)計應(yīng)用 > 分析內(nèi)核初始化時根內(nèi)存盤的加載過程(init/main.c)

分析內(nèi)核初始化時根內(nèi)存盤的加載過程(init/main.c)

作者: 時間:2006-12-12 來源:網(wǎng)絡(luò) 收藏
作者:opera
  
  概述
  ====
  1)當內(nèi)核配置了內(nèi)存盤時, 內(nèi)核在初始化時可以將軟盤加載到內(nèi)存盤中作為根盤. 當同時配置了初始化內(nèi)存盤(Initail RAM Disk)時, 內(nèi)核在初始化時可以在安裝主盤之前, 通過引導(dǎo)程序所加載的initrd文件建立一個內(nèi)存初始化盤, 首先將它安裝成根文件系統(tǒng), 然后執(zhí)行其根目錄下的linuxrc 文件, 可用于在安裝主盤之前加載一些內(nèi)核模塊. 等到linuxrc 程序退出后, 再將主盤安裝成根文件系統(tǒng), 并將內(nèi)存初始化盤轉(zhuǎn)移安裝到其/initrd目錄下.
  2)當主盤就是initrd所生成的內(nèi)存初始化盤時, 不再進行重新安裝, 在DOS下用加載的搶救盤就是這種工作方式.
  3)引導(dǎo)程序所加載的initrd為文件系統(tǒng)的映象文件, 可以是壓縮的, 也可以是不壓縮的. 能夠識別的文件系統(tǒng)有minix,ext2,romfs三種.
  4)當內(nèi)核的根盤為軟盤時, 內(nèi)核初始化時會測試軟盤的指定部位是否存在文件系統(tǒng)或壓縮文件映象, 然后將之加載或解壓到內(nèi)存盤中作為根盤. 這是單張搶救軟盤的工作方式.

  有關(guān)代碼
  ========
  代碼:
  ; init/main.c
  #ifdef CONFIG_BLK_DEV_INITRD
  kdev_t real_root_dev; 啟動參數(shù)所設(shè)定的根盤設(shè)備
  #endif
  asmlinkage void __init start_kernel(void)
  {
   char * command_line;
   unsigned long mempages;
   extern char saved_command_line[];
   lock_kernel();
   printk(linux_banner);
   setup_arch(command_line); arch/i386/kernel/setup.c中,初始化initrd_start和initrd_end兩個變量
   ...
  #ifdef CONFIG_BLK_DEV_INITRD
   if (initrd_start !initrd_below_start_ok
   initrd_start min_low_pfn PAGE_SHIFT) {
   ; min_low_pfn為內(nèi)核末端_end所開始的物理頁號,initrd_start,initrd_end在rd.c中定義
   printk(KERN_CRIT initrd overwritten (0x%08lx 0x%08lx) -
   disabling it.n,initrd_start,min_low_pfn PAGE_SHIFT);
   initrd_start = 0;
   }
  #endif
   ...
   kernel_thread(init, NULL, CLONE_FS | CLONE_FILES | CLONE_SIGNAL); 創(chuàng)建init進程
   unlock_kernel();
   current->need_resched = 1;
   cpu_idle();
  }
  static int init(void * unused)
  {
   lock_kernel();
   do_basic_setup();
   /*
   * Ok, we have completed the initial bootup, and
   * we're essentially up and running. Get rid of the
   * initmem segments and start the user-mode stuff..
   */
   free_initmem();
   unlock_kernel();
   if (open(/dev/console, O_RDWR, 0) 0)
   printk(Warning: unable to open an initial console.n);
   (void) dup(0);
   (void) dup(0);
   /*
   * We try each of these until one succeeds.
   *
   * The Bourne shell can be used instead of init if we are
   * trying to recover a really broken machine.
   */
   if (execute_command)
   execve(execute_command,argv_init,envp_init);
   execve(/sbin/init,argv_init,envp_init);
   execve(/etc/init,argv_init,envp_init);
   execve(/bin/init,argv_init,envp_init);
   execve(/bin/sh,argv_init,envp_init);
   panic(No init found. Try passing init= option to kernel.);
  }
  static void __init do_basic_setup(void)
  {
  #ifdef CONFIG_BLK_DEV_INITRD
   int real_root_mountflags;
  #endif
   ...
  #ifdef CONFIG_BLK_DEV_INITRD
   real_root_dev = ROOT_DEV; ROOT_DEV為所請求根文件系統(tǒng)的塊設(shè)備
   real_root_mountflags = root_mountflags;
   if (initrd_start mount_initrd) root_mountflags = ~MS_RDONLY;
   else mount_initrd =0;
  #endif
   start_context_thread();
   do_initcalls(); 會調(diào)用partition_setup()中加載內(nèi)存盤
   /* .. filesystems .. */
   filesystem_setup();
   /* Mount the root filesystem.. */
   mount_root();
   mount_devfs_fs ();
  #ifdef CONFIG_BLK_DEV_INITRD
   root_mountflags = real_root_mountflags;
   if (mount_initrd ROOT_DEV != real_root_dev
   MAJOR(ROOT_DEV) == RAMDISK_MAJOR MINOR(ROOT_DEV) == 0) {
   ; 如果當前根盤為initrd所建立的內(nèi)存盤
   int error;
   int i, pid;
   pid = kernel_thread(do_linuxrc, /linuxrc, SIGCHLD); 創(chuàng)建新的任務(wù)去執(zhí)行程序/linuxrc
   if (pid>0)
   while (pid != wait(i)); 等待linuxrc進程退出
   if (MAJOR(real_root_dev) != RAMDISK_MAJOR
   || MINOR(real_root_dev) != 0) {
   ; 如果原來的根盤不是0號內(nèi)存盤,則使用原來的根文件系統(tǒng),
   ; 并且將內(nèi)存盤轉(zhuǎn)移到其/initrd目錄下
   error = change_root(real_root_dev,/initrd);
   if (error)
   printk(KERN_ERR Change root to /initrd:
   error %dn,error);
   }
   }
  #endif
  }
  #ifdef CONFIG_BLK_DEV_INITRD
  static int do_linuxrc(void * shell)
  {
   static char *argv[] = { linuxrc, NULL, };
  
   close(0);close(1);close(2);
   setsid(); 設(shè)置新的session號
   (void) open(/dev/console,O_RDWR,0);
   (void) dup(0);
   (void) dup(0);
   return execve(shell, argv, envp_init);
  }
  #endif
  ; arch/i386/kernel/setup.c
  #define RAMDISK_IMAGE_START_MASK 0x07FF
  #define RAMDISK_PROMPT_FLAG 0x8000
  #define RAMDISK_LOAD_FLAG 0x4000
  #define PARAM ((unsigned char *)empty_zero_page)
  #define RAMDISK_FLAGS (*(unsigned short *) (PARAM+0x1F8)) 可用rdev設(shè)置的參數(shù)
  #define LOADER_TYPE (*(unsigned char *) (PARAM+0x210))
  #define INITRD_START (*(unsigned long *) (PARAM+0x218)) 初始化盤映象起始物理地址
  #define INITRD_SIZE (*(unsigned long *) (PARAM+0x21c)) 初始化盤字節(jié)數(shù)
  
  void __init setup_arch(char **cmdline_p)
  {
   ...
  #ifdef CONFIG_BLK_DEV_RAM
   rd_image_start = RAMDISK_FLAGS RAMDISK_IMAGE_START_MASK; 以塊為單位
   rd_prompt = ((RAMDISK_FLAGS RAMDISK_PROMPT_FLAG) != 0);
   rd_doload = ((RAMDISK_FLAGS RAMDISK_LOAD_FLAG) != 0);
  #endif
   ...
  #ifdef CONFIG_BLK_DEV_INITRD
   if (LOADER_TYPE INITRD_START) {
   if (INITRD_START + INITRD_SIZE = (max_low_pfn PAGE_SHIFT)) {
   ; max_low_pfn表示內(nèi)核空間1G范圍以下最大允許的物理頁號
   reserve_bootmem(INITRD_START, INITRD_SIZE);
   initrd_start =
   INITRD_START ? INITRD_START + PAGE_OFFSET : 0; 轉(zhuǎn)變?yōu)閮?nèi)核邏輯地址
   initrd_end = initrd_start+INITRD_SIZE;
   }
   else {
   printk(initrd extends beyond end of memory
   (0x%08lx > 0x%08lx)ndisabling initrdn,
   INITRD_START + INITRD_SIZE,
   max_low_pfn PAGE_SHIFT);
   initrd_start = 0;
   }
   }
  #endif
   ...
  }
  ; fs/partitions/check.c:
  int __init partition_setup(void)
  {
   device_init(); 包含ramdisk設(shè)備的初始化
  #ifdef CONFIG_BLK_DEV_RAM
  #ifdef CONFIG_BLK_DEV_INITRD
   if (initrd_start mount_initrd) initrd_load();
   ;如果啟動時加載了initrd文件,則用它去初始化根內(nèi)存盤
   else
  #endif
   rd_load(); 如果內(nèi)核配置了內(nèi)存盤并且根盤指定為軟盤則試圖將軟盤加載為根內(nèi)存盤
  #endif
   return 0;
  }
  __initcall(partition_setup);
  ; drivers/block/rd.c:
  int rd_doload; /* 1 = load RAM disk, 0 = don't load */
  int rd_prompt = 1; /* 1 = prompt for RAM disk, 0 = don't prompt */
  int rd_image_start; /* starting block # of image */
  #ifdef CONFIG_BLK_DEV_INITRD
  unsigned long initrd_start, initrd_end;
  int


關(guān)鍵詞: loadlin gzip

評論


技術(shù)專區(qū)

關(guān)閉