blob: b4248740ff0da355ee3ca44eb99b70d4532af773 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * Copyright (C) 1991, 1992 Linus Torvalds
3 * Copyright (C) 1997 Martin Mares
H. Peter Anvin4fd06962007-07-11 12:18:56 -07004 * Copyright (C) 2007 H. Peter Anvin
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 */
6
7/*
H. Peter Anvin9aa39092007-07-13 16:28:27 -07008 * This file builds a disk-image from two different files:
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 * - setup: 8086 machine code, sets up system parm
11 * - system: 80386 code for actual system
12 *
13 * It does some checking that all files are of the correct type, and
14 * just writes the result to stdout, removing headers and padding to
15 * the right amount. It also writes some system data to stderr.
16 */
17
18/*
19 * Changes by tytso to allow root device specification
20 * High loaded stuff by Hans Lermen & Werner Almesberger, Feb. 1996
21 * Cross compiling fixes by Gertjan van Wingerde, July 1996
22 * Rewritten by Martin Mares, April 1997
H. Peter Anvin4fd06962007-07-11 12:18:56 -070023 * Substantially overhauled by H. Peter Anvin, April 2007
Linus Torvalds1da177e2005-04-16 15:20:36 -070024 */
25
26#include <stdio.h>
27#include <string.h>
28#include <stdlib.h>
29#include <stdarg.h>
30#include <sys/types.h>
31#include <sys/stat.h>
32#include <sys/sysmacros.h>
33#include <unistd.h>
34#include <fcntl.h>
H. Peter Anvin4fd06962007-07-11 12:18:56 -070035#include <sys/mman.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <asm/boot.h>
37
H. Peter Anvin4fd06962007-07-11 12:18:56 -070038typedef unsigned char u8;
39typedef unsigned short u16;
40typedef unsigned long u32;
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
42#define DEFAULT_MAJOR_ROOT 0
43#define DEFAULT_MINOR_ROOT 0
44
H. Peter Anvin4fd06962007-07-11 12:18:56 -070045/* Minimal number of setup sectors */
46#define SETUP_SECT_MIN 5
47#define SETUP_SECT_MAX 64
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
H. Peter Anvin4fd06962007-07-11 12:18:56 -070049/* This must be large enough to hold the entire setup */
50u8 buf[SETUP_SECT_MAX*512];
Linus Torvalds1da177e2005-04-16 15:20:36 -070051int is_big_kernel;
52
H. Peter Anvin4fd06962007-07-11 12:18:56 -070053static void die(const char * str, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -070054{
55 va_list args;
56 va_start(args, str);
57 vfprintf(stderr, str, args);
58 fputc('\n', stderr);
59 exit(1);
60}
61
H. Peter Anvin4fd06962007-07-11 12:18:56 -070062static void usage(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070063{
H. Peter Anvin4fd06962007-07-11 12:18:56 -070064 die("Usage: build [-b] setup system [rootdev] [> image]");
Linus Torvalds1da177e2005-04-16 15:20:36 -070065}
66
67int main(int argc, char ** argv)
68{
KAMBAROV, ZAURa8f50342005-06-28 20:45:06 -070069 unsigned int i, sz, setup_sectors;
70 int c;
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 u32 sys_size;
H. Peter Anvin4fd06962007-07-11 12:18:56 -070072 u8 major_root, minor_root;
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 struct stat sb;
H. Peter Anvin4fd06962007-07-11 12:18:56 -070074 FILE *file;
75 int fd;
76 void *kernel;
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
78 if (argc > 2 && !strcmp(argv[1], "-b"))
79 {
80 is_big_kernel = 1;
81 argc--, argv++;
82 }
H. Peter Anvin4fd06962007-07-11 12:18:56 -070083 if ((argc < 3) || (argc > 4))
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 usage();
H. Peter Anvin4fd06962007-07-11 12:18:56 -070085 if (argc > 3) {
86 if (!strcmp(argv[3], "CURRENT")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 if (stat("/", &sb)) {
88 perror("/");
89 die("Couldn't stat /");
90 }
91 major_root = major(sb.st_dev);
92 minor_root = minor(sb.st_dev);
H. Peter Anvin4fd06962007-07-11 12:18:56 -070093 } else if (strcmp(argv[3], "FLOPPY")) {
94 if (stat(argv[3], &sb)) {
95 perror(argv[3]);
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 die("Couldn't stat root device.");
97 }
98 major_root = major(sb.st_rdev);
99 minor_root = minor(sb.st_rdev);
100 } else {
101 major_root = 0;
102 minor_root = 0;
103 }
104 } else {
105 major_root = DEFAULT_MAJOR_ROOT;
106 minor_root = DEFAULT_MINOR_ROOT;
107 }
108 fprintf(stderr, "Root device is (%d, %d)\n", major_root, minor_root);
109
H. Peter Anvin4fd06962007-07-11 12:18:56 -0700110 /* Copy the setup code */
111 file = fopen(argv[1], "r");
112 if (!file)
113 die("Unable to open `%s': %m", argv[1]);
114 c = fread(buf, 1, sizeof(buf), file);
115 if (ferror(file))
116 die("read-error on `setup'");
117 if (c < 1024)
118 die("The setup must be at least 1024 bytes");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 if (buf[510] != 0x55 || buf[511] != 0xaa)
120 die("Boot block hasn't got boot flag (0xAA55)");
H. Peter Anvin4fd06962007-07-11 12:18:56 -0700121 fclose(file);
122
123 /* Pad unused space with zeros */
124 setup_sectors = (c + 511) / 512;
125 if (setup_sectors < SETUP_SECT_MIN)
126 setup_sectors = SETUP_SECT_MIN;
127 i = setup_sectors*512;
128 memset(buf+c, 0, i-c);
129
130 /* Set the default root device */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 buf[508] = minor_root;
132 buf[509] = major_root;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
H. Peter Anvin4fd06962007-07-11 12:18:56 -0700134 fprintf(stderr, "Setup is %d bytes (padded to %d bytes).\n", c, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135
H. Peter Anvin4fd06962007-07-11 12:18:56 -0700136 /* Open and stat the kernel file */
137 fd = open(argv[2], O_RDONLY);
138 if (fd < 0)
139 die("Unable to open `%s': %m", argv[2]);
140 if (fstat(fd, &sb))
141 die("Unable to stat `%s': %m", argv[2]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 sz = sb.st_size;
H. Peter Anvin4fd06962007-07-11 12:18:56 -0700143 fprintf (stderr, "System is %d kB\n", (sz+1023)/1024);
144 kernel = mmap(NULL, sz, PROT_READ, MAP_SHARED, fd, 0);
145 if (kernel == MAP_FAILED)
146 die("Unable to mmap '%s': %m", argv[2]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 sys_size = (sz + 15) / 16;
148 if (!is_big_kernel && sys_size > DEF_SYSSIZE)
149 die("System is too big. Try using bzImage or modules.");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150
H. Peter Anvin4fd06962007-07-11 12:18:56 -0700151 /* Patch the setup code with the appropriate size parameters */
152 buf[0x1f1] = setup_sectors-1;
153 buf[0x1f4] = sys_size;
154 buf[0x1f5] = sys_size >> 8;
155 buf[0x1f6] = sys_size >> 16;
156 buf[0x1f7] = sys_size >> 24;
157
158 if (fwrite(buf, 1, i, stdout) != i)
159 die("Writing setup failed");
160
161 /* Copy the kernel code */
162 if (fwrite(kernel, 1, sz, stdout) != sz)
163 die("Writing kernel failed");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 close(fd);
165
H. Peter Anvin4fd06962007-07-11 12:18:56 -0700166 /* Everything is OK */
167 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168}