Home | History | Annotate | Line # | Download | only in bootelf
boot.c revision 1.2.70.1
      1  1.2.70.1    yamt /*	$NetBSD: boot.c,v 1.2.70.1 2006/04/11 11:53:43 yamt 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.2.70.1    yamt #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.1  itojun void LoadAndReset __P((void *));
     46       1.1  itojun 
     47       1.1  itojun char *netbsd = "/netbsd";
     48       1.1  itojun 
     49       1.1  itojun int
     50       1.1  itojun main(argc, argv)
     51       1.1  itojun 	int argc;
     52       1.1  itojun 	char *argv[];
     53       1.1  itojun {
     54       1.1  itojun 	u_long marks[MARK_MAX];
     55       1.1  itojun 	u_long start, entry;
     56       1.1  itojun 	int fd, sz, i;
     57       1.1  itojun 	u_long *ap;
     58       1.1  itojun 	u_long cksum, *lp;
     59       1.1  itojun 	void *image;
     60       1.1  itojun 
     61       1.1  itojun 	/* use the specified kernel if any */
     62       1.1  itojun 	if (argc > 1)
     63       1.1  itojun 		netbsd = argv[1];
     64       1.1  itojun 
     65       1.1  itojun 	DPRINTF("loading %s...\n", netbsd);
     66       1.1  itojun 
     67       1.1  itojun 	/* count kernel size */
     68       1.1  itojun 	marks[MARK_START] = 0;
     69       1.1  itojun 	fd = loadfile(netbsd, marks, COUNT_ALL);
     70       1.1  itojun 	if (fd == -1)
     71       1.1  itojun 		err(0, "loadfile(1)");
     72       1.1  itojun 	close(fd);
     73       1.1  itojun 
     74       1.1  itojun 	sz = marks[MARK_END] - marks[MARK_START];
     75       1.1  itojun 	start = marks[MARK_START];
     76       1.1  itojun 	entry = marks[MARK_ENTRY];
     77       1.1  itojun 
     78       1.1  itojun 	DPRINTF("size  = 0x%x\n", sz);
     79       1.1  itojun 	DPRINTF("start = 0x%lx\n", start);
     80       1.1  itojun 
     81       1.1  itojun 	ap = malloc(sz + 2 * sizeof(long));
     82       1.1  itojun 	if (ap == NULL)
     83       1.1  itojun 		err(0, "malloc");
     84       1.1  itojun 
     85       1.1  itojun 	image = &ap[2];
     86       1.1  itojun 
     87       1.1  itojun 	marks[MARK_START] = (u_long)image - start;
     88       1.1  itojun 	fd = loadfile(netbsd, marks, LOAD_ALL);
     89       1.1  itojun 	if (fd == -1)
     90       1.1  itojun 		err(0, "loadfile(2)");
     91       1.1  itojun 	close(fd);
     92       1.1  itojun 
     93       1.1  itojun 	/* ssym = marks[MARK_SYM]; */
     94       1.1  itojun 	/* esym = marks[MARK_END]; */
     95       1.1  itojun 
     96       1.1  itojun 	cksum = 0;
     97       1.1  itojun 	lp = image;
     98       1.1  itojun 	for (i = 0; i < sz / sizeof(cksum); i++)
     99       1.1  itojun 		cksum += *lp++;
    100       1.1  itojun 
    101       1.1  itojun 	ap[0] = sz;
    102       1.1  itojun 	ap[1] = cksum;
    103       1.1  itojun 	LoadAndReset(ap);
    104       1.1  itojun 
    105       1.1  itojun 	printf("LoadAndReset returned...\n");
    106       1.1  itojun 	free(ap);
    107       1.1  itojun 	exit(0);
    108       1.1  itojun }
    109       1.1  itojun 
    110       1.1  itojun void
    111       1.1  itojun LoadAndReset(image)
    112       1.1  itojun 	void *image;
    113       1.1  itojun {
    114       1.1  itojun 	int mib[2];
    115       1.1  itojun 	u_long val;
    116       1.1  itojun 
    117       1.1  itojun 	mib[0] = CTL_MACHDEP;
    118       1.1  itojun 	mib[1] = CPU_LOADANDRESET;
    119       1.1  itojun 	val = (u_long)image;
    120       1.1  itojun 
    121       1.1  itojun 	sysctl(mib, 2, NULL, NULL, &val, sizeof(val));
    122       1.1  itojun }
    123