Linux驱动中读写文件
来源:互联网 发布:java开发界面 编辑:程序博客网 时间:2024/09/21 06:18
最近做了一个TF卡烧写MAC地址,方法额外增加一个驱动,驱动中实现对TF卡和本地mac文件的读写功能,让以太网驱动加载时调用读写函数进行设置mac地址,代码比较简单,如下:
/* * setmac one chip USB 2.0 ethernet devices * * Author : yubo.wang* Date : 2017-08-25 * * This file is licensed under the terms of the GNU General Public License * version 2. This program is licensed "as is" without any warranty of any * kind, whether express or implied. */#include #include #include #include #include #include #include #include #include #include #include #include "setmac.h"#define SET_MAC_FILE "/system/etc/mac"#define SET_MAC_MOUNT "/system/etc/tf_mac/mac"int setmac_read_tf_mac(u8 *mac_addr){ u8 mac_buf[12] = {0,};int i = 0;struct file *fp; mm_segment_t fs; loff_t pos; fp = filp_open(SET_MAC_MOUNT, O_RDONLY, 0);if (IS_ERR(fp)) { printk(KERN_ERR "read tf mac addr file error,return!\n");return -1;} fs = get_fs();set_fs(KERNEL_DS);pos = 0; vfs_read(fp, mac_buf, sizeof(mac_buf), &pos);if(strlen(mac_buf) < 12){printk(KERN_ERR "read tf mac addr len < 12,return!\n"); return -1;}mac_buf[12] = '\0';printk(KERN_INFO "read tf mac:%s",mac_buf);for (i=0; i<6; i++){ u8 mac_tmp[3]= {0,};memcpy(mac_tmp, (char *)(mac_buf+i*2), 2);mac_addr[i] = simple_strtoul(mac_tmp, NULL, 16);}filp_close(fp, NULL); set_fs(fs); return 0; }EXPORT_SYMBOL_GPL(setmac_read_tf_mac);int setmac_write_tf_mac(u8 *mac_addr){ u8 mac_buf[12] = {0,};int i = 0;struct file *fp; mm_segment_t fs; loff_t pos; fp = filp_open(SET_MAC_FILE, O_RDWR | O_CREAT, 0644);if (IS_ERR(fp)) { printk(KERN_ERR "write tf mac addr file error,return!\n"); return -1;} fs = get_fs();set_fs(KERNEL_DS);for (i=0; i<6; i++){ u8 mac_tmp[3]= {0,};sprintf(mac_tmp, "%02x", mac_addr[i]);strncat(mac_buf, mac_tmp, 2);}mac_buf[12] = '\0';printk(KERN_INFO "write mac:%s",mac_buf);pos = 0;vfs_write(fp, mac_buf, sizeof(mac_buf), &pos);vfs_fsync(fp, 0);filp_close(fp, NULL);set_fs(fs);return 0;}EXPORT_SYMBOL_GPL(setmac_write_tf_mac);int setmac_read_mac_addr(u8 *mac_addr){ u8 mac_buf[12] = {0,};int i = 0;struct file *fp; mm_segment_t fs; loff_t pos; fp = filp_open(SET_MAC_FILE, O_RDONLY, 0);if (IS_ERR(fp)) { printk(KERN_ERR "read mac addr file error,return!\n");return -1;} fs = get_fs();set_fs(KERNEL_DS);pos = 0; vfs_read(fp, mac_buf, sizeof(mac_buf), &pos);if(strlen(mac_buf) < 12){printk(KERN_ERR "read mac addr len < 12,return!\n"); return -1;}mac_buf[12] = '\0';printk(KERN_INFO "read mac:%s",mac_buf);for (i=0; i<6; i++){ u8 mac_tmp[3]= {0,};memcpy(mac_tmp, (char *)(mac_buf+i*2), 2);mac_addr[i] = simple_strtoul(mac_tmp, NULL, 16);}filp_close(fp, NULL); set_fs(fs); return 0; }EXPORT_SYMBOL_GPL(setmac_read_mac_addr);int setmac_write_mac_addr(u8 *mac_addr) { u8 mac_buf[12] = {0,};int i = 0;struct file *fp; mm_segment_t fs; loff_t pos; fp = filp_open(SET_MAC_FILE, O_RDWR | O_CREAT, 0644);if (IS_ERR(fp)) { printk(KERN_ERR "write mac addr file error,return!\n");return -1;} fs = get_fs();set_fs(KERNEL_DS);for (i=0; i<6; i++){ u8 mac_tmp[3]= {0,};sprintf(mac_tmp, "%02x", mac_addr[i]);strncat(mac_buf, mac_tmp, 2);}mac_buf[12] = '\0';printk(KERN_INFO "write mac:%s",mac_buf);pos = 0;vfs_write(fp, mac_buf, sizeof(mac_buf), &pos);vfs_fsync(fp, 0);filp_close(fp, NULL);set_fs(fs);return 0;}EXPORT_SYMBOL_GPL(setmac_write_mac_addr);MODULE_AUTHOR("yubo.wang ");MODULE_DESCRIPTION("SETMAC one chip USB 2.0 ethernet devices");MODULE_LICENSE("GPL");
预留问题:
1、量产多个mac地址递增还未实现,发现ops文件指针偏移不准导致读数据异常
2、没有对mac地址有效性检查,只检查了位数,且单播多播和OUI也没有检查
阅读全文
0 0
- linux 驱动中读写文件
- Linux驱动中读写文件
- 在linux内核(驱动)中读写文件
- Linux驱动 读写文件
- 驱动中读写文件
- linux 内核驱动中对文件的读写
- Linux驱动中保存读写文件方法
- 五、 在驱动中读写文件
- Linux内核中读写文件
- linux内核中读写文件
- linux内核中读写文件
- linux内核中读写文件
- linux内核中读写文件
- linux内核中读写文件
- linux内核中读写文件
- linux内核中读写文件
- linux内核中读写文件
- Linux内核中读写文件
- JS每周读书笔记[2]--Object & Array
- 从0开始架构一个IOS程序—— 02 — 设置启动图标与启动页面
- easyUI动态加载时崩溃
- Unity17--鼠标点击物体跟着移动、分层点击移动、铰链(关节)、布料
- 在无序数组中找第k大的数字—滴滴笔试
- Linux驱动中读写文件
- Android Toolbar使用
- 2013腾讯编程马拉松初赛第五场(3月25日)
- IO-1、输入输出设备
- scala变量访问范围
- 从0开始架构一个IOS程序——03 — -分包用添加pch全局引用文件
- NIO初探
- 设置内核线程的调度策略
- 修改mysql数据库字符集为UTF8的