接下来我们就重点分析函数console_init_action的实现,以便可以了解第二个开机画面的显示过程:
 
  
  1. static int console_init_action(int nargs, char **args)   
  2. {   
  3.     int fd;   
  4.     char tmp[PROP_VALUE_MAX];   
  5.    
  6.     if (console[0]) {   
  7.         snprintf(tmp, sizeof(tmp), "/dev/%s", console);   
  8.         console_name = strdup(tmp);   
  9.     }   
  10.    
  11.     fd = open(console_name, O_RDWR);   
  12.     if (fd >= 0)   
  13.         have_console = 1;   
  14.     close(fd);   
  15.    
  16.     if( load_565rle_p_w_picpath(INIT_IMAGE_FILE) ) {   
  17.         fd = open("/dev/tty0", O_WRONLY);   
  18.         if (fd >= 0) {   
  19.             const char *msg;   
  20.                 msg = "\n"   
  21.             "\n"   
  22.             "\n"   
  23.             "\n"   
  24.             "\n"   
  25.             "\n"   
  26.             "\n"  // console is 40 cols x 30 lines   
  27.             "\n"   
  28.             "\n"   
  29.             "\n"   
  30.             "\n"   
  31.             "\n"   
  32.             "\n"   
  33.             "\n"   
  34.             "             A N D R O I D ";   
  35.             write(fd, msg, strlen(msg));   
  36.             close(fd);   
  37.         }   
  38.     }   
  39.     return 0;   
  40. }   
        这个函数主要做了两件事件:
 
         A. 初始化控制台。init进程在启动的时候,会解析内核的启动参数(保存在文件/proc/cmdline中)。如果发现内核的启动参数中包含有了一个名称为“androidboot.console”的属性,那么就会将这个属性的值保存在字符数组console中。这样我们就可以通过设备文件/dev/<console>来访问系统的控制台。如果内核的启动参数没有包含名称为“androidboot.console”的属性,那么默认就通过设备文件/dev/console来访问系统的控制台。如果能够成功地打开设备文件/dev/<console>或者/dev/console,那么就说明系统支持访问控制台,因此,全局变量have_console的就会被设置为1。
         B. 显示第二个开机画面。显示第二个开机画面是通过调用函数load_565rle_p_w_picpath来实现的。在调用函数load_565rle_p_w_picpath的时候,指定的开机画面文件为INIT_IMAGE_FILE。INIT_IMAGE_FILE是一个宏,定义在system/core/init/init.h文件中,如下所示:
 
  
  1. #define INIT_IMAGE_FILE "/initlogo.rle"   
 即第二个开机画面的内容是由文件/initlogo.rle来指定的。如果文件/initlogo.rle不存在,或者在显示它的过程中出现异常,那么函数load_565rle_p_w_picpath的返回值就会等于-1,这时候函数console_init_action就以文本的方式来显示第二个开机画面,即向编号为0的控制台(/dev/tty0)输出“ANDROID”这7个字符。
 
        函数load_565rle_p_w_picpath实现在文件system/core/init/logo.c中,如下所示:
 
  
  1. /* 565RLE p_w_picpath format: [count(2 bytes), rle(2 bytes)] */   
  2.    
  3. int load_565rle_p_w_picpath(char *fn)   
  4. {   
  5.     struct FB fb;   
  6.     struct stat s;   
  7.     unsigned short *data, *bits, *ptr;   
  8.     unsigned countmax;   
  9.     int fd;   
  10.    
  11.     if (vt_set_mode(1))   
  12.         return -1;   
  13.    
  14.     fd = open(fn, O_RDONLY);   
  15.     if (fd < 0) {   
  16.         ERROR("cannot open '%s'\n", fn);   
  17.         goto fail_restore_text;   
  18.     }   
  19.    
  20.     if (fstat(fd, &s) < 0) {   
  21.         goto fail_close_file;   
  22.     }   
  23.    
  24.     data = mmap(0, s.st_size, PROT_READ, MAP_SHARED, fd, 0);   
  25.     if (data == MAP_FAILED)   
  26.         goto fail_close_file;   
  27.    
  28.     if (fb_open(&fb))   
  29.         goto fail_unmap_data;   
  30.    
  31.     max = fb_width(&fb) * fb_height(&fb);   
  32.     ptr = data;   
  33.     count = s.st_size;   
  34.     bits = fb.bits;   
  35.     while (count > 3) {   
  36.         unsigned n = ptr[0];   
  37.         if (n > max)   
  38.             break;   
  39.         android_memset16(bits, ptr[1], n << 1);   
  40.         bits += n;   
  41.         max -= n;   
  42.         ptr += 2;   
  43.         count -= 4;   
  44.     }   
  45.    
  46.     munmap(data, s.st_size);   
  47.     fb_update(&fb);   
  48.     fb_close(&fb);   
  49.     close(fd);   
  50.     unlink(fn);   
  51.     return 0;   
  52.    
  53. fail_unmap_data:   
  54.     munmap(data, s.st_size);   
  55. fail_close_file:   
  56.     close(fd);   
  57. fail_restore_text:   
  58.     vt_set_mode(0);   
  59.     return -1;   
  60. }   
        函数首先将控制台的显示方式设置为图形方式,这是通过调用函数vt_set_mode来实现的,如下所示:
 
  
  1. static int vt_set_mode(int graphics)   
  2. {   
  3.     int fd, r;   
  4.     fd = open("/dev/tty0", O_RDWR | O_SYNC);   
  5.     if (fd < 0)   
  6.         return -1;   
  7.     r = ioctl(fd, KDSETMODE, (void*) (graphics ? KD_GRAPHICS : KD_TEXT));   
  8.     close(fd);   
  9.     return r;   
  10. }   
        函数vt_set_mode首先打开控制台设备文件/dev/tty0,接着再通过IO控制命令KDSETMODE来将控制台的显示方式设置为文本方式或者图形方式,取决于参数graphics的值。从前面的调用过程可以知道,参数graphics的值等于1,因此,这里是将控制台的显示方式设备为图形方式。
 
        回到函数load_565rle_p_w_picpath中,从前面的调用过程可以知道,参数fn的值等于“/initlogo.rle”,即指向目标设备上的initlogo.rle文件。函数load_565rle_p_w_picpath首先调用函数open打开这个文件,并且将获得的文件描述符保存在变量fd中,接着再调用函数fstat来获得这个文件的大小。有了这些信息之后,函数load_565rle_p_w_picpath就可以调用函数mmap来把文件/initlogo.rle映射到init进程的地址空间来了,以便可以读取它的内容。
       将文件/initlogo.rle映射到init进程的地址空间之后,接下来再调用函数fb_open来打开设备文件/dev/graphics/fb0。前面在介绍第一个开机画面的显示过程中提到,设备文件/dev/graphics/fb0是用来访问系统的帧缓冲区硬件设备的,因此,打开了设备文件/dev/graphics/fb0之后,我们就可以将文件/initlogo.rle的内容输出到帧缓冲区硬件设备中去了。