arm-efi: Fix compilation
[grub.git] / grub-core / loader / arm / linux.c
1 /* linux.c - boot Linux */
2 /*
3  *  GRUB  --  GRand Unified Bootloader
4  *  Copyright (C) 2013  Free Software Foundation, Inc.
5  *
6  *  GRUB is free software: you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation, either version 3 of the License, or
9  *  (at your option) any later version.
10  *
11  *  GRUB is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 #include <grub/dl.h>
21 #include <grub/fdt.h>
22 #include <grub/file.h>
23 #include <grub/loader.h>
24 #include <grub/mm.h>
25 #include <grub/misc.h>
26 #include <grub/command.h>
27 #include <grub/cache.h>
28 #include <grub/cpu/linux.h>
29 #include <grub/lib/cmdline.h>
30 #include <grub/linux.h>
31
32 GRUB_MOD_LICENSE ("GPLv3+");
33
34 static grub_dl_t my_mod;
35
36 static grub_addr_t initrd_start;
37 static grub_addr_t initrd_end;
38
39 static grub_addr_t linux_addr;
40 static grub_size_t linux_size;
41
42 static char *linux_args;
43
44 static grub_uint32_t machine_type;
45 static const void *current_fdt;
46
47 typedef void (*kernel_entry_t) (int, unsigned long, void *);
48
49 #define LINUX_ZIMAGE_OFFSET     0x24
50 #define LINUX_ZIMAGE_MAGIC      0x016f2818
51
52 #define LINUX_PHYS_OFFSET        (0x00008000)
53 #define LINUX_INITRD_PHYS_OFFSET (LINUX_PHYS_OFFSET + 0x02000000)
54 #define LINUX_FDT_PHYS_OFFSET    (LINUX_INITRD_PHYS_OFFSET - 0x10000)
55
56 static grub_size_t
57 get_atag_size (const grub_uint32_t *atag)
58 {
59   const grub_uint32_t *atag0 = atag;
60   while (atag[0] && atag[1])
61     atag += atag[0];
62   return atag - atag0;
63 }
64
65 /*
66  * linux_prepare_fdt():
67  *   Prepares a loaded FDT for being passed to Linux.
68  *   Merges in command line parameters and sets up initrd addresses.
69  */
70 static grub_err_t
71 linux_prepare_atag (void *target_atag)
72 {
73   const grub_uint32_t *atag_orig = (const grub_uint32_t *) current_fdt;
74   grub_uint32_t *tmp_atag, *to;
75   const grub_uint32_t *from;
76   grub_size_t tmp_size;
77   grub_size_t arg_size = grub_strlen (linux_args);
78   char *cmdline_orig = NULL;
79   grub_size_t cmdline_orig_len = 0;
80
81   /* some place for cmdline, initrd and terminator.  */
82   tmp_size = get_atag_size (atag_orig) + 20 + (arg_size) / 4;
83   tmp_atag = grub_malloc (tmp_size * sizeof (grub_uint32_t));
84   if (!tmp_atag)
85     return grub_errno;
86
87   for (from = atag_orig, to = tmp_atag; from[0] && from[1];
88        from += from[0])
89     switch (from[1])
90       {
91       case 0x54410004:
92       case 0x54410005:
93       case 0x54420005:
94         break;
95       case 0x54410009:
96         if (*(char *) (from + 2))
97           {
98             cmdline_orig = (char *) (from + 2);
99             cmdline_orig_len = grub_strlen (cmdline_orig) + 1;
100           }
101         break;
102       default:
103         grub_memcpy (to, from, sizeof (grub_uint32_t) * from[0]);
104         to += from[0];
105         break;
106       }
107
108   grub_dprintf ("linux", "linux inherited args: '%s'\n",
109                 cmdline_orig ? : "");
110   grub_dprintf ("linux", "linux_args: '%s'\n", linux_args);
111
112   /* Generate and set command line */
113   to[0] = 3 + (arg_size + cmdline_orig_len) / 4;
114   to[1] = 0x54410009;
115   if (cmdline_orig)
116     {
117       grub_memcpy ((char *) to + 8, cmdline_orig, cmdline_orig_len - 1);
118       *((char *) to + 8 + cmdline_orig_len - 1) = ' ';
119     }
120   grub_memcpy ((char *) to + 8 + cmdline_orig_len, linux_args, arg_size);
121   grub_memset ((char *) to + 8 + cmdline_orig_len + arg_size, 0,
122                4 - ((arg_size + cmdline_orig_len) & 3));
123   to += to[0];
124
125   if (initrd_start && initrd_end)
126     {
127       /*
128        * We're using physical addresses, so even if we have LPAE, we're
129        * restricted to a 32-bit address space.
130        */
131       grub_dprintf ("loader", "Initrd @ 0x%08x-0x%08x\n",
132                     initrd_start, initrd_end);
133
134       to[0] = 4;
135       to[1] = 0x54420005;
136       to[2] = initrd_start;
137       to[3] = initrd_end - initrd_start;
138       to += 4;
139     }
140
141   to[0] = 0;
142   to[1] = 0;
143   to += 2;
144
145   /* Copy updated FDT to its launch location */
146   grub_memcpy (target_atag, tmp_atag, sizeof (grub_uint32_t) * (to - tmp_atag));
147   grub_free (tmp_atag);
148
149   grub_dprintf ("loader", "ATAG updated for Linux boot\n");
150
151   return GRUB_ERR_NONE;
152 }
153
154 /*
155  * linux_prepare_fdt():
156  *   Prepares a loaded FDT for being passed to Linux.
157  *   Merges in command line parameters and sets up initrd addresses.
158  */
159 static grub_err_t
160 linux_prepare_fdt (void *target_fdt)
161 {
162   int node;
163   int retval;
164   int tmp_size;
165   void *tmp_fdt;
166
167   tmp_size = grub_fdt_get_totalsize (current_fdt) + 0x100 + grub_strlen (linux_args);
168   tmp_fdt = grub_malloc (tmp_size);
169   if (!tmp_fdt)
170     return grub_errno;
171
172   grub_memcpy (tmp_fdt, current_fdt, grub_fdt_get_totalsize (current_fdt));
173   grub_fdt_set_totalsize (tmp_fdt, tmp_size);
174
175   /* Find or create '/chosen' node */
176   node = grub_fdt_find_subnode (tmp_fdt, 0, "chosen");
177   if (node < 0)
178     {
179       grub_dprintf ("linux", "No 'chosen' node in FDT - creating.\n");
180       node = grub_fdt_add_subnode (tmp_fdt, 0, "chosen");
181       if (node < 0)
182         goto failure;
183     }
184
185   grub_dprintf ("linux", "linux_args: '%s'\n", linux_args);
186
187   /* Generate and set command line */
188   retval = grub_fdt_set_prop (tmp_fdt, node, "bootargs", linux_args,
189                               grub_strlen (linux_args) + 1);
190   if (retval)
191     goto failure;
192
193   if (initrd_start && initrd_end)
194     {
195       /*
196        * We're using physical addresses, so even if we have LPAE, we're
197        * restricted to a 32-bit address space.
198        */
199       grub_dprintf ("loader", "Initrd @ 0x%08x-0x%08x\n",
200                     initrd_start, initrd_end);
201
202       retval = grub_fdt_set_prop32 (tmp_fdt, node, "linux,initrd-start",
203                                     initrd_start);
204       if (retval)
205         goto failure;
206       retval = grub_fdt_set_prop32 (tmp_fdt, node, "linux,initrd-end",
207                                     initrd_end);
208       if (retval)
209         goto failure;
210     }
211
212   /* Copy updated FDT to its launch location */
213   grub_memcpy (target_fdt, tmp_fdt, tmp_size);
214   grub_free (tmp_fdt);
215
216   grub_dprintf ("loader", "FDT updated for Linux boot\n");
217
218   return GRUB_ERR_NONE;
219
220 failure:
221   grub_free (tmp_fdt);
222   return grub_error (GRUB_ERR_BAD_ARGUMENT, "unable to prepare FDT");
223 }
224
225 static grub_err_t
226 linux_boot (void)
227 {
228   kernel_entry_t linuxmain;
229   int fdt_valid, atag_valid;
230   void *target_fdt = 0;
231
232   fdt_valid = (current_fdt && grub_fdt_check_header_nosize (current_fdt) == 0);
233   atag_valid = ((((const grub_uint16_t *) current_fdt)[3] & ~3) == 0x5440
234                 && *((const grub_uint32_t *) current_fdt));
235   grub_dprintf ("loader", "atag: %p, %x, %x, %s, %s\n",
236                 current_fdt,
237                 ((const grub_uint16_t *) current_fdt)[3],
238                 *((const grub_uint32_t *) current_fdt),
239                 (const char *) current_fdt,
240                 (const char *) current_fdt + 1);
241
242   if (!fdt_valid && machine_type == GRUB_ARM_MACHINE_TYPE_FDT)
243     return grub_error (GRUB_ERR_FILE_NOT_FOUND,
244                        N_("device tree must be supplied (see `devicetree' command)"));
245
246   grub_arch_sync_caches ((void *) linux_addr, linux_size);
247
248   grub_dprintf ("loader", "Kernel at: 0x%x\n", linux_addr);
249
250   if (fdt_valid || atag_valid)
251     {
252 #ifdef GRUB_MACHINE_EFI
253       grub_size_t size;
254       if (fdt_valid)
255         size = grub_fdt_get_totalsize (current_fdt);
256       else
257         size = 4 * get_atag_size (current_fdt);
258       size += grub_strlen (linux_args) + 256;
259       target_fdt = grub_efi_allocate_loader_memory (LINUX_FDT_PHYS_OFFSET, size);
260       if (!target_fdt)
261         return grub_error (GRUB_ERR_OUT_OF_MEMORY, N_("out of memory"));
262 #else
263       target_fdt = (void *) LINUX_FDT_ADDRESS;
264 #endif
265     }
266
267   if (fdt_valid)
268     {
269       grub_err_t err;
270
271       err = linux_prepare_fdt (target_fdt);
272       if (err)
273         return err;
274       grub_dprintf ("loader", "FDT @ %p\n", target_fdt);
275     }
276   else if (atag_valid)
277     {
278       grub_err_t err;
279
280       err = linux_prepare_atag (target_fdt);
281       if (err)
282         return err;
283       grub_dprintf ("loader", "ATAG @ %p\n", target_fdt);
284     }
285
286   grub_dprintf ("loader", "Jumping to Linux...\n");
287
288   /* Boot the kernel.
289    *   Arguments to kernel:
290    *     r0 - 0
291    *     r1 - machine type
292    *     r2 - address of DTB
293    */
294   linuxmain = (kernel_entry_t) linux_addr;
295
296 #ifdef GRUB_MACHINE_EFI
297   {
298     grub_err_t err;
299     err = grub_efi_prepare_platform();
300     if (err != GRUB_ERR_NONE)
301       return err;
302   }
303 #endif
304
305   grub_arm_disable_caches_mmu ();
306
307   linuxmain (0, machine_type, target_fdt);
308
309   return grub_error (GRUB_ERR_BAD_OS, "Linux call returned");
310 }
311
312 /*
313  * Only support zImage, so no relocations necessary
314  */
315 static grub_err_t
316 linux_load (const char *filename, grub_file_t file)
317 {
318   int size;
319
320   size = grub_file_size (file);
321
322 #ifdef GRUB_MACHINE_EFI
323   linux_addr = (grub_addr_t) grub_efi_allocate_loader_memory (LINUX_PHYS_OFFSET, size);
324   if (!linux_addr)
325     return grub_errno;
326 #else
327   linux_addr = LINUX_ADDRESS;
328 #endif
329   grub_dprintf ("loader", "Loading Linux to 0x%08x\n",
330                 (grub_addr_t) linux_addr);
331
332   if (grub_file_read (file, (void *) linux_addr, size) != size)
333     {
334       if (!grub_errno)
335         grub_error (GRUB_ERR_BAD_OS, N_("premature end of file %s"),
336                     filename);
337       return grub_errno;
338     }
339
340   if (size > LINUX_ZIMAGE_OFFSET + 4
341       && *(grub_uint32_t *) (linux_addr + LINUX_ZIMAGE_OFFSET)
342       == LINUX_ZIMAGE_MAGIC)
343     ;
344   else if (size > 0x8000 && *(grub_uint32_t *) (linux_addr) == 0xea000006
345            && machine_type == GRUB_ARM_MACHINE_TYPE_RASPBERRY_PI)
346     grub_memmove ((void *) linux_addr, (void *) (linux_addr + 0x8000),
347                   size - 0x8000);
348   else
349     return grub_error (GRUB_ERR_BAD_FILE_TYPE, N_("invalid zImage"));
350
351   linux_size = size;
352
353   return GRUB_ERR_NONE;
354 }
355
356 static grub_err_t
357 linux_unload (void)
358 {
359   grub_dl_unref (my_mod);
360
361   grub_free (linux_args);
362   linux_args = NULL;
363
364   initrd_start = initrd_end = 0;
365
366   return GRUB_ERR_NONE;
367 }
368
369 static grub_err_t
370 grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
371                 int argc, char *argv[])
372 {
373   int size;
374   grub_err_t err;
375   grub_file_t file;
376   grub_dl_ref (my_mod);
377
378   if (argc == 0)
379     return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("filename expected"));
380
381   file = grub_file_open (argv[0]);
382   if (!file)
383     goto fail;
384
385   err = linux_load (argv[0], file);
386   grub_file_close (file);
387   if (err)
388     goto fail;
389
390   grub_loader_set (linux_boot, linux_unload, 0);
391
392   size = grub_loader_cmdline_size (argc, argv);
393   linux_args = grub_malloc (size + sizeof (LINUX_IMAGE));
394   if (!linux_args)
395     {
396       grub_loader_unset();
397       goto fail;
398     }
399
400   /* Create kernel command line.  */
401   grub_memcpy (linux_args, LINUX_IMAGE, sizeof (LINUX_IMAGE));
402   grub_create_loader_cmdline (argc, argv,
403                               linux_args + sizeof (LINUX_IMAGE) - 1, size);
404
405   return GRUB_ERR_NONE;
406
407 fail:
408   grub_dl_unref (my_mod);
409   return grub_errno;
410 }
411
412 static grub_err_t
413 grub_cmd_initrd (grub_command_t cmd __attribute__ ((unused)),
414                  int argc, char *argv[])
415 {
416   grub_file_t file;
417   grub_size_t size = 0;
418   struct grub_linux_initrd_context initrd_ctx = { 0, 0, 0 };
419
420   if (argc == 0)
421     return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("filename expected"));
422
423   file = grub_file_open (argv[0]);
424   if (!file)
425     return grub_errno;
426
427   if (grub_initrd_init (argc, argv, &initrd_ctx))
428     goto fail;
429
430   size = grub_get_initrd_size (&initrd_ctx);
431
432 #ifdef GRUB_MACHINE_EFI
433   if (initrd_start)
434     grub_efi_free_pages (initrd_start,
435                          (initrd_end - initrd_start + 0xfff) >> 12);
436   initrd_start = (grub_addr_t) grub_efi_allocate_loader_memory (LINUX_INITRD_PHYS_OFFSET, size);
437
438   if (!initrd_start)
439     {
440       grub_error (GRUB_ERR_OUT_OF_MEMORY, N_("out of memory"));
441       goto fail;
442     }
443 #else
444   initrd_start = LINUX_INITRD_ADDRESS;
445 #endif
446
447   grub_dprintf ("loader", "Loading initrd to 0x%08x\n",
448                 (grub_addr_t) initrd_start);
449
450   if (grub_initrd_load (&initrd_ctx, argv, (void *) initrd_start))
451     goto fail;
452
453   initrd_end = initrd_start + size;
454
455   return GRUB_ERR_NONE;
456
457 fail:
458   grub_file_close (file);
459
460   return grub_errno;
461 }
462
463 static grub_err_t
464 load_dtb (grub_file_t dtb, int size)
465 {
466   void *new_fdt = grub_zalloc (size);
467   if (!new_fdt)
468     return grub_errno;
469   grub_dprintf ("loader", "Loading device tree to %p\n",
470                 new_fdt);
471   if ((grub_file_read (dtb, new_fdt, size) != size)
472       || (grub_fdt_check_header (new_fdt, size) != 0))
473     {
474       grub_free (new_fdt);
475       return grub_error (GRUB_ERR_BAD_OS, N_("invalid device tree"));
476     }
477
478   grub_fdt_set_totalsize (new_fdt, size);
479   current_fdt = new_fdt;
480   /* 
481    * We've successfully loaded an FDT, so any machine type passed
482    * from firmware is now obsolete.
483    */
484   machine_type = GRUB_ARM_MACHINE_TYPE_FDT;
485
486   return GRUB_ERR_NONE;
487 }
488
489 static grub_err_t
490 grub_cmd_devicetree (grub_command_t cmd __attribute__ ((unused)),
491                      int argc, char *argv[])
492 {
493   grub_file_t dtb;
494   int size;
495
496   if (argc != 1)
497     return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("filename expected"));
498
499   dtb = grub_file_open (argv[0]);
500   if (!dtb)
501     return grub_errno;
502
503   size = grub_file_size (dtb);
504   if (size == 0)
505     grub_error (GRUB_ERR_BAD_OS, "empty file");
506   else
507     load_dtb (dtb, size);
508   grub_file_close (dtb);
509
510   return grub_errno;
511 }
512
513 static grub_command_t cmd_linux, cmd_initrd, cmd_devicetree;
514
515 GRUB_MOD_INIT (linux)
516 {
517   cmd_linux = grub_register_command ("linux", grub_cmd_linux,
518                                      0, N_("Load Linux."));
519   cmd_initrd = grub_register_command ("initrd", grub_cmd_initrd,
520                                       0, N_("Load initrd."));
521   cmd_devicetree = grub_register_command ("devicetree", grub_cmd_devicetree,
522                                           /* TRANSLATORS: DTB stands for device tree blob.  */
523                                           0, N_("Load DTB file."));
524   my_mod = mod;
525   current_fdt = (const void *) grub_arm_firmware_get_boot_data ();
526   machine_type = grub_arm_firmware_get_machine_type ();
527 }
528
529 GRUB_MOD_FINI (linux)
530 {
531   grub_unregister_command (cmd_linux);
532   grub_unregister_command (cmd_initrd);
533   grub_unregister_command (cmd_devicetree);
534 }