mdcart
changeset 1:c8d3d23ccd1f tip
works
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Sun, 12 Mar 2017 23:43:21 +0200 |
parents | e6d499a8eb1d |
children | |
files | .hgignore src/main.c |
diffstat | 2 files changed, 113 insertions(+), 6 deletions(-) [+] |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/.hgignore Sun Mar 12 23:43:21 2017 +0200 1.3 @@ -0,0 +1,4 @@ 1.4 +\.o$ 1.5 +\.swp$ 1.6 +\.d$ 1.7 +^mdcart$
2.1 --- a/src/main.c Sun Mar 12 06:20:22 2017 +0200 2.2 +++ b/src/main.c Sun Mar 12 23:43:21 2017 +0200 2.3 @@ -7,6 +7,7 @@ 2.4 #include <unistd.h> 2.5 #include <fcntl.h> 2.6 #include <termios.h> 2.7 +#include <sys/stat.h> 2.8 2.9 enum { 2.10 OP_NONE, 2.11 @@ -19,12 +20,14 @@ 2.12 int op_read(FILE *dev, FILE *fp); 2.13 int op_reset(FILE *dev); 2.14 int parse_args(int argc, char **argv); 2.15 +void print_progress(int p); 2.16 2.17 int op, verify = 0; 2.18 const char *devname = "/dev/ttyUSB0"; 2.19 char *fname = 0; 2.20 uint32_t start_addr; 2.21 int read_size = 128 * 1024; 2.22 +int quiet; 2.23 2.24 int main(int argc, char **argv) 2.25 { 2.26 @@ -48,8 +51,8 @@ 2.27 term.c_iflag &= ~(IXON | IXOFF); 2.28 term.c_lflag = 0; 2.29 2.30 - cfsetispeed(&term, B38400); 2.31 - cfsetospeed(&term, B38400); 2.32 + cfsetispeed(&term, B115200); 2.33 + cfsetospeed(&term, B115200); 2.34 2.35 if(tcsetattr(dev, TCSANOW, &term) == -1) { 2.36 fprintf(stderr, "failed to set terminal attributes for %s: %s\n", devname, strerror(errno)); 2.37 @@ -101,22 +104,86 @@ 2.38 return 0; 2.39 } 2.40 2.41 -#define RET_IF_FAIL(x) \ 2.42 - if(!fgets(buf, sizeof buf, dev) || buf[0] != 'O') return x 2.43 +#define RET_IF_FAIL(x) if(!fgets(buf, sizeof buf, dev) || buf[0] != 'O') return x 2.44 +/*#define RET_IF_FAIL(x) \ 2.45 + do { \ 2.46 + if(!fgets(buf, sizeof buf, dev)) return x; \ 2.47 + printf("--> %s", buf); \ 2.48 + if(buf[0] != 'O') return x; \ 2.49 + } while(0) 2.50 + */ 2.51 2.52 int op_write(FILE *dev, FILE *fp) 2.53 { 2.54 char buf[128]; 2.55 - int c; 2.56 + int c, prev_progr = -1, written = 0; 2.57 + struct stat st; 2.58 + 2.59 + fstat(fileno(fp), &st); 2.60 2.61 fprintf(dev, "p\n"); 2.62 RET_IF_FAIL(-1); 2.63 fprintf(dev, "a%d\n", start_addr); 2.64 RET_IF_FAIL(-1); 2.65 2.66 + if(!quiet) { 2.67 + printf("writing %d bytes starting from %xh\n", (int)st.st_size, start_addr); 2.68 + } 2.69 + 2.70 while((c = fgetc(fp)) != -1) { 2.71 + int p; 2.72 fprintf(dev, "w%d\n", c); 2.73 RET_IF_FAIL(-1); 2.74 + 2.75 + if(!quiet) { 2.76 + p = 100 * ++written / st.st_size; 2.77 + if(p != prev_progr) { 2.78 + prev_progr = p; 2.79 + print_progress(p); 2.80 + } 2.81 + } 2.82 + } 2.83 + if(!quiet) { 2.84 + putchar('\n'); 2.85 + } 2.86 + 2.87 + if(verify) { 2.88 + int verified = 0; 2.89 + 2.90 + rewind(fp); 2.91 + fprintf(dev, "a%d\n", start_addr); 2.92 + RET_IF_FAIL(-1); 2.93 + 2.94 + if(!quiet) { 2.95 + printf("verifying %d bytes starting from %xh\n", (int)st.st_size, start_addr); 2.96 + } 2.97 + 2.98 + while((c = fgetc(fp)) != -1) { 2.99 + int p, val; 2.100 + fprintf(dev, "r\n"); 2.101 + 2.102 + if(!fgets(buf, sizeof buf, dev) || buf[0] != 'O') { 2.103 + fprintf(stderr, "\rread error after %d bytes\n", verified); 2.104 + break; 2.105 + } 2.106 + 2.107 + if((val = atoi(buf + 3)) != c) { /* skip 'O', 'K', ' ' */ 2.108 + fprintf(stderr, "\rverification error at byte %d\n", verified); 2.109 + fprintf(stderr, "expected %d (%xh), got %d (%xh)\n", c, c, val, val); 2.110 + break; 2.111 + } 2.112 + 2.113 + if(!quiet) { 2.114 + p = 100 * ++verified / st.st_size; 2.115 + if(p != prev_progr) { 2.116 + prev_progr = p; 2.117 + print_progress(p); 2.118 + } 2.119 + } 2.120 + } 2.121 + if(!quiet) { 2.122 + putchar('\n'); 2.123 + } 2.124 } 2.125 2.126 fprintf(dev, "b\n"); 2.127 @@ -127,21 +194,38 @@ 2.128 int op_read(FILE *dev, FILE *fp) 2.129 { 2.130 char buf[128]; 2.131 - int i, val; 2.132 + int i, val, prev_progr = -1; 2.133 2.134 fprintf(dev, "p\n"); 2.135 RET_IF_FAIL(-1); 2.136 fprintf(dev, "a%d\n", start_addr); 2.137 RET_IF_FAIL(-1); 2.138 2.139 + if(!quiet) { 2.140 + printf("reading %d bytes starting from %xh\n", read_size, start_addr); 2.141 + } 2.142 + 2.143 for(i=0; i<read_size; i++) { 2.144 + int p; 2.145 fprintf(dev, "r\n"); 2.146 if(!fgets(buf, sizeof buf, dev) || buf[0] != 'O') { 2.147 fprintf(stderr, "read error after %d bytes\n", i); 2.148 break; 2.149 } 2.150 + 2.151 val = atoi(buf + 3); /* skip 'O', 'K', ' ' */ 2.152 fputc(val, fp); 2.153 + 2.154 + if(!quiet) { 2.155 + p = 100 * i / read_size; 2.156 + if(p != prev_progr) { 2.157 + prev_progr = p; 2.158 + print_progress(p); 2.159 + } 2.160 + } 2.161 + } 2.162 + if(!quiet) { 2.163 + putchar('\n'); 2.164 } 2.165 2.166 fprintf(dev, "b\n"); 2.167 @@ -201,6 +285,9 @@ 2.168 } else if(strcmp(argv[i], "-d") == 0 || strcmp(argv[i], "-device") == 0) { 2.169 devname = argv[++i]; 2.170 2.171 + } else if(strcmp(argv[i], "-q") == 0 || strcmp(argv[i], "-quiet") == 0) { 2.172 + quiet = 1; 2.173 + 2.174 } else if(strcmp(argv[i], "-h") == 0 || strcmp(argv[i], "-help") == 0) { 2.175 printf("Usage: %s [options]\n", argv[0]); 2.176 printf("Options:\n"); 2.177 @@ -210,6 +297,7 @@ 2.178 printf(" -R|-reset: reboot machine\n"); 2.179 printf(" -v|-verify: do verification after writing\n"); 2.180 printf(" -s|-size: read size in bytes\n"); 2.181 + printf(" -q|-quiet: don't print anything to stdout\n"); 2.182 printf(" -d|-device <device file>: serial device (eg /dev/ttyUSB0)\n"); 2.183 printf(" -h|-help: print usage and exit\n"); 2.184 return 0; 2.185 @@ -226,3 +314,18 @@ 2.186 } 2.187 return 0; 2.188 } 2.189 + 2.190 +void print_progress(int p) 2.191 +{ 2.192 + int i, num = p / 2; 2.193 + printf("\rprogress: ["); 2.194 + for(i=0; i<50; i++) { 2.195 + if(i == num) { 2.196 + putchar('>'); 2.197 + } else { 2.198 + putchar(i < num ? '=' : ' '); 2.199 + } 2.200 + } 2.201 + printf("] %3d%%", p); 2.202 + fflush(stdout); 2.203 +}