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

📄 mkdosfs.c.bak

📁 linux下格式化U盘的函数,从mkdosfs而来
💻 BAK
📖 第 1 页 / 共 4 页
字号:
	// die ("Too few blocks for viable file system");  if (verbose)    {      printf("%s has %d head%s and %d sector%s per track,\n",	     device_name, CF_LE_W(bs.heads), (CF_LE_W(bs.heads) != 1) ? "s" : "",	     CF_LE_W(bs.secs_track), (CF_LE_W(bs.secs_track) != 1) ? "s" : "");       printf("logical sector size is %d,\n",sector_size);      printf("using 0x%02x media descriptor, with %d sectors;\n",	     (int) (bs.media), num_sectors);      printf("file system has %d %d-bit FAT%s and %d sector%s per cluster.\n",	     (int) (bs.fats), size_fat, (bs.fats != 1) ? "s" : "",	     (int) (bs.cluster_size), (bs.cluster_size != 1) ? "s" : "");      printf ("FAT size is %d sector%s, and provides %d cluster%s.\n",	      fat_length, (fat_length != 1) ? "s" : "",	      cluster_count, (cluster_count != 1) ? "s" : "");      if (size_fat != 32)	printf ("Root directory contains %d slots.\n",		(int) (bs.dir_entries[0]) + (int) (bs.dir_entries[1]) * 256);      printf ("Volume ID is %08lx, ", volume_id &	      (atari_format ? 0x00ffffff : 0xffffffff));      if ( strcmp(volume_name, "           ") )	printf("volume label %s.\n", volume_name);      else	printf("no volume label.\n");    }  /* Make the file allocation tables! */  if ((fat = (unsigned char *) malloc (fat_length * sector_size)) == NULL)		return(_ERROR_);   	 //die ("unable to allocate space for FAT image in memory");  memset( fat, 0, fat_length * sector_size );  if(mark_FAT_cluster (0, 0xffffffff)==_ERROR_)	return _ERROR_;	/* Initial fat entries */  if(mark_FAT_cluster (1, 0xffffffff)==_ERROR_)	return _ERROR_;  fat[0] = (unsigned char) bs.media;	/* Put media type in first byte! */  if (size_fat == 32) {    /* Mark cluster 2 as EOF (used for root dir) */  if(mark_FAT_cluster (2, FAT_EOF)==_ERROR_)	return _ERROR_;  }  /* Make the root directory entries */  size_root_dir = (size_fat == 32) ?		  bs.cluster_size*sector_size :		  (((int)bs.dir_entries[1]*256+(int)bs.dir_entries[0]) *		   sizeof (struct msdos_dir_entry));  if ((root_dir = (struct msdos_dir_entry *) malloc (size_root_dir)) == NULL)    {      free (fat);		/* Tidy up before we die! */      return(_ERROR_);		//die ("unable to allocate space for root directory in memory");    }  memset(root_dir, 0, size_root_dir);  if ( memcmp(volume_name, "           ", 11) )    {      struct msdos_dir_entry *de = &root_dir[0];      memcpy(de->name, volume_name, 11);      de->attr = ATTR_VOLUME;      ctime = localtime(&create_time);      de->time = CT_LE_W((unsigned short)((ctime->tm_sec >> 1) +			  (ctime->tm_min << 5) + (ctime->tm_hour << 11)));      de->date = CT_LE_W((unsigned short)(ctime->tm_mday +					  ((ctime->tm_mon+1) << 5) +					  ((ctime->tm_year-80) << 9)));      de->ctime_ms = 0;      de->ctime = de->time;      de->cdate = de->date;      de->adate = de->date;      de->starthi = CT_LE_W(0);      de->start = CT_LE_W(0);      de->size = CT_LE_L(0);    }  if (size_fat == 32) {    /* For FAT32, create an info sector */    struct fat32_fsinfo *info;        if (!(info_sector = malloc( sector_size )))		return(_ERROR_);      		//die("Out of memory");    memset(info_sector, 0, sector_size);    /* fsinfo structure is at offset 0x1e0 in info sector by observation */    info = (struct fat32_fsinfo *)(info_sector + 0x1e0);    /* Info sector magic */    info_sector[0] = 'R';    info_sector[1] = 'R';    info_sector[2] = 'a';    info_sector[3] = 'A';    /* Magic for fsinfo structure */    info->signature = CT_LE_L(0x61417272);    /* We've allocated cluster 2 for the root dir. */    info->free_clusters = CT_LE_L(cluster_count - 1);    info->next_cluster = CT_LE_L(2);    /* Info sector also must have boot sign */    *(__u16 *)(info_sector + 0x1fe) = CT_LE_W(BOOT_SIGN);  }    if (!(blank_sector = malloc( sector_size )))      return(_ERROR_);		//die( "Out of memory" );  	memset(blank_sector, 0, sector_size);	return(_OK_);}/* Write the new filesystem's data tables to wherever they're going to end up! */#define error(str)				\  do {						\    free (fat);					\    if (info_sector) free (info_sector);	\    free (root_dir);				\	return (_ERROR_);  			\  } while(0)#define seekto(pos,errstr)						\  do {									\    loff_t __pos = (pos);						\    if (llseek (dev, __pos, SEEK_SET) != __pos)				\	error ("seek to " errstr " failed whilst writing tables");	\  } while(0)#define writebuf(buf,size,errstr)			\  do {							\    int __size = (size);				\    if (write (dev, buf, __size) != __size)		\	error ("failed whilst writing " errstr);	\  } while(0)static intwrite_tables (void){  int x;  int fat_length;  fat_length = (size_fat == 32) ?	(CF_LE_L(bs.fat32.fat32_length)) : (CF_LE_W(bs.fat_length));  seekto(0, "start of device");  /* clear all reserved sectors */  for( x = 0; x < reserved_sectors; ++x )   {	writebuf( blank_sector, sector_size, "reserved sector" );	}  /* seek back to sector 0 and write the boot sector */  seekto(0, "boot sector");  writebuf( (char *) &bs, sizeof (struct msdos_boot_sector), "boot sector" );  /* on FAT32, write the info sector and backup boot sector */  if (size_fat == 32)    {      seekto( CF_LE_W(bs.fat32.info_sector)*sector_size, "info sector" );      writebuf( info_sector, 512, "info sector" );      if (backup_boot != 0)	{	  seekto( backup_boot*sector_size, "backup boot sector" );	  writebuf( (char *) &bs, sizeof (struct msdos_boot_sector),		    "backup boot sector" );	}    }  /* seek to start of FATS and write them all */  seekto( reserved_sectors*sector_size, "first FAT" );  for (x = 1; x <= nr_fats; x++)    {	writebuf( fat, fat_length * sector_size, "FAT" );	}  /* Write the root directory directly after the last FAT. This is the root   * dir area on FAT12/16, and the first cluster on FAT32. */  writebuf( (char *) root_dir, size_root_dir, "root directory" );  if (blank_sector) free( blank_sector );  if (info_sector) free( info_sector );  free (root_dir);   /* Free up the root directory space from setup_tables */  free (fat);  /* Free up the fat table space reserved during setup_tables */	return 0;}/* Report the command usage and return a failure error code *//*voidusage (void){  fatal_error("\Usage: mkdosfs [-A] [-c] [-C] [-v] [-I] [-l bad-block-file] [-b backup-boot-sector]\n\       [-m boot-msg-file] [-n volume-name] [-i volume-id]\n\       [-s sectors-per-cluster] [-S logical-sector-size] [-f number-of-FATs]\n\       [-h hidden-sectors] [-F fat-size] [-r root-dir-entries] [-R reserved-sectors]\n\       /dev/name [blocks]\n");}/* * ++roman: On m68k, check if this is an Atari; if yes, turn on Atari variant * of MS-DOS filesystem by default. */static void check_atari( void ){#ifdef __mc68000__    FILE *f;    char line[128], *p;    if (!(f = fopen( "/proc/hardware", "r" ))) {	perror( "/proc/hardware" );	return;    }    while( fgets( line, sizeof(line), f ) ) {	if (strncmp( line, "Model:", 6 ) == 0) {	    p = line + 6;	    p += strspn( p, " \t" );	    if (strncmp( p, "Atari ", 6 ) == 0)		atari_format = 1;	    break;	}    }    fclose( f );#endif}/* The "main" entry point into the utility - we pick up the options and attempt to process them in some sort of sensible   way.  In the event that some/all of the options are invalid we need to tell the user so that something can be done! */int format(char *device, int fat){      int argc=5;optind=4;  char **argv=&device;  int c;  char *tmp;  char *listfile = NULL;  FILE *msgfile;  struct stat statbuf;  int i = 0, pos, ch;  int create = 0;  unsigned long long cblocks;  if (argc && *argv) {  char *p;  program_name=*argv; if((p=strrchr(program_name,'/')))program_name=p+1;}  check_atari();  size_fat=fat;	if (size_fat != 12 && size_fat != 16 && size_fat != 32)	  	{	    printf ("Bad FAT type \n");	    return _ERROR_;		  }	size_fat_by_user = 1;  	ignore_full_disk = 1;  if (optind < argc)   	 {   	device_name = *argv;  /* Determine the number of blocks in the FS */      if (!create)	         {			cblocks = count_blocks (device_name); /*  Have a look and see! */			if(cblocks==_ERROR_)					return _ERROR_;				}  	  }  /*if (optind == argc - 2)	*//*  Either check the user specified number */    /*	{      blocks = strtoull (argv[optind + 1], &tmp, 0);	      if (!create && blocks != cblocks)		{	  		fprintf (stderr, "Warning: block count mismatch: ");	  		fprintf (stderr, "found %llu but assuming %llu.\n",cblocks,blocks);		}    	}  else*/ if (optind == argc - 1)	/*  Or use value found */   	 {      if (create)			return _ERROR_;				//die( "Need intended size with -C." );      blocks = cblocks;      tmp = "";  	  }  else   	 {      fprintf (stderr, "No device specified!\n");      return _ERROR_;  	  }  if (*tmp)   	 {      printf ("Bad block count : %s\n", argv[optind + 1]);      return _ERROR_;    	}  /*if (check && listfile)	 	   {		 printf ("-c and -l are incompatible \n");		return _ERROR_;	}*/  if (!create)	 {    	if(check_mount (device_name)==_ERROR_)	return _ERROR_;	/* Is the device already mounted? */    	dev = open (device_name, O_RDWR);	/* Is it a suitable device to build the FS on? */    	if (dev < 0)                 {			printf ("unable to open %s");			return _ERROR_;		 } 	}  else {      off_t offset = blocks*BLOCK_SIZE - 1;      char null = 0;      /* create the file */      dev = open( device_name, O_RDWR|O_CREAT|O_TRUNC, 0666 );      if (dev < 0)			{				//die("unable to create %s");				return _ERROR_;			}      /* seek to the intended end-1, and write one byte. this creates a       * sparse-as-possible file of appropriate size. */      if (llseek( dev, offset, SEEK_SET ) != offset)			{				printf( "seek failed" );				return _ERROR_;			}      if (write( dev, &null, 1 ) < 0)			{				printf( "write failed" );				return _ERROR_;			}      if (llseek( dev, 0, SEEK_SET ) != 0)			{				printf( "seek failed" );				return _ERROR_;			}		  }    if (fstat (dev, &statbuf) < 0)    	{		printf("unable to stat %s");		return _ERROR_;	}  if (!S_ISBLK (statbuf.st_mode))	 {    statbuf.st_rdev = 0;    check = 0;	 }  else    /*     * Ignore any 'full' fixed disk devices, if -I is not given.     * On a MO-disk one doesn't need partitions.  The filesytem can go     * directly to the whole disk.  Under other OSes this is known as     * the 'superfloppy' format.  As I don't know how to find out if     * this is a MO disk I introduce a -I (ignore) switch.  -Joey     */    if (!ignore_full_disk && (	(statbuf.st_rdev & 0xff3f) == 0x0300 || /* hda, hdb */	(statbuf.st_rdev & 0xff0f) == 0x0800 || /* sd */	(statbuf.st_rdev & 0xff3f) == 0x0d00 || /* xd */	(statbuf.st_rdev & 0xff3f) == 0x1600 )  /* hdc, hdd */	)	     {			printf("Will not try to make filesystem on full-disk device '%s' (use -I if wanted)");			return _ERROR_;	     }  if(establish_params (statbuf.st_rdev,statbuf.st_size)==_ERROR_)	return _ERROR_;	                                /* Establish the media parameters */  if(setup_tables ()==_ERROR_)	return _ERROR_;		/* Establish the file system tables */  if (check)			/* Determine any bad block locations and mark them */		{		if(check_blocks ()==_ERROR_)				return	_ERROR_;		}  else if (listfile)		{		if(get_list_blocks (listfile)==_ERROR_)					return _ERROR_;		}  if(write_tables ()==_ERROR_)	return _ERROR_;		/* Write the file system tables away! */  return (_OK_);			/* Terminate with no errors! */}int main(){ if(!format("/dev/sdb1", 16))	{		printf("format successed! \n");		return _OK_;	}	else 		printf("format failed! \n");		return _ERROR_;}/* That's All Folks *//* Local Variables: *//* tab-width: 8     *//* End:             */

⌨️ 快捷键说明

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