1 /* xnu.c - load xnu kernel. Thanks to Florian Idelberger for all the
2 time he spent testing this
5 * GRUB -- GRand Unified Bootloader
6 * Copyright (C) 2009 Free Software Foundation, Inc.
8 * GRUB is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
13 * GRUB is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
22 #include <grub/file.h>
24 #include <grub/cpu/xnu.h>
27 #include <grub/loader.h>
28 #include <grub/machoload.h>
29 #include <grub/macho.h>
30 #include <grub/cpu/macho.h>
31 #include <grub/gzio.h>
32 #include <grub/command.h>
33 #include <grub/misc.h>
35 struct grub_xnu_devtree_key
*grub_xnu_devtree_root
= 0;
36 static int driverspackagenum
= 0;
37 static int driversnum
= 0;
39 /* Allocate heap by 32MB-blocks. */
40 #define GRUB_XNU_HEAP_ALLOC_BLOCK 0x2000000
43 grub_xnu_register_memory (char *prefix
, int *suffix
,
44 void *addr
, grub_size_t size
);
46 grub_xnu_heap_malloc (int size
)
51 /* This way booting is faster but less reliable.
52 Once we have advanced mm second way will be as fast as this one. */
53 val
= grub_xnu_heap_start
= (char *) 0x100000;
55 int oldblknum
, newblknum
;
57 /* The page after the heap is used for stack. Ensure it's usable. */
58 if (grub_xnu_heap_size
)
59 oldblknum
= (grub_xnu_heap_size
+ GRUB_XNU_PAGESIZE
60 + GRUB_XNU_HEAP_ALLOC_BLOCK
- 1) / GRUB_XNU_HEAP_ALLOC_BLOCK
;
63 newblknum
= (grub_xnu_heap_size
+ size
+ GRUB_XNU_PAGESIZE
64 + GRUB_XNU_HEAP_ALLOC_BLOCK
- 1) / GRUB_XNU_HEAP_ALLOC_BLOCK
;
65 if (oldblknum
!= newblknum
)
66 /* FIXME: instruct realloc to allocate at 1MB if possible once
67 advanced mm is ready. */
68 val
= grub_realloc (grub_xnu_heap_start
,
69 newblknum
* GRUB_XNU_HEAP_ALLOC_BLOCK
);
71 val
= grub_xnu_heap_start
;
74 grub_error (GRUB_ERR_OUT_OF_MEMORY
,
75 "not enough space on xnu memory heap");
78 grub_xnu_heap_start
= val
;
81 val
= (char *) grub_xnu_heap_start
+ grub_xnu_heap_size
;
82 grub_xnu_heap_size
+= size
;
83 grub_dprintf ("xnu", "val=%p\n", val
);
87 /* Make sure next block of the heap will be aligned.
88 Please notice: aligned are pointers AFTER relocation
89 and not the current ones. */
91 grub_xnu_align_heap (int align
)
93 int align_overhead
= align
- grub_xnu_heap_size
% align
;
94 if (align_overhead
== align
)
96 if (! grub_xnu_heap_malloc (align_overhead
))
101 /* Free subtree pointed by CUR. */
103 grub_xnu_free_devtree (struct grub_xnu_devtree_key
*cur
)
105 struct grub_xnu_devtree_key
*d
;
108 grub_free (cur
->name
);
109 if (cur
->datasize
== -1)
110 grub_xnu_free_devtree (cur
->first_child
);
112 grub_free (cur
->data
);
119 /* Compute the size of device tree in xnu format. */
121 grub_xnu_writetree_get_size (struct grub_xnu_devtree_key
*start
, char *name
)
124 struct grub_xnu_devtree_key
*cur
;
127 ret
= 2 * sizeof (grub_uint32_t
);
130 ret
+= 32 + sizeof (grub_uint32_t
)
131 + grub_strlen (name
) + 4
132 - (grub_strlen (name
) % 4);
134 for (cur
= start
; cur
; cur
= cur
->next
)
135 if (cur
->datasize
!= -1)
139 align_overhead
= 4 - (cur
->datasize
% 4);
140 if (align_overhead
== 4)
142 ret
+= 32 + sizeof (grub_uint32_t
) + cur
->datasize
+ align_overhead
;
145 ret
+= grub_xnu_writetree_get_size (cur
->first_child
, cur
->name
);
149 /* Write devtree in XNU format at curptr assuming the head is named NAME.*/
151 grub_xnu_writetree_toheap_real (void *curptr
,
152 struct grub_xnu_devtree_key
*start
, char *name
)
154 struct grub_xnu_devtree_key
*cur
;
155 int nkeys
= 0, nvals
= 0;
156 for (cur
= start
; cur
; cur
= cur
->next
)
158 if (cur
->datasize
== -1)
166 *((grub_uint32_t
*) curptr
) = nvals
;
167 curptr
= ((grub_uint32_t
*) curptr
) + 1;
168 *((grub_uint32_t
*) curptr
) = nkeys
;
169 curptr
= ((grub_uint32_t
*) curptr
) + 1;
171 /* First comes "name" value. */
172 grub_memset (curptr
, 0, 32);
173 grub_memcpy (curptr
, "name", 4);
174 curptr
= ((grub_uint8_t
*) curptr
) + 32;
175 *((grub_uint32_t
*)curptr
) = grub_strlen (name
) + 1;
176 curptr
= ((grub_uint32_t
*) curptr
) + 1;
177 grub_memcpy (curptr
, name
, grub_strlen (name
));
178 curptr
= ((grub_uint8_t
*) curptr
) + grub_strlen (name
);
179 grub_memset (curptr
, 0, 4 - (grub_strlen (name
) % 4));
180 curptr
= ((grub_uint8_t
*) curptr
) + (4 - (grub_strlen (name
) % 4));
182 /* Then the other values. */
183 for (cur
= start
; cur
; cur
= cur
->next
)
184 if (cur
->datasize
!= -1)
188 align_overhead
= 4 - (cur
->datasize
% 4);
189 if (align_overhead
== 4)
191 grub_memset (curptr
, 0, 32);
192 grub_strncpy (curptr
, cur
->name
, 31);
193 curptr
= ((grub_uint8_t
*) curptr
) + 32;
194 *((grub_uint32_t
*) curptr
) = cur
->datasize
;
195 curptr
= ((grub_uint32_t
*) curptr
) + 1;
196 grub_memcpy (curptr
, cur
->data
, cur
->datasize
);
197 curptr
= ((grub_uint8_t
*) curptr
) + cur
->datasize
;
198 grub_memset (curptr
, 0, align_overhead
);
199 curptr
= ((grub_uint8_t
*) curptr
) + align_overhead
;
202 /* And then the keys. Recursively use this function. */
203 for (cur
= start
; cur
; cur
= cur
->next
)
204 if (cur
->datasize
== -1)
205 if (!(curptr
= grub_xnu_writetree_toheap_real (curptr
,
213 grub_xnu_writetree_toheap (void **start
, grub_size_t
*size
)
215 struct grub_xnu_devtree_key
*chosen
;
216 struct grub_xnu_devtree_key
*memorymap
;
217 struct grub_xnu_devtree_key
*driverkey
;
218 struct grub_xnu_extdesc
*extdesc
;
221 err
= grub_xnu_align_heap (GRUB_XNU_PAGESIZE
);
225 /* Device tree itself is in the memory map of device tree. */
226 /* Create a dummy value in memory-map. */
227 chosen
= grub_xnu_create_key (&grub_xnu_devtree_root
, "chosen");
230 memorymap
= grub_xnu_create_key (&(chosen
->first_child
), "memory-map");
234 driverkey
= (struct grub_xnu_devtree_key
*) grub_malloc (sizeof (*driverkey
));
236 return grub_error (GRUB_ERR_OUT_OF_MEMORY
, "can't write device tree");
237 driverkey
->name
= grub_strdup ("DeviceTree");
238 if (! driverkey
->name
)
239 return grub_error (GRUB_ERR_OUT_OF_MEMORY
, "can't write device tree");
240 driverkey
->datasize
= sizeof (*extdesc
);
241 driverkey
->next
= memorymap
->first_child
;
242 memorymap
->first_child
= driverkey
;
243 driverkey
->data
= extdesc
244 = (struct grub_xnu_extdesc
*) grub_malloc (sizeof (*extdesc
));
245 if (! driverkey
->data
)
246 return grub_error (GRUB_ERR_OUT_OF_MEMORY
, "can't write device tree");
248 /* Allocate the space based on the size with dummy value. */
249 *size
= grub_xnu_writetree_get_size (grub_xnu_devtree_root
, "/");
250 *start
= grub_xnu_heap_malloc (*size
+ GRUB_XNU_PAGESIZE
251 - *size
% GRUB_XNU_PAGESIZE
);
253 /* Put real data in the dummy. */
254 extdesc
->addr
= (char *) *start
- grub_xnu_heap_start
255 + grub_xnu_heap_will_be_at
;
256 extdesc
->size
= (grub_uint32_t
) *size
;
258 /* Write the tree to heap. */
259 grub_xnu_writetree_toheap_real (*start
, grub_xnu_devtree_root
, "/");
260 return GRUB_ERR_NONE
;
263 /* Find a key or value in parent key. */
264 struct grub_xnu_devtree_key
*
265 grub_xnu_find_key (struct grub_xnu_devtree_key
*parent
, char *name
)
267 struct grub_xnu_devtree_key
*cur
;
268 for (cur
= parent
; cur
; cur
= cur
->next
)
269 if (grub_strcmp (cur
->name
, name
) == 0)
274 struct grub_xnu_devtree_key
*
275 grub_xnu_create_key (struct grub_xnu_devtree_key
**parent
, char *name
)
277 struct grub_xnu_devtree_key
*ret
;
278 ret
= grub_xnu_find_key (*parent
, name
);
281 ret
= (struct grub_xnu_devtree_key
*) grub_zalloc (sizeof (*ret
));
284 grub_error (GRUB_ERR_OUT_OF_MEMORY
, "can't create key %s", name
);
287 ret
->name
= grub_strdup (name
);
291 grub_error (GRUB_ERR_OUT_OF_MEMORY
, "can't create key %s", name
);
300 struct grub_xnu_devtree_key
*
301 grub_xnu_create_value (struct grub_xnu_devtree_key
**parent
, char *name
)
303 struct grub_xnu_devtree_key
*ret
;
304 ret
= grub_xnu_find_key (*parent
, name
);
307 if (ret
->datasize
== -1)
308 grub_xnu_free_devtree (ret
->first_child
);
309 else if (ret
->datasize
)
310 grub_free (ret
->data
);
315 ret
= (struct grub_xnu_devtree_key
*) grub_zalloc (sizeof (*ret
));
318 grub_error (GRUB_ERR_OUT_OF_MEMORY
, "can't create value %s", name
);
321 ret
->name
= grub_strdup (name
);
325 grub_error (GRUB_ERR_OUT_OF_MEMORY
, "can't create value %s", name
);
334 grub_xnu_unload (void)
336 grub_xnu_free_devtree (grub_xnu_devtree_root
);
337 grub_xnu_devtree_root
= 0;
339 /* Free loaded image. */
341 driverspackagenum
= 0;
342 grub_free (grub_xnu_heap_start
);
343 grub_xnu_heap_start
= 0;
344 grub_xnu_heap_size
= 0;
346 return GRUB_ERR_NONE
;
350 grub_cmd_xnu_kernel (grub_command_t cmd
__attribute__ ((unused
)),
351 int argc
, char *args
[])
355 grub_addr_t startcode
, endcode
;
357 char *ptr
, *loadaddr
;
360 return grub_error (GRUB_ERR_BAD_ARGUMENT
, "file name required");
364 macho
= grub_macho_open (args
[0]);
367 if (! grub_macho_contains_macho32 (macho
))
369 grub_macho_close (macho
);
370 return grub_error (GRUB_ERR_BAD_OS
,
371 "Kernel doesn't contain suitable architecture");
374 err
= grub_macho32_size (macho
, &startcode
, &endcode
, GRUB_MACHO_NOBSS
);
377 grub_macho_close (macho
);
382 grub_dprintf ("xnu", "endcode = %lx, startcode = %lx\n",
383 (unsigned long) endcode
, (unsigned long) startcode
);
385 loadaddr
= grub_xnu_heap_malloc (endcode
- startcode
);
386 grub_xnu_heap_will_be_at
= startcode
;
390 grub_macho_close (macho
);
392 return grub_error (GRUB_ERR_OUT_OF_MEMORY
,
393 "not enough memory to load kernel");
397 err
= grub_macho32_load (macho
, loadaddr
- startcode
, GRUB_MACHO_NOBSS
);
400 grub_macho_close (macho
);
405 grub_xnu_entry_point
= grub_macho32_get_entry_point (macho
);
406 if (! grub_xnu_entry_point
)
408 grub_macho_close (macho
);
410 return grub_error (GRUB_ERR_BAD_OS
, "couldn't find entry point");
413 grub_macho_close (macho
);
415 err
= grub_xnu_align_heap (GRUB_XNU_PAGESIZE
);
422 /* Copy parameters to kernel command line. */
423 ptr
= grub_xnu_cmdline
;
424 for (i
= 1; i
< argc
; i
++)
426 if (ptr
+ grub_strlen (args
[i
]) + 1
427 >= grub_xnu_cmdline
+ sizeof (grub_xnu_cmdline
))
429 grub_memcpy (ptr
, args
[i
], grub_strlen (args
[i
]));
430 ptr
+= grub_strlen (args
[i
]);
435 /* Replace last space by '\0'. */
436 if (ptr
!= grub_xnu_cmdline
)
439 err
= grub_cpu_xnu_fill_devicetree ();
443 grub_loader_set (grub_xnu_boot
, grub_xnu_unload
, 0);
449 /* Register a memory in a memory map under name PREFIXSUFFIX
450 and increment SUFFIX. */
452 grub_xnu_register_memory (char *prefix
, int *suffix
,
453 void *addr
, grub_size_t size
)
455 struct grub_xnu_devtree_key
*chosen
;
456 struct grub_xnu_devtree_key
*memorymap
;
457 struct grub_xnu_devtree_key
*driverkey
;
458 struct grub_xnu_extdesc
*extdesc
;
460 if (! grub_xnu_heap_size
)
461 return grub_error (GRUB_ERR_BAD_OS
, "no xnu kernel loaded");
463 chosen
= grub_xnu_create_key (&grub_xnu_devtree_root
, "chosen");
466 memorymap
= grub_xnu_create_key (&(chosen
->first_child
), "memory-map");
470 driverkey
= (struct grub_xnu_devtree_key
*) grub_malloc (sizeof (*driverkey
));
472 return grub_error (GRUB_ERR_OUT_OF_MEMORY
, "can't register memory");
475 driverkey
->name
= grub_malloc (grub_strlen (prefix
) + 10);
476 if (!driverkey
->name
)
477 return grub_error (GRUB_ERR_OUT_OF_MEMORY
, "can't register memory");
478 grub_sprintf (driverkey
->name
, "%s%d", prefix
, (*suffix
)++);
481 driverkey
->name
= grub_strdup (prefix
);
482 if (! driverkey
->name
)
483 return grub_error (GRUB_ERR_OUT_OF_MEMORY
, "can't register extension");
484 driverkey
->datasize
= sizeof (*extdesc
);
485 driverkey
->next
= memorymap
->first_child
;
486 memorymap
->first_child
= driverkey
;
487 driverkey
->data
= extdesc
488 = (struct grub_xnu_extdesc
*) grub_malloc (sizeof (*extdesc
));
489 if (! driverkey
->data
)
490 return grub_error (GRUB_ERR_OUT_OF_MEMORY
, "can't register extension");
491 extdesc
->addr
= grub_xnu_heap_will_be_at
+
492 ((grub_uint8_t
*) addr
- (grub_uint8_t
*) grub_xnu_heap_start
);
493 extdesc
->size
= (grub_uint32_t
) size
;
494 return GRUB_ERR_NONE
;
499 grub_xnu_load_driver (char *infoplistname
, grub_file_t binaryfile
)
503 grub_file_t infoplist
;
504 struct grub_xnu_extheader
*exthead
;
505 int neededspace
= sizeof (*exthead
);
507 grub_size_t infoplistsize
= 0, machosize
= 0;
509 if (! grub_xnu_heap_size
)
510 return grub_error (GRUB_ERR_BAD_OS
, "no xnu kernel loaded");
512 /* Compute the needed space. */
515 macho
= grub_macho_file (binaryfile
);
516 if (! macho
|| ! grub_macho_contains_macho32 (macho
))
519 grub_macho_close (macho
);
520 return grub_error (GRUB_ERR_BAD_OS
,
521 "Extension doesn't contain suitable architecture");
523 machosize
= grub_macho32_filesize (macho
);
524 neededspace
+= machosize
;
530 infoplist
= grub_gzfile_open (infoplistname
, 1);
533 grub_errno
= GRUB_ERR_NONE
;
536 infoplistsize
= grub_file_size (infoplist
);
537 neededspace
+= infoplistsize
+ 1;
542 /* Allocate the space. */
543 err
= grub_xnu_align_heap (GRUB_XNU_PAGESIZE
);
546 buf
= grub_xnu_heap_malloc (neededspace
);
548 exthead
= (struct grub_xnu_extheader
*) buf
;
549 grub_memset (exthead
, 0, sizeof (*exthead
));
550 buf
+= sizeof (*exthead
);
552 /* Load the binary. */
555 exthead
->binaryaddr
= (buf
- grub_xnu_heap_start
)
556 + grub_xnu_heap_will_be_at
;
557 exthead
->binarysize
= machosize
;
558 if ((err
= grub_macho32_readfile (macho
, buf
)))
560 grub_macho_close (macho
);
563 grub_macho_close (macho
);
566 grub_errno
= GRUB_ERR_NONE
;
568 /* Load the plist. */
571 exthead
->infoplistaddr
= (buf
- grub_xnu_heap_start
)
572 + grub_xnu_heap_will_be_at
;
573 exthead
->infoplistsize
= infoplistsize
+ 1;
574 if (grub_file_read (infoplist
, buf
, infoplistsize
)
575 != (grub_ssize_t
) (infoplistsize
))
577 grub_file_close (infoplist
);
579 return grub_error (GRUB_ERR_BAD_OS
, "Couldn't read file %s: ",
582 grub_file_close (infoplist
);
583 buf
[infoplistsize
] = 0;
585 grub_errno
= GRUB_ERR_NONE
;
587 /* Announce to kernel */
588 return grub_xnu_register_memory ("Driver-", &driversnum
, exthead
,
594 grub_cmd_xnu_mkext (grub_command_t cmd
__attribute__ ((unused
)),
595 int argc
, char *args
[])
600 grub_off_t readoff
= 0;
601 grub_ssize_t readlen
= -1;
602 struct grub_macho_fat_header head
;
603 struct grub_macho_fat_arch
*archs
;
607 return grub_error (GRUB_ERR_BAD_ARGUMENT
, "file name required");
609 if (! grub_xnu_heap_size
)
610 return grub_error (GRUB_ERR_BAD_OS
, "no xnu kernel loaded");
612 file
= grub_gzfile_open (args
[0], 1);
614 return grub_error (GRUB_ERR_FILE_NOT_FOUND
,
615 "Couldn't load driver package");
617 /* Sometimes caches are fat binary. Errgh. */
618 if (grub_file_read (file
, &head
, sizeof (head
))
619 != (grub_ssize_t
) (sizeof (head
)))
621 /* I don't know the internal structure of package but
622 can hardly imagine a valid package shorter than 20 bytes. */
623 grub_file_close (file
);
625 return grub_error (GRUB_ERR_BAD_OS
, "Couldn't read file %s", args
[0]);
628 /* Find the corresponding architecture. */
629 if (grub_be_to_cpu32 (head
.magic
) == GRUB_MACHO_FAT_MAGIC
)
631 narchs
= grub_be_to_cpu32 (head
.nfat_arch
);
632 archs
= grub_malloc (sizeof (struct grub_macho_fat_arch
) * narchs
);
635 grub_file_close (file
);
637 return grub_error (GRUB_ERR_OUT_OF_MEMORY
,
638 "Couldn't read file %s", args
[0]);
641 if (grub_file_read (file
, archs
,
642 sizeof (struct grub_macho_fat_arch
) * narchs
)
643 != (grub_ssize_t
) sizeof(struct grub_macho_fat_arch
) * narchs
)
647 return grub_error (GRUB_ERR_READ_ERROR
, "Cannot read fat header.");
649 for (i
= 0; i
< narchs
; i
++)
651 if (GRUB_MACHO_CPUTYPE_IS_HOST32
652 (grub_be_to_cpu32 (archs
[i
].cputype
)))
654 readoff
= grub_be_to_cpu32 (archs
[i
].offset
);
655 readlen
= grub_be_to_cpu32 (archs
[i
].size
);
662 /* It's a flat file. Some sane people still exist. */
664 readlen
= grub_file_size (file
);
669 grub_file_close (file
);
670 return grub_error (GRUB_ERR_BAD_OS
, "no suitable architecture is found");
673 /* Allocate space. */
674 err
= grub_xnu_align_heap (GRUB_XNU_PAGESIZE
);
677 grub_file_close (file
);
681 loadto
= grub_xnu_heap_malloc (readlen
);
684 grub_file_close (file
);
689 grub_file_seek (file
, readoff
);
690 if (grub_file_read (file
, loadto
, readlen
) != (grub_ssize_t
) (readlen
))
692 grub_file_close (file
);
694 return grub_error (GRUB_ERR_BAD_OS
, "Couldn't read file %s", args
[0]);
696 grub_file_close (file
);
698 /* Pass it to kernel. */
699 return grub_xnu_register_memory ("DriversPackage-", &driverspackagenum
,
704 grub_cmd_xnu_ramdisk (grub_command_t cmd
__attribute__ ((unused
)),
705 int argc
, char *args
[])
713 return grub_error (GRUB_ERR_BAD_ARGUMENT
, "file name required");
715 if (! grub_xnu_heap_size
)
716 return grub_error (GRUB_ERR_BAD_OS
, "no xnu kernel loaded");
718 file
= grub_gzfile_open (args
[0], 1);
720 return grub_error (GRUB_ERR_FILE_NOT_FOUND
,
721 "Couldn't load ramdisk");
723 err
= grub_xnu_align_heap (GRUB_XNU_PAGESIZE
);
727 size
= grub_file_size (file
);
729 loadto
= grub_xnu_heap_malloc (size
);
732 if (grub_file_read (file
, loadto
, size
)
733 != (grub_ssize_t
) (size
))
735 grub_file_close (file
);
737 return grub_error (GRUB_ERR_BAD_OS
, "Couldn't read file %s", args
[0]);
739 return grub_xnu_register_memory ("RAMDisk", 0, loadto
, size
);
742 /* Parse a devtree file. It uses the following format:
747 keyname, valuename and valuedata are in hex.
750 grub_xnu_parse_devtree (struct grub_xnu_devtree_key
**parent
,
751 char *start
, char *end
)
755 int namelen
, datalen
, i
;
756 for (ptr
= start
; ptr
&& ptr
< end
; )
758 if (grub_isspace (*ptr
))
767 /* Parse the name. */
768 for (ptr2
= ptr
; ptr2
< end
&& (grub_isspace (*ptr2
)
769 || (*ptr2
>= '0' && *ptr2
<= '9')
770 || (*ptr2
>= 'a' && *ptr2
<= 'f')
771 || (*ptr2
>= 'A' && *ptr2
<= 'F'));
773 if (! grub_isspace (*ptr2
))
778 name
= grub_malloc (namelen
+ 1);
781 for (i
= 0; i
< 2 * namelen
; i
++)
784 while (grub_isspace (*ptr
))
786 if (*ptr
>= '0' && *ptr
<= '9')
788 if (*ptr
>= 'a' && *ptr
<= 'f')
789 hex
= *ptr
- 'a' + 10;
790 if (*ptr
>= 'A' && *ptr
<= 'F')
791 hex
= *ptr
- 'A' + 10;
794 name
[i
/ 2] = hex
<< 4;
800 while (grub_isspace (*ptr
))
803 /* If it describes a key recursively invoke the function. */
806 struct grub_xnu_devtree_key
*newkey
807 = grub_xnu_create_key (parent
, name
);
811 ptr
= grub_xnu_parse_devtree (&(newkey
->first_child
), ptr
+ 1, end
);
815 /* Parse the data. */
820 for (ptr2
= ptr
; ptr2
< end
&& (grub_isspace (*ptr2
)
821 || (*ptr2
>= '0' && *ptr2
<= '9')
822 || (*ptr2
>= 'a' && *ptr2
<= 'f')
823 || (*ptr2
>= 'A' && *ptr2
<= 'F'));
825 if (! grub_isspace (*ptr2
))
830 data
= grub_malloc (datalen
);
833 for (i
= 0; i
< 2 * datalen
; i
++)
836 while (grub_isspace (*ptr
))
838 if (*ptr
>= '0' && *ptr
<= '9')
840 if (*ptr
>= 'a' && *ptr
<= 'f')
841 hex
= *ptr
- 'a' + 10;
842 if (*ptr
>= 'A' && *ptr
<= 'F')
843 hex
= *ptr
- 'A' + 10;
846 data
[i
/ 2] = hex
<< 4;
851 while (ptr
< end
&& grub_isspace (*ptr
))
854 struct grub_xnu_devtree_key
*newkey
855 = grub_xnu_create_value (parent
, name
);
859 newkey
->datasize
= datalen
;
866 if (ptr
>= end
&& *parent
!= grub_xnu_devtree_root
)
871 /* Returns true if the kext should be loaded according to plist
872 and osbundlereq. Also fill BINNAME. */
874 grub_xnu_check_os_bundle_required (char *plistname
, char *osbundlereq
,
878 char *buf
= 0, *tagstart
= 0, *ptr1
= 0, *keyptr
= 0;
879 char *stringptr
= 0, *ptr2
= 0;
883 int osbundlekeyfound
= 0, binnamekeyfound
= 0;
887 file
= grub_gzfile_open (plistname
, 1);
890 grub_file_close (file
);
892 grub_error (GRUB_ERR_BAD_OS
, "Couldn't read file %s", plistname
);
896 size
= grub_file_size (file
);
897 buf
= grub_malloc (size
);
900 grub_file_close (file
);
902 grub_error (GRUB_ERR_OUT_OF_MEMORY
, "Couldn't read file %s", plistname
);
905 if (grub_file_read (file
, buf
, size
) != (grub_ssize_t
) (size
))
907 grub_file_close (file
);
909 grub_error (GRUB_ERR_BAD_OS
, "Couldn't read file %s", plistname
);
912 grub_file_close (file
);
914 /* Set the return value for the case when no OSBundleRequired tag is found. */
916 ret
= grub_strword (osbundlereq
, "all") || grub_strword (osbundlereq
, "-");
920 /* Parse plist. It's quite dirty and inextensible but does its job. */
921 for (ptr1
= buf
; ptr1
< buf
+ size
; ptr1
++)
927 if (keyptr
&& depth
== 4
928 && grub_strcmp (keyptr
, "OSBundleRequired") == 0)
929 osbundlekeyfound
= 1;
930 if (keyptr
&& depth
== 4 &&
931 grub_strcmp (keyptr
, "CFBundleExecutable") == 0)
933 if (stringptr
&& osbundlekeyfound
&& osbundlereq
&& depth
== 4)
935 for (ptr2
= stringptr
; *ptr2
; ptr2
++)
936 *ptr2
= grub_tolower (*ptr2
);
937 ret
= grub_strword (osbundlereq
, stringptr
)
938 || grub_strword (osbundlereq
, "all");
940 if (stringptr
&& binnamekeyfound
&& binname
&& depth
== 4)
943 grub_free (*binname
);
944 *binname
= grub_strdup (stringptr
);
955 grub_error (GRUB_ERR_BAD_OS
, "can't parse %s", plistname
);
959 if (tagstart
[1] == '?' || ptr1
[-1] == '/')
961 osbundlekeyfound
= 0;
965 if (depth
== 3 && grub_strcmp (tagstart
+ 1, "key") == 0)
967 if (depth
== 3 && grub_strcmp (tagstart
+ 1, "string") == 0)
968 stringptr
= ptr1
+ 1;
969 else if (grub_strcmp (tagstart
+ 1, "/key") != 0)
971 osbundlekeyfound
= 0;
976 if (tagstart
[1] == '/')
987 /* Load all loadable kexts placed under DIRNAME and matching OSBUNDLEREQUIRED */
989 grub_xnu_scan_dir_for_kexts (char *dirname
, char *osbundlerequired
,
997 auto int load_hook (const char *filename
,
998 const struct grub_dirhook_info
*info
);
999 int load_hook (const char *filename
, const struct grub_dirhook_info
*info
)
1004 if (filename
[0] == '.')
1007 if (grub_strlen (filename
) < 5 ||
1008 grub_memcmp (filename
+ grub_strlen (filename
) - 5, ".kext", 5) != 0)
1012 = grub_malloc (grub_strlen (dirname
) + grub_strlen (filename
) + 2);
1014 /* It's a .kext. Try to load it. */
1017 grub_strcpy (newdirname
, dirname
);
1018 newdirname
[grub_strlen (newdirname
) + 1] = 0;
1019 newdirname
[grub_strlen (newdirname
)] = '/';
1020 grub_strcpy (newdirname
+ grub_strlen (newdirname
), filename
);
1021 grub_xnu_load_kext_from_dir (newdirname
, osbundlerequired
,
1023 if (grub_errno
== GRUB_ERR_BAD_OS
)
1024 grub_errno
= GRUB_ERR_NONE
;
1025 grub_free (newdirname
);
1030 if (! grub_xnu_heap_size
)
1031 return grub_error (GRUB_ERR_BAD_OS
, "no xnu kernel loaded");
1033 device_name
= grub_file_get_device_name (dirname
);
1034 dev
= grub_device_open (device_name
);
1037 fs
= grub_fs_probe (dev
);
1038 path
= grub_strchr (dirname
, ')');
1045 (fs
->dir
) (dev
, path
, load_hook
);
1046 grub_device_close (dev
);
1048 grub_free (device_name
);
1050 return GRUB_ERR_NONE
;
1053 /* Load extension DIRNAME. (extensions are directories in xnu) */
1055 grub_xnu_load_kext_from_dir (char *dirname
, char *osbundlerequired
,
1059 char *plistname
= 0;
1067 grub_file_t binfile
;
1069 auto int load_hook (const char *filename
,
1070 const struct grub_dirhook_info
*info
);
1072 int load_hook (const char *filename
, const struct grub_dirhook_info
*info
)
1074 if (grub_strlen (filename
) > 15)
1076 grub_strcpy (newdirname
+ grub_strlen (dirname
) + 1, filename
);
1078 /* If the kext contains directory "Contents" all real stuff is in
1080 if (info
->dir
&& grub_strcasecmp (filename
, "Contents") == 0)
1081 grub_xnu_load_kext_from_dir (newdirname
, osbundlerequired
,
1084 /* Directory "Plugins" contains nested kexts. */
1085 if (info
->dir
&& grub_strcasecmp (filename
, "Plugins") == 0)
1086 grub_xnu_scan_dir_for_kexts (newdirname
, osbundlerequired
,
1089 /* Directory "MacOS" contains executable, otherwise executable is
1091 if (info
->dir
&& grub_strcasecmp (filename
, "MacOS") == 0)
1094 /* Info.plist is the file which governs our future actions. */
1095 if (! info
->dir
&& grub_strcasecmp (filename
, "Info.plist") == 0
1097 plistname
= grub_strdup (newdirname
);
1101 newdirname
= grub_malloc (grub_strlen (dirname
) + 20);
1103 return grub_error (GRUB_ERR_OUT_OF_MEMORY
, "couldn't allocate buffer");
1104 grub_strcpy (newdirname
, dirname
);
1105 newdirname
[grub_strlen (dirname
)] = '/';
1106 newdirname
[grub_strlen (dirname
) + 1] = 0;
1107 device_name
= grub_file_get_device_name (dirname
);
1108 dev
= grub_device_open (device_name
);
1111 fs
= grub_fs_probe (dev
);
1112 path
= grub_strchr (dirname
, ')');
1118 newpath
= grub_strchr (newdirname
, ')');
1120 newpath
= newdirname
;
1124 /* Look at the directory. */
1126 (fs
->dir
) (dev
, path
, load_hook
);
1128 if (plistname
&& grub_xnu_check_os_bundle_required
1129 (plistname
, osbundlerequired
, &binsuffix
))
1133 /* Open the binary. */
1134 char *binname
= grub_malloc (grub_strlen (dirname
)
1135 + grub_strlen (binsuffix
)
1136 + sizeof ("/MacOS/"));
1137 grub_strcpy (binname
, dirname
);
1139 grub_strcpy (binname
+ grub_strlen (binname
), "/MacOS/");
1141 grub_strcpy (binname
+ grub_strlen (binname
), "/");
1142 grub_strcpy (binname
+ grub_strlen (binname
), binsuffix
);
1143 grub_dprintf ("xnu", "%s:%s\n", plistname
, binname
);
1144 binfile
= grub_gzfile_open (binname
, 1);
1146 grub_errno
= GRUB_ERR_NONE
;
1148 /* Load the extension. */
1149 grub_xnu_load_driver (plistname
, binfile
);
1150 grub_free (binname
);
1151 grub_free (binsuffix
);
1155 grub_dprintf ("xnu", "%s:0\n", plistname
);
1156 grub_xnu_load_driver (plistname
, 0);
1159 grub_free (plistname
);
1160 grub_device_close (dev
);
1162 grub_free (device_name
);
1164 return GRUB_ERR_NONE
;
1167 /* Load devtree file. */
1169 grub_cmd_xnu_devtree (grub_command_t cmd
__attribute__ ((unused
)),
1170 int argc
, char *args
[])
1173 char *data
, *endret
;
1174 grub_size_t datalen
;
1177 return grub_error (GRUB_ERR_BAD_ARGUMENT
, "Filename required");
1179 if (! grub_xnu_heap_size
)
1180 return grub_error (GRUB_ERR_BAD_OS
, "no xnu kernel loaded");
1182 /* Load the file. */
1183 file
= grub_gzfile_open (args
[0], 1);
1185 return grub_error (GRUB_ERR_FILE_NOT_FOUND
, "Couldn't load device tree");
1186 datalen
= grub_file_size (file
);
1187 data
= grub_malloc (datalen
+ 1);
1190 grub_file_close (file
);
1191 return grub_error (GRUB_ERR_OUT_OF_MEMORY
,
1192 "Could load device tree into memory");
1194 if (grub_file_read (file
, data
, datalen
) != (grub_ssize_t
) datalen
)
1196 grub_file_close (file
);
1199 return grub_error (GRUB_ERR_BAD_OS
, "Couldn't read file %s", args
[0]);
1201 grub_file_close (file
);
1204 /* Parse the file. */
1205 endret
= grub_xnu_parse_devtree (&grub_xnu_devtree_root
,
1206 data
, data
+ datalen
);
1210 return grub_error (GRUB_ERR_BAD_OS
, "Couldn't parse devtree");
1212 return GRUB_ERR_NONE
;
1215 static int locked
=0;
1216 static grub_dl_t my_mod
;
1218 /* Load the kext. */
1220 grub_cmd_xnu_kext (grub_command_t cmd
__attribute__ ((unused
)),
1221 int argc
, char *args
[])
1223 grub_file_t binfile
= 0;
1226 /* User explicitly specified plist and binary. */
1227 if (grub_strcmp (args
[1], "-") != 0)
1229 binfile
= grub_gzfile_open (args
[1], 1);
1232 grub_error (GRUB_ERR_BAD_OS
, "can't open file");
1233 return GRUB_ERR_NONE
;
1236 return grub_xnu_load_driver (grub_strcmp (args
[0], "-") ? args
[0] : 0,
1240 /* load kext normally. */
1242 return grub_xnu_load_kext_from_dir (args
[0], 0, 10);
1244 return grub_error (GRUB_ERR_BAD_ARGUMENT
, "file name required");
1247 /* Load a directory containing kexts. */
1249 grub_cmd_xnu_kextdir (grub_command_t cmd
__attribute__ ((unused
)),
1250 int argc
, char *args
[])
1252 if (argc
!= 1 && argc
!= 2)
1253 return grub_error (GRUB_ERR_BAD_ARGUMENT
, "directory name required");
1256 return grub_xnu_scan_dir_for_kexts (args
[0],
1257 "console,root,local-root,network-root",
1261 char *osbundlerequired
= grub_strdup (args
[1]), *ptr
;
1263 if (! osbundlerequired
)
1264 return grub_error (GRUB_ERR_OUT_OF_MEMORY
,
1265 "couldn't allocate string temporary space");
1266 for (ptr
= osbundlerequired
; *ptr
; ptr
++)
1267 *ptr
= grub_tolower (*ptr
);
1268 err
= grub_xnu_scan_dir_for_kexts (args
[0], osbundlerequired
, 10);
1269 grub_free (osbundlerequired
);
1274 struct grub_video_bitmap
*grub_xnu_bitmap
= 0;
1277 grub_cmd_xnu_splash (grub_command_t cmd
__attribute__ ((unused
)),
1278 int argc
, char *args
[])
1282 return grub_error (GRUB_ERR_BAD_ARGUMENT
, "file name required");
1284 err
= grub_video_bitmap_load (&grub_xnu_bitmap
, args
[0]);
1286 grub_xnu_bitmap
= 0;
1293 grub_cmd_xnu_resume (grub_command_t cmd
__attribute__ ((unused
)),
1294 int argc
, char *args
[])
1297 return grub_error (GRUB_ERR_BAD_ARGUMENT
, "file name required");
1299 return grub_xnu_resume (args
[0]);
1307 grub_dl_ref (my_mod
);
1315 grub_dl_unref (my_mod
);
1319 static grub_command_t cmd_kernel
, cmd_mkext
, cmd_kext
, cmd_kextdir
,
1320 cmd_ramdisk
, cmd_devtree
, cmd_resume
, cmd_splash
;
1324 cmd_kernel
= grub_register_command ("xnu_kernel", grub_cmd_xnu_kernel
, 0,
1325 "load a xnu kernel");
1326 cmd_mkext
= grub_register_command ("xnu_mkext", grub_cmd_xnu_mkext
, 0,
1327 "Load XNU extension package.");
1328 cmd_kext
= grub_register_command ("xnu_kext", grub_cmd_xnu_kext
, 0,
1329 "Load XNU extension.");
1330 cmd_kextdir
= grub_register_command ("xnu_kextdir", grub_cmd_xnu_kextdir
,
1331 "xnu_kextdir DIRECTORY [OSBundleRequired]",
1332 "Load XNU extension directory");
1333 cmd_ramdisk
= grub_register_command ("xnu_ramdisk", grub_cmd_xnu_ramdisk
, 0,
1334 "Load XNU ramdisk. "
1335 "It will be seen as md0");
1336 cmd_devtree
= grub_register_command ("xnu_devtree", grub_cmd_xnu_devtree
, 0,
1337 "Load XNU devtree");
1338 cmd_splash
= grub_register_command ("xnu_splash", grub_cmd_xnu_splash
, 0,
1339 "Load a splash image for XNU");
1342 cmd_resume
= grub_register_command ("xnu_resume", grub_cmd_xnu_resume
,
1343 0, "Load XNU hibernate image.");
1351 grub_unregister_command (cmd_resume
);
1353 grub_unregister_command (cmd_mkext
);
1354 grub_unregister_command (cmd_kext
);
1355 grub_unregister_command (cmd_kextdir
);
1356 grub_unregister_command (cmd_devtree
);
1357 grub_unregister_command (cmd_ramdisk
);
1358 grub_unregister_command (cmd_kernel
);
1359 grub_unregister_command (cmd_splash
);