0
点赞
收藏
分享

微信扫一扫

mtd设备通过分区名称找到分区信息

苦茶如歌 2022-04-07 阅读 94
c语言

 ret = find_mtd_device_path("rom",dev_name,256);
    if(-1 == ret){
        perror("find_mtd_device_path rom failed\n");
        goto fail;
    }
    if ((prom_fd = open(dev_name, O_RDONLY)) == -1) {
        perror("open ROM");
        goto fail;
    }
    if ((ret = ioctl(prom_fd, MEMGETINFO, &prom_meminfo)) != 0) {
        perror("ROM MEMGETINFO");
        goto fail;
    }

    // rom_meminfo.size:40m, erasesize:131072, writepagesize:2048, oobsize:128 
    T_INFO("rom_meminfo.size:%d, erasesize:%d, writesize:%d, oobsize:%d \n",prom_meminfo.size, 
                prom_meminfo.erasesize, prom_meminfo.writesize, prom_meminfo.oobsize);


int find_mtd_device_path(const char *dev_name, char *dev_info_buf, int dev_info_bufsize)
{
    FILE *stream = 0;
    int device_index = -1;

    if (!dev_name || !dev_info_buf || 0 == dev_info_bufsize) {
        printf("Find mtd device path, NULL input\n");
        return -1;
    }
    memset(dev_info_buf, 0, dev_info_bufsize);

    //get mtd device index and path
    sprintf(dev_info_buf, "cat /proc/mtd | grep %s | cut -d':' -f1 | cut -d'd' -f2", dev_name);
    stream = popen(dev_info_buf , "r" );
    if (NULL == stream) {
        printf("Open /proc/mtd  %s  failed.\n", dev_name);
        return -1;
    }
    fscanf(stream,"%d", &device_index);
    pclose(stream);

    if (device_index < 0) {
        printf("Not found %s partition on /proc/mtd\n", dev_name);
        return -1;
    }
    memset(dev_info_buf, 0, dev_info_bufsize);
    sprintf(dev_info_buf, "/dev/mtd%d", device_index);

    return 0;
}
举报

相关推荐

0 条评论