6 #include <sys/bootblock.h>
12 #define DFL_SECSIZE 512
15 #define MFS_FIRST_SUBP_OFFSET 32
17 static int minixfs3_read_mbr(const char* device
, char* buf
)
23 fd
= open(device
, O_RDONLY
);
25 fprintf(stderr
, "Can't open %s: %s\n", device
, strerror(errno
));
29 if (lseek(fd
, MBR_PART_OFFSET
, SEEK_SET
) != MBR_PART_OFFSET
) {
30 fprintf(stderr
, "Can't seek in %s to %d: %s\n",
31 device
, MBR_PART_OFFSET
, strerror(errno
));
36 bytes
= DFL_SECSIZE
- MBR_PART_OFFSET
;
38 if ((n
= read(fd
, buf
, bytes
)) != bytes
) {
39 fprintf(stderr
, "Can't read %d bytes from %s, %d read instead"
41 bytes
, device
, n
, strerror(errno
));
46 if ((uint8_t)buf
[bytes
-2] != 0x55 || (uint8_t)buf
[bytes
-1] != 0xAA) {
47 fprintf(stderr
, "No MBR on %s, signature is %x\n",
48 device
, *(uint16_t*)(&buf
[bytes
-2]));
58 int minixfs3_is_minix_partition(const char* partition
)
60 char buf
[DFL_SECSIZE
]; /* part table + signature */
61 int name_length
= strlen(partition
);
63 /* partition must be 0-3 */
64 if (atol(&partition
[name_length
-1]) >= 4) {
65 fprintf(stderr
, "Wrong device %s, must be /.../cxdyp[0-3]\n",
70 /* it should be partition device, not disk */
71 if (partition
[name_length
-2] != 'p') {
72 fprintf(stderr
, "Wrong device %s, must be /.../cxdyp[0-3]\n",
77 /* MINIX 3 partition with current scheme *must* have subpartitions,
78 * thus MBR has signature. minixfs3_read_mbr checks the signature.
80 if (minixfs3_read_mbr(partition
, buf
))
85 /* bootxx from NetBSD is ~8Kb, and old MINIX installations have just
86 * 1Kb of space for their bootblock. Check if there is enough space
87 * to install bootxx_minixfs3. New installation should have 16Kb before
88 * the first subpartition.
90 int minixfs3_has_bootblock_space(const char* partition
)
92 char buf
[DFL_SECSIZE
]; /* part table + signature */
94 struct mbr_partition
*part
;
95 uint32_t first_subpartition
= (uint32_t) ~0;
96 uint32_t parent_partition
= 0;
98 int name_length
= strlen(partition
);
100 /* partition must be 0-3 */
101 if (atol(&partition
[name_length
-1]) >= 4) {
102 fprintf(stderr
, "Wrong device %s, must be /.../cxdyp[0-3]\n",
106 /* it should be partition device, not disk */
107 if (partition
[name_length
-2] != 'p') {
108 fprintf(stderr
, "Wrong device %s, must be /.../cxdyp[0-3]\n",
113 if (minixfs3_read_mbr(partition
, buf
))
116 part
= (struct mbr_partition
*) buf
;
118 for (i
= 0; i
< 4; i
++) {
119 if (part
[i
].mbrp_size
&& part
[i
].mbrp_start
< first_subpartition
)
120 first_subpartition
= part
[i
].mbrp_start
;
123 strncpy(disk
, partition
, name_length
- 2);
124 disk
[name_length
- 2] = '\0';
126 if (minixfs3_read_mbr(disk
, buf
))
129 for (i
= 0; i
< 4; i
++) {
130 struct mbr_partition
*p
= &part
[i
];
131 if (p
->mbrp_size
&& p
->mbrp_start
<= first_subpartition
132 && (p
->mbrp_start
+ p
->mbrp_size
) > first_subpartition
) {
133 parent_partition
= p
->mbrp_start
;
138 if ((first_subpartition
- parent_partition
) < MFS_FIRST_SUBP_OFFSET
)