summaryrefslogtreecommitdiff
path: root/sys/arch/i386/netboot/main.c
blob: 1d36c124fc0f46f59816ce561ef89cb231ead564 (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
629
630
631
632
633
634
635
636
637
638
/*	$OpenBSD: main.c,v 1.4 1996/04/26 18:23:03 mickey Exp $	*/
/*	$NetBSD: main.c,v 1.6 1996/02/02 18:06:17 mycroft Exp $	*/

/*
 * source code in this file is from:
 * 386BSD boot blocks by Julian Elischer (julian@tfs.com)
 * 386BSD Adaptec 1542 SCSI boot blocks by Pace Willisson (pace@blitz.com)
 *
 * Mach Operating System
 * Copyright (c) 1992, 1991 Carnegie Mellon University
 * All Rights Reserved.
 * 
 * Permission to use, copy, modify and distribute this software and its
 * documentation is hereby granted, provided that both the copyright
 * notice and this permission notice appear in all copies of the
 * software, derivative works or modified versions, and any portions
 * thereof, and that both notices appear in supporting documentation.
 * 
 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
 * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
 * 
 * Carnegie Mellon requests users of this software to return to
 * 
 *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
 *  School of Computer Science
 *  Carnegie Mellon University
 *  Pittsburgh PA 15213-3890
 * 
 * any improvements or extensions that they make and grant Carnegie Mellon
 * the rights to redistribute these changes.
 */

/*
  Copyright 1988, 1989, 1990, 1991, 1992 
   by Intel Corporation, Santa Clara, California.

                All Rights Reserved

Permission to use, copy, modify, and distribute this software and
its documentation for any purpose and without fee is hereby
granted, provided that the above copyright notice appears in all
copies and that both the copyright notice and this permission notice
appear in supporting documentation, and that the name of Intel
not be used in advertising or publicity pertaining to distribution
of the software without specific, written prior permission.

INTEL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE
INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS,
IN NO EVENT SHALL INTEL BE LIABLE FOR ANY SPECIAL, INDIRECT, OR
CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
LOSS OF USE, DATA OR PROFITS, WHETHER IN ACTION OF CONTRACT,
NEGLIGENCE, OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/


#include "config.h"
#include "nbtypes.h"
#include "assert.h"
#include "param.h"
#include "packet.h"
#include "ether.h"
#include "inet.h"
#include "arp.h"
#include "tftp.h"

#include "proto.h"


u_long work_area_org = RELOC;
u_long kernel_size;
u_short real_cs;
u_long howto = 0;
u_char vendor_area[64];

enum LoadErr {
  load_success,
  load_no_answer,
  load_bad_format,
  load_bad_size,
};

static inline enum LoadErr
LoadProgFromServer(ipaddr_t server, ipaddr_t gateway, char *file_name) {
  /* TBD - fail (or at least warn) if we don't get the entire file */
  u_long start_addr;
  u_long boot_area_org;
  u_long tmp_reloc_org;
  u_long kern_copy_org;
  u_long kern_copy_p;
  struct exec head;
  u_long boot_argv[4];
#if 1 /* ndef USE_BUFFER */
  u_char tmpbuf[4096]; /* we need to load the first 4k here */
#endif

  SetTftpParms(server, gateway, file_name);

  /* TBD - heuristics about what we are loading - load linux, ... also */

  if (Read(&head, sizeof(head)) != sizeof(head))
    return load_bad_size;

  if (N_GETMAGIC(head) != ZMAGIC) {
    printf("Invalid format!\n");
    return load_bad_format;
  }

  /* Load/tftp the executable to a temporary area beyond 640k - after
     successful load, copy to correct area. If load fails or is interrrupted,
     we can recover gracefully
     */

  start_addr = (u_long)head.a_entry;
  boot_area_org = start_addr & 0x00f00000; /* some MEG boundary */
  tmp_reloc_org = 0x00100000;
#ifdef USE_BUFFER
  kern_copy_org = tmp_reloc_org + WORK_AREA_SIZE; /* leave free for relocation */
#else
  kern_copy_org = boot_area_org; /* leave free for relocation */
#endif
  kern_copy_p = kern_copy_org;
  kernel_size = head.a_text + head.a_data + head.a_bss;

  printf("Loading ");
  IpPrintAddr(server);
  printf(":%s @ 0x%x\n", file_name, boot_area_org);
#ifndef USE_BUFFER
  if(boot_area_org < work_area_org) {
    if((boot_area_org + head.a_text + head.a_data) > work_area_org) {
      printf("kernel will not fit below loader\n");
      return load_bad_size;
    }
    if((boot_area_org + head.a_text + head.a_data + head.a_bss) > 0xa0000) {
      printf("kernel too big, won't fit in 640K with bss\n");
      printf("Only hope is to link the kernel for > 1MB\n");
      return load_bad_size;
    }
    if(boot_area_org + kernel_size > work_area_org) {
      printf("loader overlaps bss, kernel must bzero\n");
    }
  }
#else
  if(boot_area_org + kernel_size > 0xa0000) {
    printf("kernel too big, won't fit in 640K with bss\n");
    printf("Only hope is to link the kernel for > 1MB\n");
    return load_bad_size;
  }
  /* check if too large for tmp buffer (TBD) */
#endif
  printf("text=0x%x", head.a_text);

#if 01
  /* skip to first 4k boundry */
  Read(tmpbuf, 4096-sizeof(head));
#endif

#if 0 /* ndef USE_BUFFER */
  /********************************************************/
  /* LOAD THE TEXT SEGMENT				*/
  /* don't clobber the first 4k yet (BIOS NEEDS IT) 	*/
  /********************************************************/
  Read(tmpbuf, sizeof(tmpbuf));
  kern_copy_p += sizeof(tmpbuf);
  PhysRead(kern_copy_p, head.a_text - sizeof(tmpbuf));
  kern_copy_p += head.a_text - sizeof(tmpbuf);
#else
  /********************************************************/
  /* LOAD THE TEXT SEGMENT				*/
  /********************************************************/
  PhysRead(kern_copy_p, head.a_text);
  kern_copy_p += head.a_text;
#endif

  /********************************************************/
  /* Load the Initialised data after the text		*/
  /********************************************************/
/* TBD - this is bogus - file system oriented */
#define CLSIZE 1
#define NBPG 4096            /* bytes/page */
#define CLOFSET (CLSIZE*NBPG-1) /* for clusters, like PGOFSET */

  while (kern_copy_p & CLOFSET)
    *(char *)kern_copy_p++ = 0;

  printf(" data=0x%x", head.a_data);
  PhysRead(kern_copy_p, head.a_data);
  kern_copy_p += head.a_data;

  /********************************************************/
  /* Skip over the uninitialised data			*/
  /* (but clear it)					*/
  /********************************************************/
  printf(" bss=0x%x", head.a_bss);
  if (kern_copy_p < RELOC &&
     (kern_copy_p + head.a_bss) > RELOC) {
    PhysBzero(kern_copy_p, RELOC-(u_int)kern_copy_p);
  } else {
    PhysBzero(kern_copy_p, head.a_bss);
  }

#ifdef LOADSYMS /* not yet, haven't worked this out yet */
  if (kern_copy_p > 0x100000) {
    /********************************************************/
    /*copy in the symbol header				*/
    /********************************************************/
    PhysBcopy(&head.a_syms, kern_copy_p, sizeof(head.a_syms));
    kern_copy_p += sizeof(head.a_syms);

    /********************************************************/
    /* READ in the symbol table				*/
    /********************************************************/
    printf(" symbols=[+0x%x", head.a_syms);
    Read(kern_copy_p, head.a_syms);
    kern_copy_p += head.a_syms;

    /********************************************************/
    /* Followed by the next integer (another header)	*/
    /* more debug symbols?					*/
    /********************************************************/
    read(&i, sizeof(u_int));
    PhysBcopy(&i, kern_copy_p, sizeof(u_int));
    i -= sizeof(u_int);
    kern_copy_p += sizeof(u_int);

    /********************************************************/
    /* and that many bytes of (debug symbols?)		*/
    /********************************************************/
    printf("+0x%x]", i);
    Read(kern_copy_p, i);
    kern_copy_p += i;
  }
#endif	LOADSYMS

  /********************************************************/
  /* and note the end address of all this			*/
  /********************************************************/

  printf(" total=0x%x",kern_copy_p - kern_copy_org);

  boot_argv[0] = 0;
  boot_argv[1] = howto;
  boot_argv[2] = 0;
  boot_argv[3] = 0;

/* TBD - place vendor_area on stack */

  printf(" entry point=0x%x\n" ,((u_int)start_addr) & 0xffffff);

  if (howto) {
    static char *rb_option_name[9] = {
      "askname",
      "single",
      "nosync",
      "halt",
      "initname",
      "dfltroot",
      "kdb",
      "rdonly",
      "dump",
      };
    int i;
    printf("Starting kernel with options (0x%x): ", howto);
    for (i=0; i<9; i++) {
      if (howto & (1<<i)) {
	printf("%s ", rb_option_name[i]);
      }
    }
    printf("\n");
  }

#if 0 /* ndef USE_BUFFER */
  PhysBcopy(tmpbuf, boot_area_org, sizeof(tmpbuf));
#endif

  StartProg(start_addr & 0xffffff, boot_argv);

  return load_success; /* hah! */
}

