xref: /openbsd/usr.sbin/mopd/mopa.out/mopa.out.c (revision db3296cf)
1 /*	$OpenBSD: mopa.out.c,v 1.5 2003/04/13 22:39:38 avsm Exp $ */
2 
3 /* mopa.out - 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 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 #ifndef LINT
51 static char rcsid[] = "$OpenBSD: mopa.out.c,v 1.5 2003/04/13 22:39:38 avsm Exp $";
52 #endif
53 
54 #include "os.h"
55 #include "common/common.h"
56 #include "common/mopdef.h"
57 #include "common/file.h"
58 #if defined(__NetBSD__) || defined(__OpenBSD__)
59 #include <sys/exec_aout.h>
60 #endif
61 #if defined(__FreeBSD__)
62 #include <sys/imgact_aout.h>
63 #endif
64 #if defined(__bsdi__)
65 #include <a.out.h>
66 #define NOAOUT
67 #endif
68 #if !defined(MID_VAX)
69 #define MID_VAX 140
70 #endif
71 
72 u_char header[512];		/* The VAX header we generate is 1 block. */
73 struct exec ex, ex_swap;
74 
75 int
76 main (int argc, char **argv)
77 {
78 	FILE   *out;		/* A FILE because that is easier. */
79 	int	i;
80 	struct dllist dl;
81 
82 #ifdef NOAOUT
83 	fprintf(stderr, "%s: has no function in OS/BSD\n", argv[0]);
84 	return(1);
85 #endif
86 
87 	if (argc != 3) {
88 		fprintf (stderr, "usage: %s kernel-in sys-out\n", argv[0]);
89 		return (1);
90 	}
91 
92 	dl.ldfd = open (argv[1], O_RDONLY);
93 	if (dl.ldfd == -1) {
94 		perror (argv[1]);
95 		return (2);
96 	}
97 
98 	GetFileInfo(dl.ldfd,
99 		    &dl.loadaddr,
100 		    &dl.xferaddr,
101 		    &dl.aout,
102 		    &dl.a_text,&dl.a_text_fill,
103 		    &dl.a_data,&dl.a_data_fill,
104 		    &dl.a_bss ,&dl.a_bss_fill );
105 
106 	if (dl.aout == -1) {
107 		fprintf(stderr,"%s: not an a.out file\n",argv[1]);
108 		return (3);
109         }
110 
111 	if (dl.aout != MID_VAX) {
112 		fprintf(stderr,"%s: file is not a VAX image (mid=%d)\n",
113 			argv[1],dl.aout);
114 		return (4);
115 	}
116 
117 	i = dl.a_text + dl.a_text_fill + dl.a_data + dl.a_data_fill +
118 	    dl.a_bss  + dl.a_bss_fill;
119 	i = (i+1) / 512;
120 
121 	dl.nloadaddr = dl.loadaddr;
122 	dl.lseek     = lseek(dl.ldfd,0L,SEEK_CUR);
123 	dl.a_lseek   = 0;
124 	dl.count     = 0;
125 	dl.dl_bsz    = 512;
126 
127 	mopFilePutLX(header,IHD_W_SIZE,0xd4,2);   /* Offset to ISD section. */
128 	mopFilePutLX(header,IHD_W_ACTIVOFF,0x30,2);/* Offset to 1st section.*/
129 	mopFilePutLX(header,IHD_W_ALIAS,IHD_C_NATIVE,2);/* It's a VAX image.*/
130 	mopFilePutLX(header,IHD_B_HDRBLKCNT,1,1); /* Only one header block. */
131 	mopFilePutLX(header,0x30+IHA_L_TFRADR1,dl.xferaddr,4); /* Xfer Addr */
132 	mopFilePutLX(header,0xd4+ISD_W_PAGCNT,i,2);/* Imagesize in blks.*/
133 
134 	out = fopen (argv[2], "w");
135 	if (!out) {
136 		perror (argv[2]);
137 		return (2);
138 	}
139 
140 	/* Now we do the actual work. Write VAX MOP-image header */
141 
142 	fwrite (header, sizeof (header), 1, out);
143 
144 	fprintf (stderr, "copying %lu", dl.a_text);
145 	fprintf (stderr, "+%lu", dl.a_data);
146 	fprintf (stderr, "+%lu", dl.a_bss);
147 	fprintf (stderr, "->%lu", dl.xferaddr);
148 	fprintf (stderr, "\n");
149 
150 	while ((i = mopFileRead(&dl,header)) > 0) {
151 		(void)fwrite(header, i, 1, out);
152 	}
153 
154 	fclose (out);
155 }
156