Esempio n. 1
0
void minfo(int argc, char **argv, int type)
{
	union bootsector boot;

	char name[EXPAND_BUF];
	int media;
	unsigned long tot_sectors;
	int size_code;
	int sector_size;
	int i;
	struct device dev;
	char drive;
	int verbose=0;
	int c;
	Stream_t *Stream;
	struct label_blk_t *labelBlock;
	int have_drive = 0;

	unsigned long sect_per_track;
	int tracks_match=0;
	int hidden;

	char *imgFile=NULL;
	
	if(helpFlag(argc, argv))
		usage(0);
	while ((c = getopt(argc, argv, "i:vh")) != EOF) {
		switch (c) {
			case 'i':
				set_cmd_line_image(optarg);
				imgFile=optarg;
				break;
			case 'v':
				verbose = 1;
				break;
			case 'h':
				usage(0);
			default:
				usage(1);
		}
	}

	for(;optind <= argc; optind++) {
		if(optind == argc) {
			if(have_drive)
				break;
			drive = get_default_drive();
		} else {
			if(!argv[optind][0] || argv[optind][1] != ':')
				usage(1);
			drive = toupper(argv[optind][0]);
		}
		have_drive = 1;

		if(! (Stream = find_device(drive, O_RDONLY, &dev, &boot, 
					   name, &media, 0, NULL)))
			exit(1);

		tot_sectors = DWORD_S(bigsect);
		SET_INT(tot_sectors, WORD_S(psect));
		sector_size = WORD_S(secsiz);
		size_code=2;
		for(i=0; i<7; i++) {
			if(sector_size == 128 << i) {
				size_code = i;
				break;
			}
		}
		printf("device information:\n");
		printf("===================\n");
		printf("filename=\"%s\"\n", name);
		printf("sectors per track: %d\n", dev.sectors);
		printf("heads: %d\n", dev.heads);
		printf("cylinders: %d\n\n", dev.tracks);

		sect_per_track = dev.sectors * dev.heads;
		if(sect_per_track != 0) {
			printf("mformat command line: mformat ");
			hidden = DWORD_S(nhs);
			if(tot_sectors ==
			   dev.tracks * sect_per_track - hidden % sect_per_track) {
				tracks_match=1;
				printf("-t %d ", dev.tracks);
			} else {
				printf("-T %ld ", tot_sectors);
			}
			if(imgFile != NULL)
				printf("-i %s ", imgFile);
			printf (" -h %d -s %d ", dev.heads, dev.sectors);
			if(hidden || !tracks_match)
				printf("-H %d ", hidden);
			if(size_code != 2)
				printf("-S %d ",size_code);
			printf("%c:\n", tolower(drive));
			printf("\n");
		}
		printf("bootsector information\n");
		printf("======================\n");
		printf("banner:\"%.8s\"\n", boot.boot.banner);
		printf("sector size: %d bytes\n", WORD_S(secsiz));
		printf("cluster size: %d sectors\n", boot.boot.clsiz);
		printf("reserved (boot) sectors: %d\n", WORD_S(nrsvsect));
		printf("fats: %d\n", boot.boot.nfat);
		printf("max available root directory slots: %d\n", 
		       WORD_S(dirents));
		printf("small size: %d sectors\n", WORD_S(psect));
		printf("media descriptor byte: 0x%x\n", boot.boot.descr);
		printf("sectors per fat: %d\n", WORD_S(fatlen));
		printf("sectors per track: %d\n", WORD_S(nsect));
		printf("heads: %d\n", WORD_S(nheads));
		printf("hidden sectors: %d\n", DWORD_S(nhs));
		printf("big size: %d sectors\n", DWORD_S(bigsect));

		if(WORD_S(fatlen)) {
		    labelBlock = &boot.boot.ext.old.labelBlock;
		} else {
		    labelBlock = &boot.boot.ext.fat32.labelBlock;
		}

		printf("physical drive id: 0x%x\n", 
		       labelBlock->physdrive);
		printf("reserved=0x%x\n", 
		       labelBlock->reserved);
		printf("dos4=0x%x\n", 
		       labelBlock->dos4);
		printf("serial number: %08X\n", 
		       _DWORD(labelBlock->serial));
		printf("disk label=\"%11.11s\"\n", 
		       labelBlock->label);
		printf("disk type=\"%8.8s\"\n", 
		       labelBlock->fat_type);

		if(!WORD_S(fatlen)){
			printf("Big fatlen=%u\n",
			       DWORD_S(ext.fat32.bigFat));
			printf("Extended flags=0x%04x\n",
			       WORD_S(ext.fat32.extFlags));
			printf("FS version=0x%04x\n",
			       WORD_S(ext.fat32.fsVersion));
			printf("rootCluster=%u\n",
			       DWORD_S(ext.fat32.rootCluster));
			if(WORD_S(ext.fat32.infoSector) != MAX16)
				printf("infoSector location=%d\n",
				       WORD_S(ext.fat32.infoSector));
			if(WORD_S(ext.fat32.backupBoot) != MAX16)
				printf("backup boot sector=%d\n",
				       WORD_S(ext.fat32.backupBoot));
			displayInfosector(Stream,&boot);
		}

		if(verbose) {
			int size;
			unsigned char *buf;

			printf("\n");
			size = WORD_S(secsiz);
			
			buf = (unsigned char *) malloc(size);
			if(!buf) {
				fprintf(stderr, "Out of memory error\n");
				exit(1);
			}

			size = READS(Stream, buf, (mt_off_t) 0, size);
			if(size < 0) {
				perror("read boot sector");
				exit(1);
			}

			print_sector("Boot sector hexdump", buf, size);
		}
	}
	FREE(&Stream);
	exit(0);
}
Esempio n. 2
0
void mlabel(int argc, char **argv, int type)
{

	char *newLabel;
	int verbose, clear, interactive, show;
	direntry_t entry;
	int result=0;
	char longname[VBUFSIZE];
	char shortname[45];
	ClashHandling_t ch;
	struct MainParam_t mp;
	Stream_t *RootDir;
	int c;
	int mangled;
	enum { SER_NONE, SER_RANDOM, SER_SET }  set_serial = SER_NONE;
	long serial = 0;
	int need_write_boot = 0;
	int have_boot = 0;
	char *eptr;
	union bootsector boot;
	Stream_t *Fs=0;
	int r;
	struct label_blk_t *labelBlock;
	int isRo=0;
	int *isRop=NULL;

	init_clash_handling(&ch);
	ch.name_converter = label_name;
	ch.ignore_entry = -2;

	verbose = 0;
	clear = 0;
	show = 0;

	if(helpFlag(argc, argv))
		usage(0);
	while ((c = getopt(argc, argv, "i:vcsnN:h")) != EOF) {
		switch (c) {
			case 'i':
				set_cmd_line_image(optarg, 0);
				break;
			case 'v':
				verbose = 1;
				break;
			case 'c':
				clear = 1;
				break;
			case 's':
				show = 1;
				break;
			case 'n':
				set_serial = SER_RANDOM;
				srandom((long)time (0));
				serial=random();
				break;
			case 'N':
				set_serial = SER_SET;
				serial = strtol(optarg, &eptr, 16);
				if(*eptr) {
					fprintf(stderr,
						"%s not a valid serial number\n",
						optarg);
					exit(1);
				}
				break;
			case 'h':
				usage(0);
			default:
				usage(1);
			}
	}

	if (argc - optind != 1 || !argv[optind][0] || argv[optind][1] != ':')
		usage(1);

	init_mp(&mp);
	newLabel = argv[optind]+2;
	if(strlen(newLabel) > VBUFSIZE) {
		fprintf(stderr, "Label too long\n");
		FREE(&RootDir);
		exit(1);
	}

	interactive = !show && !clear &&!newLabel[0] &&
		(set_serial == SER_NONE);
	if(!clear && !newLabel[0]) {
		isRop = &isRo;
	}
	RootDir = open_root_dir(argv[optind][0], isRop ? 0 : O_RDWR, isRop);
	if(isRo) {
		show = 1;
		interactive = 0;
	}	
	if(!RootDir) {
		fprintf(stderr, "%s: Cannot initialize drive\n", argv[0]);
		exit(1);
	}

	initializeDirentry(&entry, RootDir);
	r=vfat_lookup(&entry, 0, 0, ACCEPT_LABEL | MATCH_ANY,
		      shortname, longname);
	if (r == -2) {
		FREE(&RootDir);
		exit(1);
	}

	if(show || interactive){
		if(isNotFound(&entry))
			printf(" Volume has no label\n");
		else if (*longname)
			printf(" Volume label is %s (abbr=%s)\n",
			       longname, shortname);
		else
			printf(" Volume label is %s\n",  shortname);

	}

	/* ask for new label */
	if(interactive){
		newLabel = longname;
		fprintf(stderr,"Enter the new volume label : ");
		if(fgets(newLabel, VBUFSIZE, stdin) == NULL) {
			newLabel[0] = '\0';
			fprintf(stderr, "\n");
		}
		if(newLabel[0])
			newLabel[strlen(newLabel)-1] = '\0';
	}

	if((!show || newLabel[0]) && !isNotFound(&entry)){
		/* if we have a label, wipe it out before putting new one */
		if(interactive && newLabel[0] == '\0')
			if(ask_confirmation("Delete volume label (y/n): ")){
				FREE(&RootDir);
				exit(0);
			}
		entry.dir.attr = 0; /* for old mlabel */
		wipeEntry(&entry);
	}

	if (newLabel[0] != '\0') {
		ch.ignore_entry = 1;
		result = mwrite_one(RootDir,newLabel,0,labelit,NULL,&ch) ?
		  0 : 1;
	}

	have_boot = 0;
	if( (!show || newLabel[0]) || set_serial != SER_NONE) {
		Fs = GetFs(RootDir);
		have_boot = (force_read(Fs,boot.characters,0,sizeof(boot)) ==
			     sizeof(boot));
	}

	if(WORD_S(fatlen)) {
	    labelBlock = &boot.boot.ext.old.labelBlock;
	} else {
	    labelBlock = &boot.boot.ext.fat32.labelBlock;
	}

	if(!show || newLabel[0]){
		dos_name_t dosname;
		const char *shrtLabel;
		doscp_t *cp;
		if(!newLabel[0])
			shrtLabel = "NO NAME    ";
		else
			shrtLabel = newLabel;
		cp = GET_DOSCONVERT(Fs);
		label_name(cp, shrtLabel, verbose, &mangled, &dosname);

		if(have_boot && boot.boot.descr >= 0xf0 &&
		   labelBlock->dos4 == 0x29) {
			strncpy(labelBlock->label, dosname.base, 11);
			need_write_boot = 1;

		}
	}

	if((set_serial != SER_NONE) & have_boot) {
		if(have_boot && boot.boot.descr >= 0xf0 &&
		   labelBlock->dos4 == 0x29) {
			set_dword(labelBlock->serial, serial);	
			need_write_boot = 1;
		}
	}

	if(need_write_boot) {
		force_write(Fs, (char *)&boot, 0, sizeof(boot));
	}

	FREE(&RootDir);
	exit(result);
}
Esempio n. 3
0
int fatlabel_set_label(const char* device_name, const char* new_label)
{
	if (strlen(new_label) > VBUFSIZE)
		return -1;

	/*
	 * 1. Init clash handling
	 */
	struct ClashHandling_t ch;
	init_clash_handling(&ch);
	ch.name_converter = label_name;
	ch.ignore_entry = -2;

	/*
	 * 2. Open root dir
	 */
	struct Stream_t* RootDir = fs_init(device_name, O_RDWR);

	if (RootDir)
		RootDir = OpenRoot(RootDir);

	if (!RootDir)
	{
		fprintf(stderr, "Opening root dir failed.\n");
		return -2;
	}

	/*
	 * 3. Init dir entry
	 */
	struct direntry_t entry;
	initializeDirentry(&entry, RootDir);

	/*
	 * 4. Lookup vfat
	 */
	char longname[VBUFSIZE];
	char shortname[45];
	if (vfat_lookup(&entry, 0, 0, ACCEPT_LABEL | MATCH_ANY, shortname, longname) == -2)
	{
		fprintf(stderr, "Looking up vfat failed.\n");
		free_stream(&RootDir);
		return -3;
	}

	/*
	 * 5. Wipe existing entry.
	 */
	if (!isNotFound(&entry))
	{
		/* if we have a label, wipe it out before putting new one */
		entry.dir.attr = 0; /* for old mlabel */
		wipeEntry(&entry);
	}

	/*
	 * 6. Write new entry
	 */
	ch.ignore_entry = 1;

	/* don't try to write the label if it's empty */
	int result = strlen(new_label) ? mwrite_one(RootDir, new_label, labelit, &ch) : 0;

	/*
	 * 7. Load FAT boot record
	 */
	union bootsector boot;
	struct Stream_t* Fs = GetFs(RootDir);
	int have_boot = force_read(Fs, boot.characters, 0, sizeof(boot)) == sizeof(boot);

	struct label_blk_t* labelBlock = WORD_S(fatlen) ? &boot.boot.ext.old.labelBlock : &boot.boot.ext.fat32.labelBlock;

	/*
	 * 8. Get "dosconvert" struct
	 */
	struct dos_name_t dosname;
	struct doscp_t* cp = GET_DOSCONVERT(Fs);

	/*
	 * 9. Convert label
	 */
	int mangled = 0;
	label_name(cp, new_label, &mangled, &dosname);

	/*
	 * 10. Overwrite FAT boot record
	 */
	if (have_boot && boot.boot.descr >= 0xf0 && labelBlock->dos4 == 0x29)
	{
		strncpy(labelBlock->label, dosname.base, 11);
		force_write(Fs, (char *)&boot, 0, sizeof(boot));
	}

	free_stream(&RootDir);
	fs_close(Fs);

	return result;
}