我有一个小程序,我正在尝试读取嵌入式Linux平台上MTD的详细信息。我遇到了无法读取大多数块的问题,我不确定为什么会发生这种情况。
检查/dev
目录显示8个mtd
具有相同的权限:
# ls -al | grep "mtd*"
crwxrwxrwx 1 root root 90, 0 Jan 1 1970 mtd0
crwxrwxrwx 1 root root 90, 2 Jan 1 1970 mtd1
crwxrwxrwx 1 root root 90, 4 Jan 1 1970 mtd2
...
crwxrwxrwx 1 root root 90, 14 Jan 1 1970 mtd7
我的应用程序也以root身份运行:
# ls -al mtd_test
-rwxrwxrwx 1 root root 19688 Nov 30 01:18 mtd_test
检查/proc
我可以看到8个mtd中有7个被挂载(所以我希望mtd7
无法读取)
# cat /proc/mtd
dev: size erasesize name
mtd0: 00020000 00020000 "u-boot (128 kB)"
mtd1: 00020000 00020000 "u-boot Environment (128 kB)"
mtd2: 00040000 00020000 "Reserved (256 kB)"
mtd3: 00200000 00020000 "Kernel (2048 kB)"
mtd4: 00000064 00020000 "rootFS header"
mtd5: 003fff9c 00020000 "rootFS (4096 kB)"
mtd6: 00180000 00020000 "Permanent Storage (1536 KB)"
奇怪的是,只有mtd1
和mtd6
能够被阅读,所有其他人都失败了并且出现“权限被拒绝”,是否有人知道为什么会这样?
我怀疑这是我的代码,但现在是:
int main()
{
mtd_info_t mtd_info;
int count, fd;
char devs[][15] = { {"/dev/mtd0"},{"/dev/mtdblock0"},
{"/dev/mtd1"},{"/dev/mtdblock1"},
{"/dev/mtd2"},{"/dev/mtdblock2"},
{"/dev/mtd3"},{"/dev/mtdblock3"},
{"/dev/mtd4"},{"/dev/mtdblock4"},
{"/dev/mtd5"},{"/dev/mtdblock5"},
{"/dev/mtd6"},{"/dev/mtdblock6"},
{"/dev/mtd7"},{"/dev/mtdblock7"}, };
for(count = 0; count < 16; count++) {
fd = open(devs[count], O_RDWR);
if(fd > 0) {
ioctl(fd, MEMGETINFO, &mtd_info);
printf("For dev: %s\nMTD Type: %u\nMTD total size: %u bytes\nMTD erase size: %u bytes\n",
devs[count], mtd_info.type, mtd_info.size, mtd_info.erasesize);
close(fd);
}
else
printf("Failed for %s: error - %s\n", devs[count], strerror(errno));
}
return 0;
}