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