導航:首頁 > 源碼編譯 > 源碼編譯驅動

源碼編譯驅動

發布時間:2023-09-01 17:08:11

A. 驅動源碼是什麼,是怎麼個概念,麻煩講的通俗點。

就是驅動程序的源程序
編寫驅動程序要先寫出源碼,然後編譯。編譯之前的代碼就是源碼。
你下載的驅動都是經過編譯的,是可執行文件,不是源碼。源碼只有編寫者手裡才有的。

B. Android源碼編譯在真機上運行時為什麼要同時編譯驅動

模擬器也需要驅動啊,是goldfish,真機沒有驅動(針對硬體的),運行不起來的。源碼是不帶驅動源碼的,驅動源碼要另外下載,源碼里的是已經編譯好的kernel。

C. 怎麼將驅動源代碼編譯進linux系統

一、 驅動程序編譯進內核的步驟
在 linux 內核中增加程序需要完成以下三項工作:
1. 將編寫的源代碼復制到 Linux 內核源代碼的相應目錄;
2. 在目錄的 Kconfig 文件中增加新源代碼對應項目的編譯配置選項;
3. 在目錄的 Makefile 文件中增加對新源代碼的編譯條目。

bq27501驅動編譯到內核中具體步驟如下:
1. 先將驅動代碼bq27501文件夾復制到 ti-davinci/drivers/ 目錄下。
確定bq27501驅動模塊應在內核源代碼樹中處於何處。
設備驅動程序存放在內核源碼樹根目錄 drivers/ 的子目錄下,在其內部,設備驅動文件進一步按照類別,類型等有序地組織起來。
a. 字元設備存在於 drivers/char/ 目錄下
b. 塊設備存放在 drivers/block/ 目錄下
c. USB 設備則存放在 drivers/usb/ 目錄下。
注意:
(1) 此處的文件組織規則並非絕對不變,例如: USB 設備也屬於字元設備,也可以存放在 drivers/usb/ 目錄下。
(2) 在 drivers/char/ 目錄下,在該目錄下同時存在大量的 C 源代碼文件和許多其他目錄。所有對於僅僅只有一兩個源文件的設備驅動程序,可以直接存放在該目錄下,但如果驅動程序包含許多源文件和其他輔助文件,那麼可以創建一個新子目錄。
(3) bq27501的驅動是屬於字元設備驅動類別,雖然驅動相關的文件只有兩個,但是為了方面查看,將相關文件放在了bq27501的文件夾中。在drivers/char/目錄下增加新的設備過程比較簡單,但是在drivers/下直接添加新的設備稍微復雜點。所以下面首先給出在drivers/下添加bq27501驅動的過程,然後再簡單說明在drivers/char/目錄下添加的過程。

2. 在/bq27501下面新建一個Makefile文件。向裡面添加代碼:
obj-$(CONFIG_BQ27501)+=bq27501.o
此時,構建系統運行就將會進入 bq27501/ 目錄下,並且將bq27501.c 編譯為 bq27501.o
3. 在/bq27501下面新建Kconfig文件。添加代碼:
menu "bq27501 driver"

config BQ27501
tristate"BQ27501"
default y
---help---
Say 'Y' here, it will be compiled into thekernel; If you choose 'M', it will be compiled into a mole named asbq27501.ko.
endmenu
注意:help中的文字不能加回車符,否則make menuconfig編譯的時候會報錯。
4. 修改/drivers目錄下的Kconfig文件,在endmenu之前添加一條語句『source drivers/bq27501/Kconfig』 對於驅動程序,Kconfig 通常和源代碼處於同一目錄。 若建立了一個新的目錄,而且也希望 Kconfig 文件存在於該目錄中的話,那麼就必須在一個已存在的 Kconfig 文件中將它引入,需要用上面的語句將其掛接在 drivers 目錄中的Kconfig 中。

5. 修改/drivers目下Makefile文件,添加『obj-$(CONFIG_BQ27501) +=bq27501/』。這行編譯指令告訴模塊構建系統在編譯模塊時需要進入 bq27501/ 子目錄中。此時的驅動程序的編譯取決於一個特殊配置 CONFIG_BQ27501 配置選項。

