相对于操作NandFlash,操作NorFlash相对简单,因为基本不需要考虑坏块,NorFlash也没有OOB区域,也跟ECC没有一毛钱关系。它的读写擦除相对容易。

  1. int dealwithnor()
  2. {
  3.  
  4. // glob_t mtdbuf;
  5. struct mtd_info_user mtd;
  6. struct erase_info_user erase;
  7. int blocks = ;
  8. int i = ; //用于控制擦除的块的个数
  9. int k = ;
  10. int written = ; //已写入的字节数,只初始化一次
  11. unsigned int size = StateOfImage.st_size; //应该是镜像的实际大小,因为内存中大于镜像的空间的内容不可预知
  12. unsigned int result = ;
  13. unsigned int DevNum = ; //设备的数量
  14. unsigned int StartDev = ; //从第startDev开始擦除
  15. char DevName[] = {};
  16. unsigned int AllSize = ;
  17. #define MAXPARTITIONS 40
  18. struct DeviceInfo
  19. {
  20. int fd;
  21. char dir[];
  22. uint32_t size; // Total size of the MTD
  23. uint32_t erasesize;
  24.  
  25. }DevInfo[MAXPARTITIONS];//用来存储设备信息
  26.  
  27. bzero(DevInfo, sizeof(struct DeviceInfo));
  28.  
  29. /* 这是一种方法,但是有一个缺点,当mtd设备大于10个是,通过glob搜索出来的结果
  30. 会出问题,下面采用会采用第二种方法 */
  31. // if(searchmtd(&mtdbuf) != 0){
  32.  
  33. // DEBUG("Sorry! Can not find mtd device\n");
  34. // return 1; //返回大于零的数,表示升级失败
  35. // }
  36. // else
  37. // {
  38. // int fd;
  39. //
  40. // DEBUG("find %d mtd devices \n",mtdbuf.gl_pathc);
  41. // DevNum = mtdbuf.gl_pathc;
  42.  
  43. // for(i=0; i<DevNum; i++)
  44. // {
  45. // fd = safeOpen (mtdbuf.gl_pathv[i],O_SYNC | O_RDWR);
  46. // if(fd < 0)
  47. // {
  48. // printf("failt to open\n");
  49. // return 1;
  50. // }
  51. // bzero(&mtd, sizeof(struct mtd_info_user));
  52. // if (ioctl(fd, MEMGETINFO,&mtd) < 0)
  53. // {
  54. // DEBUG("ioctl(): %m\n");
  55. // DEBUG("This doesn't seem to be a valid MTD flash device!\n");
  56. // return 1;
  57. // }
  58. //
  59. // strcpy(DevInfo[i].dir, mtdbuf.gl_pathv[i]);
  60. // DevInfo[i].fd = fd;
  61. // DevInfo[i].size = mtd.size;
  62. // DevInfo[i].erasesize = mtd.erasesize;
  63. // }
  64.  
  65. // globfree(&mtdbuf);
  66. // for(i=0; i<DevNum; i++)
  67. // {
  68. // printf("\n\tinfo of %s\n",DevInfo[i].dir);
  69. // printf("%s.fd: %d\n",DevInfo[i].dir, DevInfo[i].fd);
  70. // printf("%s.size: %d\n",DevInfo[i].dir,DevInfo[i].size);
  71. // printf("%s.erasesize: %d\n",DevInfo[i].dir,DevInfo[i].erasesize);
  72. // }
  73. // }
  74.  
  75. /* 下面是第二种方法,这种方法克服了第一种方法的缺陷,不受mtd设备数量的限制 */
  76.  
  77. for(i=; i<MAXPARTITIONS; i++)
  78. {
  79. int fd;
  80. sprintf(DevName, "%s%d", "/dev/mtd",i);
  81.  
  82. if((fd = open (DevName,O_SYNC | O_RDWR)) > )
  83. {
  84. bzero(&mtd, sizeof(struct mtd_info_user));
  85. if (ioctl(fd, MEMGETINFO,&mtd) < )
  86. {
  87. DEBUG("ioctl(): %m\n");
  88. DEBUG("This doesn't seem to be a valid MTD flash device!\n");
  89. return ;
  90. }
  91. strcpy(DevInfo[i].dir, DevName);
  92. DevInfo[i].fd = fd;
  93. DevInfo[i].size = mtd.size;
  94. DevInfo[i].erasesize = mtd.erasesize;
  95. }
  96. else
  97. {
  98. DevNum = i;
  99. break;
  100. }
  101.  
  102. }
  103.  
  104. for(i=; i<DevNum; i++)
  105. {
  106. printf("\n\tinfo of %s\n",DevInfo[i].dir);
  107. printf("%s.fd: %d\n",DevInfo[i].dir, DevInfo[i].fd);
  108. printf("%s.size: %d\n",DevInfo[i].dir,DevInfo[i].size);
  109. printf("%s.erasesize: %d\n",DevInfo[i].dir,DevInfo[i].erasesize);
  110. AllSize += DevInfo[i].size;
  111. }
  112.  
  113. if(AllSize < StateOfImage.st_size)
  114. {
  115. DEBUG("ERROR!! all device size is less than ImageSize\n");
  116. return ;
  117. }
  118.  
  119. for(i=StartDev; i<DevNum; i++)
  120. {
  121.  
  122. /**
  123. * 先进行擦除操作
  124. */
  125. int j = ;
  126. g_AllImgSize = DevInfo[i].size;
  127. g_AllImgWrite = ;
  128. erase.start = ;
  129. blocks = DevInfo[i].size / mtd.erasesize; //计算要擦除的块的个数
  130. erase.length = mtd.erasesize;
  131. printf ("\nbegin to erase block %s\n", DevInfo[i].dir);
  132. for (j= ; j <= blocks; j++)
  133. {
  134. fprintf(stderr, "\rErasing blocks: %d/%d (%d%%)", j, blocks, (int)PERCENTAGE (j, blocks));
  135. g_percentage = * ((float)g_AllImgWrite / g_AllImgSize);
  136. if (ioctl(DevInfo[i].fd, MEMERASE, &erase) < )
  137. {
  138. DEBUG("\n");
  139. DEBUG("While erasing blocks 0x%.8x-0x%.8x on %s\n",\
  140. (unsigned int) erase.start, (unsigned int) (erase.start + erase.length), DevInfo[i].dir);
  141. /*return "Error while erasing blocks";*/
  142. return ;
  143. }
  144. g_AllImgWrite += erase.length;
  145. erase.start += mtd.erasesize;
  146. }
  147. printf("\n\rErased blocks: %d/%d (100%%)\n", blocks, blocks);
  148.  
  149. /**
  150. * 再进行写操作
  151. */
  152. printf ("\nbegin to write block %s\n\n", DevInfo[i].dir);
  153. g_AllImgWrite = ;
  154. k = BUFSIZE;
  155. while (size)
  156. {
  157. if (size < BUFSIZE)
  158. {
  159. k = size;
  160. }
  161. printf("\033[1A");
  162. printf("\r%s usage: %dk/%dk (%d%%)\n",\
  163. DevInfo[i].dir, KB (g_AllImgWrite + k), KB (DevInfo[i].size), (int)PERCENTAGE (g_AllImgWrite + k, DevInfo[i].size));
  164. fprintf(stderr, "Writing data: %dk/%ldk (%d%%)", KB (written + k), KB (StateOfImage.st_size), (int)PERCENTAGE (written + k, StateOfImage.st_size));
  165. result = write(DevInfo[i].fd, &upPack[written], k);
  166. if (k != result)
  167. {
  168. DEBUG ("\n");
  169. if (result < )
  170. {
  171. DEBUG("While writing data to 0x%.8x-0x%.8x on %s\n", written, written + k, DevInfo[i].dir);
  172. return ;
  173. }
  174. DEBUG("Short write count returned while writing to x%.8x-0x%.8x on %s: %d/%d bytes written to flash\n", \
  175. written,written + k, DevInfo[i].dir, written + result, DevInfo[i].size);
  176. return ;
  177. }
  178.  
  179. written += k;
  180. size -= k;
  181.  
  182. g_AllImgWrite += k;
  183. if(g_AllImgWrite >= DevInfo[i].size)
  184. {
  185. g_AllImgWrite = ;
  186. printf("\n");
  187. break;
  188. }
  189.  
  190. }
  191. printf("Wrote %d / %ldk bytes\n", written, (unsigned long int)(StateOfImage.st_size));
  192.  
  193. }
  194.  
  195. munmap(upPack, UPGRADE_SHM_SIZE);
  196. for(i=; i<DevNum; i++)
  197. {
  198. close (DevInfo[i].fd);
  199. printf("%s is closed!\n",DevInfo[i].dir);
  200. }
  201.  
  202. return ;
  203. }

