g_part_ebr.c revision 251588
1188354Smarcel/*- 2188354Smarcel * Copyright (c) 2007-2009 Marcel Moolenaar 3188354Smarcel * All rights reserved. 4188354Smarcel * 5188354Smarcel * Redistribution and use in source and binary forms, with or without 6188354Smarcel * modification, are permitted provided that the following conditions 7188354Smarcel * are met: 8188354Smarcel * 9188354Smarcel * 1. Redistributions of source code must retain the above copyright 10188354Smarcel * notice, this list of conditions and the following disclaimer. 11188354Smarcel * 2. Redistributions in binary form must reproduce the above copyright 12188354Smarcel * notice, this list of conditions and the following disclaimer in the 13188354Smarcel * documentation and/or other materials provided with the distribution. 14188354Smarcel * 15188354Smarcel * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 16188354Smarcel * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 17188354Smarcel * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 18188354Smarcel * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 19188354Smarcel * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 20188354Smarcel * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 21188354Smarcel * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 22188354Smarcel * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23188354Smarcel * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 24188354Smarcel * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25188354Smarcel */ 26188354Smarcel 27191130Smarcel#include "opt_geom.h" 28191130Smarcel 29188354Smarcel#include <sys/cdefs.h> 30188354Smarcel__FBSDID("$FreeBSD: head/sys/geom/part/g_part_ebr.c 251588 2013-06-09 23:34:26Z marcel $"); 31188354Smarcel 32188354Smarcel#include <sys/param.h> 33188354Smarcel#include <sys/bio.h> 34188354Smarcel#include <sys/diskmbr.h> 35188354Smarcel#include <sys/endian.h> 36188354Smarcel#include <sys/kernel.h> 37188354Smarcel#include <sys/kobj.h> 38188354Smarcel#include <sys/limits.h> 39188354Smarcel#include <sys/lock.h> 40188354Smarcel#include <sys/malloc.h> 41188354Smarcel#include <sys/mutex.h> 42188354Smarcel#include <sys/queue.h> 43188354Smarcel#include <sys/sbuf.h> 44188354Smarcel#include <sys/systm.h> 45219029Snetchild#include <sys/sysctl.h> 46188354Smarcel#include <geom/geom.h> 47188354Smarcel#include <geom/part/g_part.h> 48188354Smarcel 49188354Smarcel#include "g_part_if.h" 50188354Smarcel 51219029SnetchildFEATURE(geom_part_ebr, 52219029Snetchild "GEOM partitioning class for extended boot records support"); 53219029Snetchild#if defined(GEOM_PART_EBR_COMPAT) 54219029SnetchildFEATURE(geom_part_ebr_compat, 55219029Snetchild "GEOM EBR partitioning class: backward-compatible partition names"); 56219029Snetchild#endif 57219029Snetchild 58188354Smarcel#define EBRSIZE 512 59188354Smarcel 60188354Smarcelstruct g_part_ebr_table { 61188354Smarcel struct g_part_table base; 62223594Sae#ifndef GEOM_PART_EBR_COMPAT 63223594Sae u_char ebr[EBRSIZE]; 64223594Sae#endif 65188354Smarcel}; 66188354Smarcel 67188354Smarcelstruct g_part_ebr_entry { 68188354Smarcel struct g_part_entry base; 69188354Smarcel struct dos_partition ent; 70188354Smarcel}; 71188354Smarcel 72188354Smarcelstatic int g_part_ebr_add(struct g_part_table *, struct g_part_entry *, 73188354Smarcel struct g_part_parms *); 74188354Smarcelstatic int g_part_ebr_create(struct g_part_table *, struct g_part_parms *); 75188354Smarcelstatic int g_part_ebr_destroy(struct g_part_table *, struct g_part_parms *); 76188354Smarcelstatic void g_part_ebr_dumpconf(struct g_part_table *, struct g_part_entry *, 77188354Smarcel struct sbuf *, const char *); 78188354Smarcelstatic int g_part_ebr_dumpto(struct g_part_table *, struct g_part_entry *); 79191130Smarcel#if defined(GEOM_PART_EBR_COMPAT) 80191130Smarcelstatic void g_part_ebr_fullname(struct g_part_table *, struct g_part_entry *, 81191130Smarcel struct sbuf *, const char *); 82191130Smarcel#endif 83188354Smarcelstatic int g_part_ebr_modify(struct g_part_table *, struct g_part_entry *, 84188354Smarcel struct g_part_parms *); 85188354Smarcelstatic const char *g_part_ebr_name(struct g_part_table *, struct g_part_entry *, 86188354Smarcel char *, size_t); 87188667Smarcelstatic int g_part_ebr_precheck(struct g_part_table *, enum g_part_ctl, 88188667Smarcel struct g_part_parms *); 89188354Smarcelstatic int g_part_ebr_probe(struct g_part_table *, struct g_consumer *); 90188354Smarcelstatic int g_part_ebr_read(struct g_part_table *, struct g_consumer *); 91188354Smarcelstatic int g_part_ebr_setunset(struct g_part_table *, struct g_part_entry *, 92188354Smarcel const char *, unsigned int); 93188354Smarcelstatic const char *g_part_ebr_type(struct g_part_table *, struct g_part_entry *, 94188354Smarcel char *, size_t); 95188354Smarcelstatic int g_part_ebr_write(struct g_part_table *, struct g_consumer *); 96188354Smarcel 97188354Smarcelstatic kobj_method_t g_part_ebr_methods[] = { 98188354Smarcel KOBJMETHOD(g_part_add, g_part_ebr_add), 99188354Smarcel KOBJMETHOD(g_part_create, g_part_ebr_create), 100188354Smarcel KOBJMETHOD(g_part_destroy, g_part_ebr_destroy), 101188354Smarcel KOBJMETHOD(g_part_dumpconf, g_part_ebr_dumpconf), 102188354Smarcel KOBJMETHOD(g_part_dumpto, g_part_ebr_dumpto), 103191130Smarcel#if defined(GEOM_PART_EBR_COMPAT) 104191130Smarcel KOBJMETHOD(g_part_fullname, g_part_ebr_fullname), 105191130Smarcel#endif 106188354Smarcel KOBJMETHOD(g_part_modify, g_part_ebr_modify), 107188354Smarcel KOBJMETHOD(g_part_name, g_part_ebr_name), 108188667Smarcel KOBJMETHOD(g_part_precheck, g_part_ebr_precheck), 109188354Smarcel KOBJMETHOD(g_part_probe, g_part_ebr_probe), 110188354Smarcel KOBJMETHOD(g_part_read, g_part_ebr_read), 111188354Smarcel KOBJMETHOD(g_part_setunset, g_part_ebr_setunset), 112188354Smarcel KOBJMETHOD(g_part_type, g_part_ebr_type), 113188354Smarcel KOBJMETHOD(g_part_write, g_part_ebr_write), 114188354Smarcel { 0, 0 } 115188354Smarcel}; 116188354Smarcel 117188354Smarcelstatic struct g_part_scheme g_part_ebr_scheme = { 118188354Smarcel "EBR", 119188354Smarcel g_part_ebr_methods, 120188354Smarcel sizeof(struct g_part_ebr_table), 121188354Smarcel .gps_entrysz = sizeof(struct g_part_ebr_entry), 122188354Smarcel .gps_minent = 1, 123188354Smarcel .gps_maxent = INT_MAX, 124188354Smarcel}; 125188354SmarcelG_PART_SCHEME_DECLARE(g_part_ebr); 126188354Smarcel 127218014Saestatic struct g_part_ebr_alias { 128218014Sae u_char typ; 129218014Sae int alias; 130218014Sae} ebr_alias_match[] = { 131218014Sae { DOSPTYP_386BSD, G_PART_ALIAS_FREEBSD }, 132218014Sae { DOSPTYP_NTFS, G_PART_ALIAS_MS_NTFS }, 133218014Sae { DOSPTYP_FAT32, G_PART_ALIAS_MS_FAT32 }, 134218014Sae { DOSPTYP_LINSWP, G_PART_ALIAS_LINUX_SWAP }, 135218014Sae { DOSPTYP_LINUX, G_PART_ALIAS_LINUX_DATA }, 136218014Sae { DOSPTYP_LINLVM, G_PART_ALIAS_LINUX_LVM }, 137218014Sae { DOSPTYP_LINRAID, G_PART_ALIAS_LINUX_RAID }, 138218014Sae}; 139218014Sae 140188667Smarcelstatic void ebr_set_chs(struct g_part_table *, uint32_t, u_char *, u_char *, 141188667Smarcel u_char *); 142188667Smarcel 143188354Smarcelstatic void 144188354Smarcelebr_entry_decode(const char *p, struct dos_partition *ent) 145188354Smarcel{ 146188354Smarcel ent->dp_flag = p[0]; 147188354Smarcel ent->dp_shd = p[1]; 148188354Smarcel ent->dp_ssect = p[2]; 149188354Smarcel ent->dp_scyl = p[3]; 150188354Smarcel ent->dp_typ = p[4]; 151188354Smarcel ent->dp_ehd = p[5]; 152188354Smarcel ent->dp_esect = p[6]; 153188354Smarcel ent->dp_ecyl = p[7]; 154188354Smarcel ent->dp_start = le32dec(p + 8); 155188354Smarcel ent->dp_size = le32dec(p + 12); 156188354Smarcel} 157188354Smarcel 158188667Smarcelstatic void 159188667Smarcelebr_entry_link(struct g_part_table *table, uint32_t start, uint32_t end, 160188667Smarcel u_char *buf) 161188667Smarcel{ 162188667Smarcel 163188667Smarcel buf[0] = 0 /* dp_flag */; 164188667Smarcel ebr_set_chs(table, start, &buf[3] /* dp_scyl */, &buf[1] /* dp_shd */, 165188667Smarcel &buf[2] /* dp_ssect */); 166188667Smarcel buf[4] = 5 /* dp_typ */; 167188667Smarcel ebr_set_chs(table, end, &buf[7] /* dp_ecyl */, &buf[5] /* dp_ehd */, 168188667Smarcel &buf[6] /* dp_esect */); 169188667Smarcel le32enc(buf + 8, start); 170188667Smarcel le32enc(buf + 12, end - start + 1); 171188667Smarcel} 172188667Smarcel 173188354Smarcelstatic int 174188667Smarcelebr_parse_type(const char *type, u_char *dp_typ) 175188667Smarcel{ 176188667Smarcel const char *alias; 177188667Smarcel char *endp; 178188667Smarcel long lt; 179218014Sae int i; 180188667Smarcel 181188667Smarcel if (type[0] == '!') { 182188667Smarcel lt = strtol(type + 1, &endp, 0); 183188667Smarcel if (type[1] == '\0' || *endp != '\0' || lt <= 0 || lt >= 256) 184188667Smarcel return (EINVAL); 185188667Smarcel *dp_typ = (u_char)lt; 186188667Smarcel return (0); 187188667Smarcel } 188218014Sae for (i = 0; 189218014Sae i < sizeof(ebr_alias_match) / sizeof(ebr_alias_match[0]); i++) { 190218014Sae alias = g_part_alias_name(ebr_alias_match[i].alias); 191218014Sae if (strcasecmp(type, alias) == 0) { 192218014Sae *dp_typ = ebr_alias_match[i].typ; 193218014Sae return (0); 194218014Sae } 195188667Smarcel } 196188667Smarcel return (EINVAL); 197188667Smarcel} 198188667Smarcel 199218014Sae 200188667Smarcelstatic void 201188667Smarcelebr_set_chs(struct g_part_table *table, uint32_t lba, u_char *cylp, u_char *hdp, 202188667Smarcel u_char *secp) 203188667Smarcel{ 204188667Smarcel uint32_t cyl, hd, sec; 205188667Smarcel 206188667Smarcel sec = lba % table->gpt_sectors + 1; 207188667Smarcel lba /= table->gpt_sectors; 208188667Smarcel hd = lba % table->gpt_heads; 209188667Smarcel lba /= table->gpt_heads; 210188667Smarcel cyl = lba; 211188667Smarcel if (cyl > 1023) 212188667Smarcel sec = hd = cyl = ~0; 213188667Smarcel 214188667Smarcel *cylp = cyl & 0xff; 215188667Smarcel *hdp = hd & 0xff; 216188667Smarcel *secp = (sec & 0x3f) | ((cyl >> 2) & 0xc0); 217188667Smarcel} 218188667Smarcel 219188667Smarcelstatic int 220188354Smarcelg_part_ebr_add(struct g_part_table *basetable, struct g_part_entry *baseentry, 221188354Smarcel struct g_part_parms *gpp) 222188354Smarcel{ 223188667Smarcel struct g_geom *gp; 224188667Smarcel struct g_provider *pp; 225188667Smarcel struct g_part_ebr_entry *entry; 226188667Smarcel uint32_t start, size, sectors; 227188354Smarcel 228188667Smarcel if (gpp->gpp_parms & G_PART_PARM_LABEL) 229188667Smarcel return (EINVAL); 230188667Smarcel 231188667Smarcel gp = basetable->gpt_gp; 232188667Smarcel pp = LIST_FIRST(&gp->consumer)->provider; 233188667Smarcel sectors = basetable->gpt_sectors; 234188667Smarcel 235188667Smarcel entry = (struct g_part_ebr_entry *)baseentry; 236188667Smarcel 237188667Smarcel start = gpp->gpp_start; 238188667Smarcel size = gpp->gpp_size; 239188667Smarcel if (size < 2 * sectors) 240188667Smarcel return (EINVAL); 241188667Smarcel if (start % sectors) { 242188667Smarcel size = size - sectors + (start % sectors); 243188667Smarcel start = start - (start % sectors) + sectors; 244188667Smarcel } 245188667Smarcel if (size % sectors) 246188667Smarcel size = size - (size % sectors); 247188667Smarcel if (size < 2 * sectors) 248188667Smarcel return (EINVAL); 249188667Smarcel 250188667Smarcel if (baseentry->gpe_deleted) 251188667Smarcel bzero(&entry->ent, sizeof(entry->ent)); 252188667Smarcel 253213769Srpaulo KASSERT(baseentry->gpe_start <= start, ("%s", __func__)); 254213769Srpaulo KASSERT(baseentry->gpe_end >= start + size - 1, ("%s", __func__)); 255188899Smarcel baseentry->gpe_index = (start / sectors) + 1; 256188667Smarcel baseentry->gpe_offset = (off_t)(start + sectors) * pp->sectorsize; 257188667Smarcel baseentry->gpe_start = start; 258188667Smarcel baseentry->gpe_end = start + size - 1; 259188667Smarcel entry->ent.dp_start = sectors; 260188667Smarcel entry->ent.dp_size = size - sectors; 261188667Smarcel ebr_set_chs(basetable, entry->ent.dp_start, &entry->ent.dp_scyl, 262188667Smarcel &entry->ent.dp_shd, &entry->ent.dp_ssect); 263188667Smarcel ebr_set_chs(basetable, baseentry->gpe_end, &entry->ent.dp_ecyl, 264188667Smarcel &entry->ent.dp_ehd, &entry->ent.dp_esect); 265188667Smarcel return (ebr_parse_type(gpp->gpp_type, &entry->ent.dp_typ)); 266188354Smarcel} 267188354Smarcel 268188354Smarcelstatic int 269188354Smarcelg_part_ebr_create(struct g_part_table *basetable, struct g_part_parms *gpp) 270188354Smarcel{ 271231754Sae char type[64]; 272188667Smarcel struct g_consumer *cp; 273188667Smarcel struct g_provider *pp; 274190535Smarcel uint32_t msize; 275188667Smarcel int error; 276188354Smarcel 277188667Smarcel pp = gpp->gpp_provider; 278188667Smarcel 279188667Smarcel if (pp->sectorsize < EBRSIZE) 280188667Smarcel return (ENOSPC); 281188667Smarcel if (pp->sectorsize > 4096) 282188667Smarcel return (ENXIO); 283188667Smarcel 284188667Smarcel /* Check that we have a parent and that it's a MBR. */ 285188667Smarcel if (basetable->gpt_depth == 0) 286188667Smarcel return (ENXIO); 287188667Smarcel cp = LIST_FIRST(&pp->consumers); 288231754Sae error = g_getattr("PART::scheme", cp, &type); 289231754Sae if (error != 0) 290188667Smarcel return (error); 291231754Sae if (strcmp(type, "MBR") != 0) 292188667Smarcel return (ENXIO); 293231754Sae error = g_getattr("PART::type", cp, &type); 294231754Sae if (error != 0) 295231754Sae return (error); 296231754Sae if (strcmp(type, "ebr") != 0) 297231754Sae return (ENXIO); 298188667Smarcel 299221656Sae msize = MIN(pp->mediasize / pp->sectorsize, UINT32_MAX); 300190535Smarcel basetable->gpt_first = 0; 301190535Smarcel basetable->gpt_last = msize - 1; 302188899Smarcel basetable->gpt_entries = msize / basetable->gpt_sectors; 303188667Smarcel return (0); 304188354Smarcel} 305188354Smarcel 306188354Smarcelstatic int 307188354Smarcelg_part_ebr_destroy(struct g_part_table *basetable, struct g_part_parms *gpp) 308188354Smarcel{ 309188354Smarcel 310188354Smarcel /* Wipe the first sector to clear the partitioning. */ 311188354Smarcel basetable->gpt_smhead |= 1; 312188354Smarcel return (0); 313188354Smarcel} 314188354Smarcel 315188354Smarcelstatic void 316188354Smarcelg_part_ebr_dumpconf(struct g_part_table *table, struct g_part_entry *baseentry, 317188354Smarcel struct sbuf *sb, const char *indent) 318188354Smarcel{ 319188354Smarcel struct g_part_ebr_entry *entry; 320188354Smarcel 321188354Smarcel entry = (struct g_part_ebr_entry *)baseentry; 322188354Smarcel if (indent == NULL) { 323188354Smarcel /* conftxt: libdisk compatibility */ 324188354Smarcel sbuf_printf(sb, " xs MBREXT xt %u", entry->ent.dp_typ); 325188354Smarcel } else if (entry != NULL) { 326188354Smarcel /* confxml: partition entry information */ 327188354Smarcel sbuf_printf(sb, "%s<rawtype>%u</rawtype>\n", indent, 328188354Smarcel entry->ent.dp_typ); 329188354Smarcel if (entry->ent.dp_flag & 0x80) 330188354Smarcel sbuf_printf(sb, "%s<attrib>active</attrib>\n", indent); 331188354Smarcel } else { 332188354Smarcel /* confxml: scheme information */ 333188354Smarcel } 334188354Smarcel} 335188354Smarcel 336188354Smarcelstatic int 337188354Smarcelg_part_ebr_dumpto(struct g_part_table *table, struct g_part_entry *baseentry) 338188354Smarcel{ 339188354Smarcel struct g_part_ebr_entry *entry; 340188354Smarcel 341230064Struckman /* Allow dumping to a FreeBSD partition or Linux swap partition only. */ 342188354Smarcel entry = (struct g_part_ebr_entry *)baseentry; 343230064Struckman return ((entry->ent.dp_typ == DOSPTYP_386BSD || 344230064Struckman entry->ent.dp_typ == DOSPTYP_LINSWP) ? 1 : 0); 345188354Smarcel} 346188354Smarcel 347191130Smarcel#if defined(GEOM_PART_EBR_COMPAT) 348191130Smarcelstatic void 349191130Smarcelg_part_ebr_fullname(struct g_part_table *table, struct g_part_entry *entry, 350191130Smarcel struct sbuf *sb, const char *pfx) 351191130Smarcel{ 352191130Smarcel struct g_part_entry *iter; 353191130Smarcel u_int idx; 354191130Smarcel 355191130Smarcel idx = 5; 356191130Smarcel LIST_FOREACH(iter, &table->gpt_entry, gpe_entry) { 357191130Smarcel if (iter == entry) 358191130Smarcel break; 359191130Smarcel idx++; 360191130Smarcel } 361191134Smarcel sbuf_printf(sb, "%.*s%u", (int)strlen(pfx) - 1, pfx, idx); 362191130Smarcel} 363191130Smarcel#endif 364191130Smarcel 365188354Smarcelstatic int 366188354Smarcelg_part_ebr_modify(struct g_part_table *basetable, 367188354Smarcel struct g_part_entry *baseentry, struct g_part_parms *gpp) 368188354Smarcel{ 369188667Smarcel struct g_part_ebr_entry *entry; 370188354Smarcel 371188667Smarcel if (gpp->gpp_parms & G_PART_PARM_LABEL) 372188667Smarcel return (EINVAL); 373188667Smarcel 374188667Smarcel entry = (struct g_part_ebr_entry *)baseentry; 375188667Smarcel if (gpp->gpp_parms & G_PART_PARM_TYPE) 376188667Smarcel return (ebr_parse_type(gpp->gpp_type, &entry->ent.dp_typ)); 377188667Smarcel return (0); 378188354Smarcel} 379188354Smarcel 380188354Smarcelstatic const char * 381188354Smarcelg_part_ebr_name(struct g_part_table *table, struct g_part_entry *entry, 382188354Smarcel char *buf, size_t bufsz) 383188354Smarcel{ 384188354Smarcel 385190849Smarcel snprintf(buf, bufsz, "+%08u", entry->gpe_index); 386188354Smarcel return (buf); 387188354Smarcel} 388188354Smarcel 389188354Smarcelstatic int 390188667Smarcelg_part_ebr_precheck(struct g_part_table *table, enum g_part_ctl req, 391188667Smarcel struct g_part_parms *gpp) 392188667Smarcel{ 393191130Smarcel#if defined(GEOM_PART_EBR_COMPAT) 394216755Sae if (req == G_PART_CTL_DESTROY) 395216755Sae return (0); 396191130Smarcel return (ECANCELED); 397191130Smarcel#else 398188667Smarcel /* 399188667Smarcel * The index is a function of the start of the partition. 400188667Smarcel * This is not something the user can override, nor is it 401188667Smarcel * something the common code will do right. We can set the 402188667Smarcel * index now so that we get what we need. 403188667Smarcel */ 404188667Smarcel if (req == G_PART_CTL_ADD) 405188667Smarcel gpp->gpp_index = (gpp->gpp_start / table->gpt_sectors) + 1; 406188667Smarcel return (0); 407191130Smarcel#endif 408188667Smarcel} 409188667Smarcel 410188667Smarcelstatic int 411188354Smarcelg_part_ebr_probe(struct g_part_table *table, struct g_consumer *cp) 412188354Smarcel{ 413231754Sae char type[64]; 414188354Smarcel struct g_provider *pp; 415188354Smarcel u_char *buf, *p; 416216754Sae int error, index, res; 417188354Smarcel uint16_t magic; 418188354Smarcel 419188354Smarcel pp = cp->provider; 420188354Smarcel 421188354Smarcel /* Sanity-check the provider. */ 422188354Smarcel if (pp->sectorsize < EBRSIZE || pp->mediasize < pp->sectorsize) 423188354Smarcel return (ENOSPC); 424188354Smarcel if (pp->sectorsize > 4096) 425188354Smarcel return (ENXIO); 426188354Smarcel 427188354Smarcel /* Check that we have a parent and that it's a MBR. */ 428188354Smarcel if (table->gpt_depth == 0) 429188354Smarcel return (ENXIO); 430231754Sae error = g_getattr("PART::scheme", cp, &type); 431231754Sae if (error != 0) 432188354Smarcel return (error); 433231754Sae if (strcmp(type, "MBR") != 0) 434188354Smarcel return (ENXIO); 435231754Sae /* Check that partition has type DOSPTYP_EBR. */ 436231754Sae error = g_getattr("PART::type", cp, &type); 437231754Sae if (error != 0) 438231754Sae return (error); 439231754Sae if (strcmp(type, "ebr") != 0) 440231754Sae return (ENXIO); 441188354Smarcel 442188354Smarcel /* Check that there's a EBR. */ 443188354Smarcel buf = g_read_data(cp, 0L, pp->sectorsize, &error); 444188354Smarcel if (buf == NULL) 445188354Smarcel return (error); 446188354Smarcel 447188354Smarcel /* We goto out on mismatch. */ 448188354Smarcel res = ENXIO; 449188354Smarcel 450188354Smarcel magic = le16dec(buf + DOSMAGICOFFSET); 451188354Smarcel if (magic != DOSMAGIC) 452188354Smarcel goto out; 453188354Smarcel 454216754Sae for (index = 0; index < 2; index++) { 455188354Smarcel p = buf + DOSPARTOFF + index * DOSPARTSIZE; 456188354Smarcel if (p[0] != 0 && p[0] != 0x80) 457188354Smarcel goto out; 458188354Smarcel } 459190443Smarcel res = G_PART_PROBE_PRI_NORM; 460188354Smarcel 461188354Smarcel out: 462188354Smarcel g_free(buf); 463188354Smarcel return (res); 464188354Smarcel} 465188354Smarcel 466188354Smarcelstatic int 467188354Smarcelg_part_ebr_read(struct g_part_table *basetable, struct g_consumer *cp) 468188354Smarcel{ 469188354Smarcel struct dos_partition ent[2]; 470188354Smarcel struct g_provider *pp; 471188354Smarcel struct g_part_entry *baseentry; 472188354Smarcel struct g_part_ebr_table *table; 473188354Smarcel struct g_part_ebr_entry *entry; 474188354Smarcel u_char *buf; 475188354Smarcel off_t ofs, msize; 476188354Smarcel u_int lba; 477223594Sae int error, index; 478188354Smarcel 479188354Smarcel pp = cp->provider; 480188354Smarcel table = (struct g_part_ebr_table *)basetable; 481221656Sae msize = MIN(pp->mediasize / pp->sectorsize, UINT32_MAX); 482188354Smarcel 483188354Smarcel lba = 0; 484188354Smarcel while (1) { 485188354Smarcel ofs = (off_t)lba * pp->sectorsize; 486188354Smarcel buf = g_read_data(cp, ofs, pp->sectorsize, &error); 487188354Smarcel if (buf == NULL) 488188354Smarcel return (error); 489188354Smarcel 490188354Smarcel ebr_entry_decode(buf + DOSPARTOFF + 0 * DOSPARTSIZE, ent + 0); 491188354Smarcel ebr_entry_decode(buf + DOSPARTOFF + 1 * DOSPARTSIZE, ent + 1); 492216754Sae 493216754Sae /* The 3rd & 4th entries should be zeroes. */ 494216754Sae if (le64dec(buf + DOSPARTOFF + 2 * DOSPARTSIZE) + 495216754Sae le64dec(buf + DOSPARTOFF + 3 * DOSPARTSIZE) != 0) { 496216754Sae basetable->gpt_corrupt = 1; 497216754Sae printf("GEOM: %s: invalid entries in the EBR ignored.\n", 498216754Sae pp->name); 499216754Sae } 500223594Sae#ifndef GEOM_PART_EBR_COMPAT 501223594Sae /* Save the first EBR, it can contain a boot code */ 502223594Sae if (lba == 0) 503223594Sae bcopy(buf, table->ebr, sizeof(table->ebr)); 504223594Sae#endif 505188354Smarcel g_free(buf); 506188354Smarcel 507188354Smarcel if (ent[0].dp_typ == 0) 508188354Smarcel break; 509188354Smarcel 510188354Smarcel if (ent[0].dp_typ == 5 && ent[1].dp_typ == 0) { 511188354Smarcel lba = ent[0].dp_start; 512188354Smarcel continue; 513188354Smarcel } 514188354Smarcel 515188354Smarcel index = (lba / basetable->gpt_sectors) + 1; 516188354Smarcel baseentry = (struct g_part_entry *)g_part_new_entry(basetable, 517188354Smarcel index, lba, lba + ent[0].dp_start + ent[0].dp_size - 1); 518188354Smarcel baseentry->gpe_offset = (off_t)(lba + ent[0].dp_start) * 519188354Smarcel pp->sectorsize; 520188354Smarcel entry = (struct g_part_ebr_entry *)baseentry; 521188354Smarcel entry->ent = ent[0]; 522188354Smarcel 523188354Smarcel if (ent[1].dp_typ == 0) 524188354Smarcel break; 525188354Smarcel 526188354Smarcel lba = ent[1].dp_start; 527188354Smarcel } 528188354Smarcel 529188354Smarcel basetable->gpt_entries = msize / basetable->gpt_sectors; 530188354Smarcel basetable->gpt_first = 0; 531222280Sae basetable->gpt_last = msize - 1; 532188354Smarcel return (0); 533188354Smarcel} 534188354Smarcel 535188354Smarcelstatic int 536188354Smarcelg_part_ebr_setunset(struct g_part_table *table, struct g_part_entry *baseentry, 537188354Smarcel const char *attrib, unsigned int set) 538188354Smarcel{ 539188667Smarcel struct g_part_entry *iter; 540188667Smarcel struct g_part_ebr_entry *entry; 541188667Smarcel int changed; 542188354Smarcel 543251588Smarcel if (baseentry == NULL) 544251588Smarcel return (ENODEV); 545188667Smarcel if (strcasecmp(attrib, "active") != 0) 546188667Smarcel return (EINVAL); 547188667Smarcel 548188667Smarcel /* Only one entry can have the active attribute. */ 549188667Smarcel LIST_FOREACH(iter, &table->gpt_entry, gpe_entry) { 550188667Smarcel if (iter->gpe_deleted) 551188667Smarcel continue; 552188667Smarcel changed = 0; 553188667Smarcel entry = (struct g_part_ebr_entry *)iter; 554188667Smarcel if (iter == baseentry) { 555188667Smarcel if (set && (entry->ent.dp_flag & 0x80) == 0) { 556188667Smarcel entry->ent.dp_flag |= 0x80; 557188667Smarcel changed = 1; 558188667Smarcel } else if (!set && (entry->ent.dp_flag & 0x80)) { 559188667Smarcel entry->ent.dp_flag &= ~0x80; 560188667Smarcel changed = 1; 561188667Smarcel } 562188667Smarcel } else { 563188667Smarcel if (set && (entry->ent.dp_flag & 0x80)) { 564188667Smarcel entry->ent.dp_flag &= ~0x80; 565188667Smarcel changed = 1; 566188667Smarcel } 567188667Smarcel } 568188667Smarcel if (changed && !iter->gpe_created) 569188667Smarcel iter->gpe_modified = 1; 570188667Smarcel } 571188667Smarcel return (0); 572188354Smarcel} 573188354Smarcel 574188354Smarcelstatic const char * 575188354Smarcelg_part_ebr_type(struct g_part_table *basetable, struct g_part_entry *baseentry, 576188354Smarcel char *buf, size_t bufsz) 577188354Smarcel{ 578188354Smarcel struct g_part_ebr_entry *entry; 579218014Sae int i; 580188354Smarcel 581188354Smarcel entry = (struct g_part_ebr_entry *)baseentry; 582218014Sae for (i = 0; 583218014Sae i < sizeof(ebr_alias_match) / sizeof(ebr_alias_match[0]); i++) { 584218014Sae if (ebr_alias_match[i].typ == entry->ent.dp_typ) 585218014Sae return (g_part_alias_name(ebr_alias_match[i].alias)); 586218014Sae } 587218014Sae snprintf(buf, bufsz, "!%d", entry->ent.dp_typ); 588188354Smarcel return (buf); 589188354Smarcel} 590188354Smarcel 591188354Smarcelstatic int 592188354Smarcelg_part_ebr_write(struct g_part_table *basetable, struct g_consumer *cp) 593188354Smarcel{ 594223594Sae#ifndef GEOM_PART_EBR_COMPAT 595223594Sae struct g_part_ebr_table *table; 596223594Sae#endif 597188667Smarcel struct g_provider *pp; 598188667Smarcel struct g_part_entry *baseentry, *next; 599188667Smarcel struct g_part_ebr_entry *entry; 600188667Smarcel u_char *buf; 601188667Smarcel u_char *p; 602188667Smarcel int error; 603188354Smarcel 604188667Smarcel pp = cp->provider; 605188667Smarcel buf = g_malloc(pp->sectorsize, M_WAITOK | M_ZERO); 606223594Sae#ifndef GEOM_PART_EBR_COMPAT 607223594Sae table = (struct g_part_ebr_table *)basetable; 608223594Sae bcopy(table->ebr, buf, DOSPARTOFF); 609223594Sae#endif 610188667Smarcel le16enc(buf + DOSMAGICOFFSET, DOSMAGIC); 611188667Smarcel 612188667Smarcel baseentry = LIST_FIRST(&basetable->gpt_entry); 613188667Smarcel while (baseentry != NULL && baseentry->gpe_deleted) 614188667Smarcel baseentry = LIST_NEXT(baseentry, gpe_entry); 615188667Smarcel 616218909Sbrucec /* Wipe-out the first EBR when there are no slices. */ 617188667Smarcel if (baseentry == NULL) { 618188667Smarcel error = g_write_data(cp, 0, buf, pp->sectorsize); 619188667Smarcel goto out; 620188667Smarcel } 621188667Smarcel 622188667Smarcel /* 623188667Smarcel * If the first partition is not in LBA 0, we need to 624188667Smarcel * put a "link" EBR in LBA 0. 625188667Smarcel */ 626188667Smarcel if (baseentry->gpe_start != 0) { 627188667Smarcel ebr_entry_link(basetable, (uint32_t)baseentry->gpe_start, 628188667Smarcel (uint32_t)baseentry->gpe_end, buf + DOSPARTOFF); 629188667Smarcel error = g_write_data(cp, 0, buf, pp->sectorsize); 630188667Smarcel if (error) 631188667Smarcel goto out; 632188667Smarcel } 633188667Smarcel 634188667Smarcel do { 635188667Smarcel entry = (struct g_part_ebr_entry *)baseentry; 636188667Smarcel 637188667Smarcel p = buf + DOSPARTOFF; 638188667Smarcel p[0] = entry->ent.dp_flag; 639188667Smarcel p[1] = entry->ent.dp_shd; 640188667Smarcel p[2] = entry->ent.dp_ssect; 641188667Smarcel p[3] = entry->ent.dp_scyl; 642188667Smarcel p[4] = entry->ent.dp_typ; 643188667Smarcel p[5] = entry->ent.dp_ehd; 644188667Smarcel p[6] = entry->ent.dp_esect; 645188667Smarcel p[7] = entry->ent.dp_ecyl; 646188667Smarcel le32enc(p + 8, entry->ent.dp_start); 647188667Smarcel le32enc(p + 12, entry->ent.dp_size); 648188667Smarcel 649188838Smarcel next = LIST_NEXT(baseentry, gpe_entry); 650188838Smarcel while (next != NULL && next->gpe_deleted) 651188838Smarcel next = LIST_NEXT(next, gpe_entry); 652188667Smarcel 653188667Smarcel p += DOSPARTSIZE; 654188667Smarcel if (next != NULL) 655188667Smarcel ebr_entry_link(basetable, (uint32_t)next->gpe_start, 656188667Smarcel (uint32_t)next->gpe_end, p); 657188667Smarcel else 658188667Smarcel bzero(p, DOSPARTSIZE); 659188667Smarcel 660188667Smarcel error = g_write_data(cp, baseentry->gpe_start * pp->sectorsize, 661188667Smarcel buf, pp->sectorsize); 662223594Sae#ifndef GEOM_PART_EBR_COMPAT 663223594Sae if (baseentry->gpe_start == 0) 664223594Sae bzero(buf, DOSPARTOFF); 665223594Sae#endif 666188667Smarcel baseentry = next; 667188667Smarcel } while (!error && baseentry != NULL); 668188667Smarcel 669188667Smarcel out: 670188667Smarcel g_free(buf); 671188667Smarcel return (error); 672188354Smarcel} 673