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