1193323Sed/* $NetBSD: mopcopy.c,v 1.4 2009/08/20 22:26:19 he Exp $ */ 2193323Sed 3193323Sed/* mopcopy - Convert a Unix format kernel into something that 4193323Sed * can be transfered via MOP. 5193323Sed * 6193323Sed * This code was written while refering to the NetBSD/vax boot 7193323Sed * loader. Therefore anything that can be booted by the Vax 8193323Sed * should be convertable with this program. 9193323Sed * 10193323Sed * If necessary, the a.out header is stripped, and the program 11193323Sed * segments are padded out. The BSS segment is zero filled. 12193323Sed * A header is prepended that looks like an IHD header. In 13193323Sed * particular the Unix mahine ID is placed where mopd expects 14193323Sed * the image type to be (offset is IHD_W_ALIAS). If the machine 15193323Sed * ID could be mistaken for a DEC image type, then the conversion 16193323Sed * is aborted. The original a.out header is copied into the front 17193323Sed * of the header so that once we have detected the Unix machine 18234353Sdim * ID we can haul the load address and the xfer address out. 19198396Srdivacky */ 20288943Sdim 21193323Sed/* 22193323Sed * Copyright (c) 1996 Lloyd Parkes. All rights reserved. 23193323Sed * 24193323Sed * Redistribution and use in source and binary forms, with or without 25261991Sdim * modification, are permitted provided that the following conditions 26261991Sdim * are met: 27261991Sdim * 1. Redistributions of source code must retain the above copyright 28261991Sdim * notice, this list of conditions and the following disclaimer. 29261991Sdim * 2. Redistributions in binary form must reproduce the above copyright 30261991Sdim * notice, this list of conditions and the following disclaimer in the 31261991Sdim * documentation and/or other materials provided with the distribution. 32261991Sdim * 3. All advertising materials mentioning features or use of this software 33261991Sdim * must display the following acknowledgement: 34261991Sdim * This product includes software developed by Lloyd Parkes. 35261991Sdim * 4. The name of the author may not be used to endorse or promote products 36261991Sdim * derived from this software without specific prior written permission. 37261991Sdim * 38234353Sdim * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 39261991Sdim * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 40193323Sed * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 41251662Sdim * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 42251662Sdim * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 43251662Sdim * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 44251662Sdim * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 45251662Sdim * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 46251662Sdim * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 47251662Sdim * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 48251662Sdim */ 49251662Sdim 50251662Sdim#include <sys/cdefs.h> 51251662Sdim#ifndef lint 52251662Sdim__RCSID("$NetBSD: mopcopy.c,v 1.4 2009/08/20 22:26:19 he Exp $"); 53251662Sdim#endif 54251662Sdim 55251662Sdim#include "os.h" 56251662Sdim#include "common.h" 57251662Sdim#include "mopdef.h" 58251662Sdim#include "file.h" 59251662Sdim#if !defined(NOAOUT) 60251662Sdim#if defined(__NetBSD__) || defined(__OpenBSD__) 61251662Sdim#include <sys/exec_aout.h> 62276479Sdim#endif 63251662Sdim#if defined(__FreeBSD__) 64251662Sdim#include <sys/imgact_aout.h> 65251662Sdim#endif 66251662Sdim#endif /* !NOAOUT */ 67251662Sdim#if defined(__bsdi__) 68251662Sdim#include <a.out.h> 69251662Sdim#define NOAOUT 70251662Sdim#endif 71251662Sdim#if !defined(MID_VAX) 72251662Sdim#define MID_VAX 140 73251662Sdim#endif 74251662Sdim 75251662Sdim#ifndef NOELF 76251662Sdim# if defined(__NetBSD__) 77251662Sdim# include <sys/exec_elf.h> 78251662Sdim# else 79251662Sdim# define NOELF 80251662Sdim# endif 81251662Sdim# if !defined(EM_VAX) 82251662Sdim# define EM_VAX 75 83234353Sdim# endif 84234353Sdim#endif /* NOELF */ 85234353Sdim 86234353Sdimu_char header[512]; /* The VAX header we generate is 1 block. */ 87234353Sdim#if !defined(NOAOUT) 88234353Sdimstruct exec ex, ex_swap; 89234353Sdim#endif 90234353Sdim 91234353Sdimint 92234353Sdimmain(int argc, char **argv) 93234353Sdim{ 94234353Sdim FILE *out; /* A FILE because that is easier. */ 95234353Sdim int i, j; 96234353Sdim struct dllist dl; 97234353Sdim 98234353Sdim if (argc != 3) { 99234353Sdim fprintf (stderr, "usage: %s kernel-in sys-out\n", 100234353Sdim getprogname()); 101234353Sdim return (1); 102239462Sdim } 103239462Sdim 104288943Sdim dl.ldfd = open (argv[1], O_RDONLY); 105239462Sdim if (dl.ldfd == -1) 106239462Sdim err(2, "open `%s'", argv[1]); 107239462Sdim 108280031Sdim if (GetFileInfo(&dl) == -1) 109239462Sdim errx(3, "`%s' is an unknown file type", argv[1]); 110234353Sdim 111234353Sdim switch (dl.image_type) { 112234353Sdim case IMAGE_TYPE_MOP: 113234353Sdim errx(3, "`%s' is already a MOP image", argv[1]); 114234353Sdim break; 115234353Sdim 116234353Sdim#ifndef NOELF 117234353Sdim case IMAGE_TYPE_ELF32: 118234353Sdim if (dl.e_machine != EM_VAX) 119234353Sdim printf("WARNING: `%s' is not a VAX image " 120234353Sdim "(machine=%d)\n", argv[1], dl.e_machine); 121234353Sdim for (i = 0, j = 0; j < dl.e_nsec; j++) 122234353Sdim i += dl.e_sections[j].s_fsize + dl.e_sections[j].s_pad; 123234353Sdim break; 124234353Sdim#endif 125234353Sdim 126234353Sdim#ifndef NOAOUT 127234353Sdim case IMAGE_TYPE_AOUT: 128288943Sdim if (dl.a_mid != MID_VAX) 129234353Sdim printf("WARNING: `%s' is not a VAX image (mid=%d)\n", 130234353Sdim argv[1], dl.a_mid); 131234353Sdim i = dl.a_text + dl.a_text_fill + dl.a_data + dl.a_data_fill + 132234353Sdim dl.a_bss + dl.a_bss_fill; 133234353Sdim break; 134234353Sdim#endif 135234353Sdim 136234353Sdim default: 137234353Sdim errx(3, "Image type `%s' not supported", 138234353Sdim FileTypeName(dl.image_type)); 139234353Sdim } 140234353Sdim 141234353Sdim i = (i+1) / 512; 142288943Sdim 143288943Sdim dl.nloadaddr = dl.loadaddr; 144288943Sdim dl.lseek = lseek(dl.ldfd,0L,SEEK_CUR); 145288943Sdim dl.a_lseek = 0; 146288943Sdim dl.count = 0; 147288943Sdim dl.dl_bsz = 512; 148288943Sdim 149288943Sdim mopFilePutLX(header,IHD_W_SIZE,0xd4,2); /* Offset to ISD section. */ 150288943Sdim mopFilePutLX(header,IHD_W_ACTIVOFF,0x30,2);/* Offset to 1st section.*/ 151288943Sdim mopFilePutLX(header,IHD_W_ALIAS,IHD_C_NATIVE,2);/* It's a VAX image.*/ 152288943Sdim mopFilePutLX(header,IHD_B_HDRBLKCNT,1,1); /* Only one header block. */ 153288943Sdim mopFilePutLX(header,0xd4+ISD_V_VPN,dl.loadaddr/512,2);/* load Addr */ 154288943Sdim mopFilePutLX(header,0x30+IHA_L_TFRADR1,dl.xferaddr,4); /* Xfer Addr */ 155288943Sdim mopFilePutLX(header,0xd4+ISD_W_PAGCNT,i,2);/* Imagesize in blks.*/ 156288943Sdim 157288943Sdim out = fopen (argv[2], "w"); 158288943Sdim if (!out) 159288943Sdim err(2, "writing `%s'", argv[2]); 160239462Sdim 161239462Sdim /* Now we do the actual work. Write VAX MOP-image header */ 162234353Sdim 163234353Sdim fwrite (header, sizeof (header), 1, out); 164234353Sdim 165234353Sdim switch (dl.image_type) { 166234353Sdim case IMAGE_TYPE_MOP: 167234353Sdim abort(); 168234353Sdim 169239462Sdim case IMAGE_TYPE_ELF32: 170251662Sdim#ifdef NOELF 171234353Sdim abort(); 172239462Sdim#else 173296417Sdim fprintf(stderr, "copying "); 174296417Sdim for (j = 0; j < dl.e_nsec; j++) 175239462Sdim fprintf(stderr, "%s%u+%u", j == 0 ? "" : "+", 176234353Sdim dl.e_sections[j].s_fsize, 177239462Sdim dl.e_sections[j].s_pad); 178234353Sdim fprintf(stderr, "->0x%x\n", dl.xferaddr); 179234353Sdim#endif 180251662Sdim break; 181251662Sdim 182251662Sdim case IMAGE_TYPE_AOUT: 183234353Sdim#ifdef NOAOUT 184239462Sdim abort(); 185234353Sdim#else 186251662Sdim fprintf(stderr, "copying %u+%u+%u->0x%x\n", dl.a_text, 187234353Sdim dl.a_data, dl.a_bss, dl.xferaddr); 188234353Sdim#endif 189234353Sdim break; 190234353Sdim } 191288943Sdim 192288943Sdim while ((i = mopFileRead(&dl,header)) > 0) { 193288943Sdim (void)fwrite(header, i, 1, out); 194280031Sdim } 195280031Sdim 196280031Sdim fclose (out); 197280031Sdim return (0); 198234353Sdim} 199234353Sdim