]> glassweightruler.freedombox.rocks Git - Ventoy.git/blob - GRUB2/grub-2.04/grub-core/loader/efi/chainloader.c
Update PhyDrive.c
[Ventoy.git] / GRUB2 / grub-2.04 / 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 #include <grub/net.h>
38 #if defined (__i386__) || defined (__x86_64__)
39 #include <grub/macho.h>
40 #include <grub/i386/macho.h>
41 #endif
42
43 GRUB_MOD_LICENSE ("GPLv3+");
44
45 static grub_dl_t my_mod;
46
47 static grub_efi_physical_address_t address;
48 static grub_efi_uintn_t pages;
49 static grub_efi_device_path_t *file_path;
50 static grub_efi_handle_t image_handle;
51 static grub_efi_char16_t *cmdline;
52
53 static grub_err_t
54 grub_chainloader_unload (void)
55 {
56 grub_efi_boot_services_t *b;
57
58 b = grub_efi_system_table->boot_services;
59 efi_call_1 (b->unload_image, image_handle);
60 efi_call_2 (b->free_pages, address, pages);
61
62 grub_free (file_path);
63 grub_free (cmdline);
64 cmdline = 0;
65 file_path = 0;
66
67 grub_dl_unref (my_mod);
68 return GRUB_ERR_NONE;
69 }
70
71 static grub_err_t
72 grub_chainloader_boot (void)
73 {
74 grub_efi_boot_services_t *b;
75 grub_efi_status_t status;
76 grub_efi_uintn_t exit_data_size;
77 grub_efi_char16_t *exit_data = NULL;
78
79 b = grub_efi_system_table->boot_services;
80 status = efi_call_3 (b->start_image, image_handle, &exit_data_size, &exit_data);
81 if (status != GRUB_EFI_SUCCESS)
82 {
83 if (exit_data)
84 {
85 char *buf;
86
87 buf = grub_malloc (exit_data_size * 4 + 1);
88 if (buf)
89 {
90 *grub_utf16_to_utf8 ((grub_uint8_t *) buf,
91 exit_data, exit_data_size) = 0;
92
93 grub_error (GRUB_ERR_BAD_OS, buf);
94 grub_free (buf);
95 }
96 }
97 else
98 grub_error (GRUB_ERR_BAD_OS, "unknown error");
99 }
100
101 if (exit_data)
102 efi_call_1 (b->free_pool, exit_data);
103
104 grub_loader_unset ();
105
106 return grub_errno;
107 }
108
109 static void
110 copy_file_path (grub_efi_file_path_device_path_t *fp,
111 const char *str, grub_efi_uint16_t len)
112 {
113 grub_efi_char16_t *p, *path_name;
114 grub_efi_uint16_t size;
115
116 fp->header.type = GRUB_EFI_MEDIA_DEVICE_PATH_TYPE;
117 fp->header.subtype = GRUB_EFI_FILE_PATH_DEVICE_PATH_SUBTYPE;
118
119 path_name = grub_malloc (len * GRUB_MAX_UTF16_PER_UTF8 * sizeof (*path_name));
120 if (!path_name)
121 return;
122
123 size = grub_utf8_to_utf16 (path_name, len * GRUB_MAX_UTF16_PER_UTF8,
124 (const grub_uint8_t *) str, len, 0);
125 for (p = path_name; p < path_name + size; p++)
126 if (*p == '/')
127 *p = '\\';
128
129 grub_memcpy (fp->path_name, path_name, size * sizeof (*fp->path_name));
130 /* File Path is NULL terminated */
131 fp->path_name[size++] = '\0';
132 fp->header.length = size * sizeof (grub_efi_char16_t) + sizeof (*fp);
133 grub_free (path_name);
134 }
135
136 static grub_efi_device_path_t *
137 make_file_path (grub_efi_device_path_t *dp, const char *filename)
138 {
139 char *dir_start;
140 char *dir_end;
141 grub_size_t size;
142 grub_efi_device_path_t *d;
143
144 dir_start = grub_strchr (filename, ')');
145 if (! dir_start)
146 dir_start = (char *) filename;
147 else
148 dir_start++;
149
150 dir_end = grub_strrchr (dir_start, '/');
151 if (! dir_end)
152 {
153 grub_error (GRUB_ERR_BAD_FILENAME, "invalid EFI file path");
154 return 0;
155 }
156
157 size = 0;
158 d = dp;
159 while (1)
160 {
161 size += GRUB_EFI_DEVICE_PATH_LENGTH (d);
162 if ((GRUB_EFI_END_ENTIRE_DEVICE_PATH (d)))
163 break;
164 d = GRUB_EFI_NEXT_DEVICE_PATH (d);
165 }
166
167 /* File Path is NULL terminated. Allocate space for 2 extra characters */
168 /* FIXME why we split path in two components? */
169 file_path = grub_malloc (size
170 + ((grub_strlen (dir_start) + 2)
171 * GRUB_MAX_UTF16_PER_UTF8
172 * sizeof (grub_efi_char16_t))
173 + sizeof (grub_efi_file_path_device_path_t) * 2);
174 if (! file_path)
175 return 0;
176
177 grub_memcpy (file_path, dp, size);
178
179 /* Fill the file path for the directory. */
180 d = (grub_efi_device_path_t *) ((char *) file_path
181 + ((char *) d - (char *) dp));
182 //grub_efi_print_device_path (d);
183 copy_file_path ((grub_efi_file_path_device_path_t *) d,
184 dir_start, dir_end - dir_start);
185
186 /* Fill the file path for the file. */
187 d = GRUB_EFI_NEXT_DEVICE_PATH (d);
188 copy_file_path ((grub_efi_file_path_device_path_t *) d,
189 dir_end + 1, grub_strlen (dir_end + 1));
190
191 /* Fill the end of device path nodes. */
192 d = GRUB_EFI_NEXT_DEVICE_PATH (d);
193 d->type = GRUB_EFI_END_DEVICE_PATH_TYPE;
194 d->subtype = GRUB_EFI_END_ENTIRE_DEVICE_PATH_SUBTYPE;
195 d->length = sizeof (*d);
196
197 return file_path;
198 }
199
200 static grub_err_t
201 grub_cmd_chainloader (grub_command_t cmd __attribute__ ((unused)),
202 int argc, char *argv[])
203 {
204 grub_file_t file = 0;
205 grub_ssize_t size;
206 grub_efi_status_t status;
207 grub_efi_boot_services_t *b;
208 grub_device_t dev = 0;
209 grub_efi_device_path_t *dp = 0;
210 grub_efi_loaded_image_t *loaded_image;
211 char *filename;
212 void *boot_image = 0;
213 grub_efi_handle_t dev_handle = 0;
214
215 if (argc == 0)
216 return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("filename expected"));
217 filename = argv[0];
218
219 grub_dl_ref (my_mod);
220
221 /* Initialize some global variables. */
222 address = 0;
223 image_handle = 0;
224 file_path = 0;
225
226 b = grub_efi_system_table->boot_services;
227
228 file = grub_file_open (filename, GRUB_FILE_TYPE_EFI_CHAINLOADED_IMAGE);
229 if (! file)
230 goto fail;
231
232 /* Get the root device's device path. */
233 dev = grub_device_open (0);
234 if (! dev)
235 goto fail;
236
237 if (dev->disk)
238 dev_handle = grub_efidisk_get_device_handle (dev->disk);
239 else if (dev->net && dev->net->server)
240 {
241 grub_net_network_level_address_t addr;
242 struct grub_net_network_level_interface *inf;
243 grub_net_network_level_address_t gateway;
244 grub_err_t err;
245
246 err = grub_net_resolve_address (dev->net->server, &addr);
247 if (err)
248 goto fail;
249
250 err = grub_net_route_address (addr, &gateway, &inf);
251 if (err)
252 goto fail;
253
254 dev_handle = grub_efinet_get_device_handle (inf->card);
255 }
256
257 if (dev_handle)
258 dp = grub_efi_get_device_path (dev_handle);
259
260 if (! dp)
261 {
262 grub_error (GRUB_ERR_BAD_DEVICE, "not a valid root device");
263 goto fail;
264 }
265
266 file_path = make_file_path (dp, filename);
267 if (! file_path)
268 goto fail;
269
270 //grub_printf ("file path: ");
271 //grub_efi_print_device_path (file_path);
272
273 size = grub_file_size (file);
274 if (!size)
275 {
276 grub_error (GRUB_ERR_BAD_OS, N_("premature end of file %s"),
277 filename);
278 goto fail;
279 }
280 pages = (((grub_efi_uintn_t) size + ((1 << 12) - 1)) >> 12);
281
282 status = efi_call_4 (b->allocate_pages, GRUB_EFI_ALLOCATE_ANY_PAGES,
283 GRUB_EFI_LOADER_CODE,
284 pages, &address);
285 if (status != GRUB_EFI_SUCCESS)
286 {
287 grub_dprintf ("chain", "Failed to allocate %u pages\n",
288 (unsigned int) pages);
289 grub_error (GRUB_ERR_OUT_OF_MEMORY, N_("out of memory"));
290 goto fail;
291 }
292
293 boot_image = (void *) ((grub_addr_t) address);
294 if (grub_file_read (file, boot_image, size) != size)
295 {
296 if (grub_errno == GRUB_ERR_NONE)
297 grub_error (GRUB_ERR_BAD_OS, N_("premature end of file %s"),
298 filename);
299
300 goto fail;
301 }
302
303 #if defined (__i386__) || defined (__x86_64__)
304 if (size >= (grub_ssize_t) sizeof (struct grub_macho_fat_header))
305 {
306 struct grub_macho_fat_header *head = boot_image;
307 if (head->magic
308 == grub_cpu_to_le32_compile_time (GRUB_MACHO_FAT_EFI_MAGIC))
309 {
310 grub_uint32_t i;
311 struct grub_macho_fat_arch *archs
312 = (struct grub_macho_fat_arch *) (head + 1);
313 for (i = 0; i < grub_cpu_to_le32 (head->nfat_arch); i++)
314 {
315 if (GRUB_MACHO_CPUTYPE_IS_HOST_CURRENT (archs[i].cputype))
316 break;
317 }
318 if (i == grub_cpu_to_le32 (head->nfat_arch))
319 {
320 grub_error (GRUB_ERR_BAD_OS, "no compatible arch found");
321 goto fail;
322 }
323 if (grub_cpu_to_le32 (archs[i].offset)
324 > ~grub_cpu_to_le32 (archs[i].size)
325 || grub_cpu_to_le32 (archs[i].offset)
326 + grub_cpu_to_le32 (archs[i].size)
327 > (grub_size_t) size)
328 {
329 grub_error (GRUB_ERR_BAD_OS, N_("premature end of file %s"),
330 filename);
331 goto fail;
332 }
333 boot_image = (char *) boot_image + grub_cpu_to_le32 (archs[i].offset);
334 size = grub_cpu_to_le32 (archs[i].size);
335 }
336 }
337 #endif
338
339 status = efi_call_6 (b->load_image, 0, grub_efi_image_handle, file_path,
340 boot_image, size,
341 &image_handle);
342 if (status != GRUB_EFI_SUCCESS)
343 {
344 if (status == GRUB_EFI_OUT_OF_RESOURCES)
345 grub_error (GRUB_ERR_OUT_OF_MEMORY, "out of resources");
346 else
347 grub_error (GRUB_ERR_BAD_OS, "cannot load image");
348
349 goto fail;
350 }
351
352 /* LoadImage does not set a device handler when the image is
353 loaded from memory, so it is necessary to set it explicitly here.
354 This is a mess. */
355 loaded_image = grub_efi_get_loaded_image (image_handle);
356 if (! loaded_image)
357 {
358 grub_error (GRUB_ERR_BAD_OS, "no loaded image available");
359 goto fail;
360 }
361 loaded_image->device_handle = dev_handle;
362
363 if (argc > 1)
364 {
365 int i, len;
366 grub_efi_char16_t *p16;
367
368 for (i = 1, len = 0; i < argc; i++)
369 len += grub_strlen (argv[i]) + 1;
370
371 len *= sizeof (grub_efi_char16_t);
372 cmdline = p16 = grub_malloc (len);
373 if (! cmdline)
374 goto fail;
375
376 for (i = 1; i < argc; i++)
377 {
378 char *p8;
379
380 p8 = argv[i];
381 while (*p8)
382 *(p16++) = *(p8++);
383
384 *(p16++) = ' ';
385 }
386 *(--p16) = 0;
387
388 loaded_image->load_options = cmdline;
389 loaded_image->load_options_size = len;
390 }
391
392 grub_file_close (file);
393 grub_device_close (dev);
394
395 grub_loader_set (grub_chainloader_boot, grub_chainloader_unload, 0);
396 return 0;
397
398 fail:
399
400 if (dev)
401 grub_device_close (dev);
402
403 if (file)
404 grub_file_close (file);
405
406 grub_free (file_path);
407
408 if (address)
409 efi_call_2 (b->free_pages, address, pages);
410
411 grub_dl_unref (my_mod);
412
413 return grub_errno;
414 }
415
416 static grub_command_t cmd;
417
418 GRUB_MOD_INIT(chainloader)
419 {
420 cmd = grub_register_command ("chainloader", grub_cmd_chainloader,
421 0, N_("Load another boot loader."));
422 my_mod = mod;
423 }
424
425 GRUB_MOD_FINI(chainloader)
426 {
427 grub_unregister_command (cmd);
428 }