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