Training courses

Kernel and Embedded Linux

Bootlin training courses

Embedded Linux, kernel,
Yocto Project, Buildroot, real-time,
graphics, boot time, debugging...

Bootlin logo

Elixir Cross Referencer

  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
/*
 *	Load and boot NetBSD kernel on Human68k
 *
 *	written by ITOH Yasufumi
 *	public domain
 *
 *	loadbsd [-hvV] [-abDs] [-r root_device] netbsd
 *
 *	loadbsd options:
 *		-h	help
 *		-V	print version and exit
 *
 *	kernel options:
 *		-a	auto boot, opposite of -s
 *		-s	single user boot (default)
 *		-D	enter kernel debugger
 *		-b	ask root device
 *		-r	specify root device
 *		-q	quiet boot
 *		-v	verbose boot (also turn on verbosity of loadbsd)
 *
 *	$NetBSD: loadbsd.c,v 1.14 2011/02/21 02:31:59 itohy Exp $
 */

#include <sys/cdefs.h>

__RCSID("$NetBSD: loadbsd.c,v 1.14 2011/02/21 02:31:59 itohy Exp $");
#define VERSION	"$Revision: 1.14 $ $Date: 2011/02/21 02:31:59 $"

#include <sys/types.h>		/* ntohl */
#include <sys/reboot.h>
#include <sys/param.h>		/* ALIGN, ALIGNBYTES */
#include <a.out.h>
#include <sys/exec_elf.h>
#include <string.h>
#include <machine/bootinfo.h>

#include <dos.h>
#include <iocs.h>
#include "../common/xprintf.h"
#include "trampoline.h"

#define DEFAULT_ROOTDEVNAME	"sd@0,0:a"

#define ISDIGIT(c)	((c) >= '0' && (c) <= '9')

#define GETDECIMAL(var, str)	\
	do {	var *= 10; var += *str++ - '0'; } while (ISDIGIT(*str))

static const char *lookupif(const char *name,
				 unsigned *pif, unsigned *punit);
static void get_current_scsi_interface(unsigned *pif, unsigned *punit);
static int bootdev(const char *devstr);
static struct tramparg *read_kernel(const char *fn);
static int chkmpu(void);
static __dead void usage(int status, const char *msg)
					__attribute__((noreturn));

int main(int argc, char *argv[]);

int opt_v;
int opt_N;
const char *kernel_fn;

const struct hatbl {
	char name[4];
	unsigned short id;
} hatable[] = {
	X68K_BOOT_SCSIIF_LIST
};

/*
 * parse interface name
 * return the next position
 */
static const char *
lookupif(const char *name, unsigned *pif, unsigned *punit)
{
	unsigned u, unit;
	const char *p;

	for (u = 0; u < sizeof hatable / sizeof hatable[0]; u++) {
		const char *n;

		for (n = hatable[u].name, p = name; *n && *n == *p; n++, p++)
			;
		if (!*n)
			goto found;
	}
	/* not found */
	return (char *) 0;

found:
	if (*p == '@')
		p++;

	/* get unit # */
	if (!ISDIGIT(*p))
		return (char *) 0;

	unit = 0;
	GETDECIMAL(unit, p);

	*pif = hatable[u].id;
	*punit = unit;

	return p;
}

/*
 * if the SCSI interface is not specified, use the current one
 */
static void
get_current_scsi_interface(unsigned *pif, unsigned *punit)
{
	unsigned binf;
	char *bootrom;
	int bus_err_buf;

	binf = (unsigned) IOCS_BOOTINF();
	if (binf < 0x00fc0000)
		return;			/* not booted from SCSI */

	bootrom = (char *) (binf & 0x00ffffe0);
	if (IOCS_B_LPEEK(bootrom + 0x24) == 0x53435349 &&	/* 'SCSI' */
	    IOCS_B_WPEEK(bootrom + 0x28) == 0x494E) {		/* 'IN' */
		/* spc0 */
		*pif = X68K_BOOT_SCSIIF_SPC;
		*punit = 0;
	} else if (DOS_BUS_ERR(&bus_err_buf, (void *)EXSPC_BDID, 1)) {
		/* mha0 */
		*pif = X68K_BOOT_SCSIIF_MHA;
		*punit = 0;
	} else {
		/* spc1 */
		*pif = X68K_BOOT_SCSIIF_SPC;
		*punit = 1;
	}
}

