第16章-建造工具

总结

  • 这一章仅仅基本的文件读写,也比较简单。

/projects/linux-0.12/docs/第16章-建造工具/1529198037191.png

/projects/linux-0.12/docs/第16章-建造工具/1529198118515.png

  • 分别打开三个文件,各种校验,然后写到标准输入1中。命令就是重定向到文件
  • 校验规则如下图

/projects/linux-0.12/docs/第16章-建造工具/1529198823641.png

代码

  1. /*
  2. * linux/tools/build.c
  3. *
  4. * (C) 1991 Linus Torvalds
  5. */
  6. /*
  7. * This file builds a disk-image from three different files:
  8. *
  9. * - bootsect: max 510 bytes of 8086 machine code, loads the rest
  10. * - setup: max 4 sectors of 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. *
  20. * Added swap-device specification: Linux 20.12.91
  21. */
  22. #include <stdio.h> /* fprintf */
  23. #include <string.h>
  24. #include <stdlib.h> /* contains exit */
  25. #include <sys/types.h> /* unistd.h needs this */
  26. #include <sys/stat.h>
  27. #include <linux/fs.h>
  28. #include <unistd.h> /* contains read/write */
  29. #include <fcntl.h>
  30. #define MINIX_HEADER 32
  31. #define GCC_HEADER 1024
  32. #define SYS_SIZE 0x3000
  33. #define DEFAULT_MAJOR_ROOT 3
  34. #define DEFAULT_MINOR_ROOT 6
  35. #define DEFAULT_MAJOR_SWAP 0
  36. #define DEFAULT_MINOR_SWAP 0
  37. /* max nr of sectors of setup: don't change unless you also change
  38. * bootsect etc */
  39. #define SETUP_SECTS 4
  40. #define STRINGIFY(x) #x
  41. void die(char *str)
  42. {
  43. fprintf(stderr, "%s\n", str);
  44. exit(1);
  45. }
  46. void usage(void)
  47. {
  48. die("Usage: build bootsect setup system [rootdev] [> image]");
  49. }
  50. int main(int argc, char **argv)
  51. {
  52. int i, c, id;
  53. char buf[1024];
  54. char major_root, minor_root;
  55. char major_swap, minor_swap;
  56. struct stat sb;
  57. if ((argc < 4) || (argc > 6))
  58. usage();
  59. if (argc > 4)
  60. {
  61. if (strcmp(argv[4], "FLOPPY"))
  62. {
  63. if (stat(argv[4], &sb))
  64. {
  65. perror(argv[4]);
  66. die("Couldn't stat root device.");
  67. }
  68. major_root = MAJOR(sb.st_rdev);
  69. minor_root = MINOR(sb.st_rdev);
  70. }
  71. else
  72. {
  73. major_root = 0;
  74. minor_root = 0;
  75. }
  76. }
  77. else
  78. {
  79. major_root = DEFAULT_MAJOR_ROOT;
  80. minor_root = DEFAULT_MINOR_ROOT;
  81. }
  82. if (argc == 6)
  83. {
  84. if (strcmp(argv[5], "NONE"))
  85. {
  86. if (stat(argv[5], &sb))
  87. {
  88. perror(argv[5]);
  89. die("Couldn't stat root device.");
  90. }
  91. major_swap = MAJOR(sb.st_rdev);
  92. minor_swap = MINOR(sb.st_rdev);
  93. }
  94. else
  95. {
  96. major_swap = 0;
  97. minor_swap = 0;
  98. }
  99. }
  100. else
  101. {
  102. major_swap = DEFAULT_MAJOR_SWAP;
  103. minor_swap = DEFAULT_MINOR_SWAP;
  104. }
  105. fprintf(stderr, "Root device is (%d, %d)\n", major_root, minor_root);
  106. fprintf(stderr, "Swap device is (%d, %d)\n", major_swap, minor_swap);
  107. if ((major_root != 2) && (major_root != 3) &&
  108. (major_root != 0))
  109. {
  110. fprintf(stderr, "Illegal root device (major = %d)\n",
  111. major_root);
  112. die("Bad root device --- major #");
  113. }
  114. if (major_swap && major_swap != 3)
  115. {
  116. fprintf(stderr, "Illegal swap device (major = %d)\n",
  117. major_swap);
  118. die("Bad root device --- major #");
  119. }
  120. for (i = 0; i < sizeof buf; i++) buf[i] = 0;
  121. if ((id = open(argv[1], O_RDONLY, 0)) < 0)
  122. die("Unable to open 'boot'");
  123. if (read(id, buf, MINIX_HEADER) != MINIX_HEADER)
  124. die("Unable to read header of 'boot'");
  125. if (((long *) buf)[0] != 0x04100301)
  126. die("Non-Minix header of 'boot'");
  127. if (((long *) buf)[1] != MINIX_HEADER)
  128. die("Non-Minix header of 'boot'");
  129. if (((long *) buf)[3] != 0)
  130. die("Illegal data segment in 'boot'");
  131. if (((long *) buf)[4] != 0)
  132. die("Illegal bss in 'boot'");
  133. if (((long *) buf)[5] != 0)
  134. die("Non-Minix header of 'boot'");
  135. if (((long *) buf)[7] != 0)
  136. die("Illegal symbol table in 'boot'");
  137. i = read(id, buf, sizeof buf);
  138. fprintf(stderr, "Boot sector %d bytes.\n", i);
  139. if (i != 512)
  140. die("Boot block must be exactly 512 bytes");
  141. if ((*(unsigned short *)(buf + 510)) != 0xAA55)
  142. die("Boot block hasn't got boot flag (0xAA55)");
  143. buf[506] = (char) minor_swap;
  144. buf[507] = (char) major_swap;
  145. buf[508] = (char) minor_root;
  146. buf[509] = (char) major_root;
  147. i = write(1, buf, 512);
  148. if (i != 512)
  149. die("Write call failed");
  150. close (id);
  151. if ((id = open(argv[2], O_RDONLY, 0)) < 0)
  152. die("Unable to open 'setup'");
  153. if (read(id, buf, MINIX_HEADER) != MINIX_HEADER)
  154. die("Unable to read header of 'setup'");
  155. if (((long *) buf)[0] != 0x04100301)
  156. die("Non-Minix header of 'setup'");
  157. if (((long *) buf)[1] != MINIX_HEADER)
  158. die("Non-Minix header of 'setup'");
  159. if (((long *) buf)[3] != 0)
  160. die("Illegal data segment in 'setup'");
  161. if (((long *) buf)[4] != 0)
  162. die("Illegal bss in 'setup'");
  163. if (((long *) buf)[5] != 0)
  164. die("Non-Minix header of 'setup'");
  165. if (((long *) buf)[7] != 0)
  166. die("Illegal symbol table in 'setup'");
  167. for (i = 0 ; (c = read(id, buf, sizeof buf)) > 0 ; i += c )
  168. if (write(1, buf, c) != c)
  169. die("Write call failed");
  170. close (id);
  171. if (i > SETUP_SECTS * 512)
  172. die("Setup exceeds " STRINGIFY(SETUP_SECTS)
  173. " sectors - rewrite build/boot/setup");
  174. fprintf(stderr, "Setup is %d bytes.\n", i);
  175. for (c = 0 ; c < sizeof(buf) ; c++)
  176. buf[c] = '\0';
  177. while (i < SETUP_SECTS * 512)
  178. {
  179. c = SETUP_SECTS * 512 - i;
  180. if (c > sizeof(buf))
  181. c = sizeof(buf);
  182. if (write(1, buf, c) != c)
  183. die("Write call failed");
  184. i += c;
  185. }
  186. if ((id = open(argv[3], O_RDONLY, 0)) < 0)
  187. die("Unable to open 'system'");
  188. if (read(id, buf, GCC_HEADER) != GCC_HEADER)
  189. die("Unable to read header of 'system'");
  190. if (((long *) buf)[5] != 0)
  191. die("Non-GCC header of 'system'");
  192. for (i = 0 ; (c = read(id, buf, sizeof buf)) > 0 ; i += c )
  193. if (write(1, buf, c) != c)
  194. die("Write call failed");
  195. close(id);
  196. fprintf(stderr, "System is %d bytes.\n", i);
  197. if (i > SYS_SIZE * 16)
  198. die("System is too big");
  199. return(0);
  200. }