1999-07-19 18:00:18 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 1999 Global Technology Associates, Inc.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
|
|
|
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS
|
|
|
|
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
|
|
|
|
* OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
|
|
|
|
* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
|
|
|
|
* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
|
|
|
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
|
|
|
|
* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
|
|
|
|
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef lint
|
|
|
|
static const char rcsid[] =
|
1999-08-28 01:35:59 +00:00
|
|
|
"$FreeBSD$";
|
1999-07-19 18:00:18 +00:00
|
|
|
#endif /* not lint */
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <err.h>
|
2000-08-03 23:44:43 +00:00
|
|
|
#include <paths.h>
|
1999-07-19 18:00:18 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include "kgzip.h"
|
|
|
|
|
|
|
|
#define FN_SRC 0 /* Filename: source */
|
|
|
|
#define FN_OBJ 1 /* Filename: relocatable */
|
|
|
|
#define FN_KGZ 2 /* Filename: executable */
|
|
|
|
#define FN_CNT 3 /* Number of filenames */
|
|
|
|
|
|
|
|
#define SFX_OBJ ".o" /* Filename suffix: relocatable */
|
|
|
|
#define SFX_KGZ ".kgz" /* Filename suffix: executable */
|
|
|
|
#define SFX_MAX 5 /* Size of larger filename suffix */
|
|
|
|
|
|
|
|
const char *loader = "/usr/lib/kgzldr.o"; /* Default loader */
|
2000-11-04 13:02:00 +00:00
|
|
|
int format; /* Output format */
|
1999-07-19 18:00:18 +00:00
|
|
|
|
2000-01-16 21:19:04 +00:00
|
|
|
char *tname; /* Name of temporary file */
|
1999-07-19 18:00:18 +00:00
|
|
|
|
|
|
|
static void cleanup(void);
|
|
|
|
static void mk_fn(int, const char *, const char *, char *[]);
|
|
|
|
static void usage(void);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Compress a kernel.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
static char *fn[FN_CNT];
|
|
|
|
struct kgz_hdr kh;
|
|
|
|
const char *output;
|
2000-08-03 23:44:43 +00:00
|
|
|
char *tmpdir;
|
1999-07-19 18:00:18 +00:00
|
|
|
int cflag, vflag, c;
|
|
|
|
|
2000-08-03 23:44:43 +00:00
|
|
|
tmpdir = getenv("TMPDIR");
|
2000-11-20 20:35:35 +00:00
|
|
|
if (asprintf(&tname, "%s/kgzXXXXXXXXXX",
|
|
|
|
tmpdir == NULL ? _PATH_TMP : tmpdir) == -1)
|
2000-11-04 13:02:00 +00:00
|
|
|
errx(1, "Out of memory");
|
1999-07-19 18:00:18 +00:00
|
|
|
output = NULL;
|
|
|
|
cflag = vflag = 0;
|
2000-11-04 13:02:00 +00:00
|
|
|
while ((c = getopt(argc, argv, "cvf:l:o:")) != -1)
|
1999-07-19 18:00:18 +00:00
|
|
|
switch (c) {
|
|
|
|
case 'c':
|
|
|
|
cflag = 1;
|
|
|
|
break;
|
|
|
|
case 'v':
|
|
|
|
vflag = 1;
|
|
|
|
break;
|
2000-11-04 13:02:00 +00:00
|
|
|
case 'f':
|
|
|
|
if (!strcmp(optarg, "aout"))
|
|
|
|
format = F_AOUT;
|
|
|
|
else if (!strcmp(optarg, "elf"))
|
|
|
|
format = F_ELF;
|
|
|
|
else
|
|
|
|
errx(1, "%s: Unknown format", optarg);
|
|
|
|
break;
|
1999-07-19 18:00:18 +00:00
|
|
|
case 'l':
|
|
|
|
loader = optarg;
|
|
|
|
break;
|
|
|
|
case 'o':
|
|
|
|
output = optarg;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
}
|
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
|
|
|
if (argc != 1)
|
|
|
|
usage();
|
|
|
|
atexit(cleanup);
|
|
|
|
mk_fn(cflag, *argv, output, fn);
|
|
|
|
memset(&kh, 0, sizeof(kh));
|
2000-11-04 13:02:00 +00:00
|
|
|
if (fn[FN_SRC]) {
|
|
|
|
if (!format)
|
|
|
|
format = F_ELF;
|
1999-07-19 18:00:18 +00:00
|
|
|
kgzcmp(&kh, fn[FN_SRC], fn[FN_OBJ]);
|
2000-11-04 13:02:00 +00:00
|
|
|
}
|
1999-07-19 18:00:18 +00:00
|
|
|
if (!cflag)
|
|
|
|
kgzld(&kh, fn[FN_OBJ], fn[FN_KGZ]);
|
|
|
|
if (vflag)
|
|
|
|
printf("dload=%#x dsize=%#x isize=%#x entry=%#x nsize=%#x\n",
|
|
|
|
kh.dload, kh.dsize, kh.isize, kh.entry, kh.nsize);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Clean up after processing.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
cleanup(void)
|
|
|
|
{
|
|
|
|
if (tname)
|
|
|
|
unlink(tname);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Make the required filenames.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
mk_fn(int cflag, const char *f1, const char *f2, char *fn[])
|
|
|
|
{
|
|
|
|
const char *p, *s;
|
|
|
|
size_t n;
|
2000-01-16 21:19:04 +00:00
|
|
|
int i, fd;
|
1999-07-19 18:00:18 +00:00
|
|
|
|
|
|
|
i = 0;
|
|
|
|
s = strrchr(f1, 0);
|
|
|
|
n = sizeof(SFX_OBJ) - 1;
|
|
|
|
if ((size_t)(s - f1) > n && !memcmp(s - n, SFX_OBJ, n)) {
|
|
|
|
s -= n;
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
fn[i++] = (char *)f1;
|
|
|
|
if (i == FN_OBJ && !cflag) {
|
2000-01-16 21:19:04 +00:00
|
|
|
if ((fd = mkstemp(tname)) == -1)
|
1999-07-19 18:00:18 +00:00
|
|
|
err(1, NULL);
|
2000-01-16 21:19:04 +00:00
|
|
|
close(fd);
|
1999-07-19 18:00:18 +00:00
|
|
|
fn[i++] = (char *)tname;
|
|
|
|
}
|
|
|
|
if (!(fn[i] = (char *)f2)) {
|
|
|
|
p = (p = strrchr(f1, '/')) ? p + 1 : f1;
|
|
|
|
n = (size_t)(s - p);
|
|
|
|
if (!(fn[i] = malloc(n + SFX_MAX)))
|
|
|
|
err(1, NULL);
|
|
|
|
memcpy(fn[i], p, n);
|
|
|
|
strcpy(fn[i] + n, i == FN_OBJ ? SFX_OBJ : SFX_KGZ);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Display usage information.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
usage(void)
|
|
|
|
{
|
|
|
|
fprintf(stderr,
|
2000-11-04 13:02:00 +00:00
|
|
|
"usage: kgzip [-cv] [-f format] [-l file] [-o filename] file\n");
|
1999-07-19 18:00:18 +00:00
|
|
|
exit(1);
|
|
|
|
}
|