Ejemplo n.º 1
0
int
get_nv_params()
{
   int   e;
   NV_FILE * fp;

   /* Just in case Demo Applications want to parse webport.nv again */
   netidx = -1; /* reset index of nets[] to configure */

   /* invoke the generic parm reader to get TCP/IP system parameters */
   e = get_file_parms(nvfilename, nv_formats, &line);
   if (e == ENP_NOFILE)
   {
      printf("Creating sample file\n");
      fp = nv_fopen(nvfilename, "w+");
      if (fp)
      {
         if (set_nv_defaults)   /* see if app wants to init defaults */
            set_nv_defaults();
         set_nv_params(NULL);   /* write defaults to new file */

         /* now try again from the flash file we just set up */
         e = get_file_parms(nvfilename, nv_formats, &line);
      }
      else
         printf("Can't create sample file either.\n");
   }
   return(e);
}
Ejemplo n.º 2
0
/*
 * Fetch disklabel for disk.
 */
static int
readlabel(int flag)
{
	ssize_t nbytes;
	uint32_t lba;
	int f, i;
	int error;

	f = open(specname, O_RDONLY);
	if (f < 0)
		err(1, "%s", specname);
	if (is_file)
		get_file_parms(f);
	else {
		mediasize = g_mediasize(f);
		secsize = g_sectorsize(f);
		if (secsize < 0 || mediasize < 0)
			err(4, "cannot get disk geometry");
	}
	if (mediasize > (off_t)0xffffffff * secsize)
		errx(1,
		    "disks with more than 2^32-1 sectors are not supported");
	(void)lseek(f, (off_t)0, SEEK_SET);
	nbytes = read(f, bootarea, BBSIZE);
	if (nbytes == -1)
		err(4, "%s read", specname);
	if (nbytes != BBSIZE)
		errx(4, "couldn't read %d bytes from %s", BBSIZE, specname);
	close (f);
	error = bsd_disklabel_le_dec(
	    bootarea + (labeloffset + labelsoffset * secsize),
	    &lab, MAXPARTITIONS);
	if (flag && error)
		errx(1, "%s: no valid label found", specname);

	if (is_file)
		return(0);

	/*
	 * Compensate for absolute block addressing by finding the
	 * smallest partition offset and if the offset of the 'c'
	 * partition is equal to that, subtract it from all offsets.
	 */
	lba = ~0;
	for (i = 0; i < lab.d_npartitions; i++) {
		if (lab.d_partitions[i].p_size)
			lba = MIN(lba, lab.d_partitions[i].p_offset);
	}
	if (lba != 0 && lab.d_partitions[RAW_PART].p_offset == lba) {
		for (i = 0; i < lab.d_npartitions; i++) {
			if (lab.d_partitions[i].p_size)
				lab.d_partitions[i].p_offset -= lba;
		}
		/*
		 * Save the offset so that we can write the label
		 * back with absolute block addresses.
		 */
		lba_offset = lba;
	}
	return (error);
}
Ejemplo n.º 3
0
/*
 * When operating on a "virgin" disk, try getting an initial label
 * from the associated device driver.  This might work for all device
 * drivers that are able to fetch some initial device parameters
 * without even having access to a (BSD) disklabel, like SCSI disks,
 * most IDE drives, or vn devices.
 *
 * The device name must be given in its "canonical" form.
 */
static struct disklabel *
getvirginlabel(void)
{
	static struct disklabel loclab;
	struct partition *dp;
	int f;
	u_int u;

	if ((f = open(specname, O_RDONLY)) == -1) {
		warn("cannot open %s", specname);
		return (NULL);
	}

	if (is_file)
		get_file_parms(f);
	else {
		mediasize = g_mediasize(f);
		secsize = g_sectorsize(f);
		if (secsize < 0 || mediasize < 0) {
			close (f);
			return (NULL);
		}
	}
	memset(&loclab, 0, sizeof loclab);
	loclab.d_magic = DISKMAGIC;
	loclab.d_magic2 = DISKMAGIC;
	loclab.d_secsize = secsize;
	loclab.d_secperunit = mediasize / secsize;

	/*
	 * Nobody in these enlightened days uses the CHS geometry for
	 * anything, but nonetheless try to get it right.  If we fail
	 * to get any good ideas from the device, construct something
	 * which is IBM-PC friendly.
	 */
	if (ioctl(f, DIOCGFWSECTORS, &u) == 0)
		loclab.d_nsectors = u;
	else
		loclab.d_nsectors = 63;
	if (ioctl(f, DIOCGFWHEADS, &u) == 0)
		loclab.d_ntracks = u;
	else if (loclab.d_secperunit <= 63*1*1024)
		loclab.d_ntracks = 1;
	else if (loclab.d_secperunit <= 63*16*1024)
		loclab.d_ntracks = 16;
	else
		loclab.d_ntracks = 255;
	loclab.d_secpercyl = loclab.d_ntracks * loclab.d_nsectors;
	loclab.d_ncylinders = loclab.d_secperunit / loclab.d_secpercyl;
	loclab.d_npartitions = DEFPARTITIONS;

	/* Various (unneeded) compat stuff */
	loclab.d_rpm = 3600;
	loclab.d_bbsize = BBSIZE;
	loclab.d_interleave = 1;
	strncpy(loclab.d_typename, "amnesiac",
	    sizeof(loclab.d_typename));

	dp = &loclab.d_partitions[RAW_PART];
	dp->p_size = loclab.d_secperunit;
	loclab.d_checksum = dkcksum(&loclab);
	close (f);
	return (&loclab);
}