1 // SPDX-License-Identifier: GPL-2.0-only
3 * linux/fs/binfmt_em86.c
5 * Based on linux/fs/binfmt_script.c
6 * Copyright (C) 1996 Martin von Löwis
7 * original #!-checking implemented by tytso.
9 * em86 changes Copyright (C) 1997 Jim Paradis
12 #include <linux/module.h>
13 #include <linux/string.h>
14 #include <linux/stat.h>
15 #include <linux/binfmts.h>
16 #include <linux/elf.h>
17 #include <linux/init.h>
19 #include <linux/file.h>
20 #include <linux/errno.h>
23 #define EM86_INTERP "/usr/bin/em86"
24 #define EM86_I_NAME "em86"
26 static int load_em86(struct linux_binprm *bprm)
28 const char *i_name, *i_arg;
34 /* Make sure this is a Linux/Intel ELF executable... */
35 elf_ex = *((struct elfhdr *)bprm->buf);
37 if (memcmp(elf_ex.e_ident, ELFMAG, SELFMAG) != 0)
40 /* First of all, some simple consistency checks */
41 if ((elf_ex.e_type != ET_EXEC && elf_ex.e_type != ET_DYN) ||
42 (!((elf_ex.e_machine == EM_386) || (elf_ex.e_machine == EM_486))) ||
43 !bprm->file->f_op->mmap) {
47 /* Need to be able to load the file after exec */
48 if (bprm->interp_flags & BINPRM_FLAGS_PATH_INACCESSIBLE)
51 /* Unlike in the script case, we don't have to do any hairy
52 * parsing to find our interpreter... it's hardcoded!
56 i_arg = NULL; /* We reserve the right to add an arg later */
59 * Splice in (1) the interpreter's name for argv[0]
60 * (2) (optional) argument to interpreter
61 * (3) filename of emulated file (replace argv[0])
63 * This is done in reverse order, because of how the
64 * user environment and arguments are stored.
66 remove_arg_zero(bprm);
67 retval = copy_string_kernel(bprm->filename, bprm);
68 if (retval < 0) return retval;
71 retval = copy_string_kernel(i_arg, bprm);
72 if (retval < 0) return retval;
75 retval = copy_string_kernel(i_name, bprm);
76 if (retval < 0) return retval;
80 * OK, now restart the process with the interpreter's inode.
81 * Note that we use open_exec() as the name is now in kernel
82 * space, and we don't need to copy it.
84 file = open_exec(interp);
88 bprm->interpreter = file;
92 static struct linux_binfmt em86_format = {
93 .module = THIS_MODULE,
94 .load_binary = load_em86,
97 static int __init init_em86_binfmt(void)
99 register_binfmt(&em86_format);
103 static void __exit exit_em86_binfmt(void)
105 unregister_binfmt(&em86_format);
108 core_initcall(init_em86_binfmt);
109 module_exit(exit_em86_binfmt);
110 MODULE_LICENSE("GPL");