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"); }

转载于:https://www.cnblogs.com/Bshowmting/p/5907113.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值