Patchwork Use programmer name field

login
register
about
Submitter Carl-Daniel Hailfinger
Date 2009-08-12 14:25:30
Message ID <4A82D0DA.6020605@gmx.net>
Download mbox | patch
Permalink /patch/110/
State Accepted
Commit r680
Headers show

Comments

Carl-Daniel Hailfinger - 2009-08-12 14:25:30
Use programmer.name to match the --programmer parameter instead of
hardcoding the name of every single programmer in main().

-p dummyfoo won't be mistaken for -p dummy anymore.

Signed-off-by: Carl-Daniel Hailfinger <c-d.hailfinger.devel.2006@gmx.net>
Stefan Reinauer - 2009-08-12 14:32:19
On 8/12/09 4:25 PM, Carl-Daniel Hailfinger wrote:
> Use programmer.name to match the --programmer parameter instead of
> hardcoding the name of every single programmer in main().
>
> -p dummyfoo won't be mistaken for -p dummy anymore.
>
> Signed-off-by: Carl-Daniel Hailfinger <c-d.hailfinger.devel.2006@gmx.net>
>   

Acked-by: Stefan Reinauer <stepan@coresystems.de>
Carl-Daniel Hailfinger - 2009-08-12 14:43:16
On 12.08.2009 16:32, Stefan Reinauer wrote:
> On 8/12/09 4:25 PM, Carl-Daniel Hailfinger wrote:
>   
>> Use programmer.name to match the --programmer parameter instead of
>> hardcoding the name of every single programmer in main().
>>
>> -p dummyfoo won't be mistaken for -p dummy anymore.
>>
>> Signed-off-by: Carl-Daniel Hailfinger <c-d.hailfinger.devel.2006@gmx.net>  
>>     
>
> Acked-by: Stefan Reinauer <stepan@coresystems.de>
>   

Thanks, r680.

Regards,
Carl-Daniel

Patch

Index: flashrom-programmer_name/flash.h
===================================================================
--- flashrom-programmer_name/flash.h	(Revision 679)
+++ flashrom-programmer_name/flash.h	(Arbeitskopie)
@@ -88,6 +88,7 @@ 
 	PROGRAMMER_IT87SPI,
 	PROGRAMMER_FT2232SPI,
 	PROGRAMMER_SERPROG,
+	PROGRAMMER_INVALID /* This must always be the last entry. */
 };
 
 extern enum programmer programmer;
Index: flashrom-programmer_name/flashrom.c
===================================================================
--- flashrom-programmer_name/flashrom.c	(Revision 679)
+++ flashrom-programmer_name/flashrom.c	(Arbeitskopie)
@@ -38,6 +38,7 @@ 
 
 const struct programmer_entry programmer_table[] = {
 	{
+		.name			= "internal",
 		.init			= internal_init,
 		.shutdown		= internal_shutdown,
 		.map_flash_region	= physmap,
@@ -54,6 +55,7 @@ 
 	},
 
 	{
+		.name			= "dummy",
 		.init			= dummy_init,
 		.shutdown		= dummy_shutdown,
 		.map_flash_region	= dummy_map,
@@ -70,6 +72,7 @@ 
 	},
 
 	{
+		.name			= "nic3com",
 		.init			= nic3com_init,
 		.shutdown		= nic3com_shutdown,
 		.map_flash_region	= fallback_map,
@@ -86,6 +89,7 @@ 
 	},
 
 	{
+		.name			= "satasii",
 		.init			= satasii_init,
 		.shutdown		= satasii_shutdown,
 		.map_flash_region	= fallback_map,
@@ -102,6 +106,7 @@ 
 	},
 
 	{
+		.name			= "it87spi",
 		.init			= it87spi_init,
 		.shutdown		= fallback_shutdown,
 		.map_flash_region	= fallback_map,
@@ -118,6 +123,7 @@ 
 	},
 
 	{
+		.name			= "ft2232spi",
 		.init			= ft2232_spi_init,
 		.shutdown		= fallback_shutdown,
 		.map_flash_region	= fallback_map,
@@ -134,6 +140,7 @@ 
 	},
 
 	{
+		.name			= "serprog",
 		.init			= serprog_init,
 		.shutdown		= serprog_shutdown,
 		.map_flash_region	= fallback_map,
@@ -149,7 +156,7 @@ 
 		.delay			= serprog_delay,
 	},
 
-	{},
+	{}, /* This entry corresponds to PROGRAMMER_INVALID. */
 };
 
 int programmer_init(void)
@@ -523,6 +530,8 @@ 
 	FILE *image;
 	/* Probe for up to three flash chips. */
 	struct flashchip *flash, *flashes[3];
+	const char *name;
+	int namelen;
 	int opt;
 	int option_index = 0;
 	int force = 0;
@@ -565,6 +574,12 @@ 
 			printf_debug("%s\n", argv[i]);
 	}
 
+	/* Safety check. */
+	if (ARRAY_SIZE(programmer_table) - 1 != PROGRAMMER_INVALID) {
+		fprintf(stderr, "Programmer table miscompilation!\n");
+		exit(1);
+	}
+
 	setbuf(stdout, NULL);
 	while ((opt = getopt_long(argc, argv, "rRwvnVEfc:m:l:i:p:Lzh",
 				  long_options, &option_index)) != EOF) {
@@ -641,35 +656,28 @@ 
 			list_supported_wiki = 1;
 			break;
 		case 'p':
-			if (strncmp(optarg, "internal", 8) == 0) {
-				programmer = PROGRAMMER_INTERNAL;
-				if (optarg[8] == '=')
-					programmer_param = strdup(optarg + 9);
-			} else if (strncmp(optarg, "dummy", 5) == 0) {
-				programmer = PROGRAMMER_DUMMY;
-				if (optarg[5] == '=')
-					programmer_param = strdup(optarg + 6);
-			} else if (strncmp(optarg, "nic3com", 7) == 0) {
-				programmer = PROGRAMMER_NIC3COM;
-				if (optarg[7] == '=')
-					programmer_param = strdup(optarg + 8);
-			} else if (strncmp(optarg, "satasii", 7) == 0) {
-				programmer = PROGRAMMER_SATASII;
-				if (optarg[7] == '=')
-					programmer_param = strdup(optarg + 8);
-			} else if (strncmp(optarg, "it87spi", 7) == 0) {
-				programmer = PROGRAMMER_IT87SPI;
-				if (optarg[7] == '=')
-					programmer_param = strdup(optarg + 8);
-			} else if (strncmp(optarg, "ft2232spi", 9) == 0) {
-				programmer = PROGRAMMER_FT2232SPI;
-				if (optarg[9] == '=')
-					programmer_param = strdup(optarg + 10);
-			} else if (strncmp(optarg, "serprog", 7) == 0) {
-				programmer = PROGRAMMER_SERPROG;
-				if (optarg[7] == '=')
-					programmer_param = strdup(optarg + 8);
-			} else {
+			for (programmer = 0; programmer < PROGRAMMER_INVALID; programmer++) {
+				name = programmer_table[programmer].name;
+				namelen = strlen(name);
+				if (strncmp(optarg, name, namelen) == 0) {
+					switch (optarg[namelen]) {
+					case '=':
+						programmer_param = strdup(optarg + namelen + 1);
+						break;
+					case '\0':
+						break;
+					default:
+						/* The continue refers to the
+						 * for loop. It is here to be
+						 * able to differentiate between
+						 * foo and foobar.
+						 */
+						continue;
+					}
+					break;
+				}
+			}
+			if (programmer == PROGRAMMER_INVALID) {
 				printf("Error: Unknown programmer.\n");
 				exit(1);
 			}