osloader.c revision 1.6 1 /*-
2 * Copyright (C) 1999 T.Horiuchi(Brains, Corp.) and SAITOH Masanobu.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <sys/exec_coff.h>
31 #include <sys/param.h>
32 #include <sys/gmon.h>
33 #include <sys/stat.h>
34 #include <sys/sysctl.h>
35 #include <sys/socket.h>
36 #include <sys/file.h>
37 #include <uvm/uvm_param.h>
38 #include <machine/cpu.h>
39
40 #define vm_page_size (NBPG)
41
42 char *netbsd = "/netbsd";
43 struct coff_filehdr FileHdr;
44 struct coff_aouthdr AoutHdr;
45
46 static int coff_find_section(FILE *, struct coff_filehdr *,
47 struct coff_scnhdr *, int);
48
49 void LoadAndReset(char *);
50 int main(int, char **);
51
52 static int
53 coff_find_section(FILE *fd, struct coff_filehdr *fp, struct coff_scnhdr *sh, int s_type)
54 {
55 int i, pos, siz;
56
57 pos = COFF_HDR_SIZE;
58 for (i = 0; i < fp->f_nscns; i++, pos += sizeof(struct coff_scnhdr)) {
59 siz = sizeof(struct coff_scnhdr);
60 if (fread(sh, 1, siz, fd) != siz) {
61 perror("osloader");
62 exit(1);
63 }
64
65 if (sh->s_flags == s_type)
66 return (0);
67 }
68 return (-1);
69 }
70
71 void
72 LoadAndReset(char *osimage)
73 {
74 int mib[2];
75 u_long val;
76 int len;
77
78 mib[0] = CTL_MACHDEP;
79 mib[1] = CPU_LOADANDRESET;
80 val = (u_long)osimage;
81 len = sizeof(val);
82
83 sysctl(mib, 2, NULL, NULL, &val, len);
84 }
85
86 int
87 main(argc, argv)
88 int argc;
89 char *argv[];
90 {
91 FILE *fp;
92 int error;
93 long dsize;
94 struct coff_scnhdr sh;
95 u_long ep_taddr;
96 u_long ep_tsize;
97 u_long toffset;
98 u_long ep_daddr;
99 u_long ep_dsize;
100 u_long doffset;
101 char *osimage;
102 char *p;
103 int i;
104 u_long cksum;
105 u_long size;
106
107 fp = fopen(netbsd, "r");
108 if (fp == NULL) {
109 perror("osloader");
110 exit(1);
111 }
112
113 if (fread(&FileHdr, 1, sizeof(FileHdr), fp) != sizeof(FileHdr)) {
114 perror("osloader");
115 exit(1);
116 }
117
118 if (fread(&AoutHdr, 1, sizeof(AoutHdr), fp) != sizeof(AoutHdr)) {
119 perror("osloader");
120 exit(1);
121 }
122
123 /* set up command for text segment */
124 error = coff_find_section(fp, &FileHdr, &sh, COFF_STYP_TEXT);
125 if (error) {
126 printf("can't find text section: %d\n", error);
127 exit(1);
128 }
129
130 ep_taddr = COFF_ALIGN(sh.s_vaddr);
131 toffset = sh.s_scnptr - (sh.s_vaddr - ep_taddr);
132 ep_tsize = sh.s_size + (sh.s_vaddr - ep_taddr);
133
134 printf("addr %lx size 0x%lx offset 0x%lx\n", ep_taddr,
135 ep_tsize, toffset);
136
137 /* set up command for data segment */
138 error = coff_find_section(fp, &FileHdr, &sh, COFF_STYP_DATA);
139 if (error) {
140 printf("can't find data section: %d\n", error);
141 exit(1);
142 }
143
144 ep_daddr = COFF_ALIGN(sh.s_vaddr);
145 doffset = sh.s_scnptr - (sh.s_vaddr - ep_daddr);
146 dsize = sh.s_size + (sh.s_vaddr - ep_daddr);
147 ep_dsize = round_page(dsize) + AoutHdr.a_bsize;
148
149 printf("addr 0x%lx size 0x%lx offset 0x%lx\n", ep_daddr,
150 dsize, doffset);
151
152 osimage = malloc(ep_tsize+dsize+sizeof(u_long) * 2);
153 if (osimage == NULL) {
154 printf("osloader:no memory\n");
155 exit(1);
156 }
157
158 *(u_long *)osimage = ep_tsize+dsize;
159 p = osimage + 2 * sizeof(u_long);
160
161 /* load text area */
162 fseek(fp, toffset, SEEK_SET);
163 if (fread(p, 1, ep_tsize, fp) != ep_tsize) {
164 perror("osloader:");
165 exit(1);
166 }
167
168 /* load data area */
169 fseek(fp, doffset, SEEK_SET);
170 if (fread(p + ep_daddr - ep_taddr, 1, dsize, fp) != dsize) {
171 perror("osloader:");
172 exit(1);
173 }
174
175 fclose(fp);
176
177 cksum = 0;
178 size = (ep_tsize + dsize) >> 2;
179
180 for(i = 0; i < size; i++) {
181 cksum += *(u_long *)p;
182 p += sizeof(u_long);
183 }
184
185 *(u_long *)(osimage+sizeof(u_long)) = cksum;
186
187 LoadAndReset(osimage);
188
189 return (0); /* NOTREACHED */
190 }
191