1*49111Sbostic /*-
2*49111Sbostic * Copyright (c) 1991 The Regents of the University of California.
3*49111Sbostic * All rights reserved.
4*49111Sbostic *
5*49111Sbostic * %sccs.include.redist.c%
6*49111Sbostic *
7*49111Sbostic * @(#)boot.c 7.6 (Berkeley) 05/04/91
8*49111Sbostic */
925865Ssam
1045796Sbostic #include "../include/mtpr.h"
1125865Ssam
1243457Sroot #include "sys/param.h"
1343457Sroot #include "sys/time.h"
1443457Sroot #include "sys/vm.h"
1543457Sroot #include "sys/reboot.h"
1645796Sbostic #include "stand/saio.h"
1725865Ssam
1825865Ssam #include <a.out.h>
1925865Ssam
2025865Ssam /*
2125865Ssam * Boot program... arguments passed in r10 and r11 determine
2225865Ssam * whether boot stops to ask for system name and which device
2325865Ssam * boot comes from.
2425865Ssam */
2525865Ssam
2630177Skarels #define DEV_DFLT 1 /* vd/dk */
2738118Sbostic /*#define DEV_DFLT 2 /* hd */
2825865Ssam
2925865Ssam char line[100];
3025865Ssam
3130758Skarels extern unsigned opendev;
3230758Skarels extern unsigned bootdev;
3325865Ssam
main()3425865Ssam main()
3525865Ssam {
3630758Skarels register char *cp; /* skip r12 */
3733652Sbostic register u_int howto, devtype; /* howto=r11, devtype=r10 */
3837567Sbostic int io = 0, retry, type;
3925865Ssam
4025865Ssam #ifdef lint
4125865Ssam howto = 0; devtype = 0;
4225865Ssam #endif
4330758Skarels if ((devtype & B_MAGICMASK) != B_DEVMAGIC)
4430758Skarels devtype = DEV_DFLT << B_TYPESHIFT; /* unit, partition 0 */
4530758Skarels bootdev = devtype;
4625865Ssam #ifdef JUSTASK
4725865Ssam howto = RB_ASKNAME|RB_SINGLE;
4829565Ssam #else
4929565Ssam if ((howto & RB_ASKNAME) == 0) {
5030758Skarels type = (devtype >> B_TYPESHIFT) & B_TYPEMASK;
5133652Sbostic if ((unsigned)type < ndevs && devsw[type].dv_name)
5230758Skarels strcpy(line, UNIX);
5330758Skarels else
5430758Skarels howto |= RB_SINGLE|RB_ASKNAME;
5529565Ssam }
5625865Ssam #endif
5733652Sbostic for (retry = 0;;) {
5837567Sbostic if (io >= 0)
5937567Sbostic printf("\nBoot");
6025865Ssam if (howto & RB_ASKNAME) {
6125865Ssam printf(": ");
6225865Ssam gets(line);
6330758Skarels if (line[0] == 0) {
6430758Skarels strcpy(line, UNIX);
6530758Skarels printf(": %s\n", line);
6630758Skarels }
6725865Ssam } else
6825865Ssam printf(": %s\n", line);
6925865Ssam io = open(line, 0);
7029565Ssam if (io >= 0) {
7130758Skarels copyunix(howto, opendev, io);
7229565Ssam close(io);
7330758Skarels howto |= RB_SINGLE|RB_ASKNAME;
7429565Ssam }
7525865Ssam if (++retry > 2)
7625865Ssam howto |= RB_SINGLE|RB_ASKNAME;
7725865Ssam }
7825865Ssam }
7925865Ssam
8025865Ssam /*ARGSUSED*/
copyunix(howto,devtype,io)8129565Ssam copyunix(howto, devtype, io)
8229565Ssam register io, howto, devtype; /* NOTE ORDER */
8325865Ssam {
8430182Ssam register int esym; /* must be r9 */
8525865Ssam register int i;
8629565Ssam register char *addr;
8730182Ssam struct exec x;
8825865Ssam
8937567Sbostic if (read(io, (char *)&x, sizeof(x)) != sizeof(x) || N_BADMAG(x)) {
9037567Sbostic printf("bad magic #\n");
9130758Skarels return;
9230758Skarels }
9337567Sbostic printf("%ld", x.a_text);
9437567Sbostic if (x.a_magic == ZMAGIC && lseek(io, 0x400, 0) == -1)
9525865Ssam goto shread;
9630307Skarels if (read(io, (char *)RELOC, x.a_text) != x.a_text)
9725865Ssam goto shread;
9830307Skarels addr = (char *)(x.a_text + RELOC);
9937567Sbostic if (x.a_magic == ZMAGIC || x.a_magic == NMAGIC)
10025865Ssam while ((int)addr & CLOFSET)
10125865Ssam *addr++ = 0;
10237567Sbostic printf("+%ld", x.a_data);
10325865Ssam if (read(io, addr, x.a_data) != x.a_data)
10425865Ssam goto shread;
10525865Ssam addr += x.a_data;
10637567Sbostic printf("+%ld", x.a_bss);
10730182Ssam if (howto & RB_KDB && x.a_syms) {
10830182Ssam for (i = 0; i < x.a_bss; i++)
10930182Ssam *addr++ = 0;
11030182Ssam *(int *)addr = x.a_syms; /* symbol table size */
11130182Ssam addr += sizeof (int);
11237567Sbostic printf("[+%ld", x.a_syms);
11330182Ssam if (read(io, addr, x.a_syms) != x.a_syms)
11430182Ssam goto shread;
11530182Ssam addr += x.a_syms;
11630182Ssam if (read(io, addr, sizeof (int)) != sizeof (int))
11730182Ssam goto shread;
11830182Ssam i = *(int *)addr - sizeof (int); /* string table size */
11930182Ssam addr += sizeof (int);
12030182Ssam printf("+%d]", i);
12130182Ssam if (read(io, addr, i) != i)
12230182Ssam goto shread;
12330182Ssam addr += i;
12430182Ssam esym = roundup((int)addr, sizeof (int));
12530182Ssam x.a_bss = 0;
12630182Ssam } else
12730182Ssam howto &= ~RB_KDB;
12825865Ssam x.a_bss += 32*1024; /* slop */
12925865Ssam for (i = 0; i < x.a_bss; i++)
13025865Ssam *addr++ = 0;
13125865Ssam x.a_entry &= 0x1fffffff;
13237567Sbostic printf(" start 0x%lx\n", x.a_entry);
13325865Ssam mtpr(PADC, 0); /* Purge data cache */
13425865Ssam mtpr(PACC, 0); /* Purge code cache */
13529565Ssam mtpr(DCR, 1); /* Enable data cache */
13625865Ssam (*((int (*)()) x.a_entry))();
13729565Ssam return;
13825865Ssam shread:
13937567Sbostic printf("short read\n");
14030758Skarels return;
14125865Ssam }
142