]> git.proxmox.com Git - grub2.git/blob - grub-core/commands/file.c
Import grub2_2.04.orig.tar.xz
[grub2.git] / grub-core / commands / file.c
1 /*
2 * GRUB -- GRand Unified Bootloader
3 * Copyright (C) 2013 Free Software Foundation, Inc.
4 *
5 * GRUB is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * GRUB is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 #include <grub/dl.h>
20 #include <grub/misc.h>
21 #include <grub/mm.h>
22 #include <grub/env.h>
23 #include <grub/command.h>
24 #include <grub/extcmd.h>
25 #include <grub/i18n.h>
26 #include <grub/file.h>
27 #include <grub/elf.h>
28 #include <grub/xen_file.h>
29 #include <grub/efi/pe32.h>
30 #include <grub/arm/linux.h>
31 #include <grub/arm64/linux.h>
32 #include <grub/i386/linux.h>
33 #include <grub/xnu.h>
34 #include <grub/machoload.h>
35 #include <grub/fileid.h>
36
37 GRUB_MOD_LICENSE ("GPLv3+");
38
39 static const struct grub_arg_option options[] = {
40 {"is-i386-xen-pae-domu", 0, 0,
41 N_("Check if FILE can be booted as i386 PAE Xen unprivileged guest kernel"),
42 0, 0},
43 {"is-x86_64-xen-domu", 0, 0,
44 N_("Check if FILE can be booted as x86_64 Xen unprivileged guest kernel"), 0, 0},
45 {"is-x86-xen-dom0", 0, 0,
46 N_("Check if FILE can be used as Xen x86 privileged guest kernel"), 0, 0},
47 {"is-x86-multiboot", 0, 0,
48 N_("Check if FILE can be used as x86 multiboot kernel"), 0, 0},
49 {"is-x86-multiboot2", 0, 0,
50 N_("Check if FILE can be used as x86 multiboot2 kernel"), 0, 0},
51 {"is-arm-linux", 0, 0,
52 N_("Check if FILE is ARM Linux"), 0, 0},
53 {"is-arm64-linux", 0, 0,
54 N_("Check if FILE is ARM64 Linux"), 0, 0},
55 {"is-ia64-linux", 0, 0,
56 N_("Check if FILE is IA64 Linux"), 0, 0},
57 {"is-mips-linux", 0, 0,
58 N_("Check if FILE is MIPS Linux"), 0, 0},
59 {"is-mipsel-linux", 0, 0,
60 N_("Check if FILE is MIPSEL Linux"), 0, 0},
61 {"is-sparc64-linux", 0, 0,
62 N_("Check if FILE is SPARC64 Linux"), 0, 0},
63 {"is-powerpc-linux", 0, 0,
64 N_("Check if FILE is POWERPC Linux"), 0, 0},
65 {"is-x86-linux", 0, 0,
66 N_("Check if FILE is x86 Linux"), 0, 0},
67 {"is-x86-linux32", 0, 0,
68 N_("Check if FILE is x86 Linux supporting 32-bit protocol"), 0, 0},
69 {"is-x86-kfreebsd", 0, 0,
70 N_("Check if FILE is x86 kFreeBSD"), 0, 0},
71 {"is-i386-kfreebsd", 0, 0,
72 N_("Check if FILE is i386 kFreeBSD"), 0, 0},
73 {"is-x86_64-kfreebsd", 0, 0,
74 N_("Check if FILE is x86_64 kFreeBSD"), 0, 0},
75
76 {"is-x86-knetbsd", 0, 0,
77 N_("Check if FILE is x86 kNetBSD"), 0, 0},
78 {"is-i386-knetbsd", 0, 0,
79 N_("Check if FILE is i386 kNetBSD"), 0, 0},
80 {"is-x86_64-knetbsd", 0, 0,
81 N_("Check if FILE is x86_64 kNetBSD"), 0, 0},
82
83 {"is-i386-efi", 0, 0,
84 N_("Check if FILE is i386 EFI file"), 0, 0},
85 {"is-x86_64-efi", 0, 0,
86 N_("Check if FILE is x86_64 EFI file"), 0, 0},
87 {"is-ia64-efi", 0, 0,
88 N_("Check if FILE is IA64 EFI file"), 0, 0},
89 {"is-arm64-efi", 0, 0,
90 N_("Check if FILE is ARM64 EFI file"), 0, 0},
91 {"is-arm-efi", 0, 0,
92 N_("Check if FILE is ARM EFI file"), 0, 0},
93 {"is-riscv32-efi", 0, 0,
94 N_("Check if FILE is RISC-V 32bit EFI file"), 0, 0},
95 {"is-riscv64-efi", 0, 0,
96 N_("Check if FILE is RISC-V 64bit EFI file"), 0, 0},
97 {"is-hibernated-hiberfil", 0, 0,
98 N_("Check if FILE is hiberfil.sys in hibernated state"), 0, 0},
99 {"is-x86_64-xnu", 0, 0,
100 N_("Check if FILE is x86_64 XNU (Mac OS X kernel)"), 0, 0},
101 {"is-i386-xnu", 0, 0,
102 N_("Check if FILE is i386 XNU (Mac OS X kernel)"), 0, 0},
103 {"is-xnu-hibr", 0, 0,
104 N_("Check if FILE is XNU (Mac OS X kernel) hibernated image"), 0, 0},
105 {"is-x86-bios-bootsector", 0, 0,
106 N_("Check if FILE is BIOS bootsector"), 0, 0},
107 {0, 0, 0, 0, 0, 0}
108 };
109
110 enum
111 {
112 IS_PAE_DOMU,
113 IS_64_DOMU,
114 IS_DOM0,
115 IS_MULTIBOOT,
116 IS_MULTIBOOT2,
117 IS_ARM_LINUX,
118 IS_ARM64_LINUX,
119 IS_IA64_LINUX,
120 IS_MIPS_LINUX,
121 IS_MIPSEL_LINUX,
122 IS_SPARC64_LINUX,
123 IS_POWERPC_LINUX,
124 IS_X86_LINUX,
125 IS_X86_LINUX32,
126 IS_X86_KFREEBSD,
127 IS_X86_KFREEBSD32,
128 IS_X86_KFREEBSD64,
129 IS_X86_KNETBSD,
130 IS_X86_KNETBSD32,
131 IS_X86_KNETBSD64,
132 IS_32_EFI,
133 IS_64_EFI,
134 IS_IA_EFI,
135 IS_ARM64_EFI,
136 IS_ARM_EFI,
137 IS_RISCV_EFI,
138 IS_HIBERNATED,
139 IS_XNU64,
140 IS_XNU32,
141 IS_XNU_HIBR,
142 IS_BIOS_BOOTSECTOR,
143 OPT_TYPE_MIN = IS_PAE_DOMU,
144 OPT_TYPE_MAX = IS_BIOS_BOOTSECTOR
145 };
146
147
148 static grub_err_t
149 grub_cmd_file (grub_extcmd_context_t ctxt, int argc, char **args)
150 {
151 grub_file_t file = 0;
152 grub_elf_t elf = 0;
153 grub_err_t err;
154 int type = -1, i;
155 int ret = 0;
156 grub_macho_t macho = 0;
157
158 if (argc == 0)
159 return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("filename expected"));
160 for (i = OPT_TYPE_MIN; i <= OPT_TYPE_MAX; i++)
161 if (ctxt->state[i].set)
162 {
163 if (type == -1)
164 {
165 type = i;
166 continue;
167 }
168 return grub_error (GRUB_ERR_BAD_ARGUMENT, "multiple types specified");
169 }
170 if (type == -1)
171 return grub_error (GRUB_ERR_BAD_ARGUMENT, "no type specified");
172
173 file = grub_file_open (args[0], GRUB_FILE_TYPE_XNU_KERNEL);
174 if (!file)
175 return grub_errno;
176 switch (type)
177 {
178 case IS_BIOS_BOOTSECTOR:
179 {
180 grub_uint16_t sig;
181 if (grub_file_size (file) != 512)
182 break;
183 if (grub_file_seek (file, 510) == (grub_size_t) -1)
184 break;
185 if (grub_file_read (file, &sig, 2) != 2)
186 break;
187 if (sig != grub_cpu_to_le16_compile_time (0xaa55))
188 break;
189 ret = 1;
190 break;
191 }
192 case IS_IA64_LINUX:
193 {
194 Elf64_Ehdr ehdr;
195
196 if (grub_file_read (file, &ehdr, sizeof (ehdr)) != sizeof (ehdr))
197 break;
198
199 if (ehdr.e_ident[EI_MAG0] != ELFMAG0
200 || ehdr.e_ident[EI_MAG1] != ELFMAG1
201 || ehdr.e_ident[EI_MAG2] != ELFMAG2
202 || ehdr.e_ident[EI_MAG3] != ELFMAG3
203 || ehdr.e_ident[EI_VERSION] != EV_CURRENT
204 || ehdr.e_version != EV_CURRENT)
205 break;
206
207 if (ehdr.e_ident[EI_CLASS] != ELFCLASS64
208 || ehdr.e_ident[EI_DATA] != ELFDATA2LSB
209 || ehdr.e_machine != grub_cpu_to_le16_compile_time (EM_IA_64))
210 break;
211
212 ret = 1;
213
214 break;
215 }
216
217 case IS_SPARC64_LINUX:
218 {
219 Elf64_Ehdr ehdr;
220
221 if (grub_file_read (file, &ehdr, sizeof (ehdr)) != sizeof (ehdr))
222 break;
223
224 if (ehdr.e_ident[EI_MAG0] != ELFMAG0
225 || ehdr.e_ident[EI_MAG1] != ELFMAG1
226 || ehdr.e_ident[EI_MAG2] != ELFMAG2
227 || ehdr.e_ident[EI_MAG3] != ELFMAG3
228 || ehdr.e_ident[EI_VERSION] != EV_CURRENT
229 || ehdr.e_version != EV_CURRENT)
230 break;
231
232 if (ehdr.e_ident[EI_CLASS] != ELFCLASS64
233 || ehdr.e_ident[EI_DATA] != ELFDATA2MSB)
234 break;
235
236 if (ehdr.e_machine != grub_cpu_to_le16_compile_time (EM_SPARCV9)
237 || ehdr.e_type != grub_cpu_to_be16_compile_time (ET_EXEC))
238 break;
239
240 ret = 1;
241
242 break;
243 }
244
245 case IS_POWERPC_LINUX:
246 {
247 Elf32_Ehdr ehdr;
248
249 if (grub_file_read (file, &ehdr, sizeof (ehdr)) != sizeof (ehdr))
250 break;
251
252 if (ehdr.e_ident[EI_MAG0] != ELFMAG0
253 || ehdr.e_ident[EI_MAG1] != ELFMAG1
254 || ehdr.e_ident[EI_MAG2] != ELFMAG2
255 || ehdr.e_ident[EI_MAG3] != ELFMAG3
256 || ehdr.e_ident[EI_VERSION] != EV_CURRENT
257 || ehdr.e_version != EV_CURRENT)
258 break;
259
260 if (ehdr.e_ident[EI_DATA] != ELFDATA2MSB
261 || (ehdr.e_machine != grub_cpu_to_le16_compile_time (EM_PPC)
262 && ehdr.e_machine !=
263 grub_cpu_to_le16_compile_time (EM_PPC64)))
264 break;
265
266 if (ehdr.e_type != grub_cpu_to_be16_compile_time (ET_EXEC)
267 && ehdr.e_type != grub_cpu_to_be16_compile_time (ET_DYN))
268 break;
269
270 ret = 1;
271
272 break;
273 }
274
275 case IS_MIPS_LINUX:
276 {
277 Elf32_Ehdr ehdr;
278
279 if (grub_file_read (file, &ehdr, sizeof (ehdr)) != sizeof (ehdr))
280 break;
281
282 if (ehdr.e_ident[EI_MAG0] != ELFMAG0
283 || ehdr.e_ident[EI_MAG1] != ELFMAG1
284 || ehdr.e_ident[EI_MAG2] != ELFMAG2
285 || ehdr.e_ident[EI_MAG3] != ELFMAG3
286 || ehdr.e_ident[EI_VERSION] != EV_CURRENT
287 || ehdr.e_version != EV_CURRENT)
288 break;
289
290 if (ehdr.e_ident[EI_DATA] != ELFDATA2MSB
291 || ehdr.e_machine != grub_cpu_to_be16_compile_time (EM_MIPS)
292 || ehdr.e_type != grub_cpu_to_be16_compile_time (ET_EXEC))
293 break;
294
295 ret = 1;
296
297 break;
298 }
299
300 case IS_X86_KNETBSD:
301 case IS_X86_KNETBSD32:
302 case IS_X86_KNETBSD64:
303 {
304 int is32, is64;
305
306 elf = grub_elf_file (file, file->name);
307
308 if (elf->ehdr.ehdr32.e_type != grub_cpu_to_le16_compile_time (ET_EXEC)
309 || elf->ehdr.ehdr32.e_ident[EI_DATA] != ELFDATA2LSB)
310 break;
311
312 is32 = grub_elf_is_elf32 (elf);
313 is64 = grub_elf_is_elf64 (elf);
314 if (!is32 && !is64)
315 break;
316 if (!is32 && type == IS_X86_KNETBSD32)
317 break;
318 if (!is64 && type == IS_X86_KNETBSD64)
319 break;
320 if (is64)
321 ret = grub_file_check_netbsd64 (elf);
322 if (is32)
323 ret = grub_file_check_netbsd32 (elf);
324 break;
325 }
326
327 case IS_X86_KFREEBSD:
328 case IS_X86_KFREEBSD32:
329 case IS_X86_KFREEBSD64:
330 {
331 Elf32_Ehdr ehdr;
332 int is32, is64;
333
334 if (grub_file_read (file, &ehdr, sizeof (ehdr)) != sizeof (ehdr))
335 break;
336
337 if (ehdr.e_ident[EI_MAG0] != ELFMAG0
338 || ehdr.e_ident[EI_MAG1] != ELFMAG1
339 || ehdr.e_ident[EI_MAG2] != ELFMAG2
340 || ehdr.e_ident[EI_MAG3] != ELFMAG3
341 || ehdr.e_ident[EI_VERSION] != EV_CURRENT
342 || ehdr.e_version != EV_CURRENT)
343 break;
344
345 if (ehdr.e_type != grub_cpu_to_le16_compile_time (ET_EXEC)
346 || ehdr.e_ident[EI_DATA] != ELFDATA2LSB)
347 break;
348
349 if (ehdr.e_ident[EI_OSABI] != ELFOSABI_FREEBSD)
350 break;
351
352 is32 = (ehdr.e_machine == grub_cpu_to_le16_compile_time (EM_386)
353 && ehdr.e_ident[EI_CLASS] == ELFCLASS32);
354 is64 = (ehdr.e_machine == grub_cpu_to_le16_compile_time (EM_X86_64)
355 && ehdr.e_ident[EI_CLASS] == ELFCLASS64);
356 if (!is32 && !is64)
357 break;
358 if (!is32 && (type == IS_X86_KFREEBSD32 || type == IS_X86_KNETBSD32))
359 break;
360 if (!is64 && (type == IS_X86_KFREEBSD64 || type == IS_X86_KNETBSD64))
361 break;
362 ret = 1;
363
364 break;
365 }
366
367
368 case IS_MIPSEL_LINUX:
369 {
370 Elf32_Ehdr ehdr;
371
372 if (grub_file_read (file, &ehdr, sizeof (ehdr)) != sizeof (ehdr))
373 break;
374
375 if (ehdr.e_ident[EI_MAG0] != ELFMAG0
376 || ehdr.e_ident[EI_MAG1] != ELFMAG1
377 || ehdr.e_ident[EI_MAG2] != ELFMAG2
378 || ehdr.e_ident[EI_MAG3] != ELFMAG3
379 || ehdr.e_ident[EI_VERSION] != EV_CURRENT
380 || ehdr.e_version != EV_CURRENT)
381 break;
382
383 if (ehdr.e_machine != grub_cpu_to_le16_compile_time (EM_MIPS)
384 || ehdr.e_type != grub_cpu_to_le16_compile_time (ET_EXEC))
385 break;
386
387 ret = 1;
388
389 break;
390 }
391 case IS_ARM_LINUX:
392 {
393 struct linux_arm_kernel_header lh;
394
395 if (grub_file_read (file, &lh, sizeof (lh)) != sizeof (lh))
396 break;
397 /* Short forward branch in A32 state (for Raspberry pi kernels). */
398 if (lh.code0 == grub_cpu_to_le32_compile_time (0xea000006))
399 {
400 ret = 1;
401 break;
402 }
403
404 if (lh.magic ==
405 grub_cpu_to_le32_compile_time (GRUB_LINUX_ARM_MAGIC_SIGNATURE))
406 {
407 ret = 1;
408 break;
409 }
410 break;
411 }
412 case IS_ARM64_LINUX:
413 {
414 struct linux_arm64_kernel_header lh;
415
416 if (grub_file_read (file, &lh, sizeof (lh)) != sizeof (lh))
417 break;
418
419 if (lh.magic ==
420 grub_cpu_to_le32_compile_time (GRUB_LINUX_ARM64_MAGIC_SIGNATURE))
421 {
422 ret = 1;
423 break;
424 }
425 break;
426 }
427 case IS_PAE_DOMU ... IS_DOM0:
428 {
429 struct grub_xen_file_info xen_inf;
430 elf = grub_xen_file (file);
431 if (!elf)
432 break;
433 err = grub_xen_get_info (elf, &xen_inf);
434 if (err)
435 break;
436 /* Unfortuntely no way to check if kernel supports dom0. */
437 if (type == IS_DOM0)
438 ret = 1;
439 if (type == IS_PAE_DOMU)
440 ret = (xen_inf.arch == GRUB_XEN_FILE_I386_PAE
441 || xen_inf.arch == GRUB_XEN_FILE_I386_PAE_BIMODE);
442 if (type == IS_64_DOMU)
443 ret = (xen_inf.arch == GRUB_XEN_FILE_X86_64);
444 break;
445 }
446 case IS_MULTIBOOT:
447 case IS_MULTIBOOT2:
448 {
449 grub_uint32_t *buffer;
450 grub_ssize_t len;
451 grub_size_t search_size;
452 grub_uint32_t *header;
453 grub_uint32_t magic;
454 grub_size_t step;
455
456 if (type == IS_MULTIBOOT2)
457 {
458 search_size = 32768;
459 magic = grub_cpu_to_le32_compile_time (0xe85250d6);
460 step = 2;
461 }
462 else
463 {
464 search_size = 8192;
465 magic = grub_cpu_to_le32_compile_time (0x1BADB002);
466 step = 1;
467 }
468
469 buffer = grub_malloc (search_size);
470 if (!buffer)
471 break;
472
473 len = grub_file_read (file, buffer, search_size);
474 if (len < 32)
475 {
476 grub_free (buffer);
477 break;
478 }
479
480 /* Look for the multiboot header in the buffer. The header should
481 be at least 12 bytes and aligned on a 4-byte boundary. */
482 for (header = buffer;
483 ((char *) header <=
484 (char *) buffer + len - (type == IS_MULTIBOOT2 ? 16 : 12));
485 header += step)
486 {
487 if (header[0] == magic
488 && !(grub_le_to_cpu32 (header[0])
489 + grub_le_to_cpu32 (header[1])
490 + grub_le_to_cpu32 (header[2])
491 + (type == IS_MULTIBOOT2
492 ? grub_le_to_cpu32 (header[3]) : 0)))
493 {
494 ret = 1;
495 break;
496 }
497 }
498
499 grub_free (buffer);
500 break;
501 }
502 case IS_X86_LINUX32:
503 case IS_X86_LINUX:
504 {
505 struct linux_i386_kernel_header lh;
506 if (grub_file_read (file, &lh, sizeof (lh)) != sizeof (lh))
507 break;
508 if (lh.boot_flag != grub_cpu_to_le16_compile_time (0xaa55))
509 break;
510
511 if (lh.setup_sects > GRUB_LINUX_MAX_SETUP_SECTS)
512 break;
513
514 /* FIXME: some really old kernels (< 1.3.73) will fail this. */
515 if (lh.header !=
516 grub_cpu_to_le32_compile_time (GRUB_LINUX_I386_MAGIC_SIGNATURE)
517 || grub_le_to_cpu16 (lh.version) < 0x0200)
518 break;
519
520 if (type == IS_X86_LINUX)
521 {
522 ret = 1;
523 break;
524 }
525
526 /* FIXME: 2.03 is not always good enough (Linux 2.4 can be 2.03 and
527 still not support 32-bit boot. */
528 if (lh.header !=
529 grub_cpu_to_le32_compile_time (GRUB_LINUX_I386_MAGIC_SIGNATURE)
530 || grub_le_to_cpu16 (lh.version) < 0x0203)
531 break;
532
533 if (!(lh.loadflags & GRUB_LINUX_FLAG_BIG_KERNEL))
534 break;
535 ret = 1;
536 break;
537 }
538 case IS_HIBERNATED:
539 {
540 grub_uint8_t hibr_file_magic[4];
541 if (grub_file_read (file, &hibr_file_magic, sizeof (hibr_file_magic))
542 != sizeof (hibr_file_magic))
543 break;
544 if (grub_memcmp ("hibr", hibr_file_magic, sizeof (hibr_file_magic)) ==
545 0
546 || grub_memcmp ("HIBR", hibr_file_magic,
547 sizeof (hibr_file_magic)) == 0)
548 ret = 1;
549 break;
550 }
551 case IS_XNU64:
552 case IS_XNU32:
553 {
554 macho = grub_macho_open (args[0], GRUB_FILE_TYPE_XNU_KERNEL,
555 (type == IS_XNU64));
556 if (!macho)
557 break;
558 /* FIXME: more checks? */
559 ret = 1;
560 break;
561 }
562 case IS_XNU_HIBR:
563 {
564 struct grub_xnu_hibernate_header hibhead;
565 if (grub_file_read (file, &hibhead, sizeof (hibhead))
566 != sizeof (hibhead))
567 break;
568 if (hibhead.magic !=
569 grub_cpu_to_le32_compile_time (GRUB_XNU_HIBERNATE_MAGIC))
570 break;
571 ret = 1;
572 break;
573 }
574 case IS_32_EFI:
575 case IS_64_EFI:
576 case IS_IA_EFI:
577 case IS_ARM64_EFI:
578 case IS_ARM_EFI:
579 case IS_RISCV_EFI:
580 {
581 char signature[4];
582 grub_uint32_t pe_offset;
583 struct grub_pe32_coff_header coff_head;
584
585 if (grub_file_read (file, signature, 2) != 2)
586 break;
587 if (signature[0] != 'M' || signature[1] != 'Z')
588 break;
589 if ((grub_ssize_t) grub_file_seek (file, 0x3c) == -1)
590 break;
591 if (grub_file_read (file, &pe_offset, 4) != 4)
592 break;
593 if ((grub_ssize_t) grub_file_seek (file, grub_le_to_cpu32 (pe_offset))
594 == -1)
595 break;
596 if (grub_file_read (file, signature, 4) != 4)
597 break;
598 if (signature[0] != 'P' || signature[1] != 'E'
599 || signature[2] != '\0' || signature[3] != '\0')
600 break;
601
602 if (grub_file_read (file, &coff_head, sizeof (coff_head))
603 != sizeof (coff_head))
604 break;
605 if (type == IS_32_EFI
606 && coff_head.machine !=
607 grub_cpu_to_le16_compile_time (GRUB_PE32_MACHINE_I386))
608 break;
609 if (type == IS_64_EFI
610 && coff_head.machine !=
611 grub_cpu_to_le16_compile_time (GRUB_PE32_MACHINE_X86_64))
612 break;
613 if (type == IS_IA_EFI
614 && coff_head.machine !=
615 grub_cpu_to_le16_compile_time (GRUB_PE32_MACHINE_IA64))
616 break;
617 if (type == IS_ARM64_EFI
618 && coff_head.machine !=
619 grub_cpu_to_le16_compile_time (GRUB_PE32_MACHINE_ARM64))
620 break;
621 if (type == IS_ARM_EFI
622 && coff_head.machine !=
623 grub_cpu_to_le16_compile_time (GRUB_PE32_MACHINE_ARMTHUMB_MIXED))
624 break;
625 if (type == IS_RISCV_EFI
626 && coff_head.machine !=
627 grub_cpu_to_le16_compile_time (GRUB_PE32_MACHINE_RISCV64))
628 /* TODO: Determine bitness dynamically */
629 break;
630 if (type == IS_IA_EFI || type == IS_64_EFI || type == IS_ARM64_EFI ||
631 type == IS_RISCV_EFI)
632 {
633 struct grub_pe64_optional_header o64;
634 if (grub_file_read (file, &o64, sizeof (o64)) != sizeof (o64))
635 break;
636 if (o64.magic !=
637 grub_cpu_to_le16_compile_time (GRUB_PE32_PE64_MAGIC))
638 break;
639 if (o64.subsystem !=
640 grub_cpu_to_le16_compile_time
641 (GRUB_PE32_SUBSYSTEM_EFI_APPLICATION))
642 break;
643 ret = 1;
644 break;
645 }
646 if (type == IS_32_EFI || type == IS_ARM_EFI)
647 {
648 struct grub_pe32_optional_header o32;
649 if (grub_file_read (file, &o32, sizeof (o32)) != sizeof (o32))
650 break;
651 if (o32.magic !=
652 grub_cpu_to_le16_compile_time (GRUB_PE32_PE32_MAGIC))
653 break;
654 if (o32.subsystem !=
655 grub_cpu_to_le16_compile_time
656 (GRUB_PE32_SUBSYSTEM_EFI_APPLICATION))
657 break;
658 ret = 1;
659 break;
660 }
661 break;
662 }
663 }
664
665 if (elf)
666 grub_elf_close (elf);
667 else if (macho)
668 grub_macho_close (macho);
669 else if (file)
670 grub_file_close (file);
671
672 if (!ret && (grub_errno == GRUB_ERR_BAD_OS || grub_errno == GRUB_ERR_NONE))
673 /* TRANSLATORS: it's a standalone boolean value,
674 opposite of "true". */
675 grub_error (GRUB_ERR_TEST_FAILURE, N_("false"));
676 return grub_errno;
677 }
678
679 static grub_extcmd_t cmd;
680
681 GRUB_MOD_INIT(file)
682 {
683 cmd = grub_register_extcmd ("file", grub_cmd_file, 0,
684 N_("OPTIONS FILE"),
685 N_("Check if FILE is of specified type."),
686 options);
687 }
688
689 GRUB_MOD_FINI(file)
690 {
691 grub_unregister_extcmd (cmd);
692 }