반응형
[MTD] READ(Info 포함) WRITE 소스
출처> https://stackoverflow.com/questions/24302243/getting-einval-when-trying-to-write-to-mtd-device
#include <fcntl.h>
#include <mtd/mtd-user.h>
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <sys/ioctl.h>
int main(){
mtd_info_t mtd_info; // the MTD structure
erase_info_t ei; // the erase block structure
int i;
unsigned char read_buf[20] = {0x00}; // empty array for reading
int fd = open("/dev/mtd0", O_RDWR); // open the mtd device for reading and
// writing. Note you want mtd0 not mtdblock0
// also you probably need to open permissions
// to the dev (sudo chmod 777 /dev/mtd0)
ioctl(fd, MEMGETINFO, &mtd_info); // get the device info
// dump it for a sanity check, should match what's in /proc/mtd
printf("MTD Type: %x\nMTD total size: %x(hex) bytes\nMTD erase size: %x(hex) bytes\nMTD write size: %x(hex) bytes\n",
mtd_info.type, mtd_info.size, mtd_info.erasesize, mtd_info.writesize);
ei.length = mtd_info.erasesize; //set the erase block size
for(ei.start = 0; ei.start < mtd_info.size; ei.start += ei.length)
{
ioctl(fd, MEMUNLOCK, &ei);
// printf("Eraseing Block %#x\n", ei.start); // show the blocks erasing
// warning, this prints a lot!
ioctl(fd, MEMERASE, &ei);
}
lseek(fd, 0, SEEK_SET); // go to the first block
read(fd, read_buf, sizeof(read_buf)); // read 20 bytes
// sanity check, should be all 0xFF if erase worked
for(i = 0; i<20; i++)
printf("buf[%d] = 0x%02x\n", i, (unsigned int)read_buf[i]);
/**********************************************************
* important part! *
* notice the size of data array is mtd_info.writesize *
**********************************************************/
uint32_t write_size = mtd_info.writesize;
unsigned char data[write_size];//write 0
bzero(data, write_size);
lseek(fd, 0, SEEK_SET); // go back to first block's start
write(fd, data, sizeof(data)); // write our message
lseek(fd, 0, SEEK_SET); // go back to first block's start
read(fd, read_buf, sizeof(read_buf));// read the data
// sanity check, now you see the message we wrote!
for(i = 0; i<20; i++)
printf("buf[%d] = 0x%02x\n", i, (unsigned int)read_buf[i]);
close(fd);
return 0;
}
반응형
'프로...Linux' 카테고리의 다른 글
[IPC] Inter-Process Communications (0) | 2019.03.14 |
---|---|
[압축] Linux 압축 관련 (0) | 2019.02.26 |
[HowTo] iso, img, opk 화일 생성 및 추출 (0) | 2019.02.13 |
[SCRIPT] 쉘 스크립트 예제 (0) | 2019.02.02 |
[ssh] ssh 접속만 허용하고 scp나 리모트 명령은 막는 방법 (0) | 2019.01.23 |