6. 修改arch/arm目錄下的Kconfig文件,在menu "Device Drivers……endmenu"直接添加語句
source "drivers/bq27501/Kconfig"

D. 如何編譯一個linux下的驅動模塊

linux下編譯運行驅動
嵌入式linux下設備驅動的運行和linux x86 pc下運行設備驅動是類似的,由於手頭沒有嵌入式linux設備,先在vmware上的linux上學習驅動開發。
按照如下方法就可以成功編譯出hello world模塊驅動。
1、首先確定本機linux版本
怎麼查看Linux的內核kernel版本?
'uname'是Linux/unix系統中用來查看系統信息的命令,適用於所有Linux發行版。配合使用'uname'參數可以查看當前伺服器內核運行的各個狀態。
#uname -a
Linux whh 3.5.0-19-generic #30-Ubuntu SMPTue Nov 13 17:49:53 UTC 2012 i686 i686 i686 GNU/Linux

只列印內核版本,以及主要和次要版本:
#uname -r
3.5.0-19-generic

要列印系統的體系架構類型,即的機器是32位還是64位,使用:
#uname -p
i686

/proc/version 文件也包含系統內核信息:
# cat /proc/version
Linux version 3.5.0-19-generic(buildd@aatxe) (gcc version 4.7.2 (Ubuntu/Linaro 4.7.2-2ubuntu1) ) #30-UbuntuSMP Tue Nov 13 17:49:53 UTC 2012

發現自己的機器linux版本是:3.5.0-19-generic
2、下載機器內核對應linux源碼
到下面網站可以下載各個版本linux源碼https://www.kernel.org/
如我的機器3.5.0版本源碼下載地址為:https://www.kernel.org/pub/linux/kernel/v3.x/linux-3.5.tar.bz2
下載完後,找一個路徑解壓,如我解壓到/linux-3.5/
然後很重要的一步是:執行命令uname -r,可以看到Ubuntu的版本信息是3.5.0-19-generic
。進入linux源碼目錄,編輯Makefile,將EXTRAVERSION = 修改為EXTRAVERSION= -19-generic。
這些都是要配置源碼的版本號與系統版本號,如果源碼版本號和系統版本號不一致,在載入模塊的時候會出現如下錯誤:insmod: error inserting 'hello.ko': -1 Invalid mole format。
原因很明確:編譯時用的hello.ko的kenerl 不是我的pc的kenerl版本。

執行命令cp /boot/config-3.5.0-19-generic ./config,覆蓋原有配置文件。
進入linux源碼目錄,執行make menuconfig配置內核,執行make編譯內核。

3、寫一個最簡單的linux驅動代碼hello.c

/*======================================================================
Asimple kernel mole: "hello world"
======================================================================*/
#include <linux/init.h>
#include <linux/mole.h>
MODULE_LICENSE("zeroboundaryBSD/GPL");
static int hello_init(void)
{
printk(KERN_INFO"Hello World enter\n");
return0;
}

static void hello_exit(void)
{
printk(KERN_INFO"Hello World exit\n ");
}

mole_init(hello_init);
mole_exit(hello_exit);

MODULE_AUTHOR("zeroboundary");
MODULE_DESCRIPTION("A simple HelloWorld Mole");
MODULE_ALIAS("a simplestmole");

4、寫一個Makefile對源碼進行編譯
KERN_DIR = /linux-3.5
all:
make-C $(KERN_DIR) M=`pwd` moles
clean:
make-C $(KERN_DIR) M=`pwd` clean

obj-m += hello.o

5、模塊載入卸載測試
insmod hello.ko
rmmod hello.ko

然後dmesg|tail就可以看見結果了

最後,再次編譯驅動程序hello.c得到hello.ko。執行insmod ./hello.ko,即可正確insert模塊。

使用insmod hello.ko 將該Mole加入內核中。在這里需要注意的是要用 su 命令切換到root用戶,否則會顯示如下的錯誤:insmod: error inserting 'hello.ko': -1 Operation not permitted

