mkbb.c 3.5 KB

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