]>
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.
49 IN EFI_MTFTP4_PACKET
*Packet
,
52 OUT EFI_MTFTP4_OPTION
*Options OPTIONAL
62 Cur
= (UINT8
*) Packet
+ MTFTP4_OPCODE_LEN
;
63 Last
= (UINT8
*) Packet
+ PacketLen
- 1;
66 // process option name and value pairs. The last byte is always zero
76 return EFI_INVALID_PARAMETER
;
87 if ((Options
!= NULL
) && (Num
<= *Count
)) {
88 Options
[Num
- 1].OptionStr
= Name
;
89 Options
[Num
- 1].ValueStr
= Value
;
95 if ((*Count
< Num
) || (Options
== NULL
)) {
97 return EFI_BUFFER_TOO_SMALL
;
106 Allocate and fill in a array of Mtftp options from the Packet. It
107 first calls Mtftp4FillOption to get the option number, then allocate
108 the array, at last, call Mtftp4FillOption again to save the options.
110 @param Packet The packet to parse
111 @param PacketLen The length of the packet
112 @param OptionCount The number of options in the packet
113 @param OptionList The point to get the option array.
115 @retval EFI_INVALID_PARAMETER The parametera are invalid or packet isn't a
116 well-formated OACK packet.
117 @retval EFI_SUCCESS The option array is build
118 @retval EFI_OUT_OF_RESOURCES Failed to allocate memory for the array
122 Mtftp4ExtractOptions (
123 IN EFI_MTFTP4_PACKET
*Packet
,
125 IN OUT UINT32
*OptionCount
,
126 OUT EFI_MTFTP4_OPTION
**OptionList OPTIONAL
133 if (OptionList
!= NULL
) {
137 if (NTOHS (Packet
->OpCode
) != EFI_MTFTP4_OPCODE_OACK
) {
138 return EFI_INVALID_PARAMETER
;
141 if (PacketLen
== MTFTP4_OPCODE_LEN
) {
146 // The last byte must be zero to terminate the options
148 if (*((UINT8
*) Packet
+ PacketLen
- 1) != 0) {
149 return EFI_INVALID_PARAMETER
;
153 // Get the number of options
155 Status
= Mtftp4FillOptions (Packet
, PacketLen
, OptionCount
, NULL
);
157 if ((Status
== EFI_SUCCESS
) || (Status
!= EFI_BUFFER_TOO_SMALL
)) {
162 // Allocate memory for the options, then call Mtftp4FillOptions to
163 // fill it if caller want that.
165 if (OptionList
== NULL
) {
169 *OptionList
= AllocatePool (*OptionCount
* sizeof (EFI_MTFTP4_OPTION
));
171 if (*OptionList
== NULL
) {
172 return EFI_OUT_OF_RESOURCES
;
175 Mtftp4FillOptions (Packet
, PacketLen
, OptionCount
, *OptionList
);
181 Check whether two ascii strings are equel, ignore the case.
183 @param Str1 The first ascii string
184 @param Str2 The second ascii string
186 @retval TRUE Two strings are equal when case is ignored.
187 @retval FALSE Two string are not equal.
191 NetStringEqualNoCase (
199 ASSERT ((Str1
!= NULL
) && (Str2
!= NULL
));
201 for (; (*Str1
!= '\0') && (*Str2
!= '\0'); Str1
++, Str2
++) {
206 // Convert them to lower case then compare two
208 if (('A' <= Ch1
) && (Ch1
<= 'Z')) {
212 if (('A' <= Ch2
) && (Ch2
<= 'Z')) {
221 return (BOOLEAN
) (*Str1
== *Str2
);
226 Convert a string to a UINT32 number.
228 @param Str The string to convert from
230 @return The number get from the string
240 ASSERT (Str
!= NULL
);
244 for (; NET_IS_DIGIT (*Str
); Str
++) {
245 Num
= Num
* 10 + (*Str
- '0');
253 Convert a string of the format "192.168.0.1" to an IP address.
255 @param Str The string representation of IP
256 @param Ip The varible to get IP.
258 @retval EFI_INVALID_PARAMETER The IP string is invalid.
259 @retval EFI_SUCCESS The IP is parsed into the Ip
275 for (Index
= 0; Index
< 4; Index
++) {
276 if (!NET_IS_DIGIT (*Str
)) {
277 return EFI_INVALID_PARAMETER
;
280 Byte
= NetStringToU32 (Str
);
283 return EFI_INVALID_PARAMETER
;
286 Addr
= (Addr
<< 8) | Byte
;
289 // Skip all the digitals and check whether the sepeator is the dot
291 while (NET_IS_DIGIT (*Str
)) {
295 if ((Index
< 3) && (*Str
!= '.')) {
296 return EFI_INVALID_PARAMETER
;
309 Parse the MTFTP multicast option.
311 @param Value The Mtftp multicast value string
312 @param Option The option to save the info into.
314 @retval EFI_INVALID_PARAMETER The multicast value string is invalid.
315 @retval EFI_SUCCESS The multicast value is parsed into the Option
321 IN MTFTP4_OPTION
*Option
328 // The multicast option is formated like "204.0.0.1,1857,1"
329 // The server can also omit the ip and port, use ",,1"
334 Status
= NetStringToIp (Value
, &Option
->McastIp
);
336 if (EFI_ERROR (Status
)) {
340 while (*Value
&& (*Value
!= ',')) {
346 return EFI_INVALID_PARAMETER
;
352 // Convert the port setting. the server can send us a port number or
353 // empty string. such as the port in ",,1"
356 Option
->McastPort
= 0;
358 Num
= NetStringToU32 (Value
);
361 return EFI_INVALID_PARAMETER
;
364 Option
->McastPort
= (UINT16
)Num
;
366 while (NET_IS_DIGIT (*Value
)) {
372 return EFI_INVALID_PARAMETER
;
378 // Check the master/slave setting, 1 for master, 0 for slave.
380 Num
= NetStringToU32 (Value
);
382 if ((Num
!= 0) && (Num
!= 1)) {
383 return EFI_INVALID_PARAMETER
;
386 Option
->Master
= (BOOLEAN
)(Num
== 1);
388 while (NET_IS_DIGIT (*Value
)) {
392 if (*Value
!= '\0') {
393 return EFI_INVALID_PARAMETER
;
401 Parse the option in Options array to MTFTP4_OPTION which program
404 @param Options The option array, which contains addresses of each
405 option's name/value string.
406 @param Count The number of options in the Options
407 @param Request Whether this is a request or OACK. The format of
408 multicast is different according to this setting.
409 @param MtftpOption The MTFTP4_OPTION for easy access.
411 @retval EFI_INVALID_PARAMETER The option is mal-formated
412 @retval EFI_UNSUPPORTED Some option isn't supported
413 @retval EFI_SUCCESS The option are OK and has been parsed.
418 IN EFI_MTFTP4_OPTION
*Options
,
421 OUT MTFTP4_OPTION
*MtftpOption
427 EFI_MTFTP4_OPTION
*This
;
429 MtftpOption
->Exist
= 0;
431 for (Index
= 0; Index
< Count
; Index
++) {
432 This
= Options
+ Index
;
434 if ((This
->OptionStr
== NULL
) || (This
->ValueStr
== NULL
)) {
435 return EFI_INVALID_PARAMETER
;
438 if (NetStringEqualNoCase (This
->OptionStr
, (UINT8
*) "blksize")) {
440 // block size option, valid value is between [8, 65464]
442 Value
= NetStringToU32 (This
->ValueStr
);
444 if ((Value
< 8) || (Value
> 65464)) {
445 return EFI_INVALID_PARAMETER
;
448 MtftpOption
->BlkSize
= (UINT16
) Value
;
449 MtftpOption
->Exist
|= MTFTP4_BLKSIZE_EXIST
;
451 } else if (NetStringEqualNoCase (This
->OptionStr
, (UINT8
*) "timeout")) {
453 // timeout option, valid value is between [1, 255]
455 Value
= NetStringToU32 (This
->ValueStr
);
457 if ((Value
< 1) || (Value
> 255)) {
458 return EFI_INVALID_PARAMETER
;
461 MtftpOption
->Timeout
= (UINT8
) Value
;
463 } else if (NetStringEqualNoCase (This
->OptionStr
, (UINT8
*) "tsize")) {
465 // tsize option, the biggest transfer supported is 4GB with block size option
467 MtftpOption
->Tsize
= NetStringToU32 (This
->ValueStr
);
468 MtftpOption
->Exist
|= MTFTP4_TSIZE_EXIST
;
470 } else if (NetStringEqualNoCase (This
->OptionStr
, (UINT8
*) "multicast")) {
472 // Multicast option, if it is a request, the value must be a zero
473 // length string, otherwise, it is formated like "204.0.0.1,1857,1\0"
476 if (*(This
->ValueStr
) != '\0') {
477 return EFI_INVALID_PARAMETER
;
481 Status
= Mtftp4ExtractMcast (This
->ValueStr
, MtftpOption
);
483 if (EFI_ERROR (Status
)) {
488 MtftpOption
->Exist
|= MTFTP4_MCAST_EXIST
;
490 } else if (Request
) {
492 // Ignore the unsupported option if it is a reply, and return
493 // EFI_UNSUPPORTED if it's a request according to the UEFI spec.
495 return EFI_UNSUPPORTED
;
504 Parse the options in the OACK packet to MTFTP4_OPTION which program
507 @param Packet The OACK packet to parse
508 @param PacketLen The length of the packet
509 @param MtftpOption The MTFTP_OPTION for easy access.
511 @retval EFI_INVALID_PARAMETER The packet option is mal-formated
512 @retval EFI_UNSUPPORTED Some option isn't supported
513 @retval EFI_SUCCESS The option are OK and has been parsed.
517 Mtftp4ParseOptionOack (
518 IN EFI_MTFTP4_PACKET
*Packet
,
520 OUT MTFTP4_OPTION
*MtftpOption
523 EFI_MTFTP4_OPTION
*OptionList
;
527 MtftpOption
->Exist
= 0;
529 Status
= Mtftp4ExtractOptions (Packet
, PacketLen
, &Count
, &OptionList
);
531 if (EFI_ERROR (Status
) || (Count
== 0)) {
535 Status
= Mtftp4ParseOption (OptionList
, Count
, FALSE
, MtftpOption
);
537 gBS
->FreePool (OptionList
);