forked from nanoframework/nf-interpreter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nanoHAL_ConfigurationManager.c
274 lines (222 loc) · 8.7 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
//
// 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)
{
return g_TargetConfiguration.Wireless80211Configs->Configs[i];
}
}
// 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)
{
return g_TargetConfiguration.WirelessAPConfigs->Configs[i];
}
}
// not found
return NULL;
}
__nfweak bool ConfigurationManager_CheckExistingConfigurationBlock(
void *existingConfigBlock,
void *newConfigBlock,
uint32_t existingConfigBlockSize,
uint32_t newConfigBlockSize)
{
// config blocks parameters are addresses
volatile uint8_t *cursor1 = (volatile uint8_t *)existingConfigBlock;
volatile uint8_t *cursor2 = (volatile uint8_t *)newConfigBlock;
// obvious check
if (existingConfigBlockSize != newConfigBlockSize)
{
return false;
}
while (existingConfigBlockSize--)
{
if (*cursor1++ != *cursor2++)
{
// content is different!!
return false;
}
}
return true;
}