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