file.c 18 KB

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