build.c 4.6 KB

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