static int read_file(struct Stream_t *Stream, char *buf, off_t iwhere, size_t len) { DeclareThis(struct File_t); off_t pos; off_t where = truncBytes32(iwhere); struct Stream_t *Disk = This->Fs->Next; int err = This->map(This, where, &len, MT_READ, &pos); return (err <= 0) ? err : READS(Disk, buf, pos, len); }
void dir() { char buffer[512]; char fname[512]; int index = 0; int i; int runs = 0; int size = 0; int total = 0; READS(buffer,2); while(buffer[index] != 0x0) { for (i = 0; i < 6; ++i) { fname[i] = buffer[index + i]; } for (i = 6; i<32; ++i) { if (buffer[index + i] == 0x0) { break; } else { ++size; } } total = total + size; /* CHeck if filename starts with a capital */ if(fname[0] >= 'A' && fname[0] <= 'Z') { } else { PRINTS("File name: \0"); PRINTS(fname); PRINTS(" File size: \0"); PRINTN(size); PRINTS("\r\n\0"); } size = 0; runs = runs + 6; index = index + 32; } PRINTS("\r\nTotal Sectors used: \0"); PRINTN(total); PRINTS("\r\nSectors free: \0"); PRINTN(48-total); }
static int read_file(Stream_t *Stream, char *buf, mt_off_t iwhere, size_t len) { DeclareThis(File_t); mt_off_t pos; int err; off_t where = truncBytes32(iwhere); Stream_t *Disk = This->Fs->Next; err = This->map(This, where, &len, MT_READ, &pos); if(err <= 0) return err; return READS(Disk, buf, pos, len); }
static int buf_read(Stream_t *Stream, char *buf, mt_off_t start, size_t len) { size_t length; int offset; char *disk_ptr; int ret; DeclareThis(Buffer_t); if(!len) return 0; /*fprintf(stderr, "buf read %x %x %x\n", Stream, start, len);*/ switch(isInBuffer(This, start, &len)) { case OUTSIDE: case APPEND: /* always load until the end of the cylinder */ length = This->cylinderSize - (This->current + This->cur_size) % This->cylinderSize; maximize(length, This->size - This->cur_size); /* read it! */ ret=READS(This->Next, This->buf + This->cur_size, This->current + This->cur_size, length); if ( ret < 0 ) return ret; This->cur_size += ret; if (This->current+This->cur_size < start) { fprintf(stderr, "Short buffer fill\n"); exit(1); } break; case INSIDE: /* nothing to do */ break; case ERROR: return -1; } offset = OFFSET; disk_ptr = This->buf + offset; maximize(len, This->cur_size - offset); memcpy(buf, disk_ptr, len); return len; }
void mcat(int argc, char **argv, int type) { struct device *dev; struct device out_dev; char *drive, name[EXPAND_BUF]; char errmsg[200]; Stream_t *Stream; char buf[BUF_SIZE]; mt_off_t address = 0; char mode = O_RDONLY; int optindex = 1; size_t len; noPrivileges = 1; if (argc < 2) { usage(); } if (argv[1][0] == '-') { if (argv[1][1] != 'w') { usage(); } mode = O_WRONLY; optindex++; } if (argc - optindex < 1) usage(); if (skip_drive(argv[optindex]) == argv[optindex]) usage(); drive = get_drive(argv[optindex], NULL); /* check out a drive whose letter and parameters match */ sprintf(errmsg, "Drive '%s:' not supported", drive); Stream = NULL; for (dev=devices; dev->name; dev++) { FREE(&Stream); if (strcmp(dev->drive, drive) != 0) continue; out_dev = *dev; expand(dev->name,name); #ifdef USING_NEW_VOLD strcpy(name, getVoldName(dev, name)); #endif Stream = 0; #ifdef USE_XDF Stream = XdfOpen(&out_dev, name, mode, errmsg, 0); if(Stream) out_dev.use_2m = 0x7f; #endif #ifdef USE_FLOPPYD if(!Stream) Stream = FloppydOpen(&out_dev, dev, name, mode, errmsg, 0, 1); #endif if (!Stream) Stream = SimpleFileOpen(&out_dev, dev, name, mode, errmsg, 0, 1, 0); if( !Stream) continue; break; } /* print error msg if needed */ if ( dev->drive == 0 ) { FREE(&Stream); fprintf(stderr,"%s\n",errmsg); exit(1); } if (mode == O_WRONLY) { while ((len = fread(buf, 1, BUF_SIZE, stdin)) == BUF_SIZE) { WRITES(Stream, buf, address, BUF_SIZE); address += BUF_SIZE; } if (len) WRITES(Stream, buf, address, len); } else { while ((len = READS(Stream, buf, address, BUF_SIZE)) == BUF_SIZE) { fwrite(buf, 1, BUF_SIZE, stdout); address += BUF_SIZE; } if (len) fwrite(buf, 1, len, stdout); } FREE(&Stream); exit(0); }
int read_pass_through(struct Stream_t *Stream, char *buf, off_t start, size_t len) { return READS(Stream->Next, buf, start, len); }
static int buf_write(Stream_t *Stream, char *buf, mt_off_t start, size_t len) { char *disk_ptr; DeclareThis(Buffer_t); int offset, ret; if(!len) return 0; This->ever_dirty = 1; #ifdef DEBUG fprintf(stderr, "buf write %x %02x %08x %08x -- %08x %08x -- %08x\n", Stream, (unsigned char) This->buf[0], start, len, This->current, This->cur_size, This->size); fprintf(stderr, "%d %d %d %x %x\n", start == This->current + This->cur_size, This->cur_size < This->size, len >= This->sectorSize, len, This->sectorSize); #endif switch(isInBuffer(This, start, &len)) { case OUTSIDE: #ifdef DEBUG fprintf(stderr, "outside\n"); #endif if(start % This->cylinderSize || len < This->sectorSize) { size_t readSize; readSize = This->cylinderSize - This->current % This->cylinderSize; ret=READS(This->Next, This->buf, This->current, readSize); /* read it! */ if ( ret < 0 ) return ret; This->cur_size = ret; /* for dosemu. Autoextend size */ if(!This->cur_size) { memset(This->buf,0,readSize); This->cur_size = readSize; } offset = OFFSET; break; } /* FALL THROUGH */ case APPEND: #ifdef DEBUG fprintf(stderr, "append\n"); #endif len = ROUND_DOWN(len, This->sectorSize); offset = OFFSET; maximize(len, This->size - offset); This->cur_size += len; if(This->Next->Class->pre_allocate) PRE_ALLOCATE(This->Next, This->current + This->cur_size); break; case INSIDE: /* nothing to do */ #ifdef DEBUG fprintf(stderr, "inside\n"); #endif offset = OFFSET; maximize(len, This->cur_size - offset); break; case ERROR: return -1; default: #ifdef DEBUG fprintf(stderr, "Should not happen\n"); #endif exit(1); } disk_ptr = This->buf + offset; /* extend if we write beyond end */ if(offset + len > This->cur_size) { len -= (offset + len) % This->sectorSize; This->cur_size = len + offset; } memcpy(disk_ptr, buf, len); if(!This->dirty || offset < This->dirty_pos) This->dirty_pos = ROUND_DOWN(offset, This->sectorSize); if(!This->dirty || offset + len > This->dirty_end) This->dirty_end = ROUND_UP(offset + len, This->sectorSize); if(This->dirty_end > This->cur_size) { fprintf(stderr, "Internal error, dirty end too big %x %x %x %d %x\n", This->dirty_end, (unsigned int) This->cur_size, (unsigned int) len, (int) offset, (int) This->sectorSize); fprintf(stderr, "offset + len + grain - 1 = %x\n", (int) (offset + len + This->sectorSize - 1)); fprintf(stderr, "ROUNDOWN(offset + len + grain - 1) = %x\n", (int)ROUND_DOWN(offset + len + This->sectorSize - 1, This->sectorSize)); fprintf(stderr, "This->dirty = %d\n", This->dirty); exit(1); } This->dirty = 1; return len; }
void mcat(int argc, char **argv, int type) { struct device *dev; struct device out_dev; char drive, name[EXPAND_BUF]; char errmsg[200]; Stream_t *Stream; char buf[BUF_SIZE]; mt_off_t address = 0; char mode = O_RDONLY; int optindex = 1; size_t len; noPrivileges = 1; if (argc < 2) { usage(); } if (argv[1][0] == '-') { if (argv[1][1] != 'w') { usage(); } mode = O_WRONLY; optindex++; } if (argc - optindex < 1) usage(); if (!argv[optindex][0] || argv[optindex][1] != ':' || argv[optindex][2]) { usage(); } drive = toupper(argv[optindex][0]); /* check out a drive whose letter and parameters match */ sprintf(errmsg, "Drive '%c:' not supported", drive); Stream = NULL; for (dev=devices; dev->name; dev++) { FREE(&Stream); if (dev->drive != drive) continue; out_dev = *dev; expand(dev->name,name); #ifdef USING_NEW_VOLD strcpy(name, getVoldName(dev, name)); #endif Stream = 0; #ifdef USE_XDF Stream = XdfOpen(&out_dev, name, mode, errmsg, 0); if(Stream) out_dev.use_2m = 0x7f; #endif #ifdef USE_FLOPPYD if(!Stream) Stream = FloppydOpen(&out_dev, dev, name, mode, errmsg, 0, 1); #endif if (!Stream) Stream = SimpleFileOpen(&out_dev, dev, name, mode, errmsg, 0, 1, 0); if( !Stream) continue; break; } /* print error msg if needed */ if ( dev->drive == 0 ){ FREE(&Stream); fprintf(stderr,"%s\n",errmsg); exit(1); } if (mode == O_WRONLY) { mt_size_t size=0; size = out_dev.sectors * out_dev.heads * out_dev.tracks; size *= 512; while ((len = fread(buf, 1, bufLen(BUF_SIZE, size, address), stdin)) > 0) { int r = WRITES(Stream, buf, address, len); fprintf(stderr, "Wrote to %d\n", (int) address); if(r < 0) break; address += len; } } else { while ((len = READS(Stream, buf, address, BUF_SIZE)) > 0) { fwrite(buf, 1, len, stdout); address += len; } } FREE(&Stream); exit(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); }
void mbadblocks(int argc, char **argv, int type) { unsigned int i; unsigned int startSector=2; unsigned int endSector=0; struct MainParam_t mp; Fs_t *Fs; Stream_t *Dir; int ret; char *filename = NULL; char c; unsigned int badClus; int sectorMode=0; int writeMode=0; while ((c = getopt(argc, argv, "i:s:cwS:E:")) != EOF) { switch(c) { case 'i': set_cmd_line_image(optarg, 0); break; case 'c': checkListTwice(filename); filename = strdup(optarg); break; case 's': checkListTwice(filename); filename = strdup(optarg); sectorMode = 1; break; case 'S': startSector = atol(optarg); break; case 'E': endSector = atol(optarg); break; case 'w': writeMode = 1; break; case 'h': usage(0); default: usage(1); } } if (argc != optind+1 || !argv[optind][0] || argv[optind][1] != ':' || argv[optind][2]) { usage(1); } init_mp(&mp); Dir = open_root_dir(argv[optind][0], O_RDWR, NULL); if (!Dir) { fprintf(stderr,"%s: Cannot initialize drive\n", argv[0]); exit(1); } Fs = (Fs_t *)GetFs(Dir); in_len = Fs->cluster_size * Fs->sector_size; in_buf = malloc(in_len); if(!in_buf) { printOom(); ret = 1; goto exit_0; } if(writeMode) { int i; pat_buf=malloc(in_len * N_PATTERN); if(!pat_buf) { printOom(); ret = 1; goto exit_0; } srandom(time(NULL)); for(i=0; i < in_len * N_PATTERN; i++) { pat_buf[i] = random(); } } for(i=0; i < Fs->clus_start; i++ ){ ret = READS(Fs->Next, in_buf, sectorsToBytes((Stream_t*)Fs, i), Fs->sector_size); if( ret < 0 ){ perror("early error"); goto exit_0; } if(ret < (signed int) Fs->sector_size){ fprintf(stderr,"end of file in file_read\n"); ret = 1; goto exit_0; } } ret = 0; badClus = Fs->last_fat + 1; if(startSector < 2) startSector = 2; if(endSector > Fs->num_clus + 2 || endSector <= 0) endSector = Fs->num_clus + 2; if(filename) { char line[80]; FILE *f = fopen(filename, "r"); if(f == NULL) { fprintf(stderr, "Could not open %s (%s)\n", filename, strerror(errno)); ret = 1; goto exit_0; } while(fgets(line, sizeof(line), f)) { char *ptr = line + strspn(line, " \t"); long offset = strtoul(ptr, 0, 0); if(sectorMode) offset = (offset-Fs->clus_start)/Fs->cluster_size + 2; if(offset < 2) { fprintf(stderr, "Sector before start\n"); } else if(offset >= Fs->num_clus) { fprintf(stderr, "Sector beyond end\n"); } else { mark(Fs, offset, badClus); ret = 1; } } } else { Stream_t *dev; dev = Fs->Next; if(dev->Next) dev = dev->Next; in_len = Fs->cluster_size * Fs->sector_size; if(writeMode) { /* Write pattern */ for(i=startSector; i< endSector; i++){ if(got_signal) break; progress(i, Fs->num_clus); ret |= scan(Fs, dev, i, badClus, pat_buf + in_len * (i % N_PATTERN), 1); } /* Flush cache, so that we are sure we read the data back from disk, rather than from the cache */ if(!got_signal) DISCARD(dev); /* Read data back, and compare to pattern */ for(i=startSector; i< endSector; i++){ if(got_signal) break; progress(i, Fs->num_clus); ret |= scan(Fs, dev, i, badClus, pat_buf + in_len * (i % N_PATTERN), 0); } } else { for(i=startSector; i< endSector; i++){ if(got_signal) break; progress(i, Fs->num_clus); ret |= scan(Fs, dev, i, badClus, NULL, 0); } } } exit_0: FREE(&Dir); exit(ret); }
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); }