本文转载自:http://blog.csdn.net/tfslovexizi/article/details/73835594

之前看到过一个人写了4.4上添加U盘升级功能的博客http://blog.csdn.net/kris_fei/article/details/50311885,写得挺好。

我们在5.1上也要做同样的功能,具体修改如下:

diff --git a/bootable/recovery/Android.mk b/bootable/recovery/Android.mk
old mode 100644
new mode 100755
index 5a1f479..8b21040
--- a/bootable/recovery/Android.mk
+++ b/bootable/recovery/Android.mk
@@ -39,7 +39,8 @@ LOCAL_SRC_FILES := \
     asn1_decoder.cpp \
     verifier.cpp \
     adb_install.cpp \
-    fuse_sdcard_provider.c
+    fuse_sdcard_provider.c \
+    fuse_udisk_provider.c
 
 LOCAL_MODULE := recovery
 
diff --git a/bootable/recovery/default_device.cpp b/bootable/recovery/default_device.cpp
old mode 100644
new mode 100755
index d92aaf5..f4b10ee
--- a/bootable/recovery/default_device.cpp
+++ b/bootable/recovery/default_device.cpp
@@ -33,6 +33,8 @@ static const char* ITEMS[] =  {"reboot system now",
                                "power down",
                                "view recovery logs",
                                "apply update from sdcard",
+                               "apply update from udisk",
                                NULL };
 
 class DefaultDevice : public Device {
@@ -73,6 +75,8 @@ class DefaultDevice : public Device {
           case 5: return SHUTDOWN;
           case 6: return READ_RECOVERY_LASTLOG;
           case 7: return APPLY_EXT;
+          case 8: return APPLY_FROM_UDISK;
           default: return NO_ACTION;
         }
     }
