static int flash_write_nor(int mtdnum, struct img_type *img) { int fdout; char mtd_device[LINESIZE]; int ret; uint32_t checksum; long unsigned int dummy = 0; struct flash_description *flash = get_flash_info(); if (!mtd_dev_present(flash->libmtd, mtdnum)) { ERROR("MTD %d does not exist\n", mtdnum); return -ENODEV; } snprintf(mtd_device, sizeof(mtd_device), "/dev/mtd%d", mtdnum); if ((fdout = open(mtd_device, O_RDWR)) < 0) { ERROR( "%s: %s: %s", __func__, mtd_device, strerror(errno)); return -1; } ret = copyfile(img->fdin, fdout, img->size, &dummy, 0, 0, &checksum); /* tell 'nbytes == 0' (EOF) from 'nbytes < 0' (read error) */ if (ret < 0) { ERROR("Failure installing into: %s", img->device); return -1; } close(fdout); return 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; }
static void test_mtd_dev_present(void **state) { int ret; libmtd_t lib = mock_libmtd_open(); ret = mtd_dev_present(lib, 0); assert_int_equal(ret, 1); libmtd_close(lib); (void) state; }
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; }
static int flash_erase(int mtdnum) { int fd; char mtd_device[LINESIZE]; struct mtd_dev_info *mtd; int noskipbad = 0; int unlock = 0; unsigned int eb, eb_start, eb_cnt; struct flash_description *flash = get_flash_info(); if (!mtd_dev_present(flash->libmtd, mtdnum)) { ERROR("MTD %d does not exist\n", mtdnum); return -ENODEV; } mtd = &flash->mtd_info[mtdnum].mtd; snprintf(mtd_device, sizeof(mtd_device), "/dev/mtd%d", mtdnum); if ((fd = open(mtd_device, O_RDWR)) < 0) { ERROR( "%s: %s: %s", __func__, mtd_device, strerror(errno)); return -ENODEV; } /* * prepare to erase all of the MTD partition, */ eb_start = 0; eb_cnt = (mtd->size / mtd->eb_size) - eb_start; for (eb = 0; eb < eb_start + eb_cnt; eb++) { /* Always skip bad sectors */ if (!noskipbad) { int ret = mtd_is_bad(mtd, fd, eb); if (ret > 0) { continue; } else if (ret < 0) { if (errno == EOPNOTSUPP) { noskipbad = 1; } else { ERROR("%s: MTD get bad block failed", mtd_device); return -EFAULT; } } } if (unlock) { if (mtd_unlock(mtd, fd, eb) != 0) { TRACE("%s: MTD unlock failure", mtd_device); continue; } } if (mtd_erase(flash->libmtd, mtd, fd, eb) != 0) { ERROR("%s: MTD Erase failure", mtd_device); return -EFAULT; } } close(fd); return 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; }
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; }