27ede46cac506a080c479d5ac884ab3a61a5cb14
[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/fdtload.h>
31 #include <grub/cpu/linux.h>
32 #include <grub/efi/efi.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   grub_dprintf ("xen_loader",
119                 "Xen Hypervisor cmdline : %s @ %p size:%d\n",
120                 xen_hypervisor->cmdline, xen_hypervisor->cmdline,
121                 xen_hypervisor->cmdline_size);
122
123   retval = grub_fdt_set_prop (xen_boot_fdt, chosen_node, "bootargs",
124                               xen_hypervisor->cmdline,
125                               xen_hypervisor->cmdline_size);
126   if (retval)
127     return grub_error (GRUB_ERR_IO, "failed to install/update FDT");
128
129   return GRUB_ERR_NONE;
130 }
131
132 static grub_err_t
133 prepare_xen_module_params (struct xen_boot_binary *module, void *xen_boot_fdt)
134 {
135   int retval, chosen_node = 0, module_node = 0;
136   char module_name[FDT_NODE_NAME_MAX_SIZE];
137
138   retval = grub_snprintf (module_name, FDT_NODE_NAME_MAX_SIZE, "module@%lx",
139                           xen_boot_address_align (module->start,
140                                                   module->align));
141   grub_dprintf ("xen_loader", "Module node name %s \n", module_name);
142
143   if (retval < (int) sizeof ("module@"))
144     return grub_error (GRUB_ERR_IO, N_("failed to get FDT"));
145
146   chosen_node = grub_fdt_find_subnode (xen_boot_fdt, 0, "chosen");
147   if (chosen_node < 0)
148     chosen_node = grub_fdt_add_subnode (xen_boot_fdt, 0, "chosen");
149   if (chosen_node < 1)
150     return grub_error (GRUB_ERR_IO, "failed to get chosen node in FDT");
151
152   module_node =
153     grub_fdt_find_subnode (xen_boot_fdt, chosen_node, module_name);
154   if (module_node < 0)
155     module_node =
156       grub_fdt_add_subnode (xen_boot_fdt, chosen_node, module_name);
157
158   retval = grub_fdt_set_prop (xen_boot_fdt, module_node, "compatible",
159                               MODULE_CUSTOM_COMPATIBLE, sizeof(MODULE_CUSTOM_COMPATIBLE));
160   if (retval)
161     return grub_error (GRUB_ERR_IO, "failed to update FDT");
162
163   grub_dprintf ("xen_loader", "Module\n");
164
165   retval = grub_fdt_set_reg64 (xen_boot_fdt, module_node,
166                                xen_boot_address_align (module->start,
167                                                        module->align),
168                                module->size);
169   if (retval)
170     return grub_error (GRUB_ERR_IO, "failed to update FDT");
171
172   if (module->cmdline && module->cmdline_size > 0)
173     {
174       grub_dprintf ("xen_loader",
175                     "Module cmdline : %s @ %p size:%d\n",
176                     module->cmdline, module->cmdline, module->cmdline_size);
177
178       retval = grub_fdt_set_prop (xen_boot_fdt, module_node, "bootargs",
179                                   module->cmdline, module->cmdline_size + 1);
180       if (retval)
181         return grub_error (GRUB_ERR_IO, "failed to update FDT");
182     }
183   else
184     {
185       grub_dprintf ("xen_loader", "Module has no bootargs!\n");
186     }
187
188   return GRUB_ERR_NONE;
189 }
190
191 static grub_err_t
192 finalize_params_xen_boot (void)
193 {
194   struct xen_boot_binary *module;
195   void *xen_boot_fdt;
196   grub_size_t additional_size = 0x1000;
197
198   /* Hypervisor.  */
199   additional_size += FDT_NODE_NAME_MAX_SIZE + xen_hypervisor->cmdline_size;
200   FOR_LIST_ELEMENTS (module, module_head)
201   {
202     additional_size += 6 * FDT_NODE_NAME_MAX_SIZE + sizeof(MODULE_CUSTOM_COMPATIBLE) - 1
203       + module->cmdline_size;
204   }
205
206   xen_boot_fdt = grub_fdt_load (additional_size);
207   if (!xen_boot_fdt)
208     return grub_error (GRUB_ERR_IO, "failed to get FDT");
209
210   if (xen_hypervisor)
211     {
212       if (prepare_xen_hypervisor_params (xen_boot_fdt) != GRUB_ERR_NONE)
213         goto fail;
214     }
215   else
216     {
217       grub_dprintf ("xen_loader", "Failed to get Xen Hypervisor info!\n");
218       goto fail;
219     }
220
221   /* Set module params info */
222   FOR_LIST_ELEMENTS (module, module_head)
223   {
224     if (module->start && module->size > 0)
225       {
226         grub_dprintf ("xen_loader", "Module @ 0x%lx size:0x%lx\n",
227                       xen_boot_address_align (module->start, module->align),
228                       module->size);
229         if (prepare_xen_module_params (module, xen_boot_fdt) != GRUB_ERR_NONE)
230           goto fail;
231       }
232     else
233       {
234         grub_dprintf ("xen_loader", "Module info error!\n");
235         goto fail;
236       }
237   }
238
239   if (grub_fdt_install() == GRUB_ERR_NONE)
240     return GRUB_ERR_NONE;
241
242 fail:
243   grub_fdt_unload ();
244
245   return grub_error (GRUB_ERR_IO, "failed to install/update FDT");
246 }
247
248
249 static grub_err_t
250 xen_boot (void)
251 {
252   grub_err_t err = finalize_params_xen_boot ();
253   if (err)
254     return err;
255
256   return grub_arm64_uefi_boot_image (xen_hypervisor->start,
257                                      xen_hypervisor->size,
258                                      xen_hypervisor->cmdline);
259 }
260
261 static void
262 single_binary_unload (struct xen_boot_binary *binary)
263 {
264   if (!binary)
265     return;
266
267   if (binary->start && binary->size > 0)
268     {
269       grub_efi_free_pages ((grub_efi_physical_address_t) binary->start,
270                            GRUB_EFI_BYTES_TO_PAGES (binary->size + binary->align));
271     }
272
273   if (binary->cmdline && binary->cmdline_size > 0)
274     {
275       grub_free (binary->cmdline);
276       grub_dprintf ("xen_loader",
277                     "Module cmdline memory free @ %p size: %d\n",
278                     binary->cmdline, binary->cmdline_size);
279     }
280
281   if (!binary->is_hypervisor)
282     grub_list_remove (GRUB_AS_LIST (binary));
283
284   grub_dprintf ("xen_loader",
285                 "Module struct memory free @ %p size: 0x%lx\n",
286                 binary, sizeof (binary));
287   grub_free (binary);
288
289   return;
290 }
291
292 static void
293 all_binaries_unload (void)
294 {
295   struct xen_boot_binary *binary;
296
297   FOR_LIST_ELEMENTS (binary, module_head)
298   {
299     single_binary_unload (binary);
300   }
301
302   if (xen_hypervisor)
303     single_binary_unload (xen_hypervisor);
304
305   return;
306 }
307
308 static grub_err_t
309 xen_unload (void)
310 {
311   loaded = 0;
312   all_binaries_unload ();
313   grub_fdt_unload ();
314   grub_dl_unref (my_mod);
315
316   return GRUB_ERR_NONE;
317 }
318
319 static void
320 xen_boot_binary_load (struct xen_boot_binary *binary, grub_file_t file,
321                       int argc, char *argv[])
322 {
323   binary->size = grub_file_size (file);
324   grub_dprintf ("xen_loader", "Xen_boot file size: 0x%lx\n", binary->size);
325
326   binary->start
327     = (grub_addr_t) grub_efi_allocate_pages (0,
328                                              GRUB_EFI_BYTES_TO_PAGES
329                                              (binary->size +
330                                               binary->align));
331   if (!binary->start)
332     {
333       grub_error (GRUB_ERR_OUT_OF_MEMORY, N_("out of memory"));
334       return;
335     }
336
337   grub_dprintf ("xen_loader", "Xen_boot numpages: 0x%lx\n",
338                 GRUB_EFI_BYTES_TO_PAGES (binary->size + binary->align));
339
340   if (grub_file_read (file, (void *) xen_boot_address_align (binary->start,
341                                                              binary->align),
342                       binary->size) != (grub_ssize_t) binary->size)
343     {
344       single_binary_unload (binary);
345       grub_error (GRUB_ERR_BAD_OS, N_("premature end of file %s"), argv[0]);
346       return;
347     }
348
349   if (argc > 1)
350     {
351       binary->cmdline_size = grub_loader_cmdline_size (argc - 1, argv + 1);
352       binary->cmdline = grub_zalloc (binary->cmdline_size);
353       if (!binary->cmdline)
354         {
355           single_binary_unload (binary);
356           grub_error (GRUB_ERR_OUT_OF_MEMORY, N_("out of memory"));
357           return;
358         }
359       grub_create_loader_cmdline (argc - 1, argv + 1, binary->cmdline,
360                                   binary->cmdline_size);
361       grub_dprintf ("xen_loader",
362                     "Xen_boot cmdline @ %p %s, size: %d\n",
363                     binary->cmdline, binary->cmdline, binary->cmdline_size);
364     }
365   else
366     {
367       binary->cmdline_size = 0;
368       binary->cmdline = NULL;
369     }
370
371   grub_errno = GRUB_ERR_NONE;
372   return;
373 }
374
375 static grub_err_t
376 grub_cmd_xen_module (grub_command_t cmd __attribute__((unused)),
377                      int argc, char *argv[])
378 {
379
380   struct xen_boot_binary *module = NULL;
381   grub_file_t file = 0;
382   int nounzip = 0;
383
384   if (!argc)
385     {
386       grub_error (GRUB_ERR_BAD_ARGUMENT, N_("filename expected"));
387       goto fail;
388     }
389
390   if (grub_strcmp (argv[0], "--nounzip") == 0)
391     {
392       argv++;
393       argc--;
394       nounzip = 1;
395     }
396
397   if (!argc)
398     {
399       grub_error (GRUB_ERR_BAD_ARGUMENT, N_("filename expected"));
400       goto fail;
401     }
402
403   if (!loaded)
404     {
405       grub_error (GRUB_ERR_BAD_ARGUMENT,
406                   N_("you need to load the Xen Hypervisor first"));
407       goto fail;
408     }
409
410   module =
411     (struct xen_boot_binary *) grub_zalloc (sizeof (struct xen_boot_binary));
412   if (!module)
413     return grub_errno;
414
415   module->is_hypervisor = 0;
416   module->align = 4096;
417
418   grub_dprintf ("xen_loader", "Init module and node info\n");
419
420   if (nounzip)
421     grub_file_filter_disable_compression ();
422   file = grub_file_open (argv[0]);
423   if (!file)
424     goto fail;
425
426   xen_boot_binary_load (module, file, argc, argv);
427   if (grub_errno == GRUB_ERR_NONE)
428     grub_list_push (GRUB_AS_LIST_P (&module_head), GRUB_AS_LIST (module));
429
430  fail:
431   if (file)
432     grub_file_close (file);
433   if (grub_errno != GRUB_ERR_NONE)
434     single_binary_unload (module);
435
436   return grub_errno;
437 }
438
439 static grub_err_t
440 grub_cmd_xen_hypervisor (grub_command_t cmd __attribute__ ((unused)),
441                          int argc, char *argv[])
442 {
443   struct xen_hypervisor_header sh;
444   grub_file_t file = NULL;
445
446   grub_dl_ref (my_mod);
447
448   if (!argc)
449     {
450       grub_error (GRUB_ERR_BAD_ARGUMENT, N_("filename expected"));
451       goto fail;
452     }
453
454   file = grub_file_open (argv[0]);
455   if (!file)
456     goto fail;
457
458   if (grub_file_read (file, &sh, sizeof (sh)) != (long) sizeof (sh))
459     goto fail;
460   if (grub_arm64_uefi_check_image
461       ((struct grub_arm64_linux_kernel_header *) &sh) != GRUB_ERR_NONE)
462     goto fail;
463   grub_file_seek (file, 0);
464
465   /* if another module has called grub_loader_set,
466      we need to make sure that another module is unloaded properly */
467   grub_loader_unset ();
468
469   xen_hypervisor =
470     (struct xen_boot_binary *) grub_zalloc (sizeof (struct xen_boot_binary));
471   if (!xen_hypervisor)
472     return grub_errno;
473
474   xen_hypervisor->is_hypervisor = 1;
475   xen_hypervisor->align = (grub_size_t) sh.optional_header.section_alignment;
476
477   xen_boot_binary_load (xen_hypervisor, file, argc, argv);
478   if (grub_errno == GRUB_ERR_NONE)
479     {
480       grub_loader_set (xen_boot, xen_unload, 0);
481       loaded = 1;
482     }
483
484 fail:
485   if (file)
486     grub_file_close (file);
487   if (grub_errno != GRUB_ERR_NONE)
488     {
489       loaded = 0;
490       all_binaries_unload ();
491       grub_dl_unref (my_mod);
492     }
493
494   return grub_errno;
495 }
496
497 static grub_command_t cmd_xen_hypervisor;
498 static grub_command_t cmd_xen_module;
499
500 GRUB_MOD_INIT (xen_boot)
501 {
502   cmd_xen_hypervisor =
503     grub_register_command ("xen_hypervisor", grub_cmd_xen_hypervisor, 0,
504                            N_("Load a xen hypervisor."));
505   cmd_xen_module =
506     grub_register_command ("xen_module", grub_cmd_xen_module, 0,
507                            N_("Load a xen module."));
508   my_mod = mod;
509 }
510
511 GRUB_MOD_FINI (xen_boot)
512 {
513   grub_unregister_command (cmd_xen_hypervisor);
514   grub_unregister_command (cmd_xen_module);
515 }