diff --git a/bootable/recovery/device.h b/bootable/recovery/device.h
old mode 100644
new mode 100755
index 8ff4ec0..82a3708
--- a/bootable/recovery/device.h
+++ b/bootable/recovery/device.h
@@ -64,8 +64,8 @@ class Device {
     //   - do nothing (kNoAction)
     //   - invoke a specific action (a menu position: any non-negative number)
     virtual int HandleMenuKey(int key, int visible) = 0;
-
-    enum BuiltinAction { NO_ACTION, REBOOT, APPLY_EXT,
+    enum BuiltinAction { NO_ACTION, REBOOT, APPLY_EXT,APPLY_FROM_UDISK,
                          APPLY_CACHE,   // APPLY_CACHE is deprecated; has no effect
                          APPLY_ADB_SIDELOAD, WIPE_DATA, WIPE_CACHE,
                          REBOOT_BOOTLOADER, SHUTDOWN, READ_RECOVERY_LASTLOG };
diff --git a/bootable/recovery/etc/init.rc b/bootable/recovery/etc/init.rc
old mode 100644
new mode 100755
index c78a44a..0fcc49f
--- a/bootable/recovery/etc/init.rc
+++ b/bootable/recovery/etc/init.rc
@@ -20,6 +20,7 @@ on init
     symlink /system/etc /etc
 
     mkdir /sdcard
+    mkdir /udisk
     mkdir /system
     mkdir /data
     mkdir /cache
diff --git a/bootable/recovery/fuse_udisk_provider.c b/bootable/recovery/fuse_udisk_provider.c
new file mode 100755
index 0000000..789a6eb
--- /dev/null
+++ b/bootable/recovery/fuse_udisk_provider.c
@@ -0,0 +1,141 @@
+/*
+ * Copyright (C) 2014 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <errno.h>
+#include <pthread.h>
+#include <sys/mount.h>
+#include <sys/stat.h>
+#include <unistd.h>
+#include <fcntl.h>
+
+#include "fuse_sideload.h"
+
+struct file_data {
+    int fd;  // the underlying sdcard file
+
+    uint64_t file_size;
+    uint32_t block_size;
+};
+
+static int read_block_file(void* cookie, uint32_t block, uint8_t* buffer, uint32_t fetch_size) {
+    struct file_data* fd = (struct file_data*)cookie;
+
+    if (lseek(fd->fd, block * fd->block_size, SEEK_SET) < 0) {
+        return -EIO;
+    }
+
+    while (fetch_size > 0) {
+        ssize_t r = read(fd->fd, buffer, fetch_size);
+        if (r < 0) {
+            if (r != -EINTR) {
+                return -EIO;
+            }
+            r = 0;
+        }
+        fetch_size -= r;
+        buffer += r;
+    }
+
+    return 0;
+}
+
+static void close_file(void* cookie) {
+    struct file_data* fd = (struct file_data*)cookie;
+    close(fd->fd);
+}
+
+struct token {
+    pthread_t th;
+    const char* path;
+    int result;
+};
+
+static void* run_udisk_fuse(void* cookie) {
+    struct token* t = (struct token*)cookie;
+
+    struct stat sb;
+    if (stat(t->path, &sb) < 0) {
+        t->result = -1;
+        return NULL;
+    }
+
+    struct file_data fd;
+    struct provider_vtab vtab;
+
+    fd.fd = open(t->path, O_RDONLY);
+    if (fd.fd < 0) {
+        t->result = -1;
+        return NULL;
+    }
+    fd.file_size = sb.st_size;
+    fd.block_size = 65536;
+
+    vtab.read_block = read_block_file;
+    vtab.close = close_file;
+
+    t->result = run_fuse_sideload(&vtab, &fd, fd.file_size, fd.block_size);
+    return NULL;
+}
+
+// How long (in seconds) we wait for the fuse-provided package file to
+// appear, before timing out.
+#define UDISK_INSTALL_TIMEOUT 10
+
+void* start_udisk_fuse(const char* path) {
+    struct token* t = malloc(sizeof(struct token));
+
+    t->path = path;
+    pthread_create(&(t->th), NULL, run_udisk_fuse, t);
+
+    struct stat st;
+    int i;
+    for (i = 0; i < UDISK_INSTALL_TIMEOUT; ++i) {
+        if (stat(FUSE_SIDELOAD_HOST_PATHNAME, &st) != 0) {
+            if (errno == ENOENT && i < UDISK_INSTALL_TIMEOUT-1) {
+                sleep(1);
+                continue;
+            } else {
+                return NULL;
+            }
+        }
+    }
+
+    // The installation process expects to find the sdcard unmounted.
+    // Unmount it with MNT_DETACH so that our open file continues to
+    // work but new references see it as unmounted.
+    umount2("/udisk", MNT_DETACH);
+
+    return t;
+}
+
+void finish_udisk_fuse(void* cookie) {
+    if (cookie == NULL) return;
+    struct token* t = (struct token*)cookie;
+
+    // Calling stat() on this magic filename signals the fuse
+    // filesystem to shut down.
+    struct stat st;
+    stat(FUSE_SIDELOAD_HOST_EXIT_PATHNAME, &st);
+
+    pthread_join(t->th, NULL);
+    free(t);
+}
diff --git a/bootable/recovery/fuse_udisk_provider.h b/bootable/recovery/fuse_udisk_provider.h
new file mode 100755
index 0000000..3313e9b
--- /dev/null
+++ b/bootable/recovery/fuse_udisk_provider.h
@@ -0,0 +1,23 @@
+/*
+ * Copyright (C) 2014 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef __FUSE_UDISK_PROVIDER_H
+#define __FUSE_UDISK_PROVIDER_H
+
+void* start_udisk_fuse(const char* path);
+void finish_udisk_fuse(void* token);
+
+#endif
diff --git a/bootable/recovery/recovery.cpp b/bootable/recovery/recovery.cpp
old mode 100644
new mode 100755
index 693ef19..b88364d
--- a/bootable/recovery/recovery.cpp
+++ b/bootable/recovery/recovery.cpp
@@ -47,6 +47,8 @@ extern "C" {
 #include "minadbd/adb.h"
 #include "fuse_sideload.h"
 #include "fuse_sdcard_provider.h"
+#include "fuse_udisk_provider.h"
 }
 
 struct selabel_handle *sehandle;
@@ -75,6 +77,8 @@ static const char *LAST_INSTALL_FILE = "/cache/recovery/last_install";
 static const char *LOCALE_FILE = "/cache/recovery/last_locale";
 static const char *CACHE_ROOT = "/cache";
 static const char *SDCARD_ROOT = "/sdcard";
+static const char *UDISK_ROOT = "/udisk";
 static const char *TEMPORARY_LOG_FILE = "/tmp/recovery.log";
 static const char *TEMPORARY_INSTALL_FILE = "/tmp/last_install";
 static const char *LAST_KMSG_FILE = "/cache/recovery/last_kmsg";
@@ -267,6 +271,15 @@ set_sdcard_update_bootloader_message() {
     set_bootloader_message(&boot);
 }
 
+static void
+set_udisk_update_bootloader_message() {
+    struct bootloader_message boot;
+    memset(&boot, 0, sizeof(boot));
+    strlcpy(boot.command, "boot-recovery", sizeof(boot.command));
+    strlcpy(boot.recovery, "recovery\n", sizeof(boot.recovery));
+    set_bootloader_message(&boot);
+}
 // read from kernel log into buffer and write out to file
 static void
 save_kernel_log(const char *destination) {
@@ -893,6 +906,44 @@ prompt_and_wait(Device* device, int status) {
                     }
                 }
                 break;
+            case Device::APPLY_FROM_UDISK: {
+                ensure_path_mounted(UDISK_ROOT);
+                char* path = browse_directory(UDISK_ROOT, device);
+                if (path == NULL) {
+                   
+                    break;
+                }
+                set_udisk_update_bootloader_message();
+                void* token = start_udisk_fuse(path);
+
+                int status = install_package(FUSE_SIDELOAD_HOST_PATHNAME, &wipe_cache,
+                                             TEMPORARY_INSTALL_FILE, false);
+
+                finish_udisk_fuse(token);
+                ensure_path_unmounted(UDISK_ROOT);        
+                if (status == INSTALL_SUCCESS && wipe_cache) {
+                    
+                    if (erase_volume("/cache")) {
+                        
+                    } else {
+                        ui->Print("Cache wipe complete.\n");
+                    }
+                }
+
+                if (status >= 0) {
+                    if (status != INSTALL_SUCCESS) {
+                        ui->SetBackground(RecoveryUI::ERROR);
+                        ui->Print("Installation aborted.\n");
+                    } else if (!ui->IsTextVisible()) {
+                        return Device::NO_ACTION;  // reboot if logs aren't visible
+                    } else {
+                        ui->Print("\nInstall from udisk complete.\n");
+                    }
+                }
+                break;
+            }
+
         }
     }
 }
diff --git a/device/qcom/slm753/recovery.fstab b/device/qcom/slm753/recovery.fstab
index 161a8a8..5632b77 100755
--- a/device/qcom/slm753/recovery.fstab
+++ b/device/qcom/slm753/recovery.fstab
@@ -31,6 +31,8 @@
 /dev/block/bootdevice/by-name/cache        /cache          ext4    noatime,nosuid,nodev,barrier=1,data=ordered                     wait,check
 /dev/block/bootdevice/by-name/userdata     /data           ext4    noatime,nosuid,nodev,barrier=1,data=ordered,noauto_da_alloc     wait,check
 /dev/block/mmcblk1p1                              /sdcard           vfat    nosuid,nodev,barrier=1,data=ordered,nodelalloc                  wait

+/dev/block/sda1                              /udisk           vfat    nosuid,nodev,barrier=1,data=ordered,nodelalloc                  wait
 /dev/block/bootdevice/by-name/boot         /boot           emmc    defaults                                                        defaults
 /dev/block/bootdevice/by-name/recovery     /recovery       emmc    defaults                                                        defaults
 /dev/block/bootdevice/by-name/misc         /misc           emmc    defaults                                                        defaults

好,代码处理OK,全编译验证通过。有问题可以沟通。

android5.1 Recovery添加从U盘升级功能【转】的更多相关文章

  1. STM32 IAP 固件升级设计/U盘升级固件

    源:STM32 IAP 固件升级设计/U盘升级固件 固件升级的基本思路是: 将stm32 的flash划分为两个区域: 1.Bootloader区:存放bootloader的代码,bootloader ...

  2. 树莓派插入U盘自动拷贝系统日志到U盘或通过U盘升级程序

    注意,U盘用Fat32格式,NTFS格式的话,需要在Linux另外安装相应驱动. 可通过udev实现如题的功能. 在/etc/udev/rules.d/目录下新建规则文件98-logcopy.rule ...

  3. iOS appStore中的应用 实现升级功能

    .h文件中 <UIAlertViewDelegate> .m文件中 #import "SBJson.h"        //解析sbjson 数据 - (void)vi ...

  4. 强大的Flutter App升级功能

    注意:无特殊说明,Flutter版本及Dart版本如下: Flutter版本: 1.12.13+hotfix.5 Dart版本: 2.7.0 应用程序升级功能是App的基础功能之一,如果没有此功能会造 ...

  5. 使用SimpleUpdater实现现有程序升级功能

    项目:https://github.com/iccfish/FSLib.App.SimpleUpdater C/S程式一般需要部署在多台机器上,如果程式有变动,需要一台一台重新安装,相当麻烦,如果我们 ...

  6. 为Debian/Ubuntu的apt-get install添加自动补齐/完成功能

    Debian/Ubuntu的apt-get太常用了,不过偶尔可能也会碰到不太熟悉,想不起来的包的名称,除了去debian packages去查找,另外的方法就是给Debian/Ubuntu添加自动补齐 ...

  7. HoverTree项目添加了查看留言列表功能

    HoverTree项目添加了查看留言列表功能 页面:HoverTreeWeb项目下hvtpanel/usermessage/messagelist.aspx 添加留言页面:addmessage.asx ...

  8. phpcms 内容——>评论管理 中添加 打开文章链接的 功能

    需要实现的功能:在后台管理系统中的 内容 下的——>评论管理  中添加 打开文章链接的 功能 1.数据库表是 v9_comment和v9_comment_data_1. v9_comment是被 ...

  9. (转)基于企业级证书的IOS应用打包升级功能介绍

    IOS应用程序升级流程介绍:IOS手机端应用程序需要升级时,打开服务器端html文件(本文为ucab.html文件)->点击在线安装->打开plist文件(本文中为ucab.plist文件 ...

随机推荐

  1. Queries for Number of Palindromes(求任意子列的回文数)

    H. Queries for Number of Palindromes time limit per test 5 seconds memory limit per test 256 megabyt ...

  2. 梦想iOS版CAD控件2018.11.07更新

    下载地址: http://www.mxdraw.com/ndetail_10110.html 1.  增加iOS上的CAD绘图接口和使用例子 2.  增加动态交互使用例子 3.  把Android上改 ...

  3. Effective C++标题整理

    Effective C++ 话说光看这50个tip又有什么用呢?只有实际使用的时候才体现它们的价值才对. 就像只看<代码大全>不能成为一个好程序员,必须结合实际写项目经验才行. 从C转向C ...

  4. layui 动态表格之合并单元格

    需求: 下面用excel表格大概模拟下需求,左边是原来的,要改成右边这样的: ①第一步:再生成表格后调用此方法,以合并重复的单元格 done : function(res, curr, count) ...

  5. Linux之awk用法

    简介 awk是一个强大的文本分析工具,相对于grep的查找,sed的编辑,awk在其对数据分析并生成报告时,显得尤为强大.简单来说awk就是把文件逐行的读入,以空格为默认分隔符将每行切片,切开的部分再 ...

  6. Python基础之函数参数与返回值进阶

    参数作用:如果外界希望在函数内部处理数据,就可以将数据作为参数传入函数内部: 返回值作用:如果希望一个函数函数执行完成后,向外界报告函数的执行结果,就可以使用函数的返回值. 函数的返回值 进阶 利用元 ...

  7. 3. Python中的分支判断、循环

    本文利用的是Python 3.x版本,建议学习3.x版本 Python中的分支判断.循环 1. 分支条件判断 1.1 比较操作 以下是数之间常见的比较操作,例如5>3就是数学意义上的比较,5是大 ...

  8. 04 Python基础数据类型

    目录: 1) 整型 2) 为什么使用16进制以及用在哪里 3) 浮点型 4) 字符串型 5) 布尔型 6) 查看数据类型 7) 复数型 8) input() 9) print() 10) % 格式化输 ...

  9. noip模拟赛 whzzt-Warmth

    分析:这道题难度和天天爱跑步差不了多少啊......裸的暴力只有10分,最好大的还是那个5%的数据,不过这也才15分,比天天爱跑步的暴力分不知道少到哪里去了. 正解是dp,毕竟要求方案数嘛,但是这个d ...

  10. 主流图数据库Neo4J、ArangoDB、OrientDB综合对比:架构分析

    主流图数据库Neo4J.ArangoDB.OrientDB综合对比:架构分析 YOTOY 关注 0.4 2017.06.15 15:11* 字数 3733 阅读 16430评论 2喜欢 18 1: 本 ...