- return 0;
- }
-
- printf("no such FLASH device: %s%d (valid range 0 ... %d\n",
- MTD_DEV_TYPE(type), num, CFG_MAX_FLASH_BANKS - 1);
-#else
- printf("support for FLASH devices not present\n");
-#endif
- } else if (type == MTD_DEV_TYPE_NAND) {
-#if defined(CONFIG_JFFS2_NAND) && defined(CONFIG_CMD_NAND)
- if (num < CFG_MAX_NAND_DEVICE) {
-#ifndef CONFIG_NAND_LEGACY
- *size = nand_info[num].size;
-#else
- extern struct nand_chip nand_dev_desc[CFG_MAX_NAND_DEVICE];
- *size = nand_dev_desc[num].totlen;
-#endif
- return 0;
- }
-
- printf("no such NAND device: %s%d (valid range 0 ... %d)\n",
- MTD_DEV_TYPE(type), num, CFG_MAX_NAND_DEVICE - 1);
-#else
- printf("support for NAND devices not present\n");
-#endif
- } else if (type == MTD_DEV_TYPE_ONENAND) {
-#if defined(CONFIG_CMD_ONENAND)
- *size = onenand_mtd.size;
- return 0;
-#else
- printf("support for OneNAND devices not present\n");
-#endif
- } else
- printf("Unknown defice type %d\n", type);
-
- return 1;
-}
-
-#ifdef CONFIG_JFFS2_CMDLINE
-/**
- * Delete all mtd devices from a supplied devices list, free memory allocated for
- * each device and delete all device partitions.
- *
- * @return 0 on success, 1 otherwise
- */
-static int device_delall(struct list_head *head)
-{
- struct list_head *entry, *n;
- struct mtd_device *dev_tmp;
-
- /* clean devices list */
- list_for_each_safe(entry, n, head) {
- dev_tmp = list_entry(entry, struct mtd_device, link);
- list_del(entry);
- part_delall(&dev_tmp->parts);
- free(dev_tmp);
- }
- INIT_LIST_HEAD(&devices);
-
- return 0;
-}
-
-/**
- * If provided device exists it's partitions are deleted, device is removed
- * from device list and device memory is freed.
- *
- * @param dev device to be deleted
- * @return 0 on success, 1 otherwise
- */
-static int device_del(struct mtd_device *dev)
-{
- part_delall(&dev->parts);
- list_del(&dev->link);
- free(dev);
-
- if (dev == current_dev) {
- /* we just deleted current device */
- if (list_empty(&devices)) {
- current_dev = NULL;
- } else {
- /* reset first partition from first dev from the
- * devices list as current */
- current_dev = list_entry(devices.next, struct mtd_device, link);
- current_partnum = 0;
- }
- current_save();
- return 0;
- }
-
- index_partitions();
- return 0;
-}
-
-/**
- * Search global device list and return pointer to the device of type and num
- * specified.
- *
- * @param type device type
- * @param num device number
- * @return NULL if requested device does not exist
- */
-static struct mtd_device* device_find(u8 type, u8 num)
-{
- struct list_head *entry;
- struct mtd_device *dev_tmp;
-
- list_for_each(entry, &devices) {
- dev_tmp = list_entry(entry, struct mtd_device, link);
-
- if ((dev_tmp->id->type == type) && (dev_tmp->id->num == num))
- return dev_tmp;
- }
-
- return NULL;
-}
-
-/**
- * Add specified device to the global device list.
- *
- * @param dev device to be added
- */
-static void device_add(struct mtd_device *dev)
-{
- u8 current_save_needed = 0;
-
- if (list_empty(&devices)) {
- current_dev = dev;
- current_partnum = 0;
- current_save_needed = 1;
- }
-
- list_add_tail(&dev->link, &devices);
-
- if (current_save_needed > 0)
- current_save();
- else
- index_partitions();
-}
-
-/**
- * Parse device type, name and mtd-id. If syntax is ok allocate memory and
- * return pointer to the device structure.
- *
- * @param mtd_dev pointer to the device definition string i.e. <mtd-dev>
- * @param ret output pointer to next char after parse completes (output)
- * @param retdev pointer to the allocated device (output)
- * @return 0 on success, 1 otherwise
- */
-static int device_parse(const char *const mtd_dev, const char **ret, struct mtd_device **retdev)
-{
- struct mtd_device *dev;
- struct part_info *part;
- struct mtdids *id;
- const char *mtd_id;
- unsigned int mtd_id_len;
- const char *p, *pend;
- LIST_HEAD(tmp_list);
- struct list_head *entry, *n;
- u16 num_parts;
- u32 offset;
- int err = 1;
-
- p = mtd_dev;
- *retdev = NULL;
- *ret = NULL;
-
- DEBUGF("===device_parse===\n");
-
- /* fetch <mtd-id> */
- mtd_id = p;
- if (!(p = strchr(mtd_id, ':'))) {
- printf("no <mtd-id> identifier\n");
- return 1;
- }
- mtd_id_len = p - mtd_id + 1;
- p++;
-
- /* verify if we have a valid device specified */
- if ((id = id_find_by_mtd_id(mtd_id, mtd_id_len - 1)) == NULL) {
- printf("invalid mtd device '%.*s'\n", mtd_id_len - 1, mtd_id);
- return 1;
- }
-
- DEBUGF("dev type = %d (%s), dev num = %d, mtd-id = %s\n",
- id->type, MTD_DEV_TYPE(id->type),
- id->num, id->mtd_id);
- pend = strchr(p, ';');
- DEBUGF("parsing partitions %.*s\n", (pend ? pend - p : strlen(p)), p);
-
-
- /* parse partitions */
- num_parts = 0;
-
- offset = 0;
- if ((dev = device_find(id->type, id->num)) != NULL) {
- /* if device already exists start at the end of the last partition */
- part = list_entry(dev->parts.prev, struct part_info, link);
- offset = part->offset + part->size;
- }
-
- while (p && (*p != '\0') && (*p != ';')) {
- err = 1;
- if ((part_parse(p, &p, &part) != 0) || (!part))
- break;
-
- /* calculate offset when not specified */
- if (part->offset == OFFSET_NOT_SPECIFIED)
- part->offset = offset;
- else
- offset = part->offset;
-
- /* verify alignment and size */
- if (part_validate(id, part) != 0)
- break;
-
- offset += part->size;
-
- /* partition is ok, add it to the list */
- list_add_tail(&part->link, &tmp_list);
- num_parts++;
- err = 0;
- }
- if (err == 1) {
- part_delall(&tmp_list);
- return 1;
- }
-
- if (num_parts == 0) {
- printf("no partitions for device %s%d (%s)\n",
- MTD_DEV_TYPE(id->type), id->num, id->mtd_id);
- return 1;
- }
-
- DEBUGF("\ntotal partitions: %d\n", num_parts);
-
- /* check for next device presence */
- if (p) {
- if (*p == ';') {
- *ret = ++p;
- } else if (*p == '\0') {
- *ret = p;
- } else {
- printf("unexpected character '%c' at the end of device\n", *p);
- *ret = NULL;
- return 1;
- }
- }
-
- /* allocate memory for mtd_device structure */
- if ((dev = (struct mtd_device *)malloc(sizeof(struct mtd_device))) == NULL) {
- printf("out of memory\n");
- return 1;
- }
- memset(dev, 0, sizeof(struct mtd_device));
- dev->id = id;
- dev->num_parts = 0; /* part_sort_add increments num_parts */
- INIT_LIST_HEAD(&dev->parts);
- INIT_LIST_HEAD(&dev->link);
-
- /* move partitions from tmp_list to dev->parts */
- list_for_each_safe(entry, n, &tmp_list) {
- part = list_entry(entry, struct part_info, link);
- list_del(entry);
- if (part_sort_add(dev, part) != 0) {
- device_del(dev);
- return 1;
- }
- }
-
- *retdev = dev;
-
- DEBUGF("===\n\n");
- return 0;
-}
-
-/**
- * Initialize global device list.
- *
- * @return 0 on success, 1 otherwise
- */
-static int devices_init(void)
-{
- last_parts[0] = '\0';
- current_dev = NULL;
- current_save();
-
- return device_delall(&devices);
-}
-
-/*
- * Search global mtdids list and find id of requested type and number.
- *
- * @return pointer to the id if it exists, NULL otherwise
- */
-static struct mtdids* id_find(u8 type, u8 num)
-{
- struct list_head *entry;
- struct mtdids *id;
-
- list_for_each(entry, &mtdids) {
- id = list_entry(entry, struct mtdids, link);
-
- if ((id->type == type) && (id->num == num))
- return id;
- }
-
- return NULL;
-}
-
-/**
- * Search global mtdids list and find id of a requested mtd_id.
- *
- * Note: first argument is not null terminated.
- *
- * @param mtd_id string containing requested mtd_id
- * @param mtd_id_len length of supplied mtd_id
- * @return pointer to the id if it exists, NULL otherwise
- */
-static struct mtdids* id_find_by_mtd_id(const char *mtd_id, unsigned int mtd_id_len)
-{
- struct list_head *entry;
- struct mtdids *id;
-
- DEBUGF("--- id_find_by_mtd_id: '%.*s' (len = %d)\n",
- mtd_id_len, mtd_id, mtd_id_len);
-
- list_for_each(entry, &mtdids) {
- id = list_entry(entry, struct mtdids, link);
-
- DEBUGF("entry: '%s' (len = %d)\n",
- id->mtd_id, strlen(id->mtd_id));
-
- if (mtd_id_len != strlen(id->mtd_id))
- continue;
- if (strncmp(id->mtd_id, mtd_id, mtd_id_len) == 0)
- return id;
- }
-
- return NULL;
-}
-#endif /* #ifdef CONFIG_JFFS2_CMDLINE */
-
-/**
- * Parse device id string <dev-id> := 'nand'|'nor'|'onenand'<dev-num>,
- * return device type and number.
- *
- * @param id string describing device id
- * @param ret_id output pointer to next char after parse completes (output)
- * @param dev_type parsed device type (output)
- * @param dev_num parsed device number (output)
- * @return 0 on success, 1 otherwise
- */
-int id_parse(const char *id, const char **ret_id, u8 *dev_type, u8 *dev_num)
-{
- const char *p = id;
-
- *dev_type = 0;
- if (strncmp(p, "nand", 4) == 0) {
- *dev_type = MTD_DEV_TYPE_NAND;
- p += 4;
- } else if (strncmp(p, "nor", 3) == 0) {
- *dev_type = MTD_DEV_TYPE_NOR;
- p += 3;
- } else if (strncmp(p, "onenand", 7) == 0) {
- *dev_type = MTD_DEV_TYPE_ONENAND;
- p += 7;
- } else {
- printf("incorrect device type in %s\n", id);
- return 1;
- }
-
- if (!isdigit(*p)) {
- printf("incorrect device number in %s\n", id);
- return 1;
- }
-
- *dev_num = simple_strtoul(p, (char **)&p, 0);
- if (ret_id)
- *ret_id = p;
- return 0;
-}
-
-#ifdef CONFIG_JFFS2_CMDLINE
-/**
- * Process all devices and generate corresponding mtdparts string describing
- * all partitions on all devices.
- *
- * @param buf output buffer holding generated mtdparts string (output)
- * @param buflen buffer size
- * @return 0 on success, 1 otherwise
- */
-static int generate_mtdparts(char *buf, u32 buflen)
-{
- struct list_head *pentry, *dentry;
- struct mtd_device *dev;
- struct part_info *part, *prev_part;
- char *p = buf;
- char tmpbuf[32];
- u32 size, offset, len, part_cnt;
- u32 maxlen = buflen - 1;
-
- DEBUGF("--- generate_mtdparts ---\n");
-
- if (list_empty(&devices)) {
- buf[0] = '\0';
- return 0;
- }
-
- sprintf(p, "mtdparts=");
- p += 9;
-
- list_for_each(dentry, &devices) {
- dev = list_entry(dentry, struct mtd_device, link);
-
- /* copy mtd_id */
- len = strlen(dev->id->mtd_id) + 1;
- if (len > maxlen)
- goto cleanup;
- memcpy(p, dev->id->mtd_id, len - 1);
- p += len - 1;
- *(p++) = ':';
- maxlen -= len;
-
- /* format partitions */
- prev_part = NULL;
- part_cnt = 0;
- list_for_each(pentry, &dev->parts) {
- part = list_entry(pentry, struct part_info, link);
- size = part->size;
- offset = part->offset;
- part_cnt++;
-
- /* partition size */
- memsize_format(tmpbuf, size);
- len = strlen(tmpbuf);
- if (len > maxlen)
- goto cleanup;
- memcpy(p, tmpbuf, len);
- p += len;
- maxlen -= len;
-
-
- /* add offset only when there is a gap between
- * partitions */
- if ((!prev_part && (offset != 0)) ||
- (prev_part && ((prev_part->offset + prev_part->size) != part->offset))) {
-
- memsize_format(tmpbuf, offset);
- len = strlen(tmpbuf) + 1;
- if (len > maxlen)
- goto cleanup;
- *(p++) = '@';
- memcpy(p, tmpbuf, len - 1);
- p += len - 1;
- maxlen -= len;
- }
-
- /* copy name only if user supplied */
- if(!part->auto_name) {
- len = strlen(part->name) + 2;
- if (len > maxlen)
- goto cleanup;
-
- *(p++) = '(';
- memcpy(p, part->name, len - 2);
- p += len - 2;
- *(p++) = ')';
- maxlen -= len;
- }
-
- /* ro mask flag */
- if (part->mask_flags && MTD_WRITEABLE_CMD) {
- len = 2;
- if (len > maxlen)
- goto cleanup;
- *(p++) = 'r';
- *(p++) = 'o';
- maxlen -= 2;
- }
-
- /* print ',' separator if there are other partitions
- * following */
- if (dev->num_parts > part_cnt) {
- if (1 > maxlen)
- goto cleanup;
- *(p++) = ',';
- maxlen--;
- }
- prev_part = part;
- }
- /* print ';' separator if there are other devices following */
- if (dentry->next != &devices) {
- if (1 > maxlen)
- goto cleanup;
- *(p++) = ';';
- maxlen--;
- }
- }
-
- /* we still have at least one char left, as we decremented maxlen at
- * the begining */
- *p = '\0';
-
- return 0;
-
-cleanup:
- last_parts[0] = '\0';
- return 1;
-}
-
-/**
- * Call generate_mtdparts to process all devices and generate corresponding
- * mtdparts string, save it in mtdparts environment variable.
- *
- * @param buf output buffer holding generated mtdparts string (output)
- * @param buflen buffer size
- * @return 0 on success, 1 otherwise
- */
-static int generate_mtdparts_save(char *buf, u32 buflen)
-{
- int ret;
-
- ret = generate_mtdparts(buf, buflen);
-
- if ((buf[0] != '\0') && (ret == 0))
- setenv("mtdparts", buf);
- else
- setenv("mtdparts", NULL);
-
- return ret;
-}
-
-/**
- * Format and print out a partition list for each device from global device
- * list.
- */
-static void list_partitions(void)
-{
- struct list_head *dentry, *pentry;
- struct part_info *part;
- struct mtd_device *dev;
- int part_num;
-
- DEBUGF("\n---list_partitions---\n");
- list_for_each(dentry, &devices) {
- dev = list_entry(dentry, struct mtd_device, link);
- printf("\ndevice %s%d <%s>, # parts = %d\n",
- MTD_DEV_TYPE(dev->id->type), dev->id->num,
- dev->id->mtd_id, dev->num_parts);
- printf(" #: name\t\t\tsize\t\toffset\t\tmask_flags\n");
-
- /* list partitions for given device */
- part_num = 0;
- list_for_each(pentry, &dev->parts) {
- part = list_entry(pentry, struct part_info, link);
- printf("%2d: %-20s0x%08x\t0x%08x\t%d\n",
- part_num, part->name, part->size,
- part->offset, part->mask_flags);
-
- part_num++;
- }
- }
- if (list_empty(&devices))
- printf("no partitions defined\n");
-
- /* current_dev is not NULL only when we have non empty device list */
- if (current_dev) {
- part = jffs2_part_info(current_dev, current_partnum);
- if (part) {
- printf("\nactive partition: %s%d,%d - (%s) 0x%08x @ 0x%08x\n",
- MTD_DEV_TYPE(current_dev->id->type),
- current_dev->id->num, current_partnum,
- part->name, part->size, part->offset);
- } else {
- printf("could not get current partition info\n\n");
- }
- }
-
- printf("\ndefaults:\n");
- printf("mtdids : %s\n", mtdids_default);
- printf("mtdparts: %s\n", mtdparts_default);
-}
-
-/**
- * Given partition identifier in form of <dev_type><dev_num>,<part_num> find
- * corresponding device and verify partition number.
- *
- * @param id string describing device and partition or partition name
- * @param dev pointer to the requested device (output)
- * @param part_num verified partition number (output)
- * @param part pointer to requested partition (output)
- * @return 0 on success, 1 otherwise
- */
-int find_dev_and_part(const char *id, struct mtd_device **dev,
- u8 *part_num, struct part_info **part)
-{
- struct list_head *dentry, *pentry;
- u8 type, dnum, pnum;
- const char *p;
-
- DEBUGF("--- find_dev_and_part ---\nid = %s\n", id);
-
- list_for_each(dentry, &devices) {
- *part_num = 0;
- *dev = list_entry(dentry, struct mtd_device, link);
- list_for_each(pentry, &(*dev)->parts) {
- *part = list_entry(pentry, struct part_info, link);
- if (strcmp((*part)->name, id) == 0)
- return 0;
- (*part_num)++;
- }
- }
-
- p = id;
- *dev = NULL;
- *part = NULL;
- *part_num = 0;
-
- if (id_parse(p, &p, &type, &dnum) != 0)
- return 1;
-
- if ((*p++ != ',') || (*p == '\0')) {
- printf("no partition number specified\n");
- return 1;
- }
- pnum = simple_strtoul(p, (char **)&p, 0);
- if (*p != '\0') {
- printf("unexpected trailing character '%c'\n", *p);
- return 1;
- }
-
- if ((*dev = device_find(type, dnum)) == NULL) {
- printf("no such device %s%d\n", MTD_DEV_TYPE(type), dnum);
- return 1;
- }
-
- if ((*part = jffs2_part_info(*dev, pnum)) == NULL) {
- printf("no such partition\n");
- *dev = NULL;
- return 1;
- }
-
- *part_num = pnum;
-
- return 0;
-}
-
-/**
- * Find and delete partition. For partition id format see find_dev_and_part().
- *
- * @param id string describing device and partition
- * @return 0 on success, 1 otherwise
- */
-static int delete_partition(const char *id)
-{
- u8 pnum;
- struct mtd_device *dev;
- struct part_info *part;
-
- if (find_dev_and_part(id, &dev, &pnum, &part) == 0) {
-
- DEBUGF("delete_partition: device = %s%d, partition %d = (%s) 0x%08lx@0x%08lx\n",
- MTD_DEV_TYPE(dev->id->type), dev->id->num, pnum,
- part->name, part->size, part->offset);
-
- if (part_del(dev, part) != 0)
- return 1;