-
Notifications
You must be signed in to change notification settings - Fork 0
/
srecord.c
167 lines (147 loc) · 4.13 KB
/
srecord.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/*
* S-record handler
*/
#ifdef WITH_SRECORD
#include "proto.h"
#define MAX_SRECORD_SIZE 80
static uintptr_t srecord_entrypoint = ~(uintptr_t)0;
static int
srecord_onehex(const char *bp)
{
int nh = xdigit(*bp);
int nl = xdigit(*(bp + 1));
if ((nh < 0) || (nl < 0)) {
fmt("invalid hex %c%c", *bp, *(bp + 1));
return -1;
}
return (nh << 4) + nl;
}
static int
srecord_process(const char *input_buffer)
{
uint8_t buf[MAX_SRECORD_SIZE] __attribute__((unused));
board_status(8);
init_loader();
for (;;) {
// basic sanity on the input string
int linelen = strlen(input_buffer);
if ((linelen < 10)
|| (linelen % 2)
|| (input_buffer[0] != 'S')
|| !isdigit(input_buffer[1])
|| (input_buffer[1] == '4')) {
fmt("bad record length / type in %s\n", input_buffer);
return -1;
}
// get the length byte and validate
int count = srecord_onehex(input_buffer + 2);
if (count < 0) {
return -1;
}
if (((count * 2) + 4) > linelen) {
putln("record truncated");
return -1;
}
if (count > MAX_SRECORD_SIZE) {
putln("record too long");
return -1;
}
// get the expected checksum
int expected_sum = srecord_onehex(input_buffer + 4 + 2 * (count - 1));
if (expected_sum < 0) {
return -1;
}
// convert the rest of the input string into hex in the buffer
int measured_sum = count;
for (int index = 0; index < (count - 1); index++) {
int val = srecord_onehex(input_buffer + 4 + 2 * index);
if (val < 0) {
return -1;
}
measured_sum += val;
buf[index] = val;
}
// validate the checksum
measured_sum = ~measured_sum & 0xff;
if (measured_sum != expected_sum) {
fmt("sum 0x%b not 0x%b\n", measured_sum, expected_sum);
return -1;
}
// fetch an address
uint8_t type = input_buffer[1] - '0';
uintptr_t address = 0;
unsigned parse_index = 0;
switch (type) {
case 0:
case 5:
case 6:
// ignore these records
break;
case 3: // 32
case 7:
address += buf[parse_index++];
// FALLTHROUGH
case 2: // 24
case 8:
address <<= 8;
address += buf[parse_index++];
// FALLTHROUGH
case 1: // 16
case 9:
address <<= 8;
address += buf[parse_index++];
address <<= 8;
address += buf[parse_index++];
break;
default:
return -1; // should be unreachable
}
switch (type) {
case 0:
case 5:
case 6:
break;
case 1:
case 2:
case 3:
loader_load_bytes(address, buf + parse_index, count);
break;
case 7:
case 8:
case 9:
loader_set_entry(address);
return 0;
default:
return -1; // should be unreachable
}
putc('.');
do {
input_buffer = getln(100);
if (input_buffer == NULL) {
putln("cancelled");
return 0;
}
} while (strlen(input_buffer) == 0);
}
}
COMMAND(srecord);
static int
srecord(const char *input_buffer)
{
if (input_buffer == NULL) {
putln("<S-records> load a new program");
putln("go start an uploaded program");
} else if ((strlen(input_buffer) > 12)
&& (input_buffer[0] == 'S')
&& isdigit(input_buffer[1])) {
if (srecord_process(input_buffer) == 0) {
fmt("\ns-records loaded, entry address 0x%l. 'go' to run program.\n",
srecord_entrypoint);
return 0;
}
} else if (!strcmp(input_buffer, "go")) {
loader_go(0, 0);
}
return -1;
}
#endif // WITH_SRECORD