1 /** @file
2 
3   Mtftp4 Implementation.
4 
5   Mtftp4 Implementation, it supports the following RFCs:
6   RFC1350 - THE TFTP PROTOCOL (REVISION 2)
7   RFC2090 - TFTP Multicast Option
8   RFC2347 - TFTP Option Extension
9   RFC2348 - TFTP Blocksize Option
10   RFC2349 - TFTP Timeout Interval and Transfer Size Options
11 
12 Copyright (c) 2006 - 2012, Intel Corporation. All rights reserved.<BR>
13 This program and the accompanying materials
14 are licensed and made available under the terms and conditions of the BSD License
15 which accompanies this distribution.  The full text of the license may be found at
16 http://opensource.org/licenses/bsd-license.php<BR>
17 
18 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
19 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
20 
21 **/
22 
23 
24 #ifndef __EFI_MTFTP4_IMPL_H__
25 #define __EFI_MTFTP4_IMPL_H__
26 
27 #include <Uefi.h>
28 
29 #include <Protocol/Udp4.h>
30 #include <Protocol/Mtftp4.h>
31 
32 #include <Library/DebugLib.h>
33 #include <Library/BaseMemoryLib.h>
34 #include <Library/MemoryAllocationLib.h>
35 #include <Library/UefiBootServicesTableLib.h>
36 #include <Library/UdpIoLib.h>
37 #include <Library/PrintLib.h>
38 
39 extern EFI_MTFTP4_PROTOCOL  gMtftp4ProtocolTemplate;
40 
41 typedef struct _MTFTP4_SERVICE  MTFTP4_SERVICE;
42 typedef struct _MTFTP4_PROTOCOL MTFTP4_PROTOCOL;
43 
44 #include "Mtftp4Driver.h"
45 #include "Mtftp4Option.h"
46 #include "Mtftp4Support.h"
47 
48 
49 ///
50 /// Some constant value of Mtftp service.
51 ///
52 #define MTFTP4_SERVICE_SIGNATURE    SIGNATURE_32 ('T', 'F', 'T', 'P')
53 #define MTFTP4_PROTOCOL_SIGNATURE   SIGNATURE_32 ('t', 'f', 't', 'p')
54 
55 #define MTFTP4_DEFAULT_SERVER_PORT  69
56 #define MTFTP4_DEFAULT_TIMEOUT      3
57 #define MTFTP4_DEFAULT_RETRY        5
58 #define MTFTP4_DEFAULT_BLKSIZE      512
59 #define MTFTP4_TIME_TO_GETMAP       5
60 
61 #define MTFTP4_STATE_UNCONFIGED     0
62 #define MTFTP4_STATE_CONFIGED       1
63 #define MTFTP4_STATE_DESTROY        2
64 
65 ///
66 /// Mtftp service block
67 ///
68 struct _MTFTP4_SERVICE {
69   UINT32                        Signature;
70   EFI_SERVICE_BINDING_PROTOCOL  ServiceBinding;
71 
72   UINT16                        ChildrenNum;
73   LIST_ENTRY                    Children;
74 
75   EFI_EVENT                     Timer;  ///< Ticking timer for all the MTFTP clients
76   EFI_EVENT                     TimerToGetMap;
77 
78   EFI_HANDLE                    Controller;
79   EFI_HANDLE                    Image;
80 
81   //
82   // This UDP child is used to keep the connection between the UDP
83   // and MTFTP, so MTFTP will be notified when UDP is uninstalled.
84   //
85   UDP_IO                        *ConnectUdp;
86 };
87 
88 
89 typedef struct {
90   EFI_MTFTP4_PACKET             **Packet;
91   UINT32                        *PacketLen;
92   EFI_STATUS                    Status;
93 } MTFTP4_GETINFO_STATE;
94 
95 struct _MTFTP4_PROTOCOL {
96   UINT32                        Signature;
97   LIST_ENTRY                    Link;
98   EFI_MTFTP4_PROTOCOL           Mtftp4;
99 
100   INTN                          State;
101   BOOLEAN                       InDestroy;
102 
103   MTFTP4_SERVICE                *Service;
104   EFI_HANDLE                    Handle;
105 
106   EFI_MTFTP4_CONFIG_DATA        Config;
107 
108   //
109   // Operation parameters: token and requested options.
110   //
111   EFI_MTFTP4_TOKEN              *Token;
112   MTFTP4_OPTION                 RequestOption;
113   UINT16                        Operation;
114 
115   //
116   // Blocks is a list of MTFTP4_BLOCK_RANGE which contains
117   // holes in the file
118   //
119   UINT16                        BlkSize;
120   UINT16                        LastBlock;
121   LIST_ENTRY                    Blocks;
122 
123   //
124   // The server's communication end point: IP and two ports. one for
125   // initial request, one for its selected port.
126   //
127   IP4_ADDR                      ServerIp;
128   UINT16                        ListeningPort;
129   UINT16                        ConnectedPort;
130   IP4_ADDR                      Gateway;
131   UDP_IO                        *UnicastPort;
132 
133   //
134   // Timeout and retransmit status
135   //
136   NET_BUF                       *LastPacket;
137   UINT32                        PacketToLive;
138   UINT32                        CurRetry;
139   UINT32                        MaxRetry;
140   UINT32                        Timeout;
141 
142   //
143   // Parameter used by RRQ's multicast download.
144   //
145   IP4_ADDR                      McastIp;
146   UINT16                        McastPort;
147   BOOLEAN                       Master;
148   UDP_IO                        *McastUdpPort;
149 };
150 
151 typedef struct {
152   EFI_SERVICE_BINDING_PROTOCOL  *ServiceBinding;
153   UINTN                         NumberOfChildren;
154   EFI_HANDLE                    *ChildHandleBuffer;
155 } MTFTP4_DESTROY_CHILD_IN_HANDLE_BUF_CONTEXT;
156 
157 /**
158   Clean up the MTFTP session to get ready for new operation.
159 
160   @param  Instance               The MTFTP session to clean up
161   @param  Result                 The result to return to the caller who initiated
162                                  the operation.
163 **/
164 VOID
165 Mtftp4CleanOperation (
166   IN OUT MTFTP4_PROTOCOL        *Instance,
167   IN     EFI_STATUS             Result
168   );
169 
170 /**
171   Start the MTFTP session for upload.
172 
173   It will first init some states, then send the WRQ request packet,
174   and start receiving the packet.
175 
176   @param  Instance              The MTFTP session
177   @param  Operation             Redundant parameter, which is always
178                                 EFI_MTFTP4_OPCODE_WRQ here.
179 
180   @retval EFI_SUCCESS           The upload process has been started.
181   @retval Others                Failed to start the upload.
182 
183 **/
184 EFI_STATUS
185 Mtftp4WrqStart (
186   IN MTFTP4_PROTOCOL        *Instance,
187   IN UINT16                 Operation
188   );
189 
190 /**
191   Start the MTFTP session to download.
192 
193   It will first initialize some of the internal states then build and send a RRQ
194   reqeuest packet, at last, it will start receive for the downloading.
195 
196   @param  Instance              The Mtftp session
197   @param  Operation             The MTFTP opcode, it may be a EFI_MTFTP4_OPCODE_RRQ
198                                 or EFI_MTFTP4_OPCODE_DIR.
199 
200   @retval EFI_SUCCESS           The mtftp download session is started.
201   @retval Others                Failed to start downloading.
202 
203 **/
204 EFI_STATUS
205 Mtftp4RrqStart (
206   IN MTFTP4_PROTOCOL        *Instance,
207   IN UINT16                 Operation
208   );
209 
210 #define MTFTP4_SERVICE_FROM_THIS(a)   \
211   CR (a, MTFTP4_SERVICE, ServiceBinding, MTFTP4_SERVICE_SIGNATURE)
212 
213 #define MTFTP4_PROTOCOL_FROM_THIS(a)  \
214   CR (a, MTFTP4_PROTOCOL, Mtftp4, MTFTP4_PROTOCOL_SIGNATURE)
215 
216 #endif
217