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
42char *netbsd = "/netbsd";
43struct coff_filehdr FileHdr;
44struct coff_aouthdr AoutHdr;
45
46static int coff_find_section(FILE *, struct coff_filehdr *,
47			     struct coff_scnhdr *, int);
48
49void	LoadAndReset(char *);
50int	main(int, char **);
51
52static int
53coff_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
71void
72LoadAndReset(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
86int
87main(int argc, char *argv[])
88{
89	FILE *fp;
90	int error;
91	long dsize;
92	struct coff_scnhdr sh;
93	u_long ep_taddr;
94	u_long ep_tsize;
95	u_long toffset;
96	u_long ep_daddr;
97	u_long ep_dsize;
98	u_long doffset;
99	char *osimage;
100	char *p;
101	int i;
102	u_long cksum;
103	u_long size;
104
105	fp = fopen(netbsd, "r");
106	if (fp == NULL) {
107		perror("osloader");
108		exit(1);
109	}
110
111	if (fread(&FileHdr, 1, sizeof(FileHdr), fp) != sizeof(FileHdr)) {
112		perror("osloader");
113		exit(1);
114	}
115
116	if (fread(&AoutHdr, 1, sizeof(AoutHdr), fp) != sizeof(AoutHdr)) {
117		perror("osloader");
118		exit(1);
119	}
120
121	/* set up command for text segment */
122	error = coff_find_section(fp, &FileHdr, &sh, COFF_STYP_TEXT);
123	if (error) {
124		printf("can't find text section: %d\n", error);
125		exit(1);
126	}
127
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);
131
132	printf("addr %lx size 0x%lx offset 0x%lx\n", ep_taddr,
133	       ep_tsize, toffset);
134
135	/* set up command for data segment */
136	error = coff_find_section(fp, &FileHdr, &sh, COFF_STYP_DATA);
137	if (error) {
138		printf("can't find data section: %d\n", error);
139		exit(1);
140	}
141
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;
146
147	printf("addr 0x%lx size 0x%lx offset 0x%lx\n", ep_daddr,
148		 dsize, doffset);
149
150	osimage = malloc(ep_tsize+dsize+sizeof(u_long) * 2);
151	if (osimage == NULL) {
152		printf("osloader:no memory\n");
153		exit(1);
154	}
155
156	*(u_long *)osimage = ep_tsize+dsize;
157	p = osimage + 2 * sizeof(u_long);
158
159	/* load text area */
160	fseek(fp, toffset, SEEK_SET);
161	if (fread(p, 1, ep_tsize, fp) != ep_tsize) {
162		perror("osloader:");
163		exit(1);
164	}
165
166	/* load data area */
167	fseek(fp, doffset, SEEK_SET);
168	if (fread(p + ep_daddr - ep_taddr, 1, dsize, fp) != dsize) {
169		perror("osloader:");
170		exit(1);
171	}
172
173	fclose(fp);
174
175	cksum = 0;
176	size = (ep_tsize + dsize) >> 2;
177
178	for(i = 0; i < size; i++) {
179		cksum += *(u_long *)p;
180		p += sizeof(u_long);
181	}
182
183	*(u_long *)(osimage+sizeof(u_long)) = cksum;
184
185	LoadAndReset(osimage);
186
187	return (0);	/* NOTREACHED */
188}
189