2 * ----------------------------------------------------------------------------
3 * "THE BEER-WARE LICENSE" (Revision 42):
4 * <phk@login.dknet.dk> wrote this file. As long as you retain this notice you
5 * can do whatever you want with this stuff. If we meet some day, and you think
6 * this stuff is worth it, you can buy me a beer in return. Poul-Henning Kamp
7 * ----------------------------------------------------------------------------
9 * Copyright (C) 1993 Hannu Savolainen
10 * Ported to 386bsd by Serge Vakulenko
11 * based on tools/build.c by Linus Torvalds
13 * $FreeBSD: src/usr.bin/kzip/kzip.c,v 1.13.2.2 2000/07/20 10:35:20 kris Exp $
21 #include <sys/types.h>
23 #include <sys/param.h>
29 * This is the limit because a kzip'ed kernel loads at 3Mb and
32 #define MAXIMAGE (2*1024*1024)
34 static char string_names[] = {"_input_data\0_input_len\0"};
36 static struct nlist var_names[2] = { /* Symbol table */
37 { { (char*) 4 }, N_EXT|N_TEXT, 0, 0, 0 }, /* _input_data */
38 { { (char*) 16 }, N_EXT|N_TEXT, 0, 0, 0 }, /* _input_len */
48 if (read(0, (char *)&hdr, sizeof(hdr)) != sizeof(hdr))
50 if (hdr.a_magic != ZMAGIC)
51 errx(2, "bad magic in file %s, probably not a kernel", file);
52 if (lseek(0, N_TXTOFF(hdr), 0) < 0)
55 sz = N_SYMOFF(hdr) - N_TXTOFF(hdr);
70 errx(1, "unexpected EOF");
83 struct exec hdr; /* object header */
84 char image[MAXIMAGE]; /* kernel image buffer */
87 while ((n = read(0, &image[len], sizeof(image)-len+1)) > 0)
93 if (len >= sizeof(image))
94 errx(1, "input too large");
97 * Output object header
99 memset(&hdr,0,sizeof hdr);
100 hdr.a_magic = OMAGIC;
101 hdr.a_text = len + sizeof(long);
102 hdr.a_syms = sizeof(var_names);
103 write(1, (char *)&hdr, sizeof(hdr));
106 * Output text segment (compressed system & len)
108 write(1, image, len);
109 write(1, (char *)&len, sizeof(len));
112 * Output symbol table
114 var_names[1].n_value = len;
115 write(1, (char *)&var_names, sizeof(var_names));
118 * Output string table
120 len = sizeof(string_names) + sizeof(len);
121 write(1, (char *)&len, sizeof(len));
122 write(1, string_names, sizeof(string_names));
130 fprintf(stderr, "usage: kzip [-v] [ -l loadaddr] kernel\n");
135 main(int argc, char **argv)
137 pid_t Pext, Pgzip, Ppiggy, Pld;
138 int pipe1[2], pipe2[2];
139 int status, fdi, fdo, fdn, c, verbose;
142 int zip_size, offset;
144 u_long forceaddr = 0, entry;
146 char obj[MAXPATHLEN + 1];
147 char out[MAXPATHLEN + 1];
150 while ((c = getopt(argc, argv, "l:v")) != -1) {
153 forceaddr = strtoul(optarg, NULL, 0);
155 errx(1, "invalid load address");
165 if ((argc - optind) != 1)
173 if (strlen(kernname) > MAXPATHLEN - 3)
174 errx(1, "%s: File name too long", kernname);
175 strcpy(obj, kernname); strcat(obj,".o");
176 strcpy(out, kernname); strcat(out,".kz");
178 fdi = open(kernname ,O_RDONLY);
180 warn("%s", kernname);
184 /* figure out how big the uncompressed image will be */
185 if (read(fdi, (char *)&hdr, sizeof(hdr)) != sizeof(hdr))
186 err(2, "%s", argv[1]);
188 size = hdr.a_text + hdr.a_data + hdr.a_bss;
189 entry = hdr.a_entry & 0x00FFFFFF;
191 lseek(fdi, 0, SEEK_SET);
194 printf("real kernel start address will be: 0x%lx\n", entry);
195 printf("real kernel end address will be: 0x%lx\n", entry+size);
199 fdo = open(obj,O_WRONLY|O_TRUNC|O_CREAT,0666);
205 if (pipe(pipe1) < 0) { perror("pipe()"); return 1; }
207 if (pipe(pipe2) < 0) { perror("pipe()"); return 1; }
210 if (Pext < 0) { perror("fork()"); return 1; }
214 close(pipe1[0]); close(pipe1[1]);
215 close(pipe2[0]); close(pipe2[1]);
216 close(fdi); close(fdo);
222 if (Pgzip < 0) { perror("fork()"); return 1; }
226 close(pipe1[0]); close(pipe1[1]);
227 close(pipe2[0]); close(pipe2[1]);
228 close(fdi); close(fdo);
229 execlp("gzip", "gzip", "-9", "-n", NULL);
234 if (Ppiggy < 0) { warn("fork()"); return 1; }
238 close(pipe1[0]); close(pipe1[1]);
239 close(pipe2[0]); close(pipe2[1]);
240 close(fdi); close(fdo);
245 close(pipe1[0]); close(pipe1[1]);
246 close(pipe2[0]); close(pipe2[1]);
247 close(fdi); close(fdo);
249 if (waitpid(Pext, &status,0) < 0)
250 { warn("waitpid(Pextract)"); return 1; }
253 warnx("extract returned %x",status);
257 if (waitpid(Pgzip, &status,0) < 0)
258 { perror("waitpid(Pgzip)"); return 1; }
261 warnx("gzip returned %x",status);
265 if (waitpid(Ppiggy, &status,0) < 0)
266 { warn("waitpid(Ppiggy)"); return 1; }
269 warnx("piggyback returned %x",status);
276 /* a kludge to dynamically figure out where to start it */
277 if (stat(obj, &st) < 0) {
278 warn("cannot get size of compressed data");
281 zip_size = (int)st.st_size;
282 offset = entry + size - zip_size + 0x8000; /* fudge factor */
284 sprintf(base, "0x%x", roundup(offset, 4096));
287 if (Pld < 0) { warn("fork()"); return 1; }
298 "/usr/lib/aout/kzhead.o",
300 "/usr/lib/aout/kztail.o",
305 if (waitpid(Pld, &status,0) < 0)
306 { warn("waitpid(Pld)"); return 1; }
309 warnx("ld returned %x",status);
315 fdn = open(obj ,O_RDONLY);
321 /* figure out how big the compressed image is */
322 if (read(fdn, (char *)&hdr, sizeof(hdr)) != sizeof(hdr)) {
328 size = hdr.a_text + hdr.a_data + hdr.a_bss;
330 printf("kzip data start address will be: 0x%x\n",offset);
331 printf("kzip data end address will be: 0x%x\n",offset+size);