xref: /netbsd-src/usr.sbin/mopd/mopcopy/mopcopy.c (revision bdc22b2e01993381dcefeff2bc9b56ca75a4235c)
1 /*	$NetBSD: mopcopy.c,v 1.6 2016/06/08 01:16:35 christos Exp $	*/
2 
3 /* mopcopy - Convert a Unix format kernel into something that
4  * can be transfered via MOP.
5  *
6  * This code was written while refering to the NetBSD/vax boot
7  * loader. Therefore anything that can be booted by the Vax
8  * should be convertable with this program.
9  *
10  * If necessary, the a.out header is stripped, and the program
11  * segments are padded out. The BSS segment is zero filled.
12  * A header is prepended that looks like an IHD header. In
13  * particular the Unix mahine ID is placed where mopd expects
14  * the image type to be (offset is IHD_W_ALIAS). If the machine
15  * ID could be mistaken for a DEC image type, then the conversion
16  * is aborted. The original a.out header is copied into the front
17  * of the header so that once we have detected the Unix machine
18  * ID we can haul the load address and the xfer address out.
19  */
20 
21 /*
22  * Copyright (c) 1996 Lloyd Parkes.  All rights reserved.
23  *
24  * Redistribution and use in source and binary forms, with or without
25  * modification, are permitted provided that the following conditions
26  * are met:
27  * 1. Redistributions of source code must retain the above copyright
28  *    notice, this list of conditions and the following disclaimer.
29  * 2. Redistributions in binary form must reproduce the above copyright
30  *    notice, this list of conditions and the following disclaimer in the
31  *    documentation and/or other materials provided with the distribution.
32  * 3. All advertising materials mentioning features or use of this software
33  *    must display the following acknowledgement:
34  *	This product includes software developed by Lloyd Parkes.
35  * 4. The name of the author may not be used to endorse or promote products
36  *    derived from this software without specific prior written permission.
37  *
38  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
39  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
40  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
41  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
42  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
43  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
44  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
45  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
46  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
47  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
48  */
49 
50 #include "port.h"
51 #ifndef lint
52 __RCSID("$NetBSD: mopcopy.c,v 1.6 2016/06/08 01:16:35 christos Exp $");
53 #endif
54 
55 #include "os.h"
56 #include "common.h"
57 #include "mopdef.h"
58 #include "file.h"
59 #if !defined(NOAOUT)
60 #if defined(__NetBSD__) || defined(__OpenBSD__)
61 #include <sys/exec_aout.h>
62 #endif
63 #if defined(__FreeBSD__)
64 #include <sys/imgact_aout.h>
65 #endif
66 #endif /* !NOAOUT */
67 #if defined(__bsdi__)
68 #include <a.out.h>
69 #define NOAOUT
70 #endif
71 #if !defined(MID_VAX)
72 #define MID_VAX 140
73 #endif
74 
75 #ifndef NOELF
76 # if defined(__NetBSD__)
77 #  include <sys/exec_elf.h>
78 # else
79 #  define NOELF
80 # endif
81 # if !defined(EM_VAX)
82 #  define EM_VAX 75
83 # endif
84 #endif /* NOELF */
85 
86 u_char header[512];		/* The VAX header we generate is 1 block. */
87 #if !defined(NOAOUT)
88 struct exec ex, ex_swap;
89 #endif
90 
91 int
92 main(int argc, char **argv)
93 {
94 	FILE   *out;		/* A FILE because that is easier. */
95 	int	i, j;
96 	struct dllist dl;
97 
98 	if (argc != 3) {
99 		fprintf (stderr, "usage: %s kernel-in sys-out\n",
100 		    getprogname());
101 		return (1);
102 	}
103 
104 	dl.ldfd = open (argv[1], O_RDONLY);
105 	if (dl.ldfd == -1)
106 		err(2, "open `%s'", argv[1]);
107 
108 	if (GetFileInfo(&dl) == -1)
109 		errx(3, "`%s' is an unknown file type", argv[1]);
110 
111 	switch (dl.image_type) {
112 	case IMAGE_TYPE_MOP:
113 		errx(3, "`%s' is already a MOP image", argv[1]);
114 		break;
115 
116 #ifndef NOELF
117 	case IMAGE_TYPE_ELF32:
118 		if (dl.e_machine != EM_VAX)
119 			printf("WARNING: `%s' is not a VAX image "
120 			    "(machine=%d)\n", argv[1], dl.e_machine);
121 		for (i = 0, j = 0; j < dl.e_nsec; j++)
122 			i += dl.e_sections[j].s_fsize + dl.e_sections[j].s_pad;
123 		break;
124 #endif
125 
126 #ifndef NOAOUT
127 	case IMAGE_TYPE_AOUT:
128 		if (dl.a_mid != MID_VAX)
129 			printf("WARNING: `%s' is not a VAX image (mid=%d)\n",
130 			    argv[1], dl.a_mid);
131 		i = dl.a_text + dl.a_text_fill + dl.a_data + dl.a_data_fill +
132 		    dl.a_bss  + dl.a_bss_fill;
133 		break;
134 #endif
135 
136 	default:
137 		errx(3, "Image type `%s' not supported",
138 		    FileTypeName(dl.image_type));
139 	}
140 
141 	i = (i+1) / 512;
142 
143 	dl.nloadaddr = dl.loadaddr;
144 	dl.lseek     = lseek(dl.ldfd,0L,SEEK_CUR);
145 	dl.a_lseek   = 0;
146 	dl.count     = 0;
147 	dl.dl_bsz    = 512;
148 
149 	mopFilePutLX(header,IHD_W_SIZE,0xd4,2);   /* Offset to ISD section. */
150 	mopFilePutLX(header,IHD_W_ACTIVOFF,0x30,2);/* Offset to 1st section.*/
151 	mopFilePutLX(header,IHD_W_ALIAS,IHD_C_NATIVE,2);/* It's a VAX image.*/
152 	mopFilePutLX(header,IHD_B_HDRBLKCNT,1,1); /* Only one header block. */
153 	mopFilePutLX(header,0xd4+ISD_V_VPN,dl.loadaddr/512,2);/* load Addr */
154 	mopFilePutLX(header,0x30+IHA_L_TFRADR1,dl.xferaddr,4); /* Xfer Addr */
155 	mopFilePutLX(header,0xd4+ISD_W_PAGCNT,i,2);/* Imagesize in blks.*/
156 
157 	out = fopen (argv[2], "w");
158 	if (!out)
159 		err(2, "writing `%s'", argv[2]);
160 
161 	/* Now we do the actual work. Write VAX MOP-image header */
162 
163 	fwrite (header, sizeof (header), 1, out);
164 
165 	switch (dl.image_type) {
166 	case IMAGE_TYPE_MOP:
167 		abort();
168 
169 	case IMAGE_TYPE_ELF32:
170 #ifdef NOELF
171 		abort();
172 #else
173 		fprintf(stderr, "copying ");
174 		for (j = 0; j < dl.e_nsec; j++)
175 			fprintf(stderr, "%s%u+%u", j == 0 ? "" : "+",
176 			    dl.e_sections[j].s_fsize,
177 			    dl.e_sections[j].s_pad);
178 		fprintf(stderr, "->0x%x\n", dl.xferaddr);
179 #endif
180 		break;
181 
182 	case IMAGE_TYPE_AOUT:
183 #ifdef NOAOUT
184 		abort();
185 #else
186 		fprintf(stderr, "copying %u+%u+%u->0x%x\n", dl.a_text,
187 		    dl.a_data, dl.a_bss, dl.xferaddr);
188 #endif
189 		break;
190 	}
191 
192 	while ((i = mopFileRead(&dl,header)) > 0) {
193 		(void)fwrite(header, i, 1, out);
194 	}
195 
196 	fclose (out);
197 	return (0);
198 }
199