-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathabo.c
More file actions
164 lines (128 loc) · 4.5 KB
/
abo.c
File metadata and controls
164 lines (128 loc) · 4.5 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
/*
Thymio-II Firmware
Copyright (C) 2011 Philippe Retornaz <philippe dot retornaz at epfl dot ch>,
Mobots group (http://mobots.epfl.ch), Robotics system laboratory (http://lsro.epfl.ch)
EPFL Ecole polytechnique federale de Lausanne (http://www.epfl.ch)
See authors.txt for more details about other contributors.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published
by the Free Software Foundation, version 3 of the License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "abo.h"
#include "crc.h"
#include <transport/buffer/vm-buffer.h>
#include <common/consts.h>
#include <skel-usb.h>
#include <string.h>
#define ABO_VERSION 0
#define CRC_POLY 0x1021
#define CRC_SIZE 16
static void add_crc_string(const char * s) {
unsigned long len = strlen(s);
if(len & 0x1)
len++;
crc_process_8(s, len);
}
static unsigned int vmdesc_crc(void) {
unsigned int i;
unsigned int j;
const AsebaVMDescription *vmDescription = AsebaGetVMDescription(&vmState);
const AsebaVariableDescription* namedVariables = vmDescription->variables;
const AsebaLocalEventDescription* localEvents = AsebaGetLocalEventsDescriptions(&vmState);
const AsebaNativeFunctionDescription* const * nativeFunctionsDescription = AsebaGetNativeFunctionsDescriptions(&vmState);
crc_init(CRC_SIZE, CRC_POLY, 0);
crc_process_8(&vmState.bytecodeSize, 2);
crc_process_8(&vmState.variablesSize, 2);
crc_process_8(&vmState.stackSize, 2);
for(i = 0; namedVariables[i].name; i++) {
crc_process_8(&namedVariables[i].size, 2);
add_crc_string(namedVariables[i].name);
}
for(i = 0; localEvents[i].name; i++)
add_crc_string(localEvents[i].name);
for(i = 0; nativeFunctionsDescription[i]; i++) {
add_crc_string(nativeFunctionsDescription[i]->name);
for(j = 0; nativeFunctionsDescription[i]->arguments[j].size; j++) {
crc_process_8(&nativeFunctionsDescription[i]->arguments[j].size, 2);
add_crc_string(nativeFunctionsDescription[i]->arguments[j].name);
}
}
return crc_finish();
}
static unsigned int do_crc(const void * data, unsigned int size) {
unsigned char zero = 0;
crc_init(CRC_SIZE, CRC_POLY, 0);
crc_process_8(data, size);
// Make sure we are aligned on 16bits
if(size & 0x1)
crc_process_8(&zero, 1);
return crc_finish();
}
static unsigned int nodename_crc(void) {
const AsebaVMDescription *vmDescription = AsebaGetVMDescription(&vmState);
int len = strlen(vmDescription->name);
// If odd size, compute the crc with the 0
if(len & 0x1)
len++;
return do_crc(vmDescription->name, len);
}
int abo_load(FIL * f) {
unsigned long magic;
unsigned int t;
unsigned int read;
unsigned int len;
// Magic ID
f_read(f, &magic, sizeof(magic), &read);
if(read != sizeof(magic) || magic != 0x4F4241UL)
return -1;
// Binary file version
f_read(f, &t, sizeof(t), &read);
if(read != sizeof(t) || t != ABO_VERSION)
return -2;
// Protocol version
f_read(f, &t, sizeof(t), &read);
if(read != sizeof(t) || t != ASEBA_PROTOCOL_VERSION)
return -3;
// Product ID
f_read(f, &t, sizeof(t), &read);
if(read != sizeof(t) || t != PRODUCT_ID)
return -4;
// Firmware version
f_read(f, &t, sizeof(t), &read);
if(read != sizeof(t) || t != vmVariables.fwversion[0])
return -5;
// Node ID (relax this test, RF Thymio has dynamic ID)
f_read(f, &t, sizeof(t), &read);
/*if(read != sizeof(t) || t != vmVariables.id)
return -6;*/
// Node name (CRC)
f_read(f, &t, sizeof(t), &read);
if(read != sizeof(t) || nodename_crc() != t)
return -7;
// VM description (CRC)
f_read(f, &t, sizeof(t), &read);
if(read != sizeof(t) || vmdesc_crc() != t)
return -8;
// Bytecode size
f_read(f, &len, sizeof(len), &read);
if(read != sizeof(len) || len > VM_BYTECODE_SIZE)
return -9;
// Bytecode payload
f_read(f, vmState.bytecode, len * 2, &read);
if(read != len * 2)
goto out_clean;
// Bytecode CRC
f_read(f, &t, sizeof(t), &read);
if(read != sizeof(t) || do_crc(vmState.bytecode, 2*len) != t)
goto out_clean;
return 0;
out_clean:
memset(vmState.bytecode, 0, VM_BYTECODE_SIZE * 2);
return -11;
}