⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fdiskbsdlabel.c

📁 Util-linux 软件包包含许多工具。其中比较重要的是加载、卸载、格式化、分区和管理硬盘驱动器
💻 C
📖 第 1 页 / 共 2 页
字号:
#endif				) == 1) {				xbsd_print_disklabel (1);				return 1;			} else				return 0;		} else if (c == 'n')			return 0;	}}static intedit_int (int def, char *mesg){  do {    fputs (mesg, stdout);    printf (" (%d): ", def);    if (!read_line ())      return def;  }  while (!isdigit (*line_ptr));  return atoi (line_ptr);} static voidxbsd_edit_disklabel (void){  struct xbsd_disklabel *d;  d = &xbsd_dlabel;#if defined (__alpha__) || defined (__ia64__)  d -> d_secsize    = (u_long) edit_int ((u_long) d -> d_secsize     ,_("bytes/sector"));  d -> d_nsectors   = (u_long) edit_int ((u_long) d -> d_nsectors    ,_("sectors/track"));  d -> d_ntracks    = (u_long) edit_int ((u_long) d -> d_ntracks     ,_("tracks/cylinder"));  d -> d_ncylinders = (u_long) edit_int ((u_long) d -> d_ncylinders  ,_("cylinders"));#endif  /* d -> d_secpercyl can be != d -> d_nsectors * d -> d_ntracks */  while (1)  {    d -> d_secpercyl = (u_long) edit_int ((u_long) d -> d_nsectors * d -> d_ntracks,					  _("sectors/cylinder"));    if (d -> d_secpercyl <= d -> d_nsectors * d -> d_ntracks)      break;    printf (_("Must be <= sectors/track * tracks/cylinder (default).\n"));  }  d -> d_rpm        = (u_short) edit_int ((u_short) d -> d_rpm       ,_("rpm"));  d -> d_interleave = (u_short) edit_int ((u_short) d -> d_interleave,_("interleave"));  d -> d_trackskew  = (u_short) edit_int ((u_short) d -> d_trackskew ,_("trackskew"));  d -> d_cylskew    = (u_short) edit_int ((u_short) d -> d_cylskew   ,_("cylinderskew"));  d -> d_headswitch = (u_long) edit_int ((u_long) d -> d_headswitch  ,_("headswitch"));  d -> d_trkseek    = (u_long) edit_int ((u_long) d -> d_trkseek     ,_("track-to-track seek"));  d -> d_secperunit = d -> d_secpercyl * d -> d_ncylinders;}static intxbsd_get_bootstrap (char *path, void *ptr, int size){  int fd;  if ((fd = open (path, O_RDONLY)) < 0)  {    perror (path);    return 0;  }  if (read (fd, ptr, size) < 0)  {    perror (path);    close (fd);    return 0;  }  printf (" ... %s\n", path);  close (fd);  return 1;}static voidxbsd_write_bootstrap (void){  char *bootdir = BSD_LINUX_BOOTDIR;  char path[MAXPATHLEN];  char *dkbasename;  struct xbsd_disklabel dl;  char *d, *p, *e;  int sector;  if (xbsd_dlabel.d_type == BSD_DTYPE_SCSI)    dkbasename = "sd";  else    dkbasename = "wd";  printf (_("Bootstrap: %sboot -> boot%s (%s): "),	  dkbasename, dkbasename, dkbasename);  if (read_line ()) {    line_ptr[strlen (line_ptr)-1] = '\0';    dkbasename = line_ptr;  }  snprintf (path, sizeof(path), "%s/%sboot", bootdir, dkbasename);  if (!xbsd_get_bootstrap (path, disklabelbuffer, (int) xbsd_dlabel.d_secsize))    return;  /* We need a backup of the disklabel (xbsd_dlabel might have changed). */  d = &disklabelbuffer[BSD_LABELSECTOR * SECTOR_SIZE];  bcopy (d, &dl, sizeof (struct xbsd_disklabel));  /* The disklabel will be overwritten by 0's from bootxx anyway */  bzero (d, sizeof (struct xbsd_disklabel));  snprintf (path, sizeof(path), "%s/boot%s", bootdir, dkbasename);  if (!xbsd_get_bootstrap (path, &disklabelbuffer[xbsd_dlabel.d_secsize],			  (int) xbsd_dlabel.d_bbsize - xbsd_dlabel.d_secsize))    return;  e = d + sizeof (struct xbsd_disklabel);  for (p=d; p < e; p++)    if (*p) {      fprintf (stderr, _("Bootstrap overlaps with disk label!\n"));      exit ( EXIT_FAILURE );    }  bcopy (&dl, d, sizeof (struct xbsd_disklabel));#if defined (__powerpc__) || defined (__hppa__)  sector = 0;#elif defined (__alpha__)  sector = 0;  alpha_bootblock_checksum (disklabelbuffer);#else  sector = get_start_sect(xbsd_part);#endif  if (ext2_llseek (fd, (long long) sector * SECTOR_SIZE, SEEK_SET) == -1)    fatal (unable_to_seek);  if (BSD_BBSIZE != write (fd, disklabelbuffer, BSD_BBSIZE))    fatal (unable_to_write);#if defined (__alpha__)  printf (_("Bootstrap installed on %s.\n"), disk_device);#else  printf (_("Bootstrap installed on %s.\n"),    partname (disk_device, xbsd_part_index+1, 0));#endif  sync_disks ();}static voidxbsd_change_fstype (void){  int i;  i = xbsd_get_part_index (xbsd_dlabel.d_npartitions);  xbsd_dlabel.d_partitions[i].p_fstype = read_hex (xbsd_fstypes);}static intxbsd_get_part_index (int max){  char prompt[256];  char l;  snprintf (prompt, sizeof(prompt), _("Partition (a-%c): "), 'a' + max - 1);  do     l = tolower (read_char (prompt));  while (l < 'a' || l > 'a' + max - 1);  return l - 'a';}static intxbsd_check_new_partition (int *i) {	/* room for more? various BSD flavours have different maxima */	if (xbsd_dlabel.d_npartitions == BSD_MAXPARTITIONS) {		int t;		for (t = 0; t < BSD_MAXPARTITIONS; t++)			if (xbsd_dlabel.d_partitions[t].p_size == 0)				break;		if (t == BSD_MAXPARTITIONS) {			fprintf (stderr, _("The maximum number of partitions "					   "has been created\n"));			return 0;		}	}	*i = xbsd_get_part_index (BSD_MAXPARTITIONS);	if (*i >= xbsd_dlabel.d_npartitions)		xbsd_dlabel.d_npartitions = (*i) + 1;	if (xbsd_dlabel.d_partitions[*i].p_size != 0) {		fprintf (stderr, _("This partition already exists.\n"));		return 0;	}	return 1;}static voidxbsd_list_types (void) {	list_types (xbsd_fstypes);}static u_shortxbsd_dkcksum (struct xbsd_disklabel *lp) {	u_short *start, *end;	u_short sum = 0;  	start = (u_short *) lp;	end = (u_short *) &lp->d_partitions[lp->d_npartitions];	while (start < end)		sum ^= *start++;	return sum;}static intxbsd_initlabel (struct partition *p, struct xbsd_disklabel *d, int pindex) {	struct xbsd_partition *pp;	struct geom g;	get_geometry (fd, &g);	bzero (d, sizeof (struct xbsd_disklabel));	d -> d_magic = BSD_DISKMAGIC;	if (strncmp (disk_device, "/dev/sd", 7) == 0)		d -> d_type = BSD_DTYPE_SCSI;	else		d -> d_type = BSD_DTYPE_ST506;#if 0 /* not used (at least not written to disk) by NetBSD/i386 1.0 */	d -> d_subtype = BSD_DSTYPE_INDOSPART & pindex;#endif#if !defined (__alpha__)	d -> d_flags = BSD_D_DOSPART;#else	d -> d_flags = 0;#endif	d -> d_secsize = SECTOR_SIZE;		/* bytes/sector  */	d -> d_nsectors = g.sectors;		/* sectors/track */	d -> d_ntracks = g.heads;		/* tracks/cylinder (heads) */	d -> d_ncylinders = g.cylinders;	d -> d_secpercyl  = g.sectors * g.heads;/* sectors/cylinder */	if (d -> d_secpercyl == 0)		d -> d_secpercyl = 1;		/* avoid segfaults */	d -> d_secperunit = d -> d_secpercyl * d -> d_ncylinders;	d -> d_rpm = 3600;	d -> d_interleave = 1;	d -> d_trackskew = 0;	d -> d_cylskew = 0;	d -> d_headswitch = 0;	d -> d_trkseek = 0;	d -> d_magic2 = BSD_DISKMAGIC;	d -> d_bbsize = BSD_BBSIZE;	d -> d_sbsize = BSD_SBSIZE;#if !defined (__alpha__)	d -> d_npartitions = 4;	pp = &d -> d_partitions[2];		/* Partition C should be						   the NetBSD partition */	pp -> p_offset = get_start_sect(p);	pp -> p_size   = get_nr_sects(p);	pp -> p_fstype = BSD_FS_UNUSED;	pp = &d -> d_partitions[3];		/* Partition D should be						   the whole disk */	pp -> p_offset = 0;	pp -> p_size   = d -> d_secperunit;	pp -> p_fstype = BSD_FS_UNUSED;#elif defined (__alpha__)	d -> d_npartitions = 3;	pp = &d -> d_partitions[2];		/* Partition C should be						   the whole disk */	pp -> p_offset = 0;	pp -> p_size   = d -> d_secperunit;	pp -> p_fstype = BSD_FS_UNUSED;  #endif	return 1;}/* * Read a xbsd_disklabel from sector 0 or from the starting sector of p. * If it has the right magic, return 1. */static intxbsd_readlabel (struct partition *p, struct xbsd_disklabel *d){	int t, sector;	/* p is used only to get the starting sector */#if !defined (__alpha__)	sector = (p ? get_start_sect(p) : 0);#elif defined (__alpha__)	sector = 0;#endif	if (ext2_llseek (fd, (long long) sector * SECTOR_SIZE, SEEK_SET) == -1)		fatal (unable_to_seek);	if (BSD_BBSIZE != read (fd, disklabelbuffer, BSD_BBSIZE))		fatal (unable_to_read);	bcopy (&disklabelbuffer[BSD_LABELSECTOR * SECTOR_SIZE + BSD_LABELOFFSET],	       d, sizeof (struct xbsd_disklabel));	if (d -> d_magic != BSD_DISKMAGIC || d -> d_magic2 != BSD_DISKMAGIC)		return 0;	for (t = d -> d_npartitions; t < BSD_MAXPARTITIONS; t++) {		d -> d_partitions[t].p_size   = 0;		d -> d_partitions[t].p_offset = 0;		d -> d_partitions[t].p_fstype = BSD_FS_UNUSED;  	}	if (d -> d_npartitions > BSD_MAXPARTITIONS)		fprintf (stderr, _("Warning: too many partitions "				   "(%d, maximum is %d).\n"),			 d -> d_npartitions, BSD_MAXPARTITIONS);	return 1;}static intxbsd_writelabel (struct partition *p, struct xbsd_disklabel *d){  unsigned int sector;#if !defined (__alpha__) && !defined (__powerpc__) && !defined (__hppa__)  sector = get_start_sect(p) + BSD_LABELSECTOR;#else  sector = BSD_LABELSECTOR;#endif  d -> d_checksum = 0;  d -> d_checksum = xbsd_dkcksum (d);  /* This is necessary if we want to write the bootstrap later,     otherwise we'd write the old disklabel with the bootstrap.  */  bcopy (d, &disklabelbuffer[BSD_LABELSECTOR * SECTOR_SIZE + BSD_LABELOFFSET],	 sizeof (struct xbsd_disklabel));#if defined (__alpha__) && BSD_LABELSECTOR == 0  alpha_bootblock_checksum (disklabelbuffer);  if (ext2_llseek (fd, (long long) 0, SEEK_SET) == -1)    fatal (unable_to_seek);  if (BSD_BBSIZE != write (fd, disklabelbuffer, BSD_BBSIZE))    fatal (unable_to_write);#else  if (ext2_llseek (fd, (long long) sector * SECTOR_SIZE + BSD_LABELOFFSET,		   SEEK_SET) == -1)    fatal (unable_to_seek);  if (sizeof (struct xbsd_disklabel) != write (fd, d, sizeof (struct xbsd_disklabel)))    fatal (unable_to_write);#endif  sync_disks ();  return 1;}static voidsync_disks (void){  printf (_("\nSyncing disks.\n"));  sync ();  sleep (4);}#if !defined (__alpha__)static intxbsd_translate_fstype (int linux_type){  switch (linux_type)  {    case 0x01: /* DOS 12-bit FAT   */    case 0x04: /* DOS 16-bit <32M  */    case 0x06: /* DOS 16-bit >=32M */    case 0xe1: /* DOS access       */    case 0xe3: /* DOS R/O          */    case 0xf2: /* DOS secondary    */      return BSD_FS_MSDOS;    case 0x07: /* OS/2 HPFS        */      return BSD_FS_HPFS;    default:      return BSD_FS_OTHER;  }}static voidxbsd_link_part (void){  int k, i;  struct partition *p;  k = get_partition (1, partitions);  if (!xbsd_check_new_partition (&i))    return;  p = get_part_table(k);  xbsd_dlabel.d_partitions[i].p_size   = get_nr_sects(p);  xbsd_dlabel.d_partitions[i].p_offset = get_start_sect(p);  xbsd_dlabel.d_partitions[i].p_fstype = xbsd_translate_fstype(p->sys_ind);}#endif#if defined (__alpha__)#if !defined(__GLIBC__)typedef unsigned long long u_int64_t;#endifvoidalpha_bootblock_checksum (char *boot){  u_int64_t *dp, sum;  int i;    dp = (u_int64_t *)boot;  sum = 0;  for (i = 0; i < 63; i++)    sum += dp[i];  dp[63] = sum;}#endif /* __alpha__ */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -