{"id":334,"date":"2023-04-17T16:10:19","date_gmt":"2023-04-17T08:10:19","guid":{"rendered":"https:\/\/yajtech.synology.me\/wordpress\/?p=334"},"modified":"2023-04-17T16:10:23","modified_gmt":"2023-04-17T08:10:23","slug":"openwrt-%e6%96%b0%e5%a2%9e%e8%87%aa%e5%ae%9akernel-modules","status":"publish","type":"post","link":"https:\/\/www.yaj.com.tw\/wordpress\/?p=334","title":{"rendered":"OpenWRT \u65b0\u589e\u81ea\u5b9aKernel Modules"},"content":{"rendered":"\n<p>\u7a0b\u5f0f\u78bc\u9700\u8981\u653e\u7f6e\u65bc\u4e0b\u5217\u76ee\u9304\uff0c\u53ca\u9700\u8981\u4e0b\u5217\u6a94\u6848<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package\/kernel\/helloworld\/\r\n\u251c\u2500\u2500 Makefile\r\n\u2514\u2500\u2500 src\r\n    \u251c\u2500\u2500 helloworld.c\r\n    \u2514\u2500\u2500 Makefile\r\n<\/code><\/pre>\n\n\n\n<p>\u7b2c\u4e00\u5c64\u7684 Makefile\u5167\u5bb9<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>include $(TOPDIR)\/rules.mk\r\ninclude $(INCLUDE_DIR)\/kernel.mk\r\n\r\nPKG_NAME:=helloworld\r\nPKG_RELEASE:=1.0\r\n\r\ninclude $(INCLUDE_DIR)\/package.mk\r\n\r\ndefine KernelPackage\/helloworld\r\n  SUBMENU:=Other modules\r\n  TITLE:=helloworld\r\n  FILES:=$(PKG_BUILD_DIR)\/helloworld.ko\r\nendef\r\n\r\ndefine Build\/Prepare\r\n        mkdir -p $(PKG_BUILD_DIR)\r\n        $(CP) .\/src\/* $(PKG_BUILD_DIR)\/\r\nendef\r\n\r\ndefine Build\/Compile\r\n        $(MAKE) -C \"$(LINUX_DIR)\" V=1 \\\r\n        CROSS_COMPILE=\"$(TARGET_CROSS)\" \\\r\n        ARCH=\"$(LINUX_KARCH)\" \\\r\n        SUBDIRS=\"$(PKG_BUILD_DIR)\" \\\r\n        modules\r\nendef\r\n\r\n$(eval $(call KernelPackage,helloworld))<\/code><\/pre>\n\n\n\n<p> helloworld.c \u7684\u7bc4\u4f8b\u7a0b\u5f0f<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;linux\/init.h>\r\n#include &lt;linux\/module.h>\r\nMODULE_LICENSE(\"Dual BSD\/GPL\");\r\n\r\nstatic int hello_init(void)\r\n{\r\n    printk(KERN_ALERT \"Hello world\\n\");\r\n    return 0;\r\n}\r\n\r\nstatic void hello_exit(void)\r\n{\r\n    printk(KERN_ALERT \"Goodbye, cruel world\\n\");\r\n}\r\n\r\nmodule_init(hello_init);\r\nmodule_exit(hello_exit);<\/code><\/pre>\n\n\n\n<p>src\/Makefile \u5167\u5bb9\u70ba<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>obj-m += helloworld.o<\/code><\/pre>\n\n\n\n<p>make menuconfig \u7684\u9078\u64c7\uff0c\u5c07\u5176\u9078\u64c7modules<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Kernel modules  --->\r\n    Other modules  --->\r\n        &lt; > kmod-helloworld...................................... helloworld \r\n<\/code><\/pre>\n\n\n\n<p>\u53ef\u4ee5\u900f\u904e\u4e0b\u5217\u6307\u4ee4\u7de8\u8b6f<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>make package\/kernel\/helloworld\/compile V=s<\/code><\/pre>\n\n\n\n<p>\u7576\u5b89\u88dd\u9032\u5165\u7cfb\u7d71\u5f8c\uff0c\u53ef\u4ee5\u900f\u904e\u4e0b\u5217\u65b9\u5f0f\u555f\u7528\u6a21\u7d44<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>insmod helloworld.ko \nlsmod \nrmmod helloworld<\/code><\/pre>\n\n\n\n<p>\u53c3\u8003\u8cc7\u6599<\/p>\n\n\n\n<p><a href=\"https:\/\/blog.csdn.net\/stone8761\/article\/details\/87641925\">https:\/\/blog.csdn.net\/stone8761\/article\/details\/87641925<\/a><\/p>\n\n\n\n<p><a href=\"https:\/\/b31jsc.github.io\/2018\/07\/04\/OpenWRT%E4%B8%AD%E6%B7%BB%E5%8A%A0%E9%A9%B1%E5%8A%A8%E6%A8%A1%E5%9D%97\/\">https:\/\/b31jsc.github.io\/2018\/07\/04\/OpenWRT%E4%B8%AD%E6%B7%BB%E5%8A%A0%E9%A9%B1%E5%8A%A8%E6%A8%A1%E5%9D%97\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u7a0b\u5f0f\u78bc\u9700\u8981\u653e\u7f6e\u65bc\u4e0b\u5217\u76ee\u9304\uff0c\u53ca\u9700\u8981\u4e0b\u5217\u6a94\u6848 \u7b2c\u4e00\u5c64\u7684 Makefile\u5167\u5bb9 helloworld.c \u7684\u7bc4\u4f8b\u7a0b\u5f0f &#8230; <a title=\"OpenWRT \u65b0\u589e\u81ea\u5b9aKernel Modules\" class=\"read-more\" href=\"https:\/\/www.yaj.com.tw\/wordpress\/?p=334\" aria-label=\"\u95b1\u8b80\u3008OpenWRT \u65b0\u589e\u81ea\u5b9aKernel Modules\u3009\u5168\u6587\">\u95b1\u8b80\u5168\u6587<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[9],"class_list":["post-334","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-openwrt"],"_links":{"self":[{"href":"https:\/\/www.yaj.com.tw\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/334","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.yaj.com.tw\/wordpress\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.yaj.com.tw\/wordpress\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.yaj.com.tw\/wordpress\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.yaj.com.tw\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=334"}],"version-history":[{"count":4,"href":"https:\/\/www.yaj.com.tw\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/334\/revisions"}],"predecessor-version":[{"id":478,"href":"https:\/\/www.yaj.com.tw\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/334\/revisions\/478"}],"wp:attachment":[{"href":"https:\/\/www.yaj.com.tw\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=334"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.yaj.com.tw\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=334"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.yaj.com.tw\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=334"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}