在应用程序中操作NorFlash的更多相关文章

  1. c#中操作word文档-四、对象模型

    转自:http://blog.csdn.net/ruby97/article/details/7406806 Word对象模型  (.Net Perspective) 本文主要针对在Visual St ...

  2. 在应用程序中实现对NandFlash的操作

    以TC58NVG2S3ETA00 为例: 下面是它的一些物理参数: 图一 图二 图三 图四 图五 图6-0 图6-1 说明一下,在图6-1中中间的那个布局表可以看做是实际的NandFlash一页数据的 ...

  3. 【转】《深入理解计算机系统》C程序中常见的内存操作有关的典型编程错误

    原文地址:http://blog.csdn.net/slvher/article/details/9150597 对C/C++程序员来说,内存管理是个不小的挑战,绝对值得慎之又慎,否则让由上万行代码构 ...

  4. 《深入理解计算机系统》C程序中常见的内存操作有关的典型编程错误

    对C/C++程序员来说,内存管理是个不小的挑战,绝对值得慎之又慎,否则让由上万行代码构成的模块跑起来后才出现内存崩溃,是很让人痛苦的.因为崩溃的位置在时间和空间上,通常是在距真正的错误源一段距离之后才 ...

  5. PHP程序中使用PDO对象实现对数据库的增删改查操作的示例代码

    PHP程序中使用PDO对象实现对数据库的增删改查操作(PHP+smarty) dbconn.php <?php //------------------------使用PDO方式连接数据库文件- ...

  6. LINQ To SQL在N层应用程序中的CUD操作、批量删除、批量更新

    原文:LINQ To SQL在N层应用程序中的CUD操作.批量删除.批量更新 0. 说明 Linq to Sql,以下简称L2S.    以下文中所指的两层和三层结构,分别如下图所示: 准确的说,这里 ...

  7. websocketj--随时随地在Web浏览器中操作你的服务端程序

    0 - 有没有觉得Linux标准终端界面输入输出枯燥无味? 1 - 什么?vmstat命令的输出数据不直观?有没有想过能够可视化该命令的输出? 2 - 尝试过用浏览器操作Windows中的cmd吗? ...

  8. 在Python程序中的进程操作,multiprocess.Process模块

    在python程序中的进程操作 之前我们已经了解了很多进程相关的理论知识,了解进程是什么应该不再困难了,刚刚我们已经了解了,运行中的程序就是一个进程.所有的进程都是通过它的父进程来创建的.因此,运行起 ...

  9. Python程序中的进程操作

    之前我们已经了解了很多进程相关的理论知识,了解进程是什么应该不再困难了,刚刚我们已经了解了,运行中的程序就是一个进程.所有的进程都是通过它的父进程来创建的.因此,运行起来的python程序也是一个进程 ...

