1 /* $NetBSD: setnetimage.c,v 1.4 2002/07/20 08:36:19 grant 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 int main __P((int, char **)); 82 83 int 84 main(argc, argv) 85 int argc; 86 char **argv; 87 { 88 int ifd, ofd, i, nsegs; 89 size_t offsets[X_NSYMS]; 90 const char *kernel, *bootfile; 91 char *mappedbfile; 92 char *uncomp_kernel, *comp_kernel; 93 Elf32_Addr lowaddr, highaddr; 94 Elf32_Ehdr ehdr; 95 Elf32_Phdr phdr; 96 uLongf destlen; 97 struct stat osb; 98 struct seglist seglist[MAX_SEGMENTS]; 99 100 if (argc != 3) { 101 fprintf(stderr, "usage: %s kernel bootfile\n", getprogname()); 102 exit(1); 103 } 104 105 kernel = argv[1]; 106 bootfile = argv[2]; 107 108 if ((ifd = open(kernel, O_RDONLY)) < 0) 109 err(1, "%s", kernel); 110 111 if ((ofd = open(bootfile, O_RDWR)) < 0) 112 err(1, "%s", bootfile); 113 114 if (nlist(bootfile, nl) != 0) 115 errx(1, "Could not find symbols in %s", bootfile); 116 117 if (fstat(ofd, &osb) == -1) 118 err(1, "fstat %s", bootfile); 119 if (osb.st_size > SIZE_T_MAX) 120 errx(1, "%s too big to map", bootfile); 121 122 if ((mappedbfile = mmap(NULL, osb.st_size, PROT_READ | PROT_WRITE, 123 MAP_FILE | MAP_SHARED, ofd, 0)) == (caddr_t)-1) 124 err(1, "mmap %s", bootfile); 125 printf("mapped %s\n", bootfile); 126 127 if (check_elf32(mappedbfile, osb.st_size) != 0) 128 errx(1, "No ELF header in %s", bootfile); 129 130 for (i = 0; i < X_NSYMS; i++) { 131 if (findoff_elf32(mappedbfile, osb.st_size, nl[i].n_value, &offsets[i]) != 0) 132 errx(1, "Couldn't find offset for %s in %s", nl[i].n_name, bootfile); 133 #ifdef DEBUG 134 printf("%s is at offset %#x in %s\n", nl[i].n_name, offsets[i], bootfile); 135 #endif 136 } 137 138 /* read the exec header */ 139 i = read(ifd, (char *)&ehdr, sizeof(ehdr)); 140 if ((i != sizeof(ehdr)) || 141 (memcmp(ehdr.e_ident, ELFMAG, SELFMAG) != 0) || 142 (ehdr.e_ident[EI_CLASS] != ELFCLASS32)) { 143 errx(1, "No ELF header in %s", kernel); 144 } 145 146 nsegs = highaddr = 0; 147 lowaddr = UINT_MAX; 148 for (i = 0; i < ehdr.e_phnum; i++) { 149 if (lseek(ifd, (off_t) ehdr.e_phoff + i * sizeof(phdr), 0) < 0) 150 err(1, "%s", kernel); 151 if (read(ifd, &phdr, sizeof(phdr)) != sizeof(phdr)) 152 err(1, "%s", kernel); 153 if (phdr.p_type != PT_LOAD) 154 continue; 155 156 printf("load section %d at addr 0x%08x, file offset %8d, length %8d\n", 157 i, phdr.p_paddr, phdr.p_offset, phdr.p_filesz); 158 159 seglist[nsegs].addr = phdr.p_paddr; 160 seglist[nsegs].f_offset = phdr.p_offset; 161 seglist[nsegs].f_size = phdr.p_filesz; 162 nsegs++; 163 164 if (phdr.p_paddr < lowaddr) 165 lowaddr = phdr.p_paddr; 166 if (phdr.p_paddr + phdr.p_filesz > highaddr) 167 highaddr = phdr.p_paddr + phdr.p_filesz; 168 169 } 170 171 #ifdef DEBUG 172 printf("lowaddr = 0x%08x, highaddr = 0x%08x\n", lowaddr, highaddr); 173 #endif 174 printf("load address: 0x%08x\n", lowaddr); 175 printf("entry point: 0x%08x\n", ehdr.e_entry); 176 177 destlen = highaddr - lowaddr; 178 179 uncomp_kernel = (char *)malloc(destlen); 180 comp_kernel = (char *)malloc(destlen); /* Worst case... */ 181 for (i = 0; i < nsegs; i++) { 182 #ifdef DEBUG 183 printf("lseek(ifd, %d, 0)\n", seglist[i].f_offset); 184 #endif 185 if (lseek(ifd, (off_t)seglist[i].f_offset, 0) < 0) 186 err(1, "%s", kernel); 187 #ifdef DEBUG 188 printf("read(ifd, %p, %d)\n", \ 189 uncomp_kernel + seglist[i].addr - lowaddr, 190 seglist[i].f_size); 191 #endif 192 if (read(ifd, uncomp_kernel + seglist[i].addr - lowaddr, 193 seglist[i].f_size) != seglist[i].f_size) 194 err(1, "%s", kernel); 195 } 196 close(ifd); 197 198 printf("Compressing %d bytes...", highaddr - lowaddr); fflush(stdout); 199 i = compress2(comp_kernel, &destlen, uncomp_kernel, \ 200 highaddr - lowaddr, Z_BEST_COMPRESSION); 201 if (i != Z_OK) { 202 printf("\n"); 203 errx(1, "%s compression error %d", kernel, i); 204 } 205 printf("done.\n"); fflush(stdout); 206 207 printf("max kernelsize = %ld\n", NLVAR(X_MAXKERNEL_SIZE)); 208 printf("compressed size = %ld\n", destlen); 209 if (destlen > NLVAR(X_MAXKERNEL_SIZE)) 210 errx(1, "kernel %s is too big, " 211 "increase KERNELSIZE to at least %ld", 212 kernel, destlen); 213 214 NLVAR(X_KERNEL_SIZE) = destlen; 215 NLVAR(X_KERNEL_LOADADDR) = lowaddr; 216 NLVAR(X_KERNEL_ENTRY) = ehdr.e_entry; 217 memcpy(NLADDR(X_KERNEL_IMAGE), comp_kernel, destlen); 218 munmap(mappedbfile, osb.st_size); 219 printf("unmapped %s\n", bootfile); 220 close(ofd); 221 222 exit(0); 223 } 224