int GetHex(int old) {
  int r, c, ch;
  r = 0;
  ch = 0;
  while((c=getchar()) != '\n') {
    ch = 1;
    if (c>='0' && c<='9')
      r = r*16 + (c-'0');
    else if (c>='a' && c<='f')
      r = r*16 + (c+10-'a');
  }
  if (ch)
    return r;
  else
    return old;
}

static char
ToHex(int n) {
  n &= 0x0F;
  return n >= 10 ? n - 10 + 'A' : n + '0';
}

static char name_set[][9] = {
  "xxxxxxxx",
  "default",
};

static char *ext_set[] = {
  ".bsd",
  ".bsd.old",
  ".vmunix",
  ".vmunix.old",
};

static ipaddr_t server_set[2] = {
  0,
  IP_BCASTADDR,
};

static inline void
TryToLoadSomething(void) {
  int nserver;
  for (nserver=0; nserver<nelt(server_set); nserver++) {
    int nname;
    char file_name[MAX_FILE_NAME_LEN+1];
    file_name[0] = '\0';
    if (GetIpAddress(&server_set[nserver], &ip_myaddr, &ip_gateway, file_name)) {
      if (*file_name) {
	LoadProgFromServer(server_set[nserver], ip_gateway, file_name);
      }
      else {
	/* no file name supplied from server, synthesize one */
	inetaddr_t ip;
	ip.a = ip_myaddr;
	name_set[0][0] = ToHex(ip.s.a0 >> 4);
	name_set[0][1] = ToHex(ip.s.a0);
	name_set[0][2] = ToHex(ip.s.a1 >> 4);
	name_set[0][3] = ToHex(ip.s.a1);
	name_set[0][4] = ToHex(ip.s.a2 >> 4);
	name_set[0][5] = ToHex(ip.s.a2);
	name_set[0][6] = ToHex(ip.s.a3 >> 4);
	name_set[0][7] = ToHex(ip.s.a3);
	name_set[0][8] = 0;
	for (nname=0; nname<nelt(name_set); nname++) {
	  int next;
	  for (next=0; next<nelt(ext_set); next++) {
	    strncpy(file_name, name_set[nname], MAX_FILE_NAME_LEN);
	    strncat(file_name, ext_set[next], MAX_FILE_NAME_LEN-strlen(file_name));
	    LoadProgFromServer(server_set[nserver], ip_gateway, file_name);
	  }
	}
      }
    }
  }
}


