2005-04-16 22:20:36 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 1991, 1992 Linus Torvalds
|
|
|
|
* Copyright (C) 1997 Martin Mares
|
2007-07-11 19:18:56 +00:00
|
|
|
* Copyright (C) 2007 H. Peter Anvin
|
2005-04-16 22:20:36 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This file builds a disk-image from three different files:
|
|
|
|
*
|
|
|
|
* - setup: 8086 machine code, sets up system parm
|
|
|
|
* - system: 80386 code for actual system
|
|
|
|
*
|
|
|
|
* It does some checking that all files are of the correct type, and
|
|
|
|
* just writes the result to stdout, removing headers and padding to
|
|
|
|
* the right amount. It also writes some system data to stderr.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Changes by tytso to allow root device specification
|
|
|
|
* High loaded stuff by Hans Lermen & Werner Almesberger, Feb. 1996
|
|
|
|
* Cross compiling fixes by Gertjan van Wingerde, July 1996
|
|
|
|
* Rewritten by Martin Mares, April 1997
|
2007-07-11 19:18:56 +00:00
|
|
|
* Substantially overhauled by H. Peter Anvin, April 2007
|
2005-04-16 22:20:36 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/sysmacros.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <fcntl.h>
|
2007-07-11 19:18:56 +00:00
|
|
|
#include <sys/mman.h>
|
2005-04-16 22:20:36 +00:00
|
|
|
#include <asm/boot.h>
|
|
|
|
|
2007-07-11 19:18:56 +00:00
|
|
|
typedef unsigned char u8;
|
|
|
|
typedef unsigned short u16;
|
|
|
|
typedef unsigned long u32;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
#define DEFAULT_MAJOR_ROOT 0
|
|
|
|
#define DEFAULT_MINOR_ROOT 0
|
|
|
|
|
2007-07-11 19:18:56 +00:00
|
|
|
/* Minimal number of setup sectors */
|
|
|
|
#define SETUP_SECT_MIN 5
|
|
|
|
#define SETUP_SECT_MAX 64
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2007-07-11 19:18:56 +00:00
|
|
|
/* This must be large enough to hold the entire setup */
|
|
|
|
u8 buf[SETUP_SECT_MAX*512];
|
2005-04-16 22:20:36 +00:00
|
|
|
int is_big_kernel;
|
|
|
|
|
2007-07-11 19:18:56 +00:00
|
|
|
static void die(const char * str, ...)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
|
|
|
va_list args;
|
|
|
|
va_start(args, str);
|
|
|
|
vfprintf(stderr, str, args);
|
|
|
|
fputc('\n', stderr);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2007-07-11 19:18:56 +00:00
|
|
|
static void usage(void)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
2007-07-11 19:18:56 +00:00
|
|
|
die("Usage: build [-b] setup system [rootdev] [> image]");
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char ** argv)
|
|
|
|
{
|
2005-06-29 03:45:06 +00:00
|
|
|
unsigned int i, sz, setup_sectors;
|
|
|
|
int c;
|
2005-04-16 22:20:36 +00:00
|
|
|
u32 sys_size;
|
2007-07-11 19:18:56 +00:00
|
|
|
u8 major_root, minor_root;
|
2005-04-16 22:20:36 +00:00
|
|
|
struct stat sb;
|
2007-07-11 19:18:56 +00:00
|
|
|
FILE *file;
|
|
|
|
int fd;
|
|
|
|
void *kernel;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
if (argc > 2 && !strcmp(argv[1], "-b"))
|
|
|
|
{
|
|
|
|
is_big_kernel = 1;
|
|
|
|
argc--, argv++;
|
|
|
|
}
|
2007-07-11 19:18:56 +00:00
|
|
|
if ((argc < 3) || (argc > 4))
|
2005-04-16 22:20:36 +00:00
|
|
|
usage();
|
2007-07-11 19:18:56 +00:00
|
|
|
if (argc > 3) {
|
|
|
|
if (!strcmp(argv[3], "CURRENT")) {
|
2005-04-16 22:20:36 +00:00
|
|
|
if (stat("/", &sb)) {
|
|
|
|
perror("/");
|
|
|
|
die("Couldn't stat /");
|
|
|
|
}
|
|
|
|
major_root = major(sb.st_dev);
|
|
|
|
minor_root = minor(sb.st_dev);
|
2007-07-11 19:18:56 +00:00
|
|
|
} else if (strcmp(argv[3], "FLOPPY")) {
|
|
|
|
if (stat(argv[3], &sb)) {
|
|
|
|
perror(argv[3]);
|
2005-04-16 22:20:36 +00:00
|
|
|
die("Couldn't stat root device.");
|
|
|
|
}
|
|
|
|
major_root = major(sb.st_rdev);
|
|
|
|
minor_root = minor(sb.st_rdev);
|
|
|
|
} else {
|
|
|
|
major_root = 0;
|
|
|
|
minor_root = 0;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
major_root = DEFAULT_MAJOR_ROOT;
|
|
|
|
minor_root = DEFAULT_MINOR_ROOT;
|
|
|
|
}
|
|
|
|
fprintf(stderr, "Root device is (%d, %d)\n", major_root, minor_root);
|
|
|
|
|
2007-07-11 19:18:56 +00:00
|
|
|
/* Copy the setup code */
|
|
|
|
file = fopen(argv[1], "r");
|
|
|
|
if (!file)
|
|
|
|
die("Unable to open `%s': %m", argv[1]);
|
|
|
|
c = fread(buf, 1, sizeof(buf), file);
|
|
|
|
if (ferror(file))
|
|
|
|
die("read-error on `setup'");
|
|
|
|
if (c < 1024)
|
|
|
|
die("The setup must be at least 1024 bytes");
|
2005-04-16 22:20:36 +00:00
|
|
|
if (buf[510] != 0x55 || buf[511] != 0xaa)
|
|
|
|
die("Boot block hasn't got boot flag (0xAA55)");
|
2007-07-11 19:18:56 +00:00
|
|
|
fclose(file);
|
|
|
|
|
|
|
|
/* Pad unused space with zeros */
|
|
|
|
setup_sectors = (c + 511) / 512;
|
|
|
|
if (setup_sectors < SETUP_SECT_MIN)
|
|
|
|
setup_sectors = SETUP_SECT_MIN;
|
|
|
|
i = setup_sectors*512;
|
|
|
|
memset(buf+c, 0, i-c);
|
|
|
|
|
|
|
|
/* Set the default root device */
|
2005-04-16 22:20:36 +00:00
|
|
|
buf[508] = minor_root;
|
|
|
|
buf[509] = major_root;
|
|
|
|
|
2007-07-11 19:18:56 +00:00
|
|
|
fprintf(stderr, "Setup is %d bytes (padded to %d bytes).\n", c, i);
|
|
|
|
|
|
|
|
/* Open and stat the kernel file */
|
|
|
|
fd = open(argv[2], O_RDONLY);
|
|
|
|
if (fd < 0)
|
|
|
|
die("Unable to open `%s': %m", argv[2]);
|
|
|
|
if (fstat(fd, &sb))
|
|
|
|
die("Unable to stat `%s': %m", argv[2]);
|
2005-04-16 22:20:36 +00:00
|
|
|
sz = sb.st_size;
|
2007-07-11 19:18:56 +00:00
|
|
|
fprintf (stderr, "System is %d kB\n", (sz+1023)/1024);
|
|
|
|
kernel = mmap(NULL, sz, PROT_READ, MAP_SHARED, fd, 0);
|
|
|
|
if (kernel == MAP_FAILED)
|
|
|
|
die("Unable to mmap '%s': %m", argv[2]);
|
2005-04-16 22:20:36 +00:00
|
|
|
sys_size = (sz + 15) / 16;
|
|
|
|
if (!is_big_kernel && sys_size > DEF_SYSSIZE)
|
|
|
|
die("System is too big. Try using bzImage or modules.");
|
2007-07-11 19:18:56 +00:00
|
|
|
|
|
|
|
/* Patch the setup code with the appropriate size parameters */
|
|
|
|
buf[0x1f1] = setup_sectors-1;
|
|
|
|
buf[0x1f4] = sys_size;
|
|
|
|
buf[0x1f5] = sys_size >> 8;
|
|
|
|
buf[0x1f6] = sys_size >> 16;
|
|
|
|
buf[0x1f7] = sys_size >> 24;
|
|
|
|
|
|
|
|
if (fwrite(buf, 1, i, stdout) != i)
|
|
|
|
die("Writing setup failed");
|
|
|
|
|
|
|
|
/* Copy the kernel code */
|
|
|
|
if (fwrite(kernel, 1, sz, stdout) != sz)
|
|
|
|
die("Writing kernel failed");
|
2005-04-16 22:20:36 +00:00
|
|
|
close(fd);
|
|
|
|
|
2007-07-11 19:18:56 +00:00
|
|
|
/* Everything is OK */
|
|
|
|
return 0;
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|