2 * GRUB -- GRand Unified Bootloader
3 * Copyright (C) 2013 Free Software Foundation, Inc.
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.
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.
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/>.
20 #include <grub/misc.h>
23 #include <grub/command.h>
24 #include <grub/extcmd.h>
25 #include <grub/i18n.h>
26 #include <grub/file.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>
34 #include <grub/machoload.h>
35 #include <grub/fileid.h>
37 GRUB_MOD_LICENSE ("GPLv3+");
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"),
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},
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},
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},
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},
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},
143 OPT_TYPE_MIN
= IS_PAE_DOMU
,
144 OPT_TYPE_MAX
= IS_BIOS_BOOTSECTOR
149 grub_cmd_file (grub_extcmd_context_t ctxt
, int argc
, char **args
)
151 grub_file_t file
= 0;
156 grub_macho_t macho
= 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
)
168 return grub_error (GRUB_ERR_BAD_ARGUMENT
, "multiple types specified");
171 return grub_error (GRUB_ERR_BAD_ARGUMENT
, "no type specified");
173 file
= grub_file_open (args
[0], GRUB_FILE_TYPE_XNU_KERNEL
);
178 case IS_BIOS_BOOTSECTOR
:
181 if (grub_file_size (file
) != 512)
183 if (grub_file_seek (file
, 510) == (grub_size_t
) -1)
185 if (grub_file_read (file
, &sig
, 2) != 2)
187 if (sig
!= grub_cpu_to_le16_compile_time (0xaa55))
196 if (grub_file_read (file
, &ehdr
, sizeof (ehdr
)) != sizeof (ehdr
))
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
)
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
))
217 case IS_SPARC64_LINUX
:
221 if (grub_file_read (file
, &ehdr
, sizeof (ehdr
)) != sizeof (ehdr
))
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
)
232 if (ehdr
.e_ident
[EI_CLASS
] != ELFCLASS64
233 || ehdr
.e_ident
[EI_DATA
] != ELFDATA2MSB
)
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
))
245 case IS_POWERPC_LINUX
:
249 if (grub_file_read (file
, &ehdr
, sizeof (ehdr
)) != sizeof (ehdr
))
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
)
260 if (ehdr
.e_ident
[EI_DATA
] != ELFDATA2MSB
261 || (ehdr
.e_machine
!= grub_cpu_to_le16_compile_time (EM_PPC
)
263 grub_cpu_to_le16_compile_time (EM_PPC64
)))
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
))
279 if (grub_file_read (file
, &ehdr
, sizeof (ehdr
)) != sizeof (ehdr
))
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
)
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
))
301 case IS_X86_KNETBSD32
:
302 case IS_X86_KNETBSD64
:
306 elf
= grub_elf_file (file
, file
->name
);
308 if (elf
->ehdr
.ehdr32
.e_type
!= grub_cpu_to_le16_compile_time (ET_EXEC
)
309 || elf
->ehdr
.ehdr32
.e_ident
[EI_DATA
] != ELFDATA2LSB
)
312 is32
= grub_elf_is_elf32 (elf
);
313 is64
= grub_elf_is_elf64 (elf
);
316 if (!is32
&& type
== IS_X86_KNETBSD32
)
318 if (!is64
&& type
== IS_X86_KNETBSD64
)
321 ret
= grub_file_check_netbsd64 (elf
);
323 ret
= grub_file_check_netbsd32 (elf
);
327 case IS_X86_KFREEBSD
:
328 case IS_X86_KFREEBSD32
:
329 case IS_X86_KFREEBSD64
:
334 if (grub_file_read (file
, &ehdr
, sizeof (ehdr
)) != sizeof (ehdr
))
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
)
345 if (ehdr
.e_type
!= grub_cpu_to_le16_compile_time (ET_EXEC
)
346 || ehdr
.e_ident
[EI_DATA
] != ELFDATA2LSB
)
349 if (ehdr
.e_ident
[EI_OSABI
] != ELFOSABI_FREEBSD
)
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
);
358 if (!is32
&& (type
== IS_X86_KFREEBSD32
|| type
== IS_X86_KNETBSD32
))
360 if (!is64
&& (type
== IS_X86_KFREEBSD64
|| type
== IS_X86_KNETBSD64
))
368 case IS_MIPSEL_LINUX
:
372 if (grub_file_read (file
, &ehdr
, sizeof (ehdr
)) != sizeof (ehdr
))
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
)
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
))
393 struct linux_arm_kernel_header lh
;
395 if (grub_file_read (file
, &lh
, sizeof (lh
)) != sizeof (lh
))
397 /* Short forward branch in A32 state (for Raspberry pi kernels). */
398 if (lh
.code0
== grub_cpu_to_le32_compile_time (0xea000006))
405 grub_cpu_to_le32_compile_time (GRUB_LINUX_ARM_MAGIC_SIGNATURE
))
414 struct linux_arm64_kernel_header lh
;
416 if (grub_file_read (file
, &lh
, sizeof (lh
)) != sizeof (lh
))
420 grub_cpu_to_le32_compile_time (GRUB_LINUX_ARM64_MAGIC_SIGNATURE
))
427 case IS_PAE_DOMU
... IS_DOM0
:
429 struct grub_xen_file_info xen_inf
;
430 elf
= grub_xen_file (file
);
433 err
= grub_xen_get_info (elf
, &xen_inf
);
436 /* Unfortuntely no way to check if kernel supports dom0. */
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
);
449 grub_uint32_t
*buffer
;
451 grub_size_t search_size
;
452 grub_uint32_t
*header
;
456 if (type
== IS_MULTIBOOT2
)
459 magic
= grub_cpu_to_le32_compile_time (0xe85250d6);
465 magic
= grub_cpu_to_le32_compile_time (0x1BADB002);
469 buffer
= grub_malloc (search_size
);
473 len
= grub_file_read (file
, buffer
, search_size
);
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
;
484 (char *) buffer
+ len
- (type
== IS_MULTIBOOT2
? 16 : 12));
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)))
505 struct linux_i386_kernel_header lh
;
506 if (grub_file_read (file
, &lh
, sizeof (lh
)) != sizeof (lh
))
508 if (lh
.boot_flag
!= grub_cpu_to_le16_compile_time (0xaa55))
511 if (lh
.setup_sects
> GRUB_LINUX_MAX_SETUP_SECTS
)
514 /* FIXME: some really old kernels (< 1.3.73) will fail this. */
516 grub_cpu_to_le32_compile_time (GRUB_LINUX_I386_MAGIC_SIGNATURE
)
517 || grub_le_to_cpu16 (lh
.version
) < 0x0200)
520 if (type
== IS_X86_LINUX
)
526 /* FIXME: 2.03 is not always good enough (Linux 2.4 can be 2.03 and
527 still not support 32-bit boot. */
529 grub_cpu_to_le32_compile_time (GRUB_LINUX_I386_MAGIC_SIGNATURE
)
530 || grub_le_to_cpu16 (lh
.version
) < 0x0203)
533 if (!(lh
.loadflags
& GRUB_LINUX_FLAG_BIG_KERNEL
))
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
))
544 if (grub_memcmp ("hibr", hibr_file_magic
, sizeof (hibr_file_magic
)) ==
546 || grub_memcmp ("HIBR", hibr_file_magic
,
547 sizeof (hibr_file_magic
)) == 0)
554 macho
= grub_macho_open (args
[0], GRUB_FILE_TYPE_XNU_KERNEL
,
558 /* FIXME: more checks? */
564 struct grub_xnu_hibernate_header hibhead
;
565 if (grub_file_read (file
, &hibhead
, sizeof (hibhead
))
569 grub_cpu_to_le32_compile_time (GRUB_XNU_HIBERNATE_MAGIC
))
582 grub_uint32_t pe_offset
;
583 struct grub_pe32_coff_header coff_head
;
585 if (grub_file_read (file
, signature
, 2) != 2)
587 if (signature
[0] != 'M' || signature
[1] != 'Z')
589 if ((grub_ssize_t
) grub_file_seek (file
, 0x3c) == -1)
591 if (grub_file_read (file
, &pe_offset
, 4) != 4)
593 if ((grub_ssize_t
) grub_file_seek (file
, grub_le_to_cpu32 (pe_offset
))
596 if (grub_file_read (file
, signature
, 4) != 4)
598 if (signature
[0] != 'P' || signature
[1] != 'E'
599 || signature
[2] != '\0' || signature
[3] != '\0')
602 if (grub_file_read (file
, &coff_head
, sizeof (coff_head
))
603 != sizeof (coff_head
))
605 if (type
== IS_32_EFI
606 && coff_head
.machine
!=
607 grub_cpu_to_le16_compile_time (GRUB_PE32_MACHINE_I386
))
609 if (type
== IS_64_EFI
610 && coff_head
.machine
!=
611 grub_cpu_to_le16_compile_time (GRUB_PE32_MACHINE_X86_64
))
613 if (type
== IS_IA_EFI
614 && coff_head
.machine
!=
615 grub_cpu_to_le16_compile_time (GRUB_PE32_MACHINE_IA64
))
617 if (type
== IS_ARM64_EFI
618 && coff_head
.machine
!=
619 grub_cpu_to_le16_compile_time (GRUB_PE32_MACHINE_ARM64
))
621 if (type
== IS_ARM_EFI
622 && coff_head
.machine
!=
623 grub_cpu_to_le16_compile_time (GRUB_PE32_MACHINE_ARMTHUMB_MIXED
))
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 */
630 if (type
== IS_IA_EFI
|| type
== IS_64_EFI
|| type
== IS_ARM64_EFI
||
631 type
== IS_RISCV_EFI
)
633 struct grub_pe64_optional_header o64
;
634 if (grub_file_read (file
, &o64
, sizeof (o64
)) != sizeof (o64
))
637 grub_cpu_to_le16_compile_time (GRUB_PE32_PE64_MAGIC
))
640 grub_cpu_to_le16_compile_time
641 (GRUB_PE32_SUBSYSTEM_EFI_APPLICATION
))
646 if (type
== IS_32_EFI
|| type
== IS_ARM_EFI
)
648 struct grub_pe32_optional_header o32
;
649 if (grub_file_read (file
, &o32
, sizeof (o32
)) != sizeof (o32
))
652 grub_cpu_to_le16_compile_time (GRUB_PE32_PE32_MAGIC
))
655 grub_cpu_to_le16_compile_time
656 (GRUB_PE32_SUBSYSTEM_EFI_APPLICATION
))
666 grub_elf_close (elf
);
668 grub_macho_close (macho
);
670 grub_file_close (file
);
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"));
679 static grub_extcmd_t cmd
;
683 cmd
= grub_register_extcmd ("file", grub_cmd_file
, 0,
685 N_("Check if FILE is of specified type."),
691 grub_unregister_extcmd (cmd
);