static char *
DecimalToByte(char *s, u_char *n) {
  for (*n = 0; *s >= '0' && *s <= '9'; s++)
    *n = (*n * 10) + *s - '0';
  return s;
}

static ipaddr_t
IpConvertAddr(char *p) {
  inetaddr_t addr;

  if (p == (char *)0 || *p == '\0')
    return IP_ANYADDR;
  p = DecimalToByte(p, &addr.s.a0);
  if (*p == '\0' || *p++ != '.')
    return IP_ANYADDR;
  p = DecimalToByte(p, &addr.s.a1);
  if (*p == '\0' || *p++ != '.')
    return IP_ANYADDR;
  p = DecimalToByte(p, &addr.s.a2);
  if (*p == '\0' || *p++ != '.')
    return IP_ANYADDR;
  p = DecimalToByte(p, &addr.s.a3);
  if (*p != '\0')
    return IP_ANYADDR;
  return addr.a;
}

static int
GetLine(char **argv, int argvsize) {
  char *p, ch;
  static char line[128];
  int argc;

  /*
   * Read command line, implement some simple editing features
   */
  p = line;
  while ((ch = getchar()) != '\r' && ch != '\n' && (p-line) < sizeof(line)) {
    if (ch == '\b') {
      if (p > line) {
	p--;
	printf(" \b");
      }
    } else
      *p++ = ch;
  }
  *p = '\0';

  /*
   * Break command line up into an argument vector
   */
  argc = 0;
  for (p = line; *p == ' ' || *p == '\t'; p++)
    /* skip white spaces */;
  while (*p != ' ' && *p != '\t' && *p != '\n' && *p != '\0') {
    if (argc > argvsize) break;
    argv[(argc)++] = p;
    for (; *p != ' ' && *p != '\t' && *p != '\n' && *p != '\0'; p++)
      /* skip word */;
    if (*p != '\0') *p++ ='\0';
    for (; *p == ' ' || *p == '\t'; p++)
      /* skip white spaces */;
  }
  argv[(argc)] = (char *)0;
  return argc;
}

