iPXE
efi_fdt.c
Go to the documentation of this file.
1/*
2 * Copyright (C) 2019 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 (at your option) 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 * You can also choose to distribute this program under the terms of
20 * the Unmodified Binary Distribution Licence (as given in the file
21 * COPYING.UBDL), provided that you have satisfied its requirements.
22 */
23
24FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
25FILE_SECBOOT ( PERMITTED );
26
27#include <string.h>
28#include <byteswap.h>
29#include <ipxe/fdt.h>
30#include <ipxe/init.h>
31#include <ipxe/efi/efi.h>
32#include <ipxe/efi/efi_table.h>
33#include <ipxe/efi/efi_fdt.h>
34#include <ipxe/efi/Guid/Fdt.h>
35
36/** @file
37 *
38 * EFI Flattened Device Tree
39 *
40 */
41
42/** EFI Flattened Device Tree configuration table */
43static struct fdt_header *efi_fdt;
44EFI_USE_TABLE ( FDT_TABLE, &efi_fdt, 0 );
45
46/**
47 * Initialise EFI Flattened Device Tree
48 *
49 */
50static void efi_fdt_init ( void ) {
51 EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
52 EFI_STATUS efirc;
53 int rc;
54
55 /* Do nothing if no configuration table is present */
56 if ( ! efi_fdt ) {
57 DBGC ( &efi_fdt, "EFIFDT has no configuration table\n" );
58 return;
59 }
60 DBGC ( &efi_fdt, "EFIFDT configuration table at %p\n", efi_fdt );
61
62 /* Parse as system device tree */
63 if ( ( rc = fdt_parse ( &sysfdt, efi_fdt, -1UL ) ) != 0 ) {
64 DBGC ( &efi_fdt, "EFIFDT could not parse: %s\n",
65 strerror ( rc ) );
66 return;
67 }
68
69 /* Create copy, since table may be removed at any time */
70 if ( ( efirc = bs->AllocatePool ( EfiBootServicesData, sysfdt.len,
71 &sysfdt.raw ) ) != 0 ) {
72 DBGC ( &efi_fdt, "EFIFDT could not create copy\n" );
73 sysfdt.len = 0;
74 return;
75 }
76 memcpy ( sysfdt.raw, efi_fdt, sysfdt.len );
77}
78
79/** EFI Flattened Device Tree initialisation function */
80struct init_fn efi_fdt_init_fn __init_fn ( INIT_EARLY ) = {
81 .name = "efifdt",
82 .initialise = efi_fdt_init,
83};
84
85/**
86 * Determine length of EFI Flattened Device Tree
87 *
88 * @v data Configuration table data (presumed valid)
89 * @ret len Length of table
90 */
91static size_t efi_fdt_len ( const void *data ) {
92 const struct fdt_header *hdr = data;
93
94 return be32_to_cpu ( hdr->totalsize );
95}
96
97/** EFI Flattened Device Tree table type */
98static struct efi_table efi_fdt_table = {
99 .guid = &efi_fdt_table_guid,
100 .len = efi_fdt_len,
101};
102
103/** EFI Flattened Device Tree table backup */
104static void *efi_fdt_backup;
105
106/** EFI Flattened Device Tree installed table */
108
109/**
110 * Install EFI Flattened Device Tree table
111 *
112 * @v cmdline Command line, or NULL
113 * @ret rc Return status code
114 */
115int efi_fdt_install ( const char *cmdline ) {
116 int rc;
117
118 /* Create device tree */
119 if ( ( rc = fdt_create ( &efi_fdt_installed, cmdline, 0, 0 ) ) != 0 ) {
120 DBGC ( &efi_fdt, "EFI_FDT could not install: %s\n",
121 strerror ( rc ) );
122 goto err_create;
123 }
124
125 /* Install table */
127 &efi_fdt_backup ) ) != 0 ) {
128 DBGC ( &efi_fdt, "EFIFDT could not install: %s\n",
129 strerror ( rc ) );
130 goto err_install;
131 }
132
133 return 0;
134
136 err_install:
138 err_create:
139 return rc;
140}
141
142/**
143 * Uninstall EFI Flattened Device Tree table
144 *
145 * @ret rc Return status code
146 */
147int efi_fdt_uninstall ( void ) {
148 int rc;
149
150 /* Uninstall table */
152 &efi_fdt_backup ) ) != 0 ) {
153 DBGC ( &efi_fdt, "EFIFDT could not %sinstall: %s\n",
154 ( efi_fdt_backup ? "re" : "un" ), strerror ( rc ) );
155 /* Leak memory: there is nothing else we can do */
156 return rc;
157 }
158
159 /* Remove table */
161
162 return 0;
163}
struct golan_inbox_hdr hdr
Message header.
Definition CIB_PRM.h:0
Copyright (c) 2013-2014, ARM Limited.
RETURN_STATUS EFI_STATUS
Function return status for EFI API.
@ EfiBootServicesData
The data portions of a loaded Boot Serves Driver, and the default data allocation type used by a Boot...
struct arbelprm_rc_send_wqe rc
Definition arbel.h:3
static void efi_fdt_init(void)
Initialise EFI Flattened Device Tree.
Definition efi_fdt.c:50
int efi_fdt_install(const char *cmdline)
Install EFI Flattened Device Tree table.
Definition efi_fdt.c:115
static struct fdt_header * efi_fdt
EFI Flattened Device Tree configuration table.
Definition efi_fdt.c:43
static struct efi_table efi_fdt_table
EFI Flattened Device Tree table type.
Definition efi_fdt.c:98
static void * efi_fdt_backup
EFI Flattened Device Tree table backup.
Definition efi_fdt.c:104
int efi_fdt_uninstall(void)
Uninstall EFI Flattened Device Tree table.
Definition efi_fdt.c:147
static struct fdt_header * efi_fdt_installed
EFI Flattened Device Tree installed table.
Definition efi_fdt.c:107
static size_t efi_fdt_len(const void *data)
Determine length of EFI Flattened Device Tree.
Definition efi_fdt.c:91
EFI Flattened Device Tree.
EFI_GUID efi_fdt_table_guid
FDT table GUID.
Definition efi_guid.c:449
int efi_install_table(struct efi_table *table, const void *data, void **backup)
Install EFI configuration table.
Definition efi_table.c:71
int efi_uninstall_table(struct efi_table *table, void **backup)
Uninstall EFI configuration table.
Definition efi_table.c:162
EFI configuration tables.
uint8_t data[48]
Additional event data.
Definition ena.h:11
void fdt_remove(struct fdt_header *hdr)
Remove device tree.
Definition fdt.c:1459
int fdt_parse(struct fdt *fdt, struct fdt_header *hdr, size_t max_len)
Parse device tree.
Definition fdt.c:904
int fdt_create(struct fdt_header **hdr, const char *cmdline, physaddr_t initrd, size_t initrd_len)
Create device tree.
Definition fdt.c:1408
struct fdt sysfdt
The system flattened device tree (if present)
Definition fdt.c:45
#define DBGC(...)
Definition compiler.h:505
#define INIT_EARLY
Early initialisation.
Definition init.h:30
#define FILE_LICENCE(_licence)
Declare a particular licence as applying to a file.
Definition compiler.h:896
#define FILE_SECBOOT(_status)
Declare a file's UEFI Secure Boot permission status.
Definition compiler.h:926
#define be32_to_cpu(value)
Definition byteswap.h:117
EFI API.
#define EFI_USE_TABLE(_table, _ptr, _required)
Declare an EFI configuration table to be used by iPXE.
Definition efi.h:154
EFI_SYSTEM_TABLE * efi_systab
Flattened Device Tree.
String functions.
void * memcpy(void *dest, const void *src, size_t len) __nonnull
#define __init_fn(init_order)
Declare an initialisation functon.
Definition init.h:24
uint32_t cmdline
Definition multiboot.h:4
char * strerror(int errno)
Retrieve string representation of error number.
Definition strerror.c:79
EFI Boot Services Table.
Definition UefiSpec.h:1931
EFI_ALLOCATE_POOL AllocatePool
Definition UefiSpec.h:1949
An installable EFI configuration table type.
Definition efi_table.h:16
Device tree header.
Definition fdt.h:19
An initialisation function.
Definition init.h:15