setnetimage.c revision 1.1 1 /* $NetBSD: setnetimage.c,v 1.1 1999/05/13 08:38:05 simonb 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, Elf32_e_ident, Elf32_e_siz) != 0)) {
144 errx(1, "No ELF header in %s", kernel);
145 }
146
147 nsegs = highaddr = 0;
148 lowaddr = UINT_MAX;
149 for (i = 0; i < ehdr.e_phnum; i++) {
150 if (lseek(ifd, (off_t) ehdr.e_phoff + i * sizeof(phdr), 0) < 0)
151 err(1, "%s", kernel);
152 if (read(ifd, &phdr, sizeof(phdr)) != sizeof(phdr))
153 err(1, "%s", kernel);
154 if (phdr.p_type != Elf_pt_load)
155 continue;
156
157 printf("load section %d at addr 0x%08x, file offset %8d, length %8d\n",
158 i, phdr.p_paddr, phdr.p_offset, phdr.p_filesz);
159
160 seglist[nsegs].addr = phdr.p_paddr;
161 seglist[nsegs].f_offset = phdr.p_offset;
162 seglist[nsegs].f_size = phdr.p_filesz;
163 nsegs++;
164
165 if (phdr.p_paddr < lowaddr)
166 lowaddr = phdr.p_paddr;
167 if (phdr.p_paddr + phdr.p_filesz > highaddr)
168 highaddr = phdr.p_paddr + phdr.p_filesz;
169
170 }
171
172 #ifdef DEBUG
173 printf("lowaddr = 0x%08x, highaddr = 0x%08x\n", lowaddr, highaddr);
174 #endif
175 printf("load address: 0x%08x\n", lowaddr);
176 printf("entry point: 0x%08x\n", ehdr.e_entry);
177
178 destlen = highaddr - lowaddr;
179
180 uncomp_kernel = (char *)malloc(destlen);
181 comp_kernel = (char *)malloc(destlen); /* Worst case... */
182 for (i = 0; i < nsegs; i++) {
183 #ifdef DEBUG
184 printf("lseek(ifd, %d, 0)\n", seglist[i].f_offset);
185 #endif
186 if (lseek(ifd, (off_t)seglist[i].f_offset, 0) < 0)
187 err(1, "%s", kernel);
188 #ifdef DEBUG
189 printf("read(ifd, %p, %d)\n", \
190 uncomp_kernel + seglist[i].addr - lowaddr,
191 seglist[i].f_size);
192 #endif
193 if (read(ifd, uncomp_kernel + seglist[i].addr - lowaddr,
194 seglist[i].f_size) != seglist[i].f_size)
195 err(1, "%s", kernel);
196 }
197 close(ifd);
198
199 printf("Compressing %d bytes...", highaddr - lowaddr); fflush(stdout);
200 i = compress2(comp_kernel, &destlen, uncomp_kernel, \
201 highaddr - lowaddr, Z_BEST_COMPRESSION);
202 if (i != Z_OK) {
203 printf("\n");
204 errx(1, "%s compression error %d\n", kernel, i);
205 }
206 printf("done.\n"); fflush(stdout);
207
208 printf("max kernelsize = %ld\n", NLVAR(X_MAXKERNEL_SIZE));
209 printf("compressed size = %ld\n", destlen);
210 if (destlen > NLVAR(X_MAXKERNEL_SIZE))
211 errx(1, "kernel %s is too big, "
212 "increase KERNELSIZE to at least %ld\n",
213 kernel, destlen);
214
215 NLVAR(X_KERNEL_SIZE) = destlen;
216 NLVAR(X_KERNEL_LOADADDR) = lowaddr;
217 NLVAR(X_KERNEL_ENTRY) = ehdr.e_entry;
218 memcpy(NLADDR(X_KERNEL_IMAGE), comp_kernel, destlen);
219 munmap(mappedbfile, osb.st_size);
220 printf("unmapped %s\n", bootfile);
221 close(ofd);
222
223 exit(0);
224 }
225