summaryrefslogtreecommitdiff
path: root/os/init/bootinit.b
blob: 0fcd58ef900584baaa7909b2022d5cc7bb2a1d67 (plain)
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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
#
# Generalized boot Inferno
#

implement Init;

include "sys.m";
	sys:	Sys;

include "draw.m";

include "keyring.m";
	kr: Keyring;

include "security.m";
	auth: Auth;
	random: Random;

include "tftp.m";

Bootpreadlen: con 128;

Init: module
{
	init:	fn();
};

ip: string;
mask: string;
fsip: string;
bootprotocol: string;
bootserver: string;
bootfile: string;

debug: con 0;

init()
{
	ipavailable: int;
	sys = load Sys Sys->PATH;

	kexecfd := sys->open("#B/kexec", Sys->OWRITE);
	if (kexecfd == nil)
		fatal(sys->sprint("opening #B/kexec: %r"));

	ipavailable = 0;
	if (dobind("#l", "/net", sys->MREPL) && dobind("#I", "/net", sys->MAFTER))
		ipavailable = 1;

	dobind("#c", "/dev", sys->MAFTER); 	# console device

	if (!ipavailable)
		fatal("no IP stack available");
	cfd := sys->open("/net/ipifc/clone", sys->ORDWR);
	if(cfd == nil)
		fatal(sys->sprint("open /net/ipifc/clone: %r"));

	if (sys->fprint(cfd, "bind ether ether0") < 0)
		fatal(sys->sprint("binding ether0: %r"));

	fsready := 0;

	fsip = ipconfig(cfd);

	bootstring := getenvdefault("bootpath", "tftp");

	(bootprotocol, bootserver, bootfile) = parsebootstring(bootstring);

	if (bootprotocol == nil)
		fatal(bootstring + ": unrecognised syntax");

	# Run dhcp if necessary
	if (bootprotocol == "tftp" && (bootserver == nil || bootfile == nil))
		dhcp();

	# determine server
	if (bootprotocol == "net" && bootserver == nil)
		bootserver = fsip;

	if (bootserver == nil)
		fatal("couldn't determine boot server");

	if (bootfile == nil)
		fatal("couldn't determine boot file");

	if (bootprotocol == nil)
		fatal("couldn't determine boot protocol");

	sys->print("loading %s!%s!%s\n", bootprotocol, bootserver, bootfile);

	if (bootprotocol == "net") {
		sys->print("Attempting remote mount\n");
		if (netfs(bootserver) == 0)
			sys->print("Remote mount successful\n");
		else
			fatal(sys->sprint("Remote mount failed: %r"));
		fd := sys->open("/n/remote" + bootfile, Sys->OREAD);
		if (fd == nil)
			fatal(sys->sprint("%s:/n/remote%s: %r", bootserver, bootfile));
		if (sys->stream(fd, kexecfd, 4096) < 0)
			fatal(sys->sprint("copying %s: %r", bootfile));
	}
	else if (bootprotocol == "tftp") {
		tftp := load Tftp Tftp->PATH;
		if (tftp == nil)
			fatal("can't load tftp module");
		tftp->init(1);
		errstr := tftp->receive(bootserver, bootfile, kexecfd);
		if (errstr != nil)
			fatal("tftp: " + errstr);
	}
	else
		fatal("protocol " + bootprotocol + " not supported");
	sys->print("Launching new kernel\n");
	kexecfd = nil;
}

parsebootstring(s: string): (string, string, string)
{
	proto, server, file: string;
	(n, l) := sys->tokenize(s, "!");
	if (n > 3)
		return (nil, nil, nil);
	proto = hd l;
	l = tl l;
	if (l != nil) {
		server = hd l;
		l = tl l;
	}
	if (l != nil)
		file = hd l;
	case proto {
	"tftp" =>
		;
	"net" =>
		# can't have a default file, so n must be 3
		if (n != 3)
			return (nil, nil, nil);
	* =>
		return (nil, nil, nil);
	}
	return (proto, server, file);
}

dobind(f, t: string, flags: int): int
{
	if(sys->bind(f, t, flags) < 0) {
		err(sys->sprint("can't bind %s on %s: %r", f, t));
		return 0;
	}
	return 1;
}

err(s: string)
{
	sys->fprint(sys->fildes(2), "bootinit: %s\n", s);
}

hang()
{
	<-(chan of int);
}

fatal(s: string)
{
	err(s);
	hang();
}

envlist: list of string;

