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/i386/linux.h>
32
#include <grub/machoload.h>
33
#include <grub/fileid.h>
35
GRUB_MOD_LICENSE ("GPLv3+");
37
static const struct grub_arg_option options[] = {
38
{"is-i386-xen-pae-domu", 0, 0,
39
N_("Check if FILE can be booted as i386 PAE Xen unprivileged guest kernel"),
41
{"is-x86_64-xen-domu", 0, 0,
42
N_("Check if FILE can be booted as x86_64 Xen unprivileged guest kernel"), 0, 0},
43
{"is-x86-xen-dom0", 0, 0,
44
N_("Check if FILE can be used as Xen x86 privileged guest kernel"), 0, 0},
45
{"is-x86-multiboot", 0, 0,
46
N_("Check if FILE can be used as x86 multiboot kernel"), 0, 0},
47
{"is-x86-multiboot2", 0, 0,
48
N_("Check if FILE can be used as x86 multiboot2 kernel"), 0, 0},
49
{"is-arm-linux", 0, 0,
50
N_("Check if FILE is ARM Linux"), 0, 0},
51
{"is-arm64-linux", 0, 0,
52
N_("Check if FILE is ARM64 Linux"), 0, 0},
53
{"is-ia64-linux", 0, 0,
54
N_("Check if FILE is IA64 Linux"), 0, 0},
55
{"is-mips-linux", 0, 0,
56
N_("Check if FILE is MIPS Linux"), 0, 0},
57
{"is-mipsel-linux", 0, 0,
58
N_("Check if FILE is MIPSEL Linux"), 0, 0},
59
{"is-sparc64-linux", 0, 0,
60
N_("Check if FILE is SPARC64 Linux"), 0, 0},
61
{"is-powerpc-linux", 0, 0,
62
N_("Check if FILE is POWERPC Linux"), 0, 0},
63
{"is-x86-linux", 0, 0,
64
N_("Check if FILE is x86 Linux"), 0, 0},
65
{"is-x86-linux32", 0, 0,
66
N_("Check if FILE is x86 Linux supporting 32-bit protocol"), 0, 0},
67
{"is-x86-kfreebsd", 0, 0,
68
N_("Check if FILE is x86 kFreeBSD"), 0, 0},
69
{"is-i386-kfreebsd", 0, 0,
70
N_("Check if FILE is i386 kFreeBSD"), 0, 0},
71
{"is-x86_64-kfreebsd", 0, 0,
72
N_("Check if FILE is x86_64 kFreeBSD"), 0, 0},
74
{"is-x86-knetbsd", 0, 0,
75
N_("Check if FILE is x86 kNetBSD"), 0, 0},
76
{"is-i386-knetbsd", 0, 0,
77
N_("Check if FILE is i386 kNetBSD"), 0, 0},
78
{"is-x86_64-knetbsd", 0, 0,
79
N_("Check if FILE is x86_64 kNetBSD"), 0, 0},
82
N_("Check if FILE is i386 EFI file"), 0, 0},
83
{"is-x86_64-efi", 0, 0,
84
N_("Check if FILE is x86_64 EFI file"), 0, 0},
86
N_("Check if FILE is IA64 EFI file"), 0, 0},
87
{"is-arm64-efi", 0, 0,
88
N_("Check if FILE is ARM64 EFI file"), 0, 0},
90
N_("Check if FILE is ARM EFI file"), 0, 0},
91
{"is-hibernated-hiberfil", 0, 0,
92
N_("Check if FILE is hiberfil.sys in hibernated state"), 0, 0},
93
{"is-x86_64-xnu", 0, 0,
94
N_("Check if FILE is x86_64 XNU (Mac OS X kernel)"), 0, 0},
96
N_("Check if FILE is i386 XNU (Mac OS X kernel)"), 0, 0},
98
N_("Check if FILE is XNU (Mac OS X kernel) hibernated image"), 0, 0},
99
{"is-x86-bios-bootsector", 0, 0,
100
N_("Check if FILE is BIOS bootsector"), 0, 0},
136
OPT_TYPE_MIN = IS_PAE_DOMU,
137
OPT_TYPE_MAX = IS_BIOS_BOOTSECTOR
142
grub_cmd_file (grub_extcmd_context_t ctxt, int argc, char **args)
144
grub_file_t file = 0;
149
grub_macho_t macho = 0;
152
return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("filename expected"));
153
for (i = OPT_TYPE_MIN; i <= OPT_TYPE_MAX; i++)
154
if (ctxt->state[i].set)
161
return grub_error (GRUB_ERR_BAD_ARGUMENT, "multiple types specified");
164
return grub_error (GRUB_ERR_BAD_ARGUMENT, "no type specified");
166
file = grub_file_open (args[0]);
171
case IS_BIOS_BOOTSECTOR:
174
if (grub_file_size (file) != 512)
176
if (grub_file_seek (file, 510) == (grub_size_t) -1)
178
if (grub_file_read (file, &sig, 2) != 2)
180
if (sig != grub_cpu_to_le16_compile_time (0xaa55))
189
if (grub_file_read (file, &ehdr, sizeof (ehdr)) != sizeof (ehdr))
192
if (ehdr.e_ident[EI_MAG0] != ELFMAG0
193
|| ehdr.e_ident[EI_MAG1] != ELFMAG1
194
|| ehdr.e_ident[EI_MAG2] != ELFMAG2
195
|| ehdr.e_ident[EI_MAG3] != ELFMAG3
196
|| ehdr.e_ident[EI_VERSION] != EV_CURRENT
197
|| ehdr.e_version != EV_CURRENT)
200
if (ehdr.e_ident[EI_CLASS] != ELFCLASS64
201
|| ehdr.e_ident[EI_DATA] != ELFDATA2LSB
202
|| ehdr.e_machine != grub_cpu_to_le16_compile_time (EM_IA_64))
210
case IS_SPARC64_LINUX:
214
if (grub_file_read (file, &ehdr, sizeof (ehdr)) != sizeof (ehdr))
217
if (ehdr.e_ident[EI_MAG0] != ELFMAG0
218
|| ehdr.e_ident[EI_MAG1] != ELFMAG1
219
|| ehdr.e_ident[EI_MAG2] != ELFMAG2
220
|| ehdr.e_ident[EI_MAG3] != ELFMAG3
221
|| ehdr.e_ident[EI_VERSION] != EV_CURRENT
222
|| ehdr.e_version != EV_CURRENT)
225
if (ehdr.e_ident[EI_CLASS] != ELFCLASS64
226
|| ehdr.e_ident[EI_DATA] != ELFDATA2MSB)
229
if (ehdr.e_machine != grub_cpu_to_le16_compile_time (EM_SPARCV9)
230
|| ehdr.e_type != grub_cpu_to_be16_compile_time (ET_EXEC))
238
case IS_POWERPC_LINUX:
242
if (grub_file_read (file, &ehdr, sizeof (ehdr)) != sizeof (ehdr))
245
if (ehdr.e_ident[EI_MAG0] != ELFMAG0
246
|| ehdr.e_ident[EI_MAG1] != ELFMAG1
247
|| ehdr.e_ident[EI_MAG2] != ELFMAG2
248
|| ehdr.e_ident[EI_MAG3] != ELFMAG3
249
|| ehdr.e_ident[EI_VERSION] != EV_CURRENT
250
|| ehdr.e_version != EV_CURRENT)
253
if (ehdr.e_ident[EI_DATA] != ELFDATA2MSB
254
|| (ehdr.e_machine != grub_cpu_to_le16_compile_time (EM_PPC)
256
grub_cpu_to_le16_compile_time (EM_PPC64)))
259
if (ehdr.e_type != grub_cpu_to_be16_compile_time (ET_EXEC)
260
&& ehdr.e_type != grub_cpu_to_be16_compile_time (ET_DYN))
272
if (grub_file_read (file, &ehdr, sizeof (ehdr)) != sizeof (ehdr))
275
if (ehdr.e_ident[EI_MAG0] != ELFMAG0
276
|| ehdr.e_ident[EI_MAG1] != ELFMAG1
277
|| ehdr.e_ident[EI_MAG2] != ELFMAG2
278
|| ehdr.e_ident[EI_MAG3] != ELFMAG3
279
|| ehdr.e_ident[EI_VERSION] != EV_CURRENT
280
|| ehdr.e_version != EV_CURRENT)
283
if (ehdr.e_ident[EI_DATA] != ELFDATA2MSB
284
|| ehdr.e_machine != grub_cpu_to_be16_compile_time (EM_MIPS)
285
|| ehdr.e_type != grub_cpu_to_be16_compile_time (ET_EXEC))
294
case IS_X86_KNETBSD32:
295
case IS_X86_KNETBSD64:
299
elf = grub_elf_file (file, file->name);
301
if (elf->ehdr.ehdr32.e_type != grub_cpu_to_le16_compile_time (ET_EXEC)
302
|| elf->ehdr.ehdr32.e_ident[EI_DATA] != ELFDATA2LSB)
305
is32 = grub_elf_is_elf32 (elf);
306
is64 = grub_elf_is_elf64 (elf);
309
if (!is32 && type == IS_X86_KNETBSD32)
311
if (!is64 && type == IS_X86_KNETBSD64)
314
ret = grub_file_check_netbsd64 (elf);
316
ret = grub_file_check_netbsd32 (elf);
320
case IS_X86_KFREEBSD:
321
case IS_X86_KFREEBSD32:
322
case IS_X86_KFREEBSD64:
327
if (grub_file_read (file, &ehdr, sizeof (ehdr)) != sizeof (ehdr))
330
if (ehdr.e_ident[EI_MAG0] != ELFMAG0
331
|| ehdr.e_ident[EI_MAG1] != ELFMAG1
332
|| ehdr.e_ident[EI_MAG2] != ELFMAG2
333
|| ehdr.e_ident[EI_MAG3] != ELFMAG3
334
|| ehdr.e_ident[EI_VERSION] != EV_CURRENT
335
|| ehdr.e_version != EV_CURRENT)
338
if (ehdr.e_type != grub_cpu_to_le16_compile_time (ET_EXEC)
339
|| ehdr.e_ident[EI_DATA] != ELFDATA2LSB)
342
if (ehdr.e_ident[EI_OSABI] != ELFOSABI_FREEBSD)
345
is32 = (ehdr.e_machine == grub_cpu_to_le16_compile_time (EM_386)
346
&& ehdr.e_ident[EI_CLASS] == ELFCLASS32);
347
is64 = (ehdr.e_machine == grub_cpu_to_le16_compile_time (EM_X86_64)
348
&& ehdr.e_ident[EI_CLASS] == ELFCLASS64);
351
if (!is32 && (type == IS_X86_KFREEBSD32 || type == IS_X86_KNETBSD32))
353
if (!is64 && (type == IS_X86_KFREEBSD64 || type == IS_X86_KNETBSD64))
361
case IS_MIPSEL_LINUX:
365
if (grub_file_read (file, &ehdr, sizeof (ehdr)) != sizeof (ehdr))
368
if (ehdr.e_ident[EI_MAG0] != ELFMAG0
369
|| ehdr.e_ident[EI_MAG1] != ELFMAG1
370
|| ehdr.e_ident[EI_MAG2] != ELFMAG2
371
|| ehdr.e_ident[EI_MAG3] != ELFMAG3
372
|| ehdr.e_ident[EI_VERSION] != EV_CURRENT
373
|| ehdr.e_version != EV_CURRENT)
376
if (ehdr.e_machine != grub_cpu_to_le16_compile_time (EM_MIPS)
377
|| ehdr.e_type != grub_cpu_to_le16_compile_time (ET_EXEC))
386
grub_uint32_t sig, sig_pi;
387
if (grub_file_read (file, &sig_pi, 4) != 4)
390
if (sig_pi == grub_cpu_to_le32_compile_time (0xea000006))
396
if (grub_file_seek (file, 0x24) == (grub_size_t) -1)
398
if (grub_file_read (file, &sig, 4) != 4)
400
if (sig == grub_cpu_to_le32_compile_time (0x016f2818))
411
if (grub_file_seek (file, 0x38) == (grub_size_t) -1)
413
if (grub_file_read (file, &sig, 4) != 4)
415
if (sig == grub_cpu_to_le32_compile_time (0x644d5241))
422
case IS_PAE_DOMU ... IS_DOM0:
424
struct grub_xen_file_info xen_inf;
425
elf = grub_xen_file (file);
428
err = grub_xen_get_info (elf, &xen_inf);
431
/* Unfortuntely no way to check if kernel supports dom0. */
434
if (type == IS_PAE_DOMU)
435
ret = (xen_inf.arch == GRUB_XEN_FILE_I386_PAE
436
|| xen_inf.arch == GRUB_XEN_FILE_I386_PAE_BIMODE);
437
if (type == IS_64_DOMU)
438
ret = (xen_inf.arch == GRUB_XEN_FILE_X86_64);
444
grub_uint32_t *buffer;
446
grub_size_t search_size;
447
grub_uint32_t *header;
451
if (type == IS_MULTIBOOT2)
454
magic = grub_cpu_to_le32_compile_time (0xe85250d6);
460
magic = grub_cpu_to_le32_compile_time (0x1BADB002);
464
buffer = grub_malloc (search_size);
468
len = grub_file_read (file, buffer, search_size);
475
/* Look for the multiboot header in the buffer. The header should
476
be at least 12 bytes and aligned on a 4-byte boundary. */
477
for (header = buffer;
479
(char *) buffer + len - (type == IS_MULTIBOOT2 ? 16 : 12))
480
|| (header = 0); header += step)
482
if (header[0] == magic
483
&& !(grub_le_to_cpu32 (header[0])
484
+ grub_le_to_cpu32 (header[1])
485
+ grub_le_to_cpu32 (header[2])
486
+ (type == IS_MULTIBOOT2
487
? grub_le_to_cpu32 (header[3]) : 0)))
499
struct linux_kernel_header lh;
500
if (grub_file_read (file, &lh, sizeof (lh)) != sizeof (lh))
502
if (lh.boot_flag != grub_cpu_to_le16_compile_time (0xaa55))
505
if (lh.setup_sects > GRUB_LINUX_MAX_SETUP_SECTS)
508
/* FIXME: some really old kernels (< 1.3.73) will fail this. */
510
grub_cpu_to_le32_compile_time (GRUB_LINUX_MAGIC_SIGNATURE)
511
|| grub_le_to_cpu16 (lh.version) < 0x0200)
514
if (type == IS_X86_LINUX)
520
/* FIXME: 2.03 is not always good enough (Linux 2.4 can be 2.03 and
521
still not support 32-bit boot. */
523
grub_cpu_to_le32_compile_time (GRUB_LINUX_MAGIC_SIGNATURE)
524
|| grub_le_to_cpu16 (lh.version) < 0x0203)
527
if (!(lh.loadflags & GRUB_LINUX_FLAG_BIG_KERNEL))
534
grub_uint8_t hibr_file_magic[4];
535
if (grub_file_read (file, &hibr_file_magic, sizeof (hibr_file_magic))
536
!= sizeof (hibr_file_magic))
538
if (grub_memcmp ("hibr", hibr_file_magic, sizeof (hibr_file_magic)) ==
540
|| grub_memcmp ("HIBR", hibr_file_magic,
541
sizeof (hibr_file_magic)) == 0)
548
macho = grub_macho_open (args[0], (type == IS_XNU64));
551
/* FIXME: more checks? */
557
struct grub_xnu_hibernate_header hibhead;
558
if (grub_file_read (file, &hibhead, sizeof (hibhead))
562
grub_cpu_to_le32_compile_time (GRUB_XNU_HIBERNATE_MAGIC))
574
grub_uint32_t pe_offset;
575
struct grub_pe32_coff_header coff_head;
577
if (grub_file_read (file, signature, 2) != 2)
579
if (signature[0] != 'M' || signature[1] != 'Z')
581
if ((grub_ssize_t) grub_file_seek (file, 0x3c) == -1)
583
if (grub_file_read (file, &pe_offset, 4) != 4)
585
if ((grub_ssize_t) grub_file_seek (file, grub_le_to_cpu32 (pe_offset))
588
if (grub_file_read (file, signature, 4) != 4)
590
if (signature[0] != 'P' || signature[1] != 'E'
591
|| signature[2] != '\0' || signature[3] != '\0')
594
if (grub_file_read (file, &coff_head, sizeof (coff_head))
595
!= sizeof (coff_head))
597
if (type == IS_32_EFI
598
&& coff_head.machine !=
599
grub_cpu_to_le16_compile_time (GRUB_PE32_MACHINE_I386))
601
if (type == IS_64_EFI
602
&& coff_head.machine !=
603
grub_cpu_to_le16_compile_time (GRUB_PE32_MACHINE_X86_64))
605
if (type == IS_IA_EFI
606
&& coff_head.machine !=
607
grub_cpu_to_le16_compile_time (GRUB_PE32_MACHINE_IA64))
609
if (type == IS_ARM64_EFI
610
&& coff_head.machine !=
611
grub_cpu_to_le16_compile_time (GRUB_PE32_MACHINE_ARM64))
613
if (type == IS_ARM_EFI
614
&& coff_head.machine !=
615
grub_cpu_to_le16_compile_time (GRUB_PE32_MACHINE_ARMTHUMB_MIXED))
617
if (type == IS_IA_EFI || type == IS_64_EFI || type == IS_ARM64_EFI)
619
struct grub_pe64_optional_header o64;
620
if (grub_file_read (file, &o64, sizeof (o64)) != sizeof (o64))
623
grub_cpu_to_le16_compile_time (GRUB_PE32_PE64_MAGIC))
626
grub_cpu_to_le16_compile_time
627
(GRUB_PE32_SUBSYSTEM_EFI_APPLICATION))
632
if (type == IS_32_EFI || type == IS_ARM_EFI)
634
struct grub_pe32_optional_header o32;
635
if (grub_file_read (file, &o32, sizeof (o32)) != sizeof (o32))
638
grub_cpu_to_le16_compile_time (GRUB_PE32_PE32_MAGIC))
641
grub_cpu_to_le16_compile_time
642
(GRUB_PE32_SUBSYSTEM_EFI_APPLICATION))
652
grub_elf_close (elf);
654
grub_macho_close (macho);
656
grub_file_close (file);
658
if (!ret && (grub_errno == GRUB_ERR_BAD_OS || grub_errno == GRUB_ERR_NONE))
659
/* TRANSLATORS: it's a standalone boolean value,
660
opposite of "true". */
661
grub_error (GRUB_ERR_TEST_FAILURE, N_("false"));
665
static grub_extcmd_t cmd;
669
cmd = grub_register_extcmd ("file", grub_cmd_file, 0,
671
N_("Check if FILE is of specified type."),
677
grub_unregister_extcmd (cmd);