码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • Linux内核读取spi-nor flash sn


    设备量产时,需要自动设置一个mac地址和sn,如果使用随机数生成的话,可能会有重复的,这里读取spi-nor的sn,参考sn来生成设备的mac和sn;

    添加如下部分:
    在这里插入图片描述
    代码如下:

    #include 
    static ssize_t unique_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
    {
    	struct spi_nor *nor = PDE_DATA(file_inode(file));
    	u8			unique[12];
    	u64         unique_id = 0;
    	int			i, tmp;
    
    	tmp = nor->read_reg(nor, 0x4b, unique, 12);
    	if (tmp < 0) {
    		dev_dbg(nor->dev, " error %d reading unique ID\n", tmp);
    		return 0;
    	}
    
    	for (i=4; i<11; i++) {
    		unique_id |= unique[i];
    		unique_id = unique_id << 8;
    	}
    	unique_id |= unique[i];
    
    	return sprintf(buf, "%llx\n", unique_id);
    }
    
    static const struct file_operations spi_nor_proc_fops = {
    	.read   = unique_read,
    	.llseek = default_llseek,
    };
    
    
    static const struct spi_device_id *spi_nor_read_id(struct spi_nor *nor)
    {
    	int			tmp;
    	u8			id[5];
    	u32			jedec;
    	u16                     ext_jedec;
    	struct flash_info	*info;
    
    	//调用
    	proc_create_data("unique_id", 0666, NULL, &spi_nor_proc_fops, nor);
    
    
    	tmp = nor->read_reg(nor, SPINOR_OP_RDID, id, 5);
    	if (tmp < 0) {
    		dev_dbg(nor->dev, " error %d reading JEDEC ID\n", tmp);
    		return ERR_PTR(tmp);
    	}
    	jedec = id[0];
    	jedec = jedec << 8;
    	jedec |= id[1];
    	jedec = jedec << 8;
    	jedec |= id[2];
    
    	ext_jedec = id[3] << 8 | id[4];
    
    	for (tmp = 0; tmp < ARRAY_SIZE(spi_nor_ids) - 1; tmp++) {
    		info = (void *)spi_nor_ids[tmp].driver_data;
    		if (info->jedec_id == jedec) {
    			if (info->ext_id == 0 || info->ext_id == ext_jedec)
    				return &spi_nor_ids[tmp];
    		}
    	}
    	dev_err(nor->dev, "unrecognized JEDEC id %06x\n", jedec);
    	return ERR_PTR(-ENODEV);
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65

    然后应用层读取/proc/unique_id即可,应用层读取示例:

    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    
    #include 
    #include 
    
    using namespace std;
    
    int main(void)
    {
        int fd;
        
        char  uid[32] = {0};
        int size;
    
    	fd = open("/proc/unique_id", O_RDONLY);
    	if (fd < 0)
    		return 1;
    
    	size = read(fd, uid, sizeof(uid));
    	close(fd);
    	
    	uid[strlen(uid)-1] = '\0';
    	
    	//转为大写
    	std::string str = uid;
    	std::transform(str.begin(), str.end(),str.begin(), ::toupper);
    	printf("%s", str.c_str());
    
    	return 0;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
  • 相关阅读:
    华为云云耀云服务器L实例评测|在Redis的Docker容器中安装BloomFilter & 在Spring中使用Redis插件版的布隆过滤器
    一文掌握深度学习实战——预测篇
    浅析-ES6
    巧用 redis 实现点赞功能,它不比 mysql 香吗?
    打造千万级流量秒杀第二十九课 预热和压测:SLB 预热和压测的意义及方法
    微信小程序 -- 联系客服(小程序客服)
    关于 WebSocket 和 HTTP 区别的思考以及一个最简单的 WebSocket 的客户端和服务器实现
    phpstudy设置允许远程访问mysql数据库(linux,pc都适用)
    asm 单实例集群不随系统启动而自动开启
    【C++】C++职工信息管理系统
  • 原文地址:https://blog.csdn.net/wuquan_1230/article/details/126262133
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号