I have a small program which I’m using to attempt to read out the details of the MTDs on my embedded Linux platform. I’m running into problems where most blocks can’t be read and I’m not 100% sure why this is happening.
A check of the /dev directory shows 8 mtds all with the same permissions:
# 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
My application is also running as root:
# ls -al mtd_test
-rwxrwxrwx 1 root root 19688 Nov 30 01:18 mtd_test
Checking the /proc I can see that 7 of the 8 mtd’s are mounted (so I expected mtd7 to fail to be read)
# 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)"
Oddly, only mtd1 and mtd6 were able to be read, all the others failed with an error “Permission denied”, does anyone have any idea why this would be?
I doubt it’s my code, but here it is:
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;
}
If you are using an ext family FS, the command lsattr works at a deeper level on access rights, but usually doesn’t apply to
/dev/*– you can try anyway. The jffs2 FS planned to implementlsattr– not sure if they did.The
O_RDONLYaccess instead ofO_RDWRcould help for reading the memory – some devices require a specific protocol to be written to, while reading may be freely allowed.Maybe playing with the
man 2 openflags – likeO_SYNCetc… – may help.There is this page but the author uses
O_RDWRas well.