forked from nanoframework/nf-interpreter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nanoHAL_ConfigurationManager.c
167 lines (131 loc) · 5.59 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
//
// Copyright (c) 2018 The nanoFramework project contributors
// See LICENSE file in the project root for full license information.
//
#include <string.h>
#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 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;
}