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