Linux Device Driver >> Assembly (ARM)

hello, world!


參考資訊:
1. ldd

hello, world!是一個相當經典的入門教學範例,從這個範例的框架,使用者可以一探Linux Kernel的精簡之美,當然,它主要表達的目的,更多是對於操作環境的熟悉

ldd.S

    .global init_module
    .global cleanup_module

    .section .modinfo, "ae"
__UNIQUE_ID_0: .asciz "license=GPL"
__UNIQUE_ID_1: .asciz "author=Steward Fu"
__UNIQUE_ID_2: .asciz "description=Linux Driver"

    .section .text
msg_load:   .asciz "hello, world!\n" 
msg_unload: .asciz "unload it\n"

    .align 2
    .section .text
init_module:
    push {lr}
    ldr r0, =msg_load
    bl printk
    mov r0, #0
    pop {pc}

cleanup_module:
    push {lr}
    ldr r0, =msg_unload
    bl printk
    pop {pc}
    .end

init_module: 載入驅動程式時,系統會呼叫使用
cleanup_module: 卸載驅動程式時,系統會呼叫使用

Makefile

export ARCH=arm
export CROSS_COMPILE=/opt/gcc-4.9/bin/arm-linux-gnueabihf-
export AS=${CROSS_COMPILE}as

KERNEL=$(HOME)/kernel

obj-m += main.o
main-objs:= ldd.o

all:
	$(AS) -o ldd.o ldd.S
	make -C $(KERNEL) M=$(PWD) modules

clean:
	make -C $(KERNEL) M=$(PWD) clean

編譯

$ make
    arm-linux-gnueabihf-as -o ldd.o ldd.S
    make -C kernel M=hello modules
    make[1]: Entering directory 'kernel'
        AS [M]  hello/ldd.o
        LD [M]  hello/main.o
        Building modules, stage 2.
        MODPOST 1 modules
        CC      hello/main.mod.o
        LD [M]  hello/main.ko
    make[1]: Leaving directory 'kernel'

接著把main.ko放到MicroSD第一分區,進入系統後,執行如下指令掛載驅動程式:

# insmod /boot/main.ko 
    hello, world!

卸載驅動程式:

# rmmod main
    unload it


返回上一頁