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