2 * Copyright (C) 1999 T.Horiuchi(Brains, Corp.) and SAITOH Masanobu.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
30 #include <sys/exec_coff.h>
31 #include <sys/param.h>
34 #include <sys/sysctl.h>
35 #include <sys/socket.h>
37 #include <uvm/uvm_param.h>
38 #include <machine/cpu.h>
40 #define vm_page_size (NBPG)
42 char *netbsd
= "/netbsd";
43 struct coff_filehdr FileHdr
;
44 struct coff_aouthdr AoutHdr
;
46 static int coff_find_section(FILE *, struct coff_filehdr
*,
47 struct coff_scnhdr
*, int);
49 void LoadAndReset(char *);
50 int main(int, char **);
53 coff_find_section(FILE *fd
, struct coff_filehdr
*fp
, struct coff_scnhdr
*sh
, int s_type
)
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
) {
65 if (sh
->s_flags
== s_type
)
72 LoadAndReset(char *osimage
)
79 mib
[1] = CPU_LOADANDRESET
;
80 val
= (u_long
)osimage
;
83 sysctl(mib
, 2, NULL
, NULL
, &val
, len
);
87 main(int argc
, char *argv
[])
92 struct coff_scnhdr sh
;
105 fp
= fopen(netbsd
, "r");
111 if (fread(&FileHdr
, 1, sizeof(FileHdr
), fp
) != sizeof(FileHdr
)) {
116 if (fread(&AoutHdr
, 1, sizeof(AoutHdr
), fp
) != sizeof(AoutHdr
)) {
121 /* set up command for text segment */
122 error
= coff_find_section(fp
, &FileHdr
, &sh
, COFF_STYP_TEXT
);
124 printf("can't find text section: %d\n", error
);
128 ep_taddr
= COFF_ALIGN(sh
.s_vaddr
);
129 toffset
= sh
.s_scnptr
- (sh
.s_vaddr
- ep_taddr
);
130 ep_tsize
= sh
.s_size
+ (sh
.s_vaddr
- ep_taddr
);
132 printf("addr %lx size 0x%lx offset 0x%lx\n", ep_taddr
,
135 /* set up command for data segment */
136 error
= coff_find_section(fp
, &FileHdr
, &sh
, COFF_STYP_DATA
);
138 printf("can't find data section: %d\n", error
);
142 ep_daddr
= COFF_ALIGN(sh
.s_vaddr
);
143 doffset
= sh
.s_scnptr
- (sh
.s_vaddr
- ep_daddr
);
144 dsize
= sh
.s_size
+ (sh
.s_vaddr
- ep_daddr
);
145 ep_dsize
= round_page(dsize
) + AoutHdr
.a_bsize
;
147 printf("addr 0x%lx size 0x%lx offset 0x%lx\n", ep_daddr
,
150 osimage
= malloc(ep_tsize
+dsize
+sizeof(u_long
) * 2);
151 if (osimage
== NULL
) {
152 printf("osloader:no memory\n");
156 *(u_long
*)osimage
= ep_tsize
+dsize
;
157 p
= osimage
+ 2 * sizeof(u_long
);
160 fseek(fp
, toffset
, SEEK_SET
);
161 if (fread(p
, 1, ep_tsize
, fp
) != ep_tsize
) {
167 fseek(fp
, doffset
, SEEK_SET
);
168 if (fread(p
+ ep_daddr
- ep_taddr
, 1, dsize
, fp
) != dsize
) {
176 size
= (ep_tsize
+ dsize
) >> 2;
178 for(i
= 0; i
< size
; i
++) {
179 cksum
+= *(u_long
*)p
;
183 *(u_long
*)(osimage
+sizeof(u_long
)) = cksum
;
185 LoadAndReset(osimage
);
187 return (0); /* NOTREACHED */