9519d2e4d3ec1229a76eb4bce773c78c7af24a6e
[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/cpu/fdtload.h>
30 #include <grub/efi/efi.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_pages (0,
152                              GRUB_EFI_BYTES_TO_PAGES (loaded_image->load_options_size));
153   if (!loaded_image->load_options)
154     return grub_errno;
155
156   loaded_image->load_options_size =
157     2 * grub_utf8_to_utf16 (loaded_image->load_options, len,
158                             (grub_uint8_t *) args, len, NULL);
159
160   grub_dprintf ("linux", "starting image %p\n", image_handle);
161   status = b->start_image (image_handle, 0, NULL);
162
163   /* When successful, not reached */
164   b->unload_image (image_handle);
165   grub_efi_free_pages ((grub_efi_physical_address_t) loaded_image->load_options,
166                        GRUB_EFI_BYTES_TO_PAGES (loaded_image->load_options_size));
167
168   return grub_errno;
169 }
170
171 static grub_err_t
172 grub_linux_boot (void)
173 {
174   if (finalize_params_linux () != GRUB_ERR_NONE)
175     return grub_errno;
176
177   return (grub_arm64_uefi_boot_image((grub_addr_t)kernel_addr,
178                                      kernel_size, linux_args));
179 }
180
181 static grub_err_t
182 grub_linux_unload (void)
183 {
184   grub_dl_unref (my_mod);
185   loaded = 0;
186   if (initrd_start)
187     grub_efi_free_pages ((grub_efi_physical_address_t) initrd_start,
188                          GRUB_EFI_BYTES_TO_PAGES (initrd_end - initrd_start));
189   initrd_start = initrd_end = 0;
190   grub_free (linux_args);
191   if (kernel_addr)
192     grub_efi_free_pages ((grub_efi_physical_address_t) kernel_addr,
193                          GRUB_EFI_BYTES_TO_PAGES (kernel_size));
194   grub_fdt_unload ();
195   return GRUB_ERR_NONE;
196 }
197
198 static grub_err_t
199 grub_cmd_initrd (grub_command_t cmd __attribute__ ((unused)),
200                  int argc, char *argv[])
201 {
202   struct grub_linux_initrd_context initrd_ctx = { 0, 0, 0 };
203   int initrd_size, initrd_pages;
204   void *initrd_mem = NULL;
205
206   if (argc == 0)
207     {
208       grub_error (GRUB_ERR_BAD_ARGUMENT, N_("filename expected"));
209       goto fail;
210     }
211
212   if (!loaded)
213     {
214       grub_error (GRUB_ERR_BAD_ARGUMENT,
215                   N_("you need to load the kernel first"));
216       goto fail;
217     }
218
219   if (grub_initrd_init (argc, argv, &initrd_ctx))
220     goto fail;
221
222   initrd_size = grub_get_initrd_size (&initrd_ctx);
223   grub_dprintf ("linux", "Loading initrd\n");
224
225   initrd_pages = (GRUB_EFI_BYTES_TO_PAGES (initrd_size));
226   initrd_mem = grub_efi_allocate_pages (0, initrd_pages);
227   if (!initrd_mem)
228     {
229       grub_error (GRUB_ERR_OUT_OF_MEMORY, N_("out of memory"));
230       goto fail;
231     }
232
233   if (grub_initrd_load (&initrd_ctx, argv, initrd_mem))
234     goto fail;
235
236   initrd_start = (grub_addr_t) initrd_mem;
237   initrd_end = initrd_start + initrd_size;
238   grub_dprintf ("linux", "[addr=%p, size=0x%x]\n",
239                 (void *) initrd_start, initrd_size);
240
241  fail:
242   grub_initrd_close (&initrd_ctx);
243   if (initrd_mem && !initrd_start)
244     grub_efi_free_pages ((grub_efi_physical_address_t) initrd_mem,
245                          initrd_pages);
246
247   return grub_errno;
248 }
249
250 static grub_err_t
251 grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
252                 int argc, char *argv[])
253 {
254   grub_file_t file = 0;
255   struct grub_arm64_linux_kernel_header lh;
256
257   grub_dl_ref (my_mod);
258
259   if (argc == 0)
260     {
261       grub_error (GRUB_ERR_BAD_ARGUMENT, N_("filename expected"));
262       goto fail;
263     }
264
265   file = grub_file_open (argv[0]);
266   if (!file)
267     goto fail;
268
269   kernel_size = grub_file_size (file);
270
271   if (grub_file_read (file, &lh, sizeof (lh)) < (long) sizeof (lh))
272     return grub_errno;
273
274   if (grub_arm64_uefi_check_image (&lh) != GRUB_ERR_NONE)
275     goto fail;
276
277   grub_loader_unset();
278
279   grub_dprintf ("linux", "kernel file size: %lld\n", (long long) kernel_size);
280   kernel_addr = grub_efi_allocate_pages (0, GRUB_EFI_BYTES_TO_PAGES (kernel_size));
281   grub_dprintf ("linux", "kernel numpages: %lld\n",
282                 (long long) GRUB_EFI_BYTES_TO_PAGES (kernel_size));
283   if (!kernel_addr)
284     {
285       grub_error (GRUB_ERR_OUT_OF_MEMORY, N_("out of memory"));
286       goto fail;
287     }
288
289   grub_file_seek (file, 0);
290   if (grub_file_read (file, kernel_addr, kernel_size)
291       < (grub_int64_t) kernel_size)
292     {
293       if (!grub_errno)
294         grub_error (GRUB_ERR_BAD_OS, N_("premature end of file %s"), argv[0]);
295       goto fail;
296     }
297
298   grub_dprintf ("linux", "kernel @ %p\n", kernel_addr);
299
300   cmdline_size = grub_loader_cmdline_size (argc, argv) + sizeof (LINUX_IMAGE);
301   linux_args = grub_malloc (cmdline_size);
302   if (!linux_args)
303     {
304       grub_error (GRUB_ERR_OUT_OF_MEMORY, N_("out of memory"));
305       goto fail;
306     }
307   grub_memcpy (linux_args, LINUX_IMAGE, sizeof (LINUX_IMAGE));
308   grub_create_loader_cmdline (argc, argv,
309                               linux_args + sizeof (LINUX_IMAGE) - 1,
310                               cmdline_size);
311
312   if (grub_errno == GRUB_ERR_NONE)
313     {
314       grub_loader_set (grub_linux_boot, grub_linux_unload, 0);
315       loaded = 1;
316     }
317
318 fail:
319   if (file)
320     grub_file_close (file);
321
322   if (grub_errno != GRUB_ERR_NONE)
323     {
324       grub_dl_unref (my_mod);
325       loaded = 0;
326     }
327
328   if (linux_args && !loaded)
329     grub_free (linux_args);
330
331   if (kernel_addr && !loaded)
332     grub_efi_free_pages ((grub_efi_physical_address_t) kernel_addr,
333                          GRUB_EFI_BYTES_TO_PAGES (kernel_size));
334
335   return grub_errno;
336 }
337
338
339 static grub_command_t cmd_linux, cmd_initrd;
340
341 GRUB_MOD_INIT (linux)
342 {
343   cmd_linux = grub_register_command ("linux", grub_cmd_linux, 0,
344                                      N_("Load Linux."));
345   cmd_initrd = grub_register_command ("initrd", grub_cmd_initrd, 0,
346                                       N_("Load initrd."));
347   my_mod = mod;
348 }
349
350 GRUB_MOD_FINI (linux)
351 {
352   grub_unregister_command (cmd_linux);
353   grub_unregister_command (cmd_initrd);
354 }