Home | History | Annotate | Line # | Download | only in bootelf
      1  1.6       dsl /*	$NetBSD: boot.c,v 1.6 2009/03/14 21:04:13 dsl Exp $	*/
      2  1.1    itojun 
      3  1.1    itojun /*-
      4  1.1    itojun  * Copyright (C) 1999 Tsubai Masanari.  All rights reserved.
      5  1.1    itojun  *
      6  1.1    itojun  * Redistribution and use in source and binary forms, with or without
      7  1.1    itojun  * modification, are permitted provided that the following conditions
      8  1.1    itojun  * are met:
      9  1.1    itojun  * 1. Redistributions of source code must retain the above copyright
     10  1.1    itojun  *    notice, this list of conditions and the following disclaimer.
     11  1.1    itojun  * 2. Redistributions in binary form must reproduce the above copyright
     12  1.1    itojun  *    notice, this list of conditions and the following disclaimer in the
     13  1.1    itojun  *    documentation and/or other materials provided with the distribution.
     14  1.1    itojun  * 3. The name of the author may not be used to endorse or promote products
     15  1.1    itojun  *    derived from this software without specific prior written permission.
     16  1.1    itojun  *
     17  1.1    itojun  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     18  1.1    itojun  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     19  1.1    itojun  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     20  1.1    itojun  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     21  1.1    itojun  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     22  1.1    itojun  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     23  1.1    itojun  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     24  1.1    itojun  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     25  1.1    itojun  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     26  1.1    itojun  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     27  1.1    itojun  */
     28  1.1    itojun 
     29  1.1    itojun #include <sys/param.h>
     30  1.1    itojun #include <sys/sysctl.h>
     31  1.2    tsubai #include <machine/cpu.h>
     32  1.1    itojun #include <err.h>
     33  1.1    itojun #include <stdio.h>
     34  1.1    itojun #include <stdlib.h>
     35  1.1    itojun #include <unistd.h>
     36  1.1    itojun 
     37  1.3  uebayasi #include <lib/libsa/loadfile.h>
     38  1.1    itojun 
     39  1.1    itojun #if 1
     40  1.1    itojun # define DPRINTF printf
     41  1.1    itojun #else
     42  1.1    itojun # define DPRINTF while (0) printf
     43  1.1    itojun #endif
     44  1.1    itojun 
     45  1.4       dsl void LoadAndReset(void *);
     46  1.1    itojun 
     47  1.1    itojun char *netbsd = "/netbsd";
     48  1.1    itojun 
     49  1.1    itojun int
     50  1.6       dsl main(int argc, char *argv[])
     51  1.1    itojun {
     52  1.1    itojun 	u_long marks[MARK_MAX];
     53  1.1    itojun 	u_long start, entry;
     54  1.1    itojun 	int fd, sz, i;
     55  1.1    itojun 	u_long *ap;
     56  1.1    itojun 	u_long cksum, *lp;
     57  1.1    itojun 	void *image;
     58  1.1    itojun 
     59  1.1    itojun 	/* use the specified kernel if any */
     60  1.1    itojun 	if (argc > 1)
     61  1.1    itojun 		netbsd = argv[1];
     62  1.1    itojun 
     63  1.1    itojun 	DPRINTF("loading %s...\n", netbsd);
     64  1.1    itojun 
     65  1.1    itojun 	/* count kernel size */
     66  1.1    itojun 	marks[MARK_START] = 0;
     67  1.1    itojun 	fd = loadfile(netbsd, marks, COUNT_ALL);
     68  1.1    itojun 	if (fd == -1)
     69  1.1    itojun 		err(0, "loadfile(1)");
     70  1.1    itojun 	close(fd);
     71  1.1    itojun 
     72  1.1    itojun 	sz = marks[MARK_END] - marks[MARK_START];
     73  1.1    itojun 	start = marks[MARK_START];
     74  1.1    itojun 	entry = marks[MARK_ENTRY];
     75  1.1    itojun 
     76  1.1    itojun 	DPRINTF("size  = 0x%x\n", sz);
     77  1.1    itojun 	DPRINTF("start = 0x%lx\n", start);
     78  1.1    itojun 
     79  1.1    itojun 	ap = malloc(sz + 2 * sizeof(long));
     80  1.1    itojun 	if (ap == NULL)
     81  1.1    itojun 		err(0, "malloc");
     82  1.1    itojun 
     83  1.1    itojun 	image = &ap[2];
     84  1.1    itojun 
     85  1.1    itojun 	marks[MARK_START] = (u_long)image - start;
     86  1.1    itojun 	fd = loadfile(netbsd, marks, LOAD_ALL);
     87  1.1    itojun 	if (fd == -1)
     88  1.1    itojun 		err(0, "loadfile(2)");
     89  1.1    itojun 	close(fd);
     90  1.1    itojun 
     91  1.1    itojun 	/* ssym = marks[MARK_SYM]; */
     92  1.1    itojun 	/* esym = marks[MARK_END]; */
     93  1.1    itojun 
     94  1.1    itojun 	cksum = 0;
     95  1.1    itojun 	lp = image;
     96  1.1    itojun 	for (i = 0; i < sz / sizeof(cksum); i++)
     97  1.1    itojun 		cksum += *lp++;
     98  1.1    itojun 
     99  1.1    itojun 	ap[0] = sz;
    100  1.1    itojun 	ap[1] = cksum;
    101  1.1    itojun 	LoadAndReset(ap);
    102  1.1    itojun 
    103  1.1    itojun 	printf("LoadAndReset returned...\n");
    104  1.1    itojun 	free(ap);
    105  1.1    itojun 	exit(0);
    106  1.1    itojun }
    107  1.1    itojun 
    108  1.1    itojun void
    109  1.5       dsl LoadAndReset(void *image)
    110  1.1    itojun {
    111  1.1    itojun 	int mib[2];
    112  1.1    itojun 	u_long val;
    113  1.1    itojun 
    114  1.1    itojun 	mib[0] = CTL_MACHDEP;
    115  1.1    itojun 	mib[1] = CPU_LOADANDRESET;
    116  1.1    itojun 	val = (u_long)image;
    117  1.1    itojun 
    118  1.1    itojun 	sysctl(mib, 2, NULL, NULL, &val, sizeof(val));
    119  1.1    itojun }
    120