Android JNI Library Implementation Reboot,recovery

Source: Internet
Author: User
<span id="Label3"></p><p><p><span style="font-size:24px">1. Recovery function:</span></p></p><p><p><span style="font-size:24px"></span></p></p><pre code_snippet_id="429351" snippet_file_name="blog_20140718_1_5205088" name="code" class="cpp">#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 authority! printf ("[%s]reboot failed\n", __function__); return-7;}</pre><br>2. OTA upgrade function<p><p></p></p><p><p><span style="font-size:24px"></span></p></p><pre code_snippet_id="429351" snippet_file_name="blog_20140718_2_4986937" name="code" class="cpp">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");//the same requires root permission printf ("[%s]reboot failed\n", __function__); return-7;} </pre><p><p></p></p><p><p><span style="font-size:24px"><br></span></p></p>3, in fact, the above two functions assume that compiling exe under the root of the execution can actually achieve recovery and OTA Upgrade. How do I lose and implement it in JNI or apk?<p><p><span style="font-size:24px">3.1 APK Request system permission, need to sign or compile in source code!</span></p></p><p><p><span style="font-size:24px"><br></span></p></p><p><p><span style="font-size:24px">3.2 apk is not directly able to get root permissions, up to system permissions, so we can take service!</span></p></p><p><p><span style="font-size:24px">References: http://blog.chinaunix.net/uid-12348673-id-3030823.html</span></p></p><p><p><span style="font-size:24px"><br></span></p></p><p><p><span style="font-size:24px">3.3 Write the above function to two applications. It is compiled and placed <span style="font-family:‘Courier New‘,courier,monospace; font-size:24px; line-height:16px; background-color:rgb(244,244,244)">under/system/bin/</span> so that we can open the service in JNI or apk:</span></p></p><p><p><span style="font-size:24px">In Init.rc:</span></p></p><p><p><span style="font-size:24px"><span style="font-family:‘Courier New‘,courier,monospace; line-height:16px; background-color:rgb(244,244,244)">Service<span style="color:rgb(0,96,128); font-family:‘Courier New‘,courier,monospace; font-size:24px; line-height:16px; background-color:rgb(244,244,244)">Recovery</span> /system/bin/<span style="color:rgb(0,96,128); font-family:‘Courier New‘,courier,monospace; font-size:24px; line-height:16px; background-color:rgb(244,244,244)">Recovery</span></span></span></p></p><p style="word-wrap:break-word; margin-top:0px; margin-bottom:1em; padding-top:0px; padding-bottom:0px; font-family:‘Courier New‘,courier,monospace; line-height:16px; background-color:rgb(244,244,244)"><p style="word-wrap:break-word; margin-top:0px; margin-bottom:1em; padding-top:0px; padding-bottom:0px; font-family:‘Courier New‘,courier,monospace; line-height:16px; background-color:rgb(244,244,244)"></p></p><span style="font-family:‘Courier New‘,courier,monospace; line-height:16px; background-color:rgb(244,244,244)"><span style="font-family:‘Courier New‘,courier,monospace; line-height:16px; background-color:rgb(244,244,244)">Disabled</span></span><br><p><p><span style="font-size:24px"><br></span></p></p><p><p><span style="font-size:24px"><span style="font-family:‘Courier New‘,courier,monospace; line-height:16px; background-color:rgb(244,244,244)">Apk:SystemProperties.set (</span><span style="word-wrap:break-word; font-family:‘Courier New‘,courier,monospace; line-height:16px; background-color:rgb(244,244,244); color:rgb(0,96,128)">"ctl.start"</span><span style="font-family:‘Courier New‘,courier,monospace; line-height:16px; background-color:rgb(244,244,244)">, </span> <span style="word-wrap:break-word; font-family:‘Courier New‘,courier,monospace; line-height:16px; background-color:rgb(244,244,244); color:rgb(0,96,128)">"recovery"</span><span style="font-family:‘Courier New‘,courier,monospace; line-height:16px; background-color:rgb(244,244,244)">);</span><br></span></p></p><p><p><span style="font-size:24px"><span style="font-family:‘Courier New‘,courier,monospace; line-height:16px; background-color:rgb(244,244,244)"><br></span></span></p></p><p><p><span style="font-size:24px"><span style="font-family:‘Courier New‘,courier,monospace; line-height:16px; background-color:rgb(244,244,244)"><br></span></span></p></p><p><span style="font-size:24px"><span style="font-family:‘Courier New‘,courier,monospace; line-height:16px; background-color:rgb(244,244,244)">Jni:<span style="color:rgb(51,51,51); font-family:Arial; font-size:14.399999618530273px; line-height:26px"><span style="font-family:‘Courier New‘,courier,monospace; font-size:24px; line-height:16px; background-color:rgb(244,244,244)"><span style="font-family:‘Courier New‘,courier,monospace; font-size:24px; line-height:16px; background-color:rgb(244,244,244)">Property_set</span></span><span style="font-size:24px; font-family:‘Courier New‘,courier,monospace; line-height:16px; background-color:rgb(244,244,244)"><span style="font-size:24px; font-family:‘Courier New‘,courier,monospace; line-height:16px; background-color:rgb(244,244,244)">(</span></span><span style="font-size:24px; word-wrap:break-word; font-family:‘Courier New‘,courier,monospace; line-height:16px; background-color:rgb(244,244,244); color:rgb(0,96,128)"><span style="font-size:24px; word-wrap:break-word; font-family:‘Courier New‘,courier,monospace; line-height:16px; background-color:rgb(244,244,244); color:rgb(0,96,128)">"ctl.start"</span></span><span style="font-size:24px; font-family:‘Courier New‘,courier,monospace; line-height:16px; background-color:rgb(244,244,244)"><span style="font-size:24px; font-family:‘Courier New‘,courier,monospace; line-height:16px; background-color:rgb(244,244,244)">, </span></span><span style="font-size:24px; word-wrap:break-word; font-family:‘Courier New‘,courier,monospace; line-height:16px; background-color:rgb(244,244,244); color:rgb(0,96,128)"><span style="font-size:24px; word-wrap:break-word; font-family:‘Courier New‘,courier,monospace; line-height:16px; background-color:rgb(244,244,244); color:rgb(0,96,128)">"recovery"</span></span><span style="font-size:24px; font-family:‘Courier New‘,courier,monospace; line-height:16px; background-color:rgb(244,244,244)"><span style="font-size:24px; font-family:‘Courier New‘,courier,monospace; line-height:16px; background-color:rgb(244,244,244)">);</span></span></span></span></span></p><p><p><span style="font-size:24px"><br></span></p></p><p><p><span style="font-size:24px">3.4 This will enable the Recovery,ota to be realized!</span></p></p><p><p><span style="font-size:24px"><br></span></p></p><p><p><span style="font-size:24px"><br><br></span></p></p> <p><p>Android JNI Library Implementation Reboot,recovery</p></p></span>

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.