File: main.c

package info (click to toggle)
quik 2.1-9
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 364 kB
  • ctags: 553
  • sloc: ansic: 3,791; asm: 475; makefile: 103; perl: 74; sh: 44
file content (605 lines) | stat: -rw-r--r-- 14,530 bytes parent folder | download | duplicates (3)
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
/* Second stage boot loader
   
   Copyright (C) 1996 Paul Mackerras.

   Because this program is derived from the corresponding file in the
   silo-0.64 distribution, it is also

   Copyright (C) 1996 Pete A. Zaitcev
   		 1996 Maurizio Plaza
   		 1996 David S. Miller
   		 1996 Miguel de Icaza
   		 1996 Jakub Jelinek
   
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.
   
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */

#undef BOOTINFO

#include "quik.h"
#include "prom.h"
#include "gzip.h"
#define __KERNEL__
#include <elf.h>
#include <layout.h>
#ifdef BOOTINFO
#include <asm/machdep.h>
#endif

#define TMP_BUF		((unsigned char *) 0x14000)
#define TMP_END		((unsigned char *) SECOND_BASE)
#define ADDRMASK	0x0fffffff

char quik_conf[40];
int quik_conf_part;
unsigned int is_chrp = 0;

extern int start;
extern char bootdevice[];
extern unsigned char *out;
extern unsigned int input_len;

int useconf = 0;
static int pause_after;
static char *pause_message = "Type go<return> to continue.\n";
static char given_bootargs[512];
static int given_bootargs_by_user = 0;
static unsigned char *elf_image;

#define DEFAULT_TIMEOUT		-1

void fatal(const char *msg)
{
    qprintf("\nFatal error: %s\n", msg);
}

void maintabfunc (void)
{
    if (useconf) {
	cfg_print_images();
	qprintf("boot: %s", cbuff);
    }
}

void parse_name(char *imagename, int defpart, char **device,
		int *part, char **kname)
{
    int n;
    char *endp;

    /*
     * Assume partition 2 if no other has been explicitly set
     * in the config file on chrp -- Cort
     */
    if ( defpart == -1 )
	    defpart = 2;

    *kname = strchr(imagename, ':');
    if (!*kname) {
	*kname = imagename;
	*device = 0;
    } else {
	**kname = 0;
	(*kname)++;
	*device = imagename;
    }
    n = strtol(*kname, &endp, 0);
    if (endp != *kname) {
	*part = n;
	*kname = endp;
    } else if (defpart != -1)
	*part = defpart;
    else
	*part = 0;
    /* Range */
    if (**kname == '[')
	return;
    /* Path */
    if (**kname != '/')
	*kname = 0;
}

void
word_split(char **linep, char **paramsp)
{
    char *p;

    *paramsp = "\0";
    p = *linep;
    if (p == 0)
	return;
    while (*p == ' ')
	++p;
    if (*p == 0) {
	*linep = 0;
	return;
    }
    *linep = p;

    /* Don't split OF command line. */
    if (*p == '$')
        return;

    while (*p != 0 && *p != ' ')
	++p;
    while (*p == ' ')
	*p++ = 0;
    if (*p != 0)
	*paramsp = p;
}

char *
make_params(char *label, char *params)
{
    char *p, *q;
    static char buffer[2048];

    q = buffer;
    *q = 0;

    p = cfg_get_strg(label, "literal");
    if (p) {
	strcpy(q, p);
	q = strchr(q, 0);
	if (*params) {
	    if (*p)
		*q++ = ' ';
	    strcpy(q, params);
	}
	return buffer;
    }

    p = cfg_get_strg(label, "root");
    if (p) {
	strcpy (q, "root=");
	strcpy (q + 5, p);
	q = strchr (q, 0);
	*q++ = ' ';
    }
    if (cfg_get_flag(label, "read-only")) {
	strcpy (q, "ro ");
	q += 3;
    }
    if (cfg_get_flag(label, "read-write")) {
	strcpy (q, "rw ");
	q += 3;
    }
    p = cfg_get_strg(label, "ramdisk");
    if (p) {
	strcpy (q, "ramdisk=");
	strcpy (q + 8, p);
	q = strchr (q, 0);
	*q++ = ' ';
    }
    p = cfg_get_strg (label, "append");
    if (p) {
	strcpy (q, p);
	q = strchr (q, 0);
	*q++ = ' ';
    }
    *q = 0;
    pause_after = cfg_get_flag (label, "pause-after");
    p = cfg_get_strg(label, "pause-message");
    if (p)
	pause_message = p;
    if (*params)
	strcpy(q, params);

    return buffer;
}

