Index: src/flash/detectflash.c
===================================================================
--- src/flash/detectflash.c	(revision 1958)
+++ src/flash/detectflash.c	(working copy)
@@ -47,6 +47,18 @@
 
 urj_flash_cfi_array_t *urj_flash_cfi_array = NULL;
 
+int (*urj_flash_detect[]) (urj_bus_t *bus, uint32_t adr, 
+    urj_flash_cfi_array_t **cfi_array) = {
+    &urj_flash_cfi_detect,
+    &urj_flash_jedec_detect,
+    &urj_flash_amd_detect,
+#ifdef JEDEC_EXP
+    &urj_flash_jedec_exp_detect,
+#endif
+    NULL                        /* last must be NULL */
+};
+
+
 void
 urj_flash_cleanup (void)
 {
@@ -59,6 +71,7 @@
 {
     urj_flash_cfi_query_structure_t *cfi;
     const char *s;
+    int i;
 
     if (!bus)
     {
@@ -72,27 +85,12 @@
 
     URJ_BUS_PREPARE (bus);
 
-    if (urj_flash_cfi_detect (bus, adr, &urj_flash_cfi_array) != URJ_STATUS_OK)
-    {
-        urj_flash_cleanup();
-        if (urj_flash_jedec_detect (bus, adr, &urj_flash_cfi_array)
-            != URJ_STATUS_OK)
-        {
-            urj_flash_cleanup();
-            if (urj_flash_amd_detect (bus, adr, &urj_flash_cfi_array)
-                != URJ_STATUS_OK)
-            {
-                urj_flash_cleanup();
-#ifdef JEDEC_EXP
-                if (urj_flash_jedec_exp_detect (bus, adr, &urj_flash_cfi_array)
-                    != URJ_STATUS_OK)
-                {
-                    urj_flash_cleanup();
-                }
-#endif
-            }
-        }
-    }
+	for (i=0; urj_flash_detect[i] != NULL; i++) {
+		if (urj_flash_detect[i] (bus, adr, &urj_flash_cfi_array) == URJ_STATUS_OK) {
+			break;
+		}
+		urj_flash_cleanup ();
+	}
 
     if (urj_flash_cfi_array == NULL)
     {
