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