Remove grub_efi_allocate_pages.
[grub.git] / grub-core / loader / arm64 / fdt.c
1 /*
2  *  GRUB  --  GRand Unified Bootloader
3  *  Copyright (C) 2013-2015  Free Software Foundation, Inc.
4  *
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.
9  *
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.
14  *
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/>.
17  */
18
19 #include <grub/fdt.h>
20 #include <grub/mm.h>
21 #include <grub/cpu/fdtload.h>
22 #include <grub/err.h>
23 #include <grub/dl.h>
24 #include <grub/command.h>
25 #include <grub/file.h>
26 #include <grub/efi/efi.h>
27
28 static void *loaded_fdt;
29 static void *fdt;
30
31 void *
32 grub_fdt_load (grub_size_t additional_size)
33 {
34   void *raw_fdt;
35   grub_size_t size;
36
37   if (fdt)
38     {
39       size = GRUB_EFI_BYTES_TO_PAGES (grub_fdt_get_totalsize (fdt));
40       grub_efi_free_pages ((grub_efi_physical_address_t) fdt, size);
41     }
42
43   if (loaded_fdt)
44     raw_fdt = loaded_fdt;
45   else
46     raw_fdt = grub_efi_get_firmware_fdt();
47
48   size =
49     raw_fdt ? grub_fdt_get_totalsize (raw_fdt) : GRUB_FDT_EMPTY_TREE_SZ;
50   size += additional_size;
51
52   grub_dprintf ("linux", "allocating %ld bytes for fdt\n", size);
53   fdt = grub_efi_allocate_any_pages (GRUB_EFI_BYTES_TO_PAGES (size));
54   if (!fdt)
55     return NULL;
56
57   if (raw_fdt)
58     {
59       grub_memmove (fdt, raw_fdt, size);
60       grub_fdt_set_totalsize (fdt, size);
61     }
62   else
63     {
64       grub_fdt_create_empty_tree (fdt, size);
65     }
66   return fdt;
67 }
68
69 grub_err_t
70 grub_fdt_install (void)
71 {
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;
75
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");
80
81   grub_dprintf ("fdt", "Installed/updated FDT configuration table @ %p\n",
82                 fdt);
83   return GRUB_ERR_NONE;
84 }
85
86 void
87 grub_fdt_unload (void) {
88   if (!fdt) {
89     return;
90   }
91   grub_efi_free_pages ((grub_efi_physical_address_t) fdt,
92                        GRUB_EFI_BYTES_TO_PAGES (grub_fdt_get_totalsize (fdt)));
93   fdt = NULL;
94 }
95
96 static grub_err_t
97 grub_cmd_devicetree (grub_command_t cmd __attribute__ ((unused)),
98                      int argc, char *argv[])
99 {
100   grub_file_t dtb;
101   void *blob = NULL;
102   int size;
103
104   if (loaded_fdt)
105     grub_free (loaded_fdt);
106   loaded_fdt = NULL;
107
108   /* No arguments means "use firmware FDT".  */
109   if (argc == 0)
110     {
111       return GRUB_ERR_NONE;
112     }
113
114   dtb = grub_file_open (argv[0]);
115   if (!dtb)
116     goto out;
117
118   size = grub_file_size (dtb);
119   blob = grub_malloc (size);
120   if (!blob)
121     goto out;
122
123   if (grub_file_read (dtb, blob, size) < size)
124     {
125       if (!grub_errno)
126         grub_error (GRUB_ERR_BAD_OS, N_("premature end of file %s"), argv[0]);
127       goto out;
128     }
129
130   if (grub_fdt_check_header (blob, size) != 0)
131     {
132       grub_error (GRUB_ERR_BAD_OS, N_("invalid device tree"));
133       goto out;
134     }
135
136 out:
137   if (dtb)
138     grub_file_close (dtb);
139
140   if (blob)
141     {
142       if (grub_errno == GRUB_ERR_NONE)
143         loaded_fdt = blob;
144       else
145         grub_free (blob);
146     }
147
148   return grub_errno;
149 }
150
151 static grub_command_t cmd_devicetree;
152
153 GRUB_MOD_INIT (fdt)
154 {
155   cmd_devicetree =
156     grub_register_command ("devicetree", grub_cmd_devicetree, 0,
157                            N_("Load DTB file."));
158 }
159
160 GRUB_MOD_FINI (fdt)
161 {
162   grub_unregister_command (cmd_devicetree);
163 }