summaryrefslogtreecommitdiff
path: root/sys/arch/mvme88k/stand/old/boot/main.c
blob: 0759c9f2644b2601acb596222bea326d17c3a611 (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
#include <sys/param.h>
#include <sys/reboot.h>
#include "bug.h"
#include "bugio.h"
#include "machine/exec.h"

int	readblk		__P((int, char *));
int	loados		__P((void));	
void	putchar		__P((char));
void	_main		__P((void));
void	tapefileseek	__P((int));

char Clun, Dlun;

#define	DEV_BSIZE		512
#define	KERNEL_LOAD_ADDR	0x10000
#if !defined(BUG_BLKSIZE)
#define	BUG_BLKSIZE		256
#endif /* BUG_BLKSIZE */
#define	sec2blk(x)	((x) * (DEV_BSIZE/BUG_BLKSIZE))

struct kernel {
	void *entry;
	void *symtab;
	void *esym;
	int	 bflags;
	int	 bdev;
	char *kname;
	void *smini;
	void *emini;
	unsigned int end_loaded;
} kernel;

int howto = 0;
int bootdev = 0;
int *miniroot;

void
putchar(char c)
{
	bugoutchr(c);
}

main(struct bugenv *env)
{
	printf("Clun %x Dlun %x\n", env->clun, env->dlun);
	Clun = (char)env->clun;
	Dlun = (char)env->dlun;
	loados();
	return;
}


loados(void)
{
	int i, size;
	register char *loadaddr = (char *)KERNEL_LOAD_ADDR; /* load addr 64k*/
	struct exec *hdr;
	int (*fptr)();
	int *esym;
	int cnt, strtablen, ret;
	char *addr;

	howto |= RB_SINGLE|RB_KDB;

	tapefileseek(2);	/* seek to file 2 - the OS */
	if (readblk(1, loadaddr) == -1) {
		printf("Unable to read blk 0\n");
		return 1;
	}
	hdr 	= (struct exec *)loadaddr;
	
	/* We only deal with ZMAGIC files */
	if ((int)hdr->a_entry != (int)(loadaddr + sizeof(struct exec))) {
		printf("a_entry != loadaddr + exec size\n");
	}
	size = hdr->a_text + hdr->a_data;
	size -= DEV_BSIZE; /* account for the block already read */

	printf("Loading [%x+%x", hdr->a_text, hdr->a_data);
	if (readblk(size / DEV_BSIZE, loadaddr + DEV_BSIZE) == -1) {
		printf("Error reading the OS\n");
		return 1;
	}

	/* zero out BSS */

	printf("+%x]", hdr->a_bss);
#if DEBUG
	printf("zero'd out %x (%x)\n", loadaddr + hdr->a_text + hdr->a_data,
			hdr->a_bss);
#endif
	memset(loadaddr + hdr->a_text + hdr->a_data, 0, hdr->a_bss);

	addr = loadaddr + hdr->a_text + hdr->a_data + hdr->a_bss;

	if (hdr->a_syms != 0 /* && !(kernel.bflags & RB_NOSYM)*/) {
		/*
		 * DDB expects the following layout:
		 * 	no. of syms
		 *	symbols
		 *	size of strtab
		 *	entries of strtab
		 * esym->...
		 * Where as size of strtab is part of strtab, we need
		 * to prepend the size of symtab to satisfy ddb.
		 * esym is expected to point past the last byte of
		 * string table, rouded up to an int.
		 */
		bcopy(&hdr->a_syms, addr, sizeof(hdr->a_syms));
		addr += 4;  /* account for a_syms copied above */
		printf (" + [ %x",hdr->a_syms);

		cnt = (hdr->a_syms + DEV_BSIZE - 1) & ~(DEV_BSIZE - 1);

		ret = readblk(cnt / DEV_BSIZE, addr);
		if (ret != 0) {
			printf("unable to load kernel\n");
			return 1;
		}

		esym = (void *) ((int)addr + hdr->a_syms);

		if ((int)addr + cnt <= (int)esym) {
			printf("missed loading count of symbols\n\r");
			return 1;
		}

		addr += cnt;

		strtablen = *esym;
#if 0
		printf("start load %x end load %x %x\n", addr,
			len, addr +len);
		printf("esym %x *esym %x\n",esym, len);
#endif
		/*
		 * If symbol table size is not a sector multiple, we
		 * already read part of the string table. Look at the
		 * part already read, and figure out the string table
		 * size. Also, adjust the size yet to read.
		 */
		if (hdr->a_syms != cnt) {
			/* already read part of the string table */
			strtablen -= (cnt - hdr->a_syms);
		}

		if (strtablen > 0) {
			printf(" + %x",*esym);

			cnt = (strtablen + DEV_BSIZE -1) & ~(DEV_BSIZE - 1);

			ret = readblk(cnt / DEV_BSIZE, addr);
			if (ret != 0) {
				printf("unable to load kernel\n");
				return 1;
			}
			addr += strtablen;
			printf(" ]\n");
		} else {
			printf("+ %x ]\n", *esym);
		}
		esym = (int *)(((int)esym) + *esym);
		esym = (int *)(((int)esym + 4 - 1) & ~3);

		kernel.symtab = (void *)hdr->a_syms;
		kernel.esym = esym;
	} else {
		kernel.symtab = 0;
		kernel.esym = 0;
	}

	kernel.end_loaded = (unsigned int)addr;
	miniroot = (int *)esym;
	miniroot = (int *)(((int)miniroot + 0x1000 - 1) & ~0xFFF);
	tapefileseek(3);	/* seek to file 3 - minroot */
	if (readblk(1000, miniroot) != 0) {
		printf("miniroot not loaded\n");
		addr = (char *)miniroot;
	} else {
		addr = (char *)((int)miniroot + 1000 * DEV_BSIZE);
	}
	printf("esym %x miniroot @ %x (ends @ %x)\n", esym, miniroot, addr);
#if 0
	{
		char *symaddr = (char *)0x01F00000;
		int  i;

		tapefileseek(4);	/* seek to file 4 - syms */
		readblk(1, symaddr);
		i = *symaddr;
		i = (i * 0x1C + 4 + DEV_BSIZE) & ~(DEV_BSIZE - 1);
		printf("loading %d symbols (%d sectors)\n",
			*symaddr, (i + 1) * DEV_BSIZE);
		readblk(i / DEV_BSIZE, symaddr + DEV_BSIZE);
		readblk(100, 0x01F00000);
	}
#endif

	fptr = (int (*)())hdr->a_entry;
	/*
	 * Args are passed as
	 * 	r2 howto
	 *	r3 end addr
	 *	r4 (Clun << 8) | Dlun & FF
	 *	r5 esym
	 *	r6 miniroot
	 */
	bootdev = ((Clun << 8) & 0xFF00 | Dlun & 0xFF) & 0xFFFF;
#if 0
	asm volatile ("or r2, r0, %0\n\tor r3, r0, %1\n\tor r4, r0, %2\n\tor r5, r0, %3\n\tor r6, r0, %4\n\tor r7, r0, %5"
		: /* no outputs */
		: "r" (howto), "r" (addr), "r" (Clun), "r" (Dlun), "r" (esym), "r" (miniroot)
		: "r2", "r3", "r4", "r5", "r6", "r7");
#endif /* 0 */
	(*fptr)(howto, addr, bootdev, esym, miniroot);	
	return 0;
}

int
readblk(int n, char *addr)
{
	struct bugdisk_io io;

	io.clun = Clun;
	io.dlun = Dlun;
	io.status = 0;
	io.addr = (void *)addr;
	io.fileno = 0; /* for tape reads, start io at current pos */
	io.nblks = sec2blk(n);
	io.flag = IGNOREFILENO;
	io.am = 0;
	bugdskrd(&io);
	if (io.status)
		return -1;
	return 0;
}

void
_main(void)
{
	return;
}

void
tapefileseek(int i)
{
	struct bugdisk_io io;
	void *addr = (void *)KERNEL_LOAD_ADDR; /* some number - don't care */

	io.clun = Clun;
	io.dlun = Dlun;
	io.status = 0;
	io.addr = addr;
	io.fileno = i; /* for tape reads, this is the file no. */
	io.nblks = 0;
	io.flag = 0; /* we want to turn off IFN and EOF bits */
	io.am = 0;
	bugdskrd(&io);
}

__main()
{
}