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