wenxy 发表于 2009-04-20 10:04

关于直接编写应用程序读写/dev/mtd0 或者 /dev/mtdblock0的问题。

关于直接编写应用程序读写/dev/mtd0 或者 /dev/mtdblock0的问题。

开发板的Linux环境:
# uname -a
Linux 192.168.102.213 2.6.12-4.2-brcmstb #73 Tue Apr 14 16:06:50 CST 2009 7403a0 unknown

我的开发板的Nor flash分区情况是
# cat /proc/mtd
dev:    size   erasesizename
mtd0: 01800000 00020000 "rootfs"
mtd1: 00200000 00020000 "vmlinux"
mtd2: 00400000 00020000 "config"
mtd3: 00080000 00020000 "splash"
mtd4: 00080000 00020000 "cfe"
mtd5: 00000080 00020000 "mactype"
mtd6: 00040000 00020000 "nvram"
mtd7: 00040000 00020000 "feature"

# ls /dev/mtd* -al
crwxr-xr-x    1 nobody   nobody    90,   0 Mar22009 /dev/mtd0
crwxr-xr-x    1 nobody   nobody    90,   2 Mar22009 /dev/mtd1
crwxr-xr-x    1 nobody   nobody    90,   4 Mar22009 /dev/mtd2
crwxr-xr-x    1 nobody   nobody    90,   6 Mar22009 /dev/mtd3
crwxr-xr-x    1 nobody   nobody    90,   8 Mar22009 /dev/mtd4
crwxr-xr-x    1 nobody   nobody    90,10 Mar22009 /dev/mtd5
crwxr-xr-x    1 nobody   nobody    90,12 Mar22009 /dev/mtd6
-rw-r--r--    1 root   root         19 Apr 152009 /dev/mtd9
brwxr-xr-x    1 nobody   nobody    31,   0 Mar22009 /dev/mtdblock0
brwxr-xr-x    1 nobody   nobody    31,   1 Mar22009 /dev/mtdblock1
brwxr-xr-x    1 nobody   nobody    31,   2 Mar22009 /dev/mtdblock2
brwxr-xr-x    1 nobody   nobody    31,   3 Mar22009 /dev/mtdblock3
brwxr-xr-x    1 nobody   nobody    31,   4 Mar22009 /dev/mtdblock4
brwxr-xr-x    1 nobody   nobody    31,   5 Mar22009 /dev/mtdblock5
brwxr-xr-x    1 nobody   nobody    31,   6 Mar22009 /dev/mtdblock6
crwxr-xr-x    1 nobody   nobody    90,   1 Mar22009 /dev/mtdr0
crwxr-xr-x    1 nobody   nobody    90,   3 Mar22009 /dev/mtdr1
crwxr-xr-x    1 nobody   nobody    90,   5 Mar22009 /dev/mtdr2
crwxr-xr-x    1 nobody   nobody    90,   7 Mar22009 /dev/mtdr3
crwxr-xr-x    1 nobody   nobody    90,   9 Mar22009 /dev/mtdr4
crwxr-xr-x    1 nobody   nobody    90,11 Mar22009 /dev/mtdr5
crwxr-xr-x    1 nobody   nobody    90,13 Mar22009 /dev/mtdr6

通过NFS启动系统后,执行应用程序能读出 /dev/mtd6里面的数据!
但是我要写入数据,在write()之前,要调用get_mtdinfo()获取mtd设备的一些参数(例如,分区大小,擦除块大小等)后再擦除。
int get_mtdinfo(int fd)
{
        struct mtd_info_user mtdInfo;       
        struct erase_info_user mtdEraseInfo;
        int num;

        memset(&mtdInfo, 0, sizeof(struct mtd_info_user));
        num = ioctl(fd, MEMGETINFO, &mtdInfo);

        if(num < 0)
        {               
                fprintf(stderr, "Error: could not get MTD device info from MTD device, \n", errno);               
                return -1;               
        }       
        printf("mtdInfo: type=%d, flags=%d, size=%d, erasesize=%d\n", \
                        mtdInfo.type, mtdInfo.flags, mtdInfo.size, mtdInfo.erasesize);
        return 0;
}               

fd = open("/dev/mtd6", O_RDWR, S_IXGRP); //这是我打开设备地方式       

