一種通過U盤熱插拔的公升級方法

2021-07-15 04:27:37 字數 3062 閱讀 7320

在除錯android驅動中,有時會遇到無法使用adb的情況,如果能通過u盤的熱插拔能執行shell命令,就可解決adb無法使用的困境。

基本思路是是安裝乙個驅動,該驅動負責監測usb的插拔事件,然後通過非同步通知發給上層的應用,應用通過讀取u盤中的指令碼,通過system執行指令碼裡面的函式。

驅動檔案如下 

#include #include #include #define device_name "usbupdate"

static struct fasync_struct *usbupdate_async_queue;

static struct miscdevice usbupdate_dev;

int usbupdate_open(struct inode *node, struct file *filp)

int usbupdate_fasync(int fd, struct file *filp, int mode)

int usbupdate_release(struct inode *node, struct file *filp)

static struct file_operations usbupdate_dev_fops=;

static struct miscdevice usbupdate_dev = ;

static int usbupdate_notify(struct notifier_block *self, unsigned long action, void *dev)

return notify_ok;

}static struct notifier_block usbupdate_nb = ;

static int __init update_init(void)

static void __exit update_exit(void)

module_init(update_init);

module_exit(update_exit);

module_license("gpl");

module_author("www");

應用層的檔案為update.c

#include #include #include #include #include #include #include #include #include #include #include #define device_point "/dev/usbupdate"

#define log_tag "usbupdate"

#define filename "/mnt/udisk/update.sh" //公升級指令碼的路徑,不同機型的u盤掛載路徑不同,需根據機型修改

int usb_update_state=0;

int fd;

int status;

void systemstatus(int status)

else

else

} else

}

} int read_file(void)

sloge("malloc memory ok\n");

fp=fopen(filename,"r");

if(fp==null)

sloge("open ok!\n");

while(!feof(fp))

fclose(fp);

return 0;

}void sig_handler(int sig)

void open_usb_update_point(void)

signal(sigio, sig_handler);

fcntl(fd, f_setown, getpid());

f_flags = fcntl(fd, f_getfl);

fcntl(fd, f_setfl, fasync | f_flags);

}int main(int argc,char **argv)

sloge("usb update finish\n");

close(fd);

return 0;

}

相應的android.mk檔案如下

local_path := $(call my-dir)

include $(clear_vars)

local_module_tags := optional

local_prelink_module := false

local_shared_libraries := liblog

local_module := update

local_src_files := $(call all-subdir-c-files)

include $(build_executable)

在android原始碼的根路徑,source build/envsetup.sh,lunch選擇具體機型,通過mmm -b 應用程式路徑編譯該應用程式成可執行檔案,並丟進/system/bin/。

在init.rc通過如下服務啟動update服務,由於需要可執行許可權,需要在前面加上

chmod 0777  /system/bin/update
service loaddriver /system/bin/update  

class main

user root

group root

oneshot

當然,還必須保證驅動在該服務起來之前載入,否則該服務會由於開啟節點錯誤而退出。

然後將公升級指令碼拷貝到u盤,接上u盤,就可執行u盤上的指令碼了。公升級指令碼示例如下

mount -o remount /system

touch /system/dddd

mkdir /system/gggg

insmod /system/wwww.ko

cp /system/update.ko /system/

reboot

通過指標呼叫模板函式的一種方法

由於卡片的種類非常的多 100 為了對每種型別的卡片呼叫相應的處理函式,定義了乙個如下的函式模板 template int addcard const char pszcardstr,void base 可問題是怎麼獲得每個例項化後的模板函式的指標?查閱了大半天的資料,終於找到了一種可行的辦法 定義...

一種通過printf列印的除錯程式的方法

我們在寫 的時候,有時候需要除錯程式的時候,對於初學者,可以通過簡單的增加printf函式列印,這樣能夠來跟蹤程式的蹤跡,舉個很簡單的 include includeint main else printf s s d n file function line return 0 通過 printf ...

外掛程式的一種安裝方法

今天看到eclipse color themes這款外掛程式,各種心水啊 一直是could not find 我想了一下,可能是一下兩個問題 1 我直接用的是adt自帶的eclipse,可能有bug,導致不能安裝外掛程式。2 網路問題,單位的性質比較特殊,不容許翻牆,所以母雞是不是這個問題。最後,我...