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