file.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695
  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/xen_file.h>
  28. #include <grub/efi/pe32.h>
  29. #include <grub/arm/linux.h>
  30. #include <grub/arm64/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_arm_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_arm64_kernel_header lh;
  361. if (grub_file_read (file, &lh, sizeof (lh)) != sizeof (lh))
  362. break;
  363. if (lh.magic ==
  364. grub_cpu_to_le32_compile_time (GRUB_LINUX_ARM64_MAGIC_SIGNATURE))
  365. {
  366. ret = 1;
  367. break;
  368. }
  369. break;
  370. }
  371. case IS_PAE_DOMU ... IS_DOM0:
  372. {
  373. struct grub_xen_file_info xen_inf;
  374. elf = grub_xen_file (file);
  375. if (!elf)
  376. break;
  377. err = grub_xen_get_info (elf, &xen_inf);
  378. if (err)
  379. break;
  380. /* Unfortuntely no way to check if kernel supports dom0. */
  381. if (type == IS_DOM0)
  382. ret = 1;
  383. if (type == IS_PAE_DOMU)
  384. ret = (xen_inf.arch == GRUB_XEN_FILE_I386_PAE
  385. || xen_inf.arch == GRUB_XEN_FILE_I386_PAE_BIMODE);
  386. if (type == IS_64_DOMU)
  387. ret = (xen_inf.arch == GRUB_XEN_FILE_X86_64);
  388. break;
  389. }
  390. case IS_MULTIBOOT:
  391. case IS_MULTIBOOT2:
  392. {
  393. grub_uint32_t *buffer;
  394. grub_ssize_t len;
  395. grub_size_t search_size;
  396. grub_uint32_t *header;
  397. grub_uint32_t magic;
  398. grub_size_t step;
  399. if (type == IS_MULTIBOOT2)
  400. {
  401. search_size = 32768;
  402. magic = grub_cpu_to_le32_compile_time (0xe85250d6);
  403. step = 2;
  404. }
  405. else
  406. {
  407. search_size = 8192;
  408. magic = grub_cpu_to_le32_compile_time (0x1BADB002);
  409. step = 1;
  410. }
  411. buffer = grub_malloc (search_size);
  412. if (!buffer)
  413. break;
  414. len = grub_file_read (file, buffer, search_size);
  415. if (len < 32)
  416. {
  417. grub_free (buffer);
  418. break;
  419. }
  420. /* Look for the multiboot header in the buffer. The header should
  421. be at least 12 bytes and aligned on a 4-byte boundary. */
  422. for (header = buffer;
  423. ((char *) header <=
  424. (char *) buffer + len - (type == IS_MULTIBOOT2 ? 16 : 12));
  425. header += step)
  426. {
  427. if (header[0] == magic
  428. && !(grub_le_to_cpu32 (header[0])
  429. + grub_le_to_cpu32 (header[1])
  430. + grub_le_to_cpu32 (header[2])
  431. + (type == IS_MULTIBOOT2
  432. ? grub_le_to_cpu32 (header[3]) : 0)))
  433. {
  434. ret = 1;
  435. break;
  436. }
  437. }
  438. grub_free (buffer);
  439. break;
  440. }
  441. case IS_X86_LINUX32:
  442. case IS_X86_LINUX:
  443. {
  444. struct linux_i386_kernel_header lh;
  445. if (grub_file_read (file, &lh, sizeof (lh)) != sizeof (lh))
  446. break;
  447. if (lh.boot_flag != grub_cpu_to_le16_compile_time (0xaa55))
  448. break;
  449. if (lh.setup_sects > GRUB_LINUX_MAX_SETUP_SECTS)
  450. break;
  451. /* FIXME: some really old kernels (< 1.3.73) will fail this. */
  452. if (lh.header !=
  453. grub_cpu_to_le32_compile_time (GRUB_LINUX_I386_MAGIC_SIGNATURE)
  454. || grub_le_to_cpu16 (lh.version) < 0x0200)
  455. break;
  456. if (type == IS_X86_LINUX)
  457. {
  458. ret = 1;
  459. break;
  460. }
  461. /* FIXME: 2.03 is not always good enough (Linux 2.4 can be 2.03 and
  462. still not support 32-bit boot. */
  463. if (lh.header !=
  464. grub_cpu_to_le32_compile_time (GRUB_LINUX_I386_MAGIC_SIGNATURE)
  465. || grub_le_to_cpu16 (lh.version) < 0x0203)
  466. break;
  467. if (!(lh.loadflags & GRUB_LINUX_FLAG_BIG_KERNEL))
  468. break;
  469. ret = 1;
  470. break;
  471. }
  472. case IS_HIBERNATED:
  473. {
  474. grub_uint8_t hibr_file_magic[4];
  475. if (grub_file_read (file, &hibr_file_magic, sizeof (hibr_file_magic))
  476. != sizeof (hibr_file_magic))
  477. break;
  478. if (grub_memcmp ("hibr", hibr_file_magic, sizeof (hibr_file_magic)) ==
  479. 0
  480. || grub_memcmp ("HIBR", hibr_file_magic,
  481. sizeof (hibr_file_magic)) == 0)
  482. ret = 1;
  483. break;
  484. }
  485. case IS_XNU64:
  486. case IS_XNU32:
  487. {
  488. macho = grub_macho_open (args[0], GRUB_FILE_TYPE_XNU_KERNEL,
  489. (type == IS_XNU64));
  490. if (!macho)
  491. break;
  492. /* FIXME: more checks? */
  493. ret = 1;
  494. break;
  495. }
  496. case IS_XNU_HIBR:
  497. {
  498. struct grub_xnu_hibernate_header hibhead;
  499. if (grub_file_read (file, &hibhead, sizeof (hibhead))
  500. != sizeof (hibhead))
  501. break;
  502. if (hibhead.magic !=
  503. grub_cpu_to_le32_compile_time (GRUB_XNU_HIBERNATE_MAGIC))
  504. break;
  505. ret = 1;
  506. break;
  507. }
  508. case IS_32_EFI:
  509. case IS_64_EFI:
  510. case IS_IA_EFI:
  511. case IS_ARM64_EFI:
  512. case IS_ARM_EFI:
  513. case IS_RISCV32_EFI:
  514. case IS_RISCV64_EFI:
  515. {
  516. char signature[4];
  517. grub_uint32_t pe_offset;
  518. struct grub_pe32_coff_header coff_head;
  519. if (grub_file_read (file, signature, 2) != 2)
  520. break;
  521. if (signature[0] != 'M' || signature[1] != 'Z')
  522. break;
  523. if ((grub_ssize_t) grub_file_seek (file, 0x3c) == -1)
  524. break;
  525. if (grub_file_read (file, &pe_offset, 4) != 4)
  526. break;
  527. if ((grub_ssize_t) grub_file_seek (file, grub_le_to_cpu32 (pe_offset))
  528. == -1)
  529. break;
  530. if (grub_file_read (file, signature, 4) != 4)
  531. break;
  532. if (signature[0] != 'P' || signature[1] != 'E'
  533. || signature[2] != '\0' || signature[3] != '\0')
  534. break;
  535. if (grub_file_read (file, &coff_head, sizeof (coff_head))
  536. != sizeof (coff_head))
  537. break;
  538. if (type == IS_32_EFI
  539. && coff_head.machine !=
  540. grub_cpu_to_le16_compile_time (GRUB_PE32_MACHINE_I386))
  541. break;
  542. if (type == IS_64_EFI
  543. && coff_head.machine !=
  544. grub_cpu_to_le16_compile_time (GRUB_PE32_MACHINE_X86_64))
  545. break;
  546. if (type == IS_IA_EFI
  547. && coff_head.machine !=
  548. grub_cpu_to_le16_compile_time (GRUB_PE32_MACHINE_IA64))
  549. break;
  550. if (type == IS_ARM64_EFI
  551. && coff_head.machine !=
  552. grub_cpu_to_le16_compile_time (GRUB_PE32_MACHINE_ARM64))
  553. break;
  554. if (type == IS_ARM_EFI
  555. && coff_head.machine !=
  556. grub_cpu_to_le16_compile_time (GRUB_PE32_MACHINE_ARMTHUMB_MIXED))
  557. break;
  558. if ((type == IS_RISCV32_EFI || type == IS_RISCV64_EFI)
  559. && coff_head.machine !=
  560. grub_cpu_to_le16_compile_time (GRUB_PE32_MACHINE_RISCV64))
  561. /* TODO: Determine bitness dynamically */
  562. break;
  563. if (type == IS_IA_EFI || type == IS_64_EFI || type == IS_ARM64_EFI ||
  564. type == IS_RISCV32_EFI || type == IS_RISCV64_EFI)
  565. {
  566. struct grub_pe64_optional_header o64;
  567. if (grub_file_read (file, &o64, sizeof (o64)) != sizeof (o64))
  568. break;
  569. if (o64.magic !=
  570. grub_cpu_to_le16_compile_time (GRUB_PE32_PE64_MAGIC))
  571. break;
  572. if (o64.subsystem !=
  573. grub_cpu_to_le16_compile_time
  574. (GRUB_PE32_SUBSYSTEM_EFI_APPLICATION))
  575. break;
  576. ret = 1;
  577. break;
  578. }
  579. if (type == IS_32_EFI || type == IS_ARM_EFI)
  580. {
  581. struct grub_pe32_optional_header o32;
  582. if (grub_file_read (file, &o32, sizeof (o32)) != sizeof (o32))
  583. break;
  584. if (o32.magic !=
  585. grub_cpu_to_le16_compile_time (GRUB_PE32_PE32_MAGIC))
  586. break;
  587. if (o32.subsystem !=
  588. grub_cpu_to_le16_compile_time
  589. (GRUB_PE32_SUBSYSTEM_EFI_APPLICATION))
  590. break;
  591. ret = 1;
  592. break;
  593. }
  594. break;
  595. }
  596. }
  597. if (elf)
  598. grub_elf_close (elf);
  599. else if (macho)
  600. grub_macho_close (macho);
  601. else if (file)
  602. grub_file_close (file);
  603. if (!ret && (grub_errno == GRUB_ERR_BAD_OS || grub_errno == GRUB_ERR_NONE))
  604. /* TRANSLATORS: it's a standalone boolean value,
  605. opposite of "true". */
  606. grub_error (GRUB_ERR_TEST_FAILURE, N_("false"));
  607. return grub_errno;
  608. }
  609. static grub_extcmd_t cmd;
  610. GRUB_MOD_INIT(file)
  611. {
  612. cmd = grub_register_extcmd ("file", grub_cmd_file, 0,
  613. N_("OPTIONS FILE"),
  614. N_("Check if FILE is of specified type."),
  615. options);
  616. }
  617. GRUB_MOD_FINI(file)
  618. {
  619. grub_unregister_extcmd (cmd);
  620. }