mips的交叉编译驱动

Cross compiling driver for mips

我正在尝试为 mips 设备编译一个简单的驱动程序示例。驱动真的只是一个测试:

#include <linux/init.h>
#include <linux/module.h>
MODULE_LICENSE("Dual BSD/GPL");

int ath_pci_init(void)
{
    printk(KERN_ALERT "alive and hacked\n");
    return 0;
}

void ath_pci_exit(void)
{
    printk(KERN_ALERT "dead and hacked\n");
}

module_init (ath_pci_init);
module_exit (ath_pci_exit);

并使用此 Makefile 进行编译:

ifneq ($(KERNELRELEASE),)
    obj-m := driver.o
else
    KERNELDIR ?= /lib/modules/3.2.0-81-generic/build
    PWD := $(shell pwd)
default:
    $(MAKE) -C $(KERNELDIR) M=$(PWD) modules
endif

x86 完美运行。对于 mips,我尝试将 Makefile 更改为:

ifneq ($(KERNELRELEASE),)
    obj-m := driver.o
else
    KERNELDIR ?= /lib/modules/3.2.0-81-generic/build
    PWD := $(shell pwd)
default:
    $(MAKE) -C $(KERNELDIR) ARCH=mips CROSS_COMPILE=~/Downloads/openwrt2/openwrt/staging_dir/toolchain-mips_34kc_gcc-4.8-linaro_uClibc-0.9.33.2/bin/mips-openwrt-linux-uclibc- M=$(PWD) modules

endif

但它不起作用。 在暗示之后我看到我使用了第一个 Makefile 但我 运行 make default 我 运行:

make ARCH=mips CROSS_COMPILE=~/openwrt2/openwrt/staging_dir/toolchain-mips_34kc_gcc-4.8-linaro_uClibc-0.9.33.2/bin/mips-openwrt-linux-uclibc- default

在这两种情况下,我得到的是:

