2019-12-10 14:01:22 +11:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include "common.h"
|
|
|
|
|
2019-12-13 04:04:56 +11:00
|
|
|
/* Push specified file to specified device running the BASIC shell and verify
|
2019-12-10 14:01:22 +11:00
|
|
|
* that the sent contents is correct.
|
|
|
|
*/
|
|
|
|
|
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
if (argc != 4) {
|
|
|
|
fprintf(stderr, "Usage: ./upload device memptr fname\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
unsigned int memptr = strtol(argv[2], NULL, 16);
|
|
|
|
FILE *fp = fopen(argv[3], "r");
|
|
|
|
if (!fp) {
|
|
|
|
fprintf(stderr, "Can't open %s.\n", argv[3]);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
fseek(fp, 0, SEEK_END);
|
|
|
|
unsigned int bytecount = ftell(fp);
|
|
|
|
fprintf(stderr, "memptr: 0x%04x bytecount: 0x%04x.\n", memptr, bytecount);
|
2019-12-12 01:24:40 +11:00
|
|
|
if (!bytecount) {
|
|
|
|
// Nothing to read
|
|
|
|
fclose(fp);
|
|
|
|
return 0;
|
|
|
|
}
|
2019-12-10 14:01:22 +11:00
|
|
|
if (memptr+bytecount > 0xffff) {
|
|
|
|
fprintf(stderr, "memptr+bytecount out of range.\n");
|
|
|
|
fclose(fp);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
rewind(fp);
|
|
|
|
int fd = open(argv[1], O_RDWR|O_NOCTTY);
|
2019-12-13 04:04:56 +11:00
|
|
|
char s[0x40];
|
2019-12-10 14:01:22 +11:00
|
|
|
sprintf(s, "m=0x%04x", memptr);
|
2019-12-12 01:24:40 +11:00
|
|
|
sendcmdp(fd, s);
|
2019-12-13 04:04:56 +11:00
|
|
|
sprintf(s, "while m<0x%04x getc:puth a:poke m a:m=m+1", memptr+bytecount);
|
|
|
|
sendcmd(fd, s);
|
2019-12-12 01:24:40 +11:00
|
|
|
|
|
|
|
int returncode = 0;
|
2019-12-10 14:01:22 +11:00
|
|
|
while (fread(s, 1, 1, fp)) {
|
|
|
|
putchar('.');
|
|
|
|
fflush(stdout);
|
|
|
|
unsigned char c = s[0];
|
|
|
|
write(fd, &c, 1);
|
2019-12-12 01:24:40 +11:00
|
|
|
usleep(1000); // let it breathe
|
2019-12-10 14:01:22 +11:00
|
|
|
read(fd, s, 2); // read hex pair
|
|
|
|
s[2] = 0; // null terminate
|
|
|
|
unsigned char c2 = strtol(s, NULL, 16);
|
|
|
|
if (c != c2) {
|
|
|
|
// mismatch!
|
|
|
|
unsigned int pos = ftell(fp);
|
|
|
|
fprintf(stderr, "Mismatch at byte %d! %d != %d.\n", pos, c, c2);
|
2019-12-12 01:24:40 +11:00
|
|
|
// we don't exit now because we need to "consume" our whole program.
|
|
|
|
returncode = 1;
|
2019-12-10 14:01:22 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
printf("Done!\n");
|
2019-12-12 01:24:40 +11:00
|
|
|
fclose(fp);
|
|
|
|
return returncode;
|
2019-12-10 14:01:22 +11:00
|
|
|
}
|
|
|
|
|