但是出错,errno=25。
这是为什么,难是我用NFS启动了linux,所以有问题?
我直接从板子启动内核(不通过NFS),执行这个应用程序获取/dev/mtd6(或者其它mtd设备),还是出错,errno=25.
难道要修改内核的MTD设备选项,还是别的原因?
我已经搞了2天了,还没搞定,请大家帮帮我,谢谢。

[ 本帖最后由 wenxy 于 2009-4-20 12:02 编辑 ]

jiangzj123 发表于 2009-04-20 13:43

fd = open("/dev/mtd6", O_RDWR); 试试

emmoblin 发表于 2009-04-20 23:28

你的这个fd是打开的哪个设备?
查看一下mtd的源代码,看看MEMGETINFO是如何处理的,看他为什么会返回25

wenxy 发表于 2009-04-21 10:07

原帖由 jiangzj123 于 2009-4-20 13:43 发表 http://linux.chinaunix.net/bbs/images/common/back.gif
fd = open("/dev/mtd6", O_RDWR); 试试

我早就试过了,不行,错误码是errno=25.

wenxy 发表于 2009-04-21 10:14

原帖由 emmoblin 于 2009-4-20 23:28 发表 http://linux.chinaunix.net/bbs/images/common/back.gif
你的这个fd是打开的哪个设备?
查看一下mtd的源代码,看看MEMGETINFO是如何处理的,看他为什么会返回25
fd打开的是/dev/mtd0, 或者/dev/mtd1, /dev/mtd2, /dev/mtd3, /dev/mtd4,/dev/mtd5, /dev/mtd6都可以成功打开,
但是调用ioctl(fd, MEMGETINFO, &mtdInfo);总是不成功,返回值<0, 查看错误码:errno = 25。

会不会是执行环境的影响?
因为公司,以前用同样的方法来做,是成功的。我现在用以前的内核启动板子试了也不行。

是不是在bootloader里对nor flash 分区后,在内核里做了maps,要不要对每个分区再做其它的操作后,才能调用ioctl(fd, MEMGETINFO, &mtdInfo); ?

[ 本帖最后由 wenxy 于 2009-4-21 10:20 编辑 ]

yidou 发表于 2009-04-21 10:18

原帖由 wenxy 于 2009/4/21 10:14 发表 http://linux.chinaunix.net/bbs/images/common/back.gif

fd打开的是/dev/mtd0, 或者/dev/mtd1, /dev/mtd2, /dev/mtd3, /dev/mtd4,/dev/mtd5, /dev/mtd6都可以成功打开,
但是调用ioctl(fd, MEMGETINFO, &mtdInfo);总是不成功,返回值

errno = -25 吧?

wenxy 发表于 2009-04-21 11:23

搞定了,是编译参数的问题/编译环境的问题,搞了我3天了,郁闷。多谢各位。
以下是输出结果:
# test_mtd_good 0 /dev/mtd6
Note: acees MTD devices begin
Open /dev/mtd6 file, mode is read only OK!
mtdInfo: type=3, flags=5, size=262144, erasesize=131072
Char print:
      []
Hex print:
      

Note: Exit

yidou 发表于 2009-04-21 11:32

原帖由 wenxy 于 2009/4/21 11:23 发表 http://linux.chinaunix.net/bbs/images/common/back.gif
搞定了,是编译参数的问题/编译环境的问题,搞了我3天了,郁闷。多谢各位。
以下是输出结果:
# test_mtd_good 0 /dev/mtd6
Note: acees MTD devices begin
Open /dev/mtd6 file, mode is read only OK!
m ...

不解

wenxy 发表于 2009-04-21 11:32

是编译选项里,指向了FC4的linux的头文件(是错误的),正确的做法指向broadcom的内核的头文件。

CC := mipsel-uclibc-gcc #gcc
DIR := /opt/stblinux-2.6.12/include#/usr/include, 不应当指向linux FC4的目录里的头文件。

CFLAGS := -o2 -I$(DIR)

wenxy 发表于 2009-04-21 11:52

原帖由 yidou 于 2009-4-21 11:32 发表 http://linux.chinaunix.net/bbs/images/common/back.gif


不解


我正想研究一下,这两个头文件的区别:
#include <mtd/mtd-user.h>               
#include <mtd/jffs2-user.h>
页: [1] 2
查看完整版本: 关于直接编写应用程序读写/dev/mtd0 或者 /dev/mtdblock0的问题。