mkbb.c 3.5 KB

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