1. 程式人生 > >Arm9 跑的linux系統,從nandflash的指定地址去讀一串資料日誌

Arm9 跑的linux系統,從nandflash的指定地址去讀一串資料日誌

背景:通過程式燒寫工具在Nandflash的指定地址寫入1個16位元組的裝置編號,裝置啟動時從指定位置讀取該26位元組的編號,並通過計算改編碼是否合法,否則不允許啟動。

使用命令前用cat /proc/mtd 檢視一下mtdchar字元裝置;或者用ls -l /dev/mtd*

cat /proc/mtd

dev: size erasesize name 
mtd0: 00c00000 00020000 “ROOTFS” 
mtd1: 00200000 00020000 “BOOTLOADER” 
mtd2: 00200000 00020000 “KERNEL” 
mtd3: 03200000 00020000 “NAND ROOTFS partition” 
mtd4: 04b00000 00020000 “NAND DATAFS partition”

為了更詳細瞭解分割槽資訊用mtd_debug命令

通過命令確定指定地址屬於那一個裝置對映,我使用的板子地址0x900000對應在/dev/mtdblock0。然後直接讀取資料:

INT Flash_Read(char *devName,off_t offset, UINT len,  char *pBuf)
{
    int fd;
    int readLen = 0;
    if ((fd = open(devName, O_SYNC | O_RDONLY)) < 0)
    {
        perror("open");
        return  -1;
    }
    if (offset != lseek(fd, offset, SEEK_SET)) 
	{
        perror("lseek");
        return  -1;
    }
    readLen = read(fd, pBuf, len);
    if (readLen < 0)
    {
        return  -1;
    }
    return readLen;
}

void Read_SN()
{
    char Sn[16];
    BYTE i;
    memset(Sn,0x00,16);
    Flash_Read("/dev/mtdblock0",0x900000,0x10,Sn);
    PRINT_LOG("\n\n\nSn=%s\n\n\n",Sn);
    for(i=0;i<16;i++)
    {
      PRINT_LOG("  %02x",Sn[i]);
    }
    PRINT_LOG("\n\n\n");
}