osloader.c revision 1.6 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.5 dsl static int coff_find_section(FILE *, struct coff_filehdr *,
47 1.5 dsl struct coff_scnhdr *, int);
48 1.1 msaitoh
49 1.5 dsl void LoadAndReset(char *);
50 1.5 dsl int main(int, char **);
51 1.1 msaitoh
52 1.1 msaitoh static int
53 1.6 dsl coff_find_section(FILE *fd, struct coff_filehdr *fp, struct coff_scnhdr *sh, int s_type)
54 1.1 msaitoh {
55 1.1 msaitoh int i, pos, siz;
56 1.1 msaitoh
57 1.1 msaitoh pos = COFF_HDR_SIZE;
58 1.1 msaitoh for (i = 0; i < fp->f_nscns; i++, pos += sizeof(struct coff_scnhdr)) {
59 1.1 msaitoh siz = sizeof(struct coff_scnhdr);
60 1.2 msaitoh if (fread(sh, 1, siz, fd) != siz) {
61 1.1 msaitoh perror("osloader");
62 1.2 msaitoh exit(1);
63 1.1 msaitoh }
64 1.1 msaitoh
65 1.1 msaitoh if (sh->s_flags == s_type)
66 1.2 msaitoh return (0);
67 1.1 msaitoh }
68 1.2 msaitoh return (-1);
69 1.1 msaitoh }
70 1.1 msaitoh
71 1.1 msaitoh void
72 1.6 dsl LoadAndReset(char *osimage)
73 1.1 msaitoh {
74 1.1 msaitoh int mib[2];
75 1.1 msaitoh u_long val;
76 1.1 msaitoh int len;
77 1.1 msaitoh
78 1.1 msaitoh mib[0] = CTL_MACHDEP;
79 1.1 msaitoh mib[1] = CPU_LOADANDRESET;
80 1.1 msaitoh val = (u_long)osimage;
81 1.2 msaitoh len = sizeof(val);
82 1.1 msaitoh
83 1.1 msaitoh sysctl(mib, 2, NULL, NULL, &val, len);
84 1.1 msaitoh }
85 1.1 msaitoh
86 1.1 msaitoh int
87 1.2 msaitoh main(argc, argv)
88 1.2 msaitoh int argc;
89 1.2 msaitoh char *argv[];
90 1.1 msaitoh {
91 1.1 msaitoh FILE *fp;
92 1.1 msaitoh int error;
93 1.1 msaitoh long dsize;
94 1.1 msaitoh struct coff_scnhdr sh;
95 1.1 msaitoh u_long ep_taddr;
96 1.1 msaitoh u_long ep_tsize;
97 1.1 msaitoh u_long toffset;
98 1.1 msaitoh u_long ep_daddr;
99 1.1 msaitoh u_long ep_dsize;
100 1.1 msaitoh u_long doffset;
101 1.1 msaitoh char *osimage;
102 1.1 msaitoh char *p;
103 1.1 msaitoh int i;
104 1.1 msaitoh u_long cksum;
105 1.1 msaitoh u_long size;
106 1.1 msaitoh
107 1.2 msaitoh fp = fopen(netbsd, "r");
108 1.2 msaitoh if (fp == NULL) {
109 1.1 msaitoh perror("osloader");
110 1.2 msaitoh exit(1);
111 1.1 msaitoh }
112 1.1 msaitoh
113 1.2 msaitoh if (fread(&FileHdr, 1, sizeof(FileHdr), fp) != sizeof(FileHdr)) {
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(&AoutHdr, 1, sizeof(AoutHdr), fp) != sizeof(AoutHdr)) {
119 1.1 msaitoh perror("osloader");
120 1.2 msaitoh exit(1);
121 1.1 msaitoh }
122 1.1 msaitoh
123 1.1 msaitoh /* set up command for text segment */
124 1.1 msaitoh error = coff_find_section(fp, &FileHdr, &sh, COFF_STYP_TEXT);
125 1.1 msaitoh if (error) {
126 1.1 msaitoh printf("can't find text section: %d\n", error);
127 1.2 msaitoh exit(1);
128 1.1 msaitoh }
129 1.1 msaitoh
130 1.1 msaitoh ep_taddr = COFF_ALIGN(sh.s_vaddr);
131 1.1 msaitoh toffset = sh.s_scnptr - (sh.s_vaddr - ep_taddr);
132 1.1 msaitoh ep_tsize = sh.s_size + (sh.s_vaddr - ep_taddr);
133 1.1 msaitoh
134 1.3 msaitoh printf("addr %lx size 0x%lx offset 0x%lx\n", ep_taddr,
135 1.1 msaitoh ep_tsize, toffset);
136 1.1 msaitoh
137 1.1 msaitoh /* set up command for data segment */
138 1.1 msaitoh error = coff_find_section(fp, &FileHdr, &sh, COFF_STYP_DATA);
139 1.1 msaitoh if (error) {
140 1.1 msaitoh printf("can't find data section: %d\n", error);
141 1.2 msaitoh exit(1);
142 1.1 msaitoh }
143 1.1 msaitoh
144 1.1 msaitoh ep_daddr = COFF_ALIGN(sh.s_vaddr);
145 1.1 msaitoh doffset = sh.s_scnptr - (sh.s_vaddr - ep_daddr);
146 1.1 msaitoh dsize = sh.s_size + (sh.s_vaddr - ep_daddr);
147 1.1 msaitoh ep_dsize = round_page(dsize) + AoutHdr.a_bsize;
148 1.1 msaitoh
149 1.3 msaitoh printf("addr 0x%lx size 0x%lx offset 0x%lx\n", ep_daddr,
150 1.1 msaitoh dsize, doffset);
151 1.1 msaitoh
152 1.3 msaitoh osimage = malloc(ep_tsize+dsize+sizeof(u_long) * 2);
153 1.2 msaitoh if (osimage == NULL) {
154 1.1 msaitoh printf("osloader:no memory\n");
155 1.2 msaitoh exit(1);
156 1.1 msaitoh }
157 1.1 msaitoh
158 1.1 msaitoh *(u_long *)osimage = ep_tsize+dsize;
159 1.3 msaitoh p = osimage + 2 * sizeof(u_long);
160 1.1 msaitoh
161 1.1 msaitoh /* load text area */
162 1.2 msaitoh fseek(fp, toffset, SEEK_SET);
163 1.2 msaitoh if (fread(p, 1, ep_tsize, fp) != ep_tsize) {
164 1.1 msaitoh perror("osloader:");
165 1.2 msaitoh exit(1);
166 1.1 msaitoh }
167 1.1 msaitoh
168 1.1 msaitoh /* load data area */
169 1.2 msaitoh fseek(fp, doffset, SEEK_SET);
170 1.3 msaitoh if (fread(p + ep_daddr - ep_taddr, 1, dsize, fp) != dsize) {
171 1.1 msaitoh perror("osloader:");
172 1.2 msaitoh exit(1);
173 1.1 msaitoh }
174 1.1 msaitoh
175 1.2 msaitoh fclose(fp);
176 1.1 msaitoh
177 1.1 msaitoh cksum = 0;
178 1.1 msaitoh size = (ep_tsize + dsize) >> 2;
179 1.1 msaitoh
180 1.2 msaitoh for(i = 0; i < size; i++) {
181 1.1 msaitoh cksum += *(u_long *)p;
182 1.2 msaitoh p += sizeof(u_long);
183 1.1 msaitoh }
184 1.1 msaitoh
185 1.2 msaitoh *(u_long *)(osimage+sizeof(u_long)) = cksum;
186 1.1 msaitoh
187 1.2 msaitoh LoadAndReset(osimage);
188 1.1 msaitoh
189 1.3 msaitoh return (0); /* NOTREACHED */
190 1.1 msaitoh }
191