enum cmd_token_type {
  CMD_GO,
  CMD_HELP,
  CMD_RESET,
  CMD_AUTO,
  CMD_WHO,
  CMD_REBOOT,
  CMD_DISKBOOT,
  CMD_ADDRESS,
  CMD_GATEWAY,
  CMD_TFTP,
  CMD_DEBUG,
  CMD_SINGLE,
  CMD_FILE,
  CMD_SERVER,
};

struct commands {
  enum cmd_token_type cmd_token;
  char *cmd_name;
  char *cmd_help;
} commands[] = {
  {CMD_GO, 	"go",		"                       start loaded binary"},
  {CMD_HELP,	"help",		"                     this help message"},
  {CMD_RESET,	"reset",	"                    reset ethernet board"},
  {CMD_AUTO,	"auto",		"                     continue with auto boot"},
  {CMD_WHO,	"whoami",	"                       get IP address"},
  {CMD_REBOOT,	"reboot",	"                   hard reboot"},
  {CMD_DISKBOOT,	"diskboot",	"                 soft reboot (from disk)"},
  {CMD_ADDRESS,  "address",	"[<addr>]          set IP address"},
  {CMD_GATEWAY,  "gateway",	"[<addr>]          set IP gateway"},
  {CMD_SERVER, "server", "[<addr>]           set server IP address"},
  {CMD_FILE, "file", "[<name>]             set boot file name"},
  {CMD_TFTP,	"tftp",		"                     TFTP download"},
  {CMD_DEBUG, "debug", "                    enter kernel debugger at boot"},
  {CMD_SINGLE, "single", "                   enter single user at boot"},
};

#define ARGVECSIZE 100

/*
 * A very simple and terse monitor
 */
