arm64/xen: Add missing #address-cells and #size-cells properties
[grub.git] / grub-core / loader / arm64 / xen_boot.c
1 /*
2  *  GRUB  --  GRand Unified Bootloader
3  *  Copyright (C) 2014  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/cache.h>
20 #include <grub/charset.h>
21 #include <grub/command.h>
22 #include <grub/err.h>
23 #include <grub/file.h>
24 #include <grub/fdt.h>
25 #include <grub/list.h>
26 #include <grub/loader.h>
27 #include <grub/misc.h>
28 #include <grub/mm.h>
29 #include <grub/types.h>
30 #include <grub/cpu/linux.h>
31 #include <grub/efi/efi.h>
32 #include <grub/efi/fdtload.h>
33 #include <grub/efi/pe32.h>      /* required by struct xen_hypervisor_header */
34 #include <grub/i18n.h>
35 #include <grub/lib/cmdline.h>
36
37 GRUB_MOD_LICENSE ("GPLv3+");
38
39 #define XEN_HYPERVISOR_NAME  "xen_hypervisor"
40 #define MODULE_CUSTOM_COMPATIBLE  "multiboot,module"
41
42 /* This maximum size is defined in Power.org ePAPR V1.1
43  * https://www.power.org/documentation/epapr-version-1-1/
44  * 2.2.1.1 Node Name Requirements
45  * node-name@unit-address
46  * 31 + 1(@) + 16(64bit address in hex format) + 1(\0) = 49
47  */
48 #define FDT_NODE_NAME_MAX_SIZE  (49)
49
50 struct compat_string_struct
51 {
52   grub_size_t size;
53   const char *compat_string;
54 };
55 typedef struct compat_string_struct compat_string_struct_t;
56 #define FDT_COMPATIBLE(x) {.size = sizeof(x), .compat_string = (x)}
57
58 enum module_type
59 {
60   MODULE_IMAGE,
61   MODULE_INITRD,
62   MODULE_XSM,
63   MODULE_CUSTOM
64 };
65 typedef enum module_type module_type_t;
66
67 struct xen_hypervisor_header
68 {
69   struct grub_arm64_linux_kernel_header efi_head;
70
71   /* This is always PE\0\0.  */
72   grub_uint8_t signature[GRUB_PE32_SIGNATURE_SIZE];
73   /* The COFF file header.  */
74   struct grub_pe32_coff_header coff_header;
75   /* The Optional header.  */
76   struct grub_pe64_optional_header optional_header;
77 };
78
79 struct xen_boot_binary
80 {
81   struct xen_boot_binary *next;
82   struct xen_boot_binary **prev;
83   int is_hypervisor;
84
85   grub_addr_t start;
86   grub_size_t size;
87   grub_size_t align;
88
89   char *cmdline;
90   int cmdline_size;
91 };
92
93 static grub_dl_t my_mod;
94
95 static int loaded;
96
97 static struct xen_boot_binary *xen_hypervisor;
98 static struct xen_boot_binary *module_head;
99
100 static __inline grub_addr_t
101 xen_boot_address_align (grub_addr_t start, grub_size_t align)
102 {
103   return (align ? (ALIGN_UP (start, align)) : start);
104 }
105
106 static grub_err_t
107 prepare_xen_hypervisor_params (void *xen_boot_fdt)
108 {
109   int chosen_node = 0;
110   int retval;
111
112   chosen_node = grub_fdt_find_subnode (xen_boot_fdt, 0, "chosen");
113   if (chosen_node < 0)
114     chosen_node = grub_fdt_add_subnode (xen_boot_fdt, 0, "chosen");
115   if (chosen_node < 1)
116     return grub_error (GRUB_ERR_IO, "failed to get chosen node in FDT");
117
118   /*
119    * The address and size are always written using 64-bits value. Set
120    * #address-cells and #size-cells accordingly.
121    */
122   retval = grub_fdt_set_prop32 (xen_boot_fdt, chosen_node, "#address-cells", 2);
123   if (retval)
124     return grub_error (GRUB_ERR_IO, "failed to set #address-cells");
125   retval = grub_fdt_set_prop32 (xen_boot_fdt, chosen_node, "#size-cells", 2);
126   if (retval)
127     return grub_error (GRUB_ERR_IO, "failed to set #size-cells");
128
129   grub_dprintf ("xen_loader",
130                 "Xen Hypervisor cmdline : %s @ %p size:%d\n",
131                 xen_hypervisor->cmdline, xen_hypervisor->cmdline,
132                 xen_hypervisor->cmdline_size);
133
134   retval = grub_fdt_set_prop (xen_boot_fdt, chosen_node, "bootargs",
135                               xen_hypervisor->cmdline,
136                               xen_hypervisor->cmdline_size);
137   if (retval)
138     return grub_error (GRUB_ERR_IO, "failed to install/update FDT");
139
140   return GRUB_ERR_NONE;
141 }
142
143 static grub_err_t
144 prepare_xen_module_params (struct xen_boot_binary *module, void *xen_boot_fdt)
145 {
146   int retval, chosen_node = 0, module_node = 0;
147   char module_name[FDT_NODE_NAME_MAX_SIZE];
148
149   retval = grub_snprintf (module_name, FDT_NODE_NAME_MAX_SIZE, "module@%lx",
150                           xen_boot_address_align (module->start,
151                                                   module->align));
152   grub_dprintf ("xen_loader", "Module node name %s \n", module_name);
153
154   if (retval < (int) sizeof ("module@"))
155     return grub_error (GRUB_ERR_IO, N_("failed to get FDT"));
156
157   chosen_node = grub_fdt_find_subnode (xen_boot_fdt, 0, "chosen");
158   if (chosen_node < 0)
159     chosen_node = grub_fdt_add_subnode (xen_boot_fdt, 0, "chosen");
160   if (chosen_node < 1)
161     return grub_error (GRUB_ERR_IO, "failed to get chosen node in FDT");
162
163   module_node =
164     grub_fdt_find_subnode (xen_boot_fdt, chosen_node, module_name);
165   if (module_node < 0)
166     module_node =
167       grub_fdt_add_subnode (xen_boot_fdt, chosen_node, module_name);
168
169   retval = grub_fdt_set_prop (xen_boot_fdt, module_node, "compatible",
170                               MODULE_CUSTOM_COMPATIBLE, sizeof(MODULE_CUSTOM_COMPATIBLE));
171   if (retval)
172     return grub_error (GRUB_ERR_IO, "failed to update FDT");
173
174   grub_dprintf ("xen_loader", "Module\n");
175
176   retval = grub_fdt_set_reg64 (xen_boot_fdt, module_node,
177                                xen_boot_address_align (module->start,
178                                                        module->align),
179                                module->size);
180   if (retval)
181     return grub_error (GRUB_ERR_IO, "failed to update FDT");
182
183   if (module->cmdline && module->cmdline_size > 0)
184     {
185       grub_dprintf ("xen_loader",
186                     "Module cmdline : %s @ %p size:%d\n",
187                     module->cmdline, module->cmdline, module->cmdline_size);
188
189       retval = grub_fdt_set_prop (xen_boot_fdt, module_node, "bootargs",
190                                   module->cmdline, module->cmdline_size + 1);
191       if (retval)
192         return grub_error (GRUB_ERR_IO, "failed to update FDT");
193     }
194   else
195     {
196       grub_dprintf ("xen_loader", "Module has no bootargs!\n");
197     }
198
199   return GRUB_ERR_NONE;
200 }
201
202 static grub_err_t
203 finalize_params_xen_boot (void)
204 {
205   struct xen_boot_binary *module;
206   void *xen_boot_fdt;
207   grub_size_t additional_size = 0x1000;
208
209   /* Hypervisor.  */
210   additional_size += FDT_NODE_NAME_MAX_SIZE + xen_hypervisor->cmdline_size;
211   FOR_LIST_ELEMENTS (module, module_head)
212   {
213     additional_size += 6 * FDT_NODE_NAME_MAX_SIZE + sizeof(MODULE_CUSTOM_COMPATIBLE) - 1
214       + module->cmdline_size;
215   }
216
217   xen_boot_fdt = grub_fdt_load (additional_size);
218   if (!xen_boot_fdt)
219     return grub_error (GRUB_ERR_IO, "failed to get FDT");
220
221   if (xen_hypervisor)
222     {
223       if (prepare_xen_hypervisor_params (xen_boot_fdt) != GRUB_ERR_NONE)
224         goto fail;
225     }
226   else
227     {
228       grub_dprintf ("xen_loader", "Failed to get Xen Hypervisor info!\n");
229       goto fail;
230     }
231
232   /* Set module params info */
233   FOR_LIST_ELEMENTS (module, module_head)
234   {
235     if (module->start && module->size > 0)
236       {
237         grub_dprintf ("xen_loader", "Module @ 0x%lx size:0x%lx\n",
238                       xen_boot_address_align (module->start, module->align),
239                       module->size);
240         if (prepare_xen_module_params (module, xen_boot_fdt) != GRUB_ERR_NONE)
241           goto fail;
242       }
243     else
244       {
245         grub_dprintf ("xen_loader", "Module info error!\n");
246         goto fail;
247       }
248   }
249
250   if (grub_fdt_install() == GRUB_ERR_NONE)
251     return GRUB_ERR_NONE;
252
253 fail:
254   grub_fdt_unload ();
255
256   return grub_error (GRUB_ERR_IO, "failed to install/update FDT");
257 }
258
259
260 static grub_err_t
261 xen_boot (void)
262 {
263   grub_err_t err = finalize_params_xen_boot ();
264   if (err)
265     return err;
266
267   return grub_arm64_uefi_boot_image (xen_hypervisor->start,
268                                      xen_hypervisor->size,
269                                      xen_hypervisor->cmdline);
270 }
271
272 static void
273 single_binary_unload (struct xen_boot_binary *binary)
274 {
275   if (!binary)
276     return;
277
278   if (binary->start && binary->size > 0)
279     {
280       grub_efi_free_pages ((grub_efi_physical_address_t) binary->start,
281                            GRUB_EFI_BYTES_TO_PAGES (binary->size + binary->align));
282     }
283
284   if (binary->cmdline && binary->cmdline_size > 0)
285     {
286       grub_free (binary->cmdline);
287       grub_dprintf ("xen_loader",
288                     "Module cmdline memory free @ %p size: %d\n",
289                     binary->cmdline, binary->cmdline_size);
290     }
291
292   if (!binary->is_hypervisor)
293     grub_list_remove (GRUB_AS_LIST (binary));
294
295   grub_dprintf ("xen_loader",
296                 "Module struct memory free @ %p size: 0x%lx\n",
297                 binary, sizeof (binary));
298   grub_free (binary);
299
300   return;
301 }
302
303 static void
304 all_binaries_unload (void)
305 {
306   struct xen_boot_binary *binary;
307
308   FOR_LIST_ELEMENTS (binary, module_head)
309   {
310     single_binary_unload (binary);
311   }
312
313   if (xen_hypervisor)
314     single_binary_unload (xen_hypervisor);
315
316   return;
317 }
318
319 static grub_err_t
320 xen_unload (void)
321 {
322   loaded = 0;
323   all_binaries_unload ();
324   grub_fdt_unload ();
325   grub_dl_unref (my_mod);
326
327   return GRUB_ERR_NONE;
328 }
329
330 static void
331 xen_boot_binary_load (struct xen_boot_binary *binary, grub_file_t file,
332                       int argc, char *argv[])
333 {
334   binary->size = grub_file_size (file);
335   grub_dprintf ("xen_loader", "Xen_boot file size: 0x%lx\n", binary->size);
336
337   binary->start
338     = (grub_addr_t) grub_efi_allocate_any_pages (GRUB_EFI_BYTES_TO_PAGES
339                                                  (binary->size +
340                                                   binary->align));
341   if (!binary->start)
342     {
343       grub_error (GRUB_ERR_OUT_OF_MEMORY, N_("out of memory"));
344       return;
345     }
346
347   grub_dprintf ("xen_loader", "Xen_boot numpages: 0x%lx\n",
348                 GRUB_EFI_BYTES_TO_PAGES (binary->size + binary->align));
349
350   if (grub_file_read (file, (void *) xen_boot_address_align (binary->start,
351                                                              binary->align),
352                       binary->size) != (grub_ssize_t) binary->size)
353     {
354       single_binary_unload (binary);
355       grub_error (GRUB_ERR_BAD_OS, N_("premature end of file %s"), argv[0]);
356       return;
357     }
358
359   if (argc > 1)
360     {
361       binary->cmdline_size = grub_loader_cmdline_size (argc - 1, argv + 1);
362       binary->cmdline = grub_zalloc (binary->cmdline_size);
363       if (!binary->cmdline)
364         {
365           single_binary_unload (binary);
366           grub_error (GRUB_ERR_OUT_OF_MEMORY, N_("out of memory"));
367           return;
368         }
369       grub_create_loader_cmdline (argc - 1, argv + 1, binary->cmdline,
370                                   binary->cmdline_size);
371       grub_dprintf ("xen_loader",
372                     "Xen_boot cmdline @ %p %s, size: %d\n",
373                     binary->cmdline, binary->cmdline, binary->cmdline_size);
374     }
375   else
376     {
377       binary->cmdline_size = 0;
378       binary->cmdline = NULL;
379     }
380
381   grub_errno = GRUB_ERR_NONE;
382   return;
383 }
384
385 static grub_err_t
386 grub_cmd_xen_module (grub_command_t cmd __attribute__((unused)),
387                      int argc, char *argv[])
388 {
389
390   struct xen_boot_binary *module = NULL;
391   grub_file_t file = 0;
392   int nounzip = 0;
393
394   if (!argc)
395     {
396       grub_error (GRUB_ERR_BAD_ARGUMENT, N_("filename expected"));
397       goto fail;
398     }
399
400   if (grub_strcmp (argv[0], "--nounzip") == 0)
401     {
402       argv++;
403       argc--;
404       nounzip = 1;
405     }
406
407   if (!argc)
408     {
409       grub_error (GRUB_ERR_BAD_ARGUMENT, N_("filename expected"));
410       goto fail;
411     }
412
413   if (!loaded)
414     {
415       grub_error (GRUB_ERR_BAD_ARGUMENT,
416                   N_("you need to load the Xen Hypervisor first"));
417       goto fail;
418     }
419
420   module =
421     (struct xen_boot_binary *) grub_zalloc (sizeof (struct xen_boot_binary));
422   if (!module)
423     return grub_errno;
424
425   module->is_hypervisor = 0;
426   module->align = 4096;
427
428   grub_dprintf ("xen_loader", "Init module and node info\n");
429
430   if (nounzip)
431     grub_file_filter_disable_compression ();
432   file = grub_file_open (argv[0]);
433   if (!file)
434     goto fail;
435
436   xen_boot_binary_load (module, file, argc, argv);
437   if (grub_errno == GRUB_ERR_NONE)
438     grub_list_push (GRUB_AS_LIST_P (&module_head), GRUB_AS_LIST (module));
439
440  fail:
441   if (file)
442     grub_file_close (file);
443   if (grub_errno != GRUB_ERR_NONE)
444     single_binary_unload (module);
445
446   return grub_errno;
447 }
448
449 static grub_err_t
450 grub_cmd_xen_hypervisor (grub_command_t cmd __attribute__ ((unused)),
451                          int argc, char *argv[])
452 {
453   struct xen_hypervisor_header sh;
454   grub_file_t file = NULL;
455
456   grub_dl_ref (my_mod);
457
458   if (!argc)
459     {
460       grub_error (GRUB_ERR_BAD_ARGUMENT, N_("filename expected"));
461       goto fail;
462     }
463
464   file = grub_file_open (argv[0]);
465   if (!file)
466     goto fail;
467
468   if (grub_file_read (file, &sh, sizeof (sh)) != (long) sizeof (sh))
469     goto fail;
470   if (grub_arm64_uefi_check_image
471       ((struct grub_arm64_linux_kernel_header *) &sh) != GRUB_ERR_NONE)
472     goto fail;
473   grub_file_seek (file, 0);
474
475   /* if another module has called grub_loader_set,
476      we need to make sure that another module is unloaded properly */
477   grub_loader_unset ();
478
479   xen_hypervisor =
480     (struct xen_boot_binary *) grub_zalloc (sizeof (struct xen_boot_binary));
481   if (!xen_hypervisor)
482     return grub_errno;
483
484   xen_hypervisor->is_hypervisor = 1;
485   xen_hypervisor->align = (grub_size_t) sh.optional_header.section_alignment;
486
487   xen_boot_binary_load (xen_hypervisor, file, argc, argv);
488   if (grub_errno == GRUB_ERR_NONE)
489     {
490       grub_loader_set (xen_boot, xen_unload, 0);
491       loaded = 1;
492     }
493
494 fail:
495   if (file)
496     grub_file_close (file);
497   if (grub_errno != GRUB_ERR_NONE)
498     {
499       loaded = 0;
500       all_binaries_unload ();
501       grub_dl_unref (my_mod);
502     }
503
504   return grub_errno;
505 }
506
507 static grub_command_t cmd_xen_hypervisor;
508 static grub_command_t cmd_xen_module;
509
510 GRUB_MOD_INIT (xen_boot)
511 {
512   cmd_xen_hypervisor =
513     grub_register_command ("xen_hypervisor", grub_cmd_xen_hypervisor, 0,
514                            N_("Load a xen hypervisor."));
515   cmd_xen_module =
516     grub_register_command ("xen_module", grub_cmd_xen_module, 0,
517                            N_("Load a xen module."));
518   my_mod = mod;
519 }
520
521 GRUB_MOD_FINI (xen_boot)
522 {
523   grub_unregister_command (cmd_xen_hypervisor);
524   grub_unregister_command (cmd_xen_module);
525 }