Android JNI Library Implementation Reboot,recovery

Source: Internet
Author: User

1. Recovery function:

#defineUPDATE_TITLE "--update_package=" #define Update_command_file "/cache/recovery/command" #define Update_flag_ FILE "/cache/recovery/last_flag" #define Last_install_file "/cache/recovery/last_install" #define LAST_LOG_FILE "/ Cache/recovery/last_log "#define Last_locale_file"/cache/recovery/last_locale "#define printf alogdint Factory_data_ Reset (void) {char data[] = {"--wipe_data\n--locale=en_us\n"};int len = 0, fd;printf ("[%s]command:%s\n", __function__, data); fd = open (Update_command_file, o_rdwr| O_creat| O_trunc, s_irusr| S_IWUSR); if (FD < 0) {printf ("[%s]creat command File failed\n", __function__); return-3;} Len = strlen (data), if (write (fd, data, len)! = Len) {printf ("[%s]write command File failed\n", __function__); close (FD); ret urn-4;} Close (FD);//delete last_install,last_logif (Remove (last_install_file)! = 0) printf ("[%s]remove last_install failed\n" , __function__); if (remove (last_log_file)! = 0) printf ("[%s]remove last_log failed\n", __function__); Remove (last_ Locale_file)! = 0) printf ("[%s]remove last_locale failed\n ", __function__); sync ();//reboot to Recovery__reboot (Linux_reboot_magic1, LINUX_REBOOT_ MAGIC2, Linux_reboot_cmd_restart2, (void*) "recovery");//This sentence requires root privileges! printf ("[%s]reboot failed\n", __function__); return-7;}

2. OTA upgrade function

int Install_ota_package (Char const * package_file, int use_fuse) {char *path = Null;int len = 0, size, Fd;len = strlen (pack Age_file); if (len <= 0) {printf ("[%s]strlen (Package_file) =%d\n", __function__, Len); return-1;} Path = (char*) malloc (len+24+3), if (path = = 0) {printf ("[%s]malloc failed\n", __function__); return-2;} Update_command_filememset (path, 0, len+24+3), if (use_fuse)//(STRNCMP (Package_file, "/vtfuse", 7)! = 0) {strcpy (path, "--update_package=/vtfuse"); strcpy (&path[24], package_file); strcpy (&path[24+len], "\ n"); else{strcpy (Path, "--update_package="), strcpy (&path[17], package_file), strcpy (&path[17+len], "\ n"); printf ("[%s]command:%s\n", __function__, path); fd = open (Update_command_file, o_rdwr| O_creat| O_trunc, s_irusr| S_IWUSR); if (FD < 0) {printf ("[%s]creat command File failed\n", __function__); free (path); return-3;} Size = strlen (path), if (write (fd, path, size)! = size) {printf ("[%s]write command File failed\n", __function__); free (path) ; Close (fd); return-4;} ClOSE (FD);//update_flag_filememset (path, 0, len+24+3), if (use_fuse)//(STRNCMP (Package_file, "/vtfuse", 7)! = 0) {strcpy ( Path, "Updating$path=/vtfuse"); strcpy (&path[21], package_file); strcpy (&path[21+len], "\ n"); else{strcpy (Path, "updating$path="), strcpy (&path[14], package_file), strcpy (&path[14+len], "\ n"); printf ("[%s]last_flag:%s\n", __function__, path); fd = open (Update_flag_file, o_rdwr| O_creat| O_trunc, s_irusr| S_IWUSR); if (FD < 0) {printf ("[%s]creat last_flag file failed\n", __function__); free (path); return-5;} Size = strlen (path), if (write (fd, path, size)! = size) {printf ("[%s]write last_flag file failed\n", __function__); Free (pat h); close (fd); return-6;} Close (FD);//delete last_install,last_logif (Remove (last_install_file)! = 0) printf ("[%s]remove last_install failed\n" , __function__); if (remove (last_log_file)! = 0) printf ("[%s]remove last_log failed\n", __function__); sync (); free (path) ;//reboot to Recovery__reboot (Linux_reboot_magic1, Linux_reboot_magic2, linux_reboot_cmD_restart2, (void*) "recovery");//also requires root permissions printf ("[%s]reboot failed\n", __function__); return-7;} 


3, in fact, the above two functions if compiled into EXE under the root of the operation can be implemented recovery and OTA upgrade, how to use in JNI or APK and implemented?

3.1 APK Request system permission, need to sign or compile in the source code!


3.2 apk is not directly able to get root permissions, up to system permissions, so we can take service!

Reference: http://blog.chinaunix.net/uid-12348673-id-3030823.html


3.3 Write the above function two applications, compiled and placed under/system/bin/, so that we can in JNI or apk to open the service:

In init.rc:

ServiceRecovery /system/bin/Recovery

Disabled


Apk:SystemProperties.set ("Ctl.start", "recovery");



Jni:Property_set("Ctl.start", "Recovery");


3.4 So you can achieve the Recovery,ota!




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.