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