1995-04-15 08:18:20 +00:00
|
|
|
/*
|
|
|
|
* ----------------------------------------------------------------------------
|
|
|
|
* "THE BEER-WARE LICENSE" (Revision 42):
|
|
|
|
* <phk@login.dknet.dk> wrote this file. As long as you retain this notice you
|
|
|
|
* can do whatever you want with this stuff. If we meet some day, and you think
|
|
|
|
* this stuff is worth it, you can buy me a beer in return. Poul-Henning Kamp
|
|
|
|
* ----------------------------------------------------------------------------
|
|
|
|
*
|
|
|
|
* Copyright (C) 1993 Hannu Savolainen
|
|
|
|
* Ported to 386bsd by Serge Vakulenko
|
|
|
|
* based on tools/build.c by Linus Torvalds
|
|
|
|
*/
|
|
|
|
|
1997-07-18 06:40:46 +00:00
|
|
|
#ifndef lint
|
|
|
|
static const char rcsid[] =
|
1998-06-01 18:34:19 +00:00
|
|
|
"$Id: kzip.c,v 1.10 1997/07/18 06:40:46 charnier Exp $";
|
1997-07-18 06:40:46 +00:00
|
|
|
#endif /* not lint */
|
|
|
|
|
|
|
|
#include <err.h>
|
1995-04-15 08:18:20 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/file.h>
|
|
|
|
#include <sys/types.h>
|
1995-10-06 02:42:15 +00:00
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/param.h>
|
1995-04-15 08:18:20 +00:00
|
|
|
#include <sys/wait.h>
|
|
|
|
#include <a.out.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
1995-04-25 05:27:04 +00:00
|
|
|
#define MAXIMAGE (2*1024*1024)
|
|
|
|
/* This is the limit because a kzip'ed kernel loads at 3Mb and
|
|
|
|
* ends up at 1Mb
|
|
|
|
*/
|
1997-07-18 06:40:46 +00:00
|
|
|
static void
|
|
|
|
usage()
|
1995-10-06 02:42:15 +00:00
|
|
|
{
|
1997-07-18 06:40:46 +00:00
|
|
|
fprintf(stderr, "usage: kzip [-v] [ -l loadaddr] kernel\n");
|
1995-10-06 02:42:15 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
1995-04-25 05:27:04 +00:00
|
|
|
|
1995-04-15 08:18:20 +00:00
|
|
|
int
|
|
|
|
main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
pid_t Pext, Pgzip, Ppiggy, Pld;
|
|
|
|
int pipe1[2], pipe2[2];
|
1995-10-06 02:42:15 +00:00
|
|
|
int status, fdi, fdo, fdn, c, verbose;
|
|
|
|
int size;
|
|
|
|
struct exec hdr;
|
|
|
|
int zip_size, offset;
|
|
|
|
struct stat st;
|
1997-07-18 06:40:46 +00:00
|
|
|
u_long forceaddr = 0, entry;
|
1995-10-06 02:42:15 +00:00
|
|
|
char *kernname;
|
1995-04-15 08:18:20 +00:00
|
|
|
char obj[BUFSIZ];
|
|
|
|
char out[BUFSIZ];
|
1995-10-06 02:42:15 +00:00
|
|
|
char base[32];
|
|
|
|
|
1997-03-29 04:34:07 +00:00
|
|
|
while ((c = getopt(argc, argv, "l:v")) != -1) {
|
1995-10-06 02:42:15 +00:00
|
|
|
switch (c) {
|
|
|
|
case 'l':
|
|
|
|
forceaddr = strtoul(optarg, NULL, 0);
|
1997-07-18 06:40:46 +00:00
|
|
|
if (forceaddr == 0)
|
|
|
|
errx(1, "invalid load address");
|
1995-10-06 02:42:15 +00:00
|
|
|
break;
|
|
|
|
case 'v':
|
|
|
|
verbose++;
|
|
|
|
break;
|
|
|
|
default:
|
1997-07-18 06:40:46 +00:00
|
|
|
usage();
|
1995-10-06 02:42:15 +00:00
|
|
|
}
|
1995-04-15 08:18:20 +00:00
|
|
|
}
|
|
|
|
|
1995-10-06 02:42:15 +00:00
|
|
|
if ((argc - optind) != 1)
|
1997-07-18 06:40:46 +00:00
|
|
|
usage();
|
1995-10-06 02:42:15 +00:00
|
|
|
|
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
|
|
|
|
|
|
|
kernname = argv[0];
|
|
|
|
|
|
|
|
strcpy(obj, kernname); strcat(obj,".o");
|
|
|
|
strcpy(out, kernname); strcat(out,".kz");
|
1995-04-15 08:18:20 +00:00
|
|
|
|
1995-10-06 02:42:15 +00:00
|
|
|
fdi = open(kernname ,O_RDONLY);
|
1995-04-15 08:18:20 +00:00
|
|
|
if(fdi<0) {
|
1997-07-18 06:40:46 +00:00
|
|
|
warn(kernname);
|
1995-04-15 08:18:20 +00:00
|
|
|
return 2;
|
|
|
|
}
|
1995-10-06 02:42:15 +00:00
|
|
|
|
|
|
|
/* figure out how big the uncompressed image will be */
|
1997-07-18 06:40:46 +00:00
|
|
|
if (read (fdi, (char *)&hdr, sizeof(hdr)) != sizeof(hdr))
|
|
|
|
err(2, argv[1]);
|
1995-10-06 02:42:15 +00:00
|
|
|
|
|
|
|
size = hdr.a_text + hdr.a_data + hdr.a_bss;
|
1997-06-24 15:56:41 +00:00
|
|
|
entry = hdr.a_entry & 0x00FFFFFF;
|
1995-10-06 02:42:15 +00:00
|
|
|
|
|
|
|
lseek (fdi, 0, SEEK_SET);
|
|
|
|
|
|
|
|
if (verbose) {
|
|
|
|
printf("real kernel start address will be: 0x%x\n", entry);
|
|
|
|
printf("real kernel end address will be: 0x%x\n", entry+size);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1995-04-15 08:18:20 +00:00
|
|
|
fdo = open(obj,O_WRONLY|O_TRUNC|O_CREAT,0666);
|
|
|
|
if(fdo<0) {
|
1997-07-18 06:40:46 +00:00
|
|
|
warn(obj);
|
1995-04-15 08:18:20 +00:00
|
|
|
return 2;
|
|
|
|
}
|
1995-05-30 06:41:30 +00:00
|
|
|
|
1995-04-15 08:18:20 +00:00
|
|
|
if (pipe(pipe1) < 0) { perror("pipe()"); return 1; }
|
|
|
|
|
|
|
|
if (pipe(pipe2) < 0) { perror("pipe()"); return 1; }
|
|
|
|
|
|
|
|
Pext = fork();
|
|
|
|
if (Pext < 0) { perror("fork()"); return 1; }
|
|
|
|
if (!Pext) {
|
|
|
|
dup2(fdi,0);
|
|
|
|
dup2(pipe1[1],1);
|
|
|
|
close(pipe1[0]); close(pipe1[1]);
|
|
|
|
close(pipe2[0]); close(pipe2[1]);
|
|
|
|
close(fdi); close(fdo);
|
1995-10-06 02:42:15 +00:00
|
|
|
extract(kernname);
|
1995-04-15 08:18:20 +00:00
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
Pgzip = fork();
|
|
|
|
if (Pgzip < 0) { perror("fork()"); return 1; }
|
|
|
|
if (!Pgzip) {
|
|
|
|
dup2(pipe1[0],0);
|
|
|
|
dup2(pipe2[1],1);
|
|
|
|
close(pipe1[0]); close(pipe1[1]);
|
|
|
|
close(pipe2[0]); close(pipe2[1]);
|
|
|
|
close(fdi); close(fdo);
|
|
|
|
execlp("gzip", "gzip", "-9", "-n", 0);
|
|
|
|
exit (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
Ppiggy = fork();
|
1997-07-18 06:40:46 +00:00
|
|
|
if (Ppiggy < 0) { warn("fork()"); return 1; }
|
1995-04-15 08:18:20 +00:00
|
|
|
if (!Ppiggy) {
|
|
|
|
dup2(pipe2[0],0);
|
|
|
|
dup2(fdo,1);
|
|
|
|
close(pipe1[0]); close(pipe1[1]);
|
|
|
|
close(pipe2[0]); close(pipe2[1]);
|
|
|
|
close(fdi); close(fdo);
|
|
|
|
piggyback(obj);
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
close(pipe1[0]); close(pipe1[1]);
|
|
|
|
close(pipe2[0]); close(pipe2[1]);
|
|
|
|
close(fdi); close(fdo);
|
|
|
|
|
1995-05-30 06:41:30 +00:00
|
|
|
if (waitpid(Pext, &status,0) < 0)
|
1997-07-18 06:40:46 +00:00
|
|
|
{ warn("waitpid(Pextract)"); return 1; }
|
1995-04-15 08:18:20 +00:00
|
|
|
|
|
|
|
if(status) {
|
1997-07-18 06:40:46 +00:00
|
|
|
warnx("extract returned %x",status);
|
1995-04-15 08:18:20 +00:00
|
|
|
return 3;
|
|
|
|
}
|
|
|
|
|
1995-05-30 06:41:30 +00:00
|
|
|
if (waitpid(Pgzip, &status,0) < 0)
|
1995-04-15 08:18:20 +00:00
|
|
|
{ perror("waitpid(Pgzip)"); return 1; }
|
1995-05-30 06:41:30 +00:00
|
|
|
|
1995-04-15 08:18:20 +00:00
|
|
|
if(status) {
|
1997-07-18 06:40:46 +00:00
|
|
|
warnx("gzip returned %x",status);
|
1995-04-15 08:18:20 +00:00
|
|
|
return 3;
|
|
|
|
}
|
|
|
|
|
1995-05-30 06:41:30 +00:00
|
|
|
if (waitpid(Ppiggy, &status,0) < 0)
|
1997-07-18 06:40:46 +00:00
|
|
|
{ warn("waitpid(Ppiggy)"); return 1; }
|
1995-04-15 08:18:20 +00:00
|
|
|
|
|
|
|
if(status) {
|
1997-07-18 06:40:46 +00:00
|
|
|
warnx("piggyback returned %x",status);
|
1995-04-15 08:18:20 +00:00
|
|
|
return 3;
|
|
|
|
}
|
|
|
|
|
1995-10-06 02:42:15 +00:00
|
|
|
if (forceaddr)
|
|
|
|
offset = forceaddr;
|
|
|
|
else {
|
|
|
|
/* a kludge to dynamically figure out where to start it */
|
|
|
|
if (stat (obj, &st) < 0) {
|
1997-07-18 06:40:46 +00:00
|
|
|
warn("cannot get size of compressed data");
|
1995-10-06 02:42:15 +00:00
|
|
|
return 3;
|
|
|
|
}
|
|
|
|
zip_size = (int)st.st_size;
|
|
|
|
offset = entry + size - zip_size + 0x8000; /* fudge factor */
|
|
|
|
}
|
|
|
|
sprintf(base, "0x%x", roundup(offset, 4096));
|
|
|
|
|
1995-04-15 08:18:20 +00:00
|
|
|
Pld = fork();
|
1997-07-18 06:40:46 +00:00
|
|
|
if (Pld < 0) { warn("fork()"); return 1; }
|
1995-04-15 08:18:20 +00:00
|
|
|
if (!Pld) {
|
|
|
|
execlp("ld",
|
|
|
|
"ld",
|
|
|
|
"-Bstatic",
|
|
|
|
"-Z",
|
|
|
|
"-T",
|
1995-10-06 02:42:15 +00:00
|
|
|
base,
|
1995-04-15 08:18:20 +00:00
|
|
|
"-o",
|
|
|
|
out,
|
1998-06-01 18:34:19 +00:00
|
|
|
"/usr/lib/aout/kzhead.o",
|
1995-04-15 08:18:20 +00:00
|
|
|
obj,
|
1998-06-01 18:34:19 +00:00
|
|
|
"/usr/lib/aout/kztail.o",
|
1995-04-15 08:18:20 +00:00
|
|
|
0);
|
|
|
|
exit(2);
|
|
|
|
}
|
|
|
|
|
1995-05-30 06:41:30 +00:00
|
|
|
if (waitpid(Pld, &status,0) < 0)
|
1997-07-18 06:40:46 +00:00
|
|
|
{ warn("waitpid(Pld)"); return 1; }
|
1995-04-15 08:18:20 +00:00
|
|
|
|
|
|
|
if(status) {
|
1997-07-18 06:40:46 +00:00
|
|
|
warnx("ld returned %x",status);
|
1995-04-15 08:18:20 +00:00
|
|
|
return 3;
|
|
|
|
}
|
|
|
|
|
1995-10-06 02:42:15 +00:00
|
|
|
if (verbose) {
|
|
|
|
|
|
|
|
fdn = open(obj ,O_RDONLY);
|
|
|
|
if(fdn<0) {
|
1997-07-18 06:40:46 +00:00
|
|
|
warn(obj);
|
1995-10-06 02:42:15 +00:00
|
|
|
return 3;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* figure out how big the compressed image is */
|
|
|
|
if (read (fdn, (char *)&hdr, sizeof(hdr)) != sizeof(hdr)) {
|
1997-07-18 06:40:46 +00:00
|
|
|
warn(obj);
|
1995-10-06 02:42:15 +00:00
|
|
|
return 3;
|
|
|
|
}
|
|
|
|
close(fdn);
|
|
|
|
|
|
|
|
size = hdr.a_text + hdr.a_data + hdr.a_bss;
|
|
|
|
|
|
|
|
printf("kzip data start address will be: 0x%x\n",offset);
|
|
|
|
printf("kzip data end address will be: 0x%x\n",offset+size);
|
|
|
|
}
|
|
|
|
|
1995-04-15 08:18:20 +00:00
|
|
|
unlink(obj);
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
1995-05-30 06:41:30 +00:00
|
|
|
int
|
1995-04-15 08:18:20 +00:00
|
|
|
extract (char *file)
|
|
|
|
{
|
|
|
|
int sz;
|
|
|
|
char buf[BUFSIZ];
|
|
|
|
struct exec hdr;
|
|
|
|
|
1997-07-18 06:40:46 +00:00
|
|
|
if (read (0, (char *)&hdr, sizeof(hdr)) != sizeof(hdr))
|
|
|
|
err(2, file);
|
|
|
|
if (hdr.a_magic != ZMAGIC)
|
|
|
|
errx(2, "bad magic in file %s, probably not a kernel", file);
|
|
|
|
if (lseek (0, N_TXTOFF(hdr), 0) < 0)
|
|
|
|
err(2, file);
|
1995-04-15 08:18:20 +00:00
|
|
|
|
|
|
|
sz = N_SYMOFF (hdr) - N_TXTOFF (hdr);
|
|
|
|
|
|
|
|
while (sz) {
|
|
|
|
int l, n;
|
|
|
|
|
|
|
|
l = sz;
|
|
|
|
if (l > sizeof(buf))
|
|
|
|
l = sizeof(buf);
|
|
|
|
|
|
|
|
n = read (0, buf, l);
|
|
|
|
if (n != l) {
|
1995-05-30 06:41:30 +00:00
|
|
|
if (n == -1)
|
1997-07-18 06:40:46 +00:00
|
|
|
err(1, file);
|
1995-04-15 08:18:20 +00:00
|
|
|
else
|
1997-07-18 06:40:46 +00:00
|
|
|
errx(1, "unexpected EOF");
|
1995-04-15 08:18:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
write (1, buf, l);
|
|
|
|
sz -= l;
|
|
|
|
}
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
char string_names[] = {"_input_data\0_input_len\0"};
|
|
|
|
|
|
|
|
struct nlist var_names[2] = { /* Symbol table */
|
|
|
|
{ { (char*) 4 }, N_EXT|N_TEXT, 0, 0, 0 }, /* _input_data */
|
|
|
|
{ { (char*) 16 }, N_EXT|N_TEXT, 0, 0, 0 }, /* _input_len */
|
|
|
|
};
|
|
|
|
|
1995-05-30 06:41:30 +00:00
|
|
|
int
|
1995-04-15 08:18:20 +00:00
|
|
|
piggyback(char *file)
|
|
|
|
{
|
|
|
|
int n, len;
|
|
|
|
struct exec hdr; /* object header */
|
1995-04-25 05:27:04 +00:00
|
|
|
char image[MAXIMAGE]; /* kernel image buffer */
|
1995-04-15 08:18:20 +00:00
|
|
|
|
|
|
|
len = 0;
|
|
|
|
while ((n = read (0, &image[len], sizeof(image)-len+1)) > 0)
|
|
|
|
len += n;
|
|
|
|
|
1997-07-18 06:40:46 +00:00
|
|
|
if (n < 0)
|
|
|
|
err(1, "stdin");
|
1995-04-15 08:18:20 +00:00
|
|
|
|
1997-07-18 06:40:46 +00:00
|
|
|
if (len >= sizeof(image))
|
|
|
|
errx(1, "input too large");
|
1995-04-15 08:18:20 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Output object header
|
|
|
|
*/
|
|
|
|
memset(&hdr,0,sizeof hdr);
|
|
|
|
hdr.a_magic = OMAGIC;
|
|
|
|
hdr.a_text = len + sizeof(long);
|
|
|
|
hdr.a_syms = sizeof(var_names);
|
|
|
|
write (1, (char *)&hdr, sizeof(hdr));
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Output text segment (compressed system & len)
|
|
|
|
*/
|
|
|
|
write (1, image, len);
|
|
|
|
write (1, (char *)&len, sizeof(len));
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Output symbol table
|
|
|
|
*/
|
|
|
|
var_names[1].n_value = len;
|
|
|
|
write (1, (char *)&var_names, sizeof(var_names));
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Output string table
|
|
|
|
*/
|
|
|
|
len = sizeof(string_names) + sizeof(len);
|
|
|
|
write (1, (char *)&len, sizeof(len));
|
|
|
|
write (1, string_names, sizeof(string_names));
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|