125{ 126 struct g_mbr_softc *mp; 127 struct g_slicer *gsp; 128 129 gsp = gp->softc; 130 mp = gsp->softc; 131 g_slice_dumpconf(sb, indent, gp, cp, pp); 132 if (pp != NULL) { 133 if (indent == NULL) 134 sbuf_printf(sb, " ty %d", mp->type[pp->index]); 135 else 136 sbuf_printf(sb, "%s<type>%d</type>\n", indent, 137 mp->type[pp->index]); 138 } 139} 140 141 142static struct dos_partition historical_bogus_partition_table[NDOSPART] = { 143 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, }, 144 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, }, 145 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, }, 146 { 0x80, 0, 1, 0, DOSPTYP_386BSD, 255, 255, 255, 0, 50000, }, 147}; 148static struct dos_partition historical_bogus_partition_table_fixed[NDOSPART] = { 149 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, }, 150 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, }, 151 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, }, 152 { 0x80, 0, 1, 0, DOSPTYP_386BSD, 254, 255, 255, 0, 50000, }, 153}; 154 155static void 156g_mbr_print(int i, struct dos_partition *dp) 157{ 158 159 g_hexdump(dp, sizeof(dp[0])); 160 printf("[%d] f:%02x typ:%d", i, dp->dp_flag, dp->dp_typ); 161 printf(" s(CHS):%d/%d/%d", dp->dp_scyl, dp->dp_shd, dp->dp_ssect); 162 printf(" e(CHS):%d/%d/%d", dp->dp_ecyl, dp->dp_ehd, dp->dp_esect); 163 printf(" s:%d l:%d\n", dp->dp_start, dp->dp_size); 164} 165 166static struct g_geom * 167g_mbr_taste(struct g_class *mp, struct g_provider *pp, int insist) 168{ 169 struct g_geom *gp; 170 struct g_consumer *cp; 171 struct g_provider *pp2; 172 int error, i, npart; 173 struct dos_partition dp[NDOSPART]; 174 struct g_mbr_softc *ms; 175 struct g_slicer *gsp; 176 u_int fwsectors, sectorsize; 177 u_char *buf; 178 179 g_trace(G_T_TOPOLOGY, "mbr_taste(%s,%s)", mp->name, pp->name); 180 g_topology_assert(); 181 gp = g_slice_new(mp, NDOSPART, pp, &cp, &ms, sizeof *ms, g_mbr_start); 182 if (gp == NULL) 183 return (NULL); 184 gsp = gp->softc; 185 g_topology_unlock(); 186 gp->dumpconf = g_mbr_dumpconf; 187 npart = 0; 188 while (1) { /* a trick to allow us to use break */ 189 if (gp->rank != 2 && insist == 0) 190 break; 191 error = g_getattr("GEOM::fwsectors", cp, &fwsectors); 192 if (error) 193 fwsectors = 17; 194 sectorsize = cp->provider->sectorsize; 195 if (sectorsize != 512) 196 break; 197 gsp->frontstuff = sectorsize * fwsectors; 198 buf = g_read_data(cp, 0, sectorsize, &error); 199 if (buf == NULL || error != 0) 200 break; 201 if (buf[0x1fe] != 0x55 && buf[0x1ff] != 0xaa) { 202 g_free(buf); 203 break; 204 } 205 for (i = 0; i < NDOSPART; i++) 206 g_dec_dos_partition( 207 buf + DOSPARTOFF + i * sizeof(struct dos_partition), 208 dp + i); 209 g_free(buf); 210 if (bcmp(dp, historical_bogus_partition_table, 211 sizeof historical_bogus_partition_table) == 0) { 212 if (bootverbose) 213 printf("Ignoring known bogus MBR #0\n"); 214 break; 215 } 216 if (bcmp(dp, historical_bogus_partition_table_fixed, 217 sizeof historical_bogus_partition_table_fixed) == 0) { 218 if (bootverbose) 219 printf("Ignoring known bogus MBR #1\n"); 220 break; 221 } 222 npart = 0; 223 for (i = 0; i < NDOSPART; i++) { 224 /* 225 * A Protective MBR (PMBR) has a single partition of 226 * type 0xEE spanning the whole disk. Such a MBR 227 * protects a GPT on the disk from MBR tools that 228 * don't know anything about GPT. We're interpreting 229 * it a bit more loosely: any partition of type 0xEE 230 * is to be skipped as it doesn't contain any data 231 * that we should care about. We still allow other 232 * partitions to be present in the MBR. A PMBR will 233 * be handled correctly anyway. 234 */ 235 if (dp[i].dp_typ == 0xee) 236 continue; 237 if (dp[i].dp_flag != 0 && dp[i].dp_flag != 0x80) 238 continue; 239 if (dp[i].dp_typ == 0) 240 continue; 241 if (dp[i].dp_size == 0) 242 continue; 243 if (bootverbose) { 244 printf("MBR Slice %d on %s:\n", i + 1, gp->name); 245 g_mbr_print(i, dp + i); 246 } 247 npart++; 248 ms->type[i] = dp[i].dp_typ; 249 g_topology_lock(); 250 pp2 = g_slice_addslice(gp, i, 251 (off_t)dp[i].dp_start << 9ULL, 252 (off_t)dp[i].dp_size << 9ULL, 253 sectorsize, 254 "%ss%d", gp->name, i + 1); 255 g_topology_unlock(); 256 } 257 break; 258 } 259 g_topology_lock(); 260 error = g_access_rel(cp, -1, 0, 0); 261 if (npart > 0) { 262 LIST_FOREACH(pp, &gp->provider, provider) 263 g_error_provider(pp, 0); 264 return (gp); 265 } 266 g_std_spoiled(cp); 267 return (NULL); 268} 269 270 271static struct g_class g_mbr_class = { 272 MBR_CLASS_NAME, 273 g_mbr_taste, 274 NULL, 275 G_CLASS_INITIALIZER 276}; 277 278DECLARE_GEOM_CLASS(g_mbr_class, g_mbr); 279 280#define NDOSEXTPART 32 281struct g_mbrext_softc { 282 int type [NDOSEXTPART]; 283}; 284 285static int 286g_mbrext_start(struct bio *bp) 287{ 288 struct g_provider *pp; 289 struct g_geom *gp; 290 struct g_mbrext_softc *mp; 291 struct g_slicer *gsp;
| 125{ 126 struct g_mbr_softc *mp; 127 struct g_slicer *gsp; 128 129 gsp = gp->softc; 130 mp = gsp->softc; 131 g_slice_dumpconf(sb, indent, gp, cp, pp); 132 if (pp != NULL) { 133 if (indent == NULL) 134 sbuf_printf(sb, " ty %d", mp->type[pp->index]); 135 else 136 sbuf_printf(sb, "%s<type>%d</type>\n", indent, 137 mp->type[pp->index]); 138 } 139} 140 141 142static struct dos_partition historical_bogus_partition_table[NDOSPART] = { 143 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, }, 144 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, }, 145 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, }, 146 { 0x80, 0, 1, 0, DOSPTYP_386BSD, 255, 255, 255, 0, 50000, }, 147}; 148static struct dos_partition historical_bogus_partition_table_fixed[NDOSPART] = { 149 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, }, 150 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, }, 151 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, }, 152 { 0x80, 0, 1, 0, DOSPTYP_386BSD, 254, 255, 255, 0, 50000, }, 153}; 154 155static void 156g_mbr_print(int i, struct dos_partition *dp) 157{ 158 159 g_hexdump(dp, sizeof(dp[0])); 160 printf("[%d] f:%02x typ:%d", i, dp->dp_flag, dp->dp_typ); 161 printf(" s(CHS):%d/%d/%d", dp->dp_scyl, dp->dp_shd, dp->dp_ssect); 162 printf(" e(CHS):%d/%d/%d", dp->dp_ecyl, dp->dp_ehd, dp->dp_esect); 163 printf(" s:%d l:%d\n", dp->dp_start, dp->dp_size); 164} 165 166static struct g_geom * 167g_mbr_taste(struct g_class *mp, struct g_provider *pp, int insist) 168{ 169 struct g_geom *gp; 170 struct g_consumer *cp; 171 struct g_provider *pp2; 172 int error, i, npart; 173 struct dos_partition dp[NDOSPART]; 174 struct g_mbr_softc *ms; 175 struct g_slicer *gsp; 176 u_int fwsectors, sectorsize; 177 u_char *buf; 178 179 g_trace(G_T_TOPOLOGY, "mbr_taste(%s,%s)", mp->name, pp->name); 180 g_topology_assert(); 181 gp = g_slice_new(mp, NDOSPART, pp, &cp, &ms, sizeof *ms, g_mbr_start); 182 if (gp == NULL) 183 return (NULL); 184 gsp = gp->softc; 185 g_topology_unlock(); 186 gp->dumpconf = g_mbr_dumpconf; 187 npart = 0; 188 while (1) { /* a trick to allow us to use break */ 189 if (gp->rank != 2 && insist == 0) 190 break; 191 error = g_getattr("GEOM::fwsectors", cp, &fwsectors); 192 if (error) 193 fwsectors = 17; 194 sectorsize = cp->provider->sectorsize; 195 if (sectorsize != 512) 196 break; 197 gsp->frontstuff = sectorsize * fwsectors; 198 buf = g_read_data(cp, 0, sectorsize, &error); 199 if (buf == NULL || error != 0) 200 break; 201 if (buf[0x1fe] != 0x55 && buf[0x1ff] != 0xaa) { 202 g_free(buf); 203 break; 204 } 205 for (i = 0; i < NDOSPART; i++) 206 g_dec_dos_partition( 207 buf + DOSPARTOFF + i * sizeof(struct dos_partition), 208 dp + i); 209 g_free(buf); 210 if (bcmp(dp, historical_bogus_partition_table, 211 sizeof historical_bogus_partition_table) == 0) { 212 if (bootverbose) 213 printf("Ignoring known bogus MBR #0\n"); 214 break; 215 } 216 if (bcmp(dp, historical_bogus_partition_table_fixed, 217 sizeof historical_bogus_partition_table_fixed) == 0) { 218 if (bootverbose) 219 printf("Ignoring known bogus MBR #1\n"); 220 break; 221 } 222 npart = 0; 223 for (i = 0; i < NDOSPART; i++) { 224 /* 225 * A Protective MBR (PMBR) has a single partition of 226 * type 0xEE spanning the whole disk. Such a MBR 227 * protects a GPT on the disk from MBR tools that 228 * don't know anything about GPT. We're interpreting 229 * it a bit more loosely: any partition of type 0xEE 230 * is to be skipped as it doesn't contain any data 231 * that we should care about. We still allow other 232 * partitions to be present in the MBR. A PMBR will 233 * be handled correctly anyway. 234 */ 235 if (dp[i].dp_typ == 0xee) 236 continue; 237 if (dp[i].dp_flag != 0 && dp[i].dp_flag != 0x80) 238 continue; 239 if (dp[i].dp_typ == 0) 240 continue; 241 if (dp[i].dp_size == 0) 242 continue; 243 if (bootverbose) { 244 printf("MBR Slice %d on %s:\n", i + 1, gp->name); 245 g_mbr_print(i, dp + i); 246 } 247 npart++; 248 ms->type[i] = dp[i].dp_typ; 249 g_topology_lock(); 250 pp2 = g_slice_addslice(gp, i, 251 (off_t)dp[i].dp_start << 9ULL, 252 (off_t)dp[i].dp_size << 9ULL, 253 sectorsize, 254 "%ss%d", gp->name, i + 1); 255 g_topology_unlock(); 256 } 257 break; 258 } 259 g_topology_lock(); 260 error = g_access_rel(cp, -1, 0, 0); 261 if (npart > 0) { 262 LIST_FOREACH(pp, &gp->provider, provider) 263 g_error_provider(pp, 0); 264 return (gp); 265 } 266 g_std_spoiled(cp); 267 return (NULL); 268} 269 270 271static struct g_class g_mbr_class = { 272 MBR_CLASS_NAME, 273 g_mbr_taste, 274 NULL, 275 G_CLASS_INITIALIZER 276}; 277 278DECLARE_GEOM_CLASS(g_mbr_class, g_mbr); 279 280#define NDOSEXTPART 32 281struct g_mbrext_softc { 282 int type [NDOSEXTPART]; 283}; 284 285static int 286g_mbrext_start(struct bio *bp) 287{ 288 struct g_provider *pp; 289 struct g_geom *gp; 290 struct g_mbrext_softc *mp; 291 struct g_slicer *gsp;
|
308{ 309 struct g_mbrext_softc *mp; 310 struct g_slicer *gsp; 311 312 g_slice_dumpconf(sb, indent, gp, cp, pp); 313 gsp = gp->softc; 314 mp = gsp->softc; 315 if (pp != NULL) { 316 if (indent == NULL) 317 sbuf_printf(sb, " ty %d", mp->type[pp->index]); 318 else 319 sbuf_printf(sb, "%s<type>%d</type>\n", indent, 320 mp->type[pp->index]); 321 } 322} 323 324static struct g_geom * 325g_mbrext_taste(struct g_class *mp, struct g_provider *pp, int insist __unused) 326{ 327 struct g_geom *gp; 328 struct g_consumer *cp; 329 struct g_provider *pp2; 330 int error, i, slice; 331 struct g_mbrext_softc *ms; 332 off_t off; 333 u_char *buf; 334 struct dos_partition dp[4]; 335 u_int fwsectors, sectorsize; 336 struct g_slicer *gsp; 337 338 g_trace(G_T_TOPOLOGY, "g_mbrext_taste(%s,%s)", mp->name, pp->name); 339 g_topology_assert(); 340 if (strcmp(pp->geom->class->name, MBR_CLASS_NAME)) 341 return (NULL); 342 gp = g_slice_new(mp, NDOSEXTPART, pp, &cp, &ms, sizeof *ms, g_mbrext_start); 343 if (gp == NULL) 344 return (NULL); 345 gsp = gp->softc; 346 g_topology_unlock(); 347 gp->dumpconf = g_mbrext_dumpconf; 348 off = 0; 349 slice = 0; 350 while (1) { /* a trick to allow us to use break */ 351 error = g_getattr("MBR::type", cp, &i); 352 if (error || (i != DOSPTYP_EXT && i != DOSPTYP_EXTLBA)) 353 break; 354 error = g_getattr("GEOM::fwsectors", cp, &fwsectors); 355 if (error) 356 fwsectors = 17; 357 sectorsize = cp->provider->sectorsize; 358 if (sectorsize != 512) 359 break; 360 gsp->frontstuff = sectorsize * fwsectors; 361 for (;;) { 362 buf = g_read_data(cp, off, sectorsize, &error); 363 if (buf == NULL || error != 0) 364 break; 365 if (buf[0x1fe] != 0x55 && buf[0x1ff] != 0xaa) 366 break; 367 for (i = 0; i < NDOSPART; i++) 368 g_dec_dos_partition( 369 buf + DOSPARTOFF + 370 i * sizeof(struct dos_partition), dp + i); 371 g_free(buf); 372 printf("MBREXT Slice %d on %s:\n", slice + 5, gp->name); 373 g_mbr_print(0, dp); 374 g_mbr_print(1, dp + 1); 375 if ((dp[0].dp_flag & 0x7f) == 0 && 376 dp[0].dp_size != 0 && dp[0].dp_typ != 0) { 377 g_topology_lock(); 378 pp2 = g_slice_addslice(gp, slice, 379 (((off_t)dp[0].dp_start) << 9ULL) + off, 380 ((off_t)dp[0].dp_size) << 9ULL, 381 sectorsize, 382 "%*.*s%d", 383 strlen(gp->name) - 1, 384 strlen(gp->name) - 1, 385 gp->name, 386 slice + 5); 387 g_topology_unlock(); 388 ms->type[slice] = dp[0].dp_typ; 389 slice++; 390 g_error_provider(pp2, 0); 391 } 392 if (dp[1].dp_flag != 0) 393 break; 394 if (dp[1].dp_typ != DOSPTYP_EXT) 395 break; 396 if (dp[1].dp_size == 0) 397 break; 398 off = ((off_t)dp[1].dp_start) << 9ULL; 399 } 400 break; 401 } 402 g_topology_lock(); 403 error = g_access_rel(cp, -1, 0, 0); 404 if (slice > 0) { 405 /* XXX: add magic spaces */ 406 return (gp); 407 } 408 409 g_topology_assert(); 410 g_std_spoiled(cp); 411 g_topology_assert(); 412 return (NULL); 413} 414 415 416static struct g_class g_mbrext_class = { 417 MBREXT_CLASS_NAME, 418 g_mbrext_taste, 419 NULL, 420 G_CLASS_INITIALIZER 421}; 422 423DECLARE_GEOM_CLASS(g_mbrext_class, g_mbrext);
| 308{ 309 struct g_mbrext_softc *mp; 310 struct g_slicer *gsp; 311 312 g_slice_dumpconf(sb, indent, gp, cp, pp); 313 gsp = gp->softc; 314 mp = gsp->softc; 315 if (pp != NULL) { 316 if (indent == NULL) 317 sbuf_printf(sb, " ty %d", mp->type[pp->index]); 318 else 319 sbuf_printf(sb, "%s<type>%d</type>\n", indent, 320 mp->type[pp->index]); 321 } 322} 323 324static struct g_geom * 325g_mbrext_taste(struct g_class *mp, struct g_provider *pp, int insist __unused) 326{ 327 struct g_geom *gp; 328 struct g_consumer *cp; 329 struct g_provider *pp2; 330 int error, i, slice; 331 struct g_mbrext_softc *ms; 332 off_t off; 333 u_char *buf; 334 struct dos_partition dp[4]; 335 u_int fwsectors, sectorsize; 336 struct g_slicer *gsp; 337 338 g_trace(G_T_TOPOLOGY, "g_mbrext_taste(%s,%s)", mp->name, pp->name); 339 g_topology_assert(); 340 if (strcmp(pp->geom->class->name, MBR_CLASS_NAME)) 341 return (NULL); 342 gp = g_slice_new(mp, NDOSEXTPART, pp, &cp, &ms, sizeof *ms, g_mbrext_start); 343 if (gp == NULL) 344 return (NULL); 345 gsp = gp->softc; 346 g_topology_unlock(); 347 gp->dumpconf = g_mbrext_dumpconf; 348 off = 0; 349 slice = 0; 350 while (1) { /* a trick to allow us to use break */ 351 error = g_getattr("MBR::type", cp, &i); 352 if (error || (i != DOSPTYP_EXT && i != DOSPTYP_EXTLBA)) 353 break; 354 error = g_getattr("GEOM::fwsectors", cp, &fwsectors); 355 if (error) 356 fwsectors = 17; 357 sectorsize = cp->provider->sectorsize; 358 if (sectorsize != 512) 359 break; 360 gsp->frontstuff = sectorsize * fwsectors; 361 for (;;) { 362 buf = g_read_data(cp, off, sectorsize, &error); 363 if (buf == NULL || error != 0) 364 break; 365 if (buf[0x1fe] != 0x55 && buf[0x1ff] != 0xaa) 366 break; 367 for (i = 0; i < NDOSPART; i++) 368 g_dec_dos_partition( 369 buf + DOSPARTOFF + 370 i * sizeof(struct dos_partition), dp + i); 371 g_free(buf); 372 printf("MBREXT Slice %d on %s:\n", slice + 5, gp->name); 373 g_mbr_print(0, dp); 374 g_mbr_print(1, dp + 1); 375 if ((dp[0].dp_flag & 0x7f) == 0 && 376 dp[0].dp_size != 0 && dp[0].dp_typ != 0) { 377 g_topology_lock(); 378 pp2 = g_slice_addslice(gp, slice, 379 (((off_t)dp[0].dp_start) << 9ULL) + off, 380 ((off_t)dp[0].dp_size) << 9ULL, 381 sectorsize, 382 "%*.*s%d", 383 strlen(gp->name) - 1, 384 strlen(gp->name) - 1, 385 gp->name, 386 slice + 5); 387 g_topology_unlock(); 388 ms->type[slice] = dp[0].dp_typ; 389 slice++; 390 g_error_provider(pp2, 0); 391 } 392 if (dp[1].dp_flag != 0) 393 break; 394 if (dp[1].dp_typ != DOSPTYP_EXT) 395 break; 396 if (dp[1].dp_size == 0) 397 break; 398 off = ((off_t)dp[1].dp_start) << 9ULL; 399 } 400 break; 401 } 402 g_topology_lock(); 403 error = g_access_rel(cp, -1, 0, 0); 404 if (slice > 0) { 405 /* XXX: add magic spaces */ 406 return (gp); 407 } 408 409 g_topology_assert(); 410 g_std_spoiled(cp); 411 g_topology_assert(); 412 return (NULL); 413} 414 415 416static struct g_class g_mbrext_class = { 417 MBREXT_CLASS_NAME, 418 g_mbrext_taste, 419 NULL, 420 G_CLASS_INITIALIZER 421}; 422 423DECLARE_GEOM_CLASS(g_mbrext_class, g_mbrext);
|