Merge branch 'vendor/OPENSSL'
[dragonfly.git] / usr.bin / doscmd / doscmd_loader.c
1 /*
2  * Copyright (c) 1992, 1993, 1996
3  *      Berkeley Software Design, Inc.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *      This product includes software developed by Berkeley Software
16  *      Design, Inc.
17  *
18  * THIS SOFTWARE IS PROVIDED BY Berkeley Software Design, Inc. ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED.  IN NO EVENT SHALL Berkeley Software Design, Inc. BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  *
30  *      BSDI doscmd_loader.c,v 2.3 1996/04/08 19:32:33 bostic Exp
31  *
32  * $FreeBSD: src/usr.bin/doscmd/doscmd_loader.c,v 1.2.2.1 2002/04/25 11:04:51 tg Exp $
33  * $DragonFly: src/usr.bin/doscmd/doscmd_loader.c,v 1.4 2004/01/22 03:22:52 rob Exp $
34  */
35
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <a.out.h>
39
40 /*
41  * reserve space in "low" memory for the interrupt vector table
42  */
43 static const char filler[4096] = { 0, };
44
45 #define _PATH_DOS_KERNEL_DIR    "/usr/libexec/"
46 #define _PATH_DOS_KERNEL        "doscmd.kernel"
47
48 int
49 load_kernel(void)
50 {
51     FILE *fp;
52     struct exec exec;
53     int start_address;
54
55     if ((fp = fopen(_PATH_DOS_KERNEL, "r")) == NULL &&
56         (fp = fopen("obj/" _PATH_DOS_KERNEL, "r")) == NULL &&
57         (fp = fopen(_PATH_DOS_KERNEL_DIR _PATH_DOS_KERNEL, "r")) == NULL &&
58         (fp = fopen(getenv("DOS_KERNEL"), "r")) == NULL)
59         err(1, "load_kernel");
60
61     if (fread(&exec, sizeof(exec), 1, fp) != 1 || N_GETMAGIC(exec) != OMAGIC)
62         errx(1, "bad kernel file format");
63
64     start_address = exec.a_entry & (~(getpagesize() - 1));
65     if (brk(start_address + exec.a_text + exec.a_data + exec.a_bss) < 0)
66         err(1, "load_kernel");
67     fread((char *)start_address, exec.a_text + exec.a_data, 1, fp);
68     bzero((char *)(start_address + exec.a_text + exec.a_data), exec.a_bss);
69     fclose(fp);
70     return(exec.a_entry);
71 }
72
73 void
74 main(int argc, char **argv, char **environ)
75 {
76     void (*entry_point)();
77 #ifndef __DragonFly__
78     int fd = open("/dev/mem", 0);
79 #endif
80     setgid(getgid());
81     setuid(getuid());
82
83 #ifndef __DragonFly__
84     if (fd < 0)
85         err(1, "/dev/mem");
86 #endif
87
88     entry_point = (void (*)()) load_kernel();
89
90 #ifndef __DragonFly__
91     if (read(fd, 0, 0x500 != 0x500))
92         err(1, "/dev/mem");
93
94     close(fd);
95 #endif
96
97     (*entry_point)(argc, argv, environ);
98     errx(1, "return from doscmd kernel???");
99 }