static void
Monitor(void) {
  char *argv[ARGVECSIZE];
  static char file_name[MAX_FILE_NAME_LEN+1] = "default.bsd386";
  int loaded, argc;
  int i;
  int token;
  static ipaddr_t ip_servaddr = IP_ANYADDR;

  loaded = 0;
  printf("\n"
#ifdef USE_BOOTP
	 "BOOTP/"
#endif
#ifdef USE_RARP
	 "RARP/"
#endif
	 "TFTP monitor mode\n");
  for (;;) {
    char filename[MAX_FILE_NAME_LEN+1];
    ipaddr_t gateway;
    printf("ethernet boot monitor: ");
    if ((argc = GetLine(argv, ARGVECSIZE)) > 0) {

      for (token = -1, i = 0; i < nelt(commands); i++) {
	if (strcmp(argv[0], commands[i].cmd_name) == 0) {
	  token = commands[i].cmd_token;
	  break;
	}
      }
      switch (token) {
      case CMD_HELP:
	for (i = 0; i < nelt(commands); i++)
	  printf("%s %s\n", commands[i].cmd_name, commands[i].cmd_help);
	break;
      case CMD_RESET:
	PktInit();
	EtherReset();
	break;
      case CMD_REBOOT:
	EtherStop();
	ResetCpu();
	break;
      case CMD_DISKBOOT:
	EtherStop();
	exit(0);
	break;
      case CMD_AUTO:
	return;
      case CMD_WHO:
	(void) GetIpAddress(&ip_servaddr, &ip_myaddr, &gateway, filename);
	break;
      case CMD_ADDRESS:
	if (argc != 2) {
	  printf("My IP address is ");
	  IpPrintAddr(ip_myaddr);
	  printf("\n");
	} else
	  ip_myaddr = IpConvertAddr(argv[1]);
	break;
      case CMD_SERVER:
	if (argc != 2) {
	  printf("Server's IP address is ");
	  IpPrintAddr(ip_servaddr);
	  printf("\n");
	} else
	  ip_servaddr = IpConvertAddr(argv[1]);
	break;
      case CMD_FILE:
	if (argc != 2) {
	  printf("File name is \"%s\"\n", file_name);
	} else
	  strncpy(file_name, argv[1], MAX_FILE_NAME_LEN);
	break;
      case CMD_GATEWAY:
	if (argc != 2) {
	  printf("Gateway IP address is ");
	  IpPrintAddr(ip_gateway);
	  printf("\n");
	} else
	  ip_gateway = IpConvertAddr(argv[1]);
	break;
      case CMD_DEBUG:
	howto ^= RB_KDB;
	break;
      case CMD_SINGLE:
	howto ^= RB_SINGLE;
	break;
      case CMD_TFTP:
	if (ip_myaddr == IP_ANYADDR) {
	  printf("This machine's IP address must be set first.\n");
	  goto complain;
	}
	loaded = LoadProgFromServer(ip_servaddr, ip_gateway, file_name);
	printf("File could not be loaded, giving up.\n");
	break;
      default:
	goto complain;
      }
    } else
    complain:
      printf("Invalid or incorrect command. Type \"help\" for help.\n");
  }
}

static jmp_buf jmp_env;

void
HandleKbdAttn(void) {
  if (IsKbdCharReady())
    if (getc() == 0x1b) {
      EtherReset();
      PktInit();
      longjmp(jmp_env, 1);
    }
}

int time_zero;

void
main(void) {

  extern char edata[], end[];
  char volatile * volatile p;

  /* clear bss */
  for (p = edata; p < end; p++)
    *p = 0;

  printf(
#ifdef USE_BOOTP
	 "BOOTP/"
#endif
#ifdef USE_RARP
	 "RARP/"
#endif
	 "TFTP bootstrap loader @0x%x: %d/%d k of memory. ^] for attn.\n",
	 work_area_org,
	 GetMemSize(0),
	 GetMemSize(1));

  gateA20();

  PktInit();
  if (!EtherInit()) {
    printf("No ethernet board found\n");
    exit(1);
  }
  srand((time_zero=timer()) ^ eth_myaddr[5]);

  printf("Ethernet address is ");
  EtherPrintAddr(eth_myaddr);
  printf("\n");

  for (;;) {
    if (setjmp(jmp_env))
      Monitor();
    else {
      TryToLoadSomething();
    }
  }
}

#ifdef __GNUC__
void
__main(void) {
}
#endif