mkbb.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /* This utility makes a bootblock suitable for the SRM console/miniloader */
  2. /* Usage:
  3. * mkbb <device> <lxboot>
  4. *
  5. * Where <device> is the name of the device to install the bootblock on,
  6. * and <lxboot> is the name of a bootblock to merge in. This bootblock
  7. * contains the offset and size of the bootloader. It must be exactly
  8. * 512 bytes long.
  9. */
  10. #include <fcntl.h>
  11. #include <unistd.h>
  12. #include <stdlib.h>
  13. #include <stdio.h>
  14. /* Minimal definition of disklabel, so we don't have to include
  15. * asm/disklabel.h (confuses make)
  16. */
  17. #ifndef MAXPARTITIONS
  18. #define MAXPARTITIONS 8 /* max. # of partitions */
  19. #endif
  20. #ifndef u8
  21. #define u8 unsigned char
  22. #endif
  23. #ifndef u16
  24. #define u16 unsigned short
  25. #endif
  26. #ifndef u32
  27. #define u32 unsigned int
  28. #endif
  29. struct disklabel {
  30. u32 d_magic; /* must be DISKLABELMAGIC */
  31. u16 d_type, d_subtype;
  32. u8 d_typename[16];
  33. u8 d_packname[16];
  34. u32 d_secsize;
  35. u32 d_nsectors;
  36. u32 d_ntracks;
  37. u32 d_ncylinders;
  38. u32 d_secpercyl;
  39. u32 d_secprtunit;
  40. u16 d_sparespertrack;
  41. u16 d_sparespercyl;
  42. u32 d_acylinders;
  43. u16 d_rpm, d_interleave, d_trackskew, d_cylskew;
  44. u32 d_headswitch, d_trkseek, d_flags;
  45. u32 d_drivedata[5];
  46. u32 d_spare[5];
  47. u32 d_magic2; /* must be DISKLABELMAGIC */
  48. u16 d_checksum;
  49. u16 d_npartitions;
  50. u32 d_bbsize, d_sbsize;
  51. struct d_partition {
  52. u32 p_size;
  53. u32 p_offset;
  54. u32 p_fsize;
  55. u8 p_fstype;
  56. u8 p_frag;
  57. u16 p_cpg;
  58. } d_partitions[MAXPARTITIONS];
  59. };
  60. typedef union __bootblock {
  61. struct {
  62. char __pad1[64];
  63. struct disklabel __label;
  64. } __u1;
  65. struct {
  66. unsigned long __pad2[63];
  67. unsigned long __checksum;
  68. } __u2;
  69. char bootblock_bytes[512];
  70. unsigned long bootblock_quadwords[64];
  71. } bootblock;
  72. #define bootblock_label __u1.__label
  73. #define bootblock_checksum __u2.__checksum
  74. int main(int argc, char ** argv)
  75. {
  76. bootblock bootblock_from_disk;
  77. bootblock bootloader_image;
  78. int dev, fd;
  79. int i;
  80. int nread;
  81. /* Make sure of the arg count */
  82. if(argc != 3) {
  83. fprintf(stderr, "Usage: %s device lxboot\n", argv[0]);
  84. exit(0);
  85. }
  86. /* First, open the device and make sure it's accessible */
  87. dev = open(argv[1], O_RDWR);
  88. if(dev < 0) {
  89. perror(argv[1]);
  90. exit(0);
  91. }
  92. /* Now open the lxboot and make sure it's reasonable */
  93. fd = open(argv[2], O_RDONLY);
  94. if(fd < 0) {
  95. perror(argv[2]);
  96. close(dev);
  97. exit(0);
  98. }
  99. /* Read in the lxboot */
  100. nread = read(fd, &bootloader_image, sizeof(bootblock));
  101. if(nread != sizeof(bootblock)) {
  102. perror("lxboot read");
  103. fprintf(stderr, "expected %zd, got %d\n", sizeof(bootblock), nread);
  104. exit(0);
  105. }
  106. /* Read in the bootblock from disk. */
  107. nread = read(dev, &bootblock_from_disk, sizeof(bootblock));
  108. if(nread != sizeof(bootblock)) {
  109. perror("bootblock read");
  110. fprintf(stderr, "expected %zd, got %d\n", sizeof(bootblock), nread);
  111. exit(0);
  112. }
  113. /* Swap the bootblock's disklabel into the bootloader */
  114. bootloader_image.bootblock_label = bootblock_from_disk.bootblock_label;
  115. /* Calculate the bootblock checksum */
  116. bootloader_image.bootblock_checksum = 0;
  117. for(i = 0; i < 63; i++) {
  118. bootloader_image.bootblock_checksum +=
  119. bootloader_image.bootblock_quadwords[i];
  120. }
  121. /* Write the whole thing out! */
  122. lseek(dev, 0L, SEEK_SET);
  123. if(write(dev, &bootloader_image, sizeof(bootblock)) != sizeof(bootblock)) {
  124. perror("bootblock write");
  125. exit(0);
  126. }
  127. close(fd);
  128. close(dev);
  129. exit(0);
  130. }