Android recovery code analysis

Source: Internet
Author: User

1. Call the main function

In gingerbread/bootable/recovery. C has a main () at the bottom. This is the main port of the recovery application. When recovery is compiled, an executable file named recovery is generated, I put it in the/sbin directory of the recovery/File System in the out/directory. parameters will be input when calling the recovery executable file. These parameters are the parameters of the main function, as shown below:

01 int
02 main(int
argc, char
03 {
04     char
05     time_t
start = time(NULL);
08     log_init();
09 #endif
10     INFO(">>>>> Enter recovery <<<<<\n");

Argc indicates the number of parameters, and argv indicates each parameter pointer.

Starting recovery...

2. Get commond

1 static
2 get_args(int
*argc, char
***argv) {
3 //  INFO("Enter get_args\n");
4     struct
bootloader_message boot;
5     memset(&boot, 0,

If the command line has a recovery command, the recovery command of the command line is preferentially executed; otherwise, the command in the MISC partition and the command in the/Cache/recovery/command file are searched down.

3. Obtain the default firmware update path and name.

Int property_get (const char * Key, char * value, const char * default_value );

Including USB flash drive, SD card, and Flash upgrade


4. parsing commands

Int getopt_long (INT, char * const *, const char *, const struct option *, int *);

For example, case's: send_intent = optarg; break;

Optarg is a string after the moderate number of the command.

Register some commands. The register_update_commands function is an upgrade command registered in update-script and recovery-script.

Initialize a variable, int status = install_success; this variable is used to identify whether the upgrade is successful. It serves as a basis for judging when the MISC partition command is cleared.


5. Upgrade, format, and restore

Next, there are three requirements: factorytest; Update and recover; and wipe data.

01 if
(update_image != NULL) {
02     status = install_update(update_image);
03     if
(status != 0) {
04         ui_set_background(BACKGROUND_ICON_ERROR);
05         if(status==-1) g_enable_item_move =
06     }
07 }else
if(recover_image != NULL){
08     status = recover_backup(recover_image);
09     if
(status != 0) {
10         ui_set_background(BACKGROUND_ICON_ERROR);
11         if(status==-1) g_enable_item_move =
12     }
13 }

This part of the code is required for update \ recover. Update follows the install_update branch, and recovery uses recover_backup branch. g_enable_item_move is a variable that indicates whether the items on the recovery upgrade interface can be moved. False indicates that the items cannot be moved.

01 if
(wipe_flags) {
02        if( wipe_data(wipe_flags) != 0 )
03        {
04            status = INSTALL_ERROR;
05            ui_print("Data wipe failed.\n");
06            // Do not erase the commands in MISC and format them again after restart
07            g_reset_blmsg =
08            g_enable_item_move =
09        }
10    }

This part is used to erase the data, that is, the formatting requirement. Based on the erased flags records, the partitions or disks to be formatted are erased separately. If the erasure fails, the g_reset_blmsg variable serves as the discriminant basis during the erasure of boot, false indicates that the file is not wiped out. In this way, the content of the MISC partition is recovey. After the next restart, boot leads to the prompt_and_wait () function in recovery mode. The key part is

1 for
(;;) {
2     int
key = 0;
3     INFO("wait an key\n");
4     key = ui_wait_key();
5     INFO("end wait\n");

This function is used to keep waiting for user input. It is a constant loop. You can select five entries, including restoring factory settings and restarting. maybe_install_firmware_update (send_intent) is an opportunity for the user, write your intent and do something that Google has not done. This is the last function to complete the upgrade,

1 // Reset the bootloader message to revert to a normal main system boot.
2     if(g_reset_blmsg)
3     {
4     INFO("ready to clear cmd in misc \n");
5         struct
bootloader_message boot;
6         memset(&boot, 0,
7         set_bootloader_message(&boot);
8     }

Previously, this was a processing of intent. Here g_reset_blmsg is a criterion for clearing boot according to the above mentioned. If you need to clear boot, it indicates that boot will start normally after the next restart and will not enter the recovery mode.

6. After reboot completes the boot and MISC partitions, restart

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: 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.