iPXE
efiprefix.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2009 Michael Brown <mbrown@fensystems.co.uk>.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as
6  * published by the Free Software Foundation; either version 2 of the
7  * License, or any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17  * 02110-1301, USA.
18  */
19 
20 FILE_LICENCE ( GPL2_OR_LATER );
21 
22 #include <stdlib.h>
23 #include <errno.h>
24 #include <ipxe/device.h>
25 #include <ipxe/uri.h>
26 #include <ipxe/init.h>
27 #include <ipxe/efi/efi.h>
28 #include <ipxe/efi/efi_driver.h>
29 #include <ipxe/efi/efi_snp.h>
30 #include <ipxe/efi/efi_autoboot.h>
31 #include <ipxe/efi/efi_autoexec.h>
32 #include <ipxe/efi/efi_cachedhcp.h>
33 #include <ipxe/efi/efi_watchdog.h>
34 #include <ipxe/efi/efi_path.h>
35 #include <ipxe/efi/efi_veto.h>
36 
37 /**
38  * EFI entry point
39  *
40  * @v image_handle Image handle
41  * @v systab System table
42  * @ret efirc EFI return status code
43  */
45  EFI_SYSTEM_TABLE *systab ) {
46  EFI_STATUS efirc;
47  int rc;
48 
49  /* Initialise stack cookie */
50  efi_init_stack_guard ( image_handle );
51 
52  /* Initialise EFI environment */
53  if ( ( efirc = efi_init ( image_handle, systab ) ) != 0 )
54  goto err_init;
55 
56  /* Claim SNP devices for use by iPXE */
57  efi_snp_claim();
58 
59  /* Start watchdog holdoff timer */
61 
62  /* Call to main() */
63  if ( ( rc = main() ) != 0 ) {
64  efirc = EFIRC ( rc );
65  goto err_main;
66  }
67 
68  err_main:
71  efi_loaded_image->Unload ( image_handle );
73  err_init:
74  return efirc;
75 }
76 
77 /**
78  * Initialise EFI application
79  *
80  */
81 static void efi_init_application ( void ) {
84  EFI_DEVICE_PATH_PROTOCOL *bootpath;
85  struct uri *uri;
86 
87  /* Set current working URI from device path, if present */
88  bootpath = efi_current_boot_path();
89  DBGC ( device, "EFI has loaded image device path %s\n",
90  efi_devpath_text ( devpath ) );
91  DBGC ( device, "EFI has boot option device path %s\n",
92  efi_devpath_text ( bootpath ) );
93  uri = efi_path_uri ( devpath );
94  if ( bootpath && ( ! uri ) )
95  uri = efi_path_uri ( bootpath );
96  if ( uri )
97  churi ( uri );
98  uri_put ( uri );
99  free ( bootpath );
100 
101  /* Identify autoboot device, if any */
102  efi_set_autoboot_ll_addr ( device, devpath );
103 
104  /* Store cached DHCP packet, if any */
105  efi_cachedhcp_record ( device, devpath );
106 }
107 
108 /** EFI application initialisation function */
109 struct init_fn efi_init_application_fn __init_fn ( INIT_NORMAL ) = {
110  .name = "efi",
111  .initialise = efi_init_application,
112 };
113 
114 /**
115  * Probe EFI root bus
116  *
117  * @v rootdev EFI root device
118  */
119 static int efi_probe ( struct root_device *rootdev __unused ) {
120 
121  /* Try loading autoexec script */
123 
124  /* Remove any vetoed drivers */
125  efi_veto();
126 
127  /* Connect our drivers */
128  return efi_driver_connect_all();
129 }
130 
131 /**
132  * Remove EFI root bus
133  *
134  * @v rootdev EFI root device
135  */
136 static void efi_remove ( struct root_device *rootdev __unused ) {
137 
138  /* Disconnect our drivers */
140 }
141 
142 /** EFI root device driver */
143 static struct root_driver efi_root_driver = {
144  .probe = efi_probe,
145  .remove = efi_remove,
146 };
147 
148 /** EFI root device */
149 struct root_device efi_root_device __root_device = {
150  .dev = { .name = "EFI" },
151  .driver = &efi_root_driver,
152 };
int efi_driver_connect_all(void)
Connect EFI driver to all possible devices.
Definition: efi_driver.c:636
EFI_LOADED_IMAGE_PROTOCOL * efi_loaded_image
Loaded image protocol for this image.
Definition: efi_init.c:38
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
static void efi_init_stack_guard(EFI_HANDLE handle)
Initialise stack cookie.
Definition: efi.h:386
static void uri_put(struct uri *uri)
Decrement URI reference count.
Definition: uri.h:205
EFI driver interface.
void efi_veto(void)
Remove any vetoed drivers.
Definition: efi_veto.c:614
Error codes.
static void efi_remove(struct root_device *rootdev __unused)
Remove EFI root bus.
Definition: efiprefix.c:136
struct root_device efi_root_device __root_device
EFI root device.
Definition: efiprefix.c:149
#define DBGC(...)
Definition: compiler.h:505
char name[40]
Name.
Definition: device.h:78
FILE_LICENCE(GPL2_OR_LATER)
A root device.
Definition: device.h:97
EFI driver vetoes.
This protocol can be used on any device handle to obtain generic path/location information concerning...
Definition: DevicePath.h:45
static void efi_snp_claim(void)
Claim network devices for use by iPXE.
Definition: efi_snp.h:91
Uniform Resource Identifiers.
struct device dev
Device chain.
Definition: device.h:102
int efi_autoexec_load(void)
Load autoexec script.
Definition: efi_autoexec.c:168
A hardware device.
Definition: device.h:76
#define INIT_NORMAL
Normal initialisation.
Definition: init.h:31
EFI_DEVICE_PATH_PROTOCOL * efi_loaded_image_path
Device path for the loaded image's device handle.
Definition: efi_init.c:41
An initialisation function.
Definition: init.h:14
void churi(struct uri *uri)
Change working URI.
Definition: cwuri.c:45
void efi_driver_reconnect_all(void)
Reconnect original EFI drivers to all possible devices.
Definition: efi_driver.c:658
EFI_DEVICE_PATH_PROTOCOL * efi_current_boot_path(void)
Get EFI device path for current boot option.
Definition: efi_path.c:886
static void efi_watchdog_start(void)
Start EFI watchdog holdoff timer.
Definition: efi_watchdog.h:17
#define __unused
Declare a variable or data structure as unused.
Definition: compiler.h:573
static struct root_driver efi_root_driver
EFI root device driver.
Definition: efiprefix.c:143
const char * name
Definition: init.h:15
const char * efi_devpath_text(EFI_DEVICE_PATH_PROTOCOL *path)
Get textual representation of device path.
Definition: efi_debug.c:247
static void efi_watchdog_stop(void)
Stop EFI watchdog holdoff timer.
Definition: efi_watchdog.h:26
struct init_fn efi_init_application_fn __init_fn(INIT_NORMAL)
EFI application initialisation function.
static void(* free)(struct refcnt *refcnt))
Definition: refcnt.h:54
#define EFIAPI
EFI cached DHCP packet.
static void efi_init_application(void)
Initialise EFI application.
Definition: efiprefix.c:81
EFI autoboot device.
EFI_IMAGE_UNLOAD Unload
Definition: LoadedImage.h:73
EFI watchdog holdoff timer.
void efi_driver_disconnect_all(void)
Disconnect EFI driver from all possible devices.
Definition: efi_driver.c:647
A root device driver.
Definition: device.h:110
static void efi_snp_release(void)
Release network devices for use via SNP.
Definition: efi_snp.h:99
EFI device paths.
EFI System Table.
Definition: UefiSpec.h:2043
int(* probe)(struct root_device *rootdev)
Add root device.
Definition: device.h:119
EFI API.
int efi_set_autoboot_ll_addr(EFI_HANDLE device, EFI_DEVICE_PATH_PROTOCOL *path)
Identify autoboot device.
Definition: efi_autoboot.c:49
int efi_cachedhcp_record(EFI_HANDLE device, EFI_DEVICE_PATH_PROTOCOL *path)
Record cached DHCP packet.
Definition: efi_cachedhcp.c:47
struct uri * efi_path_uri(EFI_DEVICE_PATH_PROTOCOL *path)
Parse URI from device path.
Definition: efi_path.c:300
EFI_STATUS EFIAPI _efi_start(EFI_HANDLE image_handle, EFI_SYSTEM_TABLE *systab)
EFI entry point.
Definition: efiprefix.c:44
EFI autoexec script.
RETURN_STATUS EFI_STATUS
Function return status for EFI API.
Definition: UefiBaseType.h:31
Device model.
EFI_STATUS efi_init(EFI_HANDLE image_handle, EFI_SYSTEM_TABLE *systab)
Initialise EFI environment.
Definition: efi_init.c:154
static int efi_probe(struct root_device *rootdev __unused)
Probe EFI root bus.
Definition: efiprefix.c:119
A Uniform Resource Identifier.
Definition: uri.h:64
iPXE EFI SNP interface
__asmcall int main(void)
Main entry point.
Definition: main.c:28
#define EFIRC(rc)
Convert an iPXE status code to an EFI status code.
Definition: efi.h:166
Definition: efi.h:61
EFI_HANDLE DeviceHandle
The device handle that the EFI Image was loaded from.
Definition: LoadedImage.h:55