1 /** @file
2   Mtftp6 internal data structure and definition declaration.
3 
4   Copyright (c) 2009 - 2012, Intel Corporation. All rights reserved. <BR>
5 
6   This program and the accompanying materials
7   are licensed and made available under the terms and conditions of the BSD License
8   which accompanies this distribution.  The full text of the license may be found at
9   http://opensource.org/licenses/bsd-license.php.
10 
11   THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12   WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
13 
14 **/
15 
16 #ifndef __EFI_MTFTP6_IMPL_H__
17 #define __EFI_MTFTP6_IMPL_H__
18 
19 #include <Uefi.h>
20 
21 #include <Protocol/Udp6.h>
22 #include <Protocol/Mtftp6.h>
23 #include <Protocol/ServiceBinding.h>
24 #include <Protocol/DriverBinding.h>
25 
26 #include <Library/DebugLib.h>
27 #include <Library/UefiDriverEntryPoint.h>
28 #include <Library/UefiBootServicesTableLib.h>
29 #include <Library/UefiLib.h>
30 #include <Library/BaseLib.h>
31 #include <Library/NetLib.h>
32 #include <Library/PrintLib.h>
33 
34 typedef struct _MTFTP6_SERVICE  MTFTP6_SERVICE;
35 typedef struct _MTFTP6_INSTANCE MTFTP6_INSTANCE;
36 
37 #include "Mtftp6Driver.h"
38 #include "Mtftp6Option.h"
39 #include "Mtftp6Support.h"
40 
41 #define MTFTP6_SERVICE_SIGNATURE       SIGNATURE_32 ('M', 'F', '6', 'S')
42 #define MTFTP6_INSTANCE_SIGNATURE      SIGNATURE_32 ('M', 'F', '6', 'I')
43 
44 #define MTFTP6_DEFAULT_SERVER_CMD_PORT 69
45 #define MTFTP6_DEFAULT_TIMEOUT         3
46 #define MTFTP6_GET_MAPPING_TIMEOUT     3
47 #define MTFTP6_DEFAULT_MAX_RETRY       5
48 #define MTFTP6_DEFAULT_BLK_SIZE        512
49 #define MTFTP6_TICK_PER_SECOND         10000000U
50 
51 #define MTFTP6_SERVICE_FROM_THIS(a)    CR (a, MTFTP6_SERVICE, ServiceBinding, MTFTP6_SERVICE_SIGNATURE)
52 #define MTFTP6_INSTANCE_FROM_THIS(a)   CR (a, MTFTP6_INSTANCE, Mtftp6, MTFTP6_INSTANCE_SIGNATURE)
53 
54 extern EFI_MTFTP6_PROTOCOL             gMtftp6ProtocolTemplate;
55 
56 typedef struct _MTFTP6_GETINFO_CONTEXT{
57   EFI_MTFTP6_PACKET             **Packet;
58   UINT32                        *PacketLen;
59   EFI_STATUS                    Status;
60 } MTFTP6_GETINFO_CONTEXT;
61 
62 //
63 // Control block for MTFTP6 instance, it's per configuration data.
64 //
65 struct _MTFTP6_INSTANCE {
66   UINT32                        Signature;
67   EFI_HANDLE                    Handle;
68   LIST_ENTRY                    Link;
69   EFI_MTFTP6_PROTOCOL           Mtftp6;
70   MTFTP6_SERVICE                *Service;
71   EFI_MTFTP6_CONFIG_DATA        *Config;
72 
73   EFI_MTFTP6_TOKEN              *Token;
74   MTFTP6_EXT_OPTION_INFO        ExtInfo;
75 
76   UINT16                        BlkSize;
77   UINT16                        LastBlk;
78   LIST_ENTRY                    BlkList;
79 
80   EFI_IPv6_ADDRESS              ServerIp;
81   UINT16                        ServerCmdPort;
82   UINT16                        ServerDataPort;
83   UDP_IO                        *UdpIo;
84 
85   EFI_IPv6_ADDRESS              McastIp;
86   UINT16                        McastPort;
87   UDP_IO                        *McastUdpIo;
88 
89   NET_BUF                       *LastPacket;
90   UINT32                        CurRetry;
91   UINT32                        MaxRetry;
92   UINT32                        PacketToLive;
93   UINT32                        Timeout;
94 
95   EFI_TPL                       OldTpl;
96   BOOLEAN                       IsTransmitted;
97   BOOLEAN                       IsMaster;
98   BOOLEAN                       InDestroy;
99 };
100 
101 //
102 // Control block for MTFTP6 service, it's per Nic handle.
103 //
104 struct _MTFTP6_SERVICE {
105   UINT32                        Signature;
106   EFI_SERVICE_BINDING_PROTOCOL  ServiceBinding;
107   EFI_HANDLE                    Controller;
108   EFI_HANDLE                    Image;
109 
110   UINT16                        ChildrenNum;
111   LIST_ENTRY                    Children;
112   //
113   // It is used to be as internal calculagraph for all instances.
114   //
115   EFI_EVENT                     Timer;
116   //
117   // It is used to maintain the parent-child relationship between
118   // mtftp driver and udp driver.
119   //
120   UDP_IO                        *DummyUdpIo;
121 };
122 
123 typedef struct {
124   EFI_SERVICE_BINDING_PROTOCOL  *ServiceBinding;
125   UINTN                         NumberOfChildren;
126   EFI_HANDLE                    *ChildHandleBuffer;
127 } MTFTP6_DESTROY_CHILD_IN_HANDLE_BUF_CONTEXT;
128 
129 /**
130   Returns the current operating mode data for the MTFTP6 instance.
131 
132   The GetModeData() function returns the current operating mode and
133   cached data packet for the MTFTP6 instance.
134 
135   @param[in]  This               Pointer to the EFI_MTFTP6_PROTOCOL instance.
136   @param[out] ModeData           The buffer in which the EFI MTFTPv6 Protocol driver mode
137                                  data is returned.
138 
139   @retval  EFI_SUCCESS           The configuration data was returned successfully.
140   @retval  EFI_OUT_OF_RESOURCES  The required mode data could not be allocated.
141   @retval  EFI_INVALID_PARAMETER This is NULL, or ModeData is NULL.
142 
143 **/
144 EFI_STATUS
145 EFIAPI
146 EfiMtftp6GetModeData (
147   IN  EFI_MTFTP6_PROTOCOL    *This,
148   OUT EFI_MTFTP6_MODE_DATA   *ModeData
149   );
150 
151 /**
152   Initializes, changes, or resets the default operational setting for
153   this EFI MTFTPv6 Protocol driver instance.
154 
155   The Configure() function is used to set and change the configuration
156   data for this EFI MTFTPv6 Protocol driver instance. The configuration
157   data can be reset to startup defaults by calling Configure() with
158   MtftpConfigData set to NULL. Whenever the instance is reset, any
159   pending operation is aborted. By changing the EFI MTFTPv6 Protocol
160   driver instance configuration data, the client can connect to
161   different MTFTPv6 servers. The configuration parameters in
162   MtftpConfigData are used as the default parameters in later MTFTPv6
163   operations and can be overridden in later operations.
164 
165   @param[in]  This               Pointer to the EFI_MTFTP6_PROTOCOL instance.
166   @param[in]  MtftpConfigData    Pointer to the configuration data structure.
167 
168   @retval  EFI_SUCCESS           The EFI MTFTPv6 Protocol instance was configured successfully.
169   @retval  EFI_INVALID_PARAMETER One or more following conditions are TRUE:
170                                  - This is NULL.
171                                  - MtftpConfigData.StationIp is neither zero nor one
172                                    of the configured IP addresses in the underlying IPv6 driver.
173                                  - MtftpCofigData.ServerIp is not a valid IPv6 unicast address.
174                                  Note: It does not match the UEFI 2.3 Specification.
175   @retval  EFI_ACCESS_DENIED     - The configuration could not be changed at this time because there
176                                    is some MTFTP background operation in progress.
177                                  - MtftpCofigData.LocalPort is already in use.
178                                  Note: It does not match the UEFI 2.3 Specification.
179   @retval  EFI_NO_MAPPING        The underlying IPv6 driver was responsible for choosing a source
180                                  address for this instance, but no source address was available for use.
181   @retval  EFI_OUT_OF_RESOURCES  The EFI MTFTPv6 Protocol driver instance data could not be
182                                  allocated.
183                                  Note: It is not defined in the UEFI 2.3 Specification.
184   @retval  EFI_DEVICE_ERROR      An unexpected system or network error occurred. The EFI
185                                  MTFTPv6 Protocol driver instance is not configured.
186                                  Note: It is not defined in the UEFI 2.3 Specification.
187 
188 **/
189 EFI_STATUS
190 EFIAPI
191 EfiMtftp6Configure (
192   IN EFI_MTFTP6_PROTOCOL    *This,
193   IN EFI_MTFTP6_CONFIG_DATA *MtftpConfigData     OPTIONAL
194   );
195 
196 /**
197   Get the information of the download from the server.
198 
199   The GetInfo() function assembles an MTFTPv6 request packet
200   with options, sends it to the MTFTPv6 server, and may return
201   an MTFTPv6 OACK, MTFTPv6 ERROR, or ICMP ERROR packet. Retries
202   occur only if no response packets are received from the MTFTPv6
203   server before the timeout expires.
204 
205   @param[in]  This               Pointer to the EFI_MTFTP6_PROTOCOL instance.
206   @param[in]  OverrideData       Data that is used to override the existing parameters. If NULL, the
207                                  default parameters that were set in the EFI_MTFTP6_PROTOCOL.Configure()
208                                  function are used.
209   @param[in]  Filename           Pointer to null-terminated ASCII file name string.
210   @param[in]  ModeStr            Pointer to null-terminated ASCII mode string. If NULL, octet will be used
211   @param[in]  OptionCount        Number of option/value string pairs in OptionList.
212   @param[in]  OptionList         Pointer to array of option/value string pairs. Ignored if
213                                  OptionCount is zero.
214   @param[out] PacketLength       The number of bytes in the returned packet.
215   @param[out] Packet             The pointer to the received packet. This buffer must be freed by
216                                  the caller.
217 
218   @retval  EFI_SUCCESS              An MTFTPv6 OACK packet was received and is in the Packet.
219                                     Note: It does not match the UEFI 2.3 Specification.
220   @retval  EFI_INVALID_PARAMETER    One or more of the following conditions is TRUE:
221                                     - This is NULL.
222                                     - Filename is NULL.
223                                     - OptionCount is not zero and OptionList is NULL.
224                                     - One or more options in OptionList have wrong format.
225                                     - PacketLength is NULL.
226                                     - OverrideData.ServerIp is not a valid unicast IPv6 address.
227   @retval  EFI_UNSUPPORTED          One or more options in the OptionList are unsupported by
228                                     this implementation.
229   @retval  EFI_NOT_STARTED          The EFI MTFTPv6 Protocol driver has not been started.
230   @retval  EFI_NO_MAPPING           The underlying IPv6 driver was responsible for choosing a source
231                                     address for this instance, but no source address was available for use.
232   @retval  EFI_ACCESS_DENIED        The previous operation has not completed yet.
233   @retval  EFI_OUT_OF_RESOURCES     Required system resources could not be allocated.
234   @retval  EFI_TFTP_ERROR           An MTFTPv6 ERROR packet was received and is in the Packet.
235   @retval  EFI_NETWORK_UNREACHABLE  An ICMP network unreachable error packet was received, and the Packet is set to NULL.
236                                     Note: It is not defined in the UEFI 2.3 Specification.
237   @retval  EFI_HOST_UNREACHABLE     An ICMP host unreachable error packet was received, and the Packet is set to NULL.
238                                     Note: It is not defined in the UEFI 2.3 Specification.
239   @retval  EFI_PROTOCOL_UNREACHABLE An ICMP protocol unreachable error packet was received, and the Packet is set to NULL.
240                                     Note: It is not defined in the UEFI 2.3 Specification.
241   @retval  EFI_PORT_UNREACHABLE     An ICMP port unreachable error packet was received, and the Packet is set to NULL.
242   @retval  EFI_ICMP_ERROR           Some other ICMP ERROR packet was received, and the Packet is set to NULL.
243                                     Note: It does not match the UEFI 2.3 Specification.
244   @retval  EFI_PROTOCOL_ERROR       An unexpected MTFTPv6 packet was received and is in the Packet.
245   @retval  EFI_TIMEOUT              No responses were received from the MTFTPv6 server.
246   @retval  EFI_DEVICE_ERROR         An unexpected network error or system error occurred.
247 
248 **/
249 EFI_STATUS
250 EFIAPI
251 EfiMtftp6GetInfo (
252   IN  EFI_MTFTP6_PROTOCOL      *This,
253   IN  EFI_MTFTP6_OVERRIDE_DATA *OverrideData         OPTIONAL,
254   IN  UINT8                    *Filename,
255   IN  UINT8                    *ModeStr              OPTIONAL,
256   IN  UINT8                    OptionCount,
257   IN  EFI_MTFTP6_OPTION        *OptionList           OPTIONAL,
258   OUT UINT32                   *PacketLength,
259   OUT EFI_MTFTP6_PACKET        **Packet              OPTIONAL
260   );
261 
262 /**
263   Parse the options in an MTFTPv6 OACK packet.
264 
265   The ParseOptions() function parses the option fields in an MTFTPv6 OACK
266   packet and returns the number of options that were found, and optionally,
267   a list of pointers to the options in the packet. If one or more of the
268   option fields are not valid, then EFI_PROTOCOL_ERROR is returned and
269   *OptionCount and *OptionList stop at the last valid option.
270 
271   @param[in]  This               Pointer to the EFI_MTFTP6_PROTOCOL instance.
272   @param[in]  PacketLen          Length of the OACK packet to be parsed.
273   @param[in]  Packet             Pointer to the OACK packet to be parsed.
274   @param[out] OptionCount        Pointer to the number of options in the following OptionList.
275   @param[out] OptionList         Pointer to EFI_MTFTP6_OPTION storage. Each pointer in the
276                                  OptionList points to the corresponding MTFTP option buffer
277                                  in the Packet. Call the EFI Boot Service FreePool() to
278                                  release the OptionList if the options in this OptionList
279                                  are not needed any more.
280 
281   @retval  EFI_SUCCESS           The OACK packet was valid and the OptionCount, and
282                                  OptionList parameters have been updated.
283   @retval  EFI_INVALID_PARAMETER One or more of the following conditions is TRUE:
284                                  - PacketLen is 0.
285                                  - Packet is NULL or Packet is not a valid MTFTPv6 packet.
286                                  - OptionCount is NULL.
287   @retval  EFI_NOT_FOUND         No options were found in the OACK packet.
288   @retval  EFI_OUT_OF_RESOURCES  Storage for the OptionList array can not be allocated.
289   @retval  EFI_PROTOCOL_ERROR    One or more of the option fields is invalid.
290 
291 **/
292 EFI_STATUS
293 EFIAPI
294 EfiMtftp6ParseOptions (
295   IN     EFI_MTFTP6_PROTOCOL    *This,
296   IN     UINT32                 PacketLen,
297   IN     EFI_MTFTP6_PACKET      *Packet,
298   OUT    UINT32                 *OptionCount,
299   OUT    EFI_MTFTP6_OPTION      **OptionList          OPTIONAL
300   );
301 
302 /**
303   Download a file from an MTFTPv6 server.
304 
305   The ReadFile() function is used to initialize and start an MTFTPv6 download
306   process and optionally wait for completion. When the download operation
307   completes, whether successfully or not, the Token.Status field is updated
308   by the EFI MTFTPv6 Protocol driver, and then Token.Event is signaled if it
309   is not NULL.
310   Data can be downloaded from the MTFTPv6 server into either of the following
311   locations:
312   - A fixed buffer that is pointed to by Token.Buffer.
313   - A download service function that is pointed to by Token.CheckPacket.
314   If both Token.Buffer and Token.CheckPacket are used, then Token.CheckPacket
315   will be called first. If the call is successful, the packet will be stored
316   in Token.Buffer.
317 
318   @param[in]  This               Pointer to the EFI_MTFTP6_PROTOCOL instance.
319   @param[in]  Token              Pointer to the token structure to provide the parameters that are
320                                  used in this operation.
321 
322   @retval  EFI_SUCCESS              The data file has been transferred successfully.
323   @retval  EFI_OUT_OF_RESOURCES     Required system resources could not be allocated.
324   @retval  EFI_BUFFER_TOO_SMALL     BufferSize is not zero but not large enough to hold the
325                                     downloaded data in downloading process.
326                                     Note: It does not match the UEFI 2.3 Specification.
327   @retval  EFI_ABORTED              Current operation is aborted by user.
328   @retval  EFI_NETWORK_UNREACHABLE  An ICMP network unreachable error packet was received.
329                                     Note: It is not defined in the UEFI 2.3 Specification.
330   @retval  EFI_HOST_UNREACHABLE     An ICMP host unreachable error packet was received.
331                                     Note: It is not defined in the UEFI 2.3 Specification.
332   @retval  EFI_PROTOCOL_UNREACHABLE An ICMP protocol unreachable error packet was received.
333                                     Note: It is not defined in the UEFI 2.3 Specification.
334   @retval  EFI_PORT_UNREACHABLE     An ICMP port unreachable error packet was received.
335                                     Note: It is not defined in the UEFI 2.3 Specification.
336   @retval  EFI_ICMP_ERROR           An ICMP ERROR packet was received.
337   @retval  EFI_TIMEOUT              No responses were received from the MTFTPv6 server.
338   @retval  EFI_TFTP_ERROR           An MTFTPv6 ERROR packet was received.
339   @retval  EFI_DEVICE_ERROR         An unexpected network error or system error occurred.
340 
341 **/
342 EFI_STATUS
343 EFIAPI
344 EfiMtftp6ReadFile (
345   IN EFI_MTFTP6_PROTOCOL    *This,
346   IN EFI_MTFTP6_TOKEN       *Token
347   );
348 
349 /**
350   Send a file to an MTFTPv6 server.
351 
352   The WriteFile() function is used to initialize an uploading operation
353   with the given option list, and optionally, wait for completion. If one
354   or more of the options is not supported by the server, the unsupported
355   options are ignored and a standard TFTP process starts instead. When
356   the upload process completes, whether successfully or not, Token.Event
357   is signaled, and the EFI MTFTPv6 Protocol driver updates Token.Status.
358   The caller can supply the data to be uploaded in the following two modes:
359   - Through the user-provided buffer.
360   - Through a callback function.
361   With the user-provided buffer, the Token.BufferSize field indicates
362   the length of the buffer, and the driver will upload the data in the
363   buffer. With an EFI_MTFTP6_PACKET_NEEDED callback function, the driver
364   will call this callback function to get more data from the user to upload.
365 
366   @param[in]  This               Pointer to the EFI_MTFTP6_PROTOCOL instance.
367   @param[in]  Token              Pointer to the token structure to provide the parameters that are
368                                  used in this operation.
369 
370   @retval  EFI_SUCCESS           The upload session has started.
371   @retval  EFI_UNSUPPORTED       The operation is not supported by this implementation.
372   @retval  EFI_INVALID_PARAMETER One or more of the following conditions is TRUE:
373                                  - This is NULL.
374                                  - Token is NULL.
375                                  - Token.Filename is NULL.
376                                  - Token.OptionCount is not zero and Token.OptionList is NULL.
377                                  - One or more options in Token.OptionList have wrong format.
378                                  - Token.Buffer and Token.PacketNeeded are both NULL.
379                                  - Token.OverrideData.ServerIp is not valid unicast IPv6 addresses.
380   @retval  EFI_UNSUPPORTED       One or more options in the Token.OptionList are not
381                                  supported by this implementation.
382   @retval  EFI_NOT_STARTED       The EFI MTFTPv6 Protocol driver has not been started.
383   @retval  EFI_NO_MAPPING        The underlying IPv6 driver was responsible for choosing a source
384                                  address for this instance, but no source address was available for use.
385   @retval  EFI_ALREADY_STARTED   This Token is already being used in another MTFTPv6 session.
386   @retval  EFI_OUT_OF_RESOURCES  Required system resources could not be allocated.
387   @retval  EFI_ACCESS_DENIED     The previous operation has not completed yet.
388   @retval  EFI_DEVICE_ERROR      An unexpected network error or system error occurred.
389 
390 **/
391 EFI_STATUS
392 EFIAPI
393 EfiMtftp6WriteFile (
394   IN EFI_MTFTP6_PROTOCOL    *This,
395   IN EFI_MTFTP6_TOKEN       *Token
396   );
397 
398 /**
399   Download a data file directory from an MTFTPv6 server.
400 
401   The ReadDirectory() function is used to return a list of files on the
402   MTFTPv6 server that are logically (or operationally) related to
403   Token.Filename. The directory request packet that is sent to the server
404   is built with the option list that was provided by caller, if present.
405   The file information that the server returns is put into either of
406   the following locations:
407   - A fixed buffer that is pointed to by Token.Buffer.
408   - A download service function that is pointed to by Token.CheckPacket.
409   If both Token.Buffer and Token.CheckPacket are used, then Token.CheckPacket
410   will be called first. If the call is successful, the packet will be stored
411   in Token.Buffer.
412 
413   @param[in]  This               Pointer to the EFI_MTFTP6_PROTOCOL instance.
414   @param[in]  Token              Pointer to the token structure to provide the parameters that are
415                                  used in this operation.
416 
417   @retval  EFI_SUCCESS           The MTFTPv6 related file "directory" has been downloaded.
418   @retval  EFI_UNSUPPORTED       The EFI MTFTPv6 Protocol driver does not support this function.
419   @retval  EFI_INVALID_PARAMETER One or more of the following conditions is TRUE:
420                                  - This is NULL.
421                                  - Token is NULL.
422                                  - Token.Filename is NULL.
423                                  - Token.OptionCount is not zero and Token.OptionList is NULL.
424                                  - One or more options in Token.OptionList have wrong format.
425                                  - Token.Buffer and Token.CheckPacket are both NULL.
426                                  - Token.OverrideData.ServerIp is not valid unicast IPv6 addresses.
427   @retval  EFI_UNSUPPORTED       One or more options in the Token.OptionList are not
428                                  supported by this implementation.
429   @retval  EFI_NOT_STARTED       The EFI MTFTPv6 Protocol driver has not been started.
430   @retval  EFI_NO_MAPPING        The underlying IPv6 driver was responsible for choosing a source
431                                  address for this instance, but no source address was available for use.
432   @retval  EFI_ALREADY_STARTED   This Token is already being used in another MTFTPv6 session.
433   @retval  EFI_OUT_OF_RESOURCES  Required system resources could not be allocated.
434   @retval  EFI_ACCESS_DENIED     The previous operation has not completed yet.
435   @retval  EFI_DEVICE_ERROR      An unexpected network error or system error occurred.
436 
437 **/
438 EFI_STATUS
439 EFIAPI
440 EfiMtftp6ReadDirectory (
441   IN EFI_MTFTP6_PROTOCOL        *This,
442   IN EFI_MTFTP6_TOKEN           *Token
443   );
444 
445 /**
446   Polls for incoming data packets and processes outgoing data packets.
447 
448   The Poll() function can be used by network drivers and applications
449   to increase the rate that data packets are moved between the
450   communications device and the transmit and receive queues.In some
451   systems, the periodic timer event in the managed network driver may
452   not poll the underlying communications device fast enough to transmit
453   and/or receive all data packets without missing incoming packets or
454   dropping outgoing packets. Drivers and applications that are
455   experiencing packet loss should try calling the Poll() function
456   more often.
457 
458   @param[in]  This                   The MTFTP6 protocol instance.
459 
460 
461   @retval  EFI_SUCCESS           Incoming or outgoing data was processed.
462   @retval  EFI_NOT_STARTED       This EFI MTFTPv6 Protocol instance has not been started.
463   @retval  EFI_INVALID_PARAMETER This is NULL.
464   @retval  EFI_DEVICE_ERROR      An unexpected system or network error occurred.
465   @retval  EFI_TIMEOUT           Data was dropped out of the transmit and/or receive queue.
466                                  Consider increasing the polling rate.
467 
468 **/
469 EFI_STATUS
470 EFIAPI
471 EfiMtftp6Poll (
472   IN EFI_MTFTP6_PROTOCOL    *This
473   );
474 
475 #endif
476