Home | History | Annotate | Line # | Download | only in setnetimage
setnetimage.c revision 1.3
      1  1.3     cgd /*	$NetBSD: setnetimage.c,v 1.3 2001/02/19 22:48:58 cgd Exp $	*/
      2  1.1  simonb 
      3  1.1  simonb /*-
      4  1.1  simonb  * Copyright (c) 1999 The NetBSD Foundation, Inc.
      5  1.1  simonb  * All rights reserved.
      6  1.1  simonb  *
      7  1.1  simonb  * This code is derived from software contributed to The NetBSD Foundation
      8  1.1  simonb  * by Simon Burge.
      9  1.1  simonb  *
     10  1.1  simonb  * Redistribution and use in source and binary forms, with or without
     11  1.1  simonb  * modification, are permitted provided that the following conditions
     12  1.1  simonb  * are met:
     13  1.1  simonb  * 1. Redistributions of source code must retain the above copyright
     14  1.1  simonb  *    notice, this list of conditions and the following disclaimer.
     15  1.1  simonb  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.1  simonb  *    notice, this list of conditions and the following disclaimer in the
     17  1.1  simonb  *    documentation and/or other materials provided with the distribution.
     18  1.1  simonb  * 3. All advertising materials mentioning features or use of this software
     19  1.1  simonb  *    must display the following acknowledgement:
     20  1.1  simonb  *        This product includes software developed by the NetBSD
     21  1.1  simonb  *        Foundation, Inc. and its contributors.
     22  1.1  simonb  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  1.1  simonb  *    contributors may be used to endorse or promote products derived
     24  1.1  simonb  *    from this software without specific prior written permission.
     25  1.1  simonb  *
     26  1.1  simonb  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  1.1  simonb  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  1.1  simonb  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  1.1  simonb  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  1.1  simonb  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  1.1  simonb  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  1.1  simonb  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  1.1  simonb  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  1.1  simonb  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  1.1  simonb  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  1.1  simonb  * POSSIBILITY OF SUCH DAMAGE.
     37  1.1  simonb  */
     38  1.1  simonb 
     39  1.1  simonb #include <sys/types.h>
     40  1.1  simonb #include <sys/mman.h>
     41  1.1  simonb #include <sys/stat.h>
     42  1.1  simonb #include <sys/exec_elf.h>
     43  1.1  simonb 
     44  1.1  simonb #include <err.h>
     45  1.1  simonb #include <fcntl.h>
     46  1.1  simonb #include <nlist.h>
     47  1.1  simonb #include <limits.h>
     48  1.1  simonb #include <stdio.h>
     49  1.1  simonb #include <stdlib.h>
     50  1.1  simonb #include <unistd.h>
     51  1.1  simonb #include <zlib.h>
     52  1.1  simonb 
     53  1.1  simonb #include "extern.h"
     54  1.1  simonb 
     55  1.1  simonb #define MAX_SEGMENTS		10	/* We can load up to 10 segments */
     56  1.1  simonb 
     57  1.1  simonb struct nlist nl[] = {
     58  1.1  simonb #define	X_KERNEL_ENTRY		0
     59  1.1  simonb 	{ "_kernel_entry" },
     60  1.1  simonb #define	X_KERNEL_IMAGE		1
     61  1.1  simonb 	{ "_kernel_image" },
     62  1.1  simonb #define	X_KERNEL_LOADADDR	2
     63  1.1  simonb 	{ "_kernel_loadaddr" },
     64  1.1  simonb #define	X_KERNEL_SIZE		3
     65  1.1  simonb 	{ "_kernel_size" },
     66  1.1  simonb #define	X_MAXKERNEL_SIZE	4
     67  1.1  simonb 	{ "_maxkernel_size" },
     68  1.1  simonb 	{ NULL }
     69  1.1  simonb };
     70  1.1  simonb #define X_NSYMS			((sizeof(nl) / sizeof(struct nlist)) - 1)
     71  1.1  simonb 
     72  1.1  simonb struct seglist {
     73  1.1  simonb 	Elf32_Addr addr;
     74  1.1  simonb 	Elf32_Off f_offset;
     75  1.1  simonb 	Elf32_Word f_size;
     76  1.1  simonb };
     77  1.1  simonb 
     78  1.1  simonb #define NLADDR(x)	(mappedbfile + offsets[(x)])
     79  1.1  simonb #define NLVAR(x)	(*(u_long *)(NLADDR(x)))
     80  1.1  simonb 
     81  1.1  simonb int main __P((int, char **));
     82  1.1  simonb 
     83  1.1  simonb int
     84  1.1  simonb main(argc, argv)
     85  1.1  simonb 	int argc;
     86  1.1  simonb 	char **argv;
     87  1.1  simonb {
     88  1.1  simonb 	int ifd, ofd, i, nsegs;
     89  1.1  simonb 	size_t offsets[X_NSYMS];
     90  1.1  simonb 	const char *kernel, *bootfile;
     91  1.1  simonb 	char *mappedbfile;
     92  1.1  simonb 	char *uncomp_kernel, *comp_kernel;
     93  1.1  simonb 	Elf32_Addr lowaddr, highaddr;
     94  1.1  simonb 	Elf32_Ehdr ehdr;
     95  1.1  simonb 	Elf32_Phdr phdr;
     96  1.1  simonb 	uLongf destlen;
     97  1.1  simonb 	struct stat osb;
     98  1.1  simonb 	struct seglist seglist[MAX_SEGMENTS];
     99  1.1  simonb 
    100  1.1  simonb 	if (argc != 3) {
    101  1.3     cgd 		fprintf(stderr, "usage: %s kernel bootfile\n", getprogname());
    102  1.1  simonb 		exit(1);
    103  1.1  simonb 	}
    104  1.1  simonb 
    105  1.1  simonb 	kernel = argv[1];
    106  1.1  simonb 	bootfile = argv[2];
    107  1.1  simonb 
    108  1.1  simonb 	if ((ifd = open(kernel, O_RDONLY)) < 0)
    109  1.1  simonb 		err(1, "%s", kernel);
    110  1.1  simonb 
    111  1.1  simonb 	if ((ofd = open(bootfile, O_RDWR)) < 0)
    112  1.1  simonb 		err(1, "%s", bootfile);
    113  1.1  simonb 
    114  1.1  simonb 	if (nlist(bootfile, nl) != 0)
    115  1.1  simonb 		errx(1, "Could not find symbols in %s", bootfile);
    116  1.1  simonb 
    117  1.1  simonb 	if (fstat(ofd, &osb) == -1)
    118  1.1  simonb 		err(1, "fstat %s", bootfile);
    119  1.1  simonb 	if (osb.st_size > SIZE_T_MAX)
    120  1.1  simonb 		errx(1, "%s too big to map", bootfile);
    121  1.1  simonb 
    122  1.1  simonb 	if ((mappedbfile = mmap(NULL, osb.st_size, PROT_READ | PROT_WRITE,
    123  1.1  simonb 	    MAP_FILE | MAP_SHARED, ofd, 0)) == (caddr_t)-1)
    124  1.1  simonb 		err(1, "mmap %s", bootfile);
    125  1.1  simonb 	printf("mapped %s\n", bootfile);
    126  1.1  simonb 
    127  1.1  simonb 	if (check_elf32(mappedbfile, osb.st_size) != 0)
    128  1.1  simonb 		errx(1, "No ELF header in %s", bootfile);
    129  1.1  simonb 
    130  1.1  simonb 	for (i = 0; i < X_NSYMS; i++) {
    131  1.1  simonb 		if (findoff_elf32(mappedbfile, osb.st_size, nl[i].n_value, &offsets[i]) != 0)
    132  1.1  simonb 			errx(1, "Couldn't find offset for %s in %s\n", nl[i].n_name, bootfile);
    133  1.1  simonb #ifdef DEBUG
    134  1.1  simonb 		printf("%s is at offset %#x in %s\n", nl[i].n_name, offsets[i], bootfile);
    135  1.1  simonb #endif
    136  1.1  simonb 	}
    137  1.1  simonb 
    138  1.1  simonb 	/* read the exec header */
    139  1.1  simonb 	i = read(ifd, (char *)&ehdr, sizeof(ehdr));
    140  1.1  simonb 	if ((i != sizeof(ehdr)) ||
    141  1.2  kleink 	    (memcmp(ehdr.e_ident, ELFMAG, SELFMAG) != 0) ||
    142  1.2  kleink 	    (ehdr.e_ident[EI_CLASS] != ELFCLASS32)) {
    143  1.1  simonb 		errx(1, "No ELF header in %s", kernel);
    144  1.1  simonb 	}
    145  1.1  simonb 
    146  1.1  simonb 	nsegs = highaddr = 0;
    147  1.1  simonb 	lowaddr = UINT_MAX;
    148  1.1  simonb 	for (i = 0; i < ehdr.e_phnum; i++) {
    149  1.1  simonb 		if (lseek(ifd, (off_t) ehdr.e_phoff + i * sizeof(phdr), 0) < 0)
    150  1.1  simonb 			err(1, "%s", kernel);
    151  1.1  simonb 		if (read(ifd, &phdr, sizeof(phdr)) != sizeof(phdr))
    152  1.1  simonb 			err(1, "%s", kernel);
    153  1.2  kleink 		if (phdr.p_type != PT_LOAD)
    154  1.1  simonb 			continue;
    155  1.1  simonb 
    156  1.1  simonb 		printf("load section %d at addr 0x%08x, file offset %8d, length %8d\n",
    157  1.1  simonb 		    i, phdr.p_paddr, phdr.p_offset, phdr.p_filesz);
    158  1.1  simonb 
    159  1.1  simonb 		seglist[nsegs].addr = phdr.p_paddr;
    160  1.1  simonb 		seglist[nsegs].f_offset = phdr.p_offset;
    161  1.1  simonb 		seglist[nsegs].f_size = phdr.p_filesz;
    162  1.1  simonb 		nsegs++;
    163  1.1  simonb 
    164  1.1  simonb 		if (phdr.p_paddr < lowaddr)
    165  1.1  simonb 			lowaddr = phdr.p_paddr;
    166  1.1  simonb 		if (phdr.p_paddr + phdr.p_filesz > highaddr)
    167  1.1  simonb 			highaddr = phdr.p_paddr + phdr.p_filesz;
    168  1.1  simonb 
    169  1.1  simonb 	}
    170  1.1  simonb 
    171  1.1  simonb #ifdef DEBUG
    172  1.1  simonb 	printf("lowaddr =  0x%08x, highaddr = 0x%08x\n", lowaddr, highaddr);
    173  1.1  simonb #endif
    174  1.1  simonb 	printf("load address: 0x%08x\n", lowaddr);
    175  1.1  simonb 	printf("entry point:  0x%08x\n", ehdr.e_entry);
    176  1.1  simonb 
    177  1.1  simonb 	destlen = highaddr - lowaddr;
    178  1.1  simonb 
    179  1.1  simonb 	uncomp_kernel = (char *)malloc(destlen);
    180  1.1  simonb 	comp_kernel = (char *)malloc(destlen);		/* Worst case... */
    181  1.1  simonb 	for (i = 0; i < nsegs; i++) {
    182  1.1  simonb #ifdef DEBUG
    183  1.1  simonb 		printf("lseek(ifd, %d, 0)\n", seglist[i].f_offset);
    184  1.1  simonb #endif
    185  1.1  simonb 		if (lseek(ifd, (off_t)seglist[i].f_offset, 0) < 0)
    186  1.1  simonb 			err(1, "%s", kernel);
    187  1.1  simonb #ifdef DEBUG
    188  1.1  simonb 		printf("read(ifd, %p, %d)\n", \
    189  1.1  simonb 		    uncomp_kernel + seglist[i].addr - lowaddr,
    190  1.1  simonb 		    seglist[i].f_size);
    191  1.1  simonb #endif
    192  1.1  simonb 		if (read(ifd, uncomp_kernel + seglist[i].addr - lowaddr,
    193  1.1  simonb 		    seglist[i].f_size) != seglist[i].f_size)
    194  1.1  simonb 			err(1, "%s", kernel);
    195  1.1  simonb 	}
    196  1.1  simonb 	close(ifd);
    197  1.1  simonb 
    198  1.1  simonb 	printf("Compressing %d bytes...", highaddr - lowaddr); fflush(stdout);
    199  1.1  simonb 	i = compress2(comp_kernel, &destlen, uncomp_kernel, \
    200  1.1  simonb 	    highaddr - lowaddr, Z_BEST_COMPRESSION);
    201  1.1  simonb 	if (i != Z_OK) {
    202  1.1  simonb 		printf("\n");
    203  1.1  simonb 		errx(1, "%s compression error %d\n", kernel, i);
    204  1.1  simonb 	}
    205  1.1  simonb 	printf("done.\n"); fflush(stdout);
    206  1.1  simonb 
    207  1.1  simonb 	printf("max kernelsize = %ld\n", NLVAR(X_MAXKERNEL_SIZE));
    208  1.1  simonb 	printf("compressed size = %ld\n", destlen);
    209  1.1  simonb 	if (destlen > NLVAR(X_MAXKERNEL_SIZE))
    210  1.1  simonb 		errx(1, "kernel %s is too big, "
    211  1.1  simonb 		    "increase KERNELSIZE to at least %ld\n",
    212  1.1  simonb 		    kernel, destlen);
    213  1.1  simonb 
    214  1.1  simonb 	NLVAR(X_KERNEL_SIZE) = destlen;
    215  1.1  simonb 	NLVAR(X_KERNEL_LOADADDR) = lowaddr;
    216  1.1  simonb 	NLVAR(X_KERNEL_ENTRY) = ehdr.e_entry;
    217  1.1  simonb 	memcpy(NLADDR(X_KERNEL_IMAGE), comp_kernel, destlen);
    218  1.1  simonb 	munmap(mappedbfile, osb.st_size);
    219  1.1  simonb 	printf("unmapped %s\n", bootfile);
    220  1.1  simonb 	close(ofd);
    221  1.1  simonb 
    222  1.1  simonb 	exit(0);
    223  1.1  simonb }
    224