File: main.c

package info (click to toggle)
oskit 0.97.20000202-1
  • links: PTS
  • area: main
  • in suites: potato
  • size: 58,008 kB
  • ctags: 172,612
  • sloc: ansic: 832,827; asm: 7,640; sh: 3,920; yacc: 3,664; perl: 1,457; lex: 427; makefile: 337; csh: 141; awk: 78
file content (335 lines) | stat: -rw-r--r-- 9,696 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
/*
 * Copyright (c) 1995-1999 University of Utah and the Flux Group.
 * All rights reserved.
 * 
 * This file is part of the Flux OSKit.  The OSKit is free software, also known
 * as "open source;" you can redistribute it and/or modify it under the terms
 * of the GNU General Public License (GPL), version 2, as published by the Free
 * Software Foundation (FSF).  To explore alternate licensing terms, contact
 * the University of Utah at csl-dist@cs.utah.edu or +1-801-585-3271.
 * 
 * The OSKit is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE.  See the GPL for more details.  You should have
 * received a copy of the GPL along with the OSKit; see the file COPYING.  If
 * not, write to the FSF, 59 Temple Place #330, Boston, MA 02111-1307, USA.
 */

#include <oskit/x86/pc/rtc.h>		/* RTC_BASEHI */
#include <oskit/x86/pc/phys_lmm.h>	/* phys_lmm_add */
#include <oskit/x86/types.h>		/* oskit_addr_t */
#include <oskit/x86/base_cpu.h>		/* base_cpu_setup */
#include <oskit/x86/base_vm.h>		/* kvtophys */

#include <oskit/c/ctype.h>		/* isspace */
#include <oskit/c/stdlib.h>		/* malloc */
#include <oskit/c/string.h>		/* strcpy */
#include <oskit/c/stdio.h>		/* printf */
#include <oskit/c/assert.h>		/* assert */
#include <oskit/c/arpa/inet.h>		/* ntohl */
#include <oskit/c/malloc.h>		/* malloc_lmm */
#include <oskit/lmm.h>			/* lmm_remove_free */
#include <oskit/x86/pc/base_multiboot.h> /* boot_info */

#include <oskit/dev/dev.h>
#include <oskit/dev/osenv.h>
#include <oskit/dev/osenv_timer.h>
#include <oskit/clientos.h>

#include "getline.h"			/* getline */
#include "timer.h"			/* TIMER_FREQ */
#include "driver.h"			/* net_init */
#include "ether.h"			/* arptable */
#include "gdt.h"			/* copy_base_gdt */
#include "boot.h"			/* kerninfo */
#include "netboot.h"			/* netprintf */
#include "reboot.h"			/* reboot_stub_loc */

#define CMD_LINE_LEN 512

extern oskit_addr_t return_address;
extern char version[], build_info[];

/*
 * Figure out if -h (serial) or -f (fast serial) are in the oskit_bootargv
 * and put them in the default_args list.
 */
static void
init_default_args(int bootargc, char *bootargv[], char *default_args[])
{
	char **p;
	int i;

	p = &default_args[0];
	for (i = 0; i < bootargc; i++)
#define NDEFAULT_ARGS 2
		if (strcmp(bootargv[i], "-h") == 0 ||
		    strcmp(bootargv[i], "-f") == 0)
			*p++ = bootargv[i];
	*p = NULL;
}

/*
 * Build a command line from the program name, the default_args,
 * what they specified, and our -retaddr option.
 *
 * The command line is in a special format understood by the oskit
 * multiboot startup code (see base_multiboot_init_cmdline.c).
 * Basically, the format separates booting-options and environment
 * variable settings from normal args to pass to main().  The format is like
 *	progname [<booting-options and foo=bar> --] <args to main>
 * For example
 *	kernel DISPLAY=host:0 -d -- -rf foo bar
 */
