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