1 /*- 2 * Copyright (c) 2007 Marcel Moolenaar 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 */ 26 27 #include <sys/cdefs.h> 28 __FBSDID("$FreeBSD$"); 29 30 #include <sys/param.h> 31 #include <sys/bio.h> 32 #include <sys/diskmbr.h> 33 #include <sys/endian.h> 34 #include <sys/kernel.h> 35 #include <sys/kobj.h> 36 #include <sys/limits.h> 37 #include <sys/lock.h> 38 #include <sys/malloc.h> 39 #include <sys/mutex.h> 40 #include <sys/queue.h> 41 #include <sys/sbuf.h> 42 #include <sys/systm.h> 43 #include <geom/geom.h> 44 #include <geom/part/g_part.h> 45 46 #include "g_part_if.h" 47 48 #define MBRSIZE 512 49 50 struct g_part_mbr_table { 51 struct g_part_table base; 52 u_char mbr[MBRSIZE]; 53 }; 54 55 struct g_part_mbr_entry { 56 struct g_part_entry base; 57 struct dos_partition ent; 58 }; 59 60 static int g_part_mbr_add(struct g_part_table *, struct g_part_entry *, 61 struct g_part_parms *); 62 static int g_part_mbr_create(struct g_part_table *, struct g_part_parms *); 63 static int g_part_mbr_destroy(struct g_part_table *, struct g_part_parms *); 64 static int g_part_mbr_dumpto(struct g_part_table *, struct g_part_entry *); 65 static int g_part_mbr_modify(struct g_part_table *, struct g_part_entry *, 66 struct g_part_parms *); 67 static char *g_part_mbr_name(struct g_part_table *, struct g_part_entry *, 68 char *, size_t); 69 static int g_part_mbr_probe(struct g_part_table *, struct g_consumer *); 70 static int g_part_mbr_read(struct g_part_table *, struct g_consumer *); 71 static const char *g_part_mbr_type(struct g_part_table *, struct g_part_entry *, 72 char *, size_t); 73 static int g_part_mbr_write(struct g_part_table *, struct g_consumer *); 74 75 static kobj_method_t g_part_mbr_methods[] = { 76 KOBJMETHOD(g_part_add, g_part_mbr_add), 77 KOBJMETHOD(g_part_create, g_part_mbr_create), 78 KOBJMETHOD(g_part_destroy, g_part_mbr_destroy), 79 KOBJMETHOD(g_part_dumpto, g_part_mbr_dumpto), 80 KOBJMETHOD(g_part_modify, g_part_mbr_modify), 81 KOBJMETHOD(g_part_name, g_part_mbr_name), 82 KOBJMETHOD(g_part_probe, g_part_mbr_probe), 83 KOBJMETHOD(g_part_read, g_part_mbr_read), 84 KOBJMETHOD(g_part_type, g_part_mbr_type), 85 KOBJMETHOD(g_part_write, g_part_mbr_write), 86 { 0, 0 } 87 }; 88 89 static struct g_part_scheme g_part_mbr_scheme = { 90 "MBR", 91 g_part_mbr_methods, 92 sizeof(struct g_part_mbr_table), 93 .gps_entrysz = sizeof(struct g_part_mbr_entry), 94 .gps_minent = NDOSPART, 95 .gps_maxent = NDOSPART, 96 }; 97 G_PART_SCHEME_DECLARE(g_part_mbr_scheme); 98 99 static int 100 mbr_parse_type(const char *type, u_char *dp_typ) 101 { 102 const char *alias; 103 char *endp; 104 long lt; 105 106 if (type[0] == '!') { 107 lt = strtol(type + 1, &endp, 0); 108 if (type[1] == '\0' || *endp != '\0' || lt <= 0 || lt >= 256) 109 return (EINVAL); 110 *dp_typ = (u_char)lt; 111 return (0); 112 } 113 alias = g_part_alias_name(G_PART_ALIAS_FREEBSD); 114 if (!strcasecmp(type, alias)) { 115 *dp_typ = DOSPTYP_386BSD; 116 return (0); 117 } 118 return (EINVAL); 119 } 120 121 static void 122 mbr_set_chs(struct g_part_table *table, uint32_t lba, u_char *cylp, u_char *hdp, 123 u_char *secp) 124 { 125 uint32_t cyl, hd, sec; 126 127 sec = lba % table->gpt_sectors + 1; 128 lba /= table->gpt_sectors; 129 hd = lba % table->gpt_heads; 130 lba /= table->gpt_heads; 131 cyl = lba; 132 if (cyl > 1023) 133 sec = hd = cyl = ~0; 134 135 *cylp = cyl & 0xff; 136 *hdp = hd & 0xff; 137 *secp = (sec & 0x3f) | ((cyl >> 2) & 0xc0); 138 } 139 140 static int 141 g_part_mbr_add(struct g_part_table *basetable, struct g_part_entry *baseentry, 142 struct g_part_parms *gpp) 143 { 144 struct g_part_mbr_entry *entry; 145 struct g_part_mbr_table *table; 146 uint32_t start, size, sectors; 147 148 if (gpp->gpp_parms & G_PART_PARM_LABEL) 149 return (EINVAL); 150 151 sectors = basetable->gpt_sectors; 152 153 entry = (struct g_part_mbr_entry *)baseentry; 154 table = (struct g_part_mbr_table *)basetable; 155 156 start = gpp->gpp_start; 157 size = gpp->gpp_size; 158 if (size < sectors) 159 return (EINVAL); 160 if (start % sectors) { 161 size = size - sectors + (start % sectors); 162 start = start - (start % sectors) + sectors; 163 } 164 if (size % sectors) 165 size = size - (size % sectors); 166 if (size < sectors) 167 return (EINVAL); 168 169 if (baseentry->gpe_deleted) 170 bzero(&entry->ent, sizeof(entry->ent)); 171 172 KASSERT(baseentry->gpe_start <= start, (__func__)); 173 KASSERT(baseentry->gpe_end >= start + size - 1, (__func__)); 174 baseentry->gpe_start = start; 175 baseentry->gpe_end = start + size - 1; 176 entry->ent.dp_start = start; 177 entry->ent.dp_size = size; 178 mbr_set_chs(basetable, baseentry->gpe_start, &entry->ent.dp_scyl, 179 &entry->ent.dp_shd, &entry->ent.dp_ssect); 180 mbr_set_chs(basetable, baseentry->gpe_end, &entry->ent.dp_ecyl, 181 &entry->ent.dp_ehd, &entry->ent.dp_esect); 182 return (mbr_parse_type(gpp->gpp_type, &entry->ent.dp_typ)); 183 } 184 185 static int 186 g_part_mbr_create(struct g_part_table *basetable, struct g_part_parms *gpp) 187 { 188 struct g_consumer *cp; 189 struct g_provider *pp; 190 struct g_part_mbr_table *table; 191 uint64_t msize; 192 193 pp = gpp->gpp_provider; 194 cp = LIST_FIRST(&pp->consumers); 195 196 if (pp->sectorsize < MBRSIZE) 197 return (ENOSPC); 198 199 msize = pp->mediasize / pp->sectorsize; 200 basetable->gpt_first = basetable->gpt_sectors; 201 basetable->gpt_last = msize - (msize % basetable->gpt_sectors) - 1; 202 203 table = (struct g_part_mbr_table *)basetable; 204 le16enc(table->mbr + DOSMAGICOFFSET, DOSMAGIC); 205 return (0); 206 } 207 208 static int 209 g_part_mbr_destroy(struct g_part_table *basetable, struct g_part_parms *gpp) 210 { 211 212 /* Wipe the first sector to clear the partitioning. */ 213 basetable->gpt_smhead |= 1; 214 return (0); 215 } 216 217 static int 218 g_part_mbr_dumpto(struct g_part_table *table, struct g_part_entry *baseentry) 219 { 220 struct g_part_mbr_entry *entry; 221 222 /* Allow dumping to a FreeBSD partition only. */ 223 entry = (struct g_part_mbr_entry *)baseentry; 224 return ((entry->ent.dp_typ == DOSPTYP_386BSD) ? 1 : 0); 225 } 226 227 static int 228 g_part_mbr_modify(struct g_part_table *basetable, 229 struct g_part_entry *baseentry, struct g_part_parms *gpp) 230 { 231 struct g_part_mbr_entry *entry; 232 233 if (gpp->gpp_parms & G_PART_PARM_LABEL) 234 return (EINVAL); 235 236 entry = (struct g_part_mbr_entry *)baseentry; 237 if (gpp->gpp_parms & G_PART_PARM_TYPE) 238 return (mbr_parse_type(gpp->gpp_type, &entry->ent.dp_typ)); 239 return (0); 240 } 241 242 static char * 243 g_part_mbr_name(struct g_part_table *table, struct g_part_entry *baseentry, 244 char *buf, size_t bufsz) 245 { 246 247 snprintf(buf, bufsz, "s%d", baseentry->gpe_index); 248 return (buf); 249 } 250 251 static int 252 g_part_mbr_probe(struct g_part_table *table, struct g_consumer *cp) 253 { 254 struct g_provider *pp; 255 u_char *buf; 256 int error, res; 257 258 pp = cp->provider; 259 260 /* Sanity-check the provider. */ 261 if (pp->sectorsize < MBRSIZE || pp->mediasize < pp->sectorsize) 262 return (ENOSPC); 263 264 /* Check that there's a MBR. */ 265 buf = g_read_data(cp, 0L, pp->sectorsize, &error); 266 if (buf == NULL) 267 return (error); 268 res = le16dec(buf + DOSMAGICOFFSET); 269 g_free(buf); 270 return ((res == DOSMAGIC) ? G_PART_PROBE_PRI_NORM : ENXIO); 271 } 272 273 static int 274 g_part_mbr_read(struct g_part_table *basetable, struct g_consumer *cp) 275 { 276 struct dos_partition ent; 277 struct g_provider *pp; 278 struct g_part_mbr_table *table; 279 struct g_part_mbr_entry *entry; 280 u_char *buf, *p; 281 off_t chs, msize; 282 u_int sectors, heads; 283 int error, index; 284 285 pp = cp->provider; 286 table = (struct g_part_mbr_table *)basetable; 287 msize = pp->mediasize / pp->sectorsize; 288 289 buf = g_read_data(cp, 0L, pp->sectorsize, &error); 290 if (buf == NULL) 291 return (error); 292 293 bcopy(buf, table->mbr, sizeof(table->mbr)); 294 for (index = NDOSPART - 1; index >= 0; index--) { 295 p = buf + DOSPARTOFF + index * DOSPARTSIZE; 296 ent.dp_flag = p[0]; 297 ent.dp_shd = p[1]; 298 ent.dp_ssect = p[2]; 299 ent.dp_scyl = p[3]; 300 ent.dp_typ = p[4]; 301 ent.dp_ehd = p[5]; 302 ent.dp_esect = p[6]; 303 ent.dp_ecyl = p[7]; 304 ent.dp_start = le32dec(p + 8); 305 ent.dp_size = le32dec(p + 12); 306 if (ent.dp_typ == 0 || ent.dp_typ == DOSPTYP_PMBR) 307 continue; 308 if (ent.dp_flag != 0 && ent.dp_flag != 0x80) 309 continue; 310 if (ent.dp_start == 0 || ent.dp_size == 0) 311 continue; 312 sectors = ent.dp_esect & 0x3f; 313 if (sectors > basetable->gpt_sectors && 314 !basetable->gpt_fixgeom) { 315 g_part_geometry_heads(msize, sectors, &chs, &heads); 316 if (chs != 0) { 317 basetable->gpt_sectors = sectors; 318 basetable->gpt_heads = heads; 319 } 320 } 321 if ((ent.dp_start % basetable->gpt_sectors) != 0) 322 printf("GEOM: %s: partition %d does not start on a " 323 "track boundary.\n", pp->name, index + 1); 324 if ((ent.dp_size % basetable->gpt_sectors) != 0) 325 printf("GEOM: %s: partition %d does not end on a " 326 "track boundary.\n", pp->name, index + 1); 327 328 entry = (struct g_part_mbr_entry *)g_part_new_entry(basetable, 329 index + 1, ent.dp_start, ent.dp_start + ent.dp_size - 1); 330 entry->ent = ent; 331 } 332 333 basetable->gpt_entries = NDOSPART; 334 basetable->gpt_first = basetable->gpt_sectors; 335 basetable->gpt_last = msize - (msize % basetable->gpt_sectors) - 1; 336 337 return (0); 338 } 339 340 static const char * 341 g_part_mbr_type(struct g_part_table *basetable, struct g_part_entry *baseentry, 342 char *buf, size_t bufsz) 343 { 344 struct g_part_mbr_entry *entry; 345 int type; 346 347 entry = (struct g_part_mbr_entry *)baseentry; 348 type = entry->ent.dp_typ; 349 if (type == DOSPTYP_386BSD) 350 return (g_part_alias_name(G_PART_ALIAS_FREEBSD)); 351 snprintf(buf, bufsz, "!%d", type); 352 return (buf); 353 } 354 355 static int 356 g_part_mbr_write(struct g_part_table *basetable, struct g_consumer *cp) 357 { 358 struct g_part_entry *baseentry; 359 struct g_part_mbr_entry *entry; 360 struct g_part_mbr_table *table; 361 u_char *p; 362 int error, index; 363 364 table = (struct g_part_mbr_table *)basetable; 365 baseentry = LIST_FIRST(&basetable->gpt_entry); 366 for (index = 1; index <= basetable->gpt_entries; index++) { 367 p = table->mbr + DOSPARTOFF + (index - 1) * DOSPARTSIZE; 368 entry = (baseentry != NULL && index == baseentry->gpe_index) 369 ? (struct g_part_mbr_entry *)baseentry : NULL; 370 if (entry != NULL && !baseentry->gpe_deleted) { 371 p[0] = entry->ent.dp_flag; 372 p[1] = entry->ent.dp_shd; 373 p[2] = entry->ent.dp_ssect; 374 p[3] = entry->ent.dp_scyl; 375 p[4] = entry->ent.dp_typ; 376 p[5] = entry->ent.dp_ehd; 377 p[6] = entry->ent.dp_esect; 378 p[7] = entry->ent.dp_ecyl; 379 le32enc(p + 8, entry->ent.dp_start); 380 le32enc(p + 12, entry->ent.dp_size); 381 } else 382 bzero(p, DOSPARTSIZE); 383 384 if (entry != NULL) 385 baseentry = LIST_NEXT(baseentry, gpe_entry); 386 } 387 388 error = g_write_data(cp, 0, table->mbr, cp->provider->sectorsize); 389 return (error); 390 } 391