jffs2/mtdparts: Fix problem with usage from JFFS2 and MTDPARTS together

Currently using JFFS2 with MTDPARTS enabled doesn't work. This is because
mtdparts_init() is available in both files, cmd_mtdparts.c and
cmd_jffs2.c. Please note that in the original cmd_jffs2.c file (before
the jffs2/mtdparts command/file split those 2 different versions
already existed. So this is nothing new. The main problem is that the
variables "current_dev" and "current_partnum" are declared in both
files now. This doesn't work.

This patch now changes the names of those variable to more specific
names: "current_mtd_dev" and "current_mtd_partnum". This is because
this patch also changes the declaration from static to global, so
that they can be used from both files.

Please note that my first tests were not successful. The MTD devices
selected via mtdparts are now accessed but I'm failing to see the
directory listed via the "ls" command. Nothing is displayed. Perhaps
I didn't generate the JFFS2 image correctly (I never used JFFS2 in
U-Boot before). Not sure. Perhaps somebody else could take a look at
this as well. I'll continue looking into this on Monday.

Signed-off-by: Stefan Roese <sr@denx.de>
Cc: Wolfgang Denk <wd@denx.de>
Cc: Detlev Zundel <dzu@denx.de>
Cc: Ilya Yanok <yanok@emcraft.com>
Cc: Renaud barbier <renaud.barbier@ge.com>
diff --git a/common/cmd_jffs2.c b/common/cmd_jffs2.c
index 860d1d9..3bb6c3c 100644
--- a/common/cmd_jffs2.c
+++ b/common/cmd_jffs2.c
@@ -137,8 +137,15 @@
 #define MTD_WRITEABLE_CMD		1
 
 /* current active device and partition number */
-static struct mtd_device *current_dev = NULL;
-static u8 current_partnum = 0;
+#ifdef CONFIG_CMD_MTDPARTS
+/* Use the ones declared in cmd_mtdparts.c */
+extern struct mtd_device *current_mtd_dev;
+extern u8 current_mtd_partnum;
+#else
+/* Use local ones */
+struct mtd_device *current_mtd_dev = NULL;
+u8 current_mtd_partnum = 0;
+#endif
 
 #if defined(CONFIG_CMD_CRAMFS)
 extern int cramfs_check (struct part_info *info);
@@ -346,6 +353,9 @@
  * Parse and initialize global mtdids mapping and create global
  * device/partition list.
  *
+ * 'Static' version of command line mtdparts_init() routine. Single partition on
+ * a single device configuration.
+ *
  * @return 0 on success, 1 otherwise
  */
 int mtdparts_init(void)
@@ -360,18 +370,18 @@
 		struct part_info *part;
 
 		initialized = 1;
-		current_dev = (struct mtd_device *)
+		current_mtd_dev = (struct mtd_device *)
 			malloc(sizeof(struct mtd_device) +
 					sizeof(struct part_info) +
 					sizeof(struct mtdids));
-		if (!current_dev) {
+		if (!current_mtd_dev) {
 			printf("out of memory\n");
 			return 1;
 		}
-		memset(current_dev, 0, sizeof(struct mtd_device) +
-					sizeof(struct part_info) + sizeof(struct mtdids));
+		memset(current_mtd_dev, 0, sizeof(struct mtd_device) +
+		       sizeof(struct part_info) + sizeof(struct mtdids));
 
-		id = (struct mtdids *)(current_dev + 1);
+		id = (struct mtdids *)(current_mtd_dev + 1);
 		part = (struct part_info *)(id + 1);
 
 		/* id */
@@ -386,7 +396,7 @@
 		if ((mtd_id_parse(dev_name, NULL, &id->type, &id->num) != 0) ||
 				(mtd_device_validate(id->type, id->num, &size) != 0)) {
 			printf("incorrect device: %s%d\n", MTD_DEV_TYPE(id->type), id->num);
-			free(current_dev);
+			free(current_mtd_dev);
 			return 1;
 		}
 		id->size = size;
@@ -413,7 +423,7 @@
 
 		part->sector_size = get_part_sector_size(id, part);
 
-		part->dev = current_dev;
+		part->dev = current_mtd_dev;
 		INIT_LIST_HEAD(&part->link);
 
 		/* recalculate size if needed */
@@ -424,11 +434,11 @@
 				part->name, part->size, part->offset);
 
 		/* device */
