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