Home | History | Annotate | Line # | Download | only in common
file.c revision 1.6
      1  1.6  mycroft /*	$NetBSD: file.c,v 1.6 1998/07/25 06:01:13 mycroft Exp $	*/
      2  1.2  thorpej 
      3  1.1      cjs /*
      4  1.1      cjs  * Copyright (c) 1995-96 Mats O Jansson.  All rights reserved.
      5  1.1      cjs  *
      6  1.1      cjs  * Redistribution and use in source and binary forms, with or without
      7  1.1      cjs  * modification, are permitted provided that the following conditions
      8  1.1      cjs  * are met:
      9  1.1      cjs  * 1. Redistributions of source code must retain the above copyright
     10  1.1      cjs  *    notice, this list of conditions and the following disclaimer.
     11  1.1      cjs  * 2. Redistributions in binary form must reproduce the above copyright
     12  1.1      cjs  *    notice, this list of conditions and the following disclaimer in the
     13  1.1      cjs  *    documentation and/or other materials provided with the distribution.
     14  1.1      cjs  * 3. All advertising materials mentioning features or use of this software
     15  1.1      cjs  *    must display the following acknowledgement:
     16  1.1      cjs  *	This product includes software developed by Mats O Jansson.
     17  1.1      cjs  * 4. The name of the author may not be used to endorse or promote products
     18  1.1      cjs  *    derived from this software without specific prior written permission.
     19  1.1      cjs  *
     20  1.1      cjs  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     21  1.1      cjs  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     22  1.1      cjs  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     23  1.1      cjs  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     24  1.1      cjs  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     25  1.1      cjs  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     26  1.1      cjs  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     27  1.1      cjs  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     28  1.1      cjs  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     29  1.1      cjs  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     30  1.1      cjs  */
     31  1.1      cjs 
     32  1.4    lukem #include <sys/cdefs.h>
     33  1.4    lukem #ifndef lint
     34  1.6  mycroft __RCSID("$NetBSD: file.c,v 1.6 1998/07/25 06:01:13 mycroft Exp $");
     35  1.1      cjs #endif
     36  1.1      cjs 
     37  1.1      cjs #include "os.h"
     38  1.4    lukem #include "common.h"
     39  1.4    lukem #include "file.h"
     40  1.4    lukem #include "mopdef.h"
     41  1.1      cjs 
     42  1.1      cjs #ifndef NOAOUT
     43  1.1      cjs #if defined(__NetBSD__) || defined(__OpenBSD__)
     44  1.1      cjs #include <sys/exec_aout.h>
     45  1.1      cjs #endif
     46  1.1      cjs #if defined(__bsdi__)
     47  1.1      cjs #define NOAOUT
     48  1.1      cjs #endif
     49  1.1      cjs #if defined(__FreeBSD__)
     50  1.1      cjs #include <sys/imgact_aout.h>
     51  1.1      cjs #endif
     52  1.1      cjs #if !defined(MID_VAX)
     53  1.1      cjs #define MID_VAX 140
     54  1.1      cjs #endif
     55  1.1      cjs #endif
     56  1.1      cjs 
     57  1.4    lukem int	getCLBYTES __P((int));
     58  1.4    lukem int	getMID __P((int, int));
     59  1.4    lukem 
     60  1.1      cjs void
     61  1.1      cjs mopFilePutLX(buf, index, value, cnt)
     62  1.4    lukem 	u_char	       *buf;
     63  1.4    lukem 	int		index, cnt;
     64  1.4    lukem 	u_int32_t	value;
     65  1.1      cjs {
     66  1.1      cjs 	int i;
     67  1.1      cjs 	for (i = 0; i < cnt; i++) {
     68  1.1      cjs 		buf[index+i] = value % 256;
     69  1.1      cjs 		value = value / 256;
     70  1.1      cjs 	}
     71  1.1      cjs }
     72  1.1      cjs 
     73  1.1      cjs void
     74  1.1      cjs mopFilePutBX(buf, index, value, cnt)
     75  1.4    lukem 	u_char	       *buf;
     76  1.4    lukem 	int		index, cnt;
     77  1.4    lukem 	u_int32_t	value;
     78  1.1      cjs {
     79  1.1      cjs 	int i;
     80  1.1      cjs 	for (i = 0; i < cnt; i++) {
     81  1.1      cjs 		buf[index+cnt-1-i] = value % 256;
     82  1.1      cjs 		value = value / 256;
     83  1.1      cjs 	}
     84  1.1      cjs }
     85  1.1      cjs 
     86  1.4    lukem u_int32_t
     87  1.1      cjs mopFileGetLX(buf, index, cnt)
     88  1.1      cjs 	u_char	*buf;
     89  1.1      cjs 	int	index, cnt;
     90  1.1      cjs {
     91  1.4    lukem 	u_int32_t ret = 0;
     92  1.1      cjs 	int i;
     93  1.1      cjs 
     94  1.1      cjs 	for (i = 0; i < cnt; i++) {
     95  1.1      cjs 		ret = ret*256 + buf[index+cnt-1-i];
     96  1.1      cjs 	}
     97  1.1      cjs 
     98  1.1      cjs 	return(ret);
     99  1.1      cjs }
    100  1.1      cjs 
    101  1.4    lukem u_int32_t
    102  1.1      cjs mopFileGetBX(buf, index, cnt)
    103  1.1      cjs 	u_char	*buf;
    104  1.1      cjs 	int	index, cnt;
    105  1.1      cjs {
    106  1.4    lukem 	u_int32_t ret = 0;
    107  1.1      cjs 	int i;
    108  1.1      cjs 
    109  1.1      cjs 	for (i = 0; i < cnt; i++) {
    110  1.1      cjs 		ret = ret*256 + buf[index+i];
    111  1.1      cjs 	}
    112  1.1      cjs 
    113  1.1      cjs 	return(ret);
    114  1.1      cjs }
    115  1.1      cjs 
    116  1.1      cjs void
    117  1.1      cjs mopFileSwapX(buf, index, cnt)
    118  1.1      cjs 	u_char	*buf;
    119  1.1      cjs 	int	index, cnt;
    120  1.1      cjs {
    121  1.1      cjs 	int i;
    122  1.1      cjs 	u_char c;
    123  1.1      cjs 
    124  1.1      cjs 	for (i = 0; i < (cnt / 2); i++) {
    125  1.1      cjs 		c = buf[index+i];
    126  1.1      cjs 		buf[index+i] = buf[index+cnt-1-i];
    127  1.1      cjs 		buf[index+cnt-1-i] = c;
    128  1.1      cjs 	}
    129  1.1      cjs 
    130  1.1      cjs }
    131  1.1      cjs 
    132  1.1      cjs int
    133  1.1      cjs CheckMopFile(fd)
    134  1.1      cjs 	int	fd;
    135  1.1      cjs {
    136  1.1      cjs 	u_char	header[512];
    137  1.1      cjs 	short	image_type;
    138  1.1      cjs 
    139  1.1      cjs 	if (read(fd, header, 512) != 512)
    140  1.1      cjs 		return(-1);
    141  1.1      cjs 
    142  1.1      cjs 	(void)lseek(fd, (off_t) 0, SEEK_SET);
    143  1.1      cjs 
    144  1.1      cjs 	image_type = (u_short)(header[IHD_W_ALIAS+1]*256 +
    145  1.1      cjs 			       header[IHD_W_ALIAS]);
    146  1.1      cjs 
    147  1.1      cjs 	switch(image_type) {
    148  1.1      cjs 		case IHD_C_NATIVE:		/* Native mode image (VAX)   */
    149  1.1      cjs 		case IHD_C_RSX:			/* RSX image produced by TKB */
    150  1.1      cjs 		case IHD_C_BPA:			/* BASIC plus analog         */
    151  1.1      cjs 		case IHD_C_ALIAS:		/* Alias		     */
    152  1.1      cjs 		case IHD_C_CLI:			/* Image is CLI		     */
    153  1.1      cjs 		case IHD_C_PMAX:		/* PMAX system image	     */
    154  1.1      cjs 		case IHD_C_ALPHA:		/* ALPHA system image	     */
    155  1.1      cjs 			break;
    156  1.1      cjs 		default:
    157  1.1      cjs 			return(-1);
    158  1.1      cjs 	}
    159  1.1      cjs 
    160  1.1      cjs 	return(0);
    161  1.1      cjs }
    162  1.1      cjs 
    163  1.1      cjs int
    164  1.1      cjs GetMopFileInfo(fd, load, xfr)
    165  1.4    lukem 	int		fd;
    166  1.4    lukem 	u_int32_t      *load, *xfr;
    167  1.1      cjs {
    168  1.4    lukem 	u_char		header[512];
    169  1.4    lukem 	short		image_type;
    170  1.4    lukem 	u_int32_t	load_addr, xfr_addr, isd, iha, hbcnt, isize;
    171  1.1      cjs 
    172  1.1      cjs 	if (read(fd, header, 512) != 512)
    173  1.1      cjs 		return(-1);
    174  1.1      cjs 
    175  1.1      cjs 	image_type = (u_short)(header[IHD_W_ALIAS+1]*256 +
    176  1.1      cjs 			       header[IHD_W_ALIAS]);
    177  1.1      cjs 
    178  1.1      cjs 	switch(image_type) {
    179  1.1      cjs 		case IHD_C_NATIVE:		/* Native mode image (VAX)   */
    180  1.1      cjs 			isd = (header[IHD_W_SIZE+1]*256 +
    181  1.1      cjs 			       header[IHD_W_SIZE]);
    182  1.1      cjs 			iha = (header[IHD_W_ACTIVOFF+1]*256 +
    183  1.1      cjs 			       header[IHD_W_ACTIVOFF]);
    184  1.1      cjs 			hbcnt = (header[IHD_B_HDRBLKCNT]);
    185  1.1      cjs 			isize = (header[isd+ISD_W_PAGCNT+1]*256 +
    186  1.1      cjs 				 header[isd+ISD_W_PAGCNT]) * 512;
    187  1.1      cjs 			load_addr = ((header[isd+ISD_V_VPN+1]*256 +
    188  1.1      cjs 				      header[isd+ISD_V_VPN]) & ISD_M_VPN)
    189  1.1      cjs 					* 512;
    190  1.1      cjs 			xfr_addr = (header[iha+IHA_L_TFRADR1+3]*0x1000000 +
    191  1.1      cjs 				    header[iha+IHA_L_TFRADR1+2]*0x10000 +
    192  1.1      cjs 				    header[iha+IHA_L_TFRADR1+1]*0x100 +
    193  1.1      cjs 				    header[iha+IHA_L_TFRADR1]) & 0x7fffffff;
    194  1.1      cjs 			printf("Native Image (VAX)\n");
    195  1.1      cjs 			printf("Header Block Count: %d\n",hbcnt);
    196  1.1      cjs 			printf("Image Size:         %08x\n",isize);
    197  1.1      cjs 			printf("Load Address:       %08x\n",load_addr);
    198  1.1      cjs 			printf("Transfer Address:   %08x\n",xfr_addr);
    199  1.1      cjs 			break;
    200  1.1      cjs 		case IHD_C_RSX:			/* RSX image produced by TKB */
    201  1.1      cjs 			hbcnt = header[L_BBLK+1]*256 + header[L_BBLK];
    202  1.1      cjs 			isize = (header[L_BLDZ+1]*256 + header[L_BLDZ]) * 64;
    203  1.1      cjs 			load_addr = header[L_BSA+1]*256 + header[L_BSA];
    204  1.1      cjs 			xfr_addr  = header[L_BXFR+1]*256 + header[L_BXFR];
    205  1.1      cjs 			printf("RSX Image\n");
    206  1.1      cjs 			printf("Header Block Count: %d\n",hbcnt);
    207  1.1      cjs 			printf("Image Size:         %08x\n",isize);
    208  1.1      cjs 			printf("Load Address:       %08x\n",load_addr);
    209  1.1      cjs 			printf("Transfer Address:   %08x\n",xfr_addr);
    210  1.1      cjs 			break;
    211  1.1      cjs 		case IHD_C_BPA:			/* BASIC plus analog         */
    212  1.1      cjs 			printf("BASIC-Plus Image, not supported\n");
    213  1.1      cjs 			return(-1);
    214  1.1      cjs 			break;
    215  1.1      cjs 		case IHD_C_ALIAS:		/* Alias		     */
    216  1.1      cjs 			printf("Alias, not supported\n");
    217  1.1      cjs 			return(-1);
    218  1.1      cjs 			break;
    219  1.1      cjs 		case IHD_C_CLI:			/* Image is CLI		     */
    220  1.1      cjs 			printf("CLI, not supported\n");
    221  1.1      cjs 			return(-1);
    222  1.1      cjs 			break;
    223  1.1      cjs 		case IHD_C_PMAX:		/* PMAX system image	     */
    224  1.1      cjs 			isd = (header[IHD_W_SIZE+1]*256 +
    225  1.1      cjs 			       header[IHD_W_SIZE]);
    226  1.1      cjs 			iha = (header[IHD_W_ACTIVOFF+1]*256 +
    227  1.1      cjs 			       header[IHD_W_ACTIVOFF]);
    228  1.1      cjs 			hbcnt = (header[IHD_B_HDRBLKCNT]);
    229  1.1      cjs 			isize = (header[isd+ISD_W_PAGCNT+1]*256 +
    230  1.1      cjs 				 header[isd+ISD_W_PAGCNT]) * 512;
    231  1.1      cjs 			load_addr = (header[isd+ISD_V_VPN+1]*256 +
    232  1.1      cjs 				     header[isd+ISD_V_VPN]) * 512;
    233  1.1      cjs 			xfr_addr = (header[iha+IHA_L_TFRADR1+3]*0x1000000 +
    234  1.1      cjs 				    header[iha+IHA_L_TFRADR1+2]*0x10000 +
    235  1.1      cjs 				    header[iha+IHA_L_TFRADR1+1]*0x100 +
    236  1.1      cjs 				    header[iha+IHA_L_TFRADR1]);
    237  1.1      cjs 			printf("PMAX Image \n");
    238  1.1      cjs 			printf("Header Block Count: %d\n",hbcnt);
    239  1.1      cjs 			printf("Image Size:         %08x\n",isize);
    240  1.1      cjs 			printf("Load Address:       %08x\n",load_addr);
    241  1.1      cjs 			printf("Transfer Address:   %08x\n",xfr_addr);
    242  1.1      cjs 			break;
    243  1.1      cjs 		case IHD_C_ALPHA:		/* ALPHA system image	     */
    244  1.1      cjs 			isd = (header[EIHD_L_ISDOFF+3]*0x1000000 +
    245  1.1      cjs 			       header[EIHD_L_ISDOFF+2]*0x10000 +
    246  1.1      cjs 			       header[EIHD_L_ISDOFF+1]*0x100 +
    247  1.1      cjs 			       header[EIHD_L_ISDOFF]);
    248  1.1      cjs 			hbcnt = (header[EIHD_L_HDRBLKCNT+3]*0x1000000 +
    249  1.1      cjs 				 header[EIHD_L_HDRBLKCNT+2]*0x10000 +
    250  1.1      cjs 				 header[EIHD_L_HDRBLKCNT+1]*0x100 +
    251  1.1      cjs 				 header[EIHD_L_HDRBLKCNT]);
    252  1.1      cjs 			isize = (header[isd+EISD_L_SECSIZE+3]*0x1000000 +
    253  1.1      cjs 				 header[isd+EISD_L_SECSIZE+2]*0x10000 +
    254  1.1      cjs 				 header[isd+EISD_L_SECSIZE+1]*0x100 +
    255  1.1      cjs 				 header[isd+EISD_L_SECSIZE]);
    256  1.1      cjs 			load_addr = 0;
    257  1.1      cjs 			xfr_addr = 0;
    258  1.1      cjs 			printf("Alpha Image \n");
    259  1.1      cjs 			printf("Header Block Count: %d\n",hbcnt);
    260  1.1      cjs 			printf("Image Size:         %08x\n",isize);
    261  1.1      cjs 			printf("Load Address:       %08x\n",load_addr);
    262  1.1      cjs 			printf("Transfer Address:   %08x\n",xfr_addr);
    263  1.1      cjs 			break;
    264  1.1      cjs 		default:
    265  1.1      cjs 			printf("Unknown Image (%d)\n",image_type);
    266  1.1      cjs 			return(-1);
    267  1.1      cjs 	}
    268  1.1      cjs 
    269  1.1      cjs 	if (load != NULL) {
    270  1.1      cjs 		*load = load_addr;
    271  1.1      cjs 	}
    272  1.1      cjs 
    273  1.1      cjs 	if (xfr != NULL) {
    274  1.1      cjs 		*xfr  = xfr_addr;
    275  1.1      cjs 	}
    276  1.1      cjs 
    277  1.1      cjs 	return(0);
    278  1.1      cjs }
    279  1.1      cjs 
    280  1.1      cjs #ifndef NOAOUT
    281  1.1      cjs int
    282  1.1      cjs getMID(old_mid,new_mid)
    283  1.1      cjs 	int	old_mid, new_mid;
    284  1.1      cjs {
    285  1.1      cjs 	int	mid;
    286  1.1      cjs 
    287  1.1      cjs 	mid = old_mid;
    288  1.1      cjs 
    289  1.1      cjs 	switch (new_mid) {
    290  1.1      cjs 	case MID_I386:
    291  1.1      cjs 		mid = MID_I386;
    292  1.1      cjs 		break;
    293  1.1      cjs #ifdef MID_M68K
    294  1.1      cjs 	case MID_M68K:
    295  1.1      cjs 		mid = MID_M68K;
    296  1.1      cjs 		break;
    297  1.1      cjs #endif
    298  1.1      cjs #ifdef MID_M68K4K
    299  1.1      cjs 	case MID_M68K4K:
    300  1.1      cjs 		mid = MID_M68K4K;
    301  1.1      cjs 		break;
    302  1.1      cjs #endif
    303  1.1      cjs #ifdef MID_NS32532
    304  1.1      cjs 	case MID_NS32532:
    305  1.1      cjs 		mid = MID_NS32532;
    306  1.1      cjs 		break;
    307  1.1      cjs #endif
    308  1.1      cjs 	case MID_SPARC:
    309  1.1      cjs 		mid = MID_SPARC;
    310  1.1      cjs 		break;
    311  1.1      cjs #ifdef MID_PMAX
    312  1.1      cjs 	case MID_PMAX:
    313  1.1      cjs 		mid = MID_PMAX;
    314  1.1      cjs 		break;
    315  1.1      cjs #endif
    316  1.1      cjs #ifdef MID_VAX
    317  1.1      cjs 	case MID_VAX:
    318  1.1      cjs 		mid = MID_VAX;
    319  1.1      cjs 		break;
    320  1.1      cjs #endif
    321  1.1      cjs #ifdef MID_ALPHA
    322  1.1      cjs 	case MID_ALPHA:
    323  1.1      cjs 		mid = MID_ALPHA;
    324  1.1      cjs 		break;
    325  1.1      cjs #endif
    326  1.1      cjs #ifdef MID_MIPS
    327  1.1      cjs 	case MID_MIPS:
    328  1.1      cjs 		mid = MID_MIPS;
    329  1.1      cjs 		break;
    330  1.1      cjs #endif
    331  1.1      cjs #ifdef MID_ARM6
    332  1.1      cjs 	case MID_ARM6:
    333  1.1      cjs 		mid = MID_ARM6;
    334  1.1      cjs 		break;
    335  1.1      cjs #endif
    336  1.1      cjs 	default:
    337  1.5      cgd 		break;
    338  1.1      cjs 	}
    339  1.1      cjs 
    340  1.1      cjs 	return(mid);
    341  1.1      cjs }
    342  1.1      cjs 
    343  1.1      cjs int
    344  1.1      cjs getCLBYTES(mid)
    345  1.1      cjs 	int	mid;
    346  1.1      cjs {
    347  1.1      cjs 	int	clbytes;
    348  1.1      cjs 
    349  1.1      cjs 	switch (mid) {
    350  1.1      cjs #ifdef MID_VAX
    351  1.1      cjs 	case MID_VAX:
    352  1.1      cjs 		clbytes = 1024;
    353  1.1      cjs 		break;
    354  1.1      cjs #endif
    355  1.6  mycroft #ifdef MID_I386
    356  1.1      cjs 	case MID_I386:
    357  1.6  mycroft #endif
    358  1.1      cjs #ifdef MID_M68K4K
    359  1.1      cjs 	case MID_M68K4K:
    360  1.1      cjs #endif
    361  1.1      cjs #ifdef MID_NS32532
    362  1.1      cjs 	case MID_NS32532:
    363  1.1      cjs #endif
    364  1.1      cjs #ifdef MID_PMAX
    365  1.1      cjs 	case MID_PMAX:
    366  1.1      cjs #endif
    367  1.1      cjs #ifdef MID_MIPS
    368  1.1      cjs 	case MID_MIPS:
    369  1.1      cjs #endif
    370  1.1      cjs #ifdef MID_ARM6
    371  1.1      cjs 	case MID_ARM6:
    372  1.1      cjs #endif
    373  1.6  mycroft #if defined(MID_I386) || defined(MID_M68K4K) || defined(MID_NS32532) || \
    374  1.6  mycroft     defined(MID_PMAX) || defined(MID_MIPS) || defined(MID_ARM6)
    375  1.1      cjs 		clbytes = 4096;
    376  1.1      cjs 		break;
    377  1.6  mycroft #endif
    378  1.1      cjs #ifdef MID_M68K
    379  1.1      cjs 	case MID_M68K:
    380  1.1      cjs #endif
    381  1.1      cjs #ifdef MID_ALPHA
    382  1.1      cjs 	case MID_ALPHA:
    383  1.1      cjs #endif
    384  1.6  mycroft #ifdef MID_SPARC
    385  1.6  mycroft 	case MID_SPARC:
    386  1.6  mycroft #endif
    387  1.6  mycroft #if defined(MID_M68K) || defined(MID_ALPHA) || defined(MID_SPARC)
    388  1.1      cjs 		clbytes = 8192;
    389  1.1      cjs 		break;
    390  1.1      cjs #endif
    391  1.1      cjs 	default:
    392  1.1      cjs 		clbytes = 0;
    393  1.1      cjs 	}
    394  1.1      cjs 
    395  1.1      cjs 	return(clbytes);
    396  1.1      cjs }
    397  1.1      cjs #endif
    398  1.1      cjs 
    399  1.1      cjs int
    400  1.1      cjs CheckAOutFile(fd)
    401  1.1      cjs 	int	fd;
    402  1.1      cjs {
    403  1.1      cjs #ifdef NOAOUT
    404  1.1      cjs 	return(-1);
    405  1.1      cjs #else
    406  1.1      cjs 	struct exec ex, ex_swap;
    407  1.1      cjs 	int	mid = -1;
    408  1.1      cjs 
    409  1.1      cjs 	if (read(fd, (char *)&ex, sizeof(ex)) != sizeof(ex))
    410  1.1      cjs 		return(-1);
    411  1.1      cjs 
    412  1.1      cjs 	(void)lseek(fd, (off_t) 0, SEEK_SET);
    413  1.1      cjs 
    414  1.1      cjs 	if (read(fd, (char *)&ex_swap, sizeof(ex_swap)) != sizeof(ex_swap))
    415  1.1      cjs 		return(-1);
    416  1.1      cjs 
    417  1.1      cjs 	(void)lseek(fd, (off_t) 0, SEEK_SET);
    418  1.1      cjs 
    419  1.1      cjs 	mid = getMID(mid, N_GETMID (ex));
    420  1.1      cjs 
    421  1.1      cjs 	if (mid == -1) {
    422  1.1      cjs 		mid = getMID(mid, N_GETMID (ex_swap));
    423  1.1      cjs 	}
    424  1.1      cjs 
    425  1.1      cjs 	if (mid != -1) {
    426  1.1      cjs 		return(0);
    427  1.1      cjs 	} else {
    428  1.1      cjs 		return(-1);
    429  1.1      cjs 	}
    430  1.1      cjs #endif NOAOUT
    431  1.1      cjs }
    432  1.1      cjs 
    433  1.1      cjs int
    434  1.1      cjs GetAOutFileInfo(fd, load, xfr, a_text, a_text_fill,
    435  1.1      cjs 		a_data, a_data_fill, a_bss, a_bss_fill, aout)
    436  1.4    lukem 	int		 fd;
    437  1.4    lukem 	u_int32_t	*load, *xfr, *a_text, *a_text_fill;
    438  1.4    lukem 	u_int32_t	*a_data, *a_data_fill, *a_bss, *a_bss_fill;
    439  1.4    lukem 	int		 *aout;
    440  1.1      cjs {
    441  1.1      cjs #ifdef NOAOUT
    442  1.1      cjs 	return(-1);
    443  1.1      cjs #else
    444  1.1      cjs 	struct exec ex, ex_swap;
    445  1.4    lukem 	u_int32_t	mid = -1;
    446  1.4    lukem 	u_int32_t	magic, clbytes, clofset;
    447  1.1      cjs 
    448  1.1      cjs 	if (read(fd, (char *)&ex, sizeof(ex)) != sizeof(ex))
    449  1.1      cjs 		return(-1);
    450  1.1      cjs 
    451  1.1      cjs 	(void)lseek(fd, (off_t) 0, SEEK_SET);
    452  1.1      cjs 
    453  1.1      cjs 	if (read(fd, (char *)&ex_swap, sizeof(ex_swap)) != sizeof(ex_swap))
    454  1.1      cjs 		return(-1);
    455  1.1      cjs 
    456  1.1      cjs 	mopFileSwapX((u_char *)&ex_swap, 0, 4);
    457  1.1      cjs 
    458  1.1      cjs 	mid = getMID(mid, N_GETMID (ex));
    459  1.1      cjs 
    460  1.1      cjs 	if (mid == -1) {
    461  1.1      cjs 		mid = getMID(mid, N_GETMID (ex_swap));
    462  1.1      cjs 		if (mid != -1) {
    463  1.1      cjs 			mopFileSwapX((u_char *)&ex, 0, 4);
    464  1.1      cjs 		}
    465  1.1      cjs 	}
    466  1.1      cjs 
    467  1.1      cjs 	if (mid == -1) {
    468  1.1      cjs 		return(-1);
    469  1.1      cjs 	}
    470  1.1      cjs 
    471  1.1      cjs 	if (N_BADMAG (ex)) {
    472  1.1      cjs 		return(-1);
    473  1.1      cjs 	}
    474  1.1      cjs 
    475  1.1      cjs 	switch (mid) {
    476  1.1      cjs 	case MID_I386:
    477  1.1      cjs #ifdef MID_NS32532
    478  1.1      cjs 	case MID_NS32532:
    479  1.1      cjs #endif
    480  1.1      cjs #ifdef MID_PMAX
    481  1.1      cjs 	case MID_PMAX:
    482  1.1      cjs #endif
    483  1.1      cjs #ifdef MID_VAX
    484  1.1      cjs 	case MID_VAX:
    485  1.1      cjs #endif
    486  1.1      cjs #ifdef MID_ALPHA
    487  1.1      cjs 	case MID_ALPHA:
    488  1.1      cjs #endif
    489  1.1      cjs #ifdef MID_ARM6
    490  1.1      cjs 	case MID_ARM6:
    491  1.1      cjs #endif
    492  1.1      cjs 		ex.a_text  = mopFileGetLX((u_char *)&ex_swap,  4, 4);
    493  1.1      cjs 		ex.a_data  = mopFileGetLX((u_char *)&ex_swap,  8, 4);
    494  1.1      cjs 		ex.a_bss   = mopFileGetLX((u_char *)&ex_swap, 12, 4);
    495  1.1      cjs 		ex.a_syms  = mopFileGetLX((u_char *)&ex_swap, 16, 4);
    496  1.1      cjs 		ex.a_entry = mopFileGetLX((u_char *)&ex_swap, 20, 4);
    497  1.1      cjs 		ex.a_trsize= mopFileGetLX((u_char *)&ex_swap, 24, 4);
    498  1.1      cjs 		ex.a_drsize= mopFileGetLX((u_char *)&ex_swap, 28, 4);
    499  1.1      cjs 		break;
    500  1.1      cjs #ifdef MID_M68K
    501  1.1      cjs 	case MID_M68K:
    502  1.1      cjs #endif
    503  1.1      cjs #ifdef MID_M68K4K
    504  1.1      cjs 	case MID_M68K4K:
    505  1.1      cjs #endif
    506  1.1      cjs 	case MID_SPARC:
    507  1.1      cjs #ifdef MID_MIPS
    508  1.1      cjs 	case MID_MIPS:
    509  1.1      cjs #endif
    510  1.1      cjs 		ex.a_text  = mopFileGetBX((u_char *)&ex_swap,  4, 4);
    511  1.1      cjs 		ex.a_data  = mopFileGetBX((u_char *)&ex_swap,  8, 4);
    512  1.1      cjs 		ex.a_bss   = mopFileGetBX((u_char *)&ex_swap, 12, 4);
    513  1.1      cjs 		ex.a_syms  = mopFileGetBX((u_char *)&ex_swap, 16, 4);
    514  1.1      cjs 		ex.a_entry = mopFileGetBX((u_char *)&ex_swap, 20, 4);
    515  1.1      cjs 		ex.a_trsize= mopFileGetBX((u_char *)&ex_swap, 24, 4);
    516  1.1      cjs 		ex.a_drsize= mopFileGetBX((u_char *)&ex_swap, 28, 4);
    517  1.1      cjs 		break;
    518  1.1      cjs 	default:
    519  1.5      cgd 		break;
    520  1.1      cjs 	}
    521  1.1      cjs 
    522  1.1      cjs 	printf("a.out image (");
    523  1.1      cjs 	switch (N_GETMID (ex)) {
    524  1.1      cjs 	case MID_I386:
    525  1.1      cjs 		printf("i386");
    526  1.1      cjs 		break;
    527  1.1      cjs #ifdef MID_M68K
    528  1.1      cjs 	case MID_M68K:
    529  1.1      cjs 		printf("m68k");
    530  1.1      cjs 		break;
    531  1.1      cjs #endif
    532  1.1      cjs #ifdef MID_M68K4K
    533  1.1      cjs 	case MID_M68K4K:
    534  1.1      cjs 		printf("m68k 4k");
    535  1.1      cjs 		break;
    536  1.1      cjs #endif
    537  1.1      cjs #ifdef MID_NS32532
    538  1.1      cjs 	case MID_NS32532:
    539  1.1      cjs 		printf("pc532");
    540  1.1      cjs 		break;
    541  1.1      cjs #endif
    542  1.1      cjs 	case MID_SPARC:
    543  1.1      cjs 		printf("sparc");
    544  1.1      cjs 		break;
    545  1.1      cjs #ifdef MID_PMAX
    546  1.1      cjs 	case MID_PMAX:
    547  1.1      cjs 		printf("pmax");
    548  1.1      cjs 		break;
    549  1.1      cjs #endif
    550  1.1      cjs #ifdef MID_VAX
    551  1.1      cjs 	case MID_VAX:
    552  1.1      cjs 		printf("vax");
    553  1.1      cjs 		break;
    554  1.1      cjs #endif
    555  1.1      cjs #ifdef MID_ALPHA
    556  1.1      cjs 	case MID_ALPHA:
    557  1.1      cjs 		printf("alpha");
    558  1.1      cjs 		break;
    559  1.1      cjs #endif
    560  1.1      cjs #ifdef MID_MIPS
    561  1.1      cjs 	case MID_MIPS:
    562  1.1      cjs 		printf("mips");
    563  1.1      cjs 		break;
    564  1.1      cjs #endif
    565  1.1      cjs #ifdef MID_ARM6
    566  1.1      cjs 	case MID_ARM6:
    567  1.1      cjs 		printf("arm32");
    568  1.1      cjs 		break;
    569  1.1      cjs #endif
    570  1.1      cjs 	default:
    571  1.5      cgd 		break;
    572  1.1      cjs 	}
    573  1.1      cjs 	printf(") Magic: ");
    574  1.1      cjs 	switch (N_GETMAGIC (ex)) {
    575  1.1      cjs 	case OMAGIC:
    576  1.1      cjs 		printf("OMAGIC");
    577  1.1      cjs 		break;
    578  1.1      cjs 	case NMAGIC:
    579  1.1      cjs 		printf("NMAGIC");
    580  1.1      cjs 		break;
    581  1.1      cjs 	case ZMAGIC:
    582  1.1      cjs 		printf("ZMAGIC");
    583  1.1      cjs 		break;
    584  1.1      cjs 	case QMAGIC:
    585  1.1      cjs 		printf("QMAGIC");
    586  1.1      cjs 		break;
    587  1.1      cjs 	default:
    588  1.4    lukem 		printf("Unknown %ld", (long) N_GETMAGIC (ex));
    589  1.1      cjs 	}
    590  1.1      cjs 	printf("\n");
    591  1.4    lukem 	printf("Size of text:       %08lx\n", (long)ex.a_text);
    592  1.4    lukem 	printf("Size of data:       %08lx\n", (long)ex.a_data);
    593  1.4    lukem 	printf("Size of bss:        %08lx\n", (long)ex.a_bss);
    594  1.4    lukem 	printf("Size of symbol tab: %08lx\n", (long)ex.a_syms);
    595  1.4    lukem 	printf("Transfer Address:   %08lx\n", (long)ex.a_entry);
    596  1.4    lukem 	printf("Size of reloc text: %08lx\n", (long)ex.a_trsize);
    597  1.4    lukem 	printf("Size of reloc data: %08lx\n", (long)ex.a_drsize);
    598  1.3    lukem 
    599  1.1      cjs 	magic = N_GETMAGIC (ex);
    600  1.1      cjs 	clbytes = getCLBYTES(mid);
    601  1.1      cjs 	clofset = clbytes - 1;
    602  1.1      cjs 
    603  1.1      cjs 	if (load != NULL) {
    604  1.1      cjs 		*load   = 0;
    605  1.1      cjs 	}
    606  1.1      cjs 
    607  1.1      cjs 	if (xfr != NULL) {
    608  1.1      cjs 		*xfr    = ex.a_entry;
    609  1.1      cjs 	}
    610  1.1      cjs 
    611  1.1      cjs 	if (a_text != NULL) {
    612  1.1      cjs 		*a_text = ex.a_text;
    613  1.1      cjs 	}
    614  1.1      cjs 
    615  1.1      cjs 	if (a_text_fill != NULL) {
    616  1.1      cjs 		if (magic == ZMAGIC || magic == NMAGIC) {
    617  1.1      cjs 			*a_text_fill = clbytes - (ex.a_text & clofset);
    618  1.1      cjs 			if (*a_text_fill == clbytes) {
    619  1.1      cjs 				*a_text_fill = 0;
    620  1.1      cjs 			}
    621  1.1      cjs 		} else {
    622  1.1      cjs 			*a_text_fill = 0;
    623  1.1      cjs 	        }
    624  1.1      cjs 	}
    625  1.1      cjs 
    626  1.1      cjs 	if (a_data != NULL) {
    627  1.1      cjs 		*a_data = ex.a_data;
    628  1.1      cjs 	}
    629  1.1      cjs 
    630  1.1      cjs 	if (a_data_fill != NULL) {
    631  1.1      cjs 		if (magic == ZMAGIC || magic == NMAGIC) {
    632  1.1      cjs 			*a_data_fill = clbytes - (ex.a_data & clofset);
    633  1.1      cjs 			if (*a_data_fill == clbytes) {
    634  1.1      cjs 				*a_data_fill = 0;
    635  1.1      cjs 			}
    636  1.1      cjs 		} else {
    637  1.1      cjs 			*a_data_fill = 0;
    638  1.1      cjs 	        }
    639  1.1      cjs 	}
    640  1.1      cjs 
    641  1.1      cjs 	if (a_bss != NULL) {
    642  1.1      cjs 		*a_bss  = ex.a_bss;
    643  1.1      cjs 	}
    644  1.1      cjs 
    645  1.1      cjs 	if (a_bss_fill != NULL) {
    646  1.1      cjs 		if (magic == ZMAGIC || magic == NMAGIC) {
    647  1.1      cjs 			*a_bss_fill = clbytes - (ex.a_bss & clofset);
    648  1.1      cjs 			if (*a_bss_fill == clbytes) {
    649  1.1      cjs 				*a_bss_fill = 0;
    650  1.1      cjs 			}
    651  1.1      cjs 		} else {
    652  1.1      cjs 			*a_bss_fill = clbytes -
    653  1.1      cjs 				((ex.a_text+ex.a_data+ex.a_bss) & clofset);
    654  1.1      cjs 			if (*a_text_fill == clbytes) {
    655  1.1      cjs 				*a_text_fill = 0;
    656  1.1      cjs 			}
    657  1.1      cjs 	        }
    658  1.1      cjs 	}
    659  1.1      cjs 
    660  1.1      cjs 	if (aout != NULL) {
    661  1.1      cjs 		*aout = mid;
    662  1.1      cjs 	}
    663  1.1      cjs 
    664  1.1      cjs 	return(0);
    665  1.1      cjs #endif NOAOUT
    666  1.1      cjs }
    667  1.1      cjs 
    668  1.1      cjs int
    669  1.1      cjs GetFileInfo(fd, load, xfr, aout,
    670  1.1      cjs 	    a_text, a_text_fill, a_data, a_data_fill, a_bss, a_bss_fill)
    671  1.1      cjs 	int	fd, *aout;
    672  1.4    lukem 	u_int32_t	*load, *xfr, *a_text, *a_text_fill;
    673  1.4    lukem 	u_int32_t	*a_data, *a_data_fill, *a_bss, *a_bss_fill;
    674  1.1      cjs {
    675  1.1      cjs 	int	err;
    676  1.1      cjs 
    677  1.1      cjs 	err = CheckAOutFile(fd);
    678  1.1      cjs 
    679  1.1      cjs 	if (err == 0) {
    680  1.1      cjs 		err = GetAOutFileInfo(fd, load, xfr,
    681  1.1      cjs 				      a_text, a_text_fill,
    682  1.1      cjs 				      a_data, a_data_fill,
    683  1.1      cjs 				      a_bss, a_bss_fill,
    684  1.1      cjs 				      aout);
    685  1.1      cjs 		if (err != 0) {
    686  1.1      cjs 			return(-1);
    687  1.1      cjs 		}
    688  1.1      cjs 	} else {
    689  1.1      cjs 		err = CheckMopFile(fd);
    690  1.1      cjs 
    691  1.1      cjs 		if (err == 0) {
    692  1.1      cjs 			err = GetMopFileInfo(fd, load, xfr);
    693  1.1      cjs 			if (err != 0) {
    694  1.1      cjs 				return(-1);
    695  1.1      cjs 			}
    696  1.1      cjs 			*aout = -1;
    697  1.1      cjs 		} else {
    698  1.1      cjs 			return(-1);
    699  1.1      cjs 		}
    700  1.1      cjs 	}
    701  1.1      cjs 
    702  1.1      cjs 	return(0);
    703  1.1      cjs }
    704  1.1      cjs 
    705  1.1      cjs ssize_t
    706  1.1      cjs mopFileRead(dlslot, buf)
    707  1.1      cjs 	struct dllist *dlslot;
    708  1.1      cjs 	u_char	*buf;
    709  1.1      cjs {
    710  1.1      cjs 	ssize_t len, outlen;
    711  1.1      cjs 	int	bsz;
    712  1.4    lukem 	int32_t	pos, notdone, total;
    713  1.1      cjs 
    714  1.1      cjs 	if (dlslot->aout == -1) {
    715  1.1      cjs 		len = read(dlslot->ldfd,buf,dlslot->dl_bsz);
    716  1.1      cjs 	} else {
    717  1.1      cjs 		bsz = dlslot->dl_bsz;
    718  1.1      cjs 		pos = dlslot->a_lseek;
    719  1.1      cjs 		len = 0;
    720  1.1      cjs 
    721  1.1      cjs 		total = dlslot->a_text;
    722  1.1      cjs 
    723  1.1      cjs 		if (pos < total) {
    724  1.1      cjs 			notdone = total - pos;
    725  1.1      cjs 			if (notdone <= bsz) {
    726  1.1      cjs 				outlen = read(dlslot->ldfd,&buf[len],notdone);
    727  1.1      cjs 			} else {
    728  1.1      cjs 				outlen = read(dlslot->ldfd,&buf[len],bsz);
    729  1.1      cjs 			}
    730  1.1      cjs 			len = len + outlen;
    731  1.1      cjs 			pos = pos + outlen;
    732  1.1      cjs 			bsz = bsz - outlen;
    733  1.1      cjs 		}
    734  1.1      cjs 
    735  1.1      cjs 		total = total + dlslot->a_text_fill;
    736  1.1      cjs 
    737  1.1      cjs 		if ((bsz > 0) && (pos < total)) {
    738  1.1      cjs 			notdone = total - pos;
    739  1.1      cjs 			if (notdone <= bsz) {
    740  1.1      cjs 				outlen = notdone;
    741  1.1      cjs 			} else {
    742  1.1      cjs 				outlen = bsz;
    743  1.1      cjs 			}
    744  1.4    lukem 			memset(&buf[len], 0, outlen);
    745  1.1      cjs 			len = len + outlen;
    746  1.1      cjs 			pos = pos + outlen;
    747  1.1      cjs 			bsz = bsz - outlen;
    748  1.1      cjs 		}
    749  1.1      cjs 
    750  1.1      cjs 		total = total + dlslot->a_data;
    751  1.1      cjs 
    752  1.1      cjs 		if ((bsz > 0) && (pos < total)) {
    753  1.1      cjs 			notdone = total - pos;
    754  1.1      cjs 			if (notdone <= bsz) {
    755  1.1      cjs 				outlen = read(dlslot->ldfd,&buf[len],notdone);
    756  1.1      cjs 			} else {
    757  1.1      cjs 				outlen = read(dlslot->ldfd,&buf[len],bsz);
    758  1.1      cjs 			}
    759  1.1      cjs 			len = len + outlen;
    760  1.1      cjs 			pos = pos + outlen;
    761  1.1      cjs 			bsz = bsz - outlen;
    762  1.1      cjs 		}
    763  1.1      cjs 
    764  1.1      cjs 		total = total + dlslot->a_data_fill;
    765  1.1      cjs 
    766  1.1      cjs 		if ((bsz > 0) && (pos < total)) {
    767  1.1      cjs 			notdone = total - pos;
    768  1.1      cjs 			if (notdone <= bsz) {
    769  1.1      cjs 				outlen = notdone;
    770  1.1      cjs 			} else {
    771  1.1      cjs 				outlen = bsz;
    772  1.1      cjs 			}
    773  1.4    lukem 			memset(&buf[len], 0, outlen);
    774  1.1      cjs 			len = len + outlen;
    775  1.1      cjs 			pos = pos + outlen;
    776  1.1      cjs 			bsz = bsz - outlen;
    777  1.1      cjs 		}
    778  1.1      cjs 
    779  1.1      cjs 		total = total + dlslot->a_bss;
    780  1.1      cjs 
    781  1.1      cjs 		if ((bsz > 0) && (pos < total)) {
    782  1.1      cjs 			notdone = total - pos;
    783  1.1      cjs 			if (notdone <= bsz) {
    784  1.1      cjs 				outlen = notdone;
    785  1.1      cjs 			} else {
    786  1.1      cjs 				outlen = bsz;
    787  1.1      cjs 			}
    788  1.4    lukem 			memset(&buf[len], 0, outlen);
    789  1.1      cjs 			len = len + outlen;
    790  1.1      cjs 			pos = pos + outlen;
    791  1.1      cjs 			bsz = bsz - outlen;
    792  1.1      cjs 		}
    793  1.1      cjs 
    794  1.1      cjs 		total = total + dlslot->a_bss_fill;
    795  1.1      cjs 
    796  1.1      cjs 		if ((bsz > 0) && (pos < total)) {
    797  1.1      cjs 			notdone = total - pos;
    798  1.1      cjs 			if (notdone <= bsz) {
    799  1.1      cjs 				outlen = notdone;
    800  1.1      cjs 			} else {
    801  1.1      cjs 				outlen = bsz;
    802  1.1      cjs 			}
    803  1.4    lukem 			memset(&buf[len], 0, outlen);
    804  1.1      cjs 			len = len + outlen;
    805  1.1      cjs 			pos = pos + outlen;
    806  1.1      cjs 			bsz = bsz - outlen;
    807  1.1      cjs 		}
    808  1.1      cjs 
    809  1.1      cjs 		dlslot->a_lseek = pos;
    810  1.1      cjs 
    811  1.1      cjs 	}
    812  1.1      cjs 
    813  1.1      cjs 	return(len);
    814  1.1      cjs }
    815