-		current_dev->id = id;
-		INIT_LIST_HEAD(&current_dev->link);
-		current_dev->num_parts = 1;
-		INIT_LIST_HEAD(&current_dev->parts);
-		list_add(&part->link, &current_dev->parts);
+		current_mtd_dev->id = id;
+		INIT_LIST_HEAD(&current_mtd_dev->link);
+		current_mtd_dev->num_parts = 1;
+		INIT_LIST_HEAD(&current_mtd_dev->parts);
+		list_add(&part->link, &current_mtd_dev->parts);
 	}
 
 	return 0;
@@ -516,7 +526,7 @@
 	if (mtdparts_init() !=0)
 		return 1;
 
-	if ((part = jffs2_part_info(current_dev, current_partnum))){
+	if ((part = jffs2_part_info(current_mtd_dev, current_mtd_partnum))){
 
 		/* check partition type for cramfs */
 		fsname = (cramfs_check(part) ? "CRAMFS" : "JFFS2");
@@ -567,7 +577,7 @@
 	if (mtdparts_init() !=0)
 		return 1;
 
-	if ((part = jffs2_part_info(current_dev, current_partnum))){
+	if ((part = jffs2_part_info(current_mtd_dev, current_mtd_partnum))){
 
 		/* check partition type for cramfs */
 		if (cramfs_check(part)) {
@@ -602,7 +612,7 @@
 	if (mtdparts_init() !=0)
 		return 1;
 
-	if ((part = jffs2_part_info(current_dev, current_partnum))){
+	if ((part = jffs2_part_info(current_mtd_dev, current_mtd_partnum))){
 
 		/* check partition type for cramfs */
 		fsname = (cramfs_check(part) ? "CRAMFS" : "JFFS2");
diff --git a/common/cmd_mtdparts.c b/common/cmd_mtdparts.c
index e7b6acc..6a0849f 100644
--- a/common/cmd_mtdparts.c
+++ b/common/cmd_mtdparts.c
@@ -166,8 +166,8 @@
 struct list_head devices;
 
 /* current active device and partition number */
-static struct mtd_device *current_dev = NULL;
-static u8 current_partnum = 0;
+struct mtd_device *current_mtd_dev = NULL;
+u8 current_mtd_partnum = 0;
 
 static struct part_info* mtd_part_info(struct mtd_device *dev, unsigned int part_num);
 
@@ -251,12 +251,12 @@
 
 	DEBUGF("--- index partitions ---\n");
 
-	if (current_dev) {
+	if (current_mtd_dev) {
 		mtddevnum = 0;
 		list_for_each(dentry, &devices) {
 			dev = list_entry(dentry, struct mtd_device, link);
-			if (dev == current_dev) {
-				mtddevnum += current_partnum;
+			if (dev == current_mtd_dev) {
+				mtddevnum += current_mtd_partnum;
 				sprintf(buf, "%d", mtddevnum);
 				setenv("mtddevnum", buf);
 				break;
@@ -264,7 +264,7 @@
 			mtddevnum += dev->num_parts;
 		}
 
-		part = mtd_part_info(current_dev, current_partnum);
+		part = mtd_part_info(current_mtd_dev, current_mtd_partnum);
 		setenv("mtddevname", part->name);
 
 		DEBUGF("=> mtddevnum %d,\n=> mtddevname %s\n", mtddevnum, part->name);
@@ -285,9 +285,9 @@
 
 	DEBUGF("--- current_save ---\n");
 
-	if (current_dev) {
-		sprintf(buf, "%s%d,%d", MTD_DEV_TYPE(current_dev->id->type),
-					current_dev->id->num, current_partnum);
+	if (current_mtd_dev) {
+		sprintf(buf, "%s%d,%d", MTD_DEV_TYPE(current_mtd_dev->id->type),
+					current_mtd_dev->id->num, current_mtd_partnum);
 
 		setenv("partition", buf);
 		strncpy(last_partition, buf, 16);
@@ -498,18 +498,18 @@
 
 	/* otherwise just delete this partition */
 
-	if (dev == current_dev) {
+	if (dev == current_mtd_dev) {
 		/* we are modyfing partitions for the current device,
 		 * update current */
 		struct part_info *curr_pi;
-		curr_pi = mtd_part_info(current_dev, current_partnum);
+		curr_pi = mtd_part_info(current_mtd_dev, current_mtd_partnum);
 
 		if (curr_pi) {
 			if (curr_pi == part) {
 				printf("current partition deleted, resetting current to 0\n");
-				current_partnum = 0;
+				current_mtd_partnum = 0;
 			} else if (part->offset <= curr_pi->offset) {
-				current_partnum--;
+				current_mtd_partnum--;
 			}
 			current_save_needed = 1;
 		}
@@ -579,8 +579,8 @@
 
 	/* get current partition info if we are updating current device */
 	curr_pi = NULL;
-	if (dev == current_dev)
-		curr_pi = mtd_part_info(current_dev, current_partnum);
+	if (dev == current_mtd_dev)
+		curr_pi = mtd_part_info(current_mtd_dev, current_mtd_partnum);
 
 	list_for_each(entry, &dev->parts) {
 		struct part_info *pi;
@@ -600,7 +600,7 @@
 			if (curr_pi && (pi->offset <= curr_pi->offset)) {
 				/* we are modyfing partitions for the current
 				 * device, update current */
-				current_partnum++;
+				current_mtd_partnum++;
 				current_save();
 			} else {
 				index_partitions();
@@ -842,15 +842,15 @@
 	list_del(&dev->link);
 	free(dev);
 
-	if (dev == current_dev) {
+	if (dev == current_mtd_dev) {
 		/* we just deleted current device */
 		if (list_empty(&devices)) {
-			current_dev = NULL;
+			current_mtd_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_mtd_dev = list_entry(devices.next, struct mtd_device, link);
+			current_mtd_partnum = 0;
 		}
 		current_save();
 		return 0;
@@ -893,8 +893,8 @@
 	u8 current_save_needed = 0;
 
 	if (list_empty(&devices)) {
-		current_dev = dev;
-		current_partnum = 0;
+		current_mtd_dev = dev;
+		current_mtd_partnum = 0;
 		current_save_needed = 1;
 	}
 
@@ -1050,7 +1050,7 @@
 static int mtd_devices_init(void)
 {
 	last_parts[0] = '\0';
-	current_dev = NULL;
+	current_mtd_dev = NULL;
 	current_save();
 
 	return device_delall(&devices);
@@ -1330,13 +1330,13 @@
 	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 = mtd_part_info(current_dev, current_partnum);
+	/* current_mtd_dev is not NULL only when we have non empty device list */
+	if (current_mtd_dev) {
+		part = mtd_part_info(current_mtd_dev, current_mtd_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,
+					MTD_DEV_TYPE(current_mtd_dev->id->type),
+					current_mtd_dev->id->num, current_mtd_partnum,
 					part->name, part->size, part->offset);
 		} else {
 			printf("could not get current partition info\n\n");
@@ -1709,13 +1709,13 @@
 		strncpy(last_parts, parts, MTDPARTS_MAXLEN);
 
 		/* reset first partition from first dev from the list as current */
-		current_dev = list_entry(devices.next, struct mtd_device, link);
-		current_partnum = 0;
+		current_mtd_dev = list_entry(devices.next, struct mtd_device, link);
+		current_mtd_partnum = 0;
 		current_save();
 
-		DEBUGF("mtdparts_init: current_dev  = %s%d, current_partnum = %d\n",
-				MTD_DEV_TYPE(current_dev->id->type),
-				current_dev->id->num, current_partnum);
+		DEBUGF("mtdparts_init: current_mtd_dev  = %s%d, current_mtd_partnum = %d\n",
+				MTD_DEV_TYPE(current_mtd_dev->id->type),
+				current_mtd_dev->id->num, current_mtd_partnum);
 	}
 
 	/* mtdparts variable was reset to NULL, delete all devices/partitions */
@@ -1735,8 +1735,8 @@
 		DEBUGF("--- getting current partition: %s\n", tmp_ep);
 
 		if (find_dev_and_part(tmp_ep, &cdev, &pnum, &p) == 0) {
-			current_dev = cdev;
-			current_partnum = pnum;
+			current_mtd_dev = cdev;
+			current_mtd_partnum = pnum;
 			current_save();
 		}
 	} else if (getenv("partition") == NULL) {
@@ -1820,8 +1820,8 @@
 	if (find_dev_and_part(argv[1], &dev, &pnum, &part) != 0)
 		return 1;
 
-	current_dev = dev;
-	current_partnum = pnum;
+	current_mtd_dev = dev;
+	current_mtd_partnum = pnum;
 	current_save();
 
 	printf("partition changed to %s%d,%d\n",