示例#1
0
文件: readwrite.c 项目: ankitC/fsck
int main (int argc, char **argv)
{
	/* This is a sample program.  If you want to print out sector 57 of
	 * the disk, then run the program as:
	 *
	 *    ./readwrite disk 57
	 *
	 * You'll of course want to replace this with your own functions.
	 */

	unsigned char buf[sector_size_bytes];        /* temporary buffer */
	int           the_sector;                     /* IN: sector to read */

	if ((device = open(argv[1], O_RDWR)) == -1) {
		perror("Could not open device file");
		exit(-1);
	}

	the_sector = atoi(argv[2]);
	printf("Dumping sector %d:\n", the_sector);
	read_sectors(the_sector, 1, buf);
	print_sector(buf);
	check(buf);
	close(device);
	return 0;
}
示例#2
0
文件: cache_t3.cpp 项目: gberl001/SD
// Read a sector into the cache.  If the sector is already cached,
// of course no actual read occurs.  This is the primary function
// used to access the SD card.
//
sector_t * SDCache::read(uint32_t lba, bool is_fat)
{
	sector_t *ret = NULL;
	//uint32_t slot=0, ucount=0;

	// the entire read operation, including all cache manipulation,
	// needs to be protected with exclusive access to the hardware.
	//Serial.printf("cache read: lba = %d\n", lba);
	SPI.beginTransaction(SD_SPI_SPEED);
	// does the cache already have the sector?
	cache_t *c = find(lba);
	if (c) {
		ret = &c->data;
	} else {
		c = empty();
		if (c != NULL) {
			// TODO: if dirty, write to SD card
			if (SDClass::sd_read(lba, &c->data)) {
				item = c;
				c->lba = lba;
				c->usagecount = 1;
				c->flags = CACHE_FLAG_HAS_DATA;
				if (is_fat) c->flags |= CACHE_FLAG_IS_FAT;
				ret = &c->data;
				 #ifdef PRINT_SECTORS
				 Serial.printf("cache read %u\n", lba);
				 print_sector(&c->data);
				 #endif
			}
		}
	}
	//if (c) slot = c - cache, ucount = c->usagecount;
	SPI.endTransaction();
	//if (ret) {
		//Serial.printf("read    %u, %u, slot %u\n", lba, ucount, slot);
	//} else {
		//Serial.printf("read    %u, FAIL\n", lba);
	//}
	return ret;
}
示例#3
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);
}
示例#4
0
void minfo(int argc, char **argv, int type)
{
	struct bootsector boot0;
#define boot (&boot0)
	char name[EXPAND_BUF];
	int media;
	int tot_sectors;
	struct device dev;
	char *drive;
	int verbose=0;
	int c;
	Stream_t *Stream;
	struct label_blk_t *labelBlock;
	
	while ((c = getopt(argc, argv, "v")) != EOF) {
		switch (c) {
			case 'v':
				verbose = 1;
				break;
			default:
				usage();
		}
	}

	if(argc == optind)
		usage();

	for(;optind < argc; optind++) {
		if(skip_drive(argv[optind]) == argv[optind])
			usage();
		drive = get_drive(argv[optind], NULL);

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

		tot_sectors = DWORD(bigsect);
		SET_INT(tot_sectors, WORD(psect));
		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);
		printf("mformat command line: mformat -t %d -h %d -s %d ",
		       dev.tracks, dev.heads, dev.sectors);
		if(DWORD(nhs))
			printf("-H %d ", DWORD(nhs));
		printf("%s:\n", drive);
		printf("\n");
		
		printf("bootsector information\n");
		printf("======================\n");
		printf("banner:\"%8s\"\n", boot->banner);
		printf("sector size: %d bytes\n", WORD(secsiz));
		printf("cluster size: %d sectors\n", boot->clsiz);
		printf("reserved (boot) sectors: %d\n", WORD(nrsvsect));
		printf("fats: %d\n", boot->nfat);
		printf("max available root directory slots: %d\n", 
		       WORD(dirents));
		printf("small size: %d sectors\n", WORD(psect));
		printf("media descriptor byte: 0x%x\n", boot->descr);
		printf("sectors per fat: %d\n", WORD(fatlen));
		printf("sectors per track: %d\n", WORD(nsect));
		printf("heads: %d\n", WORD(nheads));
		printf("hidden sectors: %d\n", DWORD(nhs));
		printf("big size: %d sectors\n", DWORD(bigsect));

		if(WORD(fatlen)) {
		    labelBlock = &boot->ext.old.labelBlock;
		} else {
		    labelBlock = &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(fatlen)){
			printf("Big fatlen=%u\n",
			       DWORD(ext.fat32.bigFat));
			printf("Extended flags=0x%04x\n",
			       WORD(ext.fat32.extFlags));
			printf("FS version=0x%04x\n",
			       WORD(ext.fat32.fsVersion));
			printf("rootCluster=%u\n",
			       DWORD(ext.fat32.rootCluster));
			if(WORD(ext.fat32.infoSector) != MAX32)
				printf("infoSector location=%d\n",
				       WORD(ext.fat32.infoSector));
			if(WORD(ext.fat32.backupBoot) != MAX32)
				printf("backup boot sector=%d\n",
				       WORD(ext.fat32.backupBoot));
			displayInfosector(Stream,boot);
		}

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

			printf("\n");
			size = WORD(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);
		}
	}

	exit(0);
}