I am suggesting the attached patch because it:


1) synchronizes the new enum tap_state names/spellings with the corresponding string representation


2) Is safer than an array lookup which is vulnerable to a bad index and thereby a seg fault.
Thanks,

Dick

Index: src/jtag/bitq.c
===================================================================
--- src/jtag/bitq.c	(revision 1239)
+++ src/jtag/bitq.c	(working copy)
@@ -172,7 +172,7 @@
 		if (tap_transitions[cur_state].low == cmd->path[i]) bitq_io(0, 0, 0);
 		else if (tap_transitions[cur_state].high == cmd->path[i]) bitq_io(1, 0, 0);
 		else {
-			LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition", tap_state_strings[cur_state], tap_state_strings[cmd->path[i]]);
+			LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition", show_state(cur_state), show_state(cmd->path[i)]);
 			exit(-1);
 		}
 
Index: src/jtag/zy1000.c
===================================================================
--- src/jtag/zy1000.c	(revision 1239)
+++ src/jtag/zy1000.c	(working copy)
@@ -266,51 +266,51 @@
 int loadFile(const char *fileName, void **data, int *len)
 {
 	FILE * pFile;
-	pFile = fopen (fileName,"rb");
+	pFile = fopen(fileName,"rb");
 	if (pFile==NULL)
 	{
 		LOG_ERROR("Can't open %s\n", fileName);
 		return ERROR_JTAG_DEVICE_ERROR;
 	}
-    if (fseek (pFile, 0, SEEK_END)!=0)
-    {
+	if (fseek(pFile, 0, SEEK_END)!=0)
+	{
 		LOG_ERROR("Can't open %s\n", fileName);
 		fclose(pFile);
 		return ERROR_JTAG_DEVICE_ERROR;
-    }
-    *len=ftell (pFile);
-    if (*len==-1)
-    {
+	}
+	*len=ftell(pFile);
+	if (*len==-1)
+	{
 		LOG_ERROR("Can't open %s\n", fileName);
 		fclose(pFile);
 		return ERROR_JTAG_DEVICE_ERROR;
-    }
+	}
 
-    if (fseek (pFile, 0, SEEK_SET)!=0)
-    {
+	if (fseek(pFile, 0, SEEK_SET)!=0)
+	{
 		LOG_ERROR("Can't open %s\n", fileName);
 		fclose(pFile);
 		return ERROR_JTAG_DEVICE_ERROR;
-    }
-    *data=malloc(*len+1);
-    if (*data==NULL)
-    {
+	}
+	*data=malloc(*len+1);
+	if (*data==NULL)
+	{
 		LOG_ERROR("Can't open %s\n", fileName);
 		fclose(pFile);
 		return ERROR_JTAG_DEVICE_ERROR;
-    }
+	}
 
-    if (fread(*data, 1, *len, pFile)!=*len)
-    {
+	if (fread(*data, 1, *len, pFile)!=*len)
+	{
 		fclose(pFile);
 	free(*data);
 		LOG_ERROR("Can't open %s\n", fileName);
 		return ERROR_JTAG_DEVICE_ERROR;
-    }
-    fclose (pFile);
-    *(((char *)(*data))+*len)=0; /* sentinel */
+	}
+	fclose(pFile);
+	*(((char *)(*data))+*len)=0; /* sentinel */
 
-    return ERROR_OK;
+	return ERROR_OK;
 
 
 
@@ -365,7 +365,7 @@
 #if 0
 static void shiftValueInnerFlip(const enum tap_state state, const enum tap_state endState, int repeat, cyg_uint32 value)
 {
-	VERBOSE(LOG_INFO("shiftValueInner %s %s %d %08x (flipped)", tap_state_strings[state], tap_state_strings[endState], repeat, value));
+	VERBOSE(LOG_INFO("shiftValueInner %s %s %d %08x (flipped)", show_state(state), show_state(endState), repeat, value));
 	cyg_uint32 a,b;
 	a=state;
 	b=endState;
@@ -476,7 +476,7 @@
 			int r=fields[i].in_handler(inBuffer, fields[i].in_handler_priv, fields+i);
 			if (r!=ERROR_OK)
 			{
-			    /* this will cause jtag_execute_queue() to return an error */
+				/* this will cause jtag_execute_queue() to return an error */
 				jtag_error=r;
 			}
 		}
@@ -703,7 +703,7 @@
 		}
 		else
 		{
-			LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition", tap_state_strings[cur_state], tap_state_strings[path[state_count]]);
+			LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition", show_state(cur_state), show_state(path[state_count)]);
 			exit(-1);
 		}
 
Index: src/jtag/bitbang.c
===================================================================
--- src/jtag/bitbang.c	(revision 1239)
+++ src/jtag/bitbang.c	(working copy)
@@ -109,7 +109,7 @@
 		}
 		else
 		{
-			LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition", tap_state_strings[cur_state], tap_state_strings[cmd->path[state_count]]);
+			LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition", show_state(cur_state), show_state(cmd->path[state_count]));
 			exit(-1);
 		}
 
Index: src/jtag/gw16012.c
===================================================================
--- src/jtag/gw16012.c	(revision 1239)
+++ src/jtag/gw16012.c	(working copy)
@@ -240,7 +240,7 @@
 		}
 		else
 		{
-			LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition", tap_state_strings[cur_state], tap_state_strings[cmd->path[state_count]]);
+			LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition", show_state(cur_state), show_state(cmd->path[state_count]));
 			exit(-1);
 		}
 
