build.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /*
  2. * Copyright (C) 1991, 1992 Linus Torvalds
  3. * Copyright (C) 1997 Martin Mares
  4. */
  5. /*
  6. * This file builds a disk-image from three different files:
  7. *
  8. * - bootsect: compatibility mbr which prints an error message if
  9. * someone tries to boot the kernel directly.
  10. * - 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. * Changes by tytso to allow root device specification
  19. * High loaded stuff by Hans Lermen & Werner Almesberger, Feb. 1996
  20. * Cross compiling fixes by Gertjan van Wingerde, July 1996
  21. * Rewritten by Martin Mares, April 1997
  22. */
  23. #include <stdio.h>
  24. #include <string.h>
  25. #include <stdlib.h>
  26. #include <stdarg.h>
  27. #include <sys/types.h>
  28. #include <sys/stat.h>
  29. #include <sys/sysmacros.h>
  30. #include <unistd.h>
  31. #include <fcntl.h>
  32. #include <asm/boot.h>
  33. typedef unsigned char byte;
  34. typedef unsigned short word;
  35. typedef unsigned long u32;
  36. #define DEFAULT_MAJOR_ROOT 0
  37. #define DEFAULT_MINOR_ROOT 0
  38. /* Minimal number of setup sectors (see also bootsect.S) */
  39. #define SETUP_SECTS 4
  40. byte buf[1024];
  41. int fd;
  42. int is_big_kernel;
  43. void die(const char * str, ...)
  44. {
  45. va_list args;
  46. va_start(args, str);
  47. vfprintf(stderr, str, args);
  48. fputc('\n', stderr);
  49. exit(1);
  50. }
  51. void file_open(const char *name)
  52. {
  53. if ((fd = open(name, O_RDONLY, 0)) < 0)
  54. die("Unable to open `%s': %m", name);
  55. }
  56. void usage(void)
  57. {
  58. die("Usage: build [-b] bootsect setup system [rootdev] [> image]");
  59. }
  60. int main(int argc, char ** argv)
  61. {
  62. unsigned int i, sz, setup_sectors;
  63. int c;
  64. u32 sys_size;
  65. byte major_root, minor_root;
  66. struct stat sb;
  67. if (argc > 2 && !strcmp(argv[1], "-b"))
  68. {
  69. is_big_kernel = 1;
  70. argc--, argv++;
  71. }
  72. if ((argc < 4) || (argc > 5))
  73. usage();
  74. if (argc > 4) {
  75. if (!strcmp(argv[4], "CURRENT")) {
  76. if (stat("/", &sb)) {
  77. perror("/");
  78. die("Couldn't stat /");
  79. }
  80. major_root = major(sb.st_dev);
  81. minor_root = minor(sb.st_dev);
  82. } else if (strcmp(argv[4], "FLOPPY")) {
  83. if (stat(argv[4], &sb)) {
  84. perror(argv[4]);
  85. die("Couldn't stat root device.");
  86. }
  87. major_root = major(sb.st_rdev);
  88. minor_root = minor(sb.st_rdev);
  89. } else {
  90. major_root = 0;
  91. minor_root = 0;
  92. }
  93. } else {
  94. major_root = DEFAULT_MAJOR_ROOT;
  95. minor_root = DEFAULT_MINOR_ROOT;
  96. }
  97. fprintf(stderr, "Root device is (%d, %d)\n", major_root, minor_root);
  98. file_open(argv[1]);
  99. i = read(fd, buf, sizeof(buf));
  100. fprintf(stderr,"Boot sector %d bytes.\n",i);
  101. if (i != 512)
  102. die("Boot block must be exactly 512 bytes");
  103. if (buf[510] != 0x55 || buf[511] != 0xaa)
  104. die("Boot block hasn't got boot flag (0xAA55)");
  105. buf[508] = minor_root;
  106. buf[509] = major_root;
  107. if (write(1, buf, 512) != 512)
  108. die("Write call failed");
  109. close (fd);
  110. file_open(argv[2]); /* Copy the setup code */
  111. for (i=0 ; (c=read(fd, buf, sizeof(buf)))>0 ; i+=c )
  112. if (write(1, buf, c) != c)
  113. die("Write call failed");
  114. if (c != 0)
  115. die("read-error on `setup'");
  116. close (fd);
  117. setup_sectors = (i + 511) / 512; /* Pad unused space with zeros */
  118. /* for compatibility with ancient versions of LILO. */
  119. if (setup_sectors < SETUP_SECTS)
  120. setup_sectors = SETUP_SECTS;
  121. fprintf(stderr, "Setup is %d bytes.\n", i);
  122. memset(buf, 0, sizeof(buf));
  123. while (i < setup_sectors * 512) {
  124. c = setup_sectors * 512 - i;
  125. if (c > sizeof(buf))
  126. c = sizeof(buf);
  127. if (write(1, buf, c) != c)
  128. die("Write call failed");
  129. i += c;
  130. }
  131. file_open(argv[3]);
  132. if (fstat (fd, &sb))
  133. die("Unable to stat `%s': %m", argv[3]);
  134. sz = sb.st_size;
  135. fprintf (stderr, "System is %d kB\n", sz/1024);
  136. sys_size = (sz + 15) / 16;
  137. if (!is_big_kernel && sys_size > DEF_SYSSIZE)
  138. die("System is too big. Try using bzImage or modules.");
  139. while (sz > 0) {
  140. int l, n;
  141. l = (sz > sizeof(buf)) ? sizeof(buf) : sz;
  142. if ((n=read(fd, buf, l)) != l) {
  143. if (n < 0)
  144. die("Error reading %s: %m", argv[3]);
  145. else
  146. die("%s: Unexpected EOF", argv[3]);
  147. }
  148. if (write(1, buf, l) != l)
  149. die("Write failed");
  150. sz -= l;
  151. }
  152. close(fd);
  153. if (lseek(1, 497, SEEK_SET) != 497) /* Write sizes to the bootsector */
  154. die("Output: seek failed");
  155. buf[0] = setup_sectors;
  156. if (write(1, buf, 1) != 1)
  157. die("Write of setup sector count failed");
  158. if (lseek(1, 500, SEEK_SET) != 500)
  159. die("Output: seek failed");
  160. buf[0] = (sys_size & 0xff);
  161. buf[1] = ((sys_size >> 8) & 0xff);
  162. buf[2] = ((sys_size >> 16) & 0xff);
  163. buf[3] = ((sys_size >> 24) & 0xff);
  164. if (write(1, buf, 4) != 4)
  165. die("Write of image length failed");
  166. return 0; /* Everything is OK */
  167. }