如何獲取EMMC記憶體大小

xcyansmile發表於2020-11-09

如何獲取EMMC記憶體大小

類別

需求

索引類別

獲取EMMC記憶體大小

問題描述

對應無法得知EMMC總記憶體的大小,基於這個問題所寫的程式碼。

程式碼關聯

#define PATH_MAX_STRING_SIZE 256
#define BLKGETSIZE64 _IOR(0x12,114,size_t)
#define BLKGETSIZE _IO(0x12,96)

unsigned long rtk_get_size_emmc(void)
{
    int fd;
    unsigned long long v64;
    unsigned long longsectors;
    char *pblock_name = "/dev/block/mmcblk0";

    fd = open(pblock_name, O_RDWR|O_SYNC);
    if(fd < 0)
    {
        return -1;
    }

    if (ioctl(fd, BLKGETSIZE64, &v64) == 0)
    {
        /* Got bytes, convert to 512 byte sectors */
        v64 >>= 9;
        if (v64 != (unsigned long)v64)
        {

        ret_trunc:
            /* Not only DOS, but all other partition tables
             * we support can't record more than 32 bit
             * sector counts or offsets
             */
            v64 = (unsigned long)-1L;
        }
        close(fd);
        return v64;
    }
    /* Needs temp of type long */
    if (ioctl(fd, BLKGETSIZE, &longsectors))
    {
        /* Perhaps this is a disk image */
        unsigned long sz = lseek(fd, 0, SEEK_END);
        longsectors = 0;
        if (sz > 0)
            longsectors = (unsigned long)sz / 512;
        lseek(fd, 0, SEEK_SET);
    }
    if (sizeof(long) > sizeof(unsigned long)
            && longsectors != (unsigned long)longsectors
       )
    {
        goto ret_trunc;
    }
    close(fd);
    return longsectors;
}

改進建議

工作記錄…

相關文章