static char *
build_cmdline(char *progname, char *default_args[NDEFAULT_ARGS], char *input)
{
	char *toks[input? strlen(input) : 0];
	char *tok;
	int ntoks;
	int i;
	int havesep;
	int cllen;
	char *cmdline;

	/* Tokenize their input. */
	ntoks = 0;
	if (input)
		for (tok = strtok(input, " \t"); tok; tok = strtok(0, " \t"))
			toks[ntoks++] = tok;

	/* Figure out if they specified a "--". */
	havesep = 0;
	for (i = 0; i < ntoks; i++)
		if (strcmp(toks[i], "--") == 0) {
			havesep = 1;
			break;
		}
	
	/*
	 * Toggle off any bootopts that they specified.
	 * We only have to do this if there is a --, since otherwise
	 * they didn't specify any bootopts.
	 */
	if (havesep) {
		for (i = 0; i < ntoks; i++) {
			int j;

			if (strcmp(toks[i], "--") == 0)
				break;
			for (j = 0; j < NDEFAULT_ARGS && default_args[j]; j++)
				if (strcmp(toks[i], default_args[j]) == 0) {
					toks[i] = 0;
					default_args[j] = 0;
				}
		}
	}

	/* Figure out how big the command-line will be and allocate it. */
	cllen = strlen(progname) + 1;
	cllen += strlen("-retaddr 0x00ff00ff") + 1;
	for (i = 0; i < NDEFAULT_ARGS; i++)
		if (default_args[i])
			cllen += strlen(default_args[i]) + 1;
	if (! havesep)
		cllen += strlen("--") + 1;
	for (i = 0; i < ntoks; i++)
		if (toks[i])
			cllen += strlen(toks[i]) + 1;
	cllen += 1;			/* for NUL */

	cmdline = mustcalloc(cllen,1);
	assert(cmdline);

	/*
	 * Now fill it in.
	 */
	strcpy(cmdline, progname);
	strcat(cmdline, " ");

	sprintf(cmdline + strlen(progname) + 1, "-retaddr 0x%08x",
		reboot_stub_loc);
	strcat(cmdline, " ");

	for (i = 0; i < NDEFAULT_ARGS; i++)
		if (default_args[i]) {
			strcat(cmdline, default_args[i]);
			strcat(cmdline, " ");
		}

	if (! havesep) {
		strcat(cmdline, "--");
		strcat(cmdline, " ");
	}
	
	for (i = 0; i < ntoks; i++)
		if (toks[i]) {
			strcat(cmdline, toks[i]);
			strcat(cmdline, " ");
		}

	return cmdline;
}

