00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 #include <stdio.h>
00028 #include <string.h>
00029 #include <stdlib.h>
00030 #include <stdarg.h>
00031 #include <sys/types.h>
00032 #include <sys/stat.h>
00033 #include <sys/sysmacros.h>
00034 #include <unistd.h>
00035 #include <fcntl.h>
00036 #include <asm/boot.h>
00037
00038 typedef unsigned char byte;
00039 typedef unsigned short word;
00040 typedef unsigned long u32;
00041
00042 #define DEFAULT_MAJOR_ROOT 0
00043 #define DEFAULT_MINOR_ROOT 0
00044
00045
00046 #define SETUP_SECTS 4
00047
00048 byte buf[1024];
00049 int fd;
00050 int is_big_kernel;
00051
00052 void die(const char * str, ...)
00053 {
00054 va_list args;
00055 va_start(args, str);
00056 vfprintf(stderr, str, args);
00057 fputc('\n', stderr);
00058 exit(1);
00059 }
00060
00061 void file_open(const char *name)
00062 {
00063 if ((fd = open(name, O_RDONLY, 0)) < 0)
00064 die("Unable to open `%s': %m", name);
00065 }
00066
00067 void usage(void)
00068 {
00069 die("Usage: build [-b] bootsect setup system [rootdev] [> image]");
00070 }
00071
00072 int main(int argc, char ** argv)
00073 {
00074 unsigned int i, c, sz, setup_sectors;
00075 u32 sys_size;
00076 byte major_root, minor_root;
00077 struct stat sb;
00078
00079 if (argc > 2 && !strcmp(argv[1], "-b"))
00080 {
00081 is_big_kernel = 1;
00082 argc--, argv++;
00083 }
00084 if ((argc < 4) || (argc > 5))
00085 usage();
00086 if (argc > 4) {
00087 if (!strcmp(argv[4], "CURRENT")) {
00088 if (stat("/", &sb)) {
00089 perror("/");
00090 die("Couldn't stat /");
00091 }
00092 major_root = major(sb.st_dev);
00093 minor_root = minor(sb.st_dev);
00094 } else if (strcmp(argv[4], "FLOPPY")) {
00095 if (stat(argv[4], &sb)) {
00096 perror(argv[4]);
00097 die("Couldn't stat root device.");
00098 }
00099 major_root = major(sb.st_rdev);
00100 minor_root = minor(sb.st_rdev);
00101 } else {
00102 major_root = 0;
00103 minor_root = 0;
00104 }
00105 } else {
00106 major_root = DEFAULT_MAJOR_ROOT;
00107 minor_root = DEFAULT_MINOR_ROOT;
00108 }
00109 fprintf(stderr, "Root device is (%d, %d)\n", major_root, minor_root);
00110
00111 file_open(argv[1]);
00112 i = read(fd, buf, sizeof(buf));
00113 fprintf(stderr,"Boot sector %d bytes.\n",i);
00114 if (i != 512)
00115 die("Boot block must be exactly 512 bytes");
00116 if (buf[510] != 0x55 || buf[511] != 0xaa)
00117 die("Boot block hasn't got boot flag (0xAA55)");
00118 buf[508] = minor_root;
00119 buf[509] = major_root;
00120 if (write(1, buf, 512) != 512)
00121 die("Write call failed");
00122 close (fd);
00123
00124 file_open(argv[2]);
00125 for (i=0 ; (c=read(fd, buf, sizeof(buf)))>0 ; i+=c )
00126 if (write(1, buf, c) != c)
00127 die("Write call failed");
00128 if (c != 0)
00129 die("read-error on `setup'");
00130 close (fd);
00131
00132 setup_sectors = (i + 511) / 512;
00133
00134 if (setup_sectors < SETUP_SECTS)
00135 setup_sectors = SETUP_SECTS;
00136 fprintf(stderr, "Setup is %d bytes.\n", i);
00137 memset(buf, 0, sizeof(buf));
00138 while (i < setup_sectors * 512) {
00139 c = setup_sectors * 512 - i;
00140 if (c > sizeof(buf))
00141 c = sizeof(buf);
00142 if (write(1, buf, c) != c)
00143 die("Write call failed");
00144 i += c;
00145 }
00146
00147 file_open(argv[3]);
00148 if (fstat (fd, &sb))
00149 die("Unable to stat `%s': %m", argv[3]);
00150 sz = sb.st_size;
00151 fprintf (stderr, "System is %d kB\n", sz/1024);
00152 sys_size = (sz + 15) / 16;
00153
00154 if (sys_size > (is_big_kernel ? 0x28000 : DEF_SYSSIZE))
00155 die("System is too big. Try using %smodules.",
00156 is_big_kernel ? "" : "bzImage or ");
00157 if (sys_size > 0xefff)
00158 fprintf(stderr,"warning: kernel is too big for standalone boot "
00159 "from floppy\n");
00160 while (sz > 0) {
00161 int l, n;
00162
00163 l = (sz > sizeof(buf)) ? sizeof(buf) : sz;
00164 if ((n=read(fd, buf, l)) != l) {
00165 if (n < 0)
00166 die("Error reading %s: %m", argv[3]);
00167 else
00168 die("%s: Unexpected EOF", argv[3]);
00169 }
00170 if (write(1, buf, l) != l)
00171 die("Write failed");
00172 sz -= l;
00173 }
00174 close(fd);
00175
00176 if (lseek(1, 497, SEEK_SET) != 497)
00177 die("Output: seek failed");
00178 buf[0] = setup_sectors;
00179 if (write(1, buf, 1) != 1)
00180 die("Write of setup sector count failed");
00181 if (lseek(1, 500, SEEK_SET) != 500)
00182 die("Output: seek failed");
00183 buf[0] = (sys_size & 0xff);
00184 buf[1] = ((sys_size >> 8) & 0xff);
00185 if (write(1, buf, 2) != 2)
00186 die("Write of image length failed");
00187
00188 return 0;
00189 }