2012年11月15日 星期四

擷取 mtd的資訊

#define MTD_FILE "/proc/mtd"

typedef struct CtMtdInfo
{
    unsigned char       DevName[MAX_SHORT_LENGTH];            // The image file name
    unsigned char       Size[MAX_SHORT_LENGTH];               // get dev size
    unsigned char       EraseSize[MAX_SHORT_LENGTH];          // erase size
    unsigned char       PartitionName[MAX_SHORT_LENGTH];      // partition name
    unsigned char       ImageName[MAX_SHORT_LENGTH];          // image name
    int                 FlashType;                            // flash type
    int                 FsType;             // file system type
    struct CtMtdInfo    *next;
}CtMtdInfoS;

if (GetMtdInfo(MTD_FILE, &mtdInfo) == FALSE)
    {
        printf("[Comtrend] open mtd file error!!!\n");
        goto errorGetMacDevices;
    }


BOOL GetMtdInfo(char *fileName, CtMtdInfoS **pMtdInfo)
{
    CtMtdInfoS                  *pFirst, *pCurrent, *pNew;
    struct mtd_info_user      meminfo;
    BOOL                        flag = TRUE;
    FILE                        *tmp_fp;
    char                           line_buff[MAX_SHORT_LENGTH];
    char                        devName[DEV_NAME_LENGTH];
    int                         i = 0, count = 0, fd;
    pFirst = pCurrent = pNew = NULL;
    memset(devName, '\0', DEV_NAME_LENGTH);
    if (!(tmp_fp = fopen(fileName, "r")))
    {
        goto error;
        flag = FALSE;
    }
    (void)fseek(tmp_fp, 0L, SEEK_SET);
    while(!feof(tmp_fp))
    {
        bzero(line_buff, MAX_SHORT_LENGTH);
        fgets(line_buff, MAX_SHORT_LENGTH, tmp_fp);
        int buffLength = strlen(line_buff);
        i = 0;
        if (buffLength > 0)
        {
            if (strstr(line_buff, "mtd") != 0)
            {
                pNew = malloc(sizeof(CtMtdInfoS));
                pNew->next = NULL;
                if (pFirst)
                {
                    pCurrent->next = pNew;
                    pCurrent = pNew;
                } else {
                    pFirst = pCurrent = pNew;
                }
                int start = 0;
                while(line_buff[i] != '\n')
                {
                    if (line_buff[i] == ' ')
                    {
                        if (count == CT_MTD_DEVNAME)
                        {
                            strncpy(devName, &line_buff[0], i - 1);
                            sprintf((char*)pNew->DevName, "/dev/%s", devName);
                        } else if (count == CT_MTD_SIZE) {
                            strncpy((char*)pNew->Size, &line_buff[start], i - start);
                        } else if (count == CT_MTD_ERASESIZE) {
                            strncpy((char*)pNew->EraseSize, &line_buff[start], i - start);
                        }
                        start = i + 1;
                        count ++;
                    }
                    if (line_buff[i] == '"')
                    {
                        strncpy((char*)pNew->PartitionName, &line_buff[i + 1], buffLength - i - 3);
                        break;
                    }
                    i ++;
                }     
#ifdef _NEVER               
                // get flash type {{
                if ((fd = open((char*)pNew->DevName, O_RDWR)) < 0) {
                    printf("[ct_flash_fun] open %s fail!\n", pNew->DevName);
                    flag = FALSE;
                    goto error;
                }
               
                if (ioctl(fd, MEMGETINFO, &meminfo) != 0) {
                    printf("[ct_flash_fun] ioctl %s fail!\n", pNew->DevName);
                    flag = FALSE;
                    goto error;
                }
               
                close(fd);
               
                pNew->FlashType = meminfo.type == MTD_NANDFLASH ? 1 : 0;
                // get flash type }}   
#endif               
            }  
        }
        count = 0;
    }
    if (pFirst)
        *pMtdInfo = pFirst;
   
error:
   
    close(fd);
    if (tmp_fp)
        fclose(tmp_fp);   
   
    return flag;
}

沒有留言:

張貼留言