2 * GRUB -- GRand Unified Bootloader
3 * Copyright (C) 2013-2015 Free Software Foundation, Inc.
5 * GRUB is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * GRUB is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
23 #include <grub/command.h>
24 #include <grub/file.h>
25 #include <grub/efi/efi.h>
26 #include <grub/efi/fdtload.h>
27 #include <grub/efi/memory.h>
29 static void *loaded_fdt;
33 grub_fdt_load (grub_size_t additional_size)
40 size = GRUB_EFI_BYTES_TO_PAGES (grub_fdt_get_totalsize (fdt));
41 grub_efi_free_pages ((grub_addr_t) fdt, size);
47 raw_fdt = grub_efi_get_firmware_fdt();
50 raw_fdt ? grub_fdt_get_totalsize (raw_fdt) : GRUB_FDT_EMPTY_TREE_SZ;
51 size += additional_size;
53 grub_dprintf ("linux", "allocating %d bytes for fdt\n", size);
54 fdt = grub_efi_allocate_any_pages (GRUB_EFI_BYTES_TO_PAGES (size));
60 grub_memmove (fdt, raw_fdt, size);
61 grub_fdt_set_totalsize (fdt, size);
65 grub_fdt_create_empty_tree (fdt, size);
71 grub_fdt_install (void)
73 grub_efi_boot_services_t *b;
74 grub_efi_guid_t fdt_guid = GRUB_EFI_DEVICE_TREE_GUID;
75 grub_efi_status_t status;
77 b = grub_efi_system_table->boot_services;
78 status = b->install_configuration_table (&fdt_guid, fdt);
79 if (status != GRUB_EFI_SUCCESS)
80 return grub_error (GRUB_ERR_IO, "failed to install FDT");
82 grub_dprintf ("fdt", "Installed/updated FDT configuration table @ %p\n",
88 grub_fdt_unload (void) {
92 grub_efi_free_pages ((grub_addr_t) fdt,
93 GRUB_EFI_BYTES_TO_PAGES (grub_fdt_get_totalsize (fdt)));
98 grub_cmd_devicetree (grub_command_t cmd __attribute__ ((unused)),
99 int argc, char *argv[])
106 grub_free (loaded_fdt);
109 /* No arguments means "use firmware FDT". */
112 return GRUB_ERR_NONE;
115 dtb = grub_file_open (argv[0]);
119 size = grub_file_size (dtb);
120 blob = grub_malloc (size);
124 if (grub_file_read (dtb, blob, size) < size)
127 grub_error (GRUB_ERR_BAD_OS, N_("premature end of file %s"), argv[0]);
131 if (grub_fdt_check_header (blob, size) != 0)
133 grub_error (GRUB_ERR_BAD_OS, N_("invalid device tree"));
139 grub_file_close (dtb);
143 if (grub_errno == GRUB_ERR_NONE)
152 static grub_command_t cmd_devicetree;
157 grub_register_command ("devicetree", grub_cmd_devicetree, 0,
158 N_("Load DTB file."));
163 grub_unregister_command (cmd_devicetree);