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