Example #1
0
void
main(int argc, char **argv)
{
	vlong secsize;

	secsize = 0;
	ARGBEGIN{
	case 'a':
		doauto++;
		break;
	case 'b':
		blank++;
		break;
	case 'f':
		file++;
		break;
	case 'p':
		printflag++;
		break;
	case 'r':
		rdonly++;
		break;
	case 's':
		secsize = atoi(ARGF());
		break;
	case 'v':
		printchs++;
		break;
	case 'w':
		dowrite++;
		break;
	}ARGEND;

	fmtinstall('D', Dfmt);

	if(argc != 1)
		usage();

	edit.disk = opendisk(argv[0], rdonly, file);
	if(edit.disk == nil) {
		fprint(2, "cannot open disk: %r\n");
		exits("opendisk");
	}

	if(secsize != 0) {
		edit.disk->secsize = secsize;
		edit.disk->secs = edit.disk->size / secsize;
	}

	sec2cyl = edit.disk->h * edit.disk->s;
	edit.end = edit.disk->secs / sec2cyl;

	findmbr(&edit);

	if(blank)
		blankpart(&edit);
	else
		rdpart(&edit, 0, 0);

	if(doauto)
		autopart(&edit);

	if(dowrite)
		runcmd(&edit, "w");

	if(printflag)
		runcmd(&edit, "P");

	if(dowrite || printflag)
		exits(0);

	fprint(2, "cylinder = %lld bytes\n", sec2cyl*edit.disk->secsize);
	runcmd(&edit, "p");
	for(;;) {
		fprint(2, ">>> ");
		runcmd(&edit, getline(&edit));
	}
}
Example #2
0
int
main(int argc, char **argv)
{
	int i;
	char *s;
	struct Dir root;
	int flag=FLAG_ROOT;
	struct Dir bin, sbin;
	struct File *b, *sb;
	struct Dir vmm;
	struct File *v;
	assert(BLKSIZE % sizeof(struct File) == 0);

	if (argc < 3)
		usage();

	nblocks = strtol(argv[2], &s, 0);
	if (*s || s == argv[2] || nblocks < 2 || nblocks > 10240)
		usage();

	opendisk(argv[1]);

	startdir(&super->s_root, &root);

	b = diradd(&root, FTYPE_DIR, "bin");
	startdir(b, &bin);
    
	sb = diradd(&root, FTYPE_DIR, "sbin");
	startdir(sb, &sbin);

	v = diradd(&root, FTYPE_DIR, "vmm");
	startdir(v, &vmm);

	for (i = 3; i < argc; i++) {
		if(strcmp("-b", argv[i]) == 0) {
			flag = FLAG_BIN;
			continue;
		} else if(strcmp("-sb", argv[i]) == 0) {
			flag = FLAG_SBIN;
			continue;
		} else if(strcmp("-g", argv[i]) == 0) {
			flag = FLAG_VMM;
			continue;
		}

		switch (flag){
		case FLAG_ROOT:
			writefile(&root, argv[i]);
			break;
		case FLAG_BIN:
			writefile(&bin, argv[i]);
			break;
		case FLAG_SBIN:
			writefile(&sbin, argv[i]);
			break;
		case FLAG_VMM:
			writefile(&vmm, argv[i]);
			break;
		}
	}
	
	finishdir(&bin);
	finishdir(&sbin);
	finishdir(&vmm);
	finishdir(&root);
	finishdisk();
	return 0;
}
/*
 * XXX Ugly bit of code.  But, this is the only safe time that the
 * match between BIOS disks and native disks can be done.
 */
