2009-06-17 Pavel Roskin <proski@gnu.org>
[grub2/jjazz.git] / loader / efi / appleloader.c
blob94d501bcfdefb1f3fdd86e7cd82a09a7bd05756e
1 /* appleloader.c - apple legacy boot loader. */
2 /*
3 * GRUB -- GRand Unified Bootloader
4 * Copyright (C) 2008 Free Software Foundation, Inc.
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.
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.
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/>.
20 #include <grub/loader.h>
21 #include <grub/err.h>
22 #include <grub/mm.h>
23 #include <grub/dl.h>
24 #include <grub/misc.h>
25 #include <grub/efi/api.h>
26 #include <grub/efi/efi.h>
27 #include <grub/command.h>
29 static grub_dl_t my_mod;
31 static grub_efi_handle_t image_handle;
32 static grub_efi_char16_t *cmdline;
34 static grub_err_t
35 grub_appleloader_unload (void)
37 grub_efi_boot_services_t *b;
39 b = grub_efi_system_table->boot_services;
40 efi_call_1 (b->unload_image, image_handle);
42 grub_free (cmdline);
43 cmdline = 0;
45 grub_dl_unref (my_mod);
46 return GRUB_ERR_NONE;
49 static grub_err_t
50 grub_appleloader_boot (void)
52 grub_efi_boot_services_t *b;
54 b = grub_efi_system_table->boot_services;
55 efi_call_3 (b->start_image, image_handle, 0, 0);
57 grub_appleloader_unload ();
59 return grub_errno;
62 /* early 2006 Core Duo / Core Solo models */
63 static grub_uint8_t devpath_1[] =
65 0x01, 0x03, 0x18, 0x00, 0x0B, 0x00, 0x00, 0x00,
66 0x00, 0x00, 0xE0, 0xFF, 0x00, 0x00, 0x00, 0x00,
67 0xFF, 0xFF, 0xF9, 0xFF, 0x00, 0x00, 0x00, 0x00,
68 0x04, 0x06, 0x14, 0x00, 0xEB, 0x85, 0x05, 0x2B,
69 0xB8, 0xD8, 0xA9, 0x49, 0x8B, 0x8C, 0xE2, 0x1B,
70 0x01, 0xAE, 0xF2, 0xB7, 0x7F, 0xFF, 0x04, 0x00,
73 /* mid-2006 Mac Pro (and probably other Core 2 models) */
74 static grub_uint8_t devpath_2[] =
76 0x01, 0x03, 0x18, 0x00, 0x0B, 0x00, 0x00, 0x00,
77 0x00, 0x00, 0xE0, 0xFF, 0x00, 0x00, 0x00, 0x00,
78 0xFF, 0xFF, 0xF7, 0xFF, 0x00, 0x00, 0x00, 0x00,
79 0x04, 0x06, 0x14, 0x00, 0xEB, 0x85, 0x05, 0x2B,
80 0xB8, 0xD8, 0xA9, 0x49, 0x8B, 0x8C, 0xE2, 0x1B,
81 0x01, 0xAE, 0xF2, 0xB7, 0x7F, 0xFF, 0x04, 0x00,
84 /* mid-2007 MBP ("Santa Rosa" based models) */
85 static grub_uint8_t devpath_3[] =
87 0x01, 0x03, 0x18, 0x00, 0x0B, 0x00, 0x00, 0x00,
88 0x00, 0x00, 0xE0, 0xFF, 0x00, 0x00, 0x00, 0x00,
89 0xFF, 0xFF, 0xF8, 0xFF, 0x00, 0x00, 0x00, 0x00,
90 0x04, 0x06, 0x14, 0x00, 0xEB, 0x85, 0x05, 0x2B,
91 0xB8, 0xD8, 0xA9, 0x49, 0x8B, 0x8C, 0xE2, 0x1B,
92 0x01, 0xAE, 0xF2, 0xB7, 0x7F, 0xFF, 0x04, 0x00,
95 /* early-2008 MBA */
96 static grub_uint8_t devpath_4[] =
98 0x01, 0x03, 0x18, 0x00, 0x0B, 0x00, 0x00, 0x00,
99 0x00, 0x00, 0xC0, 0xFF, 0x00, 0x00, 0x00, 0x00,
100 0xFF, 0xFF, 0xF8, 0xFF, 0x00, 0x00, 0x00, 0x00,
101 0x04, 0x06, 0x14, 0x00, 0xEB, 0x85, 0x05, 0x2B,
102 0xB8, 0xD8, 0xA9, 0x49, 0x8B, 0x8C, 0xE2, 0x1B,
103 0x01, 0xAE, 0xF2, 0xB7, 0x7F, 0xFF, 0x04, 0x00,
106 /* late-2008 MB/MBP (NVidia chipset) */
107 static grub_uint8_t devpath_5[] = {
108 0x01, 0x03, 0x18, 0x00, 0x0B, 0x00, 0x00, 0x00,
109 0x00, 0x40, 0xCB, 0xFF, 0x00, 0x00, 0x00, 0x00,
110 0xFF, 0xBF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00,
111 0x04, 0x06, 0x14, 0x00, 0xEB, 0x85, 0x05, 0x2B,
112 0xB8, 0xD8, 0xA9, 0x49, 0x8B, 0x8C, 0xE2, 0x1B,
113 0x01, 0xAE, 0xF2, 0xB7, 0x7F, 0xFF, 0x04, 0x00,
116 struct devdata
118 char *model;
119 grub_efi_device_path_t *devpath;
122 struct devdata devs[] =
124 {"Core Duo/Solo", (grub_efi_device_path_t *) devpath_1},
125 {"Mac Pro", (grub_efi_device_path_t *) devpath_2},
126 {"MBP", (grub_efi_device_path_t *) devpath_3},
127 {"MBA", (grub_efi_device_path_t *) devpath_4},
128 {"MB NV", (grub_efi_device_path_t *) devpath_5},
129 {NULL, NULL},
132 static grub_err_t
133 grub_cmd_appleloader (grub_command_t cmd __attribute__ ((unused)),
134 int argc, char *argv[])
136 grub_efi_boot_services_t *b;
137 grub_efi_loaded_image_t *loaded_image;
138 struct devdata *pdev;
140 grub_dl_ref (my_mod);
142 /* Initialize some global variables. */
143 image_handle = 0;
145 b = grub_efi_system_table->boot_services;
147 for (pdev = devs ; pdev->devpath ; pdev++)
148 if (efi_call_6 (b->load_image, 0, grub_efi_image_handle, pdev->devpath,
149 NULL, 0, &image_handle) == GRUB_EFI_SUCCESS)
150 break;
152 if (! pdev->devpath)
154 grub_error (GRUB_ERR_BAD_OS, "can't find model");
155 goto fail;
158 grub_printf ("Model : %s\n", pdev->model);
160 loaded_image = grub_efi_get_loaded_image (image_handle);
161 if (! loaded_image)
163 grub_error (GRUB_ERR_BAD_OS, "no loaded image available");
164 goto fail;
167 if (argc > 0)
169 int i, len;
170 grub_efi_char16_t *p16;
172 for (i = 0, len = 0; i < argc; i++)
173 len += grub_strlen (argv[i]) + 1;
175 len *= sizeof (grub_efi_char16_t);
176 cmdline = p16 = grub_malloc (len);
177 if (! cmdline)
178 goto fail;
180 for (i = 0; i < argc; i++)
182 char *p8;
184 p8 = argv[i];
185 while (*p8)
186 *(p16++) = *(p8++);
188 *(p16++) = ' ';
190 *(--p16) = 0;
192 loaded_image->load_options = cmdline;
193 loaded_image->load_options_size = len;
196 grub_loader_set (grub_appleloader_boot, grub_appleloader_unload, 0);
198 return 0;
200 fail:
202 grub_dl_unref (my_mod);
203 return grub_errno;
206 static grub_command_t cmd;
208 GRUB_MOD_INIT(appleloader)
210 cmd = grub_register_command ("appleloader", grub_cmd_appleloader,
211 "appleloader [OPTS]", "Boot legacy system.");
212 my_mod = mod;
215 GRUB_MOD_FINI(appleloader)
217 grub_unregister_command (cmd);