mopcopy.c revision 1.14 1 /* $NetBSD: mopcopy.c,v 1.14 2024/12/03 05:57:02 kalvisd Exp $ */
2
3 /* mopcopy - Convert a Unix format kernel into something that
4 * can be transferred via MOP.
5 *
6 * This code was written while referring to the NetBSD/vax boot
7 * loader. Therefore anything that can be booted by the Vax
8 * should be convertible 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 machine 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 #if defined (HAVE_NBTOOL_CONFIG_H)
51 # include "nbtool_config.h"
52 #else
53 # include "port.h"
54 #endif /* defined (HAVE_NBTOOL_CONFIG_H) */
55 #ifndef lint
56 __RCSID("$NetBSD: mopcopy.c,v 1.14 2024/12/03 05:57:02 kalvisd Exp $");
57 #endif
58
59 #include "os.h"
60 #include "common.h"
61 #include "mopdef.h"
62 #include "file.h"
63 #if !defined(NOAOUT)
64 #if defined (HAVE_NBTOOL_CONFIG_H) || defined(__NetBSD__) || defined(__OpenBSD__)
65 #include <sys/exec_aout.h>
66 #endif
67 #if defined(__FreeBSD__)
68 #include <sys/imgact_aout.h>
69 #endif
70 #endif /* !NOAOUT */
71 #if defined(__bsdi__)
72 #include <a.out.h>
73 #define NOAOUT
74 #endif
75 #if !defined(MID_VAX)
76 #define MID_VAX 150
77 #endif
78 #if !defined(MID_VAX1K)
79 #define MID_VAX1K 140
80 #endif
81
82 #ifndef NOELF
83 # if defined (HAVE_NBTOOL_CONFIG_H) || defined(__NetBSD__)
84 # include <sys/exec_elf.h>
85 # else
86 # define NOELF
87 # endif
88 # if !defined(EM_VAX)
89 # define EM_VAX 75
90 # endif
91 #endif /* NOELF */
92
93 u_char header[512]; /* The VAX header we generate is 1 block. */
94 #if !defined(NOAOUT)
95 struct exec ex, ex_swap;
96 #endif
97
98 int
99 main(int argc, char **argv)
100 {
101 FILE *out; /* A FILE because that is easier. */
102 int i, j;
103 struct dllist dl;
104
105 if (argc != 3) {
106 fprintf (stderr, "usage: %s kernel-in sys-out\n",
107 getprogname());
108 return (1);
109 }
110
111 dl.ldfd = open (argv[1], O_RDONLY);
112 if (dl.ldfd == -1)
113 err(2, "open `%s'", argv[1]);
114
115 if (GetFileInfo(&dl) == -1)
116 errx(3, "`%s' is an unknown file type", argv[1]);
117
118 switch (dl.image_type) {
119 case IMAGE_TYPE_MOP:
120 errx(3, "`%s' is already a MOP image", argv[1]);
121 break;
122
123 #ifndef NOELF
124 case IMAGE_TYPE_ELF32:
125 if (dl.e_machine != EM_VAX)
126 printf("WARNING: `%s' is not a VAX image "
127 "(machine=%d)\n", argv[1], dl.e_machine);
128 for (i = 0, j = 0; j < dl.e_nsec; j++)
129 i += dl.e_sections[j].s_fsize + dl.e_sections[j].s_pad;
130 break;
131 #endif
132
133 #ifndef NOAOUT
134 case IMAGE_TYPE_AOUT:
135 if (dl.a_mid != MID_VAX && dl.a_mid != MID_VAX1K)
136 printf("WARNING: `%s' is not a VAX image (mid=%d)\n",
137 argv[1], dl.a_mid);
138 i = dl.a_text + dl.a_text_fill + dl.a_data + dl.a_data_fill +
139 dl.a_bss + dl.a_bss_fill;
140 break;
141 #endif
142
143 default:
144 errx(3, "Image type `%s' not supported",
145 FileTypeName(dl.image_type));
146 }
147
148 i = (i+1) / 512;
149
150 dl.nloadaddr = dl.loadaddr;
151 dl.lseek = lseek(dl.ldfd,0L,SEEK_CUR);
152 dl.a_lseek = 0;
153 dl.count = 0;
154 dl.dl_bsz = 512;
155
156 mopFilePutLX(header,IHD_W_SIZE,0xd4,2); /* Offset to ISD section. */
157 mopFilePutLX(header,IHD_W_ACTIVOFF,0x30,2);/* Offset to 1st section.*/
158 mopFilePutLX(header,IHD_W_ALIAS,IHD_C_NATIVE,2);/* It's a VAX image.*/
159 mopFilePutLX(header,IHD_B_HDRBLKCNT,1,1); /* Only one header block. */
160 mopFilePutLX(header,0xd4+ISD_V_VPN,dl.loadaddr/512,2);/* load Addr */
161 mopFilePutLX(header,0x30+IHA_L_TFRADR1,dl.xferaddr,4); /* Xfer Addr */
162 mopFilePutLX(header,0xd4+ISD_W_PAGCNT,i,2);/* Imagesize in blks.*/
163
164 out = fopen (argv[2], "w");
165 if (!out)
166 err(2, "writing `%s'", argv[2]);
167
168 /* Now we do the actual work. Write VAX MOP-image header */
169
170 fwrite (header, sizeof (header), 1, out);
171
172 switch (dl.image_type) {
173 case IMAGE_TYPE_MOP:
174 abort();
175
176 case IMAGE_TYPE_ELF32:
177 #ifdef NOELF
178 abort();
179 #else
180 fprintf(stderr, "copying ");
181 for (j = 0; j < dl.e_nsec; j++)
182 fprintf(stderr, "%s%u+%u", j == 0 ? "" : "+",
183 dl.e_sections[j].s_fsize,
184 dl.e_sections[j].s_pad);
185 fprintf(stderr, "->0x%x\n", dl.xferaddr);
186 #endif
187 break;
188
189 case IMAGE_TYPE_AOUT:
190 #ifdef NOAOUT
191 abort();
192 #else
193 fprintf(stderr, "copying %u+%u+%u->0x%x\n", dl.a_text,
194 dl.a_data, dl.a_bss, dl.xferaddr);
195 #endif
196 break;
197 }
198
199 while ((i = mopFileRead(&dl,header)) > 0) {
200 (void)fwrite(header, i, 1, out);
201 }
202
203 fclose (out);
204 return (0);
205 }
206