Android startup analysis (I) Logo display

Source: Internet
Author: User

When Android is started, a logo is displayed, and its source code is located in logo. C under the/system/CORE/init/directory:

The code I commented out is as follows:

# Include <stdio. h> <br/> # include <stdlib. h> <br/> # include <unistd. h> <br/> # include <fcntl. h> <br/> # include <sys/Mman. h> <br/> # include <sys/STAT. h> <br/> # include <sys/types. h> </P> <p> # include <Linux/FB. h> <br/> # include <Linux/KD. h> </P> <p> # include "init. H "</P> <p> # ifdef Android <br/> # include <cutils/memory. h> <br/> # else <br/> void android_memset16 (void * _ PTR, unsigned short Val, unsigned count) <br/> {<B R/> unsigned short * PTR = _ PTR; <br/> count> = 1; <br/> while (count --) <br/> * PTR ++ = val; <br/>}< br/> # endif </P> <p> struct FB {<br/> unsigned short * bits; <br/> unsigned size; <br/> int FD; <br/> struct fb_fix_screeninfo fi; <br/> struct fb_var_screeninfo VI; <br/> }; </P> <p> # define fb_width (FB)-> VI. xres) <br/> # define fb_height (FB)-> VI. yres) <br/> # define fb_size (FB)-> VI. xres *( FB)-> VI. yres * 2) </P> <p> static int fb_open (struct FB * FB) // open the framebuffer device <br/>{< br/> // the file of the FD device is/dev/graphics/fb0 <br/> FB-> FD = open ("/ dev/graphics/fb0 ", o_rdwr); <br/> If (FB-> FD <0) <br/> return-1; </P> <p> If (IOCTL (FB-> FD, fbioget_fscreeninfo, & FB-> FI) <0) <br/> goto fail; <br/> If (IOCTL (FB-> FD, fbioget_vscreeninfo, & FB-> VI) <0) <br/> goto fail; <br/> // MMAP the framebuffer size of FD <br/> FB -> Bits = MMAP (0, fb_size (FB), prot_read | prot_write, <br/> map_shared, FB-> FD, 0 ); <br/> If (FB-> bits = map_failed) <br/> goto fail; </P> <p> return 0; </P> <p> fail: <br/> close (FB-> FD); <br/> return-1; <br/>}</P> <p> static void fb_close (struct FB * FB) // disable framebuffer <br/>{< br/> munmap (FB-> bits, fb_size (FB); <br/> close (FB-> FD ); <br/>}</P> <p>/* There's got to be a more portable way to do this... */<Br/> static void fb_update (struct FB * FB) <br/>{< br/> FB-> VI. yoffset = 1; <br/> IOCTL (FB-> FD, fbioput_vscreeninfo, & FB-> VI); <br/> FB-> VI. yoffset = 0; <br/> IOCTL (FB-> FD, fbioput_vscreeninfo, & FB-> VI); <br/>}< br/> // set the terminal display mode: graphics or text? <Br/> static int vt_set_mode (INT graphics) <br/>{< br/> int FD, R; <br/> FD = open ("/dev/tty0 ", o_rdwr | o_sync); <br/> If (FD <0) <br/> return-1; <br/> // set the image or text <br/> r = IOCTL (FD, kdsetmode, (void *) (graphics? Kd_graphics: kd_text); <br/> close (FD); <br/> return r; <br/>}</P> <p>/* 565rle image format: [count (2 bytes), RLE (2 bytes)] */</P> <p> int load_565rle_image (char * fN) <br/> {<br/> struct FB; <br/> struct stat s; <br/> unsigned short * data, * bits, * PTR; <br/> unsigned count, Max; <br/> int FD; </P> <p> If (vt_set_mode (1 )) // enable graphics display mode <br/> return-1; </P> <p> // determines whether init_image_file can be opened. <br/> FD = Open (FN, o_rdonly); <br/> If (FD <0) {<br/> error ("cannot open '% s'/N", FN ); <br/> // restore the text display mode if not <br/> goto fail_restore_text; <br/>}</P> <p> If (fstat (FD, & S) <0) {<br/> goto fail_close_file; <br/>}< br/>/* Call MMAP in the user space, it is equivalent to opening up the file storage Io ing in the Process <br/> data is the returned ing address, map the image file content to the storage area <br/> */<br/> DATA = MMAP (0, S. st_size, prot_read, map_shared, FD, 0); <br/> If (Data = map_failed) <br/> goto fail_cl Ose_file; </P> <p> If (fb_open (& FB) <br/> goto fail_unmap_data; </P> <p> max = fb_width (& FB) * fb_height (& FB); <br/> PTR = data; <br/> COUNT = S. st_size; <br/> bits = FB. BITs; <br/> while (count> 3) {<br/> unsigned n = PTR [0]; <br/> If (n> MAX) <br/> break; <br/> // write the image corresponding to PTR to the framebuffer corresponding to bits. The details are not investigated. <br/> android_memset16 (bits, PTR [1], n <1); <br/> bits + = N; <br/> MAX-= N; <br/> PTR + = 2; <br /> Count-= 4; <br/>}</P> <p> munmap (data, S. st_size); <br/> fb_update (& FB); <br/> fb_close (& FB); <br/> close (FD ); <br/> unlink (FN); // Delete fn? <Br/> return 0; </P> <p> fail_unmap_data: <br/> // memory ing Io applied for by munmap release <br/> munmap (data, S. st_size); <br/> fail_close_file: <br/> close (FD); <br/> fail_restore_text: <br/> vt_set_mode (0 ); <br/> // If-1 is returned, the "android" text is displayed! <Br/> return-1; <br/>}

The analysis is as follows:

The first process of Android boot is init. In its implementation file, that is, init. C, the logo is displayed during boot by calling the load_565rle_image (init_image_file) function. The above function is implemented in logo. C under the same directory.

In the main function load_565rle_image (), call vt_set_mode () to set the display mode of the terminal. The graphics display mode of the terminal is used by default, you can call the ioctl system call to set the parameter by calling the/dev/tty0 device file. However, if you cannot open the init_image_file specified in load_565rle_image (init_image_file, directly set/dev/tty0 to text display mode, then return directly, and execute init. code in C (write the "android" logo directly to the/dev/tty0 device, which is the default startup logo, which is not creative, haha !)

However, if the image file init_image_file exists, the image file will be mapped to the bucket of the current process by calling MMAP in load_565rle_image, and then fb_open () will be called () function to open the device file/dev/graphics/fb0 corresponding to the Linux framebuufer, and map the framebuffer to the bucket through MMAP. Then, android_memset16 () is called to write images, from the parameter of the function such as android_memset16 (), we can see that the purpose of the program is to set the bytes corresponding to the image storage space address corresponding to PTR, write to the bucket corresponding to framebuffer corresponding to bits. Write framebuffer details will not be pursued, may involve some knowledge about the framebuffer principle.

 

In the init. h file:

# Define init_image_file "/initlogo. RLE"

Specifies the location of the image file and the default file name, so that you can customize the boot screen. Haha !!!

 

Conclusion: The Boot logo is displayed. If an image is used, MMAP is used to write the image content to the framebuffer storage ing space. If only text is displayed, it's just dealing with/dev/tty0. Analysis complete !!

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.