]>
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 Check whether two ascii strings are equel, ignore the case.
28 @param Str1 The first ascii string
29 @param Str2 The second ascii string
31 @retval TRUE Two strings are equal when case is ignored.
32 @retval FALSE Two string are not equal.
36 NetStringEqualNoCase (
44 ASSERT ((Str1
!= NULL
) && (Str2
!= NULL
));
46 for (; (*Str1
!= '\0') && (*Str2
!= '\0'); Str1
++, Str2
++) {
51 // Convert them to lower case then compare two
53 if (('A' <= Ch1
) && (Ch1
<= 'Z')) {
57 if (('A' <= Ch2
) && (Ch2
<= 'Z')) {
66 return (BOOLEAN
) (*Str1
== *Str2
);
71 Convert a string to a UINT32 number.
73 @param Str The string to convert from
75 @return The number get from the string
89 for (; NET_IS_DIGIT (*Str
); Str
++) {
90 Num
= Num
* 10 + (*Str
- '0');
98 Convert a string of the format "192.168.0.1" to an IP address.
100 @param Str The string representation of IP
101 @param Ip The varible to get IP.
103 @retval EFI_INVALID_PARAMETER The IP string is invalid.
104 @retval EFI_SUCCESS The IP is parsed into the Ip
120 for (Index
= 0; Index
< 4; Index
++) {
121 if (!NET_IS_DIGIT (*Str
)) {
122 return EFI_INVALID_PARAMETER
;
125 Byte
= NetStringToU32 (Str
);
128 return EFI_INVALID_PARAMETER
;
131 Addr
= (Addr
<< 8) | Byte
;
134 // Skip all the digitals and check whether the sepeator is the dot
136 while (NET_IS_DIGIT (*Str
)) {
140 if ((Index
< 3) && (*Str
!= '.')) {
141 return EFI_INVALID_PARAMETER
;
154 Go through the packet to fill the Options array with the start
155 addresses of each MTFTP option name/value pair.
157 @param Packet The packet to check
158 @param PacketLen The packet's length
159 @param Count The size of the Options on input. The actual
161 @param Options The option array to fill in
163 @retval EFI_INVALID_PARAMETER The packet is mal-formated
164 @retval EFI_BUFFER_TOO_SMALL The Options array is too small
165 @retval EFI_SUCCESS The packet has been parsed into the Options array.
170 IN EFI_MTFTP4_PACKET
*Packet
,
172 IN OUT UINT32
*Count
,
173 OUT EFI_MTFTP4_OPTION
*Options OPTIONAL
183 Cur
= (UINT8
*) Packet
+ MTFTP4_OPCODE_LEN
;
184 Last
= (UINT8
*) Packet
+ PacketLen
- 1;
187 // process option name and value pairs. The last byte is always zero
197 return EFI_INVALID_PARAMETER
;
208 if ((Options
!= NULL
) && (Num
<= *Count
)) {
209 Options
[Num
- 1].OptionStr
= Name
;
210 Options
[Num
- 1].ValueStr
= Value
;
216 if ((*Count
< Num
) || (Options
== NULL
)) {
218 return EFI_BUFFER_TOO_SMALL
;
227 Allocate and fill in a array of Mtftp options from the Packet.
229 It first calls Mtftp4FillOption to get the option number, then allocate
230 the array, at last, call Mtftp4FillOption again to save the options.
232 @param Packet The packet to parse
233 @param PacketLen The length of the packet
234 @param OptionCount The number of options in the packet
235 @param OptionList The point to get the option array.
237 @retval EFI_INVALID_PARAMETER The parametera are invalid or packet isn't a
238 well-formated OACK packet.
239 @retval EFI_SUCCESS The option array is build
240 @retval EFI_OUT_OF_RESOURCES Failed to allocate memory for the array
244 Mtftp4ExtractOptions (
245 IN EFI_MTFTP4_PACKET
*Packet
,
247 OUT UINT32
*OptionCount
,
248 OUT EFI_MTFTP4_OPTION
**OptionList OPTIONAL
255 if (OptionList
!= NULL
) {
259 if (NTOHS (Packet
->OpCode
) != EFI_MTFTP4_OPCODE_OACK
) {
260 return EFI_INVALID_PARAMETER
;
263 if (PacketLen
== MTFTP4_OPCODE_LEN
) {
268 // The last byte must be zero to terminate the options
270 if (*((UINT8
*) Packet
+ PacketLen
- 1) != 0) {
271 return EFI_INVALID_PARAMETER
;
275 // Get the number of options
277 Status
= Mtftp4FillOptions (Packet
, PacketLen
, OptionCount
, NULL
);
279 if ((Status
== EFI_SUCCESS
) || (Status
!= EFI_BUFFER_TOO_SMALL
)) {
284 // Allocate memory for the options, then call Mtftp4FillOptions to
285 // fill it if caller want that.
287 if (OptionList
== NULL
) {
291 *OptionList
= AllocatePool (*OptionCount
* sizeof (EFI_MTFTP4_OPTION
));
293 if (*OptionList
== NULL
) {
294 return EFI_OUT_OF_RESOURCES
;
297 Mtftp4FillOptions (Packet
, PacketLen
, OptionCount
, *OptionList
);
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
);