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