Merge branch 'afs' into befs2
[grub2/phcoder.git] / loader / xnu.c
blobaac4ae372607c384a5288b1f5d4c9f71627ef998
1 /* xnu.c - load xnu kernel. Thanks to Florian Idelberger for all the
2 time he spent testing this
3 */
4 /*
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>
23 #include <grub/xnu.h>
24 #include <grub/cpu/xnu.h>
25 #include <grub/mm.h>
26 #include <grub/dl.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
42 static grub_err_t
43 grub_xnu_register_memory (char *prefix, int *suffix,
44 void *addr, grub_size_t size);
45 void *
46 grub_xnu_heap_malloc (int size)
48 void *val;
50 #if 0
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;
54 #else
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;
61 else
62 oldblknum = 0;
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);
70 else
71 val = grub_xnu_heap_start;
72 if (! val)
74 grub_error (GRUB_ERR_OUT_OF_MEMORY,
75 "not enough space on xnu memory heap");
76 return 0;
78 grub_xnu_heap_start = val;
79 #endif
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);
84 return (char *) 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. */
90 grub_err_t
91 grub_xnu_align_heap (int align)
93 int align_overhead = align - grub_xnu_heap_size % align;
94 if (align_overhead == align)
95 return GRUB_ERR_NONE;
96 if (! grub_xnu_heap_malloc (align_overhead))
97 return grub_errno;
98 return GRUB_ERR_NONE;
101 /* Free subtree pointed by CUR. */
102 void
103 grub_xnu_free_devtree (struct grub_xnu_devtree_key *cur)
105 struct grub_xnu_devtree_key *d;
106 while (cur)
108 grub_free (cur->name);
109 if (cur->datasize == -1)
110 grub_xnu_free_devtree (cur->first_child);
111 else if (cur->data)
112 grub_free (cur->data);
113 d = cur->next;
114 grub_free (cur);
115 cur = d;
119 /* Compute the size of device tree in xnu format. */
120 static grub_size_t
121 grub_xnu_writetree_get_size (struct grub_xnu_devtree_key *start, char *name)
123 grub_size_t ret;
124 struct grub_xnu_devtree_key *cur;
126 /* Key header. */
127 ret = 2 * sizeof (grub_uint32_t);
129 /* "name" value. */
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)
137 int align_overhead;
139 align_overhead = 4 - (cur->datasize % 4);
140 if (align_overhead == 4)
141 align_overhead = 0;
142 ret += 32 + sizeof (grub_uint32_t) + cur->datasize + align_overhead;
144 else
145 ret += grub_xnu_writetree_get_size (cur->first_child, cur->name);
146 return ret;
149 /* Write devtree in XNU format at curptr assuming the head is named NAME.*/
150 static void *
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)
159 nkeys++;
160 else
161 nvals++;
163 /* For the name. */
164 nvals++;
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)
186 int align_overhead;
188 align_overhead = 4 - (cur->datasize % 4);
189 if (align_overhead == 4)
190 align_overhead = 0;
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,
206 cur->first_child,
207 cur->name)))
208 return 0;
209 return curptr;
212 grub_err_t
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;
219 grub_err_t err;
221 err = grub_xnu_align_heap (GRUB_XNU_PAGESIZE);
222 if (err)
223 return err;
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");
228 if (! chosen)
229 return grub_errno;
230 memorymap = grub_xnu_create_key (&(chosen->first_child), "memory-map");
231 if (! memorymap)
232 return grub_errno;
234 driverkey = (struct grub_xnu_devtree_key *) grub_malloc (sizeof (*driverkey));
235 if (! 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)
270 return cur;
271 return 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);
279 if (ret)
280 return ret;
281 ret = (struct grub_xnu_devtree_key *) grub_zalloc (sizeof (*ret));
282 if (! ret)
284 grub_error (GRUB_ERR_OUT_OF_MEMORY, "can't create key %s", name);
285 return 0;
287 ret->name = grub_strdup (name);
288 if (! ret->name)
290 grub_free (ret);
291 grub_error (GRUB_ERR_OUT_OF_MEMORY, "can't create key %s", name);
292 return 0;
294 ret->datasize = -1;
295 ret->next = *parent;
296 *parent = ret;
297 return ret;
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);
305 if (ret)
307 if (ret->datasize == -1)
308 grub_xnu_free_devtree (ret->first_child);
309 else if (ret->datasize)
310 grub_free (ret->data);
311 ret->datasize = 0;
312 ret->data = 0;
313 return ret;
315 ret = (struct grub_xnu_devtree_key *) grub_zalloc (sizeof (*ret));
316 if (! ret)
318 grub_error (GRUB_ERR_OUT_OF_MEMORY, "can't create value %s", name);
319 return 0;
321 ret->name = grub_strdup (name);
322 if (! ret->name)
324 grub_free (ret);
325 grub_error (GRUB_ERR_OUT_OF_MEMORY, "can't create value %s", name);
326 return 0;
328 ret->next = *parent;
329 *parent = ret;
330 return ret;
333 static grub_err_t
334 grub_xnu_unload (void)
336 grub_xnu_free_devtree (grub_xnu_devtree_root);
337 grub_xnu_devtree_root = 0;
339 /* Free loaded image. */
340 driversnum = 0;
341 driverspackagenum = 0;
342 grub_free (grub_xnu_heap_start);
343 grub_xnu_heap_start = 0;
344 grub_xnu_heap_size = 0;
345 grub_xnu_unlock ();
346 return GRUB_ERR_NONE;
349 static grub_err_t
350 grub_cmd_xnu_kernel (grub_command_t cmd __attribute__ ((unused)),
351 int argc, char *args[])
353 grub_err_t err;
354 grub_macho_t macho;
355 grub_addr_t startcode, endcode;
356 int i;
357 char *ptr, *loadaddr;
359 if (argc < 1)
360 return grub_error (GRUB_ERR_BAD_ARGUMENT, "file name required");
362 grub_xnu_unload ();
364 macho = grub_macho_open (args[0]);
365 if (! macho)
366 return grub_errno;
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);
375 if (err)
377 grub_macho_close (macho);
378 grub_xnu_unload ();
379 return err;
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;
388 if (! loadaddr)
390 grub_macho_close (macho);
391 grub_xnu_unload ();
392 return grub_error (GRUB_ERR_OUT_OF_MEMORY,
393 "not enough memory to load kernel");
396 /* Load kernel. */
397 err = grub_macho32_load (macho, loadaddr - startcode, GRUB_MACHO_NOBSS);
398 if (err)
400 grub_macho_close (macho);
401 grub_xnu_unload ();
402 return err;
405 grub_xnu_entry_point = grub_macho32_get_entry_point (macho);
406 if (! grub_xnu_entry_point)
408 grub_macho_close (macho);
409 grub_xnu_unload ();
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);
416 if (err)
418 grub_xnu_unload ();
419 return err;
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))
428 break;
429 grub_memcpy (ptr, args[i], grub_strlen (args[i]));
430 ptr += grub_strlen (args[i]);
431 *ptr = ' ';
432 ptr++;
435 /* Replace last space by '\0'. */
436 if (ptr != grub_xnu_cmdline)
437 *(ptr - 1) = 0;
439 err = grub_cpu_xnu_fill_devicetree ();
440 if (err)
441 return err;
443 grub_loader_set (grub_xnu_boot, grub_xnu_unload, 0);
445 grub_xnu_lock ();
446 return 0;
449 /* Register a memory in a memory map under name PREFIXSUFFIX
450 and increment SUFFIX. */
451 static grub_err_t
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");
464 if (! chosen)
465 return grub_errno;
466 memorymap = grub_xnu_create_key (&(chosen->first_child), "memory-map");
467 if (! memorymap)
468 return grub_errno;
470 driverkey = (struct grub_xnu_devtree_key *) grub_malloc (sizeof (*driverkey));
471 if (! driverkey)
472 return grub_error (GRUB_ERR_OUT_OF_MEMORY, "can't register memory");
473 if (suffix)
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)++);
480 else
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;
497 /* Load .kext. */
498 static grub_err_t
499 grub_xnu_load_driver (char *infoplistname, grub_file_t binaryfile)
501 grub_macho_t macho;
502 grub_err_t err;
503 grub_file_t infoplist;
504 struct grub_xnu_extheader *exthead;
505 int neededspace = sizeof (*exthead);
506 char *buf;
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. */
513 if (binaryfile)
515 macho = grub_macho_file (binaryfile);
516 if (! macho || ! grub_macho_contains_macho32 (macho))
518 if (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;
526 else
527 macho = 0;
529 if (infoplistname)
530 infoplist = grub_gzfile_open (infoplistname, 1);
531 else
532 infoplist = 0;
533 grub_errno = GRUB_ERR_NONE;
534 if (infoplist)
536 infoplistsize = grub_file_size (infoplist);
537 neededspace += infoplistsize + 1;
539 else
540 infoplistsize = 0;
542 /* Allocate the space. */
543 err = grub_xnu_align_heap (GRUB_XNU_PAGESIZE);
544 if (err)
545 return err;
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. */
553 if (macho)
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);
561 return err;
563 grub_macho_close (macho);
564 buf += machosize;
566 grub_errno = GRUB_ERR_NONE;
568 /* Load the plist. */
569 if (infoplist)
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);
578 grub_error_push ();
579 return grub_error (GRUB_ERR_BAD_OS, "Couldn't read file %s: ",
580 infoplistname);
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,
589 neededspace);
592 /* Load mkext. */
593 static grub_err_t
594 grub_cmd_xnu_mkext (grub_command_t cmd __attribute__ ((unused)),
595 int argc, char *args[])
597 grub_file_t file;
598 void *loadto;
599 grub_err_t err;
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;
604 int narchs, i;
606 if (argc != 1)
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);
613 if (! file)
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);
624 grub_error_push ();
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);
633 if (! archs)
635 grub_file_close (file);
636 grub_error_push ();
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)
645 grub_free (archs);
646 grub_error_push ();
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);
658 grub_free (archs);
660 else
662 /* It's a flat file. Some sane people still exist. */
663 readoff = 0;
664 readlen = grub_file_size (file);
667 if (readlen == -1)
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);
675 if (err)
677 grub_file_close (file);
678 return err;
681 loadto = grub_xnu_heap_malloc (readlen);
682 if (! loadto)
684 grub_file_close (file);
685 return grub_errno;
688 /* Read the file. */
689 grub_file_seek (file, readoff);
690 if (grub_file_read (file, loadto, readlen) != (grub_ssize_t) (readlen))
692 grub_file_close (file);
693 grub_error_push ();
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,
700 loadto, readlen);
703 static grub_err_t
704 grub_cmd_xnu_ramdisk (grub_command_t cmd __attribute__ ((unused)),
705 int argc, char *args[])
707 grub_file_t file;
708 void *loadto;
709 grub_err_t err;
710 grub_size_t size;
712 if (argc != 1)
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);
719 if (! file)
720 return grub_error (GRUB_ERR_FILE_NOT_FOUND,
721 "Couldn't load ramdisk");
723 err = grub_xnu_align_heap (GRUB_XNU_PAGESIZE);
724 if (err)
725 return err;
727 size = grub_file_size (file);
729 loadto = grub_xnu_heap_malloc (size);
730 if (! loadto)
731 return grub_errno;
732 if (grub_file_read (file, loadto, size)
733 != (grub_ssize_t) (size))
735 grub_file_close (file);
736 grub_error_push ();
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:
743 valuename:valuedata;
744 keyname{
745 contents
747 keyname, valuename and valuedata are in hex.
749 static char *
750 grub_xnu_parse_devtree (struct grub_xnu_devtree_key **parent,
751 char *start, char *end)
753 char *ptr, *ptr2;
754 char *name, *data;
755 int namelen, datalen, i;
756 for (ptr = start; ptr && ptr < end; )
758 if (grub_isspace (*ptr))
760 ptr++;
761 continue;
763 if (*ptr == '}')
764 return ptr + 1;
765 namelen = 0;
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'));
772 ptr2++)
773 if (! grub_isspace (*ptr2))
774 namelen++;
775 if (ptr2 == end)
776 return 0;
777 namelen /= 2;
778 name = grub_malloc (namelen + 1);
779 if (!name)
780 return 0;
781 for (i = 0; i < 2 * namelen; i++)
783 int hex = 0;
784 while (grub_isspace (*ptr))
785 ptr++;
786 if (*ptr >= '0' && *ptr <= '9')
787 hex = *ptr - '0';
788 if (*ptr >= 'a' && *ptr <= 'f')
789 hex = *ptr - 'a' + 10;
790 if (*ptr >= 'A' && *ptr <= 'F')
791 hex = *ptr - 'A' + 10;
793 if (i % 2 == 0)
794 name[i / 2] = hex << 4;
795 else
796 name[i / 2] |= hex;
797 ptr++;
799 name [namelen] = 0;
800 while (grub_isspace (*ptr))
801 ptr++;
803 /* If it describes a key recursively invoke the function. */
804 if (*ptr == '{')
806 struct grub_xnu_devtree_key *newkey
807 = grub_xnu_create_key (parent, name);
808 grub_free (name);
809 if (! newkey)
810 return 0;
811 ptr = grub_xnu_parse_devtree (&(newkey->first_child), ptr + 1, end);
812 continue;
815 /* Parse the data. */
816 if (*ptr != ':')
817 return 0;
818 ptr++;
819 datalen = 0;
820 for (ptr2 = ptr; ptr2 < end && (grub_isspace (*ptr2)
821 || (*ptr2 >= '0' && *ptr2 <= '9')
822 || (*ptr2 >= 'a' && *ptr2 <= 'f')
823 || (*ptr2 >= 'A' && *ptr2 <= 'F'));
824 ptr2++)
825 if (! grub_isspace (*ptr2))
826 datalen++;
827 if (ptr2 == end)
828 return 0;
829 datalen /= 2;
830 data = grub_malloc (datalen);
831 if (! data)
832 return 0;
833 for (i = 0; i < 2 * datalen; i++)
835 int hex = 0;
836 while (grub_isspace (*ptr))
837 ptr++;
838 if (*ptr >= '0' && *ptr <= '9')
839 hex = *ptr - '0';
840 if (*ptr >= 'a' && *ptr <= 'f')
841 hex = *ptr - 'a' + 10;
842 if (*ptr >= 'A' && *ptr <= 'F')
843 hex = *ptr - 'A' + 10;
845 if (i % 2 == 0)
846 data[i / 2] = hex << 4;
847 else
848 data[i / 2] |= hex;
849 ptr++;
851 while (ptr < end && grub_isspace (*ptr))
852 ptr++;
854 struct grub_xnu_devtree_key *newkey
855 = grub_xnu_create_value (parent, name);
856 grub_free (name);
857 if (! newkey)
858 return 0;
859 newkey->datasize = datalen;
860 newkey->data = data;
862 if (*ptr != ';')
863 return 0;
864 ptr++;
866 if (ptr >= end && *parent != grub_xnu_devtree_root)
867 return 0;
868 return ptr;
871 /* Returns true if the kext should be loaded according to plist
872 and osbundlereq. Also fill BINNAME. */
873 static int
874 grub_xnu_check_os_bundle_required (char *plistname, char *osbundlereq,
875 char **binname)
877 grub_file_t file;
878 char *buf = 0, *tagstart = 0, *ptr1 = 0, *keyptr = 0;
879 char *stringptr = 0, *ptr2 = 0;
880 grub_size_t size;
881 int depth = 0;
882 int ret;
883 int osbundlekeyfound = 0, binnamekeyfound = 0;
884 if (binname)
885 *binname = 0;
887 file = grub_gzfile_open (plistname, 1);
888 if (! file)
890 grub_file_close (file);
891 grub_error_push ();
892 grub_error (GRUB_ERR_BAD_OS, "Couldn't read file %s", plistname);
893 return 0;
896 size = grub_file_size (file);
897 buf = grub_malloc (size);
898 if (! buf)
900 grub_file_close (file);
901 grub_error_push ();
902 grub_error (GRUB_ERR_OUT_OF_MEMORY, "Couldn't read file %s", plistname);
903 return 0;
905 if (grub_file_read (file, buf, size) != (grub_ssize_t) (size))
907 grub_file_close (file);
908 grub_error_push ();
909 grub_error (GRUB_ERR_BAD_OS, "Couldn't read file %s", plistname);
910 return 0;
912 grub_file_close (file);
914 /* Set the return value for the case when no OSBundleRequired tag is found. */
915 if (osbundlereq)
916 ret = grub_strword (osbundlereq, "all") || grub_strword (osbundlereq, "-");
917 else
918 ret = 1;
920 /* Parse plist. It's quite dirty and inextensible but does its job. */
921 for (ptr1 = buf; ptr1 < buf + size; ptr1++)
922 switch (*ptr1)
924 case '<':
925 tagstart = ptr1;
926 *ptr1 = 0;
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)
932 binnamekeyfound = 1;
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)
942 if (*binname)
943 grub_free (*binname);
944 *binname = grub_strdup (stringptr);
947 *ptr1 = '<';
948 keyptr = 0;
949 stringptr = 0;
950 break;
951 case '>':
952 if (! tagstart)
954 grub_free (buf);
955 grub_error (GRUB_ERR_BAD_OS, "can't parse %s", plistname);
956 return 0;
958 *ptr1 = 0;
959 if (tagstart[1] == '?' || ptr1[-1] == '/')
961 osbundlekeyfound = 0;
962 *ptr1 = '>';
963 break;
965 if (depth == 3 && grub_strcmp (tagstart + 1, "key") == 0)
966 keyptr = ptr1 + 1;
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;
972 binnamekeyfound = 0;
974 *ptr1 = '>';
976 if (tagstart[1] == '/')
977 depth--;
978 else
979 depth++;
980 break;
982 grub_free (buf);
984 return ret;
987 /* Load all loadable kexts placed under DIRNAME and matching OSBUNDLEREQUIRED */
988 grub_err_t
989 grub_xnu_scan_dir_for_kexts (char *dirname, char *osbundlerequired,
990 int maxrecursion)
992 grub_device_t dev;
993 char *device_name;
994 grub_fs_t fs;
995 const char *path;
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)
1001 char *newdirname;
1002 if (! info->dir)
1003 return 0;
1004 if (filename[0] == '.')
1005 return 0;
1007 if (grub_strlen (filename) < 5 ||
1008 grub_memcmp (filename + grub_strlen (filename) - 5, ".kext", 5) != 0)
1009 return 0;
1011 newdirname
1012 = grub_malloc (grub_strlen (dirname) + grub_strlen (filename) + 2);
1014 /* It's a .kext. Try to load it. */
1015 if (newdirname)
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,
1022 maxrecursion);
1023 if (grub_errno == GRUB_ERR_BAD_OS)
1024 grub_errno = GRUB_ERR_NONE;
1025 grub_free (newdirname);
1027 return 0;
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);
1035 if (dev)
1037 fs = grub_fs_probe (dev);
1038 path = grub_strchr (dirname, ')');
1039 if (! path)
1040 path = dirname;
1041 else
1042 path++;
1044 if (fs)
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) */
1054 grub_err_t
1055 grub_xnu_load_kext_from_dir (char *dirname, char *osbundlerequired,
1056 int maxrecursion)
1058 grub_device_t dev;
1059 char *plistname = 0;
1060 char *newdirname;
1061 char *newpath;
1062 char *device_name;
1063 grub_fs_t fs;
1064 const char *path;
1065 char *binsuffix;
1066 int usemacos = 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)
1075 return 0;
1076 grub_strcpy (newdirname + grub_strlen (dirname) + 1, filename);
1078 /* If the kext contains directory "Contents" all real stuff is in
1079 this directory. */
1080 if (info->dir && grub_strcasecmp (filename, "Contents") == 0)
1081 grub_xnu_load_kext_from_dir (newdirname, osbundlerequired,
1082 maxrecursion - 1);
1084 /* Directory "Plugins" contains nested kexts. */
1085 if (info->dir && grub_strcasecmp (filename, "Plugins") == 0)
1086 grub_xnu_scan_dir_for_kexts (newdirname, osbundlerequired,
1087 maxrecursion - 1);
1089 /* Directory "MacOS" contains executable, otherwise executable is
1090 on the top. */
1091 if (info->dir && grub_strcasecmp (filename, "MacOS") == 0)
1092 usemacos = 1;
1094 /* Info.plist is the file which governs our future actions. */
1095 if (! info->dir && grub_strcasecmp (filename, "Info.plist") == 0
1096 && ! plistname)
1097 plistname = grub_strdup (newdirname);
1098 return 0;
1101 newdirname = grub_malloc (grub_strlen (dirname) + 20);
1102 if (! newdirname)
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);
1109 if (dev)
1111 fs = grub_fs_probe (dev);
1112 path = grub_strchr (dirname, ')');
1113 if (! path)
1114 path = dirname;
1115 else
1116 path++;
1118 newpath = grub_strchr (newdirname, ')');
1119 if (! newpath)
1120 newpath = newdirname;
1121 else
1122 newpath++;
1124 /* Look at the directory. */
1125 if (fs)
1126 (fs->dir) (dev, path, load_hook);
1128 if (plistname && grub_xnu_check_os_bundle_required
1129 (plistname, osbundlerequired, &binsuffix))
1131 if (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);
1138 if (usemacos)
1139 grub_strcpy (binname + grub_strlen (binname), "/MacOS/");
1140 else
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);
1145 if (! binfile)
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);
1153 else
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. */
1168 static grub_err_t
1169 grub_cmd_xnu_devtree (grub_command_t cmd __attribute__ ((unused)),
1170 int argc, char *args[])
1172 grub_file_t file;
1173 char *data, *endret;
1174 grub_size_t datalen;
1176 if (argc != 1)
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);
1184 if (! file)
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);
1188 if (! data)
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);
1197 grub_free (data);
1198 grub_error_push ();
1199 return grub_error (GRUB_ERR_BAD_OS, "Couldn't read file %s", args[0]);
1201 grub_file_close (file);
1202 data[datalen] = 0;
1204 /* Parse the file. */
1205 endret = grub_xnu_parse_devtree (&grub_xnu_devtree_root,
1206 data, data + datalen);
1207 grub_free (data);
1209 if (! endret)
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. */
1219 static grub_err_t
1220 grub_cmd_xnu_kext (grub_command_t cmd __attribute__ ((unused)),
1221 int argc, char *args[])
1223 grub_file_t binfile = 0;
1224 if (argc == 2)
1226 /* User explicitly specified plist and binary. */
1227 if (grub_strcmp (args[1], "-") != 0)
1229 binfile = grub_gzfile_open (args[1], 1);
1230 if (! binfile)
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,
1237 binfile);
1240 /* load kext normally. */
1241 if (argc == 1)
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. */
1248 static grub_err_t
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");
1255 if (argc == 1)
1256 return grub_xnu_scan_dir_for_kexts (args[0],
1257 "console,root,local-root,network-root",
1258 10);
1259 else
1261 char *osbundlerequired = grub_strdup (args[1]), *ptr;
1262 grub_err_t err;
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);
1270 return err;
1274 struct grub_video_bitmap *grub_xnu_bitmap = 0;
1276 static grub_err_t
1277 grub_cmd_xnu_splash (grub_command_t cmd __attribute__ ((unused)),
1278 int argc, char *args[])
1280 grub_err_t err;
1281 if (argc != 1)
1282 return grub_error (GRUB_ERR_BAD_ARGUMENT, "file name required");
1284 err = grub_video_bitmap_load (&grub_xnu_bitmap, args[0]);
1285 if (err)
1286 grub_xnu_bitmap = 0;
1287 return err;
1291 #ifndef GRUB_UTIL
1292 static grub_err_t
1293 grub_cmd_xnu_resume (grub_command_t cmd __attribute__ ((unused)),
1294 int argc, char *args[])
1296 if (argc != 1)
1297 return grub_error (GRUB_ERR_BAD_ARGUMENT, "file name required");
1299 return grub_xnu_resume (args[0]);
1301 #endif
1303 void
1304 grub_xnu_lock ()
1306 if (!locked)
1307 grub_dl_ref (my_mod);
1308 locked = 1;
1311 void
1312 grub_xnu_unlock ()
1314 if (locked)
1315 grub_dl_unref (my_mod);
1316 locked = 0;
1319 static grub_command_t cmd_kernel, cmd_mkext, cmd_kext, cmd_kextdir,
1320 cmd_ramdisk, cmd_devtree, cmd_resume, cmd_splash;
1322 GRUB_MOD_INIT(xnu)
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");
1341 #ifndef GRUB_UTIL
1342 cmd_resume = grub_register_command ("xnu_resume", grub_cmd_xnu_resume,
1343 0, "Load XNU hibernate image.");
1344 #endif
1345 my_mod=mod;
1348 GRUB_MOD_FINI(xnu)
1350 #ifndef GRUB_UTIL
1351 grub_unregister_command (cmd_resume);
1352 #endif
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);