void boot_kernel_initrd(void *prom_entry,char *device, int part, char *kname, 
					    char *params, char *initrdname)
{
    int i, len, image_len, image_memsize,initrd_size=0;
    Elf32_Ehdr *e;
    Elf32_Phdr *p;
    unsigned load_loc, entry, start,off;
    unsigned char *initrd_start=NULL;

	if (!kname)
		return;

	if (!load_file(device, part, kname, IMAGE_BUF, IMAGE_END, 
				   &image_len, 1, 0)) {
	    qprintf ("\nImage not found.... try again\n");
	    return;
	}

	if (image_len > IMAGE_END - IMAGE_BUF) {
	    qprintf("\nImage is too large (%u > %u)\n", image_len,
		   IMAGE_END - IMAGE_BUF);
	    return;
	}

	if(memcmp(IMAGE_BUF, GZIP_MAGIC,2) == 0 ||
	   memcmp(IMAGE_BUF, OLD_GZIP_MAGIC, 2) == 0) {
		qprintf("compressed image !\n");
		elf_image=IMAGE_BUF + ((image_len + 3) & (~3));
		out=elf_image;
		input_len=image_len;
		uncompress_kernel(0);
	}
	else
		elf_image=IMAGE_BUF;

	/* By this point the first sector is loaded (and the rest of */
	/* the kernel) so we check if it is an executable elf binary. */

	e = (Elf32_Ehdr *) elf_image;
	if (!(e->e_ident[EI_MAG0] == ELFMAG0 &&
	      e->e_ident[EI_MAG1] == ELFMAG1 &&
	      e->e_ident[EI_MAG2] == ELFMAG2 &&
	      e->e_ident[EI_MAG3] == ELFMAG3)) {
	    qprintf ("\n%s: unknown image format\n", kname);
	    return;
	}

	if (e->e_ident[EI_CLASS] != ELFCLASS32
	    || e->e_ident[EI_DATA] != ELFDATA2MSB) {
	    qprintf("Image is not a 32bit MSB ELF image\n");
	    return;
	}
	len = 0;
	image_memsize = 0;
	load_loc = 0;
	off = 0;

	p = (Elf32_Phdr *) (elf_image + e->e_phoff);
	for (i = 0; i < e->e_phnum; ++i, ++p) {
	    if (p->p_type != PT_LOAD || p->p_offset == 0)
		continue;
	    if (len == 0) {
		off = p->p_offset;
		len = p->p_filesz;
		load_loc = p->p_vaddr & ADDRMASK;
		image_memsize = p->p_memsz;
	    } else {
		len = p->p_offset + p->p_filesz - off;
		image_memsize =  p->p_offset + p->p_memsz ;
	    }
	}
	if (len == 0) {
	    qprintf("Cannot find a loadable segment in ELF image\n");
	    return;
	}
	entry = e->e_entry & ADDRMASK;
	if (len + off > image_len)
	    len = image_len - off;

	qprintf("initrd imagename = %s, mem_size: %d\n",initrdname,image_memsize);
	image_memsize=(image_memsize + 0x1000) & (~0xfff);
	if(initrdname[0]!=0) {
		initrd_start=elf_image + image_memsize + off + 0x80000;
		qprintf("initrd_start: %p\n",initrd_start);
		if(!load_file(device, part, initrdname, initrd_start,
			      IMAGE_END, &image_len, 1, 0)) {
				qprintf("\ninitrd image not found....\n");
		}
		else if (image_len > IMAGE_END - initrd_start) {
				qprintf("\ninitrd Image is too large (%u > %u)\n", image_len,
						           IMAGE_END - initrd_start);
	    }
		else {
				initrd_size=image_len;
		}
	}

    set_bootargs(params);

    /* Execute in place, the kernel can relocate itself */
    entry = (entry - load_loc) + off + (unsigned int)elf_image;
    load_loc = off + (unsigned int)elf_image;
    prom_map((unsigned int)load_loc, (unsigned int)load_loc, len+0x200000 + 
					initrd_size);

    flush_cache(load_loc, len);

    close();
    if (pause_after) {
        qprintf("%s", pause_message);
	prom_pause();
        qprintf("\n");
    }

    /*
     * For the sake of the Open Firmware XCOFF loader, the entry
     * point may actually be a procedure descriptor.
     */
    start = *(unsigned *)entry;
    /* new boot strategy - see head.S in the kernel for more info -- Cort */
    if (start == 0x60000000/* nop */ )
	    start = load_loc;
    /* not the new boot strategy, use old logic -- Cort */
    else
    {
	    if (start < load_loc || start >= load_loc + len
		|| ((unsigned *)entry)[2] != 0)
		    /* doesn't look like a procedure descriptor */
		    start += entry;
    }
    qprintf("Starting at %x, %p, %d\n", start,initrd_start,initrd_size);
#ifdef BOOTINFO    
    /* setup the bootinfo */
    binf.magic_start = bin.magic_end = BOOT_INFO_MAGIC;
    sqprintf( binf.cmd_line, "%s", params );
    sqprintf( binf.boot_loader, "Quik" );
    if ( is_chrp )
	    binf._machine = _MACH_chrp;
    else
	    binf._machine = _MACH_Pmac;
    binf.initrd_start = binf.initrd_size = 0;
    binf.systemmap_start = binf.systemmap_size = 0;
    /* put the boot info at the nearest 0x10000 boundry from the end -- Cort */
    memcpy( ((load_loc + len)+0x10000) & ~0x10000, binf,
	    sizeof(struct boot_info) );
#endif    

    (* (void (*)()) start)(initrd_start,initrd_size, prom_entry, 0, 0);

    prom_exit();
}

