Merge with /home/wd/git/u-boot/testing-NAND/ to add new NAND handling.
[platform/kernel/u-boot.git] / common / cmd_jffs2.c
index bc63f0c..ecadb79 100644 (file)
 #endif
 
 /* enable/disable debugging messages */
-#define        DEBUG
-#undef DEBUG
+#define        DEBUG_JFFS
+#undef DEBUG_JFFS
 
-#ifdef  DEBUG
+#ifdef  DEBUG_JFFS
 # define DEBUGF(fmt, args...)  printf(fmt ,##args)
 #else
 # define DEBUGF(fmt, args...)
 
 /* this flag needs to be set in part_info struct mask_flags
  * field for read-only partitions */
-#define MTD_WRITEABLE          1
+#define MTD_WRITEABLE_CMD              1
 
 #ifdef CONFIG_JFFS2_CMDLINE
 /* default values for mtdids and mtdparts variables */
@@ -242,6 +242,46 @@ static void memsize_format(char *buf, u32 size)
 }
 
 /**
+ * This routine does global indexing of all partitions. Resulting index for
+ * current partition is saved in 'mtddevnum'. Current partition name in
+ * 'mtddevname'.
+ */
+static void index_partitions(void)
+{
+       char buf[16];
+       u16 mtddevnum;
+       struct part_info *part;
+       struct list_head *dentry;
+       struct mtd_device *dev;
+
+       DEBUGF("--- index partitions ---\n");
+
+       if (current_dev) {
+               mtddevnum = 0;
+               list_for_each(dentry, &devices) {
+                       dev = list_entry(dentry, struct mtd_device, link);
+                       if (dev == current_dev) {
+                               mtddevnum += current_partnum;
+                               sprintf(buf, "%d", mtddevnum);
+                               setenv("mtddevnum", buf);
+                               break;
+                       }
+                       mtddevnum += dev->num_parts;
+               }
+
+               part = jffs2_part_info(current_dev, current_partnum);
+               setenv("mtddevname", part->name);
+
+               DEBUGF("=> mtddevnum %d,\n=> mtddevname %s\n", mtddevnum, part->name);
+       } else {
+               setenv("mtddevnum", NULL);
+               setenv("mtddevname", NULL);
+
+               DEBUGF("=> mtddevnum NULL\n=> mtddevname NULL\n");
+       }
+}
+
+/**
  * Save current device and partition in environment variable 'partition'.
  */
 static void current_save(void)
@@ -264,6 +304,7 @@ static void current_save(void)
 
                DEBUGF("=> partition NULL\n");
        }
+       index_partitions();
 }
 
 /**
@@ -279,7 +320,7 @@ static int part_validate_nor(struct mtdids *id, struct part_info *part)
 {
 #if (CONFIG_COMMANDS & CFG_CMD_FLASH)
        /* info for FLASH chips */
-       extern flash_info_t flash_info[CFG_MAX_FLASH_BANKS];
+       extern flash_info_t flash_info[];
        flash_info_t *flash;
        int offset_aligned;
        u32 end_offset;
@@ -401,6 +442,8 @@ static int part_validate(struct mtdids *id, struct part_info *part)
  */
 static int part_del(struct mtd_device *dev, struct part_info *part)
 {
+       u8 current_save_needed = 0;
+
        /* if there is only one partition, remove whole device */
        if (dev->num_parts == 1)
                return device_del(dev);
@@ -417,11 +460,10 @@ static int part_del(struct mtd_device *dev, struct part_info *part)
                        if (curr_pi == part) {
                                printf("current partition deleted, resetting current to 0\n");
                                current_partnum = 0;
-                               current_save();
                        } else if (part->offset <= curr_pi->offset) {
                                current_partnum--;
-                               current_save();
                        }
+                       current_save_needed = 1;
                }
        }
 
@@ -432,6 +474,11 @@ static int part_del(struct mtd_device *dev, struct part_info *part)
        free(part);
        dev->num_parts--;
 
