static int validate_label(mbr_args_t *a, uint label_sector) { struct disklabel *dlp; char *dlp_lim, *dlp_byte; int error; /* Next, dig out disk label */ if (read_sector(a, label_sector, 2)) { a->msg = "disk label read failed"; return SCAN_ERROR; } /* Locate disk label within block and validate */ /* * XXX (dsl) This search may be a waste of time, a lot of other i386 * code assumes the label is at offset LABELOFFSET (=0) in the sector. * * If we want to support disks from other netbsd ports, then the * code should also allow for a shorter label nearer the end of * the disk sector, and (IIRC) labels within 8k of the disk start. */ dlp = (void *)a->bp->b_data; dlp_lim = (char *)a->bp->b_data + a->bp->b_bcount - sizeof *dlp; for (;; dlp = (void *)((char *)dlp + sizeof(long))) { if ((char *)dlp > dlp_lim) { if (a->action != WRITE_LABEL) return SCAN_CONTINUE; /* Write at arch. dependant default location */ dlp_byte = (char *)a->bp->b_data + LABELOFFSET; if (label_sector) dlp_byte += MBR_LABELSECTOR * a->lp->d_secsize; else dlp_byte += LABELSECTOR * a->lp->d_secsize; dlp = (void *)dlp_byte; break; } if (dlp->d_magic != DISKMAGIC || dlp->d_magic2 != DISKMAGIC) continue; if (dlp->d_npartitions > MAXPARTITIONS || dkcksum(dlp) != 0) { a->msg = "disk label corrupted"; continue; } break; } switch (a->action) { case READ_LABEL: *a->lp = *dlp; if ((a->msg = convertdisklabel(a->lp, a->strat, a->bp, a->secperunit)) != NULL) return SCAN_ERROR; a->label_sector = label_sector; return SCAN_FOUND; case UPDATE_LABEL: case WRITE_LABEL: *dlp = *a->lp; a->bp->b_oflags &= ~BO_DONE; a->bp->b_flags &= ~B_READ; a->bp->b_flags |= B_WRITE; (*a->strat)(a->bp); error = biowait(a->bp); if (error != 0) { a->error = error; a->msg = "disk label write failed"; return SCAN_ERROR; } a->written++; /* Write label to all mbr partitions */ return SCAN_CONTINUE; default: return SCAN_ERROR; } }
/* * Attempt to read a disk label from a device * using the indicated strategy routine. * The label must be partly set up before this: * secpercyl and anything required in the strategy routine * (e.g., sector size) must be filled in before calling us. * Returns null on success and an error string on failure. */ const char * readdisklabel(dev_t dev, void (*strat)(struct buf *), struct disklabel *lp, struct cpu_disklabel *clp) { struct buf *bp; struct disklabel *dlp; struct dkbad *bdp; const char *msg = NULL; uint32_t secperunit; int i; /* minimal requirements for archtypal disk label */ if (lp->d_secsize == 0) lp->d_secsize = DEV_BSIZE; if (lp->d_secperunit == 0) lp->d_secperunit = 0x1fffffff; lp->d_npartitions = RAW_PART + 1; if (lp->d_partitions[RAW_PART].p_size == 0) lp->d_partitions[RAW_PART].p_size = lp->d_secperunit; lp->d_partitions[RAW_PART].p_offset = 0; secperunit = lp->d_secperunit; /* obtain buffer to probe drive with */ bp = geteblk((int)lp->d_secsize); bp->b_dev = dev; /* Next, dig out disk label. If successful, locate disk * label within block and validate. */ if (disk_read_sectors(strat, lp, bp, LABELSECTOR, 1) != 0) { msg = "disk label read error"; goto done; } for (dlp = (struct disklabel *)bp->b_data; dlp <= (struct disklabel *)((char *)bp->b_data + lp->d_secsize - sizeof(*dlp)); dlp = (struct disklabel *)((char *)dlp + sizeof(long))) { if (dlp->d_magic != DISKMAGIC || dlp->d_magic2 != DISKMAGIC) { if (msg == NULL) msg = "no disk label"; } else if (dlp->d_npartitions > MAXPARTITIONS || dkcksum(dlp) != 0) msg = "disk label corrupted"; else { *lp = *dlp; msg = NULL; break; } } if (msg) goto done; if ((msg = convertdisklabel(lp, strat, bp, secperunit)) != NULL) goto done; /* obtain bad sector table if requested and present */ if (clp && (bdp = &clp->bad) != NULL && (lp->d_flags & D_BADSECT)) { struct dkbad *db; i = 0; do { /* read a bad sector table */ bp->b_oflags &= ~(BO_DONE); bp->b_flags |= B_READ; bp->b_blkno = lp->d_secperunit - lp->d_nsectors + i; if (lp->d_secsize > DEV_BSIZE) bp->b_blkno *= lp->d_secsize / DEV_BSIZE; else bp->b_blkno /= DEV_BSIZE / lp->d_secsize; bp->b_bcount = lp->d_secsize; bp->b_cylinder = lp->d_ncylinders - 1; (*strat)(bp); /* if successful, validate, otherwise try another */ if (biowait(bp)) { msg = "bad sector table I/O error"; } else { db = (struct dkbad *)(bp->b_data); #define DKBAD_MAGIC 0x4321 if (db->bt_mbz == 0 && db->bt_flag == DKBAD_MAGIC) { msg = NULL; *bdp = *db; break; } else msg = "bad sector table corrupted"; } } while (bp->b_error != 0 && (i += 2) < 10 && i < lp->d_nsectors); } done: brelse(bp, 0); return (msg); }