Initial import from FreeBSD RELENG_4:
[dragonfly.git] / sys / i386 / boot / dosboot / boot.c
1 /*\r
2  * Mach Operating System\r
3  * Copyright (c) 1992, 1991 Carnegie Mellon University\r
4  * All Rights Reserved.\r
5  * \r
6  * Permission to use, copy, modify and distribute this software and its\r
7  * documentation is hereby granted, provided that both the copyright\r
8  * notice and this permission notice appear in all copies of the\r
9  * software, derivative works or modified versions, and any portions\r
10  * thereof, and that both notices appear in supporting documentation.\r
11  * \r
12  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"\r
13  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR\r
14  * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.\r
15  * \r
16  * Carnegie Mellon requests users of this software to return to\r
17  * \r
18  *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU\r
19  *  School of Computer Science\r
20  *  Carnegie Mellon University\r
21  *  Pittsburgh PA 15213-3890\r
22  * \r
23  * any improvements or extensions that they make and grant Carnegie Mellon\r
24  * the rights to redistribute these changes.\r
25  *\r
26  *      from: Mach, [92/04/03  16:51:14  rvb]\r
27  * $FreeBSD: src/sys/i386/boot/dosboot/boot.c,v 1.5 1999/08/28 00:43:19 peter Exp $\r
28  */\r
29 \r
30 \r
31 /*\r
32   Copyright 1988, 1989, 1990, 1991, 1992 \r
33    by Intel Corporation, Santa Clara, California.\r
34 \r
35                 All Rights Reserved\r
36 \r
37 Permission to use, copy, modify, and distribute this software and\r
38 its documentation for any purpose and without fee is hereby\r
39 granted, provided that the above copyright notice appears in all\r
40 copies and that both the copyright notice and this permission notice\r
41 appear in supporting documentation, and that the name of Intel\r
42 not be used in advertising or publicity pertaining to distribution\r
43 of the software without specific, written prior permission.\r
44 \r
45 INTEL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE\r
46 INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS,\r
47 IN NO EVENT SHALL INTEL BE LIABLE FOR ANY SPECIAL, INDIRECT, OR\r
48 CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM\r
49 LOSS OF USE, DATA OR PROFITS, WHETHER IN ACTION OF CONTRACT,\r
50 NEGLIGENCE, OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION\r
51 WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.\r
52 */\r
53 #include <stdio.h>\r
54 #include <conio.h>\r
55 #include <process.h>\r
56 #include <memory.h>\r
57 \r
58 #include "bootinfo.h"\r
59 #include "protmod.h"\r
60 #include "param.h"\r
61 #include "boot.h"\r
62 #include "reboot.h"\r
63 \r
64 #include "exec.h"\r
65 \r
66 int openrd(char *kernel);\r
67 void ufs_read(char *buffer, long count);\r
68 void xread(unsigned long addr, long size);\r
69 \r
70 static struct exec head;\r
71 static long argv[10];\r
72 static char buf[__LDPGSZ];\r
73 static long int startaddr;\r
74 \r
75 void pbzero(unsigned long addr, unsigned long size)\r
76 {\r
77         long s;\r
78 \r
79         memset(buf, 0, __LDPGSZ);\r
80         while (size) {\r
81                 s = size > __LDPGSZ ? __LDPGSZ : size;\r
82                 pm_copy(buf, addr, s);\r
83                 size -= s;\r
84                 addr += s;\r
85         }\r
86 }\r
87 \r
88 static long loadprog(long *hsize)\r
89 {\r
90         long addr;      /* physical address.. not directly useable */\r
91         long hmaddress;\r
92         unsigned long pad;\r
93         long i;\r
94         static int (*x_entry)() = 0;\r
95 \r
96         ufs_read(&head, (long) sizeof(head));\r
97         if (N_BADMAG(head)) {\r
98                 printf("Invalid format!\n");\r
99                 exit(0);\r
100         }\r
101 \r
102         startaddr = (long)head.a_entry;\r
103         addr = (startaddr & 0x00ffffffl); /* some MEG boundary */\r
104         printf("Booting @ 0x%lx\n", addr);\r
105         if(addr < 0x100000l)\r
106         {\r
107                 printf("Start address too low!\n");\r
108                 exit(0);\r
109         }\r
110 \r
111         poff = N_TXTOFF(head)+head.a_text+head.a_data+head.a_syms;\r
112         ufs_read((void *)&i, sizeof(long));\r
113         *hsize = head.a_text+head.a_data+head.a_bss;\r
114         *hsize = (*hsize+NBPG-1)&~(NBPG-1);\r
115         *hsize += i+4+head.a_syms;\r
116         addr=hmaddress=get_high_memory(*hsize);\r
117         if (!hmaddress) {\r
118                 printf("Sorry, can't allocate enough memory!\n");\r
119                 exit(0);\r
120         }\r
121 \r
122         poff = N_TXTOFF(head);\r
123 \r
124         /********************************************************/\r
125         /* LOAD THE TEXT SEGMENT                                */\r
126         /********************************************************/\r
127         printf("text=0x%lx ", head.a_text);\r
128         xread(addr, head.a_text);\r
129         addr += head.a_text;\r
130 \r
131         /********************************************************/\r
132         /* Load the Initialised data after the text             */\r
133         /********************************************************/\r
134         while (addr & CLOFSET)\r
135                 pm_copy("\0", addr++, 1);\r
136 \r
137         printf("data=0x%lx ", head.a_data);\r
138         xread(addr, head.a_data);\r
139         addr += head.a_data;\r
140 \r
141         /********************************************************/\r
142         /* Skip over the uninitialised data                     */\r
143         /* (but clear it)                                       */\r
144         /********************************************************/\r
145         printf("bss=0x%lx ", head.a_bss);\r
146         pbzero(addr, head.a_bss);\r
147         addr += head.a_bss;\r
148 \r
149         /* Pad to a page boundary. */\r
150         pad = (unsigned long)(addr-hmaddress+(startaddr & 0x00ffffffl)) % NBPG;\r
151         if (pad != 0) {\r
152                 pad = NBPG - pad;\r
153                 addr += pad;\r
154         }\r
155         bootinfo.bi_symtab = addr-hmaddress+(startaddr & 0x00ffffffl);\r
156 \r
157         /********************************************************/\r
158         /* Copy the symbol table size                           */\r
159         /********************************************************/\r
160         pm_copy((char *)&head.a_syms, addr, sizeof(head.a_syms));\r
161         addr += sizeof(head.a_syms);\r
162 \r
163         /********************************************************/\r
164         /* Load the symbol table                                */\r
165         /********************************************************/\r
166         printf("symbols=[+0x%lx+0x%lx+0x%lx", pad, (long) sizeof(head.a_syms),\r
167                (long) head.a_syms);\r
168         xread(addr, head.a_syms);\r
169         addr += head.a_syms;\r
170 \r
171         /********************************************************/\r
172         /* Load the string table size                           */\r
173         /********************************************************/\r
174         ufs_read((void *)&i, sizeof(long));\r
175         pm_copy((char *)&i, addr, sizeof(long));\r
176         i -= sizeof(long);\r
177         addr += sizeof(long);\r
178 \r
179         /********************************************************/\r
180         /* Load the string table                                */\r
181         /********************************************************/\r
182         printf("+0x%x+0x%lx] ", sizeof(long), i);\r
183         xread(addr, i);\r
184         addr += i;\r
185 \r
186         bootinfo.bi_esymtab = addr-hmaddress+(startaddr & 0x00ffffffl);\r
187 \r
188         /*\r
189          * For backwards compatibility, use the previously-unused adaptor\r
190          * and controller bitfields to hold the slice number.\r
191          */\r
192         printf("total=0x%lx entry point=0x%lx\n",\r
193                 addr-hmaddress+(startaddr & 0x00ffffffl),\r
194                 startaddr & 0x00ffffffl);\r
195 \r
196         return hmaddress;\r
197 }\r
198 \r
199 void bsdboot(int drive, long loadflags, char *kernel)\r
200 {\r
201         long hmaddress, size, bootdev;\r
202 \r
203         /***************************************************************\\r
204         * As a default set it to the first partition of the first       *\r
205         * floppy or hard drive                                          *\r
206         \***************************************************************/\r
207         part = unit = 0;\r
208         maj = (drive&0x80 ? 0 : 2);             /* a good first bet */\r
209 \r
210         if (openrd(kernel)) {\r
211                 printf("Can't find %s\n", kernel);\r
212                 exit(0);\r
213         }\r
214         hmaddress = loadprog(&size);\r
215         bootdev = MAKEBOOTDEV(maj, (slice >> 4), slice & 0xf, unit, part);\r
216         startprog(hmaddress, size, ((long)startaddr & 0xffffffl),\r
217                           loadflags | RB_BOOTINFO, bootdev);\r
218 }\r