2019-12-09 22:01:22 -05:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <unistd.h>
|
2020-12-12 10:06:55 -05:00
|
|
|
#include <stdint.h>
|
2019-12-09 22:01:22 -05:00
|
|
|
|
|
|
|
#include "common.h"
|
|
|
|
|
2020-04-13 10:25:27 -04:00
|
|
|
/* Push specified file to specified device running Forth and verify
|
2019-12-09 22:01:22 -05: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-11 09:24:40 -05:00
|
|
|
if (!bytecount) {
|
|
|
|
// Nothing to read
|
|
|
|
fclose(fp);
|
|
|
|
return 0;
|
|
|
|
}
|
2019-12-09 22:01:22 -05:00
|
|
|
if (memptr+bytecount > 0xffff) {
|
|
|
|
fprintf(stderr, "memptr+bytecount out of range.\n");
|
|
|
|
fclose(fp);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
rewind(fp);
|
2020-07-02 11:36:53 -04:00
|
|
|
int fd = ttyopen(argv[1]);
|
2020-04-13 10:25:27 -04:00
|
|
|
if (fd < 0) {
|
|
|
|
fprintf(stderr, "Could not open %s\n", argv[1]);
|
|
|
|
return 1;
|
|
|
|
}
|
2020-12-12 10:06:55 -05:00
|
|
|
uint16_t checksum = 0;
|
2019-12-12 12:04:56 -05:00
|
|
|
char s[0x40];
|
2020-12-12 10:06:55 -05:00
|
|
|
// Write all received bytes, keeping a checksum, then print that checksum
|
2020-04-13 10:25:27 -04:00
|
|
|
sprintf(s,
|
2020-12-12 10:06:55 -05:00
|
|
|
": _ 0 %d %d DO KEY DUP I C! + LOOP .X ; _",
|
2020-04-13 10:25:27 -04:00
|
|
|
memptr+bytecount, memptr);
|
2019-12-12 12:04:56 -05:00
|
|
|
sendcmd(fd, s);
|
2019-12-11 09:24:40 -05:00
|
|
|
|
|
|
|
int returncode = 0;
|
2019-12-09 22:01:22 -05:00
|
|
|
while (fread(s, 1, 1, fp)) {
|
2020-07-02 11:36:53 -04:00
|
|
|
putc('.', stderr);
|
|
|
|
fflush(stderr);
|
2019-12-09 22:01:22 -05:00
|
|
|
unsigned char c = s[0];
|
2020-12-12 10:06:55 -05:00
|
|
|
checksum += c;
|
2019-12-09 22:01:22 -05:00
|
|
|
write(fd, &c, 1);
|
2019-12-11 09:24:40 -05:00
|
|
|
usleep(1000); // let it breathe
|
2020-12-12 10:06:55 -05:00
|
|
|
}
|
|
|
|
fprintf(stderr, "\nBytes sent, checksum...\n");
|
|
|
|
mread(fd, s, 4); // read hex string
|
|
|
|
s[4] = 0; // null terminate
|
|
|
|
uint16_t checksum2 = strtol(s, NULL, 16);
|
|
|
|
if (checksum == checksum2) {
|
|
|
|
fprintf(stderr, "OK\n");
|
|
|
|
} else {
|
|
|
|
fprintf(stderr, "Mismatch! Expected %04x and got %04x.\n", checksum, checksum2);
|
|
|
|
returncode = 1;
|
2019-12-09 22:01:22 -05:00
|
|
|
}
|
2020-05-01 14:39:13 -04:00
|
|
|
readprompt(fd);
|
2020-04-13 10:25:27 -04:00
|
|
|
sendcmdp(fd, "FORGET _");
|
2019-12-11 09:24:40 -05:00
|
|
|
fclose(fp);
|
2020-07-02 11:36:53 -04:00
|
|
|
if (fd > 0) {
|
|
|
|
close(fd);
|
|
|
|
}
|
2019-12-11 09:24:40 -05:00
|
|
|
return returncode;
|
2019-12-09 22:01:22 -05:00
|
|
|
}
|
|
|
|
|