int get_char_timeout(int timeout) {

    int c=-1,beg=0,end;

    beg = get_ms();
    if (timeout > 0) {
	    end = beg + 100 * timeout;
	    do {
		c = nbgetchar();
	    } while (c == -1 && get_ms() <= end);
    }
    if (c == -1)
	    c = '\r';
   
    return c;

}

int get_params(char *imagename, char **device, int *part, char **kname,  
	           char **params, char **initrdname)
{
    int defpart = -1;
    char *defdevice = 0;
    char *p, *endp;
    int n;
    char *label;

    pause_after = 0;
    *initrdname = "";

    label = 0;
    defdevice = bootdevice;

    if (useconf) {
	defdevice = cfg_get_strg(0, "device");
	p = cfg_get_strg(0, "partition");
	if (p) {
	    n = strtol(p, &endp, 10);
	    if (endp != p && *endp == 0)
		defpart = n;
	}
	p = cfg_get_strg(0, "pause-message");
	if (p)
	    pause_message = p;
	p = cfg_get_strg(imagename, "image");
	if (p && *p) {
	    label = imagename;
	    imagename = p;
	    defdevice = cfg_get_strg(label, "device");
	    *initrdname = cfg_get_strg(label, "initrd");

	    p = cfg_get_strg(label, "partition");
	    if (p) {
		n = strtol(p, &endp, 10);
		if (endp != p && *endp == 0)
		    defpart = n;
	    }

	    *params = make_params(label, *params);
	}

    }
    if (!strcmp (imagename, "halt")) {
	prom_pause();
	*kname = 0;
	return 0;
    }

    if (imagename[0] == '$') {
	/* forth command string */
	call_prom("interpret", 1, 1, imagename+1);
	*kname = 0;
	return 0;
    }

    parse_name(imagename, defpart, device, part, kname);
    if (!*device)
	*device = defdevice;
    if (!*kname)
	    qprintf(
"Enter the kernel image name as [device:][partno]/path, where partno is a\n"
"number from 0 to 16.  Instead of /path you can type [mm-nn] to specify a\n"
"range of disk blocks (512B)\n");

    return 0;
}

/*
 * Print the specified message file.
 */
static void
print_message_file(char *p)
{
    char *q, *endp;
    int len = 0;
    int n, defpart = -1;
    char *device, *kname;
    int part;

    q = cfg_get_strg(0, "partition");
    if (q) {
	n = strtol(q, &endp, 10);
	if (endp != q && *endp == 0)
	    defpart = n;
    }
    parse_name(p, defpart, &device, &part, &kname);
    if (kname) {
	if (!device)
	    device = cfg_get_strg(0, "device");
	if (load_file(device, part, kname, TMP_BUF, TMP_END, &len, 1, 0)) {
	    TMP_BUF[len] = 0;
	    qprintf("\n%s", (char *)TMP_BUF);
	}
    }
}

