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