在应用程序中操作NorFlash
相对于操作NandFlash,操作NorFlash相对简单,因为基本不需要考虑坏块,NorFlash也没有OOB区域,也跟ECC没有一毛钱关系。它的读写擦除相对容易。
- int dealwithnor()
- {
- // glob_t mtdbuf;
- struct mtd_info_user mtd;
- struct erase_info_user erase;
- int blocks = ;
- int i = ; //用于控制擦除的块的个数
- int k = ;
- int written = ; //已写入的字节数,只初始化一次
- unsigned int size = StateOfImage.st_size; //应该是镜像的实际大小,因为内存中大于镜像的空间的内容不可预知
- unsigned int result = ;
- unsigned int DevNum = ; //设备的数量
- unsigned int StartDev = ; //从第startDev开始擦除
- char DevName[] = {};
- unsigned int AllSize = ;
- #define MAXPARTITIONS 40
- struct DeviceInfo
- {
- int fd;
- char dir[];
- uint32_t size; // Total size of the MTD
- uint32_t erasesize;
- }DevInfo[MAXPARTITIONS];//用来存储设备信息
- bzero(DevInfo, sizeof(struct DeviceInfo));
- /* 这是一种方法,但是有一个缺点,当mtd设备大于10个是,通过glob搜索出来的结果
- 会出问题,下面采用会采用第二种方法 */
- // if(searchmtd(&mtdbuf) != 0){
- // DEBUG("Sorry! Can not find mtd device\n");
- // return 1; //返回大于零的数,表示升级失败
- // }
- // else
- // {
- // int fd;
- //
- // DEBUG("find %d mtd devices \n",mtdbuf.gl_pathc);
- // DevNum = mtdbuf.gl_pathc;
- // for(i=0; i<DevNum; i++)
- // {
- // fd = safeOpen (mtdbuf.gl_pathv[i],O_SYNC | O_RDWR);
- // if(fd < 0)
- // {
- // printf("failt to open\n");
- // return 1;
- // }
- // bzero(&mtd, sizeof(struct mtd_info_user));
- // if (ioctl(fd, MEMGETINFO,&mtd) < 0)
- // {
- // DEBUG("ioctl(): %m\n");
- // DEBUG("This doesn't seem to be a valid MTD flash device!\n");
- // return 1;
- // }
- //
- // strcpy(DevInfo[i].dir, mtdbuf.gl_pathv[i]);
- // DevInfo[i].fd = fd;
- // DevInfo[i].size = mtd.size;
- // DevInfo[i].erasesize = mtd.erasesize;
- // }
- // globfree(&mtdbuf);
- // for(i=0; i<DevNum; i++)
- // {
- // printf("\n\tinfo of %s\n",DevInfo[i].dir);
- // printf("%s.fd: %d\n",DevInfo[i].dir, DevInfo[i].fd);
- // printf("%s.size: %d\n",DevInfo[i].dir,DevInfo[i].size);
- // printf("%s.erasesize: %d\n",DevInfo[i].dir,DevInfo[i].erasesize);
- // }
- // }
- /* 下面是第二种方法,这种方法克服了第一种方法的缺陷,不受mtd设备数量的限制 */
- for(i=; i<MAXPARTITIONS; i++)
- {
- int fd;
- sprintf(DevName, "%s%d", "/dev/mtd",i);
- if((fd = open (DevName,O_SYNC | O_RDWR)) > )
- {
- bzero(&mtd, sizeof(struct mtd_info_user));
- if (ioctl(fd, MEMGETINFO,&mtd) < )
- {
- DEBUG("ioctl(): %m\n");
- DEBUG("This doesn't seem to be a valid MTD flash device!\n");
- return ;
- }
- strcpy(DevInfo[i].dir, DevName);
- DevInfo[i].fd = fd;
- DevInfo[i].size = mtd.size;
- DevInfo[i].erasesize = mtd.erasesize;
- }
- else
- {
- DevNum = i;
- break;
- }
- }
- for(i=; i<DevNum; i++)
- {
- printf("\n\tinfo of %s\n",DevInfo[i].dir);
- printf("%s.fd: %d\n",DevInfo[i].dir, DevInfo[i].fd);
- printf("%s.size: %d\n",DevInfo[i].dir,DevInfo[i].size);
- printf("%s.erasesize: %d\n",DevInfo[i].dir,DevInfo[i].erasesize);
- AllSize += DevInfo[i].size;
- }
- if(AllSize < StateOfImage.st_size)
- {
- DEBUG("ERROR!! all device size is less than ImageSize\n");
- return ;
- }
- for(i=StartDev; i<DevNum; i++)
- {
- /**
- * 先进行擦除操作
- */
- int j = ;
- g_AllImgSize = DevInfo[i].size;
- g_AllImgWrite = ;
- erase.start = ;
- blocks = DevInfo[i].size / mtd.erasesize; //计算要擦除的块的个数
- erase.length = mtd.erasesize;
- printf ("\nbegin to erase block %s\n", DevInfo[i].dir);
- for (j= ; j <= blocks; j++)
- {
- fprintf(stderr, "\rErasing blocks: %d/%d (%d%%)", j, blocks, (int)PERCENTAGE (j, blocks));
- g_percentage = * ((float)g_AllImgWrite / g_AllImgSize);
- if (ioctl(DevInfo[i].fd, MEMERASE, &erase) < )
- {
- DEBUG("\n");
- DEBUG("While erasing blocks 0x%.8x-0x%.8x on %s\n",\
- (unsigned int) erase.start, (unsigned int) (erase.start + erase.length), DevInfo[i].dir);
- /*return "Error while erasing blocks";*/
- return ;
- }
- g_AllImgWrite += erase.length;
- erase.start += mtd.erasesize;
- }
- printf("\n\rErased blocks: %d/%d (100%%)\n", blocks, blocks);
- /**
- * 再进行写操作
- */
- printf ("\nbegin to write block %s\n\n", DevInfo[i].dir);
- g_AllImgWrite = ;
- k = BUFSIZE;
- while (size)
- {
- if (size < BUFSIZE)
- {
- k = size;
- }
- printf("\033[1A");
- printf("\r%s usage: %dk/%dk (%d%%)\n",\
- DevInfo[i].dir, KB (g_AllImgWrite + k), KB (DevInfo[i].size), (int)PERCENTAGE (g_AllImgWrite + k, DevInfo[i].size));
- fprintf(stderr, "Writing data: %dk/%ldk (%d%%)", KB (written + k), KB (StateOfImage.st_size), (int)PERCENTAGE (written + k, StateOfImage.st_size));
- result = write(DevInfo[i].fd, &upPack[written], k);
- if (k != result)
- {
- DEBUG ("\n");
- if (result < )
- {
- DEBUG("While writing data to 0x%.8x-0x%.8x on %s\n", written, written + k, DevInfo[i].dir);
- return ;
- }
- DEBUG("Short write count returned while writing to x%.8x-0x%.8x on %s: %d/%d bytes written to flash\n", \
- written,written + k, DevInfo[i].dir, written + result, DevInfo[i].size);
- return ;
- }
- written += k;
- size -= k;
- g_AllImgWrite += k;
- if(g_AllImgWrite >= DevInfo[i].size)
- {
- g_AllImgWrite = ;
- printf("\n");
- break;
- }
- }
- printf("Wrote %d / %ldk bytes\n", written, (unsigned long int)(StateOfImage.st_size));
- }
- munmap(upPack, UPGRADE_SHM_SIZE);
- for(i=; i<DevNum; i++)
- {
- close (DevInfo[i].fd);
- printf("%s is closed!\n",DevInfo[i].dir);
- }
- return ;
- }
在应用程序中操作NorFlash的更多相关文章
- c#中操作word文档-四、对象模型
转自:http://blog.csdn.net/ruby97/article/details/7406806 Word对象模型 (.Net Perspective) 本文主要针对在Visual St ...
- 在应用程序中实现对NandFlash的操作
以TC58NVG2S3ETA00 为例: 下面是它的一些物理参数: 图一 图二 图三 图四 图五 图6-0 图6-1 说明一下,在图6-1中中间的那个布局表可以看做是实际的NandFlash一页数据的 ...
- 【转】《深入理解计算机系统》C程序中常见的内存操作有关的典型编程错误
原文地址:http://blog.csdn.net/slvher/article/details/9150597 对C/C++程序员来说,内存管理是个不小的挑战,绝对值得慎之又慎,否则让由上万行代码构 ...
- 《深入理解计算机系统》C程序中常见的内存操作有关的典型编程错误
对C/C++程序员来说,内存管理是个不小的挑战,绝对值得慎之又慎,否则让由上万行代码构成的模块跑起来后才出现内存崩溃,是很让人痛苦的.因为崩溃的位置在时间和空间上,通常是在距真正的错误源一段距离之后才 ...
- PHP程序中使用PDO对象实现对数据库的增删改查操作的示例代码
PHP程序中使用PDO对象实现对数据库的增删改查操作(PHP+smarty) dbconn.php <?php //------------------------使用PDO方式连接数据库文件- ...
- LINQ To SQL在N层应用程序中的CUD操作、批量删除、批量更新
原文:LINQ To SQL在N层应用程序中的CUD操作.批量删除.批量更新 0. 说明 Linq to Sql,以下简称L2S. 以下文中所指的两层和三层结构,分别如下图所示: 准确的说,这里 ...
- websocketj--随时随地在Web浏览器中操作你的服务端程序
0 - 有没有觉得Linux标准终端界面输入输出枯燥无味? 1 - 什么?vmstat命令的输出数据不直观?有没有想过能够可视化该命令的输出? 2 - 尝试过用浏览器操作Windows中的cmd吗? ...
- 在Python程序中的进程操作,multiprocess.Process模块
在python程序中的进程操作 之前我们已经了解了很多进程相关的理论知识,了解进程是什么应该不再困难了,刚刚我们已经了解了,运行中的程序就是一个进程.所有的进程都是通过它的父进程来创建的.因此,运行起 ...
- Python程序中的进程操作
之前我们已经了解了很多进程相关的理论知识,了解进程是什么应该不再困难了,刚刚我们已经了解了,运行中的程序就是一个进程.所有的进程都是通过它的父进程来创建的.因此,运行起来的python程序也是一个进程 ...
随机推荐
- NovaMind *的安装、和谐破解到永久使用
XMind *思维导图的安装步 同类型的软件,这两款软件: XMind 和 NovaMind,各有所长.建议,都安装,合适的时候方便使用. XMind界面如下: NovaMind界面如下: 本博文,主 ...
- jquery 回车切换 tab功能
挺有趣的,Jquery 回车切换tab功能的实现哦 <html> <head><!--jquery库.js--></head> <body> ...
- Axis2与Web项目整合
一.说明: 上一篇介绍了通过使用Axis2来发布和调用WebService,但是是把WebService发布在Axis2提供的项目中,如果我们需要在自己的Web项目中来使用Axis2发布WebServ ...
- SD卡中的命令CMD
SD卡中的命令是SD控制器和SD卡之间的桥梁,它封装了SD卡的实现细节,不影响SD卡中FLASH的读写变更. 命令的长度是48位,它的字段如图: SD校准定义的CMD如下:
- Codeforces 161 D. Distance in Tree (树dp)
题目链接:http://codeforces.com/problemset/problem/161/D 题意: 给你一棵树,问你有多少对点的距离为k. 思路: dp[i][j]表示离i节点距离为j的点 ...
- 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 ...
- 使用VS2013调试FluorineFx程序
VS2013,建立 FluorineFx Web 项目方法: 先新建.项目.Web.选择.NET 3.5 ASP.NET 窗体程序来新建一个项目.复制 log.Templates.WEB-INF 文件 ...
- [Sparrow OS 设计文档连载(一)] Introduction
- 字符串左移n位操作
void reverse(char* str, int begin, int end) { char temp; for( ; begin < end; begin++) { temp = st ...
- ASP.net 服务器监控
参考代码: 1,页面 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="SMP ...