/* Here we are launched */
int main(void *prom_entry, struct first_info *fip, unsigned long id)
{
    char *kname, *params, *device, *initrdname, *q, *imagename;
    int part,c;
    int fileok = 0,timeout;
    extern char __bss_start, _end;
    struct first_info real_fip;
    char bootargs[512];

#ifdef BOOTINFO
    struct boot_info binf;
#endif

    /*
     * If we're not being called by the first stage bootloader
     * and we don't have the BootX signature assume we're
     * chrp. -- Cort
     */

    if ( (id != 0xdeadbeef) && ((unsigned long)prom_entry != 0x426f6f58) )
    {
	    is_chrp = 1;
	    prom_entry = (void *)id;	/* chrp passes prom_entry in r5 */
	    /*
	     * Make our own information packet with some default values
	     * we can assume are true on chrp.
	     */
	    fip = &real_fip;
	    strcpy( fip->quik_vers, "chrp" );
	    /*
	     * Assume root partition is partition 2.  We should
	     * scan the disk looking for a linux FS with /etc/quik.conf.
	     * -- Cort
	     */
	    fip->conf_part = 2;
	    strcpy( fip->conf_file, "/etc/quik.conf" );
    }

    if ( (unsigned long)prom_entry == 0x426f6f58 )
    {
	    qprintf("BootX launched us\n");
	    prom_entry = (void *)id;
	    /*
	     * These should come from the bootx info.
	     */
	    fip = &real_fip;
	    strcpy( fip->quik_vers, "BootX" );
	    /* Assume root partition is partition 9 -- Cort */
	    fip->conf_part = 9;
	    strcpy( fip->conf_file, "/etc/quik.conf" );
    }
	    
    memset(&__bss_start, 0, &_end - &__bss_start);
    prom_init(prom_entry);
    qprintf("Second-stage QUIK loader\n");
    qprintf("Welcome to quik. mate is good.\n");

    if (diskinit() == -1)
	prom_exit();

    quik_conf_part = fip->conf_part;
    strncpy(quik_conf, fip->conf_file, sizeof(fip->conf_file));
    if (*quik_conf && quik_conf_part >= 0) {
	int len;
	fileok = load_file(0, quik_conf_part, quik_conf,
			   TMP_BUF, TMP_END, &len, 1, 0);
	if (!fileok || (unsigned) len >= 65535)
	    qprintf("\nCouldn't load %s\n", quik_conf);
	else {
	    char *p;
	    if (cfg_parse(quik_conf, (char *)TMP_BUF, len) < 0)
	        qprintf ("Syntax error or read error in %s.\n", quik_conf);
	    useconf = 1;
	    p = cfg_get_strg(0, "init-code");
	    if (p)
		call_prom("interpret", 1, 1, p);
	    p = cfg_get_strg(0, "init-message");
	    if (p)
		qprintf("%s", p);
	    p = cfg_get_strg(0, "message");
	    if (p)
		print_message_file(p);
	}
    } else
	qprintf ("\n");

    timeout = DEFAULT_TIMEOUT;
    if (useconf && (q = cfg_get_strg(0, "timeout")) != 0 && *q != 0)
        timeout = strtol(q, NULL, 0);

    prom_get_chosen("bootargs", bootargs, sizeof(bootargs));
    imagename = bootargs;
qprintf("chosen/bootargs = %s\n",imagename);
    word_split(&imagename, &params);

    qprintf("boot: ");
    c=get_char_timeout(timeout);

    if (c == '\r' && !is_chrp) {
		qprintf("%s", imagename);
		if (params)
	    	qprintf(" %s", params);
		qprintf("\n");

		if(imagename[0]!=0) {
         	get_params(imagename, &device, &part, &kname, &params, &initrdname);
	     	boot_kernel_initrd(prom_entry,device,part,kname,params,initrdname);
			qprintf("boot: ");
		}
	}

    for (;;) {
   		if (c == '\r' && useconf) {
			imagename = cfg_get_default();
			qprintf("%s\n", imagename);
		}
		else {
			cmdinit();
			cmdedit(maintabfunc, c);
			qprintf("\n");
			strcpy(given_bootargs, cbuff);
			given_bootargs_by_user = 1;
			imagename = cbuff;
			word_split(&imagename, &params);
		}
		get_params(imagename, &device, &part, &kname, &params, &initrdname);
		boot_kernel_initrd(prom_entry,device,part,kname,params,initrdname);

		params="";
    	qprintf("boot: ");
   		c=getchar();
    }
  
}