Also skip uudecode for reading when the LPC does not uuencode data.
authorNathael Pajani <nathael.pajani@ed3l.fr>
Thu, 3 Oct 2013 14:03:46 +0000 (16:03 +0200)
committerNathael Pajani <nathael.pajani@ed3l.fr>
Thu, 3 Oct 2013 14:03:46 +0000 (16:03 +0200)
isp_commands.c
isp_commands.h
isp_wrapper.c
lpcisp.c
prog_commands.c

index 4cf1cbc..074fe4d 100644 (file)
@@ -369,7 +369,7 @@ static unsigned int calc_checksum(unsigned char* data, unsigned int size)
  * perform read-memory operation
  * read 'count' bytes from 'addr' to 'data' buffer
  */
-int isp_read_memory(char* data, uint32_t addr, unsigned int count)
+int isp_read_memory(char* data, uint32_t addr, unsigned int count, unsigned int uuencoded)
 {
        /* Serial communication */
        char buf[SERIAL_BUFSIZE];
@@ -386,6 +386,27 @@ int isp_read_memory(char* data, uint32_t addr, unsigned int count)
                return ret;
        }
 
+       if (uuencoded == 0) {
+               if (SERIAL_BUFSIZE < count) {
+                       printf("Error when trying to read %u bytes of memory, buffer too small.\n", count);
+                       printf("Read %d max bytes at a time.\n", SERIAL_BUFSIZE);
+                       return -7;
+               }
+               len = isp_serial_read(buf, SERIAL_BUFSIZE, count);
+               if (len <= 0) {
+                       printf("Error reading memory.\n");
+                       return -6;
+               }
+               /* Wait some time before reading possible remaining data */
+               usleep( 1000 );
+               len += isp_serial_read((buf + len), (SERIAL_BUFSIZE - len), count);
+               if (len < 0) { /* Length may be null, as we may already have received everything */
+                       printf("Error reading memory.\n");
+                       return -5;
+               }
+               return len;
+       }
+
        /* Now, find the number of blocks of the reply. */
        blocks = get_nb_blocks(count, "Reading");
 
@@ -480,6 +501,15 @@ int isp_send_buf_to_ram(char* data, unsigned long int addr, unsigned int count,
                return -8;
        }
 
+       /* First check if we must UU-encode data */
+       if (perform_uuencode == 0) {
+               if (isp_serial_write(data, count) != (int)count) {
+                       printf("Error sending raw binary data.\n");
+                       return -7;
+               }
+               /* No checks required, we are done */
+               return 0;
+       }
        /* Now, find the number of blocks of data to send. */
        blocks = get_nb_blocks(count, "Sending");
 
@@ -496,16 +526,6 @@ int isp_send_buf_to_ram(char* data, unsigned long int addr, unsigned int count,
                if (datasize >= MAX_DATA_BLOCK_SIZE) {
                        datasize = MAX_DATA_BLOCK_SIZE;
                }
-               if (perform_uuencode == 0) {
-                       if (isp_serial_write(data + total_bytes_sent, datasize) != (int)datasize) {
-                               printf("Error sending raw binary data.\n");
-                               ret = -7;
-                               break;
-                       } else {
-                               /* No checks required */
-                               return ret;
-                       }
-               }
 
                /* uuencode data */
                encoded_size = isp_uu_encode(buf, data + total_bytes_sent, datasize);
index 93d755c..095b89a 100644 (file)
@@ -71,7 +71,7 @@ int isp_cmd_read_memory(int arg_count, char** args);
  * perform read-memory operation
  * read 'count' bytes from 'addr' to 'data' buffer
  */
-int isp_read_memory(char* data, uint32_t addr, unsigned int count);
+int isp_read_memory(char* data, uint32_t addr, unsigned int count, unsigned int uuencoded);
 
 /*
  * write-to-ram
index 0715973..3d534a5 100644 (file)
@@ -38,8 +38,8 @@ extern int trace_on;
 
 /*
  * read-memory
- * aruments : address count file
- * read 'count' bytes from 'address', store then in 'file'
+ * aruments : address count file uuencoded
+ * read 'count' bytes from 'address', store then in 'file', and maybe decode uuencoded data.
  */
 int isp_cmd_read_memory(int arg_count, char** args)
 {
@@ -49,9 +49,10 @@ int isp_cmd_read_memory(int arg_count, char** args)
        /* Reply handling */
        char* data;
        int ret = 0, len = 0;
+       unsigned int uuencoded = 1;
 
        /* Check read-memory arguments */
-       if (arg_count != 3) {
+       if (arg_count < 3) {
                printf("read-memory command needs address and count. Both must be multiple of 4.\n");
                return -12;
        }
@@ -66,6 +67,9 @@ int isp_cmd_read_memory(int arg_count, char** args)
                return -11;
        }
        out_file_name = args[2];
+       if (arg_count > 3) {
+               uuencoded = strtoul(args[3], NULL, 0);
+       }
 
        /* Allocate buffer */
        data = malloc(count);
@@ -75,7 +79,7 @@ int isp_cmd_read_memory(int arg_count, char** args)
        }
 
        /* Read data */
-       len = isp_read_memory(data, addr, count);
+       len = isp_read_memory(data, addr, count, uuencoded);
        if (len != (int)count) {
                printf("Read returned %d bytes instead of %lu.\n", len, count);
        }
index 6459b89..846d197 100644 (file)
--- a/lpcisp.c
+++ b/lpcisp.c
@@ -57,7 +57,7 @@ void help(char *prog_name)
                "  command specific arguments are as follow:\n" \
                "  \t unlock \n" \
                "  \t write-to-ram address file uuencode : send 'file' to 'address' in ram with or without uuencoding\n" \
-               "  \t read-memory address count file : read 'count' bytes from 'address', store then in 'file'\n" \
+               "  \t read-memory address count file uuencoded : read 'count' bytes from 'address', store then in 'file' (maybe decode)\n" \
                "  \t prepare-for-write first last : prepare sectors from 'first' to 'last' for write operation\n" \
                "  \t copy-ram-to-flash flash_addr ram_addr count : copy count bytes (256, 512, 1024 or 4096)\n" \
                "  \t     from 'ram_addr' to 'flash_addr'\n" \
index 7dccf50..0e3ebde 100644 (file)
@@ -57,7 +57,7 @@ int dump_to_file(struct part_desc* part, char* filename)
        }
 
        /* Read data */
-       len = isp_read_memory(data, part->flash_base, part->flash_size);
+       len = isp_read_memory(data, part->flash_base, part->flash_size, part->uuencode);
        if (len != (int)(part->flash_size)) {
                printf("Read returned %d bytes instead of %u.\n", len, part->flash_size);
        }
@@ -122,7 +122,7 @@ int start_prog(struct part_desc* part)
        int ret = 0, len = 0;
        uint32_t addr = 0;
 
-       len = isp_read_memory((char*)&addr, (part->flash_base + part->reset_vector_offset), sizeof(addr));
+       len = isp_read_memory((char*)&addr, (part->flash_base + part->reset_vector_offset), sizeof(addr), part->uuencode);
        if (len != sizeof(addr)) {
                printf("Unable to read reset address from flash.\n");
                return len;