]> git.proxmox.com Git - grub2.git/blob - grub-core/loader/efi/chainloader.c
merge trunk
[grub2.git] / grub-core / loader / efi / chainloader.c
1 /* chainloader.c - boot another boot loader */
2 /*
3 * GRUB -- GRand Unified Bootloader
4 * Copyright (C) 2002,2004,2006,2007,2008 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 /* TODO: support load options. */
21
22 #include <grub/loader.h>
23 #include <grub/file.h>
24 #include <grub/err.h>
25 #include <grub/device.h>
26 #include <grub/disk.h>
27 #include <grub/misc.h>
28 #include <grub/charset.h>
29 #include <grub/mm.h>
30 #include <grub/types.h>
31 #include <grub/dl.h>
32 #include <grub/efi/api.h>
33 #include <grub/efi/efi.h>
34 #include <grub/efi/disk.h>
35 #include <grub/command.h>
36 #include <grub/i18n.h>
37
38 GRUB_MOD_LICENSE ("GPLv3+");
39
40 static grub_dl_t my_mod;
41
42 static grub_efi_physical_address_t address;
43 static grub_efi_uintn_t pages;
44 static grub_efi_device_path_t *file_path;
45 static grub_efi_handle_t image_handle;
46 static grub_efi_char16_t *cmdline;
47
48 static grub_err_t
49 grub_chainloader_unload (void)
50 {
51 grub_efi_boot_services_t *b;
52
53 b = grub_efi_system_table->boot_services;
54 efi_call_1 (b->unload_image, image_handle);
55 efi_call_2 (b->free_pages, address, pages);
56
57 grub_free (file_path);
58 grub_free (cmdline);
59 cmdline = 0;
60
61 grub_dl_unref (my_mod);
62 return GRUB_ERR_NONE;
63 }
64
65 static grub_err_t
66 grub_chainloader_boot (void)
67 {
68 grub_efi_boot_services_t *b;
69 grub_efi_status_t status;
70 grub_efi_uintn_t exit_data_size;
71 grub_efi_char16_t *exit_data;
72
73 b = grub_efi_system_table->boot_services;
74 status = efi_call_3 (b->start_image, image_handle, &exit_data_size, &exit_data);
75 if (status != GRUB_EFI_SUCCESS)
76 {
77 if (exit_data)
78 {
79 char *buf;
80
81 buf = grub_malloc (exit_data_size * 4 + 1);
82 if (buf)
83 {
84 *grub_utf16_to_utf8 ((grub_uint8_t *) buf,
85 exit_data, exit_data_size) = 0;
86
87 grub_error (GRUB_ERR_BAD_OS, buf);
88 grub_free (buf);
89 }
90 else
91 grub_error (GRUB_ERR_BAD_OS, "unknown error");
92 }
93 }
94
95 if (exit_data)
96 efi_call_1 (b->free_pool, exit_data);
97
98 grub_chainloader_unload ();
99
100 return grub_errno;
101 }
102
103 static void
104 copy_file_path (grub_efi_file_path_device_path_t *fp,
105 const char *str, grub_efi_uint16_t len)
106 {
107 grub_efi_char16_t *p;
108 grub_efi_uint16_t size;
109
110 fp->header.type = GRUB_EFI_MEDIA_DEVICE_PATH_TYPE;
111 fp->header.subtype = GRUB_EFI_FILE_PATH_DEVICE_PATH_SUBTYPE;
112 size = len * sizeof (grub_efi_char16_t) + sizeof (*fp);
113 fp->header.length[0] = (grub_efi_uint8_t) (size & 0xff);
114 fp->header.length[1] = (grub_efi_uint8_t) (size >> 8);
115 for (p = fp->path_name; len > 0; len--, p++, str++)
116 {
117 /* FIXME: this assumes that the path is in ASCII. */
118 *p = (grub_efi_char16_t) (*str == '/' ? '\\' : *str);
119 }
120 }
121
122 static grub_efi_device_path_t *
123 make_file_path (grub_efi_device_path_t *dp, const char *filename)
124 {
125 char *dir_start;
126 char *dir_end;
127 grub_size_t size;
128 grub_efi_device_path_t *d;
129
130 dir_start = grub_strchr (filename, ')');
131 if (! dir_start)
132 dir_start = (char *) filename;
133 else
134 dir_start++;
135
136 dir_end = grub_strrchr (dir_start, '/');
137 if (! dir_end)
138 {
139 grub_error (GRUB_ERR_BAD_FILENAME, "invalid EFI file path");
140 return 0;
141 }
142
143 size = 0;
144 d = dp;
145 while (1)
146 {
147 size += GRUB_EFI_DEVICE_PATH_LENGTH (d);
148 if ((GRUB_EFI_END_ENTIRE_DEVICE_PATH (d)))
149 break;
150 d = GRUB_EFI_NEXT_DEVICE_PATH (d);
151 }
152
153 file_path = grub_malloc (size
154 + ((grub_strlen (dir_start) + 1)
155 * sizeof (grub_efi_char16_t))
156 + sizeof (grub_efi_file_path_device_path_t) * 2);
157 if (! file_path)
158 return 0;
159
160 grub_memcpy (file_path, dp, size);
161
162 /* Fill the file path for the directory. */
163 d = (grub_efi_device_path_t *) ((char *) file_path
164 + ((char *) d - (char *) dp));
165 grub_efi_print_device_path (d);
166 copy_file_path ((grub_efi_file_path_device_path_t *) d,
167 dir_start, dir_end - dir_start);
168
169 /* Fill the file path for the file. */
170 d = GRUB_EFI_NEXT_DEVICE_PATH (d);
171 copy_file_path ((grub_efi_file_path_device_path_t *) d,
172 dir_end + 1, grub_strlen (dir_end + 1));
173
174 /* Fill the end of device path nodes. */
175 d = GRUB_EFI_NEXT_DEVICE_PATH (d);
176 d->type = GRUB_EFI_END_DEVICE_PATH_TYPE;
177 d->subtype = GRUB_EFI_END_ENTIRE_DEVICE_PATH_SUBTYPE;
178 d->length[0] = sizeof (*d);
179 d->length[1] = 0;
180
181 return file_path;
182 }
183
184 static grub_err_t
185 grub_cmd_chainloader (grub_command_t cmd __attribute__ ((unused)),
186 int argc, char *argv[])
187 {
188 grub_file_t file = 0;
189 grub_ssize_t size;
190 grub_efi_status_t status;
191 grub_efi_boot_services_t *b;
192 grub_efi_handle_t dev_handle = 0;
193 grub_device_t dev = 0;
194 grub_efi_device_path_t *dp = 0;
195 grub_efi_loaded_image_t *loaded_image;
196 char *filename;
197
198 if (argc == 0)
199 return grub_error (GRUB_ERR_BAD_ARGUMENT, "no file specified");
200 filename = argv[0];
201
202 grub_dl_ref (my_mod);
203
204 /* Initialize some global variables. */
205 address = 0;
206 image_handle = 0;
207 file_path = 0;
208
209 b = grub_efi_system_table->boot_services;
210
211 file = grub_file_open (filename);
212 if (! file)
213 goto fail;
214
215 /* Get the root device's device path. */
216 dev = grub_device_open (0);
217 if (! dev)
218 goto fail;
219
220 if (dev->disk)
221 {
222 dev_handle = grub_efidisk_get_device_handle (dev->disk);
223 if (dev_handle)
224 dp = grub_efi_get_device_path (dev_handle);
225 }
226
227 if (! dev->disk || ! dev_handle || ! dp)
228 {
229 grub_error (GRUB_ERR_BAD_DEVICE, "not a valid root device");
230 goto fail;
231 }
232
233 file_path = make_file_path (dp, filename);
234 if (! file_path)
235 goto fail;
236
237 grub_printf ("file path: ");
238 grub_efi_print_device_path (file_path);
239
240 size = grub_file_size (file);
241 pages = (((grub_efi_uintn_t) size + ((1 << 12) - 1)) >> 12);
242
243 status = efi_call_4 (b->allocate_pages, GRUB_EFI_ALLOCATE_ANY_PAGES,
244 GRUB_EFI_LOADER_CODE,
245 pages, &address);
246 if (status != GRUB_EFI_SUCCESS)
247 {
248 grub_error (GRUB_ERR_OUT_OF_MEMORY, "cannot allocate %u pages", pages);
249 goto fail;
250 }
251
252 if (grub_file_read (file, (void *) ((grub_addr_t) address), size) != size)
253 {
254 if (grub_errno == GRUB_ERR_NONE)
255 grub_error (GRUB_ERR_BAD_OS, "too small");
256
257 goto fail;
258 }
259
260 status = efi_call_6 (b->load_image, 0, grub_efi_image_handle, file_path,
261 (void *) ((grub_addr_t) address), size,
262 &image_handle);
263 if (status != GRUB_EFI_SUCCESS)
264 {
265 if (status == GRUB_EFI_OUT_OF_RESOURCES)
266 grub_error (GRUB_ERR_OUT_OF_MEMORY, "out of resources");
267 else
268 grub_error (GRUB_ERR_BAD_OS, "cannot load image");
269
270 goto fail;
271 }
272
273 /* LoadImage does not set a device handler when the image is
274 loaded from memory, so it is necessary to set it explicitly here.
275 This is a mess. */
276 loaded_image = grub_efi_get_loaded_image (image_handle);
277 if (! loaded_image)
278 {
279 grub_error (GRUB_ERR_BAD_OS, "no loaded image available");
280 goto fail;
281 }
282 loaded_image->device_handle = dev_handle;
283
284 grub_file_close (file);
285
286 if (argc > 1)
287 {
288 int i, len;
289 grub_efi_char16_t *p16;
290
291 for (i = 1, len = 0; i < argc; i++)
292 len += grub_strlen (argv[i]) + 1;
293
294 len *= sizeof (grub_efi_char16_t);
295 cmdline = p16 = grub_malloc (len);
296 if (! cmdline)
297 goto fail;
298
299 for (i = 1; i < argc; i++)
300 {
301 char *p8;
302
303 p8 = argv[i];
304 while (*p8)
305 *(p16++) = *(p8++);
306
307 *(p16++) = ' ';
308 }
309 *(--p16) = 0;
310
311 loaded_image->load_options = cmdline;
312 loaded_image->load_options_size = len;
313 }
314
315 grub_loader_set (grub_chainloader_boot, grub_chainloader_unload, 0);
316 return 0;
317
318 fail:
319
320 if (dev)
321 grub_device_close (dev);
322
323 if (file)
324 grub_file_close (file);
325
326 if (file_path)
327 grub_free (file_path);
328
329 if (address)
330 efi_call_2 (b->free_pages, address, pages);
331
332 grub_dl_unref (my_mod);
333
334 return grub_errno;
335 }
336
337 static grub_command_t cmd;
338
339 GRUB_MOD_INIT(chainloader)
340 {
341 cmd = grub_register_command ("chainloader", grub_cmd_chainloader,
342 0, N_("Load another boot loader."));
343 my_mod = mod;
344 }
345
346 GRUB_MOD_FINI(chainloader)
347 {
348 grub_unregister_command (cmd);
349 }