flash: factor out adjusting of Flash address to the end of sector
authorBartlomiej Sieka <tur@semihalf.com>
Wed, 1 Oct 2008 13:26:27 +0000 (15:26 +0200)
committerWolfgang Denk <wd@denx.de>
Sat, 18 Oct 2008 19:54:00 +0000 (21:54 +0200)
The upcoming automatic update feature needs the ability to adjust an
address within Flash to the end of its respective sector. Factor out
this functionality to a new function flash_sect_roundb().

Signed-off-by: Rafal Czubak <rcz@semihalf.com>
Signed-off-by: Bartlomiej Sieka <tur@semihalf.com>
Signed-off-by: Stefan Roese <sr@denx.de>
common/cmd_flash.c
include/flash.h

index 18d2250..29e5b6d 100644 (file)
@@ -105,6 +105,47 @@ abbrev_spec (char *str, flash_info_t ** pinfo, int *psf, int *psl)
 }
 
 /*
+ * Take *addr in Flash and adjust it to fall on the end of its sector
+ */
+int flash_sect_roundb (ulong *addr)
+{
+       flash_info_t *info;
+       ulong bank, sector_end_addr;
+       char found;
+       int i;
+
+       /* find the end addr of the sector where the *addr is */
+       found = 0;
+       for (bank = 0; bank < CFG_MAX_FLASH_BANKS && !found; ++bank) {
+               info = &flash_info[bank];
+               for (i = 0; i < info->sector_count && !found; ++i) {
+                       /* get the end address of the sector */
+                       if (i == info->sector_count - 1) {
+                               sector_end_addr = info->start[0] +
+                                                               info->size - 1;
+                       } else {
+                               sector_end_addr = info->start[i+1] - 1;
+                       }
+
+                       if (*addr <= sector_end_addr &&
+                                               *addr >= info->start[i]) {
+                               found = 1;
+                               /* adjust *addr if necessary */
+                               if (*addr < sector_end_addr)
+                                       *addr = sector_end_addr;
+                       } /* sector */
+               } /* bank */
+       }
+       if (!found) {
+               /* error, addres not in flash */
+               printf("Error: end address (0x%08lx) not in flash!\n", *addr);
+               return 1;
+       }
+
+       return 0;
+}
+
+/*
  * This function computes the start and end addresses for both
  * erase and protect commands. The range of the addresses on which
  * either of the commands is to operate can be given in two forms:
@@ -126,8 +167,6 @@ addr_spec(char *arg1, char *arg2, ulong *addr_first, ulong *addr_last)
 {
        char *ep;
        char len_used; /* indicates if the "start +length" form used */
-       char found;
-       ulong bank;
 
        *addr_first = simple_strtoul(arg1, &ep, 16);
        if (ep == arg1 || *ep != '\0')
@@ -157,38 +196,8 @@ addr_spec(char *arg1, char *arg2, ulong *addr_first, ulong *addr_last)
                 * sector boundary, so that the commands don't fail later on.
                 */
 
-               /* find the end addr of the sector where the *addr_last is */
-               found = 0;
-               for (bank = 0; bank < CFG_MAX_FLASH_BANKS && !found; ++bank){
-                       int i;
-                       flash_info_t *info = &flash_info[bank];
-                       for (i = 0; i < info->sector_count && !found; ++i){
-                               /* get the end address of the sector */
-                               ulong sector_end_addr;
-                               if (i == info->sector_count - 1){
-                                       sector_end_addr =
-                                               info->start[0] + info->size - 1;
-                               } else {
-                                       sector_end_addr =
-                                               info->start[i+1] - 1;
-                               }
-                               if (*addr_last <= sector_end_addr &&
-                                               *addr_last >= info->start[i]){
-                                       /* sector found */
-                                       found = 1;
-                                       /* adjust *addr_last if necessary */
-                                       if (*addr_last < sector_end_addr){
-                                               *addr_last = sector_end_addr;
-                                       }
-                               }
-                       } /* sector */
-               } /* bank */
-               if (!found){
-                       /* error, addres not in flash */
-                       printf("Error: end address (0x%08lx) not in flash!\n",
-                                                               *addr_last);
+               if (flash_sect_roundb(addr_last) > 0)
                        return -1;
-               }
        } /* "start +length" from used */
 
        return 1;
index af8a7c0..6f5d7d5 100644 (file)
@@ -91,6 +91,7 @@ extern void flash_print_info (flash_info_t *);
 extern int flash_erase (flash_info_t *, int, int);
 extern int flash_sect_erase (ulong addr_first, ulong addr_last);
 extern int flash_sect_protect (int flag, ulong addr_first, ulong addr_last);
+extern int flash_sect_roundb (ulong *addr);
 
 /* common/flash.c */
 extern void flash_protect (int flag, ulong from, ulong to, flash_info_t *info);