Home | History | Annotate | Line # | Download | only in bootcoff
      1  1.2  msaitoh /*-
      2  1.2  msaitoh  * Copyright (C) 1999 T.Horiuchi(Brains, Corp.) and SAITOH Masanobu.
      3  1.2  msaitoh  * All rights reserved.
      4  1.2  msaitoh  *
      5  1.2  msaitoh  * Redistribution and use in source and binary forms, with or without
      6  1.2  msaitoh  * modification, are permitted provided that the following conditions
      7  1.2  msaitoh  * are met:
      8  1.2  msaitoh  * 1. Redistributions of source code must retain the above copyright
      9  1.2  msaitoh  *    notice, this list of conditions and the following disclaimer.
     10  1.2  msaitoh  * 2. Redistributions in binary form must reproduce the above copyright
     11  1.2  msaitoh  *    notice, this list of conditions and the following disclaimer in the
     12  1.2  msaitoh  *    documentation and/or other materials provided with the distribution.
     13  1.2  msaitoh  * 3. The name of the author may not be used to endorse or promote products
     14  1.2  msaitoh  *    derived from this software without specific prior written permission.
     15  1.2  msaitoh  *
     16  1.2  msaitoh  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     17  1.2  msaitoh  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     18  1.2  msaitoh  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     19  1.2  msaitoh  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     20  1.2  msaitoh  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     21  1.2  msaitoh  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     22  1.2  msaitoh  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     23  1.2  msaitoh  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     24  1.2  msaitoh  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     25  1.2  msaitoh  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     26  1.2  msaitoh  */
     27  1.1  msaitoh 
     28  1.1  msaitoh #include <stdio.h>
     29  1.1  msaitoh #include <stdlib.h>
     30  1.2  msaitoh #include <sys/exec_coff.h>
     31  1.1  msaitoh #include <sys/param.h>
     32  1.1  msaitoh #include <sys/gmon.h>
     33  1.1  msaitoh #include <sys/stat.h>
     34  1.1  msaitoh #include <sys/sysctl.h>
     35  1.1  msaitoh #include <sys/socket.h>
     36  1.1  msaitoh #include <sys/file.h>
     37  1.4      mrg #include <uvm/uvm_param.h>
     38  1.1  msaitoh #include <machine/cpu.h>
     39  1.1  msaitoh 
     40  1.2  msaitoh #define vm_page_size (NBPG)
     41  1.1  msaitoh 
     42  1.1  msaitoh char *netbsd = "/netbsd";
     43  1.1  msaitoh struct coff_filehdr FileHdr;
     44  1.1  msaitoh struct coff_aouthdr AoutHdr;
     45  1.1  msaitoh 
     46  1.5      dsl static int coff_find_section(FILE *, struct coff_filehdr *,
     47  1.5      dsl 			     struct coff_scnhdr *, int);
     48  1.1  msaitoh 
     49  1.5      dsl void	LoadAndReset(char *);
     50  1.5      dsl int	main(int, char **);
     51  1.1  msaitoh 
     52  1.1  msaitoh static int
     53  1.6      dsl coff_find_section(FILE *fd, struct coff_filehdr *fp, struct coff_scnhdr *sh, int s_type)
     54  1.1  msaitoh {
     55  1.1  msaitoh 	int i, pos, siz;
     56  1.1  msaitoh 
     57  1.1  msaitoh 	pos = COFF_HDR_SIZE;
     58  1.1  msaitoh 	for (i = 0; i < fp->f_nscns; i++, pos += sizeof(struct coff_scnhdr)) {
     59  1.1  msaitoh 		siz = sizeof(struct coff_scnhdr);
     60  1.2  msaitoh 		if (fread(sh, 1, siz, fd) != siz) {
     61  1.1  msaitoh 			perror("osloader");
     62  1.2  msaitoh 			exit(1);
     63  1.1  msaitoh 		}
     64  1.1  msaitoh 
     65  1.1  msaitoh 		if (sh->s_flags == s_type)
     66  1.2  msaitoh 			return (0);
     67  1.1  msaitoh 	}
     68  1.2  msaitoh 	return (-1);
     69  1.1  msaitoh }
     70  1.1  msaitoh 
     71  1.1  msaitoh void
     72  1.6      dsl LoadAndReset(char *osimage)
     73  1.1  msaitoh {
     74  1.1  msaitoh 	int mib[2];
     75  1.1  msaitoh 	u_long val;
     76  1.1  msaitoh 	int len;
     77  1.1  msaitoh 
     78  1.1  msaitoh 	mib[0] = CTL_MACHDEP;
     79  1.1  msaitoh 	mib[1] = CPU_LOADANDRESET;
     80  1.1  msaitoh 	val = (u_long)osimage;
     81  1.2  msaitoh 	len = sizeof(val);
     82  1.1  msaitoh 
     83  1.1  msaitoh 	sysctl(mib, 2, NULL, NULL, &val, len);
     84  1.1  msaitoh }
     85  1.1  msaitoh 
     86  1.1  msaitoh int
     87  1.7      dsl main(int argc, char *argv[])
     88  1.1  msaitoh {
     89  1.1  msaitoh 	FILE *fp;
     90  1.1  msaitoh 	int error;
     91  1.1  msaitoh 	long dsize;
     92  1.1  msaitoh 	struct coff_scnhdr sh;
     93  1.1  msaitoh 	u_long ep_taddr;
     94  1.1  msaitoh 	u_long ep_tsize;
     95  1.1  msaitoh 	u_long toffset;
     96  1.1  msaitoh 	u_long ep_daddr;
     97  1.1  msaitoh 	u_long ep_dsize;
     98  1.1  msaitoh 	u_long doffset;
     99  1.1  msaitoh 	char *osimage;
    100  1.1  msaitoh 	char *p;
    101  1.1  msaitoh 	int i;
    102  1.1  msaitoh 	u_long cksum;
    103  1.1  msaitoh 	u_long size;
    104  1.1  msaitoh 
    105  1.2  msaitoh 	fp = fopen(netbsd, "r");
    106  1.2  msaitoh 	if (fp == NULL) {
    107  1.1  msaitoh 		perror("osloader");
    108  1.2  msaitoh 		exit(1);
    109  1.1  msaitoh 	}
    110  1.1  msaitoh 
    111  1.2  msaitoh 	if (fread(&FileHdr, 1, sizeof(FileHdr), fp) != sizeof(FileHdr)) {
    112  1.1  msaitoh 		perror("osloader");
    113  1.2  msaitoh 		exit(1);
    114  1.1  msaitoh 	}
    115  1.1  msaitoh 
    116  1.2  msaitoh 	if (fread(&AoutHdr, 1, sizeof(AoutHdr), fp) != sizeof(AoutHdr)) {
    117  1.1  msaitoh 		perror("osloader");
    118  1.2  msaitoh 		exit(1);
    119  1.1  msaitoh 	}
    120  1.1  msaitoh 
    121  1.1  msaitoh 	/* set up command for text segment */
    122  1.1  msaitoh 	error = coff_find_section(fp, &FileHdr, &sh, COFF_STYP_TEXT);
    123  1.1  msaitoh 	if (error) {
    124  1.1  msaitoh 		printf("can't find text section: %d\n", error);
    125  1.2  msaitoh 		exit(1);
    126  1.1  msaitoh 	}
    127  1.1  msaitoh 
    128  1.1  msaitoh 	ep_taddr = COFF_ALIGN(sh.s_vaddr);
    129  1.1  msaitoh 	toffset = sh.s_scnptr - (sh.s_vaddr - ep_taddr);
    130  1.1  msaitoh 	ep_tsize = sh.s_size + (sh.s_vaddr - ep_taddr);
    131  1.1  msaitoh 
    132  1.3  msaitoh 	printf("addr %lx size 0x%lx offset 0x%lx\n", ep_taddr,
    133  1.1  msaitoh 	       ep_tsize, toffset);
    134  1.1  msaitoh 
    135  1.1  msaitoh 	/* set up command for data segment */
    136  1.1  msaitoh 	error = coff_find_section(fp, &FileHdr, &sh, COFF_STYP_DATA);
    137  1.1  msaitoh 	if (error) {
    138  1.1  msaitoh 		printf("can't find data section: %d\n", error);
    139  1.2  msaitoh 		exit(1);
    140  1.1  msaitoh 	}
    141  1.1  msaitoh 
    142  1.1  msaitoh 	ep_daddr = COFF_ALIGN(sh.s_vaddr);
    143  1.1  msaitoh 	doffset = sh.s_scnptr - (sh.s_vaddr - ep_daddr);
    144  1.1  msaitoh 	dsize = sh.s_size + (sh.s_vaddr - ep_daddr);
    145  1.1  msaitoh 	ep_dsize = round_page(dsize) + AoutHdr.a_bsize;
    146  1.1  msaitoh 
    147  1.3  msaitoh 	printf("addr 0x%lx size 0x%lx offset 0x%lx\n", ep_daddr,
    148  1.1  msaitoh 		 dsize, doffset);
    149  1.1  msaitoh 
    150  1.3  msaitoh 	osimage = malloc(ep_tsize+dsize+sizeof(u_long) * 2);
    151  1.2  msaitoh 	if (osimage == NULL) {
    152  1.1  msaitoh 		printf("osloader:no memory\n");
    153  1.2  msaitoh 		exit(1);
    154  1.1  msaitoh 	}
    155  1.1  msaitoh 
    156  1.1  msaitoh 	*(u_long *)osimage = ep_tsize+dsize;
    157  1.3  msaitoh 	p = osimage + 2 * sizeof(u_long);
    158  1.1  msaitoh 
    159  1.1  msaitoh 	/* load text area */
    160  1.2  msaitoh 	fseek(fp, toffset, SEEK_SET);
    161  1.2  msaitoh 	if (fread(p, 1, ep_tsize, fp) != ep_tsize) {
    162  1.1  msaitoh 		perror("osloader:");
    163  1.2  msaitoh 		exit(1);
    164  1.1  msaitoh 	}
    165  1.1  msaitoh 
    166  1.1  msaitoh 	/* load data area */
    167  1.2  msaitoh 	fseek(fp, doffset, SEEK_SET);
    168  1.3  msaitoh 	if (fread(p + ep_daddr - ep_taddr, 1, dsize, fp) != dsize) {
    169  1.1  msaitoh 		perror("osloader:");
    170  1.2  msaitoh 		exit(1);
    171  1.1  msaitoh 	}
    172  1.1  msaitoh 
    173  1.2  msaitoh 	fclose(fp);
    174  1.1  msaitoh 
    175  1.1  msaitoh 	cksum = 0;
    176  1.1  msaitoh 	size = (ep_tsize + dsize) >> 2;
    177  1.1  msaitoh 
    178  1.2  msaitoh 	for(i = 0; i < size; i++) {
    179  1.1  msaitoh 		cksum += *(u_long *)p;
    180  1.2  msaitoh 		p += sizeof(u_long);
    181  1.1  msaitoh 	}
    182  1.1  msaitoh 
    183  1.2  msaitoh 	*(u_long *)(osimage+sizeof(u_long)) = cksum;
    184  1.1  msaitoh 
    185  1.2  msaitoh 	LoadAndReset(osimage);
    186  1.1  msaitoh 
    187  1.3  msaitoh 	return (0);	/* NOTREACHED */
    188  1.1  msaitoh }
    189