/*
 * parse device name
 *
 * [/<controller>@<unit>/]<device>@<unit>[,<lun>][:<partition>]
 *
 * <unit> must be target SCSI ID if <device> is a SCSI device
 *
 *	full form:
 *		/spc@0/sd@1,2:e
 *
 *	partial form:
 *		/mha@0/sd@1	= /mha@0/sd@1,0:a
 *		sd@1:e		= /current_device/sd@1,0e
 *		sd@1,2:e	= /current_device/sd@1,2:e
 */

const struct devtbl {
	char name[3];
	u_char major;
} devtable[] = {
	X68K_BOOT_DEV_LIST,
	X68K_BOOT_NETIF_LIST
};

static int
bootdev(const char *devstr)
{
	unsigned u;
	unsigned major, unit, lun, partition;
	int dev;
	const char *s = devstr;
	unsigned interface = 0, unit_if = 0;

	if (*s == '/') {
		/*
		 * /<interface>/<device>"
		 * "/spc@1/sd@2,3:e"
		 */
		while (*++s == '/')	/* skip slashes */
			;
		if (!strchr(s, '/'))
			xerrx(1, "%s: bad format", devstr);

		if (!(s = lookupif(s, &interface, &unit_if)))
			xerrx(1, "%s: unknown interface", devstr);

		while (*s == '/')	/* skip slashes */
			s++;
	} else {
		/* make lint happy */
		interface = 0;
		unit_if = 0;
	}

	/* allow r at the top */
	if (*s == 'r')
		s++;

	for (u = 0; u < sizeof devtable / sizeof devtable[0]; u++)
		if (s[0] == devtable[u].name[0] && s[1] == devtable[u].name[1])
			goto found;

	/* not found */
	xerrx(1, "%s: unknown device", devstr);

found:	major = devtable[u].major;

	/*
	 * <type>@unit[,lun][:part]
	 * "sd@1,3:a"
	 */

	/* get device unit # */
	s += 2;
	if (*s == '@')
		s++;
	if (!*s)
		xerrx(1, "%s: missing unit number", devstr);
	if (!ISDIGIT(*s))
		xerrx(1, "%s: wrong device", devstr);

	unit = 0;
	GETDECIMAL(unit, s);

	lun = 0;
	if (*s == ',') {
		s++;
		if (!ISDIGIT(*s))
			xerrx(1, "%s: wrong device", devstr);
		GETDECIMAL(lun, s);
	}

	/* get device partition */
	if (*s == ':')
		s++;
	if (!*s)
		partition = 0;	/* no partition letter -- assuming 'a' */
	else if (!s[1])
		partition = *s - 'a';
	else
		xerrx(1, "%s: wrong partition letter", devstr);

	/*
	 * sanity check
	 */
	if (unit_if >= 16)
		xerrx(1, "%s: interface unit # too large", devstr);
	if (unit >= 16)
		xerrx(1, "%s: device unit # too large", devstr);
	if (lun >= 8)
		xerrx(1, "%s: SCSI LUN >= 8 is not supported yet", devstr);
	if (partition >= 16)
		xerrx(1, "%s: unsupported partition", devstr);

	/*
	 * encode device to be passed to kernel
	 */
	if (X68K_BOOT_DEV_IS_SCSI(major)) {
		/*
		 * encode SCSI device
		 */
		if (interface == 0)
			get_current_scsi_interface(&interface, &unit_if);

		dev = X68K_MAKESCSIBOOTDEV(major, interface, unit_if,
						unit, lun, partition);
	} else {
		/* encode non-SCSI device */
		dev = X68K_MAKEBOOTDEV(major, unit, partition);
	}

	if (opt_v)
		xwarnx("%s: major %u, if %u, un_if %u, unit %u, lun %u, partition %u; bootdev 0x%x",
			devstr, major, interface, unit_if, unit, lun, partition, dev);

	return dev;
}

#define LOADBSD
#include "../common/exec_sub.c"

