1 /** @file
2 
3   Copyright (c) 2008 - 2009, Apple Inc. All rights reserved.<BR>
4 
5   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
9 
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.
12 
13 **/
14 
15 #include <Uefi.h>
16 #include <Library/BaseMemoryLib.h>
17 #include <Library/DebugLib.h>
18 #include <Library/UefiRuntimeServicesTableLib.h>
19 #include <Library/UefiBootServicesTableLib.h>
20 
21 #include <Protocol/SimpleNetwork.h>
22 #include <Protocol/PxeBaseCode.h>
23 
24 
25 BOOLEAN   gUseIpv6 = FALSE;
26 
27 EFI_STATUS
28 EFIAPI
EblGetCurrentIpAddress(IN OUT EFI_IP_ADDRESS * Ip)29 EblGetCurrentIpAddress (
30   IN OUT   EFI_IP_ADDRESS *Ip
31   )
32 {
33   EFI_STATUS                  Status;
34   EFI_PXE_BASE_CODE_PROTOCOL  *Pxe;
35 
36   Status = gBS->LocateProtocol (&gEfiPxeBaseCodeProtocolGuid, NULL, (VOID **)&Pxe);
37   if (EFI_ERROR(Status)) {
38     return Status;
39   }
40 
41   Status = Pxe->Start (Pxe, gUseIpv6);
42   if (EFI_ERROR(Status) && (Status != EFI_ALREADY_STARTED)) {
43     return Status;
44   }
45 
46   CopyMem (Ip, &Pxe->Mode->StationIp, sizeof (EFI_IP_ADDRESS));
47 
48   return EFI_SUCCESS;
49 }
50 
51 
52 EFI_STATUS
53 EFIAPI
EblGetCurrentMacAddress(IN OUT EFI_MAC_ADDRESS * Mac)54 EblGetCurrentMacAddress (
55   IN OUT  EFI_MAC_ADDRESS *Mac
56   )
57 {
58   EFI_STATUS                    Status;
59   EFI_SIMPLE_NETWORK_PROTOCOL   *SimpleNet;
60 
61   Status = gBS->LocateProtocol (&gEfiSimpleNetworkProtocolGuid, NULL, (VOID **)&SimpleNet);
62   if (EFI_ERROR(Status)) {
63     return Status;
64   }
65 
66   CopyMem (Mac, SimpleNet->Mode->CurrentAddress.Addr, sizeof (EFI_MAC_ADDRESS));
67   return Status;
68 }
69 
70 
71 CHAR8 *
72 EFIAPI
EblLoadFileBootTypeString(IN EFI_HANDLE Handle)73 EblLoadFileBootTypeString (
74   IN  EFI_HANDLE Handle
75   )
76 {
77   EFI_STATUS    Status;
78   VOID          *NullPtr;
79 
80   Status = gBS->HandleProtocol (Handle, &gEfiPxeBaseCodeProtocolGuid, &NullPtr);
81   if (!EFI_ERROR (Status)) {
82     return "EFI PXE Network Boot";
83   }
84 
85   return "";
86 }
87 
88 EFI_STATUS
89 EFIAPI
EblPerformDHCP(IN BOOLEAN SortOffers)90 EblPerformDHCP (
91   IN  BOOLEAN  SortOffers
92   )
93 {
94   EFI_STATUS                  Status;
95   EFI_PXE_BASE_CODE_PROTOCOL  *Pxe;
96 
97   Status = gBS->LocateProtocol (&gEfiPxeBaseCodeProtocolGuid, NULL, (VOID **)&Pxe);
98   if (EFI_ERROR(Status)) {
99     return Status;
100   }
101 
102   Status = Pxe->Start (Pxe, gUseIpv6);
103   if (EFI_ERROR(Status) && (Status != EFI_ALREADY_STARTED)) {
104     return Status;
105   }
106 
107   Status = Pxe->Dhcp(Pxe, TRUE);
108   return Status;
109 }
110 
111 
112 EFI_STATUS
113 EFIAPI
EblSetStationIp(IN EFI_IP_ADDRESS * NewStationIp,OPTIONAL IN EFI_IP_ADDRESS * NewSubnetMask OPTIONAL)114 EblSetStationIp (
115   IN EFI_IP_ADDRESS *NewStationIp,  OPTIONAL
116   IN EFI_IP_ADDRESS *NewSubnetMask  OPTIONAL
117   )
118 {
119   EFI_STATUS                  Status;
120   EFI_PXE_BASE_CODE_PROTOCOL  *Pxe;
121 
122   Status = gBS->LocateProtocol (&gEfiPxeBaseCodeProtocolGuid, NULL, (VOID **)&Pxe);
123   if (EFI_ERROR(Status)) {
124     return Status;
125   }
126 
127   Status = Pxe->Start (Pxe, gUseIpv6);
128   if (EFI_ERROR(Status) && (Status != EFI_ALREADY_STARTED)) {
129     return Status;
130   }
131 
132   Status = Pxe->SetStationIp (Pxe, NewStationIp, NewSubnetMask);
133   return Status;
134 }
135 
136 
137 EFI_STATUS
138 EFIAPI
EblMtftp(IN EFI_PXE_BASE_CODE_TFTP_OPCODE Operation,IN OUT VOID * BufferPtr OPTIONAL,IN BOOLEAN Overwrite,IN OUT UINT64 * BufferSize,IN UINTN * BlockSize OPTIONAL,IN EFI_IP_ADDRESS * ServerIp,IN UINT8 * Filename OPTIONAL,IN EFI_PXE_BASE_CODE_MTFTP_INFO * Info OPTIONAL,IN BOOLEAN DontUseBuffer)139 EblMtftp (
140   IN EFI_PXE_BASE_CODE_TFTP_OPCODE             Operation,
141   IN OUT VOID                                  *BufferPtr OPTIONAL,
142   IN BOOLEAN                                   Overwrite,
143   IN OUT UINT64                                *BufferSize,
144   IN UINTN                                     *BlockSize OPTIONAL,
145   IN EFI_IP_ADDRESS                            *ServerIp,
146   IN UINT8                                     *Filename  OPTIONAL,
147   IN EFI_PXE_BASE_CODE_MTFTP_INFO              *Info      OPTIONAL,
148   IN BOOLEAN                                   DontUseBuffer
149   )
150 {
151   EFI_STATUS                  Status;
152   EFI_PXE_BASE_CODE_PROTOCOL  *Pxe;
153 
154   Status = gBS->LocateProtocol (&gEfiPxeBaseCodeProtocolGuid, NULL, (VOID **)&Pxe);
155   if (EFI_ERROR(Status)) {
156     return Status;
157   }
158 
159   Status = Pxe->Mtftp (
160                   Pxe,
161                   Operation,
162                   BufferPtr,
163                   Overwrite,
164                   BufferSize,
165                   BlockSize,
166                   ServerIp,
167                   Filename,
168                   Info,
169                   DontUseBuffer
170                   );
171   return Status;
172 }
173 
174