getenv(name: string): string
{
	if (envlist == nil) {
		fd := sys->open("/dev/sysenv", Sys->OREAD);
		if (fd != nil) {
			ntok: int;
			buf := array[1024] of byte;
			nr := sys->read(fd, buf, len buf);
			if(nr > 0)
				(ntok, envlist) = sys->tokenize(string buf, "\n");
		}
	}
	ls := envlist;
	while(ls != nil) {
		(ntok2, ls2) := sys->tokenize(hd ls, "=");
		if(hd ls2 == name)
			return hd tl ls2;
		ls = tl ls;
	}
	return nil;
}

getenvdefault(name: string, default: string): string
{
	rv := getenv(name);
	if (rv == nil)
		return default;
	return rv;
}

ipconfig(cfd: ref sys->FD): string
{
	ip = getenv("wireip");
	if (ip == nil)
		ip = getenv("ip");
	mask = getenv("ipmask");
	fsip = getenv("fsip");
	if (ip != nil && mask != nil) {
		sys->print("ip %s %s\n", ip, mask);
		sys->fprint(cfd, "add %s %s", ip, mask);
		gwip := getenv("gwip");
		if (gwip != nil) {
			sys->print("gwip %s\n", gwip);
			rfd := sys->open("/net/iproute", Sys->ORDWR);
			if (rfd == nil || sys->fprint(rfd, "add 0.0.0.0 0.0.0.0 %s", gwip) < 0)
				err(sys->sprint("failed to add default route: %r"));
		}
	}
	if (ip == nil || mask == nil)
		return bootp(cfd);
	return fsip;
}

bootpdone: int;

bootp(cfd: ref sys->FD): string
{
	if (bootpdone == 1)
		return fsip;

	bootpdone = 1;

	sys->print("bootp ...");

	if (sys->fprint(cfd, "bootp") < 0) {
		sys->print("init: bootp: %r");
		return nil;
	}

	fd := sys->open("/net/bootp", sys->OREAD);
	if(fd == nil) {
		err(sys->sprint("open /net/bootp: %r"));
		return nil;
	}

	buf := array[Bootpreadlen] of byte;
	nr := sys->read(fd, buf, len buf);
	fd = nil;
	if(nr <= 0) {
		err(sys->sprint("read /net/bootp: %r"));
		return nil;
	}
	(ntok, ls) := sys->tokenize(string buf, " \t\n");
	while(ls != nil) {
		name := hd ls;
		ls = tl ls;
		if (ls == nil)
			break;
		value := hd ls;
		ls = tl ls;
		if (name == "fsip")
			fsip = value;
		else if (name == "ipaddr")
			ip = value;
		else if (name == "ipmask")
			mask = value;
	}
	return fsip;
}

netfs(server: string): int
{
	auth = load Auth  Auth->PATH;
	if (auth != nil)
		auth->init();

	kr = load Keyring Keyring->PATH;
	sys->print("dial...");
	(ok, c) := sys->dial("tcp!" + server + "!6666", nil);
	if(ok < 0)
		return -1;
	
	if(kr != nil && auth != nil){
		err: string;
		sys->print("Authenticate ...");
		ai := kr->readauthinfo("/nvfs/default");
		if(ai == nil){
			sys->print("readauthinfo /nvfs/default failed: %r\n");
			sys->print("trying mount as `nobody'\n");
		}
		(c.dfd, err) = auth->client("none", ai, c.dfd);
		if(c.dfd == nil){
			sys->print("authentication failed: %s\n", err);
			return -1;
		}
	}
	
	sys->print("mount ...");
	
	c.cfd = nil;
	n := sys->mount(c.dfd, nil, "/n/remote", sys->MREPL, "");
	if(n > 0)
		return 0;
	return -1;
}

#
#
# DHCP
#
#

Dhcp: adt {
	op: int;
	htype: int;
	hops: int;
	xid: int;
	secs: int;
	flags: int;
	ciaddr: int;
	yiaddr: int;
	siaddr: int;
	giaddr: int;
	chaddr: array of byte;
	sname: string;
	file: string;
};

nboputl(buf: array of byte, val: int)
{
	buf[0] = byte (val >> 24);
	buf[1] = byte (val >> 16);
	buf[2] = byte (val >> 8);
	buf[3] = byte val;
}

nboputs(buf: array of byte, val: int)
{
	buf[0] = byte (val >> 8);
	buf[1] = byte val;
}

nbogets(buf: array of byte): int
{
	return (int buf[0] << 8) | int buf[1];
}

