Ejemplo n.º 1
0
int mtd_get_dev_info1(libmtd_t desc, int dev_num, struct mtd_dev_info *mtd)
{
	int ret;
	struct stat st;
	struct libmtd *lib = (struct libmtd *)desc;

	memset(mtd, 0, sizeof(struct mtd_dev_info));
	mtd->dev_num = dev_num;

	if (!lib->sysfs_supported)
		return legacy_get_dev_info1(dev_num, mtd);
	else {
		char file[strlen(lib->mtd) + 10];

		sprintf(file, lib->mtd, dev_num);
		if (stat(file, &st)) {
			if (errno == ENOENT)
				errno = ENODEV;
			return -1;
		}
	}

	if (dev_get_major(lib, dev_num, &mtd->major, &mtd->minor))
		return -1;

	ret = dev_read_data(lib->mtd_name, dev_num, &mtd->name,
			    MTD_NAME_MAX + 1);
	if (ret < 0)
		return -1;
	((char *)mtd->name)[ret - 1] = '\0';

	ret = dev_read_data(lib->mtd_type, dev_num, &mtd->type_str,
			    MTD_TYPE_MAX + 1);
	if (ret < 0)
		return -1;
	((char *)mtd->type_str)[ret - 1] = '\0';

	if (dev_read_pos_int(lib->mtd_eb_size, dev_num, &mtd->eb_size))
		return -1;
	if (dev_read_pos_ll(lib->mtd_size, dev_num, &mtd->size))
		return -1;
	if (dev_read_pos_int(lib->mtd_min_io_size, dev_num, &mtd->min_io_size))
		return -1;
	if (dev_read_pos_int(lib->mtd_subpage_size, dev_num, &mtd->subpage_size))
		return -1;
	if (dev_read_pos_int(lib->mtd_oob_size, dev_num, &mtd->oob_size))
		return -1;
	if (dev_read_pos_int(lib->mtd_region_cnt, dev_num, &mtd->region_cnt))
		return -1;
	if (dev_read_hex_int(lib->mtd_flags, dev_num, &ret))
		return -1;
	mtd->writable = !!(ret & MTD_WRITEABLE);

	mtd->eb_cnt = mtd->size / mtd->eb_size;
	mtd->type = type_str2int(mtd->type_str);
	mtd->bb_allowed = !!(mtd->type == MTD_NANDFLASH);

	return 0;
}
Ejemplo n.º 2
0
/**
 * find_dev_num - find UBI device number by its character device node.
 *
 * @lib   UBI library descriptor
 * @node  UBI character device node name
 *
 * This function returns positive UBI device number in case of success and %-1
 * in case of failure.
 */
static int find_dev_num(struct libubi *lib, const char *node)
{
	struct stat stat;
	struct ubi_info info;
	int i, major, minor;

	if (lstat(node, &stat))
		return -1;

	if (!S_ISCHR(stat.st_mode)) {
		errno = EINVAL;
		return -1;
	}

	major = major(stat.st_rdev);
	minor = minor(stat.st_rdev);

	if (minor != 0) {
		errno = -EINVAL;
		return -1;
	}

	if (ubi_get_info((libubi_t *)lib, &info))
		return -1;

	for (i = info.lowest_dev_num; i <= info.highest_dev_num; i++) {
		int major1, minor1, ret;
		char buf[50];

		ret = dev_read_data(lib->dev_dev, i, &buf[0], 50);
		if (ret < 0)
			return -1;

		ret = sscanf(&buf[0], "%d:%d\n", &major1, &minor1);
		if (ret != 2) {
			fprintf(stderr, "LIBUBI: bad value at sysfs file\n");
			errno = EINVAL;
			return -1;
		}

		if (minor1 == minor && major1 == major)
			return i;
	}

	errno = ENOENT;
	return -1;
}