Index: src/jtag/jlink.c
===================================================================
--- src/jtag/jlink.c	(revision 1239)
+++ src/jtag/jlink.c	(working copy)
@@ -376,7 +376,7 @@
 		}
 		else
 		{
-			LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition", tap_state_strings[cur_state], tap_state_strings[path[i]]);
+			LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition", show_state(cur_state), show_state(path[i]));
 			exit(-1);
 		}
 
Index: src/jtag/usbprog.c
===================================================================
--- src/jtag/usbprog.c	(revision 1239)
+++ src/jtag/usbprog.c	(working copy)
@@ -282,7 +282,7 @@
 		}
 		else
 		{
-			LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition", tap_state_strings[cur_state], tap_state_strings[cmd->path[state_count]]);
+			LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition", show_state(cur_state), show_state(cmd->path[state_count]));
 			exit(-1);
 		}
 
Index: src/jtag/ft2232.c
===================================================================
--- src/jtag/ft2232.c	(revision 1239)
+++ src/jtag/ft2232.c	(working copy)
@@ -525,7 +525,7 @@
 				buf_set_u32(&tms_byte, bit_count++, 1, 0x1);
 			else
 			{
-				LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition", tap_state_strings[cur_state], tap_state_strings[cmd->path[state_count]]);
+				LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition", show_state(cur_state), show_state(cmd->path[state_count]));
 				exit(-1);
 			}
 
Index: src/jtag/jtag.c
===================================================================
--- src/jtag/jtag.c	(revision 1239)
+++ src/jtag/jtag.c	(working copy)
@@ -41,14 +41,6 @@
 int jtag_error=ERROR_OK;
 
 
-char* tap_state_strings[16] =
-{
-	"tlr",
-	"sds", "cd", "sd", "e1d", "pd", "e2d", "ud",
-	"rti",
-	"sis", "ci", "si", "e1i", "pi", "e2i", "ui"
-};
-
 typedef struct cmd_queue_page_s
 {
 	void *address;
@@ -1004,7 +996,7 @@
 		if ((tap_transitions[cur_state].low != path[i])&&
 				(tap_transitions[cur_state].high != path[i]))
 		{
-			LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition", tap_state_strings[cur_state], tap_state_strings[path[i]]);
+			LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition", show_state(cur_state), show_state(path[i]));
 			exit(-1);
 		}
 		cur_state = path[i];
@@ -2656,14 +2648,14 @@
 	{
 		for (state = 0; state < 16; state++)
 		{
-			if (strcmp(args[0], tap_state_strings[state]) == 0)
+			if (strcmp(args[0], show_state(state)) == 0)
 			{
 				jtag_add_end_state(state);
 				jtag_execute_queue();
 			}
 		}
 	}
-	command_print(cmd_ctx, "current endstate: %s", tap_state_strings[cmd_queue_end_state]);
+	command_print(cmd_ctx, "current endstate: %s", show_state(cmd_queue_end_state));
 
 	return ERROR_OK;
 }
@@ -2915,3 +2907,34 @@
 				Jim_Nvp_value2name_simple( nvp_jtag_tap_event, e)->name);
 	}
 }
+
+
+/* map state number to SVF state string */
+const char* show_state(enum tap_state state)
+{
+	const char* ret;
+
+	switch( state )
+	{
+	case TAP_RESET:		ret = "RESET";			break;
+	case TAP_IDLE:		ret = "IDLE";			break;
+	case TAP_DRSELECT:	ret = "DRSELECT";		break;
+	case TAP_DRCAPTURE: ret = "DRCAPTURE";		break;
+	case TAP_DRSHIFT:	ret = "DRSHIFT";			break;
+	case TAP_DREXIT1:	ret = "DREXIT1";			break;
+	case TAP_DRPAUSE:	ret = "DRPAUSE";			break;
+	case TAP_DREXIT2:	ret = "DREXIT2";			break;
+	case TAP_DRUPDATE:	ret = "DRUPDATE";		break;
+	case TAP_IRSELECT:	ret = "IRSELECT";		break;
+	case TAP_IRCAPTURE: ret = "IRCAPTURE";		break;
+	case TAP_IRSHIFT:	ret = "IRSHIFT";			break;
+	case TAP_IREXIT1:	ret = "IREXIT1";			break;
+	case TAP_IRPAUSE:	ret = "IRPAUSE";			break;
+	case TAP_IREXIT2:	ret = "IREXIT2";			break;
+	case TAP_IRUPDATE:	ret = "IRUPDATE";		break;
+	default:				ret = "???";
+	}
+
+	return ret;
+}
+
Index: src/jtag/jtag.h
===================================================================
--- src/jtag/jtag.h	(revision 1239)
+++ src/jtag/jtag.h	(working copy)
@@ -50,7 +50,6 @@
 	enum tap_state low;
 } tap_transition_t;
 
-extern char* tap_state_strings[16];
 extern int tap_move_map[16];	/* map 16 TAP states to 6 stable states */
 extern u8 tap_move[6][6];		/* value scanned to TMS to move from one of six stable states to another */
 extern tap_transition_t tap_transitions[16];	/* describe the TAP state diagram */
@@ -529,4 +528,17 @@
 	interface_jtag_add_dr_out(tap, num_fields, num_bits, value, cmd_queue_end_state);
 }
 
+#if defined(DEBUG)
+#define D(x)		x
+#else
+#define D(x)		/* nothing */
+#endif
+
+/**
+ * Function show_state
+ * converts the binary \a state to a meaningful character string.
+ */
+const char* show_state(enum tap_state state);
+
+
 #endif /* JTAG_H */
_______________________________________________
Openocd-development mailing list
[email protected]
https://lists.berlios.de/mailman/listinfo/openocd-development

Reply via email to