Linux驱动读取本地文件信息


使用wifi模块时,由于芯片厂家把芯片的MAC地址已写入固定位置,在使用模块时避免有相同的MAC,
可以使用这种方法,在wifi模块动态加载时,从本地读取配置好的MAC。

#include <linux/module.h> // Needed by all modules #include <linux/kernel.h> // Needed for KERN_INFO #include <linux/fs.h> // Needed by filp #include <asm/uaccess.h> // Needed by segment descriptors int init_module(void) { // Create variables struct file *f; char buf[128]; mm_segment_t fs; int i; // Init the buffer with 0 for(i=0;i<128;i++) buf[i] = 0; // To see in /var/log/messages that the module is operating printk(KERN_INFO "My module is loaded\n"); // I am using Fedora and for the test I have chosen following file // Obviously it is much smaller than the 128 bytes, but hell with it =) f = filp_open("/etc/fedora-release", O_RDONLY, 0); if(f == NULL) printk(KERN_ALERT "filp_open error!!.\n"); else{ // Get current segment descriptor fs = get_fs(); // Set segment descriptor associated to kernel space set_fs(get_ds()); // Read the file f->f_op->read(f, buf, 128, &f->f_pos); // Restore segment descriptor set_fs(fs); // See what we read from file printk(KERN_INFO "buf:%s\n",buf); } filp_close(f,NULL); return 0; } void cleanup_module(void) { printk(KERN_INFO "My module is unloaded\n"); }
posted @ 2016-09-25 21:46  Bshowmting  阅读(1393)  评论(0编辑  收藏  举报