Fix potentialyl unaligned memory accesses in mflash driver.
[openocd.git] / src / flash / mflash.c
index b5c7d46ba912171649bec9ef59eec0b88005d231..47c73382c1da02f23f7fcd1a5ae0b2049cd195ed 100644 (file)
@@ -670,13 +670,11 @@ static int mg_mflash_write(u32 addr, u8 *buff, u32 len)
 
 static int mg_write_cmd(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
 {
-       u32 address, buf_cnt;
+       u32 address, buf_cnt, cnt, res, i;
        u8 *buffer;
-       /* TODO : multi-bank support, large file support */
        fileio_t fileio;
        duration_t duration;
        char *duration_text;
-       int ret;
 
        if (argc != 3) {
                return ERROR_COMMAND_SYNTAX_ERROR;
@@ -688,18 +686,32 @@ static int mg_write_cmd(struct command_context_s *cmd_ctx, char *cmd, char **arg
                return ERROR_FAIL;
        }
 
-       buffer = malloc(fileio.size);
-
-       if (fileio_read(&fileio, fileio.size, buffer, &buf_cnt) != ERROR_OK)
-       {
-               free(buffer);
+       buffer = malloc(MG_FILEIO_CHUNK);
+       if (!buffer) {
                fileio_close(&fileio);
                return ERROR_FAIL;
        }
 
+       cnt = fileio.size / MG_FILEIO_CHUNK;
+       res = fileio.size % MG_FILEIO_CHUNK;
+
        duration_start_measure(&duration);
 
-       ret = mg_mflash_write(address, buffer, (u32)fileio.size);
+       for (i = 0; i < cnt; i++) {
+               if (fileio_read(&fileio, MG_FILEIO_CHUNK, buffer, &buf_cnt) !=
+                               ERROR_OK)
+                       goto mg_write_cmd_err;
+               if (mg_mflash_write(address, buffer, MG_FILEIO_CHUNK) != ERROR_OK)
+                       goto mg_write_cmd_err;
+               address += MG_FILEIO_CHUNK;
+       }
+       if (res) {
+               if (fileio_read(&fileio, res, buffer, &buf_cnt) != ERROR_OK)
+                       goto mg_write_cmd_err;                  
+               if (mg_mflash_write(address, buffer, res) != ERROR_OK)
+                       goto mg_write_cmd_err;
+       }
 
        duration_stop_measure(&duration, &duration_text);
 
@@ -708,19 +720,24 @@ static int mg_write_cmd(struct command_context_s *cmd_ctx, char *cmd, char **arg
                (float)fileio.size / 1024.0 / ((float)duration.duration.tv_sec + ((float)duration.duration.tv_usec / 1000000.0)));
 
        free(duration_text);
-
-       fileio_close(&fileio);
-
        free(buffer);
+       fileio_close(&fileio);
 
        return ERROR_OK;
+
+mg_write_cmd_err:
+       duration_stop_measure(&duration, &duration_text);
+       free(duration_text);
+       free(buffer);
+       fileio_close(&fileio);
+
+       return ERROR_FAIL;
 }
 
 static int mg_dump_cmd(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
 {
-       u32 address, size_written, size;
+       u32 address, size_written, size, cnt, res, i;
        u8 *buffer;
-       /* TODO : multi-bank support */
        fileio_t fileio;
        duration_t duration;
        char *duration_text;
@@ -735,30 +752,54 @@ static int mg_dump_cmd(struct command_context_s *cmd_ctx, char *cmd, char **args
        if (fileio_open(&fileio, args[1], FILEIO_WRITE, FILEIO_BINARY) != ERROR_OK) {
                return ERROR_FAIL;
        }
+       buffer = malloc(MG_FILEIO_CHUNK);
+       if (!buffer) {
+               fileio_close(&fileio);
+               return ERROR_FAIL;
+       }
 
-       buffer = malloc(size);
-
+       cnt = size / MG_FILEIO_CHUNK;
+       res = size % MG_FILEIO_CHUNK;
        duration_start_measure(&duration);
 
-       mg_mflash_read(address, buffer, size);
+       for (i = 0; i < cnt; i++) {
+               if (mg_mflash_read(address, buffer, MG_FILEIO_CHUNK) != ERROR_OK)
+                       goto mg_dump_cmd_err;
+               if (fileio_write(&fileio, MG_FILEIO_CHUNK, buffer, &size_written)
+                               != ERROR_OK)
+                       goto mg_dump_cmd_err;
+               address += MG_FILEIO_CHUNK;
+       }
+       if (res) {
+               if (mg_mflash_read(address, buffer, res) != ERROR_OK)
+                       goto mg_dump_cmd_err;
+               if (fileio_write(&fileio, res, buffer, &size_written) != ERROR_OK)
+                       goto mg_dump_cmd_err;
+       }
 
        duration_stop_measure(&duration, &duration_text);
 
-       fileio_write(&fileio, size, buffer, &size_written);
-
        command_print(cmd_ctx, "dump image (address 0x%8.8x size %u) to file %s in %s (%f kB/s)",
                                address, size, args[1], duration_text,
                                (float)size / 1024.0 / ((float)duration.duration.tv_sec + ((float)duration.duration.tv_usec / 1000000.0)));
 
        free(duration_text);
-
-       fileio_close(&fileio);
-
        free(buffer);
+       fileio_close(&fileio);
 
        return ERROR_OK;
-}
 
+mg_dump_cmd_err:
+       duration_stop_measure(&duration, &duration_text);
+       free(duration_text);
+       free(buffer);
+       fileio_close(&fileio);
+       return ERROR_FAIL;      
+}
 
 static int mg_set_feature(mg_feature_id feature, mg_feature_val config)
 {
@@ -1085,8 +1126,9 @@ static int mg_set_pll(mg_pll_t *pll)
        u8 buff[512];
 
        memset(buff, 0xff, 512);
-       *((u32 *)&buff[0]) = pll->lock_cyc;     /* PLL Lock cycle */
-       *((u16 *)&buff[4]) = pll->feedback_div; /* PLL Feedback 9bit Divider */
+       /* PLL Lock cycle and Feedback 9bit Divider */
+       memcpy(buff, &pll->lock_cyc, sizeof(u32));
+       memcpy(buff + 4, &pll->feedback_div, sizeof(u16));
        buff[6] = pll->input_div;               /* PLL Input 5bit Divider */
        buff[7] = pll->output_div;              /* PLL Output Divider */
 

Linking to existing account procedure

If you already have an account and want to add another login method you MUST first sign in with your existing account and then change URL to read https://review.openocd.org/login/?link to get to this page again but this time it'll work for linking. Thank you.

SSH host keys fingerprints

1024 SHA256:YKx8b7u5ZWdcbp7/4AeXNaqElP49m6QrwfXaqQGJAOk gerrit-code-review@openocd.zylin.com (DSA)
384 SHA256:jHIbSQa4REvwCFG4cq5LBlBLxmxSqelQPem/EXIrxjk gerrit-code-review@openocd.org (ECDSA)
521 SHA256:UAOPYkU9Fjtcao0Ul/Rrlnj/OsQvt+pgdYSZ4jOYdgs gerrit-code-review@openocd.org (ECDSA)
256 SHA256:A13M5QlnozFOvTllybRZH6vm7iSt0XLxbA48yfc2yfY gerrit-code-review@openocd.org (ECDSA)
256 SHA256:spYMBqEYoAOtK7yZBrcwE8ZpYt6b68Cfh9yEVetvbXg gerrit-code-review@openocd.org (ED25519)
+--[ED25519 256]--+
|=..              |
|+o..   .         |
|*.o   . .        |
|+B . . .         |
|Bo. = o S        |
|Oo.+ + =         |
|oB=.* = . o      |
| =+=.+   + E     |
|. .=o   . o      |
+----[SHA256]-----+
2048 SHA256:0Onrb7/PHjpo6iVZ7xQX2riKN83FJ3KGU0TvI0TaFG4 gerrit-code-review@openocd.zylin.com (RSA)