int
main(int argc, char *argv[])
{
	char buf[CMD_LINE_LEN];
	int cmdlinelen;
	oskit_error_t err;
	int done = 0;
	struct kerninfo kinfo;
	struct multiboot_info *mbi;
	char *input = buf;
	char *cmdline = NULL;
	int i;
	char *default_args[NDEFAULT_ARGS+1] = {0};
	char *argstring;
	oskit_osenv_t *osenv;
	oskit_osenv_timer_t *osenv_timer = 0;

	oskit_clientos_init();

	printf("%s (compiled %s)\n", version, __DATE__);

	osenv = oskit_osenv_create_default();
	oskit_register(&oskit_osenv_iid, (void *) osenv);
	oskit_dev_init(osenv);

	/*
	 * Need the osenv_timer interface.
	 */
	oskit_osenv_lookup(osenv,
			   &oskit_osenv_timer_iid, (void *) &osenv_timer);
	assert(osenv_timer);
	oskit_osenv_timer_register(osenv_timer, bumpticks, TIMER_FREQ);

	err = net_init(osenv);
	if (err)
		panic("Couldn't initialize network: 0x%08x", err);
	netprintf("I am %s (IP: %I, GW: %I, netmask: %I)\r\n",
		  hostname, arptable[ARP_CLIENT].ipaddr,
		  arptable[ARP_GATEWAY].ipaddr, netmask);

	init_default_args(oskit_bootargc, oskit_bootargv, default_args);
	
	/*
	 * Figure out our rootserver, dir to mount, and file to load.
	 * Then try to get it.
	 */
	printf("\nType \"help\" for help.\n\n");
reprompt:
	input = buf; /* In case we moved input forward to eat spaces */
	cmdlinelen = CMD_LINE_LEN;
	printf("NetBoot> ");
	getline(input, 512);
	input[strlen(input) - 1] = '\0';	/* chop \n */
	while (input[0] && isspace(input[0])) {
		input++;
		cmdlinelen--; /* record the amount of space avail. in buffer */
	}
	if (strcmp(input, "help") == 0) {
		printf("\n");
		printf("%s\n", version);
		printf("%s\n", build_info);
		printf("\n");
		printf("To boot, tell me the kernel file name and args in the form\n"
		       "\t<IPaddr|host>:<path> [<bootopts and env vars> --] [<program args>]\n"
		       "For example:\n"
		       "\t155.22.33.44:/home/foo/kernel -f -h DISPLAY=host:0 -- -wow\n"
		       "or\n"
		       "\tmyhost:/home/foo/kernel -x -y file1 file2\n"
		       "The directory must be an NFS exported directory.\n"
		       "The kernel must be a MultiBoot kernel.\n"
		       "Hostnames are currently limited to \"marker\" and \"fast\".\n");
		printf("\n");
		printf("Type \"quit\" or \"exit\" to %s.\n",
		       return_address? "return to caller" : "reboot");
		if (default_args[0]) {
			printf("\n");
			printf("The default bootopts are as follows and act as toggles:\n");
			printf("\t");
			for (i = 0; i < NDEFAULT_ARGS && default_args[i]; i++)
				printf("%s ", default_args[i]);
			printf("\n");
		}
		printf("\n");
		goto reprompt;
	}
	if (strcmp(input, "quit") == 0 || strcmp(input, "exit") == 0) {
		done = 1;
		goto done;
	}
	if (input[0] == '\0')
		goto reprompt;
	if (! parse_cmdline(input,
			    cmdlinelen,
			    &kinfo.ki_ip.s_addr,
			    &kinfo.ki_dirname,
			    &kinfo.ki_filename,
			    &argstring)) {
		printf("Bad command line, try again.\n");
		goto reprompt;
	}
	cmdline = build_cmdline(kinfo.ki_filename, default_args, argstring);
	netprintf("Root server: %I, dir: %s,\r\n"
		  "  file: %s,  cmdline: `%s'\r\n",
		  kinfo.ki_ip.s_addr, kinfo.ki_dirname, kinfo.ki_filename,
		  cmdline);
	memcpy(&arptable[ARP_ROOTSERVER].ipaddr, &kinfo.ki_ip.s_addr,
	       sizeof arptable[ARP_ROOTSERVER].ipaddr);
	if (getkernel_net(&kinfo) != 0) {
		printf("Can't get the kernel, try again.\n");
		free(cmdline);
		goto reprompt;
	}


	/*
	 * Clean some stuff up before we load the new image.
	 * In particular we don't want the timer hammering it,
	 * and don't want it using some bogus GDT (we may overwrite ours
	 * with the new image).
	 */
 done:
	net_shutdown();
	oskit_osenv_timer_shutdown(osenv_timer);
	copy_base_gdt();

	if (done)
		exit(0);

	/*
	 * Reserve the memory where the kernel will eventually reside.
	 * Future malloc's, like the one for the multiboot_info struct
	 * we pass to the new kernel, will stay out of this region.
	 *
	 * IMPORTANT NOTE: We must do this AFTER the shutdown stuff
	 * because all lmm_remove_free does is alloc all the free
	 * blocks in the given range.
	 * If things were already allocated in that range but get freed
	 * after the lmm_remove_free, they can be allocated again and
	 * then get clobbered by the kernel copy in do_boot and Bad Things
	 * will happen.
	 */
	lmm_remove_free(&malloc_lmm,
			(void *)phystokv(kinfo.ki_mbhdr.load_addr),
			kerninfo_imagesize(&kinfo));

	/*
	 * Allocate and fill in a multiboot_info struct we will pass.
	 */
	mbi = kinfo.ki_mbinfo = mustcalloc(sizeof(struct multiboot_info), 1);
	mbi->mem_lower = boot_info.mem_lower;
	mbi->mem_upper = boot_info.mem_upper;
	mbi->flags |= MULTIBOOT_MEMORY;
	if (cmdline) {
		mbi->cmdline = kvtophys(strdup(cmdline));
		mbi->flags |= MULTIBOOT_CMDLINE;
	}
	
	/* Load it. */
	loadkernel(&kinfo);
	panic("loadkernel returned");
	return 0; /* eat the warning */
}