-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Expand file tree
/
Copy pathxmodem.cpp
More file actions
318 lines (302 loc) · 12.9 KB
/
Copy pathxmodem.cpp
File metadata and controls
318 lines (302 loc) · 12.9 KB
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
/**
* @file xmodem.cpp
* @brief Implementation of XMODEM protocol for Meshtastic devices.
*
* This file contains the implementation of the XMODEM protocol for Meshtastic devices. It is based on the XMODEM implementation
* by Georges Menie (www.menie.org) and has been adapted for protobuf encapsulation.
*
* The XMODEM protocol is used for reliable transmission of binary data over a serial connection. This implementation supports
* both sending and receiving of data.
*
* The XModemAdapter class provides the main functionality for the protocol, including CRC calculation, packet handling, and
* control signal sending.
*
* @copyright Copyright (c) 2001-2019 Georges Menie
* @author
* @author
* @date
*/
/***********************************************************************************************************************
* based on XMODEM implementation by Georges Menie (www.menie.org)
***********************************************************************************************************************
* Copyright 2001-2019 Georges Menie (www.menie.org)
* All rights reserved.
*
* Adapted for protobuf encapsulation. this is not really Xmodem any more.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of the University of California, Berkeley nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
**********************************************************************************************************************/
#include "xmodem.h"
#include "SPILock.h"
#include <cstring>
#ifdef FSCom
XModemAdapter xModem;
XModemAdapter::XModemAdapter() {}
bool XModemAdapter::isValidFilename(const char *name)
{
if (!name || name[0] == '\0')
return false;
const bool driveLetter = (name[0] >= 'A' && name[0] <= 'Z') || (name[0] >= 'a' && name[0] <= 'z');
if (driveLetter && name[1] == ':')
return false;
// Reject any ".." path component. Absolute paths and subdirectories are fine; they stay within
// the filesystem root, so only traversal out of it needs blocking.
for (const char *seg = name; *seg;) {
const char *slash = strpbrk(seg, "/\\");
const size_t len = slash ? (size_t)(slash - seg) : strlen(seg);
if (len == 2 && seg[0] == '.' && seg[1] == '.')
return false;
if (!slash)
break;
seg = slash + 1;
}
return true;
}
/**
* Calculates the CRC-16 CCITT checksum of the given buffer.
*
* @param buffer The buffer to calculate the checksum for.
* @param length The length of the buffer.
* @return The calculated checksum.
*/
unsigned short XModemAdapter::crc16_ccitt(const pb_byte_t *buffer, int length)
{
unsigned short crc16 = 0;
while (length != 0) {
crc16 = (unsigned char)(crc16 >> 8) | (crc16 << 8);
crc16 ^= *buffer;
crc16 ^= (unsigned char)(crc16 & 0xff) >> 4;
crc16 ^= (crc16 << 8) << 4;
crc16 ^= ((crc16 & 0xff) << 4) << 1;
buffer++;
length--;
}
return crc16;
}
/**
* Calculates the checksum of the given buffer and compares it to the given
* expected checksum. Returns 1 if the checksums match, 0 otherwise.
*
* @param buf The buffer to calculate the checksum of.
* @param sz The size of the buffer.
* @param tcrc The expected checksum.
* @return 1 if the checksums match, 0 otherwise.
*/
int XModemAdapter::check(const pb_byte_t *buf, int sz, unsigned short tcrc)
{
return crc16_ccitt(buf, sz) == tcrc;
}
void XModemAdapter::sendControl(meshtastic_XModem_Control c)
{
xmodemStore = meshtastic_XModem_init_zero;
xmodemStore.control = c;
LOG_DEBUG("XModem: Notify Send control %d", c);
packetReady.notifyObservers(packetno);
}
meshtastic_XModem XModemAdapter::getForPhone()
{
return xmodemStore;
}
void XModemAdapter::resetForPhone()
{
xmodemStore = meshtastic_XModem_init_zero;
}
void XModemAdapter::handlePacket(meshtastic_XModem xmodemPacket)
{
switch (xmodemPacket.control) {
case meshtastic_XModem_Control_SOH:
case meshtastic_XModem_Control_STX:
if ((xmodemPacket.seq == 0) && !isReceiving && !isTransmitting) {
// NULL packet has the destination filename
strncpy(filename, (const char *)xmodemPacket.buffer.bytes, sizeof(filename) - 1);
filename[sizeof(filename) - 1] = '\0';
// The filename is attacker-controlled; refuse a ".." that could write/read/delete
// outside the filesystem root (real host paths on the posix daemon).
if (!isValidFilename(filename)) {
LOG_WARN("XModem: rejecting unsafe filename");
sendControl(meshtastic_XModem_Control_NAK);
break;
}
if (xmodemPacket.control == meshtastic_XModem_Control_SOH) { // Receive this file and put to Flash
// FILE_O_WRITE on Adafruit_LittleFS is append, not truncate - remove first.
spiLock->lock();
if (FSCom.exists(filename))
FSCom.remove(filename);
file = FSCom.open(filename, FILE_O_WRITE);
spiLock->unlock();
if (file) {
LOG_INFO("XModem: receiving %s", filename);
sendControl(meshtastic_XModem_Control_ACK);
isReceiving = true;
packetno = 1;
break;
}
LOG_WARN("XModem: open(%s, WRITE) failed", filename);
sendControl(meshtastic_XModem_Control_NAK);
isReceiving = false;
break;
} else { // Transmit this file from Flash
LOG_INFO("XModem: Transmit file %s", filename);
spiLock->lock();
file = FSCom.open(filename, FILE_O_READ);
spiLock->unlock();
if (file) {
packetno = 1;
isTransmitting = true;
xmodemStore = meshtastic_XModem_init_zero;
xmodemStore.control = meshtastic_XModem_Control_SOH;
xmodemStore.seq = packetno;
spiLock->lock();
xmodemStore.buffer.size = file.read(xmodemStore.buffer.bytes, sizeof(meshtastic_XModem_buffer_t::bytes));
spiLock->unlock();
xmodemStore.crc16 = crc16_ccitt(xmodemStore.buffer.bytes, xmodemStore.buffer.size);
LOG_DEBUG("XModem: STX Notify Send packet %d, %d Bytes", packetno, xmodemStore.buffer.size);
if (xmodemStore.buffer.size < sizeof(meshtastic_XModem_buffer_t::bytes)) {
isEOT = true;
// send EOT on next Ack
}
packetReady.notifyObservers(packetno);
break;
}
sendControl(meshtastic_XModem_Control_NAK);
isTransmitting = false;
break;
}
} else {
if (isReceiving) {
// normal file data packet
if ((xmodemPacket.seq == packetno) &&
check(xmodemPacket.buffer.bytes, xmodemPacket.buffer.size, xmodemPacket.crc16)) {
// valid packet
spiLock->lock();
size_t written = file.write(xmodemPacket.buffer.bytes, xmodemPacket.buffer.size);
spiLock->unlock();
if (written != xmodemPacket.buffer.size) {
LOG_WARN("XModem: short write seq=%d expected=%d wrote=%d (LittleFS partition full?)",
(int)xmodemPacket.seq, (int)xmodemPacket.buffer.size, (int)written);
}
sendControl(meshtastic_XModem_Control_ACK);
packetno++;
break;
}
// invalid packet
sendControl(meshtastic_XModem_Control_NAK);
break;
} else if (isTransmitting) {
// just received something weird.
sendControl(meshtastic_XModem_Control_CAN);
isTransmitting = false;
break;
}
}
break;
case meshtastic_XModem_Control_EOT:
// End of transmission
sendControl(meshtastic_XModem_Control_ACK);
spiLock->lock();
file.flush();
file.close();
spiLock->unlock();
isReceiving = false;
break;
case meshtastic_XModem_Control_CAN:
// Cancel transmission and remove file
sendControl(meshtastic_XModem_Control_ACK);
spiLock->lock();
file.flush();
file.close();
FSCom.remove(filename);
spiLock->unlock();
isReceiving = false;
break;
case meshtastic_XModem_Control_ACK:
// Acknowledge Send the next packet
if (isTransmitting) {
if (isEOT) {
sendControl(meshtastic_XModem_Control_EOT);
spiLock->lock();
file.close();
spiLock->unlock();
LOG_INFO("XModem: Finished send file %s", filename);
isTransmitting = false;
isEOT = false;
break;
}
retrans = MAXRETRANS; // reset retransmit counter
packetno++;
xmodemStore = meshtastic_XModem_init_zero;
xmodemStore.control = meshtastic_XModem_Control_SOH;
xmodemStore.seq = packetno;
spiLock->lock();
xmodemStore.buffer.size = file.read(xmodemStore.buffer.bytes, sizeof(meshtastic_XModem_buffer_t::bytes));
spiLock->unlock();
xmodemStore.crc16 = crc16_ccitt(xmodemStore.buffer.bytes, xmodemStore.buffer.size);
LOG_DEBUG("XModem: ACK Notify Send packet %d, %d Bytes", packetno, xmodemStore.buffer.size);
if (xmodemStore.buffer.size < sizeof(meshtastic_XModem_buffer_t::bytes)) {
isEOT = true;
// send EOT on next Ack
}
packetReady.notifyObservers(packetno);
} else {
// just received something weird.
sendControl(meshtastic_XModem_Control_CAN);
}
break;
case meshtastic_XModem_Control_NAK:
// Negative acknowledge. Send the same buffer again
if (isTransmitting) {
if (--retrans <= 0) {
sendControl(meshtastic_XModem_Control_CAN);
spiLock->lock();
file.close();
spiLock->unlock();
LOG_INFO("XModem: Retransmit timeout, cancel file %s", filename);
isTransmitting = false;
break;
}
xmodemStore = meshtastic_XModem_init_zero;
xmodemStore.control = meshtastic_XModem_Control_SOH;
xmodemStore.seq = packetno;
spiLock->lock();
file.seek((packetno - 1) * sizeof(meshtastic_XModem_buffer_t::bytes));
xmodemStore.buffer.size = file.read(xmodemStore.buffer.bytes, sizeof(meshtastic_XModem_buffer_t::bytes));
spiLock->unlock();
xmodemStore.crc16 = crc16_ccitt(xmodemStore.buffer.bytes, xmodemStore.buffer.size);
LOG_DEBUG("XModem: NAK Notify Send packet %d, %d Bytes", packetno, xmodemStore.buffer.size);
if (xmodemStore.buffer.size < sizeof(meshtastic_XModem_buffer_t::bytes)) {
isEOT = true;
// send EOT on next Ack
}
packetReady.notifyObservers(packetno);
} else {
// just received something weird.
sendControl(meshtastic_XModem_Control_CAN);
}
break;
default:
// Unknown control character
break;
}
}
#endif