]> git.kernelconcepts.de Git - karo-tx-uboot.git/blobdiff - common/cmd_sf.c
cosmetic: suvd3: align #defines
[karo-tx-uboot.git] / common / cmd_sf.c
index c8c547ac0b71ed3a6d41c1a71d5aac0883016345..5ac1d0c4c1ef4ef9cc34b30b3e056b2a76744beb 100644 (file)
 #ifndef CONFIG_SF_DEFAULT_MODE
 # define CONFIG_SF_DEFAULT_MODE                SPI_MODE_3
 #endif
+#ifndef CONFIG_SF_DEFAULT_CS
+# define CONFIG_SF_DEFAULT_CS          0
+#endif
+#ifndef CONFIG_SF_DEFAULT_BUS
+# define CONFIG_SF_DEFAULT_BUS         0
+#endif
 
 static struct spi_flash *flash;
 
@@ -63,27 +69,26 @@ static int sf_parse_len_arg(char *arg, ulong *len)
 
 static int do_spi_flash_probe(int argc, char * const argv[])
 {
-       unsigned int bus = 0;
-       unsigned int cs;
+       unsigned int bus = CONFIG_SF_DEFAULT_BUS;
+       unsigned int cs = CONFIG_SF_DEFAULT_CS;
        unsigned int speed = CONFIG_SF_DEFAULT_SPEED;
        unsigned int mode = CONFIG_SF_DEFAULT_MODE;
        char *endp;
        struct spi_flash *new;
 
-       if (argc < 2)
-               return -1;
-
-       cs = simple_strtoul(argv[1], &endp, 0);
-       if (*argv[1] == 0 || (*endp != 0 && *endp != ':'))
-               return -1;
-       if (*endp == ':') {
-               if (endp[1] == 0)
-                       return -1;
-
-               bus = cs;
-               cs = simple_strtoul(endp + 1, &endp, 0);
-               if (*endp != 0)
+       if (argc >= 2) {
+               cs = simple_strtoul(argv[1], &endp, 0);
+               if (*argv[1] == 0 || (*endp != 0 && *endp != ':'))
                        return -1;
+               if (*endp == ':') {
+                       if (endp[1] == 0)
+                               return -1;
+
+                       bus = cs;
+                       cs = simple_strtoul(endp + 1, &endp, 0);
+                       if (*endp != 0)
+                               return -1;
+               }
        }
 
        if (argc >= 3) {
@@ -127,12 +132,12 @@ static int do_spi_flash_probe(int argc, char * const argv[])
 static const char *spi_flash_update_block(struct spi_flash *flash, u32 offset,
                size_t len, const char *buf, char *cmp_buf, size_t *skipped)
 {
-       debug("offset=%#x, sector_size=%#x, len=%#x\n",
+       debug("offset=%#x, sector_size=%#x, len=%#zx\n",
                offset, flash->sector_size, len);
        if (spi_flash_read(flash, offset, len, cmp_buf))
                return "read";
        if (memcmp(cmp_buf, buf, len) == 0) {
-               debug("Skip region %x size %x: no change\n",
+               debug("Skip region %x size %zx: no change\n",
                        offset, len);
                *skipped += len;
                return NULL;
@@ -161,12 +166,11 @@ static int spi_flash_update(struct spi_flash *flash, u32 offset,
        char *cmp_buf;
        const char *end = buf + len;
        size_t todo;            /* number of bytes to do in this pass */
-       size_t skipped;         /* statistics */
+       size_t skipped = 0;     /* statistics */
 
        cmp_buf = malloc(flash->sector_size);
        if (cmp_buf) {
-               for (skipped = 0; buf < end && !err_oper;
-                               buf += todo, offset += todo) {
+               for (; buf < end && !err_oper; buf += todo, offset += todo) {
                        todo = min(end - buf, flash->sector_size);
                        err_oper = spi_flash_update_block(flash, offset, todo,
                                        buf, cmp_buf, &skipped);
@@ -181,6 +185,7 @@ static int spi_flash_update(struct spi_flash *flash, u32 offset,
        }
        printf("%zu bytes written, %zu bytes skipped\n", len - skipped,
               skipped);
+
        return 0;
 }
 
@@ -206,6 +211,13 @@ static int do_spi_flash_read_write(int argc, char * const argv[])
        if (*argv[3] == 0 || *endp != 0)
                return -1;
 
+       /* Consistency checking */
+       if (offset + len > flash->size) {
+               printf("ERROR: attempting %s past flash size (%#x)\n",
+                       argv[0], flash->size);
+               return 1;
+       }
+
        buf = map_physmem(addr, len, MAP_WRBACK);
        if (!buf) {
                puts("Failed to map physical memory\n");
@@ -247,6 +259,13 @@ static int do_spi_flash_erase(int argc, char * const argv[])
        if (ret != 1)
                return -1;
 
+       /* Consistency checking */
+       if (offset + len > flash->size) {
+               printf("ERROR: attempting %s past flash size (%#x)\n",
+                       argv[0], flash->size);
+               return 1;
+       }
+
        ret = spi_flash_erase(flash, offset, len);
        if (ret) {
                printf("SPI flash %s failed\n", argv[0]);
@@ -293,13 +312,13 @@ done:
                return ret;
 
 usage:
-       return cmd_usage(cmdtp);
+       return CMD_RET_USAGE;
 }
 
 U_BOOT_CMD(
        sf,     5,      1,      do_spi_flash,
        "SPI flash sub-system",
-       "probe [bus:]cs [hz] [mode]     - init flash device on given SPI bus\n"
+       "probe [[bus:]cs] [hz] [mode]   - init flash device on given SPI bus\n"
        "                                 and chip select\n"
        "sf read addr offset len        - read `len' bytes starting at\n"
        "                                 `offset' to memory at `addr'\n"