]>
git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Universal/Network/Mtftp4Dxe/Mtftp4Option.c
2 Routines to process MTFTP4 options.
4 Copyright (c) 2006 - 2007, Intel Corporation<BR>
5 All rights reserved. This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php<BR>
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
15 #include "Mtftp4Impl.h"
17 CHAR8
*mMtftp4SupportedOptions
[MTFTP4_SUPPORTED_OPTIONS
] = {
26 Go through the packet to fill the Options array with the start
27 addresses of each MTFTP option name/value pair.
29 @param Packet The packet to check
30 @param PacketLen The packet's length
31 @param Count The size of the Options on input. The actual
33 @param Options The option array to fill in
35 @retval EFI_INVALID_PARAMETER The packet is mal-formated
36 @retval EFI_BUFFER_TOO_SMALL The Options array is too small
37 @retval EFI_SUCCESS The packet has been parsed into the Options array.
42 IN EFI_MTFTP4_PACKET
*Packet
,
45 OUT EFI_MTFTP4_OPTION
*Options OPTIONAL
55 Cur
= (UINT8
*) Packet
+ MTFTP4_OPCODE_LEN
;
56 Last
= (UINT8
*) Packet
+ PacketLen
- 1;
59 // process option name and value pairs. The last byte is always zero
69 return EFI_INVALID_PARAMETER
;
80 if ((Options
!= NULL
) && (Num
<= *Count
)) {
81 Options
[Num
- 1].OptionStr
= Name
;
82 Options
[Num
- 1].ValueStr
= Value
;
88 if ((*Count
< Num
) || (Options
== NULL
)) {
90 return EFI_BUFFER_TOO_SMALL
;
99 Allocate and fill in a array of Mtftp options from the Packet.
101 It first calls Mtftp4FillOption to get the option number, then allocate
102 the array, at last, call Mtftp4FillOption again to save the options.
104 @param Packet The packet to parse
105 @param PacketLen The length of the packet
106 @param OptionCount The number of options in the packet
107 @param OptionList The point to get the option array.
109 @retval EFI_INVALID_PARAMETER The parametera are invalid or packet isn't a
110 well-formated OACK packet.
111 @retval EFI_SUCCESS The option array is build
112 @retval EFI_OUT_OF_RESOURCES Failed to allocate memory for the array
116 Mtftp4ExtractOptions (
117 IN EFI_MTFTP4_PACKET
*Packet
,
119 OUT UINT32
*OptionCount
,
120 OUT EFI_MTFTP4_OPTION
**OptionList OPTIONAL
127 if (OptionList
!= NULL
) {
131 if (NTOHS (Packet
->OpCode
) != EFI_MTFTP4_OPCODE_OACK
) {
132 return EFI_INVALID_PARAMETER
;
135 if (PacketLen
== MTFTP4_OPCODE_LEN
) {
140 // The last byte must be zero to terminate the options
142 if (*((UINT8
*) Packet
+ PacketLen
- 1) != 0) {
143 return EFI_INVALID_PARAMETER
;
147 // Get the number of options
149 Status
= Mtftp4FillOptions (Packet
, PacketLen
, OptionCount
, NULL
);
151 if ((Status
== EFI_SUCCESS
) || (Status
!= EFI_BUFFER_TOO_SMALL
)) {
156 // Allocate memory for the options, then call Mtftp4FillOptions to
157 // fill it if caller want that.
159 if (OptionList
== NULL
) {
163 *OptionList
= AllocatePool (*OptionCount
* sizeof (EFI_MTFTP4_OPTION
));
165 if (*OptionList
== NULL
) {
166 return EFI_OUT_OF_RESOURCES
;
169 Mtftp4FillOptions (Packet
, PacketLen
, OptionCount
, *OptionList
);
175 Check whether two ascii strings are equel, ignore the case.
177 @param Str1 The first ascii string
178 @param Str2 The second ascii string
180 @retval TRUE Two strings are equal when case is ignored.
181 @retval FALSE Two string are not equal.
185 NetStringEqualNoCase (
193 ASSERT ((Str1
!= NULL
) && (Str2
!= NULL
));
195 for (; (*Str1
!= '\0') && (*Str2
!= '\0'); Str1
++, Str2
++) {
200 // Convert them to lower case then compare two
202 if (('A' <= Ch1
) && (Ch1
<= 'Z')) {
206 if (('A' <= Ch2
) && (Ch2
<= 'Z')) {
215 return (BOOLEAN
) (*Str1
== *Str2
);
220 Convert a string to a UINT32 number.
222 @param Str The string to convert from
224 @return The number get from the string
234 ASSERT (Str
!= NULL
);
238 for (; NET_IS_DIGIT (*Str
); Str
++) {
239 Num
= Num
* 10 + (*Str
- '0');
247 Convert a string of the format "192.168.0.1" to an IP address.
249 @param Str The string representation of IP
250 @param Ip The varible to get IP.
252 @retval EFI_INVALID_PARAMETER The IP string is invalid.
253 @retval EFI_SUCCESS The IP is parsed into the Ip
269 for (Index
= 0; Index
< 4; Index
++) {
270 if (!NET_IS_DIGIT (*Str
)) {
271 return EFI_INVALID_PARAMETER
;
274 Byte
= NetStringToU32 (Str
);
277 return EFI_INVALID_PARAMETER
;
280 Addr
= (Addr
<< 8) | Byte
;
283 // Skip all the digitals and check whether the sepeator is the dot
285 while (NET_IS_DIGIT (*Str
)) {
289 if ((Index
< 3) && (*Str
!= '.')) {
290 return EFI_INVALID_PARAMETER
;
303 Parse the MTFTP multicast option.
305 @param Value The Mtftp multicast value string
306 @param Option The option to save the info into.
308 @retval EFI_INVALID_PARAMETER The multicast value string is invalid.
309 @retval EFI_SUCCESS The multicast value is parsed into the Option
315 IN OUT MTFTP4_OPTION
*Option
322 // The multicast option is formated like "204.0.0.1,1857,1"
323 // The server can also omit the ip and port, use ",,1"
328 Status
= NetStringToIp (Value
, &Option
->McastIp
);
330 if (EFI_ERROR (Status
)) {
334 while ((*Value
!= 0) && (*Value
!= ',')) {
340 return EFI_INVALID_PARAMETER
;
346 // Convert the port setting. the server can send us a port number or
347 // empty string. such as the port in ",,1"
350 Option
->McastPort
= 0;
352 Num
= NetStringToU32 (Value
);
355 return EFI_INVALID_PARAMETER
;
358 Option
->McastPort
= (UINT16
) Num
;
360 while (NET_IS_DIGIT (*Value
)) {
366 return EFI_INVALID_PARAMETER
;
372 // Check the master/slave setting, 1 for master, 0 for slave.
374 Num
= NetStringToU32 (Value
);
376 if ((Num
!= 0) && (Num
!= 1)) {
377 return EFI_INVALID_PARAMETER
;
380 Option
->Master
= (BOOLEAN
) (Num
== 1);
382 while (NET_IS_DIGIT (*Value
)) {
386 if (*Value
!= '\0') {
387 return EFI_INVALID_PARAMETER
;
395 Parse the option in Options array to MTFTP4_OPTION which program
398 @param Options The option array, which contains addresses of each
399 option's name/value string.
400 @param Count The number of options in the Options
401 @param Request Whether this is a request or OACK. The format of
402 multicast is different according to this setting.
403 @param MtftpOption The MTFTP4_OPTION for easy access.
405 @retval EFI_INVALID_PARAMETER The option is mal-formated
406 @retval EFI_UNSUPPORTED Some option isn't supported
407 @retval EFI_SUCCESS The option are OK and has been parsed.
412 IN EFI_MTFTP4_OPTION
*Options
,
415 OUT MTFTP4_OPTION
*MtftpOption
421 EFI_MTFTP4_OPTION
*This
;
423 MtftpOption
->Exist
= 0;
425 for (Index
= 0; Index
< Count
; Index
++) {
426 This
= Options
+ Index
;
428 if ((This
->OptionStr
== NULL
) || (This
->ValueStr
== NULL
)) {
429 return EFI_INVALID_PARAMETER
;
432 if (NetStringEqualNoCase (This
->OptionStr
, (UINT8
*) "blksize")) {
434 // block size option, valid value is between [8, 65464]
436 Value
= NetStringToU32 (This
->ValueStr
);
438 if ((Value
< 8) || (Value
> 65464)) {
439 return EFI_INVALID_PARAMETER
;
442 MtftpOption
->BlkSize
= (UINT16
) Value
;
443 MtftpOption
->Exist
|= MTFTP4_BLKSIZE_EXIST
;
445 } else if (NetStringEqualNoCase (This
->OptionStr
, (UINT8
*) "timeout")) {
447 // timeout option, valid value is between [1, 255]
449 Value
= NetStringToU32 (This
->ValueStr
);
451 if ((Value
< 1) || (Value
> 255)) {
452 return EFI_INVALID_PARAMETER
;
455 MtftpOption
->Timeout
= (UINT8
) Value
;
457 } else if (NetStringEqualNoCase (This
->OptionStr
, (UINT8
*) "tsize")) {
459 // tsize option, the biggest transfer supported is 4GB with block size option
461 MtftpOption
->Tsize
= NetStringToU32 (This
->ValueStr
);
462 MtftpOption
->Exist
|= MTFTP4_TSIZE_EXIST
;
464 } else if (NetStringEqualNoCase (This
->OptionStr
, (UINT8
*) "multicast")) {
466 // Multicast option, if it is a request, the value must be a zero
467 // length string, otherwise, it is formated like "204.0.0.1,1857,1\0"
470 if (*(This
->ValueStr
) != '\0') {
471 return EFI_INVALID_PARAMETER
;
475 Status
= Mtftp4ExtractMcast (This
->ValueStr
, MtftpOption
);
477 if (EFI_ERROR (Status
)) {
482 MtftpOption
->Exist
|= MTFTP4_MCAST_EXIST
;
484 } else if (Request
) {
486 // Ignore the unsupported option if it is a reply, and return
487 // EFI_UNSUPPORTED if it's a request according to the UEFI spec.
489 return EFI_UNSUPPORTED
;
498 Parse the options in the OACK packet to MTFTP4_OPTION which program
501 @param Packet The OACK packet to parse
502 @param PacketLen The length of the packet
503 @param MtftpOption The MTFTP_OPTION for easy access.
505 @retval EFI_INVALID_PARAMETER The packet option is mal-formated
506 @retval EFI_UNSUPPORTED Some option isn't supported
507 @retval EFI_SUCCESS The option are OK and has been parsed.
511 Mtftp4ParseOptionOack (
512 IN EFI_MTFTP4_PACKET
*Packet
,
514 OUT MTFTP4_OPTION
*MtftpOption
517 EFI_MTFTP4_OPTION
*OptionList
;
521 MtftpOption
->Exist
= 0;
523 Status
= Mtftp4ExtractOptions (Packet
, PacketLen
, &Count
, &OptionList
);
525 if (EFI_ERROR (Status
) || (Count
== 0)) {
529 Status
= Mtftp4ParseOption (OptionList
, Count
, FALSE
, MtftpOption
);
531 gBS
->FreePool (OptionList
);