/*
 * read kernel and create trampoline
 *
 *	|----------------------| <- allocated buf addr
 *	| kernel image         |
 *	~ (exec file contents) ~
 *	|                      |
 *	|----------------------| <- return value (entry addr of trampoline)
 *	| struct tramparg      |
 *	| (trampoline args)    |
 *	|----------------------|
 *	| trampoline code      |
 *	| (in assembly)        |
 *	|----------------------|
 */
static struct tramparg *
read_kernel(const char *fn)
{
	int fd;
	union dos_fcb *fcb;
	size_t filesize, nread;
	void *buf;
	struct tramparg *arg;
	size_t size_tramp = end_trampoline - trampoline;

	kernel_fn = fn;

	if ((fd = DOS_OPEN(fn, 0x00)) < 0)	/* read only */
		xerr(1, "%s: open", fn);

	if ((int)(fcb = DOS_GET_FCB_ADR(fd)) < 0)
		xerr(1, "%s: get_fcb_adr", fn);

	/*
	 * XXX FCB is in supervisor area
	 */
	/*if (fcb->blk.mode != 0)*/
	if (IOCS_B_BPEEK((char *)fcb + 1) & 0x80)
		xerrx(1, "%s: Not a regular file", fn);

	/*filesize = fcb->blk.size;*/
	filesize = IOCS_B_LPEEK(&fcb->blk.size);

	if (filesize < sizeof(Elf32_Ehdr))
		xerrx(1, "%s: Unknown format", fn);

	/*
	 * read entire file
	 */
	if ((int)(buf = DOS_MALLOC(filesize + ALIGNBYTES
				   + sizeof(struct tramparg)
				   + size_tramp + SIZE_TMPSTACK)) < 0)
		xerr(1, "read_kernel");

	if ((nread = DOS_READ(fd, buf, filesize)) != filesize) {
		if ((int)nread < 0)
			xerr(1, "%s: read", fn);
		else
			xerrx(1, "%s: short read", fn);
	}

	if (DOS_CLOSE(fd) < 0)
		xerr(1, "%s: close", fn);

	/*
	 * address for argument for trampoline code
	 */
	arg = (struct tramparg *) ALIGN((char *) buf + nread);

	if (opt_v)
		xwarnx("trampoline arg at %p", arg);

	xk_load(&arg->xk, buf, 0 /* XXX load addr should not be fixed */);

	/*
	 * create argument for trampoline code
	 */
	arg->bsr_inst = TRAMP_BSR + sizeof(struct tramparg) - 2;
	arg->tmp_stack = (char *) arg + sizeof(struct tramparg)
				+ size_tramp + SIZE_TMPSTACK;
	arg->mpu_type = IOCS_MPU_STAT() & 0xff;

	arg->xk.d5 = IOCS_BOOTINF();	/* unused for now */
#if 0
	/* filled afterwards */
	arg->xk.rootdev = 
	arg->xk.boothowto = 
#endif

	if (opt_v)
		xwarnx("args: mpu %d, image %p, load 0x%x, entry 0x%x",
			arg->mpu_type, arg->xk.sec[0].sec_image,
			arg->xk.load_addr, arg->xk.entry_addr);

	/*
	 * copy trampoline code
	 */
	if (opt_v)
		xwarnx("trampoline code at %p (%u bytes)",
			(char *) arg + sizeof(struct tramparg), size_tramp);

	memcpy((char *) arg + sizeof(struct tramparg), trampoline, size_tramp);

	return arg;
}

/*
 * MC68000/010		-> return zero
 * MC68020 and later	-> return nonzero
 */
static int
chkmpu(void)
{
	register int ret __asm("%d0");

	__asm("| %0 <- this must be %%d0\n\
	moveq	#1,%%d0\n\
	.long	0x103B02FF	| foo: moveb %%pc@((foo+1)-foo-2:B,d0:W:2),%%d0\n\
	|	      ^ ^\n\
	| %%d0.b	= 0x02	(68000/010)\n\
	|	= 0xff	(68020 and later)\n\
	bmis	1f\n\
	moveq	#0,%%d0		| 68000/010\n\
1:"	: "=d" (ret));

	return ret;
}

