Index: ChangeLog
===================================================================
RCS file: /cvs/ecos/ecos/packages/redboot/current/ChangeLog,v
retrieving revision 1.271
diff -u -r1.271 ChangeLog
--- ChangeLog	3 Apr 2009 09:32:24 -0000	1.271
+++ ChangeLog	3 Apr 2009 13:13:37 -0000
@@ -1,3 +1,9 @@
+2009-04-03  Rene Schipp von Branitz Nielsen <rbn@vitesse.com>
+
+	* src/flash.c: Add support for auto-detecting whether an
+	image in flash is gzipped or flat. Corresponding CDL option
+	is CYGOPT_REDBOOT_ZLIB_FLASH_AUTO_DETECT.
+
 2009-04-02  Rene Schipp von Branitz Nielsen <rbn@vitesse.com>
 
 	* src/flash.c: Fix compilation warnings when redundant FIS
Index: cdl/redboot.cdl
===================================================================
RCS file: /cvs/ecos/ecos/packages/redboot/current/cdl/redboot.cdl,v
retrieving revision 1.83
diff -u -r1.83 redboot.cdl
--- cdl/redboot.cdl	6 Feb 2009 15:39:29 -0000	1.83
+++ cdl/redboot.cdl	3 Apr 2009 13:13:37 -0000
@@ -225,6 +225,21 @@
                         to uncompress GZIP compressed data."
                  compile -library=libextras.a gunzip.c
             }
+
+            cdl_option CYGOPT_REDBOOT_ZLIB_FLASH_AUTO_DETECT {
+                 display       "Support auto-detection of compressed images"
+                 active_if     CYGPKG_REDBOOT_FLASH
+                 active_if     !CYGSEM_IO_FLASH_READ_INDIRECT
+                 flavor        bool
+                 default_value 0
+                 description   "
+                        Enable this option to support the -a option with
+                        the fis load command. When using the -a option,
+                        first a gzip/zlib decompression will be attempted.
+                        If that one fails, a simple copy of the image to
+                        RAM will be performed.
+                        Only used if CYGPRI_REDBOOT_ZLIB_FLASH is active."
+            }
         }
 
         cdl_option CYGBLD_BUILD_REDBOOT_WITH_XYZMODEM {
Index: doc/redboot_cmds.sgml
===================================================================
RCS file: /cvs/ecos/ecos/packages/redboot/current/doc/redboot_cmds.sgml,v
retrieving revision 1.20
diff -u -r1.20 redboot_cmds.sgml
--- doc/redboot_cmds.sgml	20 Mar 2009 08:32:11 -0000	1.20
+++ doc/redboot_cmds.sgml	3 Apr 2009 13:13:37 -0000
@@ -910,7 +910,7 @@
 Display contents of FLASH Image System [FIS]
   fis list [-c] [-d]
 Load image from FLASH Image System [FIS] into RAM
-  fis load [-d] [-b &lt;memory_load_address&gt;] [-c] name
+  fis load [-d] [-a] [-b &lt;memory_load_address&gt;] [-c] name
 Write raw data directly to FLASH
   fis write -f &lt;flash_addr&gt; -b &lt;mem_base&gt; -l &lt;image_length&gt;
 </screen> 
@@ -2408,6 +2408,7 @@
 	<arg>-b <replaceable> load address</replaceable></arg>
 	<arg>-c </arg>
 	<arg>-d </arg>
+	<arg>-a </arg>
 	<arg><replaceable>name</replaceable></arg>
       </cmdsynopsis>
     </refsynopsisdiv>
@@ -2452,6 +2453,14 @@
 	      flash to RAM.</entry>
 	    </row>
 	    <row>
+	      <entry>-a</entry>
+	      <entry></entry>
+	      <entry>Auto-detect if an image is gzipped, i.e.
+	      attempt to decompress image while copying it from
+	      flash to RAM. If this fails, attempt to load it
+	      as a flat image.</entry>
+	    </row>
+	    <row>
 	      <entry><replaceable>name</replaceable></entry>
 	      <entry>String</entry>
 	      <entry>The name of the file, as shown in the FIS
Index: src/flash.c
===================================================================
RCS file: /cvs/ecos/ecos/packages/redboot/current/src/flash.c,v
retrieving revision 1.89
diff -u -r1.89 flash.c
--- src/flash.c	3 Apr 2009 09:32:24 -0000	1.89
+++ src/flash.c	3 Apr 2009 13:13:38 -0000
@@ -100,6 +100,10 @@
 static char fis_load_usage[] = 
 #ifdef CYGPRI_REDBOOT_ZLIB_FLASH
                       "[-d] "
+
+#ifdef CYGOPT_REDBOOT_ZLIB_FLASH_AUTO_DETECT
+                      "[-a] "
+#endif
 #endif
                       "[-b <memory_load_address>] [-c] name";
 
@@ -1476,13 +1480,17 @@
     CYG_ADDRESS mem_addr;
     bool mem_addr_set = false;
     bool show_cksum = false;
-    struct option_info opts[3];
+    struct option_info opts[4];
 #if defined(CYGSEM_REDBOOT_FIS_CRC_CHECK)
     unsigned long cksum;
 #endif
     int num_options;
 #if defined(CYGPRI_REDBOOT_ZLIB_FLASH) ||  defined(CYGSEM_REDBOOT_FIS_CRC_CHECK)
     bool decompress = false;
+    int err = 0;
+#ifdef CYGOPT_REDBOOT_ZLIB_FLASH_AUTO_DETECT
+    bool decompress_auto_detect = false;
+#endif
 #endif
     cyg_flashaddr_t err_addr;
 
@@ -1495,6 +1503,11 @@
     init_opts(&opts[num_options], 'd', false, OPTION_ARG_TYPE_FLG, 
               (void *)&decompress, 0, "decompress");
     num_options++;
+#ifdef CYGOPT_REDBOOT_ZLIB_FLASH_AUTO_DETECT
+    init_opts(&opts[num_options], 'a', false, OPTION_ARG_TYPE_FLG, 
+              (void *)&decompress_auto_detect, 0, "auto-detect image format");
+    num_options++;
+#endif
 #endif
 
     CYG_ASSERT(num_options <= NUM_ELEMS(opts), "Too many options");
@@ -1519,8 +1532,17 @@
     }
 #endif
 #ifdef CYGPRI_REDBOOT_ZLIB_FLASH
+#ifdef CYGOPT_REDBOOT_ZLIB_FLASH_AUTO_DETECT
     if (decompress) {
-        int err;
+        // -d takes precedence over -a
+        decompress_auto_detect = false;
+    }
+#endif
+    if (decompress
+#ifdef CYGOPT_REDBOOT_ZLIB_FLASH_AUTO_DETECT
+        || decompress_auto_detect
+#endif
+    ) {
         _pipe_t fis_load_pipe;
         _pipe_t* p = &fis_load_pipe;
         p->out_buf = (unsigned char*) mem_addr;
@@ -1536,11 +1558,22 @@
         // Free used resources, do final translation of
         // error value.
         err = (*_dc_close)(p, err);
+#ifdef CYGOPT_REDBOOT_ZLIB_FLASH_AUTO_DETECT
+        if(!decompress_auto_detect || err == 0) {
+#endif
+            if (0 != err && p->msg) {
+                diag_printf("decompression error: %s\n", p->msg);
+            } else {
+                diag_printf("Image loaded from %p-%p\n", (unsigned char *)mem_addr, p->out_buf);
+            }
+#ifdef CYGOPT_REDBOOT_ZLIB_FLASH_AUTO_DETECT
+        }
+#endif
 
-        if (0 != err && p->msg) {
-            diag_printf("decompression error: %s\n", p->msg);
+        if(err != 0) {
+          entry_address = (unsigned long)NO_MEMORY;
         } else {
-            diag_printf("Image loaded from %p-%p\n", (unsigned char *)mem_addr, p->out_buf);
+          entry_address = (unsigned long)img->entry_point;
         }
 
         // Set load address/top
@@ -1549,8 +1582,13 @@
 
         // Reload fis directory
         fis_read_directory();
+#ifdef CYGOPT_REDBOOT_ZLIB_FLASH_AUTO_DETECT
+    }
+    if(!decompress && (!decompress_auto_detect || err != 0))
+#else
     } else // dangling block
 #endif
+#endif // CYGPRI_REDBOOT_ZLIB_FLASH
     {
         cyg_flash_read(img->flash_base, (void *)mem_addr, img->data_length, 
                        &err_addr);
@@ -1558,8 +1596,8 @@
         // Set load address/top
         load_address = mem_addr;
         load_address_end = mem_addr + img->data_length;
+        entry_address = (unsigned long)img->entry_point;
     }
-    entry_address = (unsigned long)img->entry_point;
 
 #ifdef CYGSEM_REDBOOT_FIS_CRC_CHECK
     cksum = cyg_crc32((unsigned char *)mem_addr, img->data_length);
@@ -1567,7 +1605,11 @@
         diag_printf("Checksum: 0x%08lx\n", cksum);
     }
     // When decompressing, leave CRC checking to decompressor
-    if (!decompress && img->file_cksum) {
+    if ((!decompress
+#ifdef CYGOPT_REDBOOT_ZLIB_FLASH_AUTO_DETECT    
+    && (!decompress_auto_detect || err)
+#endif
+    ) && img->file_cksum) {
         if (cksum != img->file_cksum) {
             diag_printf("** Warning - checksum failure.  stored: 0x%08lx, computed: 0x%08lx\n",
                         img->file_cksum, cksum);