nbogetl(buf: array of byte): int
{
	return (int buf[0] << 24) | (int buf[1] << 16) | (int buf[2] << 8) | int buf[3];
}

stringget(buf: array of byte): string
{
	for (x := 0; x < len buf; x++)
		if (buf[x] == byte 0)
			break;
	if (x == 0)
		return nil;
	return string buf[0 : x];
}

memcmp(b1: array of byte, b2: array of byte): int
{
	l := len b1;
	if (l < len b2)
		return int -b2[l];
	if (l > len b2)
		return int b1[l];
	for (i := 0; i < l; i++) {
		d := int b1[i] - int b2[i];
		if (d != 0)
			return d;
	}
	return 0;
}

memncpy(out: array of byte, in: array of byte)
{
	if (in == nil)
		return;
	l := len in;
	if (l > len out)
		l = len out;
	out[0 :] = in[0 : l];
}

memset(out: array of byte, val: byte)
{
	for (l := 0; l < len out; l++)
		out[l] = val;
}

dhcpsend(dfd: ref Sys->FD, dhcp: ref Dhcp)
{
	buf := array[576] of byte;
	buf[0] = byte dhcp.op;
	buf[1] = byte dhcp.htype;
	buf[2] = byte len dhcp.chaddr;
	buf[3] = byte dhcp.hops;
	nboputl(buf[4 : 8], dhcp.xid);
	nboputs(buf[8 : 10], dhcp.secs);
	nboputs(buf[10 : 12], dhcp.flags);
	nboputl(buf[12 : 16], dhcp.ciaddr);
	nboputl(buf[16 : 20], dhcp.yiaddr);
	nboputl(buf[20 : 24], dhcp.siaddr);
	nboputl(buf[24 : 28], dhcp.giaddr);
	memset(buf[28 :], byte 0);
	memncpy(buf[28 : 44], dhcp.chaddr);
	memncpy(buf[44 : 108], array of byte dhcp.sname);
	memncpy(buf[108 : 236], array of byte dhcp.file);
	sys->write(dfd, buf, len buf);
}

kill(pid: int)
{
	fd := sys->open("#p/" + string pid + "/ctl", sys->OWRITE);
	if (fd == nil)
		return;

	msg := array of byte "kill";
        sys->write(fd, msg, len msg);
}

ipfmt(ipaddr: int): string
{
	return sys->sprint("%ud.%ud.%ud.%ud",
		(ipaddr >> 24) & 16rff,
		(ipaddr >> 16) & 16rff,
		(ipaddr >> 8) & 16rff,
		ipaddr & 16rff);
}

dumpdhcp(dhcp: ref Dhcp)
{
	sys->print("op %d htype %d hops %d xid %ud\n", dhcp.op, dhcp.htype, dhcp.hops, dhcp.xid);
	sys->print("secs %d flags 0x%.4ux\n", dhcp.secs, dhcp.flags);
	sys->print("ciaddr %s\n", ipfmt(dhcp.ciaddr));
	sys->print("yiaddr %s\n", ipfmt(dhcp.yiaddr));
	sys->print("siaddr %s\n", ipfmt(dhcp.siaddr));
	sys->print("giaddr %s\n", ipfmt(dhcp.giaddr));
	sys->print("chaddr ");
	for (x := 0; x < len dhcp.chaddr; x++)
		sys->print("%.2ux", int dhcp.chaddr[x]);
	sys->print("\n");
	if (dhcp.sname != nil)
		sys->print("sname %s\n", dhcp.sname);
	if (dhcp.file != nil)
		sys->print("file %s\n", dhcp.file);
}

dhcplisten(pidc: chan of int, fd: ref Sys->FD, dc: chan of ref Dhcp)
{
	pid := sys->pctl(0, nil);
	pidc <-= pid;
	buf := array [576] of byte;
	while (1) {
		n := sys->read(fd, buf, len buf);
		dhcp := ref Dhcp;
		dhcp.op = int buf[0];
		dhcp.htype = int buf[1];
		hlen := int buf[2];
		dhcp.hops = int buf[3];
		dhcp.xid = nbogetl(buf[4 : 8]);
		dhcp.secs = nbogets(buf[8 : 10]);
		dhcp.flags = nbogets(buf[10 : 12]);
		dhcp.ciaddr = nbogetl(buf[12 : 16]);
		dhcp.yiaddr = nbogetl(buf[16 : 20]);
		dhcp.siaddr = nbogetl(buf[20 : 24]);
		dhcp.giaddr = nbogetl(buf[24: 28]);
		dhcp.chaddr = buf[28 : 28 + hlen];
		dhcp.sname = stringget(buf[44 : 108]);
		dhcp.file = stringget(buf[108 : 236]);
		dc <-= dhcp;
	}
}
	