內核模塊版本信息的命令為modinfo hello.ko
通過lsmod命令可以查看驅動是否成功載入到內核中
通過insmod命令載入剛編譯成功的time.ko模塊後,似乎系統沒有反應,也沒看到列印信息。而事實上,內核模塊的列印信息一般不會列印在終端上。驅動的列印都在內核日誌中,我們可以使用dmesg命令查看內核日誌信息。dmesg|tail

可能還會遇到這種問題insmod: error inserting 'hello.ko': -1 Invalid mole format
用dmesg|tail查看內核日誌詳細錯誤
disagrees about version of symbolmole_layout,詳細看這里。
http://www.ibm.com/developerworks/cn/linux/l-cn-kernelmoles/index.html
在X86上我的辦法是:
make -C/usr/src/linux-headers-3.5.0-19-generic SUBDIRS=$PWD moles

E. 如何在Ubuntu上安裝內核對應的源碼來編譯驅動

如何在Ubuntu上安裝內核對應的源碼來編譯驅動?
最近在學驅動模塊,平台是ARM,因為課上老師講的有點顯淺,所以打算自己再深入研究一下。
所以在虛擬機裡面裝了ubuntu,打算用來深入學習。
目前的版本是
# uname -a
Linux ubuntu-ldm 4.4.0-28-generic #47-Ubuntu SMP Fri Jun 24 10:09:13 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux

我已經安裝了以下的包
# dpkg -l | grep linux
ii console-setup-linux 1.108ubuntu15 all Linux specific part of console-setup
ii libselinux1:amd64 2.4-3build2 amd64 SELinux runtime shared libraries
ii linux-base 4.0ubuntu1 all Linux image base package
ii linux-firmware 1.157.2 all Firmware for Linux kernel drivers
ii linux-generic 4.4.0.28.30 amd64 Complete Generic Linux kernel and headers
ii linux-headers-4.4.0-28 4.4.0-28.47 all Header files related to Linux kernel version 4.4.0
ii linux-headers-4.4.0-28-generic 4.4.0-28.47 amd64 Linux kernel headers for version 4.4.0 on 64 bit x86 SMP
ii linux-headers-generic 4.4.0.28.30 amd64 Generic Linux kernel headers
ii linux-image-4.4.0-28-generic 4.4.0-28.47 amd64 Linux kernel image for version 4.4.0 on 64 bit x86 SMP
ii linux-image-extra-4.4.0-28-generic 4.4.0-28.47 amd64 Linux kernel extra moles for version 4.4.0 on 64 bit x86 SMP
ii linux-image-generic 4.4.0.28.30 amd64 Generic Linux kernel image
ii linux-libc-dev:amd64 4.4.0-28.47 amd64 Linux Kernel Headers for development
ii linux-sound-base 1.0.25+dfsg-0ubuntu5 all base package for ALSA and OSS sound systems
ii linux-source 4.4.0.28.30 all Linux kernel source with Ubuntu patches
ii linux-source-4.4.0 4.4.0-28.47 all Linux kernel source for version 4.4.0 with Ubuntu patches
ii util-linux 2.27.1-6ubuntu3.1 amd64 miscellaneous system utilities

並且將/usr/src/linux-source-4.4.0.tar.bz2解壓到/home/test/WorkSpace/Kernel/linux-source-4.4.0下
然後寫了一個小小的測試程序
#include <linux/init.h>
#include <linux/mole.h>

MODULE_LICENSE("Dual BSD/GPL");

static int hello_init (void) {
printk (KERN_ALERT "Hello, World\n");
return 0;
}

static void hello_exit (void) {
printk (KERN_ALERT "Goodbye, cruel world\n");
}

mole_init (hello_init);
mole_exit (hello_exit);

Makefile
obj-m += mole.o

mole-objs := hello.o

all:
make moles M=`pwd` -C /home/test/WorkSpace/Kernel/linux-source-4.4.0

clean:
make moles clean M=`pwd` -C /home/test/WorkSpace/Kernel/linux-source-4.4.0

令我不解的是
# sudo insmod mole.ko
insmod: ERROR: could not insert mole mole.ko: Invalid mole format

實在想不通為何沒法裝載這個內核模塊?
是因為我用錯了版本的源碼嘛?
難道是要用自己編譯的uImage來啟動系統才可以嘛?
要如何自己手動編譯替換Ubuntu的內核?

F. 如何編譯一個linux下的驅動模塊

這是一個簡單而完整的實例,對於理解Linux下的驅動模塊是非常有幫助的。

1.源碼如下:
/*
* hello.c -- the example of printf "hello world!" in the screen of driver program
*/
#include <linux/init.h>
#include <linux/mole.h>
MODULE_LICENSE("Dual BSD/GPL");/* declare the license of the mole ,it is necessary */
static int hello_init(void)
{
printk(KERN_ALERT "Hello World enter!\n");
return 0;
}
static int hello_exit(void)
{
printk(KERN_ALERT "Hello world exit!\n");
}
mole_init(hello_init); /* load the mole */
mole_exit(hello_exit); /* unload the mole */
進入目錄:
[root@Alex_linux /]#cd /work/jiakun_test/moletest
[root@Alex_linux moletest]# vi hello.c
然後拷入上面書上的源碼。
2.編譯代碼:
1>.首先我在2.4內核的虛擬機上進行編譯,編譯過程如下:
[root@Alex_linux moletest]#gcc -D__KERNEL__ -I /usr/src/linux -DMODULE -Wall -O2 -c -o hello.o hello.c
其中-I選項指定內河源碼,也就是內核源碼樹路徑。編譯結果:
hello.c:1:22: net/sock.h: No such file or directory
hello.c: In function `hello_init':
hello.c:6: warning: implicit declaration of function `printk'
hello.c:6: `KERN_ALERT' undeclared (first use in this function)
hello.c:6: (Each undeclared identifier is reported only once
hello.c:6: for each function it appears in.)
hello.c:6: parse error before string constant
hello.c: In function `hello_exit':
hello.c:11: `KERN_ALERT' undeclared (first use in this function)
hello.c:11: parse error before string constant
hello.c: At top level:
hello.c:13: warning: type defaults to `int' in declaration of `mole_init'
hello.c:13: warning: parameter names (without types) in function declaration
hello.c:13: warning: data definition has no type or storage class
hello.c:14: warning: type defaults to `int' in declaration of `mole_exit'
hello.c:14: warning: parameter names (without types) in function declaration
hello.c:14: warning: data definition has no type or storage class
在網上查詢有網友提示沒有引入kernel.h
解決:vi hello.c
在第一行加入:#include <linux/kernel.h>
再次編譯仍然報KERN_ALERT沒有聲明
修改編譯條件-I,再次編譯:
[root@Alex_linux moletest]#gcc -D__KERNEL__ -I /usr/src/linux -DMODULE -Wall -O2 -c -o hello.o hello.c
[root@Alex_linux moletest]#ls
hello.c hello.o Makefile
[root@Alex_linux moletest]#
2>.接著我嘗試在2.6內核的虛擬機上進行編譯
編譯過程如下:
[root@JiaKun moletest]# ls
hello.c makefile
[root@JiaKun moletest]# vi hello.c
[root@JiaKun moletest]# make
make -C /mylinux/kernel/2.4.18-rmk7 M=/home/alex/test/moletest moles
make: *** /mylinux/kernel/2.4.18-rmk7: No such file or directory. Stop.
make: *** [moles] Error 2
[root@JiaKun moletest]# vi makefile
[root@JiaKun moletest]# make
make -C /usr/src/kernels/2.6.18-53.el5-i686 M=/home/alex/test/moletest moles
make[1]: Entering directory `/usr/src/kernels/2.6.18-53.el5-i686'
scripts/Makefile.build:17: /home/alex/test/moletest/Makefile: No such file or directory
make[2]: *** No rule to make target `/home/alex/test/moletest/Makefile'. Stop.
make[1]: *** [_mole_/home/alex/test/moletest] Error 2
make[1]: Leaving directory `/usr/src/kernels/2.6.18-53.el5-i686'
make: *** [moles] Error 2
[root@JiaKun moletest]# mv makefile Makefile
[root@JiaKun moletest]# make
make -C /usr/src/kernels/2.6.18-53.el5-i686 M=/home/alex/test/moletest moles
make[1]: Entering directory `/usr/src/kernels/2.6.18-53.el5-i686'
CC [M] /home/alex/test/moletest/hello.o
Building moles, stage 2.
MODPOST
CC /home/alex/test/moletest/hello.mod.o
LD [M] /home/alex/test/moletest/hello.ko
make[1]: Leaving directory `/usr/src/kernels/2.6.18-53.el5-i686'
[root@JiaKun moletest]# ls
hello.c hello.ko hello.mod.c hello.mod.o hello.o Makefile Mole.symvers

3.執行代碼,載入驅動模塊:
2.4內核載入模塊:
insmod ./hello.o
但是此時並沒有輸出printk列印的信息。但是可以在/var/log/messages 中看到列印的信息,這是由於KERN_ALERT優先順序不夠高。這里
需要修改為:KERN_EMERG。再次編譯,載入模塊即可以看到結果
2.6內核載入模塊:
[root@JiaKun moletest]# insmod hello.ko
[root@JiaKun moletest]#
Message from syslogd@ at Sat Jul 26 19:52:44 2008 ...
JiaKun kernel: Hello, world
有的朋友可能會出現insmod命令找不到的錯誤,這可能有下面幾個原因:
<1> 你的系統沒有安裝mole-init-tools工具,關於此問題,只需安裝即可,但是一般裝完系統是有這個命令的。
<2> 環境變數沒有添加導致不能使用該命令。使用echo $PATH即可查看PATH環境變數,發現沒有/sbin這個路徑,所以你當然不能使用insmod這個命令了。解決的方法很簡單,只需在命令行輸入:
PATH = "$PATH:/sbin"即可添加。(insmod在/sbin這個目錄下,你可以使用whereis insmod查看)。
<3> insmod這個命令需要在root許可權下才能使用。
載入完成後你可以輸入lsmod查看hello這個模塊哦。

4.卸載驅動模塊:rmmod hello.
載入模塊後就可在屏幕上看到如下信息:Hello world enter.
卸載時就可在屏幕上看到如下信息:hello world exit.
[root@JiaKun moletest]# rmmod hello.ko
[root@JiaKun moletest]#
Message from syslogd@ at Sat Jul 26 19:52:58 2008 ...
JiaKun kernel: Goodbye, cruel world

另外,如果有多個文件,則按下列方式編寫Makefile文件(file1.c、file2.c):
obj -m := molename.o
mole-objs := file1.o file2.o

閱讀全文

與源碼編譯驅動相關的資料

熱點內容
吃雞國際體驗服為什麼伺服器繁忙 瀏覽:92
php中sleep 瀏覽:488
vr怎麼看視頻演算法 瀏覽:84
手機app如何申報個人所得稅零申報 瀏覽:692
如何截獲手機app連接的ip 瀏覽:330
冰箱壓縮機是否需要電容 瀏覽:344
python列表每一行數據求和 瀏覽:274
自己有一台伺服器可以玩什麼 瀏覽:656
社會學波普諾pdf 瀏覽:584
解壓做食物的小視頻 瀏覽:758
pdf怎麼單獨設置文件夾 瀏覽:474
業務邏輯程序員 瀏覽:659
addto新建文件夾什麼意思 瀏覽:160
有伺服器地址怎麼安裝軟體 瀏覽:659
安卓如何完全清除數據 瀏覽:690
安卓安卓證書怎麼信任 瀏覽:53
伺服器被攻擊如何解決 瀏覽:221
學霸變成程序員 瀏覽:882
c語言編譯錯誤fatalerror 瀏覽:441
ipv4內部伺服器地址怎麼分配 瀏覽:464