static __dead void
usage(int status, const char *msg)
{
	extern const char *const __progname;

	if (msg)
		xwarnx("%s", msg);

	xerrprintf("\
%s [-hvV] [-abDs] [-r root_device] netbsd\n\
\n\
loadbsd options:\n\
\t-h	help\n\
\t-v	verbose\n\
\t-V	print version and exit\n\
\n\
kernel options:\n\
\t-a	auto boot, opposite of -s\n\
\t-s	single user boot (default)\n\
\t-D	enter kernel debugger\n\
\t-b	ask root device\n\
\t-r	specify root device (default %s)\n\
\t	format:  [/interface/]device@unit[,lun][:partition]\n\
\t	    interface: one of  spc@0, spc@1, mha@0\n\
\t		       (current boot interface if omitted)\n\
\t	    device:    one of  fd, sd, cd, md, ne\n\
\t	    unit:      device unit number (SCSI ID for SCSI device)\n\
\t	    lun:       SCSI LUN # (0 if omitted)\n\
\t	    partition: partition letter ('a' if omitted)\n\
", __progname, DEFAULT_ROOTDEVNAME);

	DOS_EXIT2(status);
}

int
main(int argc, char *argv[])
{
	char *rootdevname = 0;
	int rootdev;
	u_long boothowto = RB_SINGLE;
	const char *kernel;
	char *p, **flg, **arg;
	struct tramparg *tramp;
	struct dos_dregs regs;	/* unused... */
	int i;

	/* parse options */
	for (arg = flg = argv + 1; (p = *flg) && *p == '-'; ) {
		int c;

		while ((c = *++p))
			switch (c) {
			case 'h':
				usage(0, (char *) 0);
				/* NOTREACHED */
				break;
			case 'N':	/* don't actually execute kernel */
				opt_N = 1;
				break;
			case 'v':
				opt_v = 1;
				boothowto |= AB_VERBOSE; /* XXX */
				break;
			case 'V':
				xprintf("loadbsd %s\n", VERSION);
				return 0;

			/*
			 * kernel boot flags
			 */
			case 'r':
				if (rootdevname)
					usage(1, "multiple -r flags");
				else if (!*++arg)
					usage(1, "-r requires device name");
				else
					rootdevname = *arg;
				break;
			case 'b':
				boothowto |= RB_ASKNAME;
				break;
			case 'a':
				boothowto &= ~RB_SINGLE;
				break;
			case 's':
				boothowto |= RB_SINGLE;
				break;
			case 'D':
				boothowto |= RB_KDB;
				break;
			case 'q':
				boothowto |= AB_QUIET;
				break;

			default:
				usage(1, (char *) 0);
				/* NOTREACHED */
				break;
			}
		flg = ++arg;
	}

	/* check MPU */
	if (chkmpu() == 0)
		xerrx(1, "Can't boot NetBSD on 68000/010");

	argc -= arg - argv;
	argv = arg;

	if (argc != 1)
		usage(1, (char *) 0);

	kernel = *argv;

	rootdev = bootdev(rootdevname ? rootdevname : DEFAULT_ROOTDEVNAME);

	if (opt_v)
		xwarnx("boothowto 0x%x", boothowto);

	tramp = read_kernel(kernel);

	tramp->xk.rootdev = rootdev;
	tramp->xk.boothowto = boothowto;

	/*
	 * we never return, and make sure the disk cache
	 * be flushed (if write-back cache is enabled)
	 */
	if (opt_v)
		xwarnx("flush disk cache...");

	i = DOS_FFLUSH_SET(1);		/* enable fflush */
	DOS_FFLUSH();			/* then, issue fflush */
	(void) DOS_FFLUSH_SET(i);	/* restore old mode just in case */

	/*
	 * the program assumes the MPU caches off
	 */
	if (opt_v)
		xwarnx("flush and disable MPU caches...");

	IOCS_CACHE_MD(-1);		/* flush */
	if (!opt_N)
		IOCS_CACHE_MD(0);	/* disable both caches */

	if (opt_v)
		xwarnx("Jumping to the kernel.  Good Luck!");

	if (opt_N)
		xerrx(0, "But don't actually do it.");

	DOS_SUPER_JSR((void (*)(void)) tramp, &regs, &regs);

	/* NOTREACHED */

	xwarnx("??? return from kernel");

	return 1;
}