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