]> git.kernelconcepts.de Git - karo-tx-redboot.git/blobdiff - packages/redboot/v2_0/src/flash.c
WinCE redundant image support
[karo-tx-redboot.git] / packages / redboot / v2_0 / src / flash.c
index b3f0beaa4ab56514e3fbf6ff9ed61ec6deba6338..c72621367fb667cdda9e7be16935aa2b10c50bab 100644 (file)
 #include <cyg/io/flash.h>
 #include <fis.h>
 #include <sib.h>
-#include <cyg/infra/cyg_ass.h>        // assertion macros
+#include <cyg/infra/cyg_ass.h>                 // assertion macros
 
 #ifdef CYGBLD_BUILD_REDBOOT_WITH_WINCE_SUPPORT
 #include <wince.h>
+#include <winceinc.h>
 #endif
 
 #ifdef CYGSEM_REDBOOT_FLASH_COMBINED_FIS_AND_CONFIG
 extern void conf_endian_fixup(void *p);
 #endif
 
+#if defined(CYGBLD_BUILD_REDBOOT_WITH_WINCE_SUPPORT) && defined(CYGSEM_REDBOOT_FLASH_CONFIG)
+#include <flash_config.h>
+
+#if (REDBOOT_IMAGE_SIZE != CYGBLD_REDBOOT_MIN_IMAGE_SIZE)
+#error REDBOOT_IMAGE_SIZE != CYGBLD_REDBOOT_MIN_IMAGE_SIZE
+#endif
+
+RedBoot_config_option("automatic boot partition selection",
+                                       xt0_auto_OS_part_sel,
+                                       ALWAYS_ENABLED, true,
+                                       CONFIG_BOOL,
+                                       false
+       );
+
+RedBoot_config_option("first OS boot partition",
+                                       xt1_first_OS_part,
+                                       "xt0_auto_OS_part_sel", true,
+                                       CONFIG_STRING,
+                                       "osimage1"
+       );
+
+RedBoot_config_option("second OS boot partition",
+                                       xt2_second_OS_part,
+                                       "xt0_auto_OS_part_sel", true,
+                                       CONFIG_STRING,
+                                       "osimage2"
+       );
+
+#endif // CYGBLD_BUILD_REDBOOT_WITH_WINCE_SUPPORT && CYGSEM_REDBOOT_FLASH_CONFIG
+
 // Round a quantity up
 #define _rup(n,s) ((((n)+(s-1))/s)*s)
 
@@ -79,7 +110,7 @@ local_cmd_entry("init",
                "[-f]",
                fis_init,
                FIS_cmds
-    );
+       );
 #ifdef CYGSEM_REDBOOT_FIS_CRC_CHECK
 # define FIS_LIST_OPTS "[-c] [-d]"
 #else
@@ -90,52 +121,52 @@ local_cmd_entry("list",
                FIS_LIST_OPTS,
                fis_list,
                FIS_cmds
-    );
+       );
 local_cmd_entry("free",
                "Display free [available] locations within FLASH Image System [FIS]",
                "",
                fis_free,
                FIS_cmds
-    );
+       );
 local_cmd_entry("delete",
                "Delete an image from FLASH Image System [FIS]",
                "name",
                fis_delete,
                FIS_cmds
-    );
+       );
 
 static char fis_load_usage[] =
 #ifdef CYGPRI_REDBOOT_ZLIB_FLASH
-                     "[-d] "
+       "[-d] "
 #endif
-                     "[-b <memory_load_address>] [-c] "
+       "[-b <memory_load_address>] [-c] "
 #ifdef CYGBLD_BUILD_REDBOOT_WITH_WINCE_SUPPORT
-                     "[-w]"
+       "[-w]"
 #endif
-                     "name\n"
-                     "Options:\n"
+       "name\n"
+       "Options:\n"
 #ifdef CYGPRI_REDBOOT_ZLIB_FLASH
-                     "\t-d: decompress\n"
+       "\t-d: decompress\n"
 #endif
-                     "\t-c: print checksum\n"
+       "\t-c: print checksum\n"
 #ifdef CYGBLD_BUILD_REDBOOT_WITH_WINCE_SUPPORT
-                     "\t-w: load as Windows CE image\n"
+       "\t-w: load as Windows CE image\n"
 #endif
-                     ;
+       ;
 
 local_cmd_entry("load",
                "Load image from FLASH Image System [FIS] into RAM",
                fis_load_usage,
                fis_load,
                FIS_cmds
-    );
+       );
 local_cmd_entry("create",
                "Create an image",
                "[-b <mem_base>] [-l <image_length>] [-s <data_length>]\n"
                "      [-f <flash_addr>] [-e <entry_point>] [-r <ram_addr>] [-n] <name>",
                fis_create,
                FIS_cmds
-    );
+       );
 #endif
 
 // Raw flash access functions
@@ -144,27 +175,27 @@ local_cmd_entry("erase",
                "-f <flash_addr> -l <length>",
                fis_erase,
                FIS_cmds
-    );
+       );
 #ifdef CYGHWR_IO_FLASH_BLOCK_LOCKING
 local_cmd_entry("lock",
                "LOCK FLASH contents",
                "[-f <flash_addr> -l <length>] [name]",
                fis_lock,
                FIS_cmds
-    );
+       );
 local_cmd_entry("unlock",
                "UNLOCK FLASH contents",
                "[-f <flash_addr> -l <length>] [name]",
                fis_unlock,
                FIS_cmds
-    );
+       );
 #endif
 local_cmd_entry("write",
                "Write raw data directly to FLASH",
                "-f <flash_addr> -b <mem_base> -l <image_length>",
                fis_write,
                FIS_cmds
-    );
+       );
 
 // Define table boundaries
 CYG_HAL_TABLE_BEGIN( __FIS_cmds_TAB__, FIS_cmds);
@@ -175,11 +206,11 @@ extern struct cmd __FIS_cmds_TAB__[], __FIS_cmds_TAB_END__;
 // CLI function
 static cmd_fun do_fis;
 RedBoot_nested_cmd("fis",
-           "Manage FLASH images",
-           "{cmds}",
-           do_fis,
-           __FIS_cmds_TAB__, &__FIS_cmds_TAB_END__
-    );
+                               "Manage FLASH images",
+                               "{cmds}",
+                               do_fis,
+                               __FIS_cmds_TAB__, &__FIS_cmds_TAB_END__
+       );
 
 // Local data used by these routines
 void *flash_start, *flash_end;
@@ -201,15 +232,15 @@ extern struct _config *config;
 static void
 fis_usage(char *why)
 {
-    diag_printf("*** invalid 'fis' command: %s\n", why);
-    cmd_usage(__FIS_cmds_TAB__, &__FIS_cmds_TAB_END__, "fis ");
+       diag_printf("*** invalid 'fis' command: %s\n", why);
+       cmd_usage(__FIS_cmds_TAB__, &__FIS_cmds_TAB_END__, "fis ");
 }
 
 static void
 _show_invalid_flash_address(CYG_ADDRESS flash_addr, int stat)
 {
-    diag_printf("Invalid FLASH address %p: %s\n", (void *)flash_addr, flash_errmsg(stat));
-    diag_printf("   valid range is %p-%p\n", flash_start, flash_end);
+       diag_printf("Invalid FLASH address %p: %s\n", (void *)flash_addr, flash_errmsg(stat));
+       diag_printf("   valid range is %p-%p\n", flash_start, flash_end);
 }
 
 #ifdef CYGOPT_REDBOOT_FIS
@@ -219,10 +250,10 @@ _show_invalid_flash_address(CYG_ADDRESS flash_addr, int stat)
 static inline void fis_endian_fixup(void *addr)
 {
 #ifdef REDBOOT_FLASH_REVERSE_BYTEORDER
-    struct fis_image_desc *p = addr;
-    int cnt = fisdir_size / sizeof(struct fis_image_desc);
+       struct fis_image_desc *p = addr;
+       int cnt = fisdir_size / sizeof(struct fis_image_desc);
 
-    while (cnt-- > 0) {
+       while (cnt-- > 0) {
                p->flash_base = CYG_SWAP32(p->flash_base);
                p->mem_base = CYG_SWAP32(p->mem_base);
                p->size = CYG_SWAP32(p->size);
@@ -231,36 +262,36 @@ static inline void fis_endian_fixup(void *addr)
                p->desc_cksum = CYG_SWAP32(p->desc_cksum);
                p->file_cksum = CYG_SWAP32(p->file_cksum);
                p++;
-    }
+       }
 #endif
 }
 
 void
 fis_read_directory(void)
 {
-    void *err_addr;
+       void *err_addr;
 
-    FLASH_READ(fis_addr, fis_work_block, fisdir_size, &err_addr);
-    fis_endian_fixup(fis_work_block);
+       FLASH_READ(fis_addr, fis_work_block, fisdir_size, &err_addr);
+       fis_endian_fixup(fis_work_block);
 }
 
 struct fis_image_desc *
 fis_lookup(char *name, int *num)
 {
-    int i;
-    struct fis_image_desc *img;
+       int i;
+       struct fis_image_desc *img;
 
-    fis_read_directory();
+       fis_read_directory();
 
-    img = (struct fis_image_desc *)fis_work_block;
-    for (i = 0;         i < fisdir_size/sizeof(*img);  i++, img++) {
+       img = (struct fis_image_desc *)fis_work_block;
+       for (i = 0;      i < fisdir_size/sizeof(*img);  i++, img++) {
                if ((img->u.name[0] != 0xFF) &&
                        (strcasecmp(name, img->u.name) == 0)) {
                        if (num) *num = i;
                        return img;
                }
-    }
-    return NULL;
+       }
+       return NULL;
 }
 
 int fis_start_update_directory(int autolock)
@@ -288,7 +319,7 @@ int fis_start_update_directory(int autolock)
        img = fis_work_block;
 
        memcpy(img->u.valid_info.magic_name, CYG_REDBOOT_RFIS_VALID_MAGIC,
-                  CYG_REDBOOT_RFIS_VALID_MAGIC_LENGTH);
+               CYG_REDBOOT_RFIS_VALID_MAGIC_LENGTH);
        img->u.valid_info.valid_flag[0] = CYG_REDBOOT_RFIS_IN_PROGRESS;
        img->u.valid_info.valid_flag[1] = CYG_REDBOOT_RFIS_IN_PROGRESS;
        img->u.valid_info.version_count = img->u.valid_info.version_count+1;
@@ -347,13 +378,13 @@ fis_update_directory(int autolock, int error)
        } else {
                //success
                void *tmp_fis_addr = (void *)((CYG_ADDRESS)fis_addr +
-                                                                         CYG_REDBOOT_RFIS_VALID_MAGIC_LENGTH);
+                                                                       CYG_REDBOOT_RFIS_VALID_MAGIC_LENGTH);
 
                img->u.valid_info.valid_flag[0] = CYG_REDBOOT_RFIS_VALID;
                img->u.valid_info.valid_flag[1] = CYG_REDBOOT_RFIS_VALID;
 
                flash_program(tmp_fis_addr, img->u.valid_info.valid_flag,
-                                         sizeof(img->u.valid_info.valid_flag), &err_addr);
+                                       sizeof(img->u.valid_info.valid_flag), &err_addr);
        }
 #ifdef CYGHWR_IO_FLASH_BLOCK_LOCKING
        if (do_autolock)