static void
matchbiosdisks(void)
{
	struct btinfo_biosgeom *big;
	struct bi_biosgeom_entry *be;
	device_t dv;
	deviter_t di;
	int i, ck, error, m, n;
	struct vnode *tv;
	char mbr[DEV_BSIZE];
	int dklist_size;
	int numbig;

	if (x86_ndisks)
		return;
	big = lookup_bootinfo(BTINFO_BIOSGEOM);

	numbig = big ? big->num : 0;

	/* First, count all native disks. */
	for (dv = deviter_first(&di, DEVITER_F_ROOT_FIRST); dv != NULL;
	     dv = deviter_next(&di)) {
		if (is_valid_disk(dv))
			x86_ndisks++;
	}
	deviter_release(&di);

	dklist_size = sizeof(struct disklist) + (x86_ndisks - 1) *
	    sizeof(struct nativedisk_info);

	/* XXX M_TEMP is wrong */
	x86_alldisks = malloc(dklist_size, M_TEMP, M_NOWAIT | M_ZERO);
	if (x86_alldisks == NULL)
		return;

	x86_alldisks->dl_nnativedisks = x86_ndisks;
	x86_alldisks->dl_nbiosdisks = numbig;
	for (i = 0; i < numbig; i++) {
		x86_alldisks->dl_biosdisks[i].bi_dev = big->disk[i].dev;
		x86_alldisks->dl_biosdisks[i].bi_sec = big->disk[i].sec;
		x86_alldisks->dl_biosdisks[i].bi_head = big->disk[i].head;
		x86_alldisks->dl_biosdisks[i].bi_cyl = big->disk[i].cyl;
		x86_alldisks->dl_biosdisks[i].bi_lbasecs = big->disk[i].totsec;
		x86_alldisks->dl_biosdisks[i].bi_flags = big->disk[i].flags;
		DPRINTF(("%s: disk %x: flags %x",
		    __func__, big->disk[i].dev, big->disk[i].flags));
#ifdef BIOSDISK_EXTINFO_V3
		DPRINTF((", interface %x, device %llx",
		    big->disk[i].interface_path, big->disk[i].device_path));
#endif
		DPRINTF(("\n"));
	}

	/* XXX Code duplication from findroot(). */
	n = -1;
	for (dv = deviter_first(&di, DEVITER_F_ROOT_FIRST); dv != NULL;
	     dv = deviter_next(&di)) {
		if (!is_valid_disk(dv))
			continue;
		DPRINTF(("%s: trying to match (%s) %s: ", __func__,
		    device_xname(dv), device_cfdata(dv)->cf_name));
		n++;
		snprintf(x86_alldisks->dl_nativedisks[n].ni_devname,
		    sizeof(x86_alldisks->dl_nativedisks[n].ni_devname),
		    "%s", device_xname(dv));

		if ((tv = opendisk(dv)) == NULL) {
			DPRINTF(("cannot open\n"));
			continue;
		}

		error = vn_rdwr(UIO_READ, tv, mbr, DEV_BSIZE, 0, UIO_SYSSPACE,
		    0, NOCRED, NULL, NULL);
		VOP_CLOSE(tv, FREAD, NOCRED);
		vput(tv);
		if (error) {
			DPRINTF(("MBR read failure %d\n", error));
			continue;
		}

		for (ck = i = 0; i < DEV_BSIZE; i++)
			ck += mbr[i];
		for (m = i = 0; i < numbig; i++) {
			be = &big->disk[i];
			if (be->flags & BI_GEOM_INVALID)
				continue;
			DPRINTF(("matched with %d dev ck %x bios ck %x\n",
			    i, ck, be->cksum));
			if (be->cksum == ck && memcmp(&mbr[MBR_PART_OFFSET],
			    be->mbrparts, MBR_PART_COUNT
			    * sizeof(struct mbr_partition)) == 0) {
				DPRINTF(("%s: matched BIOS disk %x with %s\n",
				    __func__, be->dev, device_xname(dv)));
				x86_alldisks->dl_nativedisks[n].
				    ni_biosmatches[m++] = i;
			}
		}
		x86_alldisks->dl_nativedisks[n].ni_nmatches = m;
	}
	deviter_release(&di);
}
Example #4
0
const char *
getfsspecname(char *buf, size_t bufsiz, const char *name)
{
	static const int mib[] = { CTL_HW, HW_DISKNAMES };
	static const unsigned int miblen = __arraycount(mib);
	char *drives, *dk;
	size_t len;
	int fd, savee;
	char *vname;

	drives = NULL;
	vname = NULL;
	if (strncasecmp(name, "NAME=", 5) != 0) {
#ifdef COMPAT_DKWEDGE
		/*
		 * We try to open the disk name, and if we fail with EBUSY
		 * we use the name as the label to find the wedge.
		 */
		char rbuf[MAXPATHLEN];
		if (name[0] == '/') {
			if (getdiskrawname(rbuf, sizeof(rbuf), name) != NULL) {
				if ((fd = open(rbuf, O_RDONLY)) == -1) {
					if (errno == EBUSY) {
						name = strrchr(name, '/') + 1;
						goto search;
					}
				} else
					close(fd);
			}
		}
#endif
		strlcpy(buf, name, bufsiz);
		return buf;
	} else
		name += 5;

#ifdef COMPAT_DKWEDGE
search:
#endif
	vname = malloc(strlen(name) * 4 + 1);
	if (vname == NULL) {
		savee = errno;
		strlcpy(buf, "malloc failed", bufsiz);
		goto out;
	}

	strunvis(vname, name);

	if (sysctl(mib, miblen, NULL, &len, NULL, 0) == -1) {
		savee = errno;
		strlcpy(buf, "sysctl hw.disknames failed", bufsiz);
		goto out;
	}

	drives = malloc(len);
	if (drives == NULL) {
		savee = errno;
		strlcpy(buf, "malloc failed", bufsiz);
		goto out;
	}
	if (sysctl(mib, miblen, drives, &len, NULL, 0) == -1) {
		savee = errno;
		strlcpy(buf, "sysctl hw.disknames failed", bufsiz);
		goto out;
	}

	for (dk = strtok(drives, " "); dk != NULL; dk = strtok(NULL, " ")) {
		struct dkwedge_info dkw;
		if (strncmp(dk, "dk", 2) != 0)
			continue;
		fd = opendisk(dk, O_RDONLY, buf, bufsiz, 0);
		if (fd == -1)
			continue;
		if (ioctl(fd, DIOCGWEDGEINFO, &dkw) == -1) {
			savee = errno;
			snprintf(buf, bufsiz, "%s: getwedgeinfo", dk);
			(void)close(fd);
			goto out;
		}
		(void)close(fd);
		if (strcmp(vname, (char *)dkw.dkw_wname) == 0) {
			char *p = strstr(buf, "/rdk");
			if (p++ == NULL) 
				return buf;
			strcpy(p, p + 1);
			free(drives);
			free(vname);
			return buf;
		}
	}
	savee = ESRCH;
	snprintf(buf, bufsiz, "no match for `%s'", vname);
out:
	free(drives);
	free(vname);
	errno = savee;
	return NULL;
}