]>
git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Universal/Network/Mtftp4Dxe/Mtftp4Option.c
3 Copyright (c) 2006, Intel Corporation
4 All rights reserved. This program and the accompanying materials
5 are licensed and made available under the terms and conditions of the BSD License
6 which accompanies this distribution. The full text of the license may be found at
7 http://opensource.org/licenses/bsd-license.php
9 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
10 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
17 routines to process MTFTP4 options
22 #include "Mtftp4Impl.h"
24 CHAR8
*mMtftp4SupportedOptions
[MTFTP4_SUPPORTED_OPTIONS
] = {
33 Go through the packet to fill the Options array with the start
34 addresses of each MTFTP option name/value pair.
36 @param Packet The packet to check
37 @param PacketLen The packet's length
38 @param Count The size of the Options on input. The actual
40 @param Options The option array to fill in
42 @retval EFI_INVALID_PARAMETER The packet is mal-formated
43 @retval EFI_BUFFER_TOO_SMALL The Options array is too small
44 @retval EFI_SUCCESS The packet has been parsed into the Options array.
50 IN EFI_MTFTP4_PACKET
*Packet
,
53 OUT EFI_MTFTP4_OPTION
*Options OPTIONAL
63 Cur
= (UINT8
*) Packet
+ MTFTP4_OPCODE_LEN
;
64 Last
= (UINT8
*) Packet
+ PacketLen
- 1;
67 // process option name and value pairs. The last byte is always zero
77 return EFI_INVALID_PARAMETER
;
88 if ((Options
!= NULL
) && (Num
<= *Count
)) {
89 Options
[Num
- 1].OptionStr
= Name
;
90 Options
[Num
- 1].ValueStr
= Value
;
96 if ((*Count
< Num
) || (Options
== NULL
)) {
98 return EFI_BUFFER_TOO_SMALL
;
107 Allocate and fill in a array of Mtftp options from the Packet. It
108 first calls Mtftp4FillOption to get the option number, then allocate
109 the array, at last, call Mtftp4FillOption again to save the options.
111 @param Packet The packet to parse
112 @param PacketLen The length of the packet
113 @param OptionCount The number of options in the packet
114 @param OptionList The point to get the option array.
116 @retval EFI_INVALID_PARAMETER The parametera are invalid or packet isn't a
117 well-formated OACK packet.
118 @retval EFI_SUCCESS The option array is build
119 @retval EFI_OUT_OF_RESOURCES Failed to allocate memory for the array
123 Mtftp4ExtractOptions (
124 IN EFI_MTFTP4_PACKET
*Packet
,
126 IN OUT UINT32
*OptionCount
,
127 OUT EFI_MTFTP4_OPTION
**OptionList OPTIONAL
134 if (OptionList
!= NULL
) {
138 if (NTOHS (Packet
->OpCode
) != EFI_MTFTP4_OPCODE_OACK
) {
139 return EFI_INVALID_PARAMETER
;
142 if (PacketLen
== MTFTP4_OPCODE_LEN
) {
147 // The last byte must be zero to terminate the options
149 if (*((UINT8
*) Packet
+ PacketLen
- 1) != 0) {
150 return EFI_INVALID_PARAMETER
;
154 // Get the number of options
156 Status
= Mtftp4FillOptions (Packet
, PacketLen
, OptionCount
, NULL
);
158 if ((Status
== EFI_SUCCESS
) || (Status
!= EFI_BUFFER_TOO_SMALL
)) {
163 // Allocate memory for the options, then call Mtftp4FillOptions to
164 // fill it if caller want that.
166 if (OptionList
== NULL
) {
170 *OptionList
= NetAllocatePool (*OptionCount
* sizeof (EFI_MTFTP4_OPTION
));
172 if (*OptionList
== NULL
) {
173 return EFI_OUT_OF_RESOURCES
;
176 Mtftp4FillOptions (Packet
, PacketLen
, OptionCount
, *OptionList
);
182 Check whether two ascii strings are equel, ignore the case.
184 @param Str1 The first ascii string
185 @param Str2 The second ascii string
187 @retval TRUE Two strings are equal when case is ignored.
188 @retval FALSE Two string are not equal.
192 NetStringEqualNoCase (
200 ASSERT ((Str1
!= NULL
) && (Str2
!= NULL
));
202 for (; (*Str1
!= '\0') && (*Str2
!= '\0'); Str1
++, Str2
++) {
207 // Convert them to lower case then compare two
209 if (('A' <= Ch1
) && (Ch1
<= 'Z')) {
213 if (('A' <= Ch2
) && (Ch2
<= 'Z')) {
222 return (BOOLEAN
) (*Str1
== *Str2
);
227 Convert a string to a UINT32 number.
229 @param Str The string to convert from
231 @return The number get from the string
241 ASSERT (Str
!= NULL
);
245 for (; NET_IS_DIGIT (*Str
); Str
++) {
246 Num
= Num
* 10 + (*Str
- '0');
254 Convert a string of the format "192.168.0.1" to an IP address.
256 @param Str The string representation of IP
257 @param Ip The varible to get IP.
259 @retval EFI_INVALID_PARAMETER The IP string is invalid.
260 @retval EFI_SUCCESS The IP is parsed into the Ip
277 for (Index
= 0; Index
< 4; Index
++) {
278 if (!NET_IS_DIGIT (*Str
)) {
279 return EFI_INVALID_PARAMETER
;
282 Byte
= NetStringToU32 (Str
);
285 return EFI_INVALID_PARAMETER
;
288 Addr
= (Addr
<< 8) | Byte
;
291 // Skip all the digitals and check whether the sepeator is the dot
293 while (NET_IS_DIGIT (*Str
)) {
297 if ((Index
< 3) && (*Str
!= '.')) {
298 return EFI_INVALID_PARAMETER
;
311 Parse the MTFTP multicast option.
313 @param Value The Mtftp multicast value string
314 @param Option The option to save the info into.
316 @retval EFI_INVALID_PARAMETER The multicast value string is invalid.
317 @retval EFI_SUCCESS The multicast value is parsed into the Option
324 IN MTFTP4_OPTION
*Option
331 // The multicast option is formated like "204.0.0.1,1857,1"
332 // The server can also omit the ip and port, use ",,1"
337 Status
= NetStringToIp (Value
, &Option
->McastIp
);
339 if (EFI_ERROR (Status
)) {
343 while (*Value
&& (*Value
!= ',')) {
349 return EFI_INVALID_PARAMETER
;
355 // Convert the port setting. the server can send us a port number or
356 // empty string. such as the port in ",,1"
359 Option
->McastPort
= 0;
361 Num
= NetStringToU32 (Value
);
364 return EFI_INVALID_PARAMETER
;
367 Option
->McastPort
= (UINT16
)Num
;
369 while (NET_IS_DIGIT (*Value
)) {
375 return EFI_INVALID_PARAMETER
;
381 // Check the master/slave setting, 1 for master, 0 for slave.
383 Num
= NetStringToU32 (Value
);
385 if ((Num
!= 0) && (Num
!= 1)) {
386 return EFI_INVALID_PARAMETER
;
389 Option
->Master
= (BOOLEAN
)(Num
== 1);
391 while (NET_IS_DIGIT (*Value
)) {
395 if (*Value
!= '\0') {
396 return EFI_INVALID_PARAMETER
;
404 Parse the option in Options array to MTFTP4_OPTION which program
407 @param Options The option array, which contains addresses of each
408 option's name/value string.
409 @param Count The number of options in the Options
410 @param Request Whether this is a request or OACK. The format of
411 multicast is different according to this setting.
412 @param MtftpOption The MTFTP4_OPTION for easy access.
414 @retval EFI_INVALID_PARAMETER The option is mal-formated
415 @retval EFI_UNSUPPORTED Some option isn't supported
416 @retval EFI_SUCCESS The option are OK and has been parsed.
421 IN EFI_MTFTP4_OPTION
*Options
,
424 OUT MTFTP4_OPTION
*MtftpOption
430 EFI_MTFTP4_OPTION
*This
;
432 MtftpOption
->Exist
= 0;
434 for (Index
= 0; Index
< Count
; Index
++) {
435 This
= Options
+ Index
;
437 if ((This
->OptionStr
== NULL
) || (This
->ValueStr
== NULL
)) {
438 return EFI_INVALID_PARAMETER
;
441 if (NetStringEqualNoCase (This
->OptionStr
, (UINT8
*) "blksize")) {
443 // block size option, valid value is between [8, 65464]
445 Value
= NetStringToU32 (This
->ValueStr
);
447 if ((Value
< 8) || (Value
> 65464)) {
448 return EFI_INVALID_PARAMETER
;
451 MtftpOption
->BlkSize
= (UINT16
) Value
;
452 MtftpOption
->Exist
|= MTFTP4_BLKSIZE_EXIST
;
454 } else if (NetStringEqualNoCase (This
->OptionStr
, (UINT8
*) "timeout")) {
456 // timeout option, valid value is between [1, 255]
458 Value
= NetStringToU32 (This
->ValueStr
);
460 if ((Value
< 1) || (Value
> 255)) {
461 return EFI_INVALID_PARAMETER
;
464 MtftpOption
->Timeout
= (UINT8
) Value
;
466 } else if (NetStringEqualNoCase (This
->OptionStr
, (UINT8
*) "tsize")) {
468 // tsize option, the biggest transfer supported is 4GB with block size option
470 MtftpOption
->Tsize
= NetStringToU32 (This
->ValueStr
);
471 MtftpOption
->Exist
|= MTFTP4_TSIZE_EXIST
;
473 } else if (NetStringEqualNoCase (This
->OptionStr
, (UINT8
*) "multicast")) {
475 // Multicast option, if it is a request, the value must be a zero
476 // length string, otherwise, it is formated like "204.0.0.1,1857,1\0"
479 if (*(This
->ValueStr
) != '\0') {
480 return EFI_INVALID_PARAMETER
;
484 Status
= Mtftp4ExtractMcast (This
->ValueStr
, MtftpOption
);
486 if (EFI_ERROR (Status
)) {
491 MtftpOption
->Exist
|= MTFTP4_MCAST_EXIST
;
493 } else if (Request
) {
495 // Ignore the unsupported option if it is a reply, and return
496 // EFI_UNSUPPORTED if it's a request according to the UEFI spec.
498 return EFI_UNSUPPORTED
;
507 Parse the options in the OACK packet to MTFTP4_OPTION which program
510 @param Packet The OACK packet to parse
511 @param PacketLen The length of the packet
512 @param MtftpOption The MTFTP_OPTION for easy access.
514 @retval EFI_INVALID_PARAMETER The packet option is mal-formated
515 @retval EFI_UNSUPPORTED Some option isn't supported
516 @retval EFI_SUCCESS The option are OK and has been parsed.
520 Mtftp4ParseOptionOack (
521 IN EFI_MTFTP4_PACKET
*Packet
,
523 OUT MTFTP4_OPTION
*MtftpOption
526 EFI_MTFTP4_OPTION
*OptionList
;
530 MtftpOption
->Exist
= 0;
532 Status
= Mtftp4ExtractOptions (Packet
, PacketLen
, &Count
, &OptionList
);
534 if (EFI_ERROR (Status
) || (Count
== 0)) {
538 Status
= Mtftp4ParseOption (OptionList
, Count
, FALSE
, MtftpOption
);
540 NetFreePool (OptionList
);