+       if (current_save_needed > 0)
+               current_save();
+       else
+               index_partitions();
+
        return 0;
 }
 
@@ -475,6 +522,8 @@ static int part_sort_add(struct mtd_device *dev, struct part_info *part)
        if (list_empty(&dev->parts)) {
                DEBUGF("part_sort_add: list empty\n");
                list_add(&part->link, &dev->parts);
+               dev->num_parts++;
+               index_partitions();
                return 0;
        }
 
@@ -498,18 +547,23 @@ static int part_sort_add(struct mtd_device *dev, struct part_info *part)
 
                if (new_pi->offset <= pi->offset) {
                        list_add_tail(&part->link, entry);
+                       dev->num_parts++;
 
                        if (curr_pi && (pi->offset <= curr_pi->offset)) {
                                /* we are modyfing partitions for the current
                                 * device, update current */
                                current_partnum++;
                                current_save();
+                       } else {
+                               index_partitions();
                        }
-
                        return 0;
                }
        }
+
        list_add_tail(&part->link, &dev->parts);
+       dev->num_parts++;
+       index_partitions();
        return 0;
 }
 
@@ -530,7 +584,6 @@ static int part_add(struct mtd_device *dev, struct part_info *part)
        if (part_sort_add(dev, part) != 0)
                return 1;
 
-       dev->num_parts++;
        return 0;
 }
 
@@ -600,7 +653,7 @@ static int part_parse(const char *const partdef, const char **ret, struct part_i
        /* test for options */
        mask_flags = 0;
        if (strncmp(p, "ro", 2) == 0) {
-               mask_flags |= MTD_WRITEABLE;
+               mask_flags |= MTD_WRITEABLE_CMD;
                p += 2;
        }
 
@@ -665,7 +718,7 @@ static int device_validate(u8 type, u8 num, u32 *size)
        if (type == MTD_DEV_TYPE_NOR) {
 #if (CONFIG_COMMANDS & CFG_CMD_FLASH)
                if (num < CFG_MAX_FLASH_BANKS) {
-                       extern flash_info_t flash_info[CFG_MAX_FLASH_BANKS];
+                       extern flash_info_t flash_info[];
                        *size = flash_info[num].size;
 
                        return 0;
@@ -746,9 +799,10 @@ static int device_del(struct mtd_device *dev)
                        current_partnum = 0;
                }
                current_save();
+               return 0;
        }
 
-
+       index_partitions();
        return 0;
 }
 
@@ -782,13 +836,20 @@ static struct mtd_device* device_find(u8 type, u8 num)
  */
 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();
+               current_save_needed = 1;
        }
 
        list_add_tail(&dev->link, &devices);
+
+       if (current_save_needed > 0)
+               current_save();
+       else
+               index_partitions();
 }
 
 /**
@@ -907,7 +968,7 @@ static int device_parse(const char *const mtd_dev, const char **ret, struct mtd_
        }
        memset(dev, 0, sizeof(struct mtd_device));
        dev->id = id;
-       dev->num_parts = num_parts;
+       dev->num_parts = 0; /* part_sort_add increments num_parts */
        INIT_LIST_HEAD(&dev->parts);
        INIT_LIST_HEAD(&dev->link);
 
@@ -1120,7 +1181,7 @@ static int generate_mtdparts(char *buf, u32 buflen)
                        }
 
                        /* ro mask flag */
-                       if (part->mask_flags && MTD_WRITEABLE) {
+                       if (part->mask_flags && MTD_WRITEABLE_CMD) {
                                len = 2;
                                if (len > maxlen)
                                        goto cleanup;
@@ -1559,7 +1620,7 @@ int mtdparts_init(void)
                ids_changed = 1;
 
                if (parse_mtdids(ids) != 0) {
-                       device_delall(&devices);
+                       devices_init();
                        return 1;
                }