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/>.
21 #include <grub/cpu/fdtload.h>
24 #include <grub/command.h>
25 #include <grub/file.h>
26 #include <grub/efi/efi.h>
28 static void *loaded_fdt;
32 grub_fdt_load (grub_size_t additional_size)
39 size = GRUB_EFI_BYTES_TO_PAGES (grub_fdt_get_totalsize (fdt));
40 grub_efi_free_pages ((grub_efi_physical_address_t) fdt, size);
46 raw_fdt = grub_efi_get_firmware_fdt();
49 raw_fdt ? grub_fdt_get_totalsize (raw_fdt) : GRUB_FDT_EMPTY_TREE_SZ;
50 size += additional_size;
52 grub_dprintf ("linux", "allocating %ld bytes for fdt\n", size);
53 fdt = grub_efi_allocate_pages (0, GRUB_EFI_BYTES_TO_PAGES (size));
59 grub_memmove (fdt, raw_fdt, size);
60 grub_fdt_set_totalsize (fdt, size);
64 grub_fdt_create_empty_tree (fdt, size);
70 grub_fdt_install (void)
72 grub_efi_boot_services_t *b;
73 grub_efi_guid_t fdt_guid = GRUB_EFI_DEVICE_TREE_GUID;
74 grub_efi_status_t status;
76 b = grub_efi_system_table->boot_services;
77 status = b->install_configuration_table (&fdt_guid, fdt);
78 if (status != GRUB_EFI_SUCCESS)
79 return grub_error (GRUB_ERR_IO, "failed to install FDT");
81 grub_dprintf ("fdt", "Installed/updated FDT configuration table @ %p\n",
87 grub_fdt_unload (void) {
91 grub_efi_free_pages ((grub_efi_physical_address_t) fdt,
92 GRUB_EFI_BYTES_TO_PAGES (grub_fdt_get_totalsize (fdt)));
97 grub_cmd_devicetree (grub_command_t cmd __attribute__ ((unused)),
98 int argc, char *argv[])
105 grub_free (loaded_fdt);
108 /* No arguments means "use firmware FDT". */
111 return GRUB_ERR_NONE;
114 dtb = grub_file_open (argv[0]);
118 size = grub_file_size (dtb);
119 blob = grub_malloc (size);
123 if (grub_file_read (dtb, blob, size) < size)
126 grub_error (GRUB_ERR_BAD_OS, N_("premature end of file %s"), argv[0]);
130 if (grub_fdt_check_header (blob, size) != 0)
132 grub_error (GRUB_ERR_BAD_OS, N_("invalid device tree"));
138 grub_file_close (dtb);
142 if (grub_errno == GRUB_ERR_NONE)
151 static grub_command_t cmd_devicetree;
156 grub_register_command ("devicetree", grub_cmd_devicetree, 0,
157 N_("Load DTB file."));
162 grub_unregister_command (cmd_devicetree);