make -C /lib/modules/3.2.0-81-generic/build M=<folder> modules
make[1]: Entering directory `/usr/src/linux-headers-3.2.0-81-generic'
/usr/src/linux-headers-3.2.0-81-generic/arch/mips/Kbuild.platforms:35: /usr/src/linux-headers-3.2.0-81-generic/arch/mips/alchemy/Platform: No such file or directory
/usr/src/linux-headers-3.2.0-81-generic/arch/mips/Kbuild.platforms:35: /usr/src/linux-headers-3.2.0-81-generic/arch/mips/ar7/Platform: No such file or directory
/usr/src/linux-headers-3.2.0-81-generic/arch/mips/Kbuild.platforms:35: /usr/src/linux-headers-3.2.0-81-generic/arch/mips/ath79/Platform: No such file or directory
/usr/src/linux-headers-3.2.0-81-generic/arch/mips/Kbuild.platforms:35: /usr/src/linux-headers-3.2.0-81-generic/arch/mips/bcm47xx/Platform: No such file or directory
/usr/src/linux-headers-3.2.0-81-generic/arch/mips/Kbuild.platforms:35: /usr/src/linux-headers-3.2.0-81-generic/arch/mips/bcm63xx/Platform: No such file or directory
/usr/src/linux-headers-3.2.0-81-generic/arch/mips/Kbuild.platforms:35: /usr/src/linux-headers-3.2.0-81-generic/arch/mips/cavium-octeon/Platform: No such file or directory
/usr/src/linux-headers-3.2.0-81-generic/arch/mips/Kbuild.platforms:35: /usr/src/linux-headers-3.2.0-81-generic/arch/mips/cobalt/Platform: No such file or directory
/usr/src/linux-headers-3.2.0-81-generic/arch/mips/Kbuild.platforms:35: /usr/src/linux-headers-3.2.0-81-generic/arch/mips/dec/Platform: No such file or directory
/usr/src/linux-headers-3.2.0-81-generic/arch/mips/Kbuild.platforms:35: /usr/src/linux-headers-3.2.0-81-generic/arch/mips/emma/Platform: No such file or directory
/usr/src/linux-headers-3.2.0-81-generic/arch/mips/Kbuild.platforms:35: /usr/src/linux-headers-3.2.0-81-generic/arch/mips/jazz/Platform: No such file or directory
/usr/src/linux-headers-3.2.0-81-generic/arch/mips/Kbuild.platforms:35: /usr/src/linux-headers-3.2.0-81-generic/arch/mips/jz4740/Platform: No such file or directory
/usr/src/linux-headers-3.2.0-81-generic/arch/mips/Kbuild.platforms:35: /usr/src/linux-headers-3.2.0-81-generic/arch/mips/lantiq/Platform: No such file or directory
/usr/src/linux-headers-3.2.0-81-generic/arch/mips/Kbuild.platforms:35: /usr/src/linux-headers-3.2.0-81-generic/arch/mips/lasat/Platform: No such file or directory
/usr/src/linux-headers-3.2.0-81-generic/arch/mips/Kbuild.platforms:35: /usr/src/linux-headers-3.2.0-81-generic/arch/mips/loongson/Platform: No such file or directory
/usr/src/linux-headers-3.2.0-81-generic/arch/mips/Kbuild.platforms:35: /usr/src/linux-headers-3.2.0-81-generic/arch/mips/mipssim/Platform: No such file or directory
/usr/src/linux-headers-3.2.0-81-generic/arch/mips/Kbuild.platforms:35: /usr/src/linux-headers-3.2.0-81-generic/arch/mips/mti-malta/Platform: No such file or directory
/usr/src/linux-headers-3.2.0-81-generic/arch/mips/Kbuild.platforms:35: /usr/src/linux-headers-3.2.0-81-generic/arch/mips/netlogic/Platform: No such file or directory
/usr/src/linux-headers-3.2.0-81-generic/arch/mips/Kbuild.platforms:35: /usr/src/linux-headers-3.2.0-81-generic/arch/mips/pmc-sierra/Platform: No such file or directory
/usr/src/linux-headers-3.2.0-81-generic/arch/mips/Kbuild.platforms:35: /usr/src/linux-headers-3.2.0-81-generic/arch/mips/pnx833x/Platform: No such file or directory
/usr/src/linux-headers-3.2.0-81-generic/arch/mips/Kbuild.platforms:35: /usr/src/linux-headers-3.2.0-81-generic/arch/mips/pnx8550/Platform: No such file or directory
/usr/src/linux-headers-3.2.0-81-generic/arch/mips/Kbuild.platforms:35: /usr/src/linux-headers-3.2.0-81-generic/arch/mips/powertv/Platform: No such file or directory
/usr/src/linux-headers-3.2.0-81-generic/arch/mips/Kbuild.platforms:35: /usr/src/linux-headers-3.2.0-81-generic/arch/mips/rb532/Platform: No such file or directory
/usr/src/linux-headers-3.2.0-81-generic/arch/mips/Kbuild.platforms:35: /usr/src/linux-headers-3.2.0-81-generic/arch/mips/sgi-ip22/Platform: No such file or directory
/usr/src/linux-headers-3.2.0-81-generic/arch/mips/Kbuild.platforms:35: /usr/src/linux-headers-3.2.0-81-generic/arch/mips/sgi-ip27/Platform: No such file or directory
/usr/src/linux-headers-3.2.0-81-generic/arch/mips/Kbuild.platforms:35: /usr/src/linux-headers-3.2.0-81-generic/arch/mips/sgi-ip32/Platform: No such file or directory
/usr/src/linux-headers-3.2.0-81-generic/arch/mips/Kbuild.platforms:35: /usr/src/linux-headers-3.2.0-81-generic/arch/mips/sibyte/Platform: No such file or directory
/usr/src/linux-headers-3.2.0-81-generic/arch/mips/Kbuild.platforms:35: /usr/src/linux-headers-3.2.0-81-generic/arch/mips/sni/Platform: No such file or directory
/usr/src/linux-headers-3.2.0-81-generic/arch/mips/Kbuild.platforms:35: /usr/src/linux-headers-3.2.0-81-generic/arch/mips/txx9/Platform: No such file or directory
/usr/src/linux-headers-3.2.0-81-generic/arch/mips/Kbuild.platforms:35: /usr/src/linux-headers-3.2.0-81-generic/arch/mips/vr41xx/Platform: No such file or directory
/usr/src/linux-headers-3.2.0-81-generic/arch/mips/Kbuild.platforms:35: /usr/src/linux-headers-3.2.0-81-generic/arch/mips/wrppmc/Platform: No such file or directory
expr: syntax error
make[1]: *** No rule to make target `/usr/src/linux-headers-3.2.0-81-generic/arch/mips/wrppmc/Platform'.  Stop.
make[1]: Leaving directory `/usr/src/linux-headers-3.2.0-81-generic'
make: *** [default] Error 2

我猜测通过将 CC 重定向到工具链,我必须为其提供与本机编译器相同的资源(或等效资源)。但是,我不知道该怎么做。

如何编译我的驱动程序?

提前致谢

我发现要交叉编译一个驱动你还需要一个交叉编译好的内核,以及一个有效的内核配置文件。

编译驱动需要先下载内核,然后运行:

make ARCH=mips CROSS_COMPILE=mips-openwrt-linux-uclibc- oldconfig
make ARCH=mips CROSS_COMPILE=mips-openwrt-linux-uclibc- prepare

然后更新 makefile 以使用此内核,例如:

KERNELDIR ?= <path to kernel>/kernel/3.2.0-81-generic/