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