arm64 linux loader: improve type portability
[grub.git] / grub-core / loader / arm64 / linux.c
1 /*
2  *  GRUB  --  GRand Unified Bootloader
3  *  Copyright (C) 2013  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/charset.h>
20 #include <grub/command.h>
21 #include <grub/err.h>
22 #include <grub/file.h>
23 #include <grub/fdt.h>
24 #include <grub/linux.h>
25 #include <grub/loader.h>
26 #include <grub/mm.h>
27 #include <grub/types.h>
28 #include <grub/cpu/linux.h>
29 #include <grub/efi/efi.h>
30 #include <grub/efi/fdtload.h>
31 #include <grub/efi/pe32.h>
32 #include <grub/i18n.h>
33 #include <grub/lib/cmdline.h>
34
35 GRUB_MOD_LICENSE ("GPLv3+");
36
37 static grub_dl_t my_mod;
38 static int loaded;
39
40 static void *kernel_addr;
41 static grub_uint64_t kernel_size;
42
43 static char *linux_args;
44 static grub_uint32_t cmdline_size;
45
46 static grub_addr_t initrd_start;
47 static grub_addr_t initrd_end;
48
49 grub_err_t
50 grub_arm64_uefi_check_image (struct grub_arm64_linux_kernel_header * lh)
51 {
52   if (lh->magic != GRUB_ARM64_LINUX_MAGIC)
53     return grub_error(GRUB_ERR_BAD_OS, "invalid magic number");
54
55   if ((lh->code0 & 0xffff) != GRUB_PE32_MAGIC)
56     return grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET,
57                        N_("plain image kernel not supported - rebuild with CONFIG_(U)EFI_STUB enabled"));
58
59   grub_dprintf ("linux", "UEFI stub kernel:\n");
60   grub_dprintf ("linux", "PE/COFF header @ %08x\n", lh->hdr_offset);
61
62   return GRUB_ERR_NONE;
63 }
64
65 static grub_err_t
66 finalize_params_linux (void)
67 {
68   int node, retval;
69
70   void *fdt;
71
72   fdt = grub_fdt_load (0x400);
73
74   if (!fdt)
75     goto failure;
76
77   node = grub_fdt_find_subnode (fdt, 0, "chosen");
78   if (node < 0)
79     node = grub_fdt_add_subnode (fdt, 0, "chosen");
80
81   if (node < 1)
82     goto failure;
83
84   /* Set initrd info */
85   if (initrd_start && initrd_end > initrd_start)
86     {
87       grub_dprintf ("linux", "Initrd @ %p-%p\n",
88                     (void *) initrd_start, (void *) initrd_end);
89
90       retval = grub_fdt_set_prop64 (fdt, node, "linux,initrd-start",
91                                     initrd_start);
92       if (retval)
93         goto failure;
94       retval = grub_fdt_set_prop64 (fdt, node, "linux,initrd-end",
95                                     initrd_end);
96       if (retval)
97         goto failure;
98     }
99
100   if (grub_fdt_install() != GRUB_ERR_NONE)
101     goto failure;
102
103   return GRUB_ERR_NONE;
104
105 failure:
106   grub_fdt_unload();
107   return grub_error(GRUB_ERR_BAD_OS, "failed to install/update FDT");
108 }
109
110 grub_err_t
111 grub_arm64_uefi_boot_image (grub_addr_t addr, grub_size_t size, char *args)
112 {
113   grub_efi_memory_mapped_device_path_t *mempath;
114   grub_efi_handle_t image_handle;
115   grub_efi_boot_services_t *b;
116   grub_efi_status_t status;
117   grub_efi_loaded_image_t *loaded_image;
118   int len;
119
120   mempath = grub_malloc (2 * sizeof (grub_efi_memory_mapped_device_path_t));
121   if (!mempath)
122     return grub_errno;
123
124   mempath[0].header.type = GRUB_EFI_HARDWARE_DEVICE_PATH_TYPE;
125   mempath[0].header.subtype = GRUB_EFI_MEMORY_MAPPED_DEVICE_PATH_SUBTYPE;
126   mempath[0].header.length = grub_cpu_to_le16_compile_time (sizeof (*mempath));
127   mempath[0].memory_type = GRUB_EFI_LOADER_DATA;
128   mempath[0].start_address = addr;
129   mempath[0].end_address = addr + size;
130
131   mempath[1].header.type = GRUB_EFI_END_DEVICE_PATH_TYPE;
132   mempath[1].header.subtype = GRUB_EFI_END_ENTIRE_DEVICE_PATH_SUBTYPE;
133   mempath[1].header.length = sizeof (grub_efi_device_path_t);
134
135   b = grub_efi_system_table->boot_services;
136   status = b->load_image (0, grub_efi_image_handle,
137                           (grub_efi_device_path_t *) mempath,
138                           (void *) addr, size, &image_handle);
139   if (status != GRUB_EFI_SUCCESS)
140     return grub_error (GRUB_ERR_BAD_OS, "cannot load image");
141
142   grub_dprintf ("linux", "linux command line: '%s'\n", args);
143
144   /* Convert command line to UCS-2 */
145   loaded_image = grub_efi_get_loaded_image (image_handle);
146   loaded_image->load_options_size = len =
147     (grub_strlen (args) + 1) * sizeof (grub_efi_char16_t);
148   loaded_image->load_options =
149     grub_efi_allocate_any_pages (GRUB_EFI_BYTES_TO_PAGES (loaded_image->load_options_size));
150   if (!loaded_image->load_options)
151     return grub_errno;
152
153   loaded_image->load_options_size =
154     2 * grub_utf8_to_utf16 (loaded_image->load_options, len,
155                             (grub_uint8_t *) args, len, NULL);
156
157   grub_dprintf ("linux", "starting image %p\n", image_handle);
158   status = b->start_image (image_handle, 0, NULL);
159
160   /* When successful, not reached */
161   b->unload_image (image_handle);
162   grub_efi_free_pages ((grub_addr_t) loaded_image->load_options,
163                        GRUB_EFI_BYTES_TO_PAGES (loaded_image->load_options_size));
164
165   return grub_errno;
166 }
167
168 static grub_err_t
169 grub_linux_boot (void)
170 {
171   if (finalize_params_linux () != GRUB_ERR_NONE)
172     return grub_errno;
173
174   return (grub_arm64_uefi_boot_image((grub_addr_t)kernel_addr,
175                                      kernel_size, linux_args));
176 }
177
178 static grub_err_t
179 grub_linux_unload (void)
180 {
181   grub_dl_unref (my_mod);
182   loaded = 0;
183   if (initrd_start)
184     grub_efi_free_pages ((grub_efi_physical_address_t) initrd_start,
185                          GRUB_EFI_BYTES_TO_PAGES (initrd_end - initrd_start));
186   initrd_start = initrd_end = 0;
187   grub_free (linux_args);
188   if (kernel_addr)
189     grub_efi_free_pages ((grub_addr_t) kernel_addr,
190                          GRUB_EFI_BYTES_TO_PAGES (kernel_size));
191   grub_fdt_unload ();
192   return GRUB_ERR_NONE;
193 }
194
195 static grub_err_t
196 grub_cmd_initrd (grub_command_t cmd __attribute__ ((unused)),
197                  int argc, char *argv[])
198 {
199   struct grub_linux_initrd_context initrd_ctx = { 0, 0, 0 };
200   int initrd_size, initrd_pages;
201   void *initrd_mem = NULL;
202
203   if (argc == 0)
204     {
205       grub_error (GRUB_ERR_BAD_ARGUMENT, N_("filename expected"));
206       goto fail;
207     }
208
209   if (!loaded)
210     {
211       grub_error (GRUB_ERR_BAD_ARGUMENT,
212                   N_("you need to load the kernel first"));
213       goto fail;
214     }
215
216   if (grub_initrd_init (argc, argv, &initrd_ctx))
217     goto fail;
218
219   initrd_size = grub_get_initrd_size (&initrd_ctx);
220   grub_dprintf ("linux", "Loading initrd\n");
221
222   initrd_pages = (GRUB_EFI_BYTES_TO_PAGES (initrd_size));
223   initrd_mem = grub_efi_allocate_any_pages (initrd_pages);
224   if (!initrd_mem)
225     {
226       grub_error (GRUB_ERR_OUT_OF_MEMORY, N_("out of memory"));
227       goto fail;
228     }
229
230   if (grub_initrd_load (&initrd_ctx, argv, initrd_mem))
231     goto fail;
232
233   initrd_start = (grub_addr_t) initrd_mem;
234   initrd_end = initrd_start + initrd_size;
235   grub_dprintf ("linux", "[addr=%p, size=0x%x]\n",
236                 (void *) initrd_start, initrd_size);
237
238  fail:
239   grub_initrd_close (&initrd_ctx);
240   if (initrd_mem && !initrd_start)
241     grub_efi_free_pages ((grub_addr_t) initrd_mem, initrd_pages);
242
243   return grub_errno;
244 }
245
246 static grub_err_t
247 grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
248                 int argc, char *argv[])
249 {
250   grub_file_t file = 0;
251   struct grub_arm64_linux_kernel_header lh;
252
253   grub_dl_ref (my_mod);
254
255   if (argc == 0)
256     {
257       grub_error (GRUB_ERR_BAD_ARGUMENT, N_("filename expected"));
258       goto fail;
259     }
260
261   file = grub_file_open (argv[0]);
262   if (!file)
263     goto fail;
264
265   kernel_size = grub_file_size (file);
266
267   if (grub_file_read (file, &lh, sizeof (lh)) < (long) sizeof (lh))
268     return grub_errno;
269
270   if (grub_arm64_uefi_check_image (&lh) != GRUB_ERR_NONE)
271     goto fail;
272
273   grub_loader_unset();
274
275   grub_dprintf ("linux", "kernel file size: %lld\n", (long long) kernel_size);
276   kernel_addr = grub_efi_allocate_any_pages (GRUB_EFI_BYTES_TO_PAGES (kernel_size));
277   grub_dprintf ("linux", "kernel numpages: %lld\n",
278                 (long long) GRUB_EFI_BYTES_TO_PAGES (kernel_size));
279   if (!kernel_addr)
280     {
281       grub_error (GRUB_ERR_OUT_OF_MEMORY, N_("out of memory"));
282       goto fail;
283     }
284
285   grub_file_seek (file, 0);
286   if (grub_file_read (file, kernel_addr, kernel_size)
287       < (grub_int64_t) kernel_size)
288     {
289       if (!grub_errno)
290         grub_error (GRUB_ERR_BAD_OS, N_("premature end of file %s"), argv[0]);
291       goto fail;
292     }
293
294   grub_dprintf ("linux", "kernel @ %p\n", kernel_addr);
295
296   cmdline_size = grub_loader_cmdline_size (argc, argv) + sizeof (LINUX_IMAGE);
297   linux_args = grub_malloc (cmdline_size);
298   if (!linux_args)
299     {
300       grub_error (GRUB_ERR_OUT_OF_MEMORY, N_("out of memory"));
301       goto fail;
302     }
303   grub_memcpy (linux_args, LINUX_IMAGE, sizeof (LINUX_IMAGE));
304   grub_create_loader_cmdline (argc, argv,
305                               linux_args + sizeof (LINUX_IMAGE) - 1,
306                               cmdline_size);
307
308   if (grub_errno == GRUB_ERR_NONE)
309     {
310       grub_loader_set (grub_linux_boot, grub_linux_unload, 0);
311       loaded = 1;
312     }
313
314 fail:
315   if (file)
316     grub_file_close (file);
317
318   if (grub_errno != GRUB_ERR_NONE)
319     {
320       grub_dl_unref (my_mod);
321       loaded = 0;
322     }
323
324   if (linux_args && !loaded)
325     grub_free (linux_args);
326
327   if (kernel_addr && !loaded)
328     grub_efi_free_pages ((grub_addr_t) kernel_addr,
329                          GRUB_EFI_BYTES_TO_PAGES (kernel_size));
330
331   return grub_errno;
332 }
333
334
335 static grub_command_t cmd_linux, cmd_initrd;
336
337 GRUB_MOD_INIT (linux)
338 {
339   cmd_linux = grub_register_command ("linux", grub_cmd_linux, 0,
340                                      N_("Load Linux."));
341   cmd_initrd = grub_register_command ("initrd", grub_cmd_initrd, 0,
342                                       N_("Load initrd."));
343   my_mod = mod;
344 }
345
346 GRUB_MOD_FINI (linux)
347 {
348   grub_unregister_command (cmd_linux);
349   grub_unregister_command (cmd_initrd);
350 }