forked from nanoframework/nf-interpreter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nanoHAL_ConfigurationManager.c
341 lines (276 loc) · 11 KB
/
nanoHAL_ConfigurationManager.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
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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
//
// Copyright (c) .NET Foundation and Contributors
// See LICENSE file in the project root for full license information.
//
#include <nanoHAL_v2.h>
#include <nanoHAL_Network.h>
uint32_t FindNextBlock(uint32_t startAddress, uint32_t endAddress, const unsigned char *marker)
{
// all configuration markers are 4 bytes length
unsigned char *cursor = (unsigned char *)startAddress;
while (cursor < (unsigned char *)endAddress - 4)
{
if (memcmp(cursor, marker, 4) == 0)
{
// found one!
break;
}
cursor++;
}
return (uint32_t)cursor;
}
uint32_t GetBlockCount(uint32_t startAddress, uint32_t endAddress, uint32_t blockSize, const unsigned char *marker)
{
// all configuration markers are 4 bytes length
int blockCount = 0;
unsigned char *cursor = (unsigned char *)startAddress;
while (cursor < (unsigned char *)endAddress - 4)
{
if (memcmp(cursor, marker, 4) == 0)
{
// found one!
blockCount++;
// bump cursor to the end of the config block size
cursor += blockSize;
}
else
{
cursor++;
}
}
return blockCount;
}
__nfweak void *ConfigurationManager_FindNetworkConfigurationBlocks(uint32_t startAddress, uint32_t endAddress)
{
uint32_t nextBlock = startAddress;
// first pass: find out how many blocks of this type we have
uint32_t blockCount = GetBlockCount(
startAddress,
endAddress,
sizeof(HAL_Configuration_NetworkInterface),
c_MARKER_CONFIGURATION_NETWORK_V1);
// allocate config struct
HAL_CONFIGURATION_NETWORK *networkConfigs = (HAL_CONFIGURATION_NETWORK *)platform_malloc(
offsetof(HAL_CONFIGURATION_NETWORK, Configs) + blockCount * sizeof(networkConfigs->Configs[0]));
// set collection count
networkConfigs->Count = blockCount;
if (blockCount > 0)
{
// second pass: get address of each config block
for (uint32_t i = 0; i < blockCount; i++)
{
nextBlock = FindNextBlock(nextBlock, endAddress, c_MARKER_CONFIGURATION_NETWORK_V1);
networkConfigs->Configs[i] = (HAL_Configuration_NetworkInterface *)nextBlock;
}
}
return networkConfigs;
}
__nfweak void *ConfigurationManager_FindNetworkWireless80211ConfigurationBlocks(
uint32_t startAddress,
uint32_t endAddress)
{
uint32_t nextBlock = startAddress;
// first pass: find out how many blocks of this type we have
uint32_t blockCount = GetBlockCount(
startAddress,
endAddress,
sizeof(HAL_Configuration_Wireless80211),
c_MARKER_CONFIGURATION_WIRELESS80211_V1);
// allocate config struct
HAL_CONFIGURATION_NETWORK_WIRELESS80211 *networkWirelessConfigs =
(HAL_CONFIGURATION_NETWORK_WIRELESS80211 *)platform_malloc(
offsetof(HAL_CONFIGURATION_NETWORK_WIRELESS80211, Configs) +
blockCount * sizeof(networkWirelessConfigs->Configs[0]));
// set collection count
networkWirelessConfigs->Count = blockCount;
if (blockCount > 0)
{
// second pass: get address of each config block
for (uint32_t i = 0; i < blockCount; i++)
{
nextBlock = FindNextBlock(nextBlock, endAddress, c_MARKER_CONFIGURATION_WIRELESS80211_V1);
networkWirelessConfigs->Configs[i] = (HAL_Configuration_Wireless80211 *)nextBlock;
}
}
return networkWirelessConfigs;
}
__nfweak void *ConfigurationManager_FindX509CertificateConfigurationBlocks(uint32_t startAddress, uint32_t endAddress)
{
uint32_t nextBlock = startAddress;
uint32_t allocationSize = 0;
// first pass: find out how many blocks of this type we have
// because these blocks have an unknow size, need to call this without a fixed size
uint32_t blockCount = GetBlockCount(startAddress, endAddress, 1, c_MARKER_CONFIGURATION_X509CAROOTBUNDLE_V1);
// start computing allocation size, first part is the struct initial fields
allocationSize = offsetof(HAL_CONFIGURATION_X509_CERTIFICATE, Certificates);
// second pass: find out the size of each X509 certificate (because they can have different sizes and we need this
// to allocate memory for the struct)
if (blockCount > 0)
{
for (uint32_t i = 0; i < blockCount; i++)
{
nextBlock = FindNextBlock(nextBlock, endAddress, c_MARKER_CONFIGURATION_X509CAROOTBUNDLE_V1);
// header
allocationSize += offsetof(HAL_Configuration_X509CaRootBundle, Certificate);
// certificate
allocationSize += ((HAL_Configuration_X509CaRootBundle *)nextBlock)->CertificateSize;
}
}
// allocate config struct
HAL_CONFIGURATION_X509_CERTIFICATE *certificateStore =
(HAL_CONFIGURATION_X509_CERTIFICATE *)platform_malloc(allocationSize);
// set collection count
certificateStore->Count = blockCount;
if (blockCount > 0)
{
// second pass: get address of each config block
for (uint32_t i = 0; i < blockCount; i++)
{
nextBlock = FindNextBlock(nextBlock, endAddress, c_MARKER_CONFIGURATION_X509CAROOTBUNDLE_V1);
certificateStore->Certificates[i] = (HAL_Configuration_X509CaRootBundle *)nextBlock;
}
}
return certificateStore;
}
__nfweak void *ConfigurationManager_FindX509DeviceCertificatesConfigurationBlocks(
uint32_t startAddress,
uint32_t endAddress)
{
uint32_t nextBlock = startAddress;
uint32_t allocationSize = 0;
// first pass: find out how many blocks of this type we have
// because these blocks have an unknow size, need to call this without a fixed size
uint32_t blockCount = GetBlockCount(startAddress, endAddress, 1, c_MARKER_CONFIGURATION_X509DEVICECERTIFICATE_V1);
// start computing allocation size, first part is the struct initial fields
allocationSize = offsetof(HAL_CONFIGURATION_X509_DEVICE_CERTIFICATE, Certificates);
// second pass: find out the size of each X509 certificate (because they can have different sizes and we need this
// to allocate memory for the struct)
if (blockCount > 0)
{
for (uint32_t i = 0; i < blockCount; i++)
{
nextBlock = FindNextBlock(nextBlock, endAddress, c_MARKER_CONFIGURATION_X509DEVICECERTIFICATE_V1);
// header
allocationSize += offsetof(HAL_Configuration_X509DeviceCertificate, Certificate);
// certificate
allocationSize += ((HAL_Configuration_X509DeviceCertificate *)nextBlock)->CertificateSize;
}
}
// allocate config struct
HAL_CONFIGURATION_X509_DEVICE_CERTIFICATE *deviceCertificates =
(HAL_CONFIGURATION_X509_DEVICE_CERTIFICATE *)platform_malloc(allocationSize);
// set collection count
deviceCertificates->Count = blockCount;
if (blockCount > 0)
{
// second pass: get address of each config block
for (uint32_t i = 0; i < blockCount; i++)
{
nextBlock = FindNextBlock(nextBlock, endAddress, c_MARKER_CONFIGURATION_X509DEVICECERTIFICATE_V1);
deviceCertificates->Certificates[i] = (HAL_Configuration_X509DeviceCertificate *)nextBlock;
}
}
return deviceCertificates;
}
__nfweak HAL_Configuration_Wireless80211 *ConfigurationManager_GetWirelessConfigurationFromId(uint32_t configurationId)
{
for (int i = 0; i < g_TargetConfiguration.Wireless80211Configs->Count; i++)
{
if (g_TargetConfiguration.Wireless80211Configs->Configs[i]->Id == configurationId)
{
// need to make a copy
HAL_Configuration_Wireless80211 *configBlock =
(HAL_Configuration_Wireless80211 *)platform_malloc(sizeof(HAL_Configuration_Wireless80211));
// check allocation
if (configBlock)
{
memcpy(
configBlock,
g_TargetConfiguration.Wireless80211Configs->Configs[i],
sizeof(HAL_Configuration_Wireless80211));
return configBlock;
}
}
}
// not found
return NULL;
}
__nfweak HAL_Configuration_WirelessAP *ConfigurationManager_GetWirelessAPConfigurationFromId(uint32_t configurationId)
{
for (int i = 0; i < g_TargetConfiguration.WirelessAPConfigs->Count; i++)
{
if (g_TargetConfiguration.WirelessAPConfigs->Configs[i]->Id == configurationId)
{
// need to make a copy
HAL_Configuration_WirelessAP *configBlock =
(HAL_Configuration_WirelessAP *)platform_malloc(sizeof(HAL_Configuration_WirelessAP));
// check allocation
if (configBlock)
{
memcpy(
configBlock,
g_TargetConfiguration.WirelessAPConfigs->Configs[i],
sizeof(HAL_Configuration_WirelessAP));
return configBlock;
}
}
}
// not found
return NULL;
}
__nfweak HAL_Configuration_X509CaRootBundle *ConfigurationManager_GetCertificateStore()
{
if (g_TargetConfiguration.CertificateStore->Count)
{
// need to make a copy
// need to compute size as the cert size is variable
int32_t blockSize = offsetof(HAL_Configuration_X509CaRootBundle, Certificate) +
g_TargetConfiguration.CertificateStore->Certificates[0]->CertificateSize;
HAL_Configuration_X509CaRootBundle *configBlock =
(HAL_Configuration_X509CaRootBundle *)platform_malloc(blockSize);
// check allocation
if (configBlock)
{
memcpy(configBlock, g_TargetConfiguration.CertificateStore->Certificates[0], blockSize);
return configBlock;
}
}
// not found
return NULL;
}
__nfweak HAL_Configuration_X509DeviceCertificate *ConfigurationManager_GetDeviceCertificate()
{
if (g_TargetConfiguration.DeviceCertificates->Count)
{
// need to make a copy
// need to compute size as the cert size is variable
int32_t blockSize = offsetof(HAL_Configuration_X509DeviceCertificate, Certificate) +
g_TargetConfiguration.DeviceCertificates->Certificates[0]->CertificateSize;
HAL_Configuration_X509DeviceCertificate *configBlock =
(HAL_Configuration_X509DeviceCertificate *)platform_malloc(blockSize);
// check allocation
if (configBlock)
{
memcpy(configBlock, g_TargetConfiguration.DeviceCertificates->Certificates[0], blockSize);
return configBlock;
}
}
// not found
return NULL;
}
__nfweak bool ConfigurationManager_CheckExistingConfigurationBlock(
void *existingConfigBlock,
void *newConfigBlock,
uint32_t existingConfigBlockSize,
uint32_t newConfigBlockSize)
{
// config blocks parameters are addresses
uint8_t *cursor1 = (uint8_t *)existingConfigBlock;
uint8_t *cursor2 = (uint8_t *)newConfigBlock;
// obvious check
if (existingConfigBlockSize != newConfigBlockSize)
{
return false;
}
return memcmp(cursor1, cursor2, existingConfigBlockSize) == 0;
}