随机推荐

  1. NovaMind *的安装、和谐破解到永久使用

    XMind *思维导图的安装步 同类型的软件,这两款软件: XMind 和 NovaMind,各有所长.建议,都安装,合适的时候方便使用. XMind界面如下: NovaMind界面如下: 本博文,主 ...

  2. jquery 回车切换 tab功能

    挺有趣的,Jquery 回车切换tab功能的实现哦 <html> <head><!--jquery库.js--></head> <body> ...

  3. Axis2与Web项目整合

    一.说明: 上一篇介绍了通过使用Axis2来发布和调用WebService,但是是把WebService发布在Axis2提供的项目中,如果我们需要在自己的Web项目中来使用Axis2发布WebServ ...

  4. SD卡中的命令CMD

    SD卡中的命令是SD控制器和SD卡之间的桥梁,它封装了SD卡的实现细节,不影响SD卡中FLASH的读写变更. 命令的长度是48位,它的字段如图: SD校准定义的CMD如下:

  5. Codeforces 161 D. Distance in Tree (树dp)

    题目链接:http://codeforces.com/problemset/problem/161/D 题意: 给你一棵树,问你有多少对点的距离为k. 思路: dp[i][j]表示离i节点距离为j的点 ...

  6. JavaServer Faces 2.2 requires Dynamic Web Module 2.5 or newer

    Description Resource Path Location Type JavaServer Faces 2.2 can not be installed : One or more cons ...

  7. 使用VS2013调试FluorineFx程序

    VS2013,建立 FluorineFx Web 项目方法: 先新建.项目.Web.选择.NET 3.5 ASP.NET 窗体程序来新建一个项目.复制 log.Templates.WEB-INF 文件 ...

  8. [Sparrow OS 设计文档连载(一)] Introduction

  9. 字符串左移n位操作

    void reverse(char* str, int begin, int end) { char temp; for( ; begin < end; begin++) { temp = st ...

  10. ASP.net 服务器监控

    参考代码: 1,页面 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="SMP ...