timeoutproc(pid: chan of int, howlong: int, c: chan of string)
{
	pid <-= sys->pctl(0, nil);

	sys->sleep(howlong);

	# send timeout
	c <-= "timed out";
}

tpid := -1;
tc: chan of string;

timeoutcancel()
{
	if (tpid >= 0) {
		kill(tpid);
		tpid = -1;
	}
}

timeoutstart(howlong: int): (chan of string)
{
	timeoutcancel();
	pidc := chan of int;
	tc = chan of string;
	spawn timeoutproc(pidc, howlong, tc);
	tpid = <- pidc;
	return tc;
}

atohn(b: byte): int
{
	if (b >= byte '0' && b <= byte '9')
		return int (b - byte '0');
	if (b >= byte 'A' && b <= byte 'F')
		return int b - 'A' + 10;
	if (b >= byte 'a' && b <= byte 'f')
		return int b - 'a' + 10;
	return -1;
}

atohb(buf: array of byte): int
{
	tn := atohn(buf[0]);
	bn := atohn(buf[1]);
	if (tn < 0 || bn < 0)
		return -1;
	return tn * 16 + bn;
}

gethaddr(dhcp: ref Dhcp): int
{
	fd := sys->open("#l/ether0/addr", Sys->OREAD);
	if (fd == nil)
		return 0;
	buf := array [100] of byte;
	n := sys->read(fd, buf, len buf);
	if (n < 0)
		return 0;
	dhcp.htype = 1;
	hlen := n / 2;
	dhcp.chaddr = array [hlen] of byte;
	for (i := 0; i < hlen; i++)
		dhcp.chaddr[i] = byte atohb(buf[i * 2 : i * 2 + 2]);
	return 1;
}

parsedq(dq: string): (int, int)
{
	(c, l) := sys->tokenize(dq, ".");
	if (c != 4)
		return (0, 0);
	a := hd l;
	l = tl l;
	b := hd l;
	l = tl l;
	d := hd l;
	l = tl l;
	addr := (int a << 24) | (int b << 16) | (int d << 8) | int hd l;
	return (1, addr);
}

dhcp()
{
	ok: int;
	conn: Sys->Connection;
	rdhcp: ref Dhcp;

	if (random == nil)
		random = load Random Random->PATH;

	(ok, conn) = sys->dial("udp!255.255.255.255!67", "68");
	if (!ok)
		fatal(sys->sprint("failed to dial udp broadcast: %r"));

	pidc := chan of int;
	dc := chan of ref Dhcp;
	spawn dhcplisten(pidc, conn.dfd, dc);
	dhcppid := <- pidc;
	dhcp := ref Dhcp;
	dhcp.op = 1;
	dhcp.htype  = 1;
	gethaddr(dhcp);
	dhcp.hops = 0;
	dhcp.xid = random->randomint(Random->NotQuiteRandom);
	dhcp.secs = 0;
	dhcp.flags = 0;
	(ok, dhcp.ciaddr) = parsedq(ip);
	dhcp.yiaddr = 0;
	dhcp.siaddr = 0;
	dhcp.giaddr = 0;
	if (bootfile != "bootp")
		dhcp.file = bootfile;
	else
		dhcp.file = nil;
	ok = 0;
	for (count := 0; !ok && count < 5; count++) {
		mtc := timeoutstart(3000);
		dhcpsend(conn.dfd, dhcp);
		timedout := 0;
		do {
			alt {
				<- mtc =>
					timedout = 1;
				rdhcp = <- dc =>
					if (debug)
						dumpdhcp(rdhcp);
					if (rdhcp.ciaddr != dhcp.ciaddr || rdhcp.xid != dhcp.xid
						|| memcmp(rdhcp.chaddr, dhcp.chaddr) != 0) {
						break;
					}
					if (rdhcp.file != nil) {
						ok = 1;
						timeoutcancel();
					}
			}
		} while (!timedout && !ok);
		dhcp.xid++;
	}
	if (ok) {
		if (bootfile == nil)
			bootfile = rdhcp.file;
		if (bootserver == nil)
			bootserver = ipfmt(rdhcp.siaddr);
	}
	else
		err("bootp timed out");
	kill(dhcppid);
}