1 // SPDX-License-Identifier: GPL-2.0
2 /* This utility makes a bootblock suitable for the SRM console/miniloader */
5 * mkbb <device> <lxboot>
7 * Where <device> is the name of the device to install the bootblock on,
8 * and <lxboot> is the name of a bootblock to merge in. This bootblock
9 * contains the offset and size of the bootloader. It must be exactly
18 /* Minimal definition of disklabel, so we don't have to include
19 * asm/disklabel.h (confuses make)
22 #define MAXPARTITIONS 8 /* max. # of partitions */
26 #define u8 unsigned char
30 #define u16 unsigned short
34 #define u32 unsigned int
38 u32 d_magic
; /* must be DISKLABELMAGIC */
39 u16 d_type
, d_subtype
;
51 u16 d_rpm
, d_interleave
, d_trackskew
, d_cylskew
;
52 u32 d_headswitch
, d_trkseek
, d_flags
;
55 u32 d_magic2
; /* must be DISKLABELMAGIC */
58 u32 d_bbsize
, d_sbsize
;
66 } d_partitions
[MAXPARTITIONS
];
70 typedef union __bootblock
{
73 struct disklabel __label
;
76 unsigned long __pad2
[63];
77 unsigned long __checksum
;
79 char bootblock_bytes
[512];
80 unsigned long bootblock_quadwords
[64];
83 #define bootblock_label __u1.__label
84 #define bootblock_checksum __u2.__checksum
86 int main(int argc
, char ** argv
)
88 bootblock bootblock_from_disk
;
89 bootblock bootloader_image
;
94 /* Make sure of the arg count */
96 fprintf(stderr
, "Usage: %s device lxboot\n", argv
[0]);
100 /* First, open the device and make sure it's accessible */
101 dev
= open(argv
[1], O_RDWR
);
107 /* Now open the lxboot and make sure it's reasonable */
108 fd
= open(argv
[2], O_RDONLY
);
115 /* Read in the lxboot */
116 nread
= read(fd
, &bootloader_image
, sizeof(bootblock
));
117 if(nread
!= sizeof(bootblock
)) {
118 perror("lxboot read");
119 fprintf(stderr
, "expected %zd, got %d\n", sizeof(bootblock
), nread
);
123 /* Read in the bootblock from disk. */
124 nread
= read(dev
, &bootblock_from_disk
, sizeof(bootblock
));
125 if(nread
!= sizeof(bootblock
)) {
126 perror("bootblock read");
127 fprintf(stderr
, "expected %zd, got %d\n", sizeof(bootblock
), nread
);
131 /* Swap the bootblock's disklabel into the bootloader */
132 bootloader_image
.bootblock_label
= bootblock_from_disk
.bootblock_label
;
134 /* Calculate the bootblock checksum */
135 bootloader_image
.bootblock_checksum
= 0;
136 for(i
= 0; i
< 63; i
++) {
137 bootloader_image
.bootblock_checksum
+=
138 bootloader_image
.bootblock_quadwords
[i
];
141 /* Write the whole thing out! */
142 lseek(dev
, 0L, SEEK_SET
);
143 if(write(dev
, &bootloader_image
, sizeof(bootblock
)) != sizeof(bootblock
)) {
144 perror("bootblock write");