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