2012年11月21日 星期三

LDD3 scull_load 無法編譯 修改方法

 [LINUX] 在ubuntu環境編譯 skull 的方法
編譯LDD3書上範例 skull時,遇到了一些問題,茲記錄如下:

程式碼範例下載:http://www.oreilly.com.tw/product_linux.php?id=a184
測試機器編譯環境:Linux 2.6.32-45-generic #Ubuntu 10.04
書上的範例是適用於 Linux 2.4/2.6 版本的,因此在之後版本的核心下編譯會造成許多錯誤訊息,以下針對scull的編譯錯誤作一整理說明。

1. 缺少linux/config.h,

    原因:因為 config.h 在 linux kernel 2.6.19已經拿到 config.h
    改法:有兩種方式
    a. 在檔案內直接註解 #include <linux/config.h>
    b. 自行建立一個空的檔案,例如執行下列命令
    touch /usr/src/linux-headers-`uname -r`/include/linux/config.


2. scripts/Makefile.build:49: *** CFLAGS was changed in "/home/albert/examples/scull/Makefile". Fix it to use ccflags-y. Stop.

    改法:有兩種方式:
    a. CFLAGS -> EXTRA_CFLAGS
    b. make KBUILD_NOPEDANTIC=1



3. 錯誤: 初始值設定項裡有不明的欄位 「ioctl」

    linux內核到2.6.36之後把ioctl這個成員給移除了,改用了以下兩名新成員

        long (*unlocked_ioctl) (struct file *, unsigned int, unsigned long);
        long (*compat_ioctl) (struct file *, unsigned int, unsigned long);

    此處需更改兩個地方

    a. 更改 file_operations 的 ioctl

        struct file_operations scull_sngl_fops = {
        .owner = THIS_MODULE,
        .llseek = scull_llseek,
        .read = scull_read,
        .write = scull_write,
        //.ioctl = scull_ioctl,
        .unlocked_ioctl = scull_ioctl,
        .open = scull_s_open,
        .release = scull_s_release,
        };

    b. 更改 scull_ioctl 的函數原型 (main.c scull.h)

        //int scull_ioctl(struct inode *inode, struct file *filp, unsigned int cmd, unsigned long arg)
        long scull_ioctl( struct file *filp, unsigned int cmd, unsigned long arg)


4. 隱含宣告函式 「init_MUTEX」 [-Werror=implicit-function-declaration]


    在新版本的linux?核中,已經廢除init_mutex,新版本直接使用sema_init()。原始的定義如下 (linux 2.6.xx 時仍存在此定義)

        #define init_MUTEX(sem) sema_init(sem, 1)
        #define init_MUTEX_LOCKED(sem) sema_init(sem, 0)

    因此可更改如下:

        //init_MUTEX(&scull_devices[i].sem);
        sema_init(&scull_devices[i].sem, 1);


5. 錯誤: 「TASK_INTERRUPTIBLE」 undeclared (first use in this function)

    此常數定義在 sched.h
    需在 access.c 與 pipe.c 加上 #include <linux/sched.h>  即可


6. 找不到 SPIN_LOCK_UNLOCKED’

    原因:SPIN_LOCK_UNLOCKED has been deprecated since 2.6.19 and will get removed in 2.6.39。
    改法:
    // static spinlock_t scull_u_lock = SPIN_LOCK_UNLOCKED;
    static DEFINE_SPINLOCK(scull_u_lock);


7. 錯誤: 「struct task_struct」 沒具名為 「uid」 的成員

    原因:struct task_struct 已經重新定義
    改法:

        current->uid 改成 current->cred->uid
        current->euid 改成 current->cred->euid
        current->comm 改成 current->cred->comm

Hello World module 安裝


建立root密碼:
  • 1. 安裝完Ubuntu的時候,第一次使用root會提示要你輸入password,你會發現你自己根本沒有設置password。如果你要使用和Unix一樣使用root帳戶,解決辦法是:
  • 1.1. 在終端輸入 sudo passwd root (注意是passwd不是password)這樣你就可以通過 su 登錄成為root,它會提示要求password,這時候你就輸入你當前帳戶的密碼。然後它會提示你輸入Enter new UNIX password 和 Rtry new UNIX password。那麼你就新建一個root賬戶的密碼就可以了,看見password updated successfully就成功了。
編寫hello.c

編寫Makefile 
將hello.c與Makefile放在相同資料夾下
%make
%su
%insmod ./hello.ko
%lsmod
%dmesg
%rmmod hello
%dmesg

當然,如果你使用的是ubuntu,以上命令前要加sudo。打入 sudo insmod demo.ko,輸入管理者密碼即可成功掛載。當然畫面上不會出現任何訊息,因為printf是寫入核心態,我們的畫面是使用者態,核心的printf只 能從log或dmesg命令查看,因此打入dmesg,就可以看到掛載訊息了。至於相同的,卸載就打入 sudo rmmod demo,從dmesg也可以看到成功卸載的log。

字元驅動程式筆記

1. dev_t 型別
(1)在核心中,dev_t型別(定義在linux/types.h 中)用來保存裝置編號 - 包括major和minor。
(2)在核心 2.6.0中,dev_t是一個『32位的數』,其中12位表示major,20位表示minor。


(3)獲得dev_t的major、minor:
  MAJOR(dev_t dev);
  MINOR(dev_t dev);
(4)轉換成dev_t 類型:
  MKDEV(int major, int minor);

2. 向kernel註冊
(1)include linux/fs.h
fs.h 是編寫device drivers 所需要的header file. 許多重要的函數和數據結構在此定義.
(2) 註冊函數
事先設定major==> int register_chrdev_region(dev_t first, unsigned int count, char *name)
動態分配major==> int alloc_chrdev_region(dev_t *dev, unsigned int firstminor, unsigned int count, char *name)
反註冊都相同 ==> void unregister_chrdev_region(dev_t first, unsigned int count);
ps: 核心2.6以前的方法 ==>int register_chrdev(unsigned int major, const char *name, struct file_operations *fops);
2.6也還能用,但效能彈性有限。
而反註冊方法===> int unregister_chrdev(unsigned int major, const char *name);


3. cdev 結構管理的函數, 它代表内核中的字元設備.
(1)include linux/cdev.h
struct cdev *cdev_alloc(void);
void cdev_init(struct cdev *dev, struct file_operations *fops);
int cdev_add(struct cdev *dev, dev_t num, unsigned int count);
void cdev_del(struct cdev *dev);


4. file_operations 結構
這個結構定義在linux/fs.h中,用來建立驅動程式操作到設備編號的連接。
它包含了一组函數指標,每個打開的文件(在內部用file表示)和一组函數關聯(通過包含指向一個file_operations结構的『f_op』字段)。


5. file结構
stuct file定義在linux/fs.h中,是一個內核結構,它不會出現在用戶程序中。
(與用戶空間程序中的FILE结構沒有任何關聯,FILE不會出现在內核代碼中。)
   file结構代表一個打開的文件(不僅限於設備驅動程序,系统中每個打開的文件在內核空間中都有一個對應的file结構)。
然而,指向『struct file』的指標通常稱為『flip』。


6. read和write方法
(1)ssize_t read(struct file *filp, char __user *buff, size_t count, loff_t *offp);
ssize_t write(struct file *filp, const char __user *buff, size_t count, loff_t *offp);

(2)參數filp是文件指標,count是請求傳輸的數據長度,buff是指向用戶空間的緩衝區,
offp是一個指向“long offset type”對象的指標,
它指明用戶在文件中進行存取操作的位置。
返回值是“signed size type(有符號的尺寸類型)”。

(3)read和write方法不能直接引用buff參數(用戶空間)的內容,
安全訪問應該通過『内核提供的專用函數』完成:(定義在asm/uaccess.h中)
unsigned long copy_to_user(void __user *to,const void *from,unsigned long count);
unsigned long copy_from_user(void *to,const void __user *from,unsigned long count);

(4)read和write方法的返回值
http://wiki.magiclinux.org/ftp/haulm/book/Device/ch03s07.html (3.7.1/2)
其中『負值』意味著『錯誤』,錯誤碼 在linux/errno.h中定義,
如:-EINTR(系统調用被中斷)-EFAULT(無效地址)