1 /*- 2 * Copyright (c) 2006, 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/apm.h> 32 #include <sys/bio.h> 33 #include <sys/diskmbr.h> 34 #include <sys/endian.h> 35 #include <sys/kernel.h> 36 #include <sys/kobj.h> 37 #include <sys/limits.h> 38 #include <sys/lock.h> 39 #include <sys/malloc.h> 40 #include <sys/mutex.h> 41 #include <sys/queue.h> 42 #include <sys/sbuf.h> 43 #include <sys/systm.h> 44 #include <geom/geom.h> 45 #include <geom/part/g_part.h> 46 47 #include "g_part_if.h" 48 49 struct g_part_apm_table { 50 struct g_part_table base; 51 struct apm_ddr ddr; 52 struct apm_ent self; 53 }; 54 55 struct g_part_apm_entry { 56 struct g_part_entry base; 57 struct apm_ent ent; 58 }; 59 60 static int g_part_apm_add(struct g_part_table *, struct g_part_entry *, 61 struct g_part_parms *); 62 static int g_part_apm_create(struct g_part_table *, struct g_part_parms *); 63 static int g_part_apm_destroy(struct g_part_table *, struct g_part_parms *); 64 static int g_part_apm_dumpconf(struct g_part_table *, struct g_part_entry *, 65 struct sbuf *, const char *); 66 static int g_part_apm_dumpto(struct g_part_table *, struct g_part_entry *); 67 static int g_part_apm_modify(struct g_part_table *, struct g_part_entry *, 68 struct g_part_parms *); 69 static char *g_part_apm_name(struct g_part_table *, struct g_part_entry *, 70 char *, size_t); 71 static int g_part_apm_probe(struct g_part_table *, struct g_consumer *); 72 static int g_part_apm_read(struct g_part_table *, struct g_consumer *); 73 static const char *g_part_apm_type(struct g_part_table *, struct g_part_entry *, 74 char *, size_t); 75 static int g_part_apm_write(struct g_part_table *, struct g_consumer *); 76 77 static kobj_method_t g_part_apm_methods[] = { 78 KOBJMETHOD(g_part_add, g_part_apm_add), 79 KOBJMETHOD(g_part_create, g_part_apm_create), 80 KOBJMETHOD(g_part_destroy, g_part_apm_destroy), 81 KOBJMETHOD(g_part_dumpconf, g_part_apm_dumpconf), 82 KOBJMETHOD(g_part_dumpto, g_part_apm_dumpto), 83 KOBJMETHOD(g_part_modify, g_part_apm_modify), 84 KOBJMETHOD(g_part_name, g_part_apm_name), 85 KOBJMETHOD(g_part_probe, g_part_apm_probe), 86 KOBJMETHOD(g_part_read, g_part_apm_read), 87 KOBJMETHOD(g_part_type, g_part_apm_type), 88 KOBJMETHOD(g_part_write, g_part_apm_write), 89 { 0, 0 } 90 }; 91 92 static struct g_part_scheme g_part_apm_scheme = { 93 "APM", 94 g_part_apm_methods, 95 sizeof(struct g_part_apm_table), 96 .gps_entrysz = sizeof(struct g_part_apm_entry), 97 .gps_minent = 16, 98 .gps_maxent = INT_MAX, 99 }; 100 G_PART_SCHEME_DECLARE(g_part_apm); 101 102 static int 103 apm_parse_type(const char *type, char *buf, size_t bufsz) 104 { 105 const char *alias; 106 107 if (type[0] == '!') { 108 type++; 109 if (strlen(type) > bufsz) 110 return (EINVAL); 111 if (!strcmp(type, APM_ENT_TYPE_SELF) || 112 !strcmp(type, APM_ENT_TYPE_UNUSED)) 113 return (EINVAL); 114 strncpy(buf, type, bufsz); 115 return (0); 116 } 117 alias = g_part_alias_name(G_PART_ALIAS_FREEBSD); 118 if (!strcasecmp(type, alias)) { 119 strcpy(buf, APM_ENT_TYPE_FREEBSD); 120 return (0); 121 } 122 alias = g_part_alias_name(G_PART_ALIAS_FREEBSD_SWAP); 123 if (!strcasecmp(type, alias)) { 124 strcpy(buf, APM_ENT_TYPE_FREEBSD_SWAP); 125 return (0); 126 } 127 alias = g_part_alias_name(G_PART_ALIAS_FREEBSD_UFS); 128 if (!strcasecmp(type, alias)) { 129 strcpy(buf, APM_ENT_TYPE_FREEBSD_UFS); 130 return (0); 131 } 132 alias = g_part_alias_name(G_PART_ALIAS_FREEBSD_VINUM); 133 if (!strcasecmp(type, alias)) { 134 strcpy(buf, APM_ENT_TYPE_FREEBSD_VINUM); 135 return (0); 136 } 137 alias = g_part_alias_name(G_PART_ALIAS_FREEBSD_ZFS); 138 if (!strcasecmp(type, alias)) { 139 strcpy(buf, APM_ENT_TYPE_FREEBSD_ZFS); 140 return (0); 141 } 142 return (EINVAL); 143 } 144 145 static int 146 apm_read_ent(struct g_consumer *cp, uint32_t blk, struct apm_ent *ent) 147 { 148 struct g_provider *pp; 149 char *buf; 150 int error; 151 152 pp = cp->provider; 153 buf = g_read_data(cp, pp->sectorsize * blk, pp->sectorsize, &error); 154 if (buf == NULL) 155 return (error); 156 ent->ent_sig = be16dec(buf); 157 ent->ent_pmblkcnt = be32dec(buf + 4); 158 ent->ent_start = be32dec(buf + 8); 159 ent->ent_size = be32dec(buf + 12); 160 bcopy(buf + 16, ent->ent_name, sizeof(ent->ent_name)); 161 bcopy(buf + 48, ent->ent_type, sizeof(ent->ent_type)); 162 g_free(buf); 163 return (0); 164 } 165 166 static int 167 g_part_apm_add(struct g_part_table *basetable, struct g_part_entry *baseentry, 168 struct g_part_parms *gpp) 169 { 170 struct g_part_apm_entry *entry; 171 struct g_part_apm_table *table; 172 int error; 173 174 entry = (struct g_part_apm_entry *)baseentry; 175 table = (struct g_part_apm_table *)basetable; 176 entry->ent.ent_sig = APM_ENT_SIG; 177 entry->ent.ent_pmblkcnt = table->self.ent_pmblkcnt; 178 entry->ent.ent_start = gpp->gpp_start; 179 entry->ent.ent_size = gpp->gpp_size; 180 if (baseentry->gpe_deleted) { 181 bzero(entry->ent.ent_type, sizeof(entry->ent.ent_type)); 182 bzero(entry->ent.ent_name, sizeof(entry->ent.ent_name)); 183 } 184 error = apm_parse_type(gpp->gpp_type, entry->ent.ent_type, 185 sizeof(entry->ent.ent_type)); 186 if (error) 187 return (error); 188 if (gpp->gpp_parms & G_PART_PARM_LABEL) { 189 if (strlen(gpp->gpp_label) > sizeof(entry->ent.ent_name)) 190 return (EINVAL); 191 strncpy(entry->ent.ent_name, gpp->gpp_label, 192 sizeof(entry->ent.ent_name)); 193 } 194 return (0); 195 } 196 197 static int 198 g_part_apm_create(struct g_part_table *basetable, struct g_part_parms *gpp) 199 { 200 struct g_provider *pp; 201 struct g_part_apm_table *table; 202 203 table = (struct g_part_apm_table *)basetable; 204 pp = gpp->gpp_provider; 205 if (pp->sectorsize != 512 || 206 pp->mediasize < (2 + 2 * basetable->gpt_entries) * pp->sectorsize) 207 return (ENOSPC); 208 209 basetable->gpt_first = 2 + basetable->gpt_entries; 210 basetable->gpt_last = (pp->mediasize / pp->sectorsize) - 1; 211 212 table->ddr.ddr_sig = APM_DDR_SIG; 213 table->ddr.ddr_blksize = pp->sectorsize; 214 table->ddr.ddr_blkcount = basetable->gpt_last + 1; 215 216 table->self.ent_sig = APM_ENT_SIG; 217 table->self.ent_pmblkcnt = basetable->gpt_entries + 1; 218 table->self.ent_start = 1; 219 table->self.ent_size = table->self.ent_pmblkcnt; 220 strcpy(table->self.ent_name, "Apple"); 221 strcpy(table->self.ent_type, APM_ENT_TYPE_SELF); 222 return (0); 223 } 224 225 static int 226 g_part_apm_destroy(struct g_part_table *basetable, struct g_part_parms *gpp) 227 { 228 229 /* Wipe the first 2 sectors to clear the partitioning. */ 230 basetable->gpt_smhead |= 3; 231 return (0); 232 } 233 234 static int 235 g_part_apm_dumpconf(struct g_part_table *table, struct g_part_entry *baseentry, 236 struct sbuf *sb, const char *indent) 237 { 238 struct g_part_apm_entry *entry; 239 240 if (indent != NULL) 241 return (0); 242 243 entry = (struct g_part_apm_entry *)baseentry; 244 sbuf_printf(sb, " xs APPLE xt %s", entry->ent.ent_type); 245 return (0); 246 } 247 248 static int 249 g_part_apm_dumpto(struct g_part_table *table, struct g_part_entry *baseentry) 250 { 251 struct g_part_apm_entry *entry; 252 253 entry = (struct g_part_apm_entry *)baseentry; 254 return ((!strcmp(entry->ent.ent_type, APM_ENT_TYPE_FREEBSD_SWAP)) 255 ? 1 : 0); 256 } 257 258 static int 259 g_part_apm_modify(struct g_part_table *basetable, 260 struct g_part_entry *baseentry, struct g_part_parms *gpp) 261 { 262 struct g_part_apm_entry *entry; 263 int error; 264 265 entry = (struct g_part_apm_entry *)baseentry; 266 if (gpp->gpp_parms & G_PART_PARM_LABEL) { 267 if (strlen(gpp->gpp_label) > sizeof(entry->ent.ent_name)) 268 return (EINVAL); 269 } 270 if (gpp->gpp_parms & G_PART_PARM_TYPE) { 271 error = apm_parse_type(gpp->gpp_type, entry->ent.ent_type, 272 sizeof(entry->ent.ent_type)); 273 if (error) 274 return (error); 275 } 276 if (gpp->gpp_parms & G_PART_PARM_LABEL) { 277 strncpy(entry->ent.ent_name, gpp->gpp_label, 278 sizeof(entry->ent.ent_name)); 279 } 280 return (0); 281 } 282 283 static char * 284 g_part_apm_name(struct g_part_table *table, struct g_part_entry *baseentry, 285 char *buf, size_t bufsz) 286 { 287 288 snprintf(buf, bufsz, "s%d", baseentry->gpe_index + 1); 289 return (buf); 290 } 291 292 static int 293 g_part_apm_probe(struct g_part_table *basetable, struct g_consumer *cp) 294 { 295 struct g_provider *pp; 296 struct g_part_apm_table *table; 297 char *buf; 298 int error; 299 300 /* We don't nest, which means that our depth should be 0. */ 301 if (basetable->gpt_depth != 0) 302 return (ENXIO); 303 304 table = (struct g_part_apm_table *)basetable; 305 pp = cp->provider; 306 307 /* Sanity-check the provider. */ 308 if (pp->mediasize < 4 * pp->sectorsize) 309 return (ENOSPC); 310 311 /* Check that there's a Driver Descriptor Record (DDR). */ 312 buf = g_read_data(cp, 0L, pp->sectorsize, &error); 313 if (buf == NULL) 314 return (error); 315 table->ddr.ddr_sig = be16dec(buf); 316 table->ddr.ddr_blksize = be16dec(buf + 2); 317 table->ddr.ddr_blkcount = be32dec(buf + 4); 318 g_free(buf); 319 if (table->ddr.ddr_sig != APM_DDR_SIG) 320 return (ENXIO); 321 if (table->ddr.ddr_blksize != pp->sectorsize) 322 return (ENXIO); 323 324 /* Check that there's a Partition Map. */ 325 error = apm_read_ent(cp, 1, &table->self); 326 if (error) 327 return (error); 328 if (table->self.ent_sig != APM_ENT_SIG) 329 return (ENXIO); 330 if (strcmp(table->self.ent_type, APM_ENT_TYPE_SELF)) 331 return (ENXIO); 332 if (table->self.ent_pmblkcnt >= table->ddr.ddr_blkcount) 333 return (ENXIO); 334 return (G_PART_PROBE_PRI_NORM); 335 } 336 337 static int 338 g_part_apm_read(struct g_part_table *basetable, struct g_consumer *cp) 339 { 340 struct apm_ent ent; 341 struct g_part_apm_entry *entry; 342 struct g_part_apm_table *table; 343 int error, index; 344 345 table = (struct g_part_apm_table *)basetable; 346 347 basetable->gpt_first = table->self.ent_pmblkcnt + 1; 348 basetable->gpt_last = table->ddr.ddr_blkcount - 1; 349 basetable->gpt_entries = table->self.ent_pmblkcnt - 1; 350 351 for (index = table->self.ent_pmblkcnt - 1; index > 0; index--) { 352 error = apm_read_ent(cp, index + 1, &ent); 353 if (error) 354 continue; 355 if (!strcmp(ent.ent_type, APM_ENT_TYPE_UNUSED)) 356 continue; 357 entry = (struct g_part_apm_entry *)g_part_new_entry(basetable, 358 index, ent.ent_start, ent.ent_start + ent.ent_size - 1); 359 entry->ent = ent; 360 } 361 362 return (0); 363 } 364 365 static const char * 366 g_part_apm_type(struct g_part_table *basetable, struct g_part_entry *baseentry, 367 char *buf, size_t bufsz) 368 { 369 struct g_part_apm_entry *entry; 370 const char *type; 371 size_t len; 372 373 entry = (struct g_part_apm_entry *)baseentry; 374 type = entry->ent.ent_type; 375 if (!strcmp(type, APM_ENT_TYPE_FREEBSD)) 376 return (g_part_alias_name(G_PART_ALIAS_FREEBSD)); 377 if (!strcmp(type, APM_ENT_TYPE_FREEBSD_SWAP)) 378 return (g_part_alias_name(G_PART_ALIAS_FREEBSD_SWAP)); 379 if (!strcmp(type, APM_ENT_TYPE_FREEBSD_UFS)) 380 return (g_part_alias_name(G_PART_ALIAS_FREEBSD_UFS)); 381 if (!strcmp(type, APM_ENT_TYPE_FREEBSD_VINUM)) 382 return (g_part_alias_name(G_PART_ALIAS_FREEBSD_VINUM)); 383 if (!strcmp(type, APM_ENT_TYPE_FREEBSD_ZFS)) 384 return (g_part_alias_name(G_PART_ALIAS_FREEBSD_ZFS)); 385 buf[0] = '!'; 386 len = MIN(sizeof(entry->ent.ent_type), bufsz - 2); 387 bcopy(type, buf + 1, len); 388 buf[len + 1] = '\0'; 389 return (buf); 390 } 391 392 static int 393 g_part_apm_write(struct g_part_table *basetable, struct g_consumer *cp) 394 { 395 char buf[512]; 396 struct g_part_entry *baseentry; 397 struct g_part_apm_entry *entry; 398 struct g_part_apm_table *table; 399 int error, index; 400 401 table = (struct g_part_apm_table *)basetable; 402 bzero(buf, sizeof(buf)); 403 404 /* Write the DDR and 'self' entry only when we're newly created. */ 405 if (basetable->gpt_created) { 406 be16enc(buf, table->ddr.ddr_sig); 407 be16enc(buf + 2, table->ddr.ddr_blksize); 408 be32enc(buf + 4, table->ddr.ddr_blkcount); 409 error = g_write_data(cp, 0, buf, sizeof(buf)); 410 if (error) 411 return (error); 412 } 413 414 be16enc(buf, table->self.ent_sig); 415 be16enc(buf + 2, 0); 416 be32enc(buf + 4, table->self.ent_pmblkcnt); 417 418 if (basetable->gpt_created) { 419 be32enc(buf + 8, table->self.ent_start); 420 be32enc(buf + 12, table->self.ent_size); 421 bcopy(table->self.ent_name, buf + 16, 422 sizeof(table->self.ent_name)); 423 bcopy(table->self.ent_type, buf + 48, 424 sizeof(table->self.ent_type)); 425 error = g_write_data(cp, 512, buf, sizeof(buf)); 426 if (error) 427 return (error); 428 } 429 430 baseentry = LIST_FIRST(&basetable->gpt_entry); 431 for (index = 1; index <= basetable->gpt_entries; index++) { 432 entry = (baseentry != NULL && index == baseentry->gpe_index) 433 ? (struct g_part_apm_entry *)baseentry : NULL; 434 if (entry != NULL && !baseentry->gpe_deleted) { 435 be32enc(buf + 8, entry->ent.ent_start); 436 be32enc(buf + 12, entry->ent.ent_size); 437 bcopy(entry->ent.ent_name, buf + 16, 438 sizeof(entry->ent.ent_name)); 439 bcopy(entry->ent.ent_type, buf + 48, 440 sizeof(entry->ent.ent_type)); 441 } else { 442 bzero(buf + 8, 4 + 4 + 32 + 32); 443 strcpy(buf + 48, APM_ENT_TYPE_UNUSED); 444 } 445 error = g_write_data(cp, (index + 1) * 512, buf, sizeof(buf)); 446 if (error) 447 return (error); 448 if (entry != NULL) 449 baseentry = LIST_NEXT(baseentry, gpe_entry); 450 } 451 452 return (0); 453 } 454