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