read_id.c 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /******************************************************************************
  2. 版权所有:
  3. 文件名称: read_id.c
  4. 文件版本: 01.01
  5. 创建作者: sunxi
  6. 创建日期: 2014-10-30
  7. 功能说明: 读取flash id
  8. 其它说明:
  9. 修改记录:
  10. */
  11. #include "head.h"
  12. #ifdef LINUX_KERNEL_APP
  13. #include <linux/kernel.h>
  14. #include <linux/init.h>
  15. #include <linux/module.h>
  16. #include <linux/moduleparam.h>
  17. #include <linux/fs.h>
  18. #include <linux/sched.h>
  19. #include <linux/interrupt.h>
  20. #include <linux/kthread.h>
  21. #include <linux/delay.h>
  22. #include <linux/wait.h>
  23. #include <asm/uaccess.h>
  24. #include <asm/io.h>
  25. #include <asm/ioctls.h>
  26. #include <termios.h>
  27. #include <linux/syscalls.h>
  28. #else
  29. #include <linux/fs.h>
  30. #include <linux/sched.h>
  31. #include <linux/wait.h>
  32. #include <asm/ioctls.h>
  33. #include <termios.h>
  34. #include "rt.h"
  35. #endif
  36. #define PROC_FLASHID "/proc/flashid"
  37. /******************************************************************************
  38. 函数名称: read_unique_id
  39. 函数版本: 01.01
  40. 创建作者: sunxi
  41. 创建日期: 2014-10-30
  42. 函数说明: 读取ID。
  43. 参数说明: id: 8字节的ID.
  44. 返回值: 成功返回0,失败返回负数。。
  45. 修改记录:
  46. */
  47. int read_unique_id(unsigned char *id)
  48. {
  49. unsigned char buf[17];
  50. unsigned char tmpbuf[9];
  51. int fd = 0;
  52. int ret = 0;
  53. uint32_t tmp = 0;
  54. memset(buf, 0, sizeof(buf));
  55. memset(tmpbuf, 0, sizeof(tmpbuf));
  56. fd = sys_open(PROC_FLASHID, O_RDONLY, 0);
  57. if(fd < 0)
  58. {
  59. printk("open %s error!\n", PROC_FLASHID);
  60. return -1;
  61. }
  62. ret = sys_read(fd, buf, sizeof(buf) - 1);
  63. if(ret < 0)
  64. {
  65. printk("read_unique_id error!\n");
  66. ret = -1;
  67. goto end;
  68. }
  69. memcpy(tmpbuf, buf, 8);
  70. tmp = simple_strtoul(tmpbuf, NULL, 16);
  71. id[0] = (tmp >> 24) & 0xFF;
  72. id[1] = (tmp >> 16) & 0xFF;
  73. id[2] = (tmp >> 8) & 0xFF;
  74. id[3] = (tmp >> 0) & 0xFF;
  75. memcpy(tmpbuf, buf+8, 8);
  76. tmp = simple_strtoul(tmpbuf, NULL, 16);
  77. id[4] = (tmp >> 24) & 0xFF;
  78. id[5] = (tmp >> 16) & 0xFF;
  79. id[6] = (tmp >> 8) & 0xFF;
  80. id[7] = (tmp >> 0) & 0xFF;
  81. ret = 0;
  82. end:
  83. sys_close(fd);
  84. return ret;
  85. }