OpenCore  1.0.4
OpenCore Bootloader
Loading...
Searching...
No Matches
Ip4Utils.c
Go to the documentation of this file.
1
12#include "NetworkBootInternal.h"
13
26BOOLEAN
28 IN IP4_ADDR Ip,
29 IN IP4_ADDR Netmask
30 )
31{
32 //
33 // Only support the station address with 0.0.0.0/0 to enable DHCP client.
34 //
35 if (Netmask == IP4_ALLZERO_ADDRESS) {
36 return (BOOLEAN)(Ip == IP4_ALLZERO_ADDRESS);
37 }
38
39 //
40 // Only support the continuous net masks
41 //
42 if (NetGetMaskLength (Netmask) == (IP4_MASK_MAX + 1)) {
43 return FALSE;
44 }
45
46 //
47 // Station address can't be class D or class E address
48 //
49 if (NetGetIpClass (Ip) > IP4_ADDR_CLASSC) {
50 return FALSE;
51 }
52
53 return NetIp4IsUnicast (Ip, Netmask);
54}
55
65UINT8
67 IN EFI_IPv4_ADDRESS *SubnetMask
68 )
69{
70 UINT8 Len;
71 UINT32 ReverseMask;
72
73 //
74 // The SubnetMask is in network byte order.
75 //
76 ReverseMask = SwapBytes32 (*(UINT32 *)&SubnetMask[0]);
77
78 //
79 // Reverse it.
80 //
81 ReverseMask = ~ReverseMask;
82
83 if ((ReverseMask & (ReverseMask + 1)) != 0) {
84 return 0;
85 }
86
87 Len = 0;
88
89 while (ReverseMask != 0) {
90 ReverseMask = ReverseMask >> 1;
91 Len++;
92 }
93
94 return (UINT8)(32 - Len);
95}
96
107EFI_STATUS
109 IN CHAR16 *Str,
110 OUT EFI_IPv4_ADDRESS *Ip
111 )
112{
113 UINTN Index;
114 UINTN Number;
115
116 Index = 0;
117
118 while (*Str != L'\0') {
119 if (Index > 3) {
120 return EFI_INVALID_PARAMETER;
121 }
122
123 Number = 0;
124 while ((*Str >= L'0') && (*Str <= L'9')) {
125 Number = Number * 10 + (*Str - L'0');
126 Str++;
127 }
128
129 if (Number > 0xFF) {
130 return EFI_INVALID_PARAMETER;
131 }
132
133 Ip->Addr[Index] = (UINT8)Number;
134
135 if ((*Str != L'\0') && (*Str != L'.')) {
136 //
137 // The current character should be either the NULL terminator or
138 // the dot delimiter.
139 //
140 return EFI_INVALID_PARAMETER;
141 }
142
143 if (*Str == L'.') {
144 //
145 // Skip the delimiter.
146 //
147 Str++;
148 }
149
150 Index++;
151 }
152
153 if (Index != 4) {
154 return EFI_INVALID_PARAMETER;
155 }
156
157 return EFI_SUCCESS;
158}
159
172EFI_STATUS
174 IN CHAR16 *Str,
175 OUT EFI_IPv4_ADDRESS **PtrIpList,
176 OUT UINTN *IpCount
177 )
178{
179 UINTN BeginIndex;
180 UINTN EndIndex;
181 UINTN Index;
182 UINTN IpIndex;
183 CHAR16 *StrTemp;
184 BOOLEAN SpaceTag;
185
186 BeginIndex = 0;
187 EndIndex = BeginIndex;
188 Index = 0;
189 IpIndex = 0;
190 StrTemp = NULL;
191 SpaceTag = TRUE;
192
193 *PtrIpList = NULL;
194 *IpCount = 0;
195
196 if (Str == NULL) {
197 return EFI_SUCCESS;
198 }
199
200 //
201 // Get the number of Ip.
202 //
203 while (*(Str + Index) != L'\0') {
204 if (*(Str + Index) == L' ') {
205 SpaceTag = TRUE;
206 } else {
207 if (SpaceTag) {
208 (*IpCount)++;
209 SpaceTag = FALSE;
210 }
211 }
212
213 Index++;
214 }
215
216 if (*IpCount == 0) {
217 return EFI_SUCCESS;
218 }
219
220 //
221 // Allocate buffer for IpList.
222 //
223 *PtrIpList = AllocateZeroPool (*IpCount * sizeof (EFI_IPv4_ADDRESS));
224 if (*PtrIpList == NULL) {
225 return EFI_OUT_OF_RESOURCES;
226 }
227
228 //
229 // Get IpList from Str.
230 //
231 Index = 0;
232 while (*(Str + Index) != L'\0') {
233 if (*(Str + Index) == L' ') {
234 if (!SpaceTag) {
235 StrTemp = AllocateZeroPool ((EndIndex - BeginIndex + 1) * sizeof (CHAR16));
236 if (StrTemp == NULL) {
237 FreePool (*PtrIpList);
238 *PtrIpList = NULL;
239 *IpCount = 0;
240 return EFI_OUT_OF_RESOURCES;
241 }
242
243 CopyMem (StrTemp, Str + BeginIndex, (EndIndex - BeginIndex) * sizeof (CHAR16));
244 *(StrTemp + (EndIndex - BeginIndex)) = L'\0';
245
246 if (Ip4Config2StrToIp (StrTemp, &((*PtrIpList)[IpIndex])) != EFI_SUCCESS) {
247 FreePool (StrTemp);
248 FreePool (*PtrIpList);
249 *PtrIpList = NULL;
250 *IpCount = 0;
251 return EFI_INVALID_PARAMETER;
252 }
253
254 BeginIndex = EndIndex;
255 IpIndex++;
256
257 FreePool (StrTemp);
258 }
259
260 BeginIndex++;
261 EndIndex++;
262 SpaceTag = TRUE;
263 } else {
264 EndIndex++;
265 SpaceTag = FALSE;
266 }
267
268 Index++;
269
270 if (*(Str + Index) == L'\0') {
271 if (!SpaceTag) {
272 StrTemp = AllocateZeroPool ((EndIndex - BeginIndex + 1) * sizeof (CHAR16));
273 if (StrTemp == NULL) {
274 FreePool (*PtrIpList);
275 *PtrIpList = NULL;
276 *IpCount = 0;
277 return EFI_OUT_OF_RESOURCES;
278 }
279
280 CopyMem (StrTemp, Str + BeginIndex, (EndIndex - BeginIndex) * sizeof (CHAR16));
281 *(StrTemp + (EndIndex - BeginIndex)) = L'\0';
282
283 if (Ip4Config2StrToIp (StrTemp, &((*PtrIpList)[IpIndex])) != EFI_SUCCESS) {
284 FreePool (StrTemp);
285 FreePool (*PtrIpList);
286 *PtrIpList = NULL;
287 *IpCount = 0;
288 return EFI_INVALID_PARAMETER;
289 }
290
291 FreePool (StrTemp);
292 }
293 }
294 }
295
296 return EFI_SUCCESS;
297}
EFI_STATUS Ip4Config2StrToIpList(IN CHAR16 *Str, OUT EFI_IPv4_ADDRESS **PtrIpList, OUT UINTN *IpCount)
Definition Ip4Utils.c:173
BOOLEAN Ip4StationAddressValid(IN IP4_ADDR Ip, IN IP4_ADDR Netmask)
Definition Ip4Utils.c:27
UINT8 GetSubnetMaskPrefixLength(IN EFI_IPv4_ADDRESS *SubnetMask)
Definition Ip4Utils.c:66
EFI_STATUS Ip4Config2StrToIp(IN CHAR16 *Str, OUT EFI_IPv4_ADDRESS *Ip)
Definition Ip4Utils.c:108
#define IP4_ALLZERO_ADDRESS
VOID *EFIAPI CopyMem(OUT VOID *DestinationBuffer, IN CONST VOID *SourceBuffer, IN UINTN Length)
#define Len
Definition deflate.h:82