@@ -361,37 +392,37 @@ fis_update_directory(int autolock, int error)
 #endif
 
 #else // CYGOPT_REDBOOT_REDUNDANT_FIS
-    int blk_size = fisdir_size;
-    int stat;
+       int blk_size = fisdir_size;
+       int stat;
 
-    fis_endian_fixup(fis_work_block);
+       fis_endian_fixup(fis_work_block);
 #ifdef CYGSEM_REDBOOT_FLASH_COMBINED_FIS_AND_CONFIG
-    memcpy((char *)fis_work_block+fisdir_size, config, cfg_size);
-    conf_endian_fixup((char *)fis_work_block+fisdir_size);
-    blk_size += cfg_size;
+       memcpy((char *)fis_work_block+fisdir_size, config, cfg_size);
+       conf_endian_fixup((char *)fis_work_block+fisdir_size);
+       blk_size += cfg_size;
 #endif
 #ifdef CYGHWR_IO_FLASH_BLOCK_LOCKING
-    if (do_autolock)
+       if (do_autolock)
                flash_unlock((void *)fis_addr, blk_size, &err_addr);
 #endif
-    if ((stat = flash_erase(fis_addr, flash_block_size, &err_addr)) != 0) {
+       if ((stat = flash_erase(fis_addr, flash_block_size, &err_addr)) != 0) {
                diag_printf("Error erasing FIS directory at %p: %s\n", err_addr, flash_errmsg(stat));
-    } else {
+       } else {
                if ((stat = FLASH_PROGRAM(fis_addr, fis_work_block,
-                                                                 flash_block_size, &err_addr)) != 0) {
+                                                                               flash_block_size, &err_addr)) != 0) {
                        diag_printf("Error writing FIS directory at %p: %s\n",
                                                err_addr, flash_errmsg(stat));
                }
-    }
-    fis_endian_fixup(fis_work_block);
+       }
+       fis_endian_fixup(fis_work_block);
 #ifdef CYGHWR_IO_FLASH_BLOCK_LOCKING
-    if (do_autolock)
+       if (do_autolock)
                flash_lock((void *)fis_addr, blk_size, &err_addr);
 #endif
 
 #endif // CYGOPT_REDBOOT_REDUNDANT_FIS
 
-    return 0;
+       return 0;
 }
 
 #ifdef CYGOPT_REDBOOT_REDUNDANT_FIS
@@ -401,14 +432,14 @@ fis_get_valid_buf(struct fis_image_desc *img0, struct fis_image_desc *img1, int
 {
        *update_was_interrupted = 0;
        if (strncmp(img1->u.valid_info.magic_name, CYG_REDBOOT_RFIS_VALID_MAGIC,
-                               CYG_REDBOOT_RFIS_VALID_MAGIC_LENGTH) != 0) {
+                                       CYG_REDBOOT_RFIS_VALID_MAGIC_LENGTH) != 0) {
                //buf0 must be valid
                if (img0->u.valid_info.version_count > 0) {
                        *update_was_interrupted = 1;
                }
                return 0;
        } else if (strncmp(img0->u.valid_info.magic_name, CYG_REDBOOT_RFIS_VALID_MAGIC,
-                                          CYG_REDBOOT_RFIS_VALID_MAGIC_LENGTH) != 0) {
+                                                       CYG_REDBOOT_RFIS_VALID_MAGIC_LENGTH) != 0) {
                //buf1 must be valid
                if (img1->u.valid_info.version_count > 0) {
                        *update_was_interrupted = 1;
@@ -422,7 +453,7 @@ fis_get_valid_buf(struct fis_image_desc *img0, struct fis_image_desc *img1, int
                *update_was_interrupted = 1;
                return 0;
        } else if ((img0->u.valid_info.valid_flag[0] != CYG_REDBOOT_RFIS_VALID) ||
-                          (img0->u.valid_info.valid_flag[1] != CYG_REDBOOT_RFIS_VALID)) {
+                       (img0->u.valid_info.valid_flag[1] != CYG_REDBOOT_RFIS_VALID)) {
                //buf1 must be valid
                *update_was_interrupted = 1;
                return 1;
@@ -435,23 +466,23 @@ fis_get_valid_buf(struct fis_image_desc *img0, struct fis_image_desc *img1, int
 void
 fis_erase_redundant_directory(void)
 {
-    int stat;
-    void *err_addr;
+       int stat;
+       void *err_addr;
 
 #ifdef CYGSEM_REDBOOT_FLASH_LOCK_SPECIAL
-    // Ensure [quietly] that the directory is unlocked before trying
-    // to update
-    flash_unlock((void *)redundant_fis_addr, fisdir_size,
-                &err_addr);
+       // Ensure [quietly] that the directory is unlocked before trying
+       // to update
+       flash_unlock((void *)redundant_fis_addr, fisdir_size,
+                               &err_addr);
 #endif
-    if ((stat = flash_erase(redundant_fis_addr, fisdir_size,
-                           &err_addr)) != 0) {
+       if ((stat = flash_erase(redundant_fis_addr, fisdir_size,
+                                                                       &err_addr)) != 0) {
                diag_printf("Error erasing FIS directory at %p: %s\n",
                                        err_addr, flash_errmsg(stat));
-    }
+       }
 #ifdef CYGSEM_REDBOOT_FLASH_LOCK_SPECIAL
-    // Ensure [quietly] that the directory is locked after the update
-    flash_lock((void *)redundant_fis_addr, fisdir_size, &err_addr);
+       // Ensure [quietly] that the directory is locked after the update
+       flash_lock((void *)redundant_fis_addr, fisdir_size, &err_addr);
 #endif
 }
 #endif
@@ -459,119 +490,119 @@ fis_erase_redundant_directory(void)
 static void
 fis_init(int argc, char *argv[])
 {
-    int stat;
-    struct fis_image_desc *img;
-    void *err_addr;
-    bool full_init = false;
-    struct option_info opts[1];
-    CYG_ADDRESS redboot_flash_start;
-    unsigned long redboot_image_size;
-
-    init_opts(&opts[0], 'f', false, OPTION_ARG_TYPE_FLG,
-                         &full_init, NULL, "full initialization, erases all of flash");
-    if (!scan_opts(argc, argv, 2, opts, 1, 0, 0, "")) {
+       int stat;
+       struct fis_image_desc *img;
+       void *err_addr;
+       bool full_init = false;
+       struct option_info opts[1];
+       CYG_ADDRESS redboot_flash_start;
+       unsigned long redboot_image_size;
+
+       init_opts(&opts[0], 'f', false, OPTION_ARG_TYPE_FLG,
+                       &full_init, NULL, "full initialization, erases all of flash");
+       if (!scan_opts(argc, argv, 2, opts, 1, 0, 0, "")) {
                return;
-    }
+       }
 
-    if (!verify_action("About to initialize [format] FLASH image system")) {
+       if (!verify_action("About to initialize [format] FLASH image system")) {
                diag_printf("** Aborted\n");
                return;
-    }
-    diag_printf("*** Initialize FLASH Image System\n");
+       }
+       diag_printf("*** Initialize FLASH Image System\n");
 
 #define MIN_REDBOOT_IMAGE_SIZE CYGBLD_REDBOOT_MIN_IMAGE_SIZE
-    redboot_image_size = flash_block_size > MIN_REDBOOT_IMAGE_SIZE ?
+       redboot_image_size = flash_block_size > MIN_REDBOOT_IMAGE_SIZE ?
                flash_block_size : MIN_REDBOOT_IMAGE_SIZE;
 
-    img = fis_work_block;
-    memset(img, 0xFF, fisdir_size);  // Start with erased data
+       img = fis_work_block;
+       memset(img, 0xFF, fisdir_size);  // Start with erased data
 
 #ifdef CYGOPT_REDBOOT_REDUNDANT_FIS
-    //create the valid flag entry
-    memset(img, 0, sizeof(struct fis_image_desc));
-    strcpy(img->u.valid_info.magic_name, CYG_REDBOOT_RFIS_VALID_MAGIC);
-    img->u.valid_info.valid_flag[0] = CYG_REDBOOT_RFIS_VALID;
-    img->u.valid_info.valid_flag[1] = CYG_REDBOOT_RFIS_VALID;
-    img->u.valid_info.version_count = 0;
-    img++;
+       //create the valid flag entry
+       memset(img, 0, sizeof(struct fis_image_desc));
+       strcpy(img->u.valid_info.magic_name, CYG_REDBOOT_RFIS_VALID_MAGIC);
+       img->u.valid_info.valid_flag[0] = CYG_REDBOOT_RFIS_VALID;
+       img->u.valid_info.valid_flag[1] = CYG_REDBOOT_RFIS_VALID;
+       img->u.valid_info.version_count = 0;
+       img++;
 #endif
 
-    // Create a pseudo image for RedBoot
+       // Create a pseudo image for RedBoot
 #ifdef CYGOPT_REDBOOT_FIS_RESERVED_BASE
-    memset(img, 0, sizeof(*img));
-    strcpy(img->u.name, "(reserved)");
-    img->flash_base = (CYG_ADDRESS)flash_start;
-    img->mem_base = (CYG_ADDRESS)flash_start;
-    img->size = CYGNUM_REDBOOT_FLASH_RESERVED_BASE;
-    img++;
-#endif
-    redboot_flash_start = (CYG_ADDRESS)flash_start + CYGBLD_REDBOOT_FLASH_BOOT_OFFSET;
+       memset(img, 0, sizeof(*img));
+       strcpy(img->u.name, "(reserved)");
+       img->flash_base = (CYG_ADDRESS)flash_start;
+       img->mem_base = (CYG_ADDRESS)flash_start;
+       img->size = CYGNUM_REDBOOT_FLASH_RESERVED_BASE;
+       img++;
+#endif
+       redboot_flash_start = (CYG_ADDRESS)flash_start + CYGBLD_REDBOOT_FLASH_BOOT_OFFSET;
 #ifdef CYGOPT_REDBOOT_FIS_REDBOOT
-    memset(img, 0, sizeof(*img));
-    strcpy(img->u.name, "RedBoot");
-    img->flash_base = redboot_flash_start;
-    img->mem_base = redboot_flash_start;
-    img->size = redboot_image_size;
-    img++;
-    redboot_flash_start += redboot_image_size;
+       memset(img, 0, sizeof(*img));
+       strcpy(img->u.name, "RedBoot");
+       img->flash_base = redboot_flash_start;
+       img->mem_base = redboot_flash_start;
+       img->size = redboot_image_size;
+       img++;
+       redboot_flash_start += redboot_image_size;
 #endif
 #ifdef CYGOPT_REDBOOT_FIS_REDBOOT_POST
 #ifdef CYGNUM_REDBOOT_FIS_REDBOOT_POST_OFFSET
-    // Take care to place the POST entry at the right offset:
-    redboot_flash_start = (CYG_ADDRESS)flash_start + CYGNUM_REDBOOT_FIS_REDBOOT_POST_OFFSET;
+       // Take care to place the POST entry at the right offset:
+       redboot_flash_start = (CYG_ADDRESS)flash_start + CYGNUM_REDBOOT_FIS_REDBOOT_POST_OFFSET;
 #endif
-    memset(img, 0, sizeof(*img));
-    strcpy(img->u.name, "RedBoot[post]");
-    img->flash_base = redboot_flash_start;
-    img->mem_base = redboot_flash_start;
-    img->size = redboot_image_size;
-    img++;
-    redboot_flash_start += redboot_image_size;
+       memset(img, 0, sizeof(*img));
+       strcpy(img->u.name, "RedBoot[post]");
+       img->flash_base = redboot_flash_start;
+       img->mem_base = redboot_flash_start;
+       img->size = redboot_image_size;
+       img++;
+       redboot_flash_start += redboot_image_size;
 #endif
 #ifdef CYGOPT_REDBOOT_FIS_REDBOOT_BACKUP
-    // And a backup image
-    memset(img, 0, sizeof(*img));
-    strcpy(img->u.name, "RedBoot[backup]");
-    img->flash_base = redboot_flash_start;
-    img->mem_base = redboot_flash_start;
-    img->size = redboot_image_size;
-    img++;
-    redboot_flash_start += redboot_image_size;
+       // And a backup image
+       memset(img, 0, sizeof(*img));
+       strcpy(img->u.name, "RedBoot[backup]");
+       img->flash_base = redboot_flash_start;
+       img->mem_base = redboot_flash_start;
+       img->size = redboot_image_size;
+       img++;
+       redboot_flash_start += redboot_image_size;
 #endif
 #if defined(CYGSEM_REDBOOT_FLASH_CONFIG) && defined(CYGHWR_REDBOOT_FLASH_CONFIG_MEDIA_FLASH)
-    // And a descriptor for the configuration data
-    memset(img, 0, sizeof(*img));
-    strcpy(img->u.name, "RedBoot config");
-    img->flash_base = (CYG_ADDRESS)cfg_base;
-    img->mem_base = (CYG_ADDRESS)cfg_base;
-    img->size = cfg_size;
-    img++;
-#endif
-    // And a descriptor for the descriptor table itself
-    memset(img, 0, sizeof(*img));
-    strcpy(img->u.name, "FIS directory");
-    img->flash_base = (CYG_ADDRESS)fis_addr;
-    img->mem_base = (CYG_ADDRESS)fis_addr;
-    img->size = fisdir_size;
-    img++;
-
-    //create the entry for the redundant fis table
+       // And a descriptor for the configuration data
+       memset(img, 0, sizeof(*img));
+       strcpy(img->u.name, "RedBoot config");
+       img->flash_base = (CYG_ADDRESS)cfg_base;
+       img->mem_base = (CYG_ADDRESS)cfg_base;
+       img->size = cfg_size;
+       img++;
+#endif
+       // And a descriptor for the descriptor table itself
+       memset(img, 0, sizeof(*img));
+       strcpy(img->u.name, "FIS directory");
+       img->flash_base = (CYG_ADDRESS)fis_addr;
+       img->mem_base = (CYG_ADDRESS)fis_addr;
+       img->size = fisdir_size;
+       img++;
+
+       //create the entry for the redundant fis table
 #ifdef CYGOPT_REDBOOT_REDUNDANT_FIS
-    memset(img, 0, sizeof(*img));
-    strcpy(img->u.name, "Redundant FIS");
-    img->flash_base = (CYG_ADDRESS)redundant_fis_addr;
-    img->mem_base = (CYG_ADDRESS)redundant_fis_addr;
-    img->size = fisdir_size;
-    img++;
+       memset(img, 0, sizeof(*img));
+       strcpy(img->u.name, "Redundant FIS");
+       img->flash_base = (CYG_ADDRESS)redundant_fis_addr;
+       img->mem_base = (CYG_ADDRESS)redundant_fis_addr;
+       img->size = fisdir_size;
+       img++;
 #endif
 
 #ifdef CYGOPT_REDBOOT_FIS_DIRECTORY_ARM_SIB_ID
-    // FIS gets the size of a full block - note, this should be changed
-    // if support is added for multi-block FIS structures.
-    img = (struct fis_image_desc *)((CYG_ADDRESS)fis_work_block + fisdir_size);
-    // Add a footer so the FIS will be recognized by the ARM Boot
-    // Monitor as a reserved area.
-    {
+       // FIS gets the size of a full block - note, this should be changed
+       // if support is added for multi-block FIS structures.
+       img = (struct fis_image_desc *)((CYG_ADDRESS)fis_work_block + fisdir_size);
+       // Add a footer so the FIS will be recognized by the ARM Boot
+       // Monitor as a reserved area.
+       {
                tFooter *footer_p = (tFooter*)((CYG_ADDRESS)img - sizeof(tFooter));
                cyg_uint32 check = 0;
                cyg_uint32 *check_ptr = (cyg_uint32 *)footer_p;
@@ -598,13 +629,13 @@ fis_init(int argc, char *argv[])
                        check += *check_ptr++;
                }
                footer_p->checksum = ~check;
-    }
+       }
 #endif
 
-    // Do this after creating the initialized table because that inherently
-    // calculates where the high water mark of default RedBoot images is.
+       // Do this after creating the initialized table because that inherently
+       // calculates where the high water mark of default RedBoot images is.
 
-    if (full_init) {
+       if (full_init) {
                unsigned long erase_size;
                CYG_ADDRESS erase_start;
                // Erase everything except default RedBoot images, fis block,
@@ -616,7 +647,7 @@ fis_init(int argc, char *argv[])
                if ( erase_size > erase_start ) {
                        erase_size -= erase_start;
                        if ((stat = flash_erase((void *)erase_start, erase_size,
-                                                                       &err_addr)) != 0) {
+                                                                                       &err_addr)) != 0) {
                                diag_printf("   initialization failed at %p: %s\n",
                                                        err_addr, flash_errmsg(stat));
                        }
@@ -626,16 +657,16 @@ fis_init(int argc, char *argv[])
                erase_start = redboot_flash_start; // high water of created images
                // Now the empty bits between the end of Redboot and the cfg and dir
                // blocks.
-#if defined(CYGSEM_REDBOOT_FLASH_CONFIG) && \
-    defined(CYGHWR_REDBOOT_FLASH_CONFIG_MEDIA_FLASH) && \
-    !defined(CYGSEM_REDBOOT_FLASH_COMBINED_FIS_AND_CONFIG)
+#if defined(CYGSEM_REDBOOT_FLASH_CONFIG) &&                                    \
+       defined(CYGHWR_REDBOOT_FLASH_CONFIG_MEDIA_FLASH) &&             \
+       !defined(CYGSEM_REDBOOT_FLASH_COMBINED_FIS_AND_CONFIG)
                if (fis_addr > cfg_base) {
                        erase_size = (CYG_ADDRESS)cfg_base - erase_start; // the gap between HWM and config data
                } else {
                        erase_size = (CYG_ADDRESS)fis_addr - erase_start; // the gap between HWM and fis data
                }
                if ((stat = flash_erase((void *)erase_start, erase_size,
-                                                               &err_addr)) != 0) {
+                                                                               &err_addr)) != 0) {
                        diag_printf("     initialization failed %p: %s\n",
                                                err_addr, flash_errmsg(stat));
                }
@@ -646,7 +677,7 @@ fis_init(int argc, char *argv[])
                        erase_size = (CYG_ADDRESS)cfg_base - erase_start; // the gap between fis and config data
                }
                if ((stat = flash_erase((void *)erase_start, erase_size,
-                                                               &err_addr)) != 0) {
+                                                                               &err_addr)) != 0) {
                        diag_printf("     initialization failed %p: %s\n",
                                                err_addr, flash_errmsg(stat));
                }
@@ -654,7 +685,7 @@ fis_init(int argc, char *argv[])
 #else  // !CYGSEM_REDBOOT_FLASH_CONFIG
                erase_size = (CYG_ADDRESS)fis_addr - erase_start; // the gap between HWM and fis data
                if ((stat = flash_erase((void *)erase_start, erase_size,
-                                                               &err_addr)) != 0) {
+                                                                               &err_addr)) != 0) {
                        diag_printf("     initialization failed %p: %s\n",
                                                err_addr, flash_errmsg(stat));
                }
@@ -664,7 +695,7 @@ fis_init(int argc, char *argv[])
                if ( erase_start < (((CYG_ADDRESS)flash_end)+1) ) {
                        erase_size = ((CYG_ADDRESS)flash_end - erase_start) + 1;
                        if ((stat = flash_erase((void *)erase_start, erase_size,
-                                                                       &err_addr)) != 0) {
+                                                                                       &err_addr)) != 0) {
                                diag_printf("   initialization failed at %p: %s\n",
                                                        err_addr, flash_errmsg(stat));
                        }
@@ -673,56 +704,56 @@ fis_init(int argc, char *argv[])
                // In this case, 'fis free' works by scanning for erased blocks.  Since the
                // "-f" option was not supplied, there may be areas which are not used but
                // don't appear to be free since they are not erased - thus the warning
-    } else {
+       } else {
                diag_printf("    Warning: device contents not erased, some blocks may not be usable\n");
 #endif
-    }
-    fis_start_update_directory(0);
-    fis_update_directory(0, 0);
+       }
+       fis_start_update_directory(0);
+       fis_update_directory(0, 0);
 #ifdef CYGOPT_REDBOOT_REDUNDANT_FIS
-    fis_erase_redundant_directory();
+       fis_erase_redundant_directory();
 #endif
 }
 
 static void
 fis_list(int argc, char *argv[])
 {
-    struct fis_image_desc *img;
-    int i = 0, image_indx;
-    bool show_cksums = false;
-    bool show_datalen = false;
-    struct option_info opts[2];
-    unsigned long last_addr, lowest_addr;
-    bool image_found;
+       struct fis_image_desc *img;
+       int i = 0, image_indx;
+       bool show_cksums = false;
+       bool show_datalen = false;
+       struct option_info opts[2];
+       unsigned long last_addr, lowest_addr;
+       bool image_found;
 
 #ifdef CYGHWR_REDBOOT_ARM_FLASH_SIB
-    // FIXME: this is somewhat half-baked
-    extern void arm_fis_list(void);
-    arm_fis_list();
-    return;
+       // FIXME: this is somewhat half-baked
+       extern void arm_fis_list(void);
+       arm_fis_list();
+       return;
 #endif
 
-    init_opts(&opts[i++], 'd', false, OPTION_ARG_TYPE_FLG,
-                         &show_datalen, NULL, "display data length");
+       init_opts(&opts[i++], 'd', false, OPTION_ARG_TYPE_FLG,
+                       &show_datalen, NULL, "display data length");
 #ifdef CYGSEM_REDBOOT_FIS_CRC_CHECK
-    init_opts(&opts[i++], 'c', false, OPTION_ARG_TYPE_FLG,
-                         &show_cksums, NULL, "display checksums");
+       init_opts(&opts[i++], 'c', false, OPTION_ARG_TYPE_FLG,
+                       &show_cksums, NULL, "display checksums");
 #endif
-    if (!scan_opts(argc, argv, 2, opts, i, 0, 0, "")) {
+       if (!scan_opts(argc, argv, 2, opts, i, 0, 0, "")) {
                return;
-    }
-    fis_read_directory();
+       }
+       fis_read_directory();
 
-    // Let diag_printf do the formatting in both cases, rather than counting
-    // cols by hand....
-    diag_printf("%-16s  %-10s  %-10s  %-10s  %-s\n",
+       // Let diag_printf do the formatting in both cases, rather than counting
+       // cols by hand....
+       diag_printf("%-16s  %-10s  %-10s  %-10s  %-s\n",
                                "Name","FLASH addr",
                                show_cksums ? "Checksum" : "Mem addr",
                                show_datalen ? "Datalen" : "Length",
                                "Entry point" );
-    last_addr = 0;
-    image_indx = 0;
-    do {
+       last_addr = 0;
+       image_indx = 0;
+       do {
                image_found = false;
                lowest_addr = 0xFFFFFFFF;
                img = fis_work_block;
@@ -750,70 +781,70 @@ fis_list(int argc, char *argv[])
                                                (unsigned long)img->entry_point);
                }
                last_addr = lowest_addr + 1;
-    } while (image_found == true);
+       } while (image_found == true);
 }
 
 #ifdef CYGDAT_REDBOOT_FIS_MAX_FREE_CHUNKS
 struct free_chunk {
-    CYG_ADDRESS start, end;
+       CYG_ADDRESS start, end;
 };
 
 static int
 find_free(struct free_chunk *chunks)
 {
-    CYG_ADDRESS *fis_ptr, *fis_end;
-    struct fis_image_desc *img;
-    int i, idx;
-    int num_chunks = 1;
-
-    // Do not search the area reserved for pre-RedBoot systems:
-    fis_ptr = (CYG_ADDRESS *)((CYG_ADDRESS)flash_start +
-                                                         CYGNUM_REDBOOT_FLASH_RESERVED_BASE);
-    fis_end = (CYG_ADDRESS *)flash_end;
-    chunks[num_chunks - 1].start = (CYG_ADDRESS)fis_ptr;
-    chunks[num_chunks - 1].end = (CYG_ADDRESS)fis_end;
-    fis_read_directory();
-    img = fis_work_block;
-    for (i = 0;  i < fisdir_size/sizeof(*img);  i++, img++) {
-       if (img->u.name[0] != 0xFF) {
-           // Figure out which chunk this is in and split it
-           for (idx = 0;  idx < num_chunks;  idx++) {
-               if ((img->flash_base >= chunks[idx].start) &&
-                   (img->flash_base <= chunks[idx].end)) {
-                   if (img->flash_base == chunks[idx].start) {
-                       chunks[idx].start += img->size;
-                       if (chunks[idx].start >= chunks[idx].end) {
-                           // This free chunk has collapsed
-                           while (idx < (num_chunks-1)) {
-                               chunks[idx] = chunks[idx+1];
-                               idx++;
-                           }
-                           num_chunks--;
-                       }
-                   } else if ((img->flash_base+img->size) == chunks[idx].end) {
-                       chunks[idx].end = img->flash_base;
-                   } else {
-                       // Split chunk into two parts
-                       if ((img->flash_base+img->size) < (CYG_ADDRESS)fis_end) {
-                           int j;
-                           // make room for new chunk
-                           for (j = num_chunks; j > idx + 1; j--)
-                               chunks[j] = chunks[j-1];
-                           chunks[idx+1].start = img->flash_base + img->size;
-                           chunks[idx+1].end = chunks[idx].end;
-                           if (++num_chunks == CYGDAT_REDBOOT_FIS_MAX_FREE_CHUNKS) {
-                               diag_printf("Warning: too many free chunks\n");
-                               return num_chunks;
-                           }
+       CYG_ADDRESS *fis_ptr, *fis_end;
+       struct fis_image_desc *img;
+       int i, idx;
+       int num_chunks = 1;
+
+       // Do not search the area reserved for pre-RedBoot systems:
+       fis_ptr = (CYG_ADDRESS *)((CYG_ADDRESS)flash_start +
+                                                       CYGNUM_REDBOOT_FLASH_RESERVED_BASE);
+       fis_end = (CYG_ADDRESS *)flash_end;
+       chunks[num_chunks - 1].start = (CYG_ADDRESS)fis_ptr;
+       chunks[num_chunks - 1].end = (CYG_ADDRESS)fis_end;
+       fis_read_directory();
+       img = fis_work_block;
+       for (i = 0;  i < fisdir_size/sizeof(*img);  i++, img++) {
+               if (img->u.name[0] != 0xFF) {
+                       // Figure out which chunk this is in and split it
+                       for (idx = 0;  idx < num_chunks;  idx++) {
+                               if ((img->flash_base >= chunks[idx].start) &&
+                                       (img->flash_base <= chunks[idx].end)) {
+                                       if (img->flash_base == chunks[idx].start) {
+                                               chunks[idx].start += img->size;
+                                               if (chunks[idx].start >= chunks[idx].end) {
+                                                       // This free chunk has collapsed
+                                                       while (idx < (num_chunks-1)) {
+                                                               chunks[idx] = chunks[idx+1];
+                                                               idx++;
+                                                       }
+                                                       num_chunks--;
+                                               }
+                                       } else if ((img->flash_base+img->size) == chunks[idx].end) {
+                                               chunks[idx].end = img->flash_base;
+                                       } else {
+                                               // Split chunk into two parts
+                                               if ((img->flash_base+img->size) < (CYG_ADDRESS)fis_end) {
+                                                       int j;
+                                                       // make room for new chunk
+                                                       for (j = num_chunks; j > idx + 1; j--)
+                                                               chunks[j] = chunks[j-1];
+                                                       chunks[idx+1].start = img->flash_base + img->size;
+                                                       chunks[idx+1].end = chunks[idx].end;
+                                                       if (++num_chunks == CYGDAT_REDBOOT_FIS_MAX_FREE_CHUNKS) {
+                                                               diag_printf("Warning: too many free chunks\n");
+                                                               return num_chunks;
+                                                       }
+                                               }
+                                               chunks[idx].end = img->flash_base;
+                                       }
+                                       break;
+                               }
                        }
-                       chunks[idx].end = img->flash_base;
-                   }
-                   break;
                }
-           }
        }
-    }
-    return num_chunks;
+       return num_chunks;
 }
 #endif // CYGDAT_REDBOOT_FIS_MAX_FREE_CHUNKS
 
@@ -821,17 +852,17 @@ static void
 fis_free(int argc, char *argv[])
 {
 #ifndef CYGDAT_REDBOOT_FIS_MAX_FREE_CHUNKS
-    unsigned long *fis_ptr, *fis_end, flash_data;
-    unsigned long *area_start;
-    void *err_addr;
+       unsigned long *fis_ptr, *fis_end, flash_data;
+       unsigned long *area_start;
+       void *err_addr;
 
        FLASH_Enable(flash_start, flash_end);
-    // Do not search the area reserved for pre-RedBoot systems:
-    fis_ptr = (unsigned long *)((CYG_ADDRESS)flash_start +
+       // Do not search the area reserved for pre-RedBoot systems:
+       fis_ptr = (unsigned long *)((CYG_ADDRESS)flash_start +
                                                                CYGNUM_REDBOOT_FLASH_RESERVED_BASE);
-    fis_end = (unsigned long *)(CYG_ADDRESS)flash_end;
-    area_start = fis_ptr;
-    while (fis_ptr < fis_end) {
+       fis_end = (unsigned long *)(CYG_ADDRESS)flash_end;
+       area_start = fis_ptr;
+       while (fis_ptr < fis_end) {
                flash_read(fis_ptr, &flash_data, sizeof(unsigned long), &err_addr);
                if (flash_data != 0xFFFFFFFF) {
                        if (area_start != fis_ptr) {
@@ -852,22 +883,22 @@ fis_free(int argc, char *argv[])
                } else {
                        fis_ptr += flash_block_size / sizeof(CYG_ADDRESS);
                }
-    }
-    if (area_start != fis_ptr) {
+       }
+       if (area_start != fis_ptr) {
                diag_printf("  0x%08x .. 0x%08x\n",
                                        (CYG_ADDRESS)area_start, (CYG_ADDRESS)fis_ptr);
-    }
+       }
        FLASH_Disable(flash_start, flash_end);
 #else
-    struct free_chunk chunks[CYGDAT_REDBOOT_FIS_MAX_FREE_CHUNKS];
-    int idx, num_chunks;
+       struct free_chunk chunks[CYGDAT_REDBOOT_FIS_MAX_FREE_CHUNKS];
+       int idx, num_chunks;
 
-    num_chunks = find_free(chunks);
-    for (idx = 0; idx < num_chunks; idx++) {
+       num_chunks = find_free(chunks);
+       for (idx = 0; idx < num_chunks; idx++) {
                diag_printf("  0x%08x .. 0x%08x\n",
                                        chunks[idx].start,
                                        chunks[idx].end);
-    }
+       }
 #endif
 }
 
@@ -876,17 +907,17 @@ static bool
 fis_find_free(CYG_ADDRESS *addr, unsigned long length)
 {
 #ifndef CYGDAT_REDBOOT_FIS_MAX_FREE_CHUNKS
-    unsigned long *fis_ptr, *fis_end, flash_data;
-    unsigned long *area_start;
-    void *err_addr;
+       unsigned long *fis_ptr, *fis_end, flash_data;
+       unsigned long *area_start;
+       void *err_addr;
 
-    // Do not search the area reserved for pre-RedBoot systems:
-    fis_ptr = (unsigned long *)((CYG_ADDRESS)flash_start +
+       // Do not search the area reserved for pre-RedBoot systems:
+       fis_ptr = (unsigned long *)((CYG_ADDRESS)flash_start +
                                                                CYGNUM_REDBOOT_FLASH_RESERVED_BASE);
-    fis_end = (unsigned long *)(CYG_ADDRESS)flash_end;
+       fis_end = (unsigned long *)(CYG_ADDRESS)flash_end;
        FLASH_Enable(fis_ptr, fis_end);
-    area_start = fis_ptr;
-    while (fis_ptr < fis_end) {
+       area_start = fis_ptr;
+       while (fis_ptr < fis_end) {
                flash_read(fis_ptr, &flash_data, sizeof(unsigned long), &err_addr);
                if (flash_data != 0xFFFFFFFF) {
                        if (area_start != fis_ptr) {
@@ -909,73 +940,73 @@ fis_find_free(CYG_ADDRESS *addr, unsigned long length)
                } else {
                        fis_ptr += flash_block_size / sizeof(CYG_ADDRESS);
                }
-    }
+       }
        FLASH_Disable((void *)((CYG_ADDRESS)flash_start +
-                                                  CYGNUM_REDBOOT_FLASH_RESERVED_BASE), fis_end);
-    if (area_start != fis_ptr) {
+                                                                       CYGNUM_REDBOOT_FLASH_RESERVED_BASE), fis_end);
+       if (area_start != fis_ptr) {
                if (fis_ptr - area_start >= length / sizeof(unsigned)) {
                        *addr = (CYG_ADDRESS)area_start;
                        return true;
                }
-    }
-    return false;
+       }
+       return false;
 #else
-    struct free_chunk chunks[CYGDAT_REDBOOT_FIS_MAX_FREE_CHUNKS];
-    int idx, num_chunks;
+       struct free_chunk chunks[CYGDAT_REDBOOT_FIS_MAX_FREE_CHUNKS];
+       int idx, num_chunks;
 
-    num_chunks = find_free(chunks);
-    for (idx = 0; idx < num_chunks; idx++) {
+       num_chunks = find_free(chunks);
+       for (idx = 0; idx < num_chunks; idx++) {
                if (chunks[idx].end + 1 - chunks[idx].start >= length) {
                        *addr = (CYG_ADDRESS)chunks[idx].start;
                        return true;
                }
-    }
-    return false;
+       }
+       return false;
 #endif
 }
 
 static void
 fis_create(int argc, char *argv[])
 {
-    int i, stat;
-    unsigned long length, img_size;
-    CYG_ADDRESS mem_addr, exec_addr, flash_addr, entry_addr;
-    char *name;
-    bool mem_addr_set = false;
-    bool exec_addr_set = false;
-    bool entry_addr_set = false;
-    bool flash_addr_set = false;
-    bool length_set = false;
-    bool img_size_set = false;
-    bool no_copy = false;
-    void *err_addr;
-    struct fis_image_desc *img = NULL;
-    bool defaults_assumed;
-    struct option_info opts[7];
-    bool prog_ok = true;
-
-    init_opts(&opts[0], 'b', true, OPTION_ARG_TYPE_NUM,
-                         &mem_addr, &mem_addr_set, "memory base address");
-    init_opts(&opts[1], 'r', true, OPTION_ARG_TYPE_NUM,
-                         &exec_addr, &exec_addr_set, "ram base address");
-    init_opts(&opts[2], 'e', true, OPTION_ARG_TYPE_NUM,
-                         &entry_addr, &entry_addr_set, "entry point address");
-    init_opts(&opts[3], 'f', true, OPTION_ARG_TYPE_NUM,
-                         &flash_addr, &flash_addr_set, "FLASH memory base address");
-    init_opts(&opts[4], 'l', true, OPTION_ARG_TYPE_NUM,
-                         &length, &length_set, "image length [in FLASH]");
-    init_opts(&opts[5], 's', true, OPTION_ARG_TYPE_NUM,
-                         &img_size, &img_size_set, "image size [actual data]");
-    init_opts(&opts[6], 'n', false, OPTION_ARG_TYPE_FLG,
-                         &no_copy, NULL, "don't copy from RAM to FLASH, just update directory");
-    if (!scan_opts(argc, argv, 2, opts, 7, &name, OPTION_ARG_TYPE_STR, "file name")) {
+       int i, stat;
+       unsigned long length, img_size;
+       CYG_ADDRESS mem_addr, exec_addr, flash_addr, entry_addr;
+       char *name;
+       bool mem_addr_set = false;
+       bool exec_addr_set = false;
+       bool entry_addr_set = false;
+       bool flash_addr_set = false;
+       bool length_set = false;
+       bool img_size_set = false;
+       bool no_copy = false;
+       void *err_addr;
+       struct fis_image_desc *img = NULL;
+       bool defaults_assumed;
+       struct option_info opts[7];
+       bool prog_ok = true;
+
+       init_opts(&opts[0], 'b', true, OPTION_ARG_TYPE_NUM,
+                       &mem_addr, &mem_addr_set, "memory base address");
+       init_opts(&opts[1], 'r', true, OPTION_ARG_TYPE_NUM,
+                       &exec_addr, &exec_addr_set, "ram base address");
+       init_opts(&opts[2], 'e', true, OPTION_ARG_TYPE_NUM,
+                       &entry_addr, &entry_addr_set, "entry point address");
+       init_opts(&opts[3], 'f', true, OPTION_ARG_TYPE_NUM,
+                       &flash_addr, &flash_addr_set, "FLASH memory base address");
+       init_opts(&opts[4], 'l', true, OPTION_ARG_TYPE_NUM,
+                       &length, &length_set, "image length [in FLASH]");
+       init_opts(&opts[5], 's', true, OPTION_ARG_TYPE_NUM,
+                       &img_size, &img_size_set, "image size [actual data]");
+       init_opts(&opts[6], 'n', false, OPTION_ARG_TYPE_FLG,
+                       &no_copy, NULL, "don't copy from RAM to FLASH, just update directory");
+       if (!scan_opts(argc, argv, 2, opts, 7, &name, OPTION_ARG_TYPE_STR, "file name")) {
                fis_usage("invalid arguments");
                return;
        }
 
-    fis_read_directory();
-    defaults_assumed = false;
-    if (name) {
+       fis_read_directory();
+       defaults_assumed = false;
+       if (name) {
                // Search existing files to acquire defaults for params not specified:
                img = fis_lookup(name, NULL);
                if (img) {
@@ -986,8 +1017,8 @@ fis_create(int argc, char *argv[])
                                defaults_assumed = true;
                        }
                }
-    }
-    if (!mem_addr_set && (load_address >= (CYG_ADDRESS)ram_start) &&
+       }
+       if (!mem_addr_set && (load_address >= (CYG_ADDRESS)ram_start) &&
                (load_address_end) < (CYG_ADDRESS)ram_end) {
                mem_addr = load_address;
                mem_addr_set = true;
@@ -1004,9 +1035,9 @@ fis_create(int argc, char *argv[])
                        img_size = load_address_end - load_address;
                        img_size_set = true;
                }
-    }
-    // Get the remaining fall-back values from the fis
-    if (img) {
+       }
+       // Get the remaining fall-back values from the fis
+       if (img) {
                if (!exec_addr_set) {
                        // Preserve "normal" behaviour
                        exec_addr_set = true;
@@ -1017,42 +1048,42 @@ fis_create(int argc, char *argv[])
                        flash_addr = img->flash_base;
                        defaults_assumed = true;
                }
-    }
+       }
 
-    if ((!no_copy && !mem_addr_set) || (no_copy && !flash_addr_set) ||
+       if ((!no_copy && !mem_addr_set) || (no_copy && !flash_addr_set) ||
                !length_set || !name) {
                fis_usage("required parameter missing");
                return;
-    }
-    if (!img_size_set) {
+       }
+       if (!img_size_set) {
                img_size = length;
-    }
-    // 'length' is size of FLASH image, 'img_size' is actual data size
-    // Round up length to FLASH block size
+       }
+       // 'length' is size of FLASH image, 'img_size' is actual data size
+       // Round up length to FLASH block size
 #ifndef CYGPKG_HAL_MIPS // FIXME: compiler is b0rken
-    length = ((length + flash_block_size - 1) / flash_block_size) * flash_block_size;
-    if (length < img_size) {
+       length = ((length + flash_block_size - 1) / flash_block_size) * flash_block_size;
+       if (length < img_size) {
                diag_printf("Invalid FLASH image size/length combination\n");
                return;
-    }
+       }
 #endif
-    if (flash_addr_set &&
+       if (flash_addr_set &&
                ((stat = flash_verify_addr((void *)flash_addr)) ||
-                (stat = flash_verify_addr((void *)(flash_addr + length - 1))))) {
+                       (stat = flash_verify_addr((void *)(flash_addr + length - 1))))) {
                _show_invalid_flash_address(flash_addr, stat);
                return;
-    }
-    if (flash_addr_set && ((flash_addr & (flash_block_size - 1)) != 0)) {
+       }
+       if (flash_addr_set && ((flash_addr & (flash_block_size - 1)) != 0)) {
                diag_printf("Invalid FLASH address: %p\n", (void *)flash_addr);
                diag_printf("   must be 0x%x aligned\n", flash_block_size);
                return;
-    }
-    if (strlen(name) >= sizeof(img->u.name)) {
+       }
+       if (strlen(name) >= sizeof(img->u.name)) {
                diag_printf("Name is too long, must be less than %d chars\n",
                                        sizeof(img->u.name));
                return;
-    }
-    if (!no_copy) {
+       }
+       if (!no_copy) {
                if ((mem_addr < (CYG_ADDRESS)ram_start) ||
                        ((mem_addr+img_size) >= (CYG_ADDRESS)ram_end)) {
                        diag_printf("** WARNING: RAM address: %p may be invalid\n", (void *)mem_addr);
@@ -1062,9 +1093,9 @@ fis_create(int argc, char *argv[])
                        diag_printf("Can't locate %lx(%ld) bytes free in FLASH\n", length, length);
                        return;
                }
-    }
-    // First, see if the image by this name has agreable properties
-    if (img) {
+       }
+       // First, see if the image by this name has agreable properties
+       if (img) {
                if (flash_addr_set && (img->flash_base != flash_addr)) {
                        diag_printf("Image found, but flash address (%p)\n"
                                                "             is incorrect (present image location %p)\n",
@@ -1084,14 +1115,14 @@ fis_create(int argc, char *argv[])
                        if (defaults_assumed) {
                                if (no_copy &&
                                        !verify_action("* CAUTION * about to program '%s'\n                at %p..%p from %p",
-                                                                  name, (void *)flash_addr,
-                                                                  (void *)(flash_addr + img_size - 1),
-                                                                  (void *)mem_addr)) {
+                                                               name, (void *)flash_addr,
+                                                               (void *)(flash_addr + img_size - 1),
+                                                               (void *)mem_addr)) {
                                        return;  // The guy gave up
                                }
                        }
                }
-    } else {
+       } else {
 #ifdef CYGDAT_REDBOOT_FIS_MAX_FREE_CHUNKS
                // Make sure that any FLASH address specified directly is truly free
                if (flash_addr_set && !no_copy) {
@@ -1123,11 +1154,11 @@ fis_create(int argc, char *argv[])
                        diag_printf("Can't find an empty slot in FIS directory!\n");
                        return;
                }
-    }
-    if (!no_copy) {
+       }
+       if (!no_copy) {
                // Safety check - make sure the address range is not within the code we're running
                if (flash_code_overlaps((void *)flash_addr,
-                                                               (void *)(flash_addr + img_size - 1))) {
+                                                                       (void *)(flash_addr + img_size - 1))) {
                        diag_printf("Can't program this region - contains code in use!\n");
                        return;
                }
@@ -1145,15 +1176,15 @@ fis_create(int argc, char *argv[])
                        // Now program it
                        FLASH_Enable((void *)flash_addr, (void *)(flash_addr + length));
                        stat = FLASH_PROGRAM((void *)flash_addr, (void *)mem_addr,
-                                                                img_size, &err_addr);
+                                                               img_size, &err_addr);
                        FLASH_Disable((void *)flash_addr, (void *)(flash_addr + length));
                        if (stat != 0) {
                                diag_printf("Can't program region at %p: %s\n", err_addr, flash_errmsg(stat));
                                prog_ok = false;
                        }
                }
-    }
-    if (prog_ok) {
+       }
+       if (prog_ok) {
                // Update directory
                memset(img, 0, sizeof(*img));
                strcpy(img->u.name, name);
@@ -1172,150 +1203,223 @@ fis_create(int argc, char *argv[])
 #endif
                fis_start_update_directory(0);
                fis_update_directory(0, 0);
-    }
+       }
 }
 
 extern void arm_fis_delete(char *);
 static void
 fis_delete(int argc, char *argv[])
 {
-    char *name;
-    int num_reserved, i, stat;
-    void *err_addr;
-    struct fis_image_desc *img;
+       char *name;
+       int num_reserved, i, stat;
+       void *err_addr;
+       struct fis_image_desc *img;
 
-    if (!scan_opts(argc, argv, 2, 0, 0, &name, OPTION_ARG_TYPE_STR, "image name")) {
+       if (!scan_opts(argc, argv, 2, 0, 0, &name, OPTION_ARG_TYPE_STR, "image name")) {
                fis_usage("invalid arguments");
                return;
        }
 #ifdef CYGHWR_REDBOOT_ARM_FLASH_SIB
-    // FIXME: this is somewhat half-baked
-    arm_fis_delete(name);
-    return;
+       // FIXME: this is somewhat half-baked
+       arm_fis_delete(name);
+       return;
 #endif
-    img = (struct fis_image_desc *)fis_work_block;
-    num_reserved = 0;
+       img = (struct fis_image_desc *)fis_work_block;
+       num_reserved = 0;
 #ifdef CYGOPT_REDBOOT_FIS_RESERVED_BASE
-    num_reserved++;
+       num_reserved++;
 #endif
 #ifdef CYGOPT_REDBOOT_FIS_REDBOOT
-    num_reserved++;
+       num_reserved++;
 #endif
 #ifdef CYGOPT_REDBOOT_FIS_REDBOOT_BACKUP
-    num_reserved++;
+       num_reserved++;
 #endif
 #ifdef CYGOPT_REDBOOT_FIS_REDBOOT_POST
-    num_reserved++;
+       num_reserved++;
 #endif
 #if defined(CYGSEM_REDBOOT_FLASH_CONFIG) && defined(CYGHWR_REDBOOT_FLASH_CONFIG_MEDIA_FLASH)
-    num_reserved++;
+       num_reserved++;
 #endif
 #if 1 // And the descriptor for the descriptor table itself
-    num_reserved++;
+       num_reserved++;
 #endif
 
-    img = fis_lookup(name, &i);
-    if (img) {
-       if (i < num_reserved) {
-           diag_printf("Sorry, '%s' is a reserved image and cannot be deleted\n", img->u.name);
-           return;
-       }
-       if (!verify_action("Delete image '%s'", name)) {
-           return;
-       }
-    } else {
+       img = fis_lookup(name, &i);
+       if (img) {
+               if (i < num_reserved) {
+                       diag_printf("Sorry, '%s' is a reserved image and cannot be deleted\n", img->u.name);
+                       return;
+               }
+               if (!verify_action("Delete image '%s'", name)) {
+                       return;
+               }
+       } else {
                diag_printf("No image '%s' found\n", name);
-       return;
-    }
-    // Erase Data blocks (free space)
-    if ((stat = flash_erase((void *)img->flash_base, img->size, &err_addr)) != 0) {
+               return;
+       }
+       // Erase Data blocks (free space)
+       if ((stat = flash_erase((void *)img->flash_base, img->size, &err_addr)) != 0) {
                diag_printf("Error erasing at %p: %s\n", err_addr, flash_errmsg(stat));
-    } else {
+       } else {
                img->u.name[0] = 0xFF;
                fis_start_update_directory(0);
                fis_update_directory(0, 0);
-    }
+       }
+}
+
+#ifdef CYGBLD_BUILD_REDBOOT_WITH_WINCE_SUPPORT
+static bool load_ce_img(struct fis_image_desc *img)
+{
+       bool ret = true;
+
+       FLASH_Enable((void *)img->flash_base, (void *)(img->flash_base + img->size));
+       if (!ce_is_bin_image((void *)img->flash_base, img->size)) {
+               diag_printf("** Error: This does not seem to be a valid Windows CE image\n");
+               ret =  false;
+       }
+       if (ret) {
+               if (!ce_bin_load((void *)img->flash_base, img->size)) {
+                       diag_printf("** Error: Failed to load Windows CE image\n");
+                       ret =  false;
+               }
+       }
+       FLASH_Disable((void *)img->flash_base, (void *)(img->flash_base + img->size));
+       return ret;
+}
+
+static void store_boot_img_name(char *image_name)
+{
+
+       ce_std_driver_globals *std_drv_glb = &_KARO_CECFG_START;
+
+       strncpy(std_drv_glb->chBootPartition, image_name, sizeof(std_drv_glb->chBootPartition));
+
+       std_drv_glb->chBootPartition[15] = '\0';
 }
+#endif
 
 static void
 fis_load(int argc, char *argv[])
 {
-    char *name;
-    struct fis_image_desc *img;
-    CYG_ADDRESS mem_addr;
-    bool mem_addr_set = false;
-    bool show_cksum = false;
-    bool load_ce = false;
-    struct option_info opts[4];
+       char *name = NULL;
+       struct fis_image_desc *img = NULL;
+       CYG_ADDRESS mem_addr;
+       bool mem_addr_set = false;
+       bool show_cksum = false;
+#ifdef CYGBLD_BUILD_REDBOOT_WITH_WINCE_SUPPORT
+       bool load_ce = false;
+#else
+#define load_ce                0
+#endif
+       bool auto_part_name = false;
+       struct option_info opts[4];
 #if defined(CYGSEM_REDBOOT_FIS_CRC_CHECK)
-    unsigned long cksum;
+       unsigned long cksum;
 #endif
-    int num_options = 0;
+       int num_options = 0;
 #if defined(CYGPRI_REDBOOT_ZLIB_FLASH) ||  defined(CYGSEM_REDBOOT_FIS_CRC_CHECK)
-    bool decompress = false;
+       bool decompress = false;
 #endif
-    void *err_addr;
+       void *err_addr;
 
-    init_opts(&opts[num_options++], 'b', true, OPTION_ARG_TYPE_NUM,
-                         &mem_addr, &mem_addr_set, "memory [load] base address");
-    init_opts(&opts[num_options++], 'c', false, OPTION_ARG_TYPE_FLG,
-                         &show_cksum, NULL, "display checksum");
+       init_opts(&opts[num_options++], 'b', true, OPTION_ARG_TYPE_NUM,
+                       &mem_addr, &mem_addr_set, "memory [load] base address");
+       init_opts(&opts[num_options++], 'c', false, OPTION_ARG_TYPE_FLG,
+                       &show_cksum, NULL, "display checksum");
 #ifdef CYGBLD_BUILD_REDBOOT_WITH_WINCE_SUPPORT
-    init_opts(&opts[num_options++], 'w', false, OPTION_ARG_TYPE_FLG,
-                         &load_ce, NULL, "parse as Windows CE image");
+       init_opts(&opts[num_options++], 'w', false, OPTION_ARG_TYPE_FLG,
+                       &load_ce, NULL, "parse as Windows CE image");
 #endif
 #ifdef CYGPRI_REDBOOT_ZLIB_FLASH
-    init_opts(&opts[num_options++], 'd', false, OPTION_ARG_TYPE_FLG,
-                         &decompress, 0, "decompress");
+       init_opts(&opts[num_options++], 'd', false, OPTION_ARG_TYPE_FLG,
+                       &decompress, 0, "decompress");
 #endif
 
-    CYG_ASSERT(num_options <= num_options, "Too many options");
+       CYG_ASSERT(num_options <= NUM_ELEMS(opts), "Too many options");
 
-    if (!scan_opts(argc, argv, 2, opts, num_options, &name,
-                                  OPTION_ARG_TYPE_STR, "image name")) {
+       if (!scan_opts(argc, argv, 2, opts, num_options, &name,
+                                       OPTION_ARG_TYPE_STR, "image name")) {
                fis_usage("invalid arguments");
                return;
-    }
-    if ((img = fis_lookup(name, NULL)) == NULL) {
-               diag_printf("No image '%s' found\n", name);
-               return;
-    }
-    if (!mem_addr_set || load_ce) {
-               mem_addr = img->mem_base;
-    }
-    // Load image from FLASH into RAM
-#ifdef CYGSEM_REDBOOT_VALIDATE_USER_RAM_LOADS
-    if (!valid_address((void *)mem_addr)) {
-               if (!load_ce) {
-                       diag_printf("Not a loadable image - try using -b ADDRESS option\n");
+       }
+
+#ifdef CYGBLD_BUILD_REDBOOT_WITH_WINCE_SUPPORT
+       // check if the name of the OS partition should be chosen automatically
+       if (load_ce) {
+               if (!flash_get_config("xt0_auto_OS_part_sel", &auto_part_name, CONFIG_BOOL)) {
+                       diag_printf("fconfig variable 'xt0_auto_OS_part_sel' not found; use 'fconfig -i' to create it\n");
+                       return;
+               }
+       }
+#endif
+       if (name) {
+               if ((img = fis_lookup(name, NULL)) == NULL) {
+                       if (!auto_part_name) {
+                               diag_printf("No image '%s' found\n", name);
+                               return;
+                       }
                } else {
-                       diag_printf("Not a loadable image\n");
+                       if (!mem_addr_set && !load_ce) {
+                               mem_addr = img->mem_base;
+                       }
                }
+       }
+       // Load image from FLASH into RAM
+#ifdef CYGSEM_REDBOOT_VALIDATE_USER_RAM_LOADS
+       // this does not make sense for WINCE as the load address is stored in the descriptor
+       // inside the image itself. It does not matter what load address is specified in the FIS directory
+       if (!load_ce && !valid_address((void *)mem_addr)) {
+               diag_printf("Not a loadable image - try using -b ADDRESS option\n");
                return;
-    }
+       }
 #endif
 #ifdef CYGBLD_BUILD_REDBOOT_WITH_WINCE_SUPPORT
-    if (load_ce) {
+       if (load_ce) {
                if (mem_addr_set) {
                        diag_printf("Warning: -b argument ignored for Windows CE image\n");
                }
-               FLASH_Enable((void *)img->flash_base, (void *)(img->flash_base + img->size));
-               if (!ce_is_bin_image((void *)img->flash_base, img->size)) {
-                       diag_printf("** Error: This does not seem to be a valid Windows CE image\n");
-                       return;
-               }
-               if (!ce_bin_load((void *)img->flash_base, img->size)) {
-                       diag_printf("** Error: Failed to load Windows CE image\n");
+               if (!auto_part_name) {
+                       if (!load_ce_img(img))
+                               return;
+               } else {
+                       if (!img || !load_ce_img(img)) {
+                               // try the first OS image
+                               if (!flash_get_config("xt1_first_OS_part", &name, CONFIG_STRING)) {
+                                       diag_printf("fconfig variable 'xt1_first_OS_part' not found; use 'fconfig -i' to create it\n");
+                               }
+                               if (name) {
+                                       if ((img = fis_lookup(name, NULL)) == NULL) {
+                                               diag_printf("No image '%s' found\n", name);
+                                       } else {
+                                               diag_printf("loading WINCE OS from partition '%s'\n", name);
+                                       }
+                               }
+                               if (!img || !load_ce_img(img)) {
+                                       if (!flash_get_config("xt2_second_OS_part", &name, CONFIG_STRING)) {
+                                               diag_printf("fconfig variable 'xt2_second_OS_part' not found; use 'fconfig -i' to create it\n");
+                                               return;
+                                       }
+                                       if ((img = fis_lookup(name, NULL)) == NULL) {
+                                               diag_printf("No image '%s' found\n", name);
+                                               return;
+                                       } else {
+                                               diag_printf("loading WINCE OS from partition '%s'\n", name);
+                                       }
+                                       if (!load_ce_img(img))
+                                               return;
+                               }
+                       }
+                       store_boot_img_name(name);
                }
-               FLASH_Disable((void *)img->flash_base, (void *)(img->flash_base + img->size));
                // Set load address/top
                load_address = mem_addr;
                load_address_end = mem_addr + img->data_length;
                return;
-    }
+       }
 #endif
 #ifdef CYGPRI_REDBOOT_ZLIB_FLASH
-    if (decompress) {
+       if (decompress) {
                int err;
                _pipe_t fis_load_pipe;
                _pipe_t *p = &fis_load_pipe;
@@ -1345,41 +1449,41 @@ fis_load(int argc, char *argv[])
 
                // Reload fis directory
                fis_read_directory();
-    } else // dangling block
+       } else // dangling block
 #endif
-               {
-                       int err;
-
-                       FLASH_Enable((void *)img->flash_base, (void *)(img->flash_base + img->size));
-                       err = flash_read((void *)img->flash_base, (void *)mem_addr,
-                                                        img->data_length, &err_addr);
-                       FLASH_Disable((void *)img->flash_base, (void *)(img->flash_base + img->size));
-                       if (err != FLASH_ERR_OK) {
-                               diag_printf("** Error: Failed to load image from flash\n");
-                               return;
-                       }
+       {
+               int err;
 
-                       // Set load address/top
-                       load_address = mem_addr;
-                       load_address_end = mem_addr + img->data_length;
+               FLASH_Enable((void *)img->flash_base, (void *)(img->flash_base + img->size));
+               err = flash_read((void *)img->flash_base, (void *)mem_addr,
+                                               img->data_length, &err_addr);
+               FLASH_Disable((void *)img->flash_base, (void *)(img->flash_base + img->size));
+               if (err != FLASH_ERR_OK) {
+                       diag_printf("** Error: Failed to load image from flash\n");
+                       return;
                }
-    entry_address = (unsigned long)img->entry_point;
+
+               // Set load address/top
+               load_address = mem_addr;
+               load_address_end = mem_addr + img->data_length;
+       }
+       entry_address = (unsigned long)img->entry_point;
 
 #ifdef CYGSEM_REDBOOT_FIS_CRC_CHECK
-    cksum = cyg_crc32((unsigned char *)mem_addr, img->data_length);
-    if (show_cksum) {
+       cksum = cyg_crc32((unsigned char *)mem_addr, img->data_length);
+       if (show_cksum) {
                diag_printf("Checksum: 0x%08lx\n", cksum);
-    }
-    // When decompressing, leave CRC checking to decompressor
-    if (!decompress && img->file_cksum) {
+       }
+       // When decompressing, leave CRC checking to decompressor
+       if (!decompress && img->file_cksum) {
                if (cksum != img->file_cksum) {
                        diag_printf("** Warning - checksum failure.      stored: 0x%08lx, computed: 0x%08lx\n",
                                                img->file_cksum, cksum);
                        entry_address = (unsigned long)NO_MEMORY;
                }
-    }
+       }
 #endif
-    diag_printf("image loaded 0x%08lx-0x%08lx, assumed entry at 0x%08lx\n",
+       diag_printf("image loaded 0x%08lx-0x%08lx, assumed entry at 0x%08lx\n",
                                load_address, load_address_end - 1, entry_address);
 }
 #endif // CYGOPT_REDBOOT_FIS
@@ -1387,125 +1491,125 @@ fis_load(int argc, char *argv[])
 static void
 fis_write(int argc, char *argv[])
 {
-    int stat;
-    unsigned long length;
-    CYG_ADDRESS mem_addr, flash_addr;
-    bool mem_addr_set = false;
-    bool flash_addr_set = false;
-    bool length_set = false;
-    void *err_addr;
-    struct option_info opts[3];
-    bool prog_ok;
-
-    init_opts(&opts[0], 'b', true, OPTION_ARG_TYPE_NUM,
-                         &mem_addr, &mem_addr_set, "memory base address");
-    init_opts(&opts[1], 'f', true, OPTION_ARG_TYPE_NUM,
-                         &flash_addr, &flash_addr_set, "FLASH memory base address");
-    init_opts(&opts[2], 'l', true, OPTION_ARG_TYPE_NUM,
-                         &length, &length_set, "image length [in FLASH]");
-    if (!scan_opts(argc, argv, 2, opts, 3, 0, 0, 0))
-               {
-                       fis_usage("invalid arguments");
-                       return;
-               }
+       int stat;
+       unsigned long length;
+       CYG_ADDRESS mem_addr, flash_addr;
+       bool mem_addr_set = false;
+       bool flash_addr_set = false;
+       bool length_set = false;
+       void *err_addr;
+       struct option_info opts[3];
+       bool prog_ok;
+
+       init_opts(&opts[0], 'b', true, OPTION_ARG_TYPE_NUM,
+                       &mem_addr, &mem_addr_set, "memory base address");
+       init_opts(&opts[1], 'f', true, OPTION_ARG_TYPE_NUM,
+                       &flash_addr, &flash_addr_set, "FLASH memory base address");
+       init_opts(&opts[2], 'l', true, OPTION_ARG_TYPE_NUM,
+                       &length, &length_set, "image length [in FLASH]");
+       if (!scan_opts(argc, argv, 2, opts, 3, 0, 0, 0))
+       {
+               fis_usage("invalid arguments");
+               return;
+       }
 
-    if (!mem_addr_set || !flash_addr_set || !length_set) {
+       if (!mem_addr_set || !flash_addr_set || !length_set) {
                fis_usage("required parameter missing");
                return;
-    }
+       }
 
-    // Round up length to FLASH block size
+       // Round up length to FLASH block size
 #ifndef CYGPKG_HAL_MIPS // FIXME: compiler is b0rken
-    length = ((length + flash_block_size - 1) / flash_block_size) * flash_block_size;
+       length = ((length + flash_block_size - 1) / flash_block_size) * flash_block_size;
 #endif
-    if (flash_addr_set &&
+       if (flash_addr_set &&
                ((stat = flash_verify_addr((void *)flash_addr)) ||
-                (stat = flash_verify_addr((void *)(flash_addr + length - 1))))) {
+                       (stat = flash_verify_addr((void *)(flash_addr + length - 1))))) {
                _show_invalid_flash_address(flash_addr, stat);
                return;
-    }
-    if (flash_addr_set && flash_addr & (flash_block_size-1)) {
+       }
+       if (flash_addr_set && flash_addr & (flash_block_size-1)) {
                diag_printf("Invalid FLASH address: %p\n", (void *)flash_addr);
                diag_printf("   must be 0x%x aligned\n", flash_block_size);
                return;
-    }
-    if ((mem_addr < (CYG_ADDRESS)ram_start) ||
+       }
+       if ((mem_addr < (CYG_ADDRESS)ram_start) ||
                ((mem_addr + length) >= (CYG_ADDRESS)ram_end)) {
                diag_printf("** WARNING: RAM address: %p may be invalid\n", (void *)mem_addr);
                diag_printf("   valid range is %p-%p\n", (void *)ram_start, (void *)ram_end);
-    }
-    // Safety check - make sure the address range is not within the code we're running
-    if (flash_code_overlaps((void *)flash_addr, (void *)(flash_addr + length - 1))) {
+       }
+       // Safety check - make sure the address range is not within the code we're running
+       if (flash_code_overlaps((void *)flash_addr, (void *)(flash_addr + length - 1))) {
                diag_printf("Can't program this region - contains code in use!\n");
                return;
-    }
-    if (!verify_action("* CAUTION * about to program FLASH\n           at %p..%p from %p",
-                                          (void *)flash_addr, (void *)(flash_addr + length - 1),
-                                          (void *)mem_addr)) {
+       }
+       if (!verify_action("* CAUTION * about to program FLASH\n                at %p..%p from %p",
+                                               (void *)flash_addr, (void *)(flash_addr + length - 1),
+                                               (void *)mem_addr)) {
                return;  // The guy gave up
-    }
-    prog_ok = true;
-    if (prog_ok) {
+       }
+       prog_ok = true;
+       if (prog_ok) {
                // Erase area to be programmed
                if ((stat = flash_erase((void *)flash_addr, length, &err_addr)) != 0) {
                        diag_printf("Can't erase region at %p: %s\n", err_addr, flash_errmsg(stat));
                        prog_ok = false;
                }
-    }
-    if (prog_ok) {
+       }
+       if (prog_ok) {
                // Now program it
                stat = FLASH_PROGRAM((void *)flash_addr, (void *)mem_addr, length, &err_addr);
                if (stat != 0) {
                        diag_printf("Can't program region at %p: %s\n", err_addr, flash_errmsg(stat));
                        prog_ok = false;
                }
-    }
+       }
 }
 
 static void
 fis_erase(int argc, char *argv[])
 {
-    int stat;
-    unsigned long length;
-    CYG_ADDRESS flash_addr;
-    bool flash_addr_set = false;
-    bool length_set = false;
-    void *err_addr;
-    struct option_info opts[2];
-
-    init_opts(&opts[0], 'f', true, OPTION_ARG_TYPE_NUM,
-                         &flash_addr, &flash_addr_set, "FLASH memory base address");
-    init_opts(&opts[1], 'l', true, OPTION_ARG_TYPE_NUM,
-                         &length, &length_set, "length");
-    if (!scan_opts(argc, argv, 2, opts, 2, NULL, 0, NULL))
-               {
-                       fis_usage("invalid arguments");
-                       return;
-               }
+       int stat;
+       unsigned long length;
+       CYG_ADDRESS flash_addr;
+       bool flash_addr_set = false;
+       bool length_set = false;
+       void *err_addr;
+       struct option_info opts[2];
+
+       init_opts(&opts[0], 'f', true, OPTION_ARG_TYPE_NUM,
+                       &flash_addr, &flash_addr_set, "FLASH memory base address");
+       init_opts(&opts[1], 'l', true, OPTION_ARG_TYPE_NUM,
+                       &length, &length_set, "length");
+       if (!scan_opts(argc, argv, 2, opts, 2, NULL, 0, NULL))
+       {
+               fis_usage("invalid arguments");
+               return;
+       }
 
-    if (!flash_addr_set || !length_set) {
+       if (!flash_addr_set || !length_set) {
                fis_usage("missing argument");
                return;
-    }
-    if (flash_addr_set &&
+       }
+       if (flash_addr_set &&
                ((stat = flash_verify_addr((void *)flash_addr)) ||
-                (stat = flash_verify_addr((void *)(flash_addr + length - 1))))) {
+                       (stat = flash_verify_addr((void *)(flash_addr + length - 1))))) {
                _show_invalid_flash_address(flash_addr, stat);
                return;
-    }
-    if (flash_addr_set && flash_addr & (flash_block_size-1)) {
+       }
+       if (flash_addr_set && flash_addr & (flash_block_size-1)) {
                diag_printf("Invalid FLASH address: %p\n", (void *)flash_addr);
                diag_printf("   must be 0x%x aligned\n", flash_block_size);
                return;
-    }
-    // Safety check - make sure the address range is not within the code we're running
-    if (flash_code_overlaps((void *)flash_addr, (void *)(flash_addr + length - 1))) {
+       }
+       // Safety check - make sure the address range is not within the code we're running
+       if (flash_code_overlaps((void *)flash_addr, (void *)(flash_addr + length - 1))) {
                diag_printf("Can't erase this region - contains code in use!\n");
                return;
-    }
-    if ((stat = flash_erase((void *)flash_addr, length, &err_addr)) != 0) {
+       }
+       if ((stat = flash_erase((void *)flash_addr, length, &err_addr)) != 0) {
                diag_printf("Error erasing at %p: %s\n", err_addr, flash_errmsg(stat));
-    }
+       }
 }
 
 #ifdef CYGHWR_IO_FLASH_BLOCK_LOCKING
@@ -1513,26 +1617,26 @@ fis_erase(int argc, char *argv[])
 static void
 fis_lock(int argc, char *argv[])
 {
-    char *name;
-    int stat;
-    unsigned long length;
-    CYG_ADDRESS flash_addr;
-    bool flash_addr_set = false;
-    bool length_set = false;
-    void *err_addr;
-    struct option_info opts[2];
-
-    init_opts(&opts[0], 'f', true, OPTION_ARG_TYPE_NUM,
-                         &flash_addr, &flash_addr_set, "FLASH memory base address");
-    init_opts(&opts[1], 'l', true, OPTION_ARG_TYPE_NUM,
-                         &length, &length_set, "length");
-    if (!scan_opts(argc, argv, 2, opts, 2, &name, OPTION_ARG_TYPE_STR, "image name")) {
+       char *name;
+       int stat;
+       unsigned long length;
+       CYG_ADDRESS flash_addr;
+       bool flash_addr_set = false;
+       bool length_set = false;
+       void *err_addr;
+       struct option_info opts[2];
+
+       init_opts(&opts[0], 'f', true, OPTION_ARG_TYPE_NUM,
+                       &flash_addr, &flash_addr_set, "FLASH memory base address");
+       init_opts(&opts[1], 'l', true, OPTION_ARG_TYPE_NUM,
+                       &length, &length_set, "length");
+       if (!scan_opts(argc, argv, 2, opts, 2, &name, OPTION_ARG_TYPE_STR, "image name")) {
                fis_usage("invalid arguments");
                return;
        }
 #ifdef CYGOPT_REDBOOT_FIS
-    /* Get parameters from image if specified */
-    if (name) {
+       /* Get parameters from image if specified */
+       if (name) {
                struct fis_image_desc *img;
                if ((img = fis_lookup(name, NULL)) == NULL) {
                        diag_printf("No image '%s' found\n", name);
@@ -1541,45 +1645,45 @@ fis_lock(int argc, char *argv[])
 
                flash_addr = img->flash_base;
                length = img->size;
-    } else
+       } else
 #endif
                if (!flash_addr_set || !length_set) {
                        fis_usage("missing argument");
                        return;
                }
-    if (flash_addr_set &&
+       if (flash_addr_set &&
                ((stat = flash_verify_addr((void *)flash_addr)) ||
-                (stat = flash_verify_addr((void *)(flash_addr + length - 1))))) {
+                       (stat = flash_verify_addr((void *)(flash_addr + length - 1))))) {
                _show_invalid_flash_address(flash_addr, stat);
                return;
-    }
-    if ((stat = flash_lock((void *)flash_addr, length, &err_addr)) != 0) {
+       }
+       if ((stat = flash_lock((void *)flash_addr, length, &err_addr)) != 0) {
                diag_printf("Error locking at %p: %s\n", err_addr, flash_errmsg(stat));
-    }
+       }
 }
 
 static void
 fis_unlock(int argc, char *argv[])
 {
-    char *name;
-    int stat;
-    unsigned long length;
-    CYG_ADDRESS flash_addr;
-    bool flash_addr_set = false;
-    bool length_set = false;
-    void *err_addr;
-    struct option_info opts[2];
-
-    init_opts(&opts[0], 'f', true, OPTION_ARG_TYPE_NUM,
-                         &flash_addr, &flash_addr_set, "FLASH memory base address");
-    init_opts(&opts[1], 'l', true, OPTION_ARG_TYPE_NUM,
-                         &length, &length_set, "length");
-    if (!scan_opts(argc, argv, 2, opts, 2, &name, OPTION_ARG_TYPE_STR, "image name")) {
+       char *name;
+       int stat;
+       unsigned long length;
+       CYG_ADDRESS flash_addr;
+       bool flash_addr_set = false;
+       bool length_set = false;
+       void *err_addr;
+       struct option_info opts[2];
+
+       init_opts(&opts[0], 'f', true, OPTION_ARG_TYPE_NUM,
+                       &flash_addr, &flash_addr_set, "FLASH memory base address");
+       init_opts(&opts[1], 'l', true, OPTION_ARG_TYPE_NUM,
+                       &length, &length_set, "length");
+       if (!scan_opts(argc, argv, 2, opts, 2, &name, OPTION_ARG_TYPE_STR, "image name")) {
                fis_usage("invalid arguments");
                return;
        }
 #ifdef CYGOPT_REDBOOT_FIS
-    if (name) {
+       if (name) {
                struct fis_image_desc *img;
                if ((img = fis_lookup(name, NULL)) == NULL) {
                        diag_printf("No image '%s' found\n", name);
@@ -1588,22 +1692,22 @@ fis_unlock(int argc, char *argv[])
 
                flash_addr = img->flash_base;
                length = img->size;
-    } else
+       } else
 #endif
                if (!flash_addr_set || !length_set) {
                        fis_usage("missing argument");
                        return;
                }
-    if (flash_addr_set &&
+       if (flash_addr_set &&
                ((stat = flash_verify_addr((void *)flash_addr)) ||
-                (stat = flash_verify_addr((void *)(flash_addr + length - 1))))) {
+                       (stat = flash_verify_addr((void *)(flash_addr + length - 1))))) {
                _show_invalid_flash_address(flash_addr, stat);
                return;
-    }
+       }
 
-    if ((stat = flash_unlock((void *)flash_addr, length, &err_addr)) != 0) {
+       if ((stat = flash_unlock((void *)flash_addr, length, &err_addr)) != 0) {
                diag_printf("Error unlocking at %p: %s\n", err_addr, flash_errmsg(stat));
-    }
+       }
 }
 #endif
 
@@ -1613,8 +1717,8 @@ int __flash_init = 0;
 void
 _flash_info(void)
 {
-    if (!__flash_init) return;
-    diag_printf("FLASH: %p - 0x%x, %d blocks of 0x%08x bytes each.\n",
+       if (!__flash_init) return;
+       diag_printf("FLASH: %p - 0x%x, %d blocks of 0x%08x bytes each.\n",
                                flash_start, (CYG_ADDRWORD)flash_end + 1, flash_num_blocks, flash_block_size);
 }
 
@@ -1633,10 +1737,10 @@ do_flash_init(void)
 
        //check the size of fis_valid_info
        CYG_ASSERT((sizeof(struct fis_valid_info) <= sizeof(img0.u.name)),
-                          "fis_valid_info size mismatch");
+                       "fis_valid_info size mismatch");
        //try to check the alignment of version_count
        CYG_ASSERT(((&img0.u.valid_info.version_count - &img0) % sizeof(unsigned long) == 0),
-                          "alignment problem");
+                       "alignment problem");
 #endif
        if (!__flash_init) {
                __flash_init = 1;
@@ -1696,12 +1800,12 @@ do_flash_init(void)
 
                if (CYGNUM_REDBOOT_FIS_REDUNDANT_DIRECTORY_BLOCK < 0) {
                        redundant_fis_addr = (void *)((CYG_ADDRESS)flash_end + 1 +
-                                                                                 (CYGNUM_REDBOOT_FIS_REDUNDANT_DIRECTORY_BLOCK *
-                                                                                  flash_block_size));
+                                                                               (CYGNUM_REDBOOT_FIS_REDUNDANT_DIRECTORY_BLOCK *
+                                                                                       flash_block_size));
                } else {
                        redundant_fis_addr = (void *)((CYG_ADDRESS)flash_start +
-                                                                                 (CYGNUM_REDBOOT_FIS_REDUNDANT_DIRECTORY_BLOCK *
-                                                                                  flash_block_size));
+                                                                               (CYGNUM_REDBOOT_FIS_REDUNDANT_DIRECTORY_BLOCK *
+                                                                                       flash_block_size));
                }
 
                if (((CYG_ADDRESS)redundant_fis_addr + fisdir_size - 1) > (CYG_ADDRESS)flash_end) {
@@ -1712,12 +1816,12 @@ do_flash_init(void)
                FLASH_READ(redundant_fis_addr, &img1, sizeof(img1), &err_addr);
 
                if (strncmp(img0.u.valid_info.magic_name, CYG_REDBOOT_RFIS_VALID_MAGIC,
-                                       CYG_REDBOOT_RFIS_VALID_MAGIC_LENGTH) != 0) {
-                               memset(&img0, 0, sizeof(img0));
-                       }
+                                               CYG_REDBOOT_RFIS_VALID_MAGIC_LENGTH) != 0) {
+                       memset(&img0, 0, sizeof(img0));
+               }
 
                if (strncmp(img1.u.valid_info.magic_name, CYG_REDBOOT_RFIS_VALID_MAGIC,
-                                       CYG_REDBOOT_RFIS_VALID_MAGIC_LENGTH) != 0) {
+                                               CYG_REDBOOT_RFIS_VALID_MAGIC_LENGTH) != 0) {
                        memset(&img1, 0, sizeof(img0));
                }
 
@@ -1751,10 +1855,10 @@ do_flash_init(void)
 static void
 _do_flash_init(void)
 {
-    static int init_done = 0;
-    if (init_done) return;
-    init_done = 1;
-    do_flash_init();
+       static int init_done = 0;
+       if (init_done) return;
+       init_done = 1;
+       do_flash_init();
 }
 
 RedBoot_init(_do_flash_init, RedBoot_INIT_FIRST);
@@ -1762,20 +1866,20 @@ RedBoot_init(_do_flash_init, RedBoot_INIT_FIRST);
 static void
 do_fis(int argc, char *argv[])
 {
-    struct cmd *cmd;
+       struct cmd *cmd;
 
-    if (argc < 2) {
+       if (argc < 2) {
                fis_usage("too few arguments");
                return;
-    }
-    if (do_flash_init() < 0) {
-       diag_printf("Sorry, no FLASH memory is available\n");
-       return;
-    }
-    if ((cmd = cmd_search(__FIS_cmds_TAB__, &__FIS_cmds_TAB_END__,
-                         argv[1])) != NULL) {
+       }
+       if (do_flash_init() < 0) {
+               diag_printf("Sorry, no FLASH memory is available\n");
+               return;
+       }
+       if ((cmd = cmd_search(__FIS_cmds_TAB__, &__FIS_cmds_TAB_END__,
+                                                               argv[1])) != NULL) {
                (cmd->fun)(argc, argv);
                return;
-    }
-    fis_usage("unrecognized command");
+       }
+       fis_usage("unrecognized command");
 }