119d8d79cSThomas Gleixner /* 219d8d79cSThomas Gleixner * Copyright (C) 1991, 1992 Linus Torvalds 319d8d79cSThomas Gleixner * Copyright (C) 1997 Martin Mares 419d8d79cSThomas Gleixner * Copyright (C) 2007 H. Peter Anvin 519d8d79cSThomas Gleixner */ 619d8d79cSThomas Gleixner 719d8d79cSThomas Gleixner /* 819d8d79cSThomas Gleixner * This file builds a disk-image from two different files: 919d8d79cSThomas Gleixner * 1019d8d79cSThomas Gleixner * - setup: 8086 machine code, sets up system parm 1119d8d79cSThomas Gleixner * - system: 80386 code for actual system 1219d8d79cSThomas Gleixner * 1319d8d79cSThomas Gleixner * It does some checking that all files are of the correct type, and 1419d8d79cSThomas Gleixner * just writes the result to stdout, removing headers and padding to 1519d8d79cSThomas Gleixner * the right amount. It also writes some system data to stderr. 1619d8d79cSThomas Gleixner */ 1719d8d79cSThomas Gleixner 1819d8d79cSThomas Gleixner /* 1919d8d79cSThomas Gleixner * Changes by tytso to allow root device specification 2019d8d79cSThomas Gleixner * High loaded stuff by Hans Lermen & Werner Almesberger, Feb. 1996 2119d8d79cSThomas Gleixner * Cross compiling fixes by Gertjan van Wingerde, July 1996 2219d8d79cSThomas Gleixner * Rewritten by Martin Mares, April 1997 2319d8d79cSThomas Gleixner * Substantially overhauled by H. Peter Anvin, April 2007 2419d8d79cSThomas Gleixner */ 2519d8d79cSThomas Gleixner 2619d8d79cSThomas Gleixner #include <stdio.h> 2719d8d79cSThomas Gleixner #include <string.h> 2819d8d79cSThomas Gleixner #include <stdlib.h> 2919d8d79cSThomas Gleixner #include <stdarg.h> 3019d8d79cSThomas Gleixner #include <sys/types.h> 3119d8d79cSThomas Gleixner #include <sys/stat.h> 3219d8d79cSThomas Gleixner #include <sys/sysmacros.h> 3319d8d79cSThomas Gleixner #include <unistd.h> 3419d8d79cSThomas Gleixner #include <fcntl.h> 3519d8d79cSThomas Gleixner #include <sys/mman.h> 3619d8d79cSThomas Gleixner #include <asm/boot.h> 3719d8d79cSThomas Gleixner 3819d8d79cSThomas Gleixner typedef unsigned char u8; 3919d8d79cSThomas Gleixner typedef unsigned short u16; 4019d8d79cSThomas Gleixner typedef unsigned long u32; 4119d8d79cSThomas Gleixner 4219d8d79cSThomas Gleixner #define DEFAULT_MAJOR_ROOT 0 4319d8d79cSThomas Gleixner #define DEFAULT_MINOR_ROOT 0 4419d8d79cSThomas Gleixner 4519d8d79cSThomas Gleixner /* Minimal number of setup sectors */ 4619d8d79cSThomas Gleixner #define SETUP_SECT_MIN 5 4719d8d79cSThomas Gleixner #define SETUP_SECT_MAX 64 4819d8d79cSThomas Gleixner 4919d8d79cSThomas Gleixner /* This must be large enough to hold the entire setup */ 5019d8d79cSThomas Gleixner u8 buf[SETUP_SECT_MAX*512]; 5119d8d79cSThomas Gleixner int is_big_kernel; 5219d8d79cSThomas Gleixner 5319d8d79cSThomas Gleixner static void die(const char * str, ...) 5419d8d79cSThomas Gleixner { 5519d8d79cSThomas Gleixner va_list args; 5619d8d79cSThomas Gleixner va_start(args, str); 5719d8d79cSThomas Gleixner vfprintf(stderr, str, args); 5819d8d79cSThomas Gleixner fputc('\n', stderr); 5919d8d79cSThomas Gleixner exit(1); 6019d8d79cSThomas Gleixner } 6119d8d79cSThomas Gleixner 6219d8d79cSThomas Gleixner static void usage(void) 6319d8d79cSThomas Gleixner { 6419d8d79cSThomas Gleixner die("Usage: build [-b] setup system [rootdev] [> image]"); 6519d8d79cSThomas Gleixner } 6619d8d79cSThomas Gleixner 6719d8d79cSThomas Gleixner int main(int argc, char ** argv) 6819d8d79cSThomas Gleixner { 6919d8d79cSThomas Gleixner unsigned int i, sz, setup_sectors; 7019d8d79cSThomas Gleixner int c; 7119d8d79cSThomas Gleixner u32 sys_size; 7219d8d79cSThomas Gleixner u8 major_root, minor_root; 7319d8d79cSThomas Gleixner struct stat sb; 7419d8d79cSThomas Gleixner FILE *file; 7519d8d79cSThomas Gleixner int fd; 7619d8d79cSThomas Gleixner void *kernel; 7719d8d79cSThomas Gleixner 7819d8d79cSThomas Gleixner if (argc > 2 && !strcmp(argv[1], "-b")) 7919d8d79cSThomas Gleixner { 8019d8d79cSThomas Gleixner is_big_kernel = 1; 8119d8d79cSThomas Gleixner argc--, argv++; 8219d8d79cSThomas Gleixner } 8319d8d79cSThomas Gleixner if ((argc < 3) || (argc > 4)) 8419d8d79cSThomas Gleixner usage(); 8519d8d79cSThomas Gleixner if (argc > 3) { 8619d8d79cSThomas Gleixner if (!strcmp(argv[3], "CURRENT")) { 8719d8d79cSThomas Gleixner if (stat("/", &sb)) { 8819d8d79cSThomas Gleixner perror("/"); 8919d8d79cSThomas Gleixner die("Couldn't stat /"); 9019d8d79cSThomas Gleixner } 9119d8d79cSThomas Gleixner major_root = major(sb.st_dev); 9219d8d79cSThomas Gleixner minor_root = minor(sb.st_dev); 9319d8d79cSThomas Gleixner } else if (strcmp(argv[3], "FLOPPY")) { 9419d8d79cSThomas Gleixner if (stat(argv[3], &sb)) { 9519d8d79cSThomas Gleixner perror(argv[3]); 9619d8d79cSThomas Gleixner die("Couldn't stat root device."); 9719d8d79cSThomas Gleixner } 9819d8d79cSThomas Gleixner major_root = major(sb.st_rdev); 9919d8d79cSThomas Gleixner minor_root = minor(sb.st_rdev); 10019d8d79cSThomas Gleixner } else { 10119d8d79cSThomas Gleixner major_root = 0; 10219d8d79cSThomas Gleixner minor_root = 0; 10319d8d79cSThomas Gleixner } 10419d8d79cSThomas Gleixner } else { 10519d8d79cSThomas Gleixner major_root = DEFAULT_MAJOR_ROOT; 10619d8d79cSThomas Gleixner minor_root = DEFAULT_MINOR_ROOT; 10719d8d79cSThomas Gleixner } 10819d8d79cSThomas Gleixner fprintf(stderr, "Root device is (%d, %d)\n", major_root, minor_root); 10919d8d79cSThomas Gleixner 11019d8d79cSThomas Gleixner /* Copy the setup code */ 11119d8d79cSThomas Gleixner file = fopen(argv[1], "r"); 11219d8d79cSThomas Gleixner if (!file) 11319d8d79cSThomas Gleixner die("Unable to open `%s': %m", argv[1]); 11419d8d79cSThomas Gleixner c = fread(buf, 1, sizeof(buf), file); 11519d8d79cSThomas Gleixner if (ferror(file)) 11619d8d79cSThomas Gleixner die("read-error on `setup'"); 11719d8d79cSThomas Gleixner if (c < 1024) 11819d8d79cSThomas Gleixner die("The setup must be at least 1024 bytes"); 11919d8d79cSThomas Gleixner if (buf[510] != 0x55 || buf[511] != 0xaa) 12019d8d79cSThomas Gleixner die("Boot block hasn't got boot flag (0xAA55)"); 12119d8d79cSThomas Gleixner fclose(file); 12219d8d79cSThomas Gleixner 12319d8d79cSThomas Gleixner /* Pad unused space with zeros */ 12419d8d79cSThomas Gleixner setup_sectors = (c + 511) / 512; 12519d8d79cSThomas Gleixner if (setup_sectors < SETUP_SECT_MIN) 12619d8d79cSThomas Gleixner setup_sectors = SETUP_SECT_MIN; 12719d8d79cSThomas Gleixner i = setup_sectors*512; 12819d8d79cSThomas Gleixner memset(buf+c, 0, i-c); 12919d8d79cSThomas Gleixner 13019d8d79cSThomas Gleixner /* Set the default root device */ 13119d8d79cSThomas Gleixner buf[508] = minor_root; 13219d8d79cSThomas Gleixner buf[509] = major_root; 13319d8d79cSThomas Gleixner 13419d8d79cSThomas Gleixner fprintf(stderr, "Setup is %d bytes (padded to %d bytes).\n", c, i); 13519d8d79cSThomas Gleixner 13619d8d79cSThomas Gleixner /* Open and stat the kernel file */ 13719d8d79cSThomas Gleixner fd = open(argv[2], O_RDONLY); 13819d8d79cSThomas Gleixner if (fd < 0) 13919d8d79cSThomas Gleixner die("Unable to open `%s': %m", argv[2]); 14019d8d79cSThomas Gleixner if (fstat(fd, &sb)) 14119d8d79cSThomas Gleixner die("Unable to stat `%s': %m", argv[2]); 14219d8d79cSThomas Gleixner sz = sb.st_size; 14319d8d79cSThomas Gleixner fprintf (stderr, "System is %d kB\n", (sz+1023)/1024); 14419d8d79cSThomas Gleixner kernel = mmap(NULL, sz, PROT_READ, MAP_SHARED, fd, 0); 14519d8d79cSThomas Gleixner if (kernel == MAP_FAILED) 14619d8d79cSThomas Gleixner die("Unable to mmap '%s': %m", argv[2]); 14719d8d79cSThomas Gleixner sys_size = (sz + 15) / 16; 14819d8d79cSThomas Gleixner if (!is_big_kernel && sys_size > DEF_SYSSIZE) 14919d8d79cSThomas Gleixner die("System is too big. Try using bzImage or modules."); 15019d8d79cSThomas Gleixner 15119d8d79cSThomas Gleixner /* Patch the setup code with the appropriate size parameters */ 15219d8d79cSThomas Gleixner buf[0x1f1] = setup_sectors-1; 15319d8d79cSThomas Gleixner buf[0x1f4] = sys_size; 15419d8d79cSThomas Gleixner buf[0x1f5] = sys_size >> 8; 15519d8d79cSThomas Gleixner buf[0x1f6] = sys_size >> 16; 15619d8d79cSThomas Gleixner buf[0x1f7] = sys_size >> 24; 15719d8d79cSThomas Gleixner 15819d8d79cSThomas Gleixner if (fwrite(buf, 1, i, stdout) != i) 15919d8d79cSThomas Gleixner die("Writing setup failed"); 16019d8d79cSThomas Gleixner 16119d8d79cSThomas Gleixner /* Copy the kernel code */ 16219d8d79cSThomas Gleixner if (fwrite(kernel, 1, sz, stdout) != sz) 16319d8d79cSThomas Gleixner die("Writing kernel failed"); 16419d8d79cSThomas Gleixner close(fd); 16519d8d79cSThomas Gleixner 16619d8d79cSThomas Gleixner /* Everything is OK */ 16719d8d79cSThomas Gleixner return 0; 16819d8d79cSThomas Gleixner } 169