Exemple #1
0
int scan_mtd(libmtd_t lib_mtd)
{
	struct mtd_dev_info dev_info;
	struct mtd_info info;
	int i, idx = 0;

	if (mtd_get_info(lib_mtd, &info))
		return -1;

	if (!info.mtd_dev_cnt)
		return 0;

	mtd_dev = xcalloc(info.mtd_dev_cnt, sizeof(mtd_dev[0]));

	for (i = info.lowest_mtd_num; i <= info.highest_mtd_num; ++i) {
		if (!mtd_dev_present(lib_mtd, i))
			continue;

		if (mtd_get_dev_info1(lib_mtd, i, &dev_info)) {
			perror("mtd_get_dev_info1");
			return -1;
		}

		memcpy(&(mtd_dev[idx++].info), &dev_info, sizeof(dev_info));
	}

	num_mtd_devices = idx;

	if (sort_by)
		qsort(mtd_dev, num_mtd_devices, sizeof(*mtd_dev), compare_mtd);
	return 0;
}
Exemple #2
0
static int print_dev_info(libmtd_t libmtd, const struct mtd_info *mtd_info, int mtdn)
{
	int err;
	struct mtd_dev_info mtd;

	err = mtd_get_dev_info1(libmtd, mtdn, &mtd);
	if (err) {
		if (errno == ENODEV)
			return errmsg("mtd%d does not correspond to any "
				      "existing MTD device", mtdn);
		return sys_errmsg("cannot get information about MTD device %d",
				  mtdn);
	}

	printf("mtd%d\n", mtd.mtd_num);
	printf("Name:                           %s\n", mtd.name);
	printf("Type:                           %s\n", mtd.type_str);
	printf("Eraseblock size:                ");
	ubiutils_print_bytes(mtd.eb_size, 0);
	printf("\n");
	printf("Amount of eraseblocks:          %d (", mtd.eb_cnt);
	ubiutils_print_bytes(mtd.size, 0);
	printf(")\n");
	printf("Minimum input/output unit size: %d %s\n",
	       mtd.min_io_size, mtd.min_io_size > 1 ? "bytes" : "byte");
	if (mtd_info->sysfs_supported)
		printf("Sub-page size:                  %d %s\n",
		       mtd.subpage_size,
		       mtd.subpage_size > 1 ? "bytes" : "byte");
	else if (mtd.type == MTD_NANDFLASH || mtd.type == MTD_MLCNANDFLASH)
		printf("Sub-page size:                  unknown\n");

	if (mtd.oob_size > 0)
		printf("OOB size:                       %d bytes\n",
		       mtd.oob_size);
	if (mtd.region_cnt > 0)
		printf("Additional erase regions:       %d\n", mtd.oob_size);
	if (mtd_info->sysfs_supported)
		printf("Character device major/minor:   %d:%d\n",
		       mtd.major, mtd.minor);
	printf("Bad blocks are allowed:         %s\n",
	       mtd.bb_allowed ? "true" : "false");
	printf("Device is writable:             %s\n",
	      mtd.writable ? "true" : "false");

	if (args.ubinfo)
		print_ubi_info(mtd_info, &mtd);

	print_region_info(&mtd);

	printf("\n");
	return 0;
}
int mtd_get_dev_info(libmtd_t desc, const char *node, struct mtd_dev_info *mtd)
{
	int dev_num;
	struct libmtd *lib = (struct libmtd *)desc;

	if (!lib->sysfs_supported)
		return legacy_get_dev_info(node, mtd);

	if (dev_node2num(lib, node, &dev_num))
		return -1;

	return mtd_get_dev_info1(desc, dev_num, mtd);
}
Exemple #4
0
static void test_mtd_get_dev_info1(void **state)
{
	struct libmtd *lib = mock_libmtd_open();
	struct mtd_dev_info info;
	int dev_num = 0;
	memset(&info, 0, sizeof(info));
	expect_open(SYSFS_ROOT "/class/mtd/mtd0/dev", O_RDONLY, 0);
	expect_read_real(50,0);
	expect_read(1,0);
	expect_close(3,1);
	expect_open(SYSFS_ROOT "/class/mtd/mtd0/name", O_RDONLY, 0);
	expect_read_real(128,0);
	expect_read(1,0);
	expect_close(3,1);
	expect_open(SYSFS_ROOT "/class/mtd/mtd0/type", O_RDONLY, 4);
	expect_read(65,0);
	expect_read(1,0);
	expect_close(4,0);
	expect_open(SYSFS_ROOT "/class/mtd/mtd0/erasesize", O_RDONLY, 0);
	expect_read_real(50, 0);
	expect_close(3,1);
	expect_open(SYSFS_ROOT "/class/mtd/mtd0/size", O_RDONLY, 0);
	expect_read_real(50,0);
	expect_close(3,1);
	expect_open(SYSFS_ROOT "/class/mtd/mtd0/writesize", O_RDONLY, 0);
	expect_read_real(50,0);
	expect_close(3,1);
	expect_open(SYSFS_ROOT "/class/mtd/mtd0/subpagesize", O_RDONLY, 0);
	expect_read_real(50,0);
	expect_close(3,1);
	expect_open(SYSFS_ROOT "/class/mtd/mtd0/oobsize", O_RDONLY, 0);
	expect_read_real(50,0);
	expect_close(3,1);
	expect_open(SYSFS_ROOT "/class/mtd/mtd0/oobavail", O_RDONLY, 0);
	expect_read_real(50,0);
	expect_close(3,1);
	expect_open(SYSFS_ROOT "/class/mtd/mtd0/numeraseregions", O_RDONLY, 0);
	expect_read_real(50,0);
	expect_close(3,1);
	expect_open(SYSFS_ROOT "/class/mtd/mtd0/flags", O_RDONLY, 0);
	expect_read_real(50,0);
	expect_close(3,1);
	int r = mtd_get_dev_info1(lib, dev_num, &info);
	assert_int_equal(r, 0);
	/* TODO check values */

	libmtd_close(lib);
	(void)state;
}
Exemple #5
0
static int print_general_info(libmtd_t libmtd, const struct mtd_info *mtd_info,
			      int all)
{
	int i, err, first = 1;
	struct mtd_dev_info mtd;

	printf("Count of MTD devices:           %d\n", mtd_info->mtd_dev_cnt);
	if (mtd_info->mtd_dev_cnt == 0)
		return 0;

	for (i = mtd_info->lowest_mtd_num;
	     i <= mtd_info->highest_mtd_num; i++) {
		err = mtd_get_dev_info1(libmtd, i, &mtd);
		if (err == -1) {
			if (errno == ENODEV)
				continue;
			return sys_errmsg("libmtd failed to get MTD device %d "
					  "information", i);
		}

		if (!first)
			printf(", mtd%d", i);
		else {
			printf("Present MTD devices:            mtd%d", i);
			first = 0;
		}
	}
	printf("\n");
	printf("Sysfs interface supported:      %s\n",
	       mtd_info->sysfs_supported ? "yes" : "no");

	if (!all)
		return 0;

	printf("\n");

	for (i = mtd_info->lowest_mtd_num;
	     i <= mtd_info->highest_mtd_num; i++) {
		if (!mtd_dev_present(libmtd, i))
			continue;
		err = print_dev_info(libmtd, mtd_info, i);
		if (err)
			return err;
	}

	return 0;
}
Exemple #6
0
int scan_mtd_devices (void)
{
	int err;
	struct flash_description *flash = get_flash_info();
	struct mtd_info *mtd_info = &flash->mtd;
	libmtd_t libmtd = flash->libmtd;
	char blacklist[100] = { 0 };
	char *token;
	char *saveptr;
	int i, index;

#if defined(CONFIG_UBIBLACKLIST)
	strncpy(blacklist, CONFIG_UBIBLACKLIST, sizeof(blacklist));
#endif

	if (!libmtd) {
		ERROR("MTD is not present on the target");
		return -1;
	}
	err = mtd_get_info(libmtd, mtd_info);
	if (err) {
		if (errno == ENODEV)
			ERROR("MTD is not present on the board");
		return 0;
	}

	/* Allocate memory to store MTD infos */
	flash->mtd_info = (struct mtd_ubi_info *)calloc(
				mtd_info->highest_mtd_num + 1,
				sizeof(struct mtd_ubi_info));
	if (!flash->mtd_info) {
		ERROR("No enough memory for MTD structures");
		return -ENOMEM;
	}

	token = strtok_r(blacklist, " ", &saveptr);
	if (token) {
		errno = 0;
		index = strtoul(token, NULL, 10);
		if (errno == 0) {
			ubi_insert_blacklist(index, flash);

			while ((token = strtok_r(NULL, " ", &saveptr))) {
				errno = 0;
				index = strtoul(token, NULL, 10);
				if (errno != 0)
					break;
				ubi_insert_blacklist(index, flash);
			}
		}
	}

	for (i = mtd_info->lowest_mtd_num;
	     i <= mtd_info->highest_mtd_num; i++) {
		if (!mtd_dev_present(libmtd, i))
			continue;
		err = mtd_get_dev_info1(libmtd, i, &flash->mtd_info[i].mtd);
		if (err) {
			TRACE("No information from MTD%d", i);
			continue;
		}
#if defined(CONFIG_UBIVOL)
		if (!flash->mtd_info[i].skipubi)
			scan_ubi_partitions(i);
#endif
	}

	return mtd_info->mtd_dev_cnt;
}
Exemple #7
0
int scan_mtd_devices (void)
{
	int err;
	struct flash_description *flash = get_flash_info();
	struct mtd_info *mtd_info = &flash->mtd;
	struct mtd_ubi_info *mtd_ubi_info;
	libmtd_t libmtd = flash->libmtd;
	char blacklist[100] = { 0 };
	char *token;
	char *saveptr;
	int i, index;

#if defined(CONFIG_UBIBLACKLIST)
	strncpy(blacklist, CONFIG_UBIBLACKLIST, sizeof(blacklist));
#endif

	/* Blacklist passed on the command line has priority */
	if (strlen(mtd_ubi_blacklist))
		strncpy(blacklist, mtd_ubi_blacklist, sizeof(blacklist));

	if (!libmtd) {
		ERROR("MTD is not present on the target");
		return -1;
	}
	err = mtd_get_info(libmtd, mtd_info);
	if (err) {
		if (errno == ENODEV)
			ERROR("MTD is not present on the board");
		return 0;
	}

	/* Allocate memory to store MTD infos */
	flash->mtd_info = (struct mtd_ubi_info *)calloc(
				mtd_info->highest_mtd_num + 1,
				sizeof(struct mtd_ubi_info));
	if (!flash->mtd_info) {
		ERROR("No enough memory for MTD structures");
		return -ENOMEM;
	}

	token = strtok_r(blacklist, " ", &saveptr);
	if (token) {
		errno = 0;
		index = strtoul(token, NULL, 10);
		if (errno == 0) {
			ubi_insert_blacklist(index, flash);

			while ((token = strtok_r(NULL, " ", &saveptr))) {
				errno = 0;
				index = strtoul(token, NULL, 10);
				if (errno != 0)
					break;
				ubi_insert_blacklist(index, flash);
			}
		}
	}

	for (i = mtd_info->lowest_mtd_num;
	     i <= mtd_info->highest_mtd_num; i++) {
		/* initialize data */
		mtd_ubi_info = &flash->mtd_info[i];
		LIST_INIT(&mtd_ubi_info->ubi_partitions);
		if (!mtd_dev_present(libmtd, i))
			continue;
		err = mtd_get_dev_info1(libmtd, i, &flash->mtd_info[i].mtd);
		if (err) {
			TRACE("No information from MTD%d", i);
			continue;
		}
	}

#if defined(CONFIG_UBIVOL)
	/*
	 * Now search for MTD that are already attached
	 */
	scan_for_ubi_devices();

	/*
	 * Search for volumes in MTD that are not attached, default case
	 */

	for (i = mtd_info->lowest_mtd_num;
	     i <= mtd_info->highest_mtd_num; i++) {
		if (flash->libubi && !flash->mtd_info[i].skipubi &&
				!flash->mtd_info[i].scanned &&
				flash->mtd_info[i].mtd.type != MTD_UBIVOLUME)
			scan_ubi_partitions(i);
	}
#endif

	return mtd_info->mtd_dev_cnt;
}