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