static int menu(const char* path) { static const void *prompt = "\nCommand (m for help): "; char cmd[256]; while(1) { printf(prompt); int res = read(stdin_fd, cmd, sizeof(cmd)); if(res == -1) { printf("error reading command\n"); return 1; } printf("\n"); if(cmd[res-1] == '\n') cmd[res-1] = 0; else // strange, non canonic input ... cmd[res] = 0; if(strcmp(cmd, "p") == 0) read_partition_table(path); else if(strcmp(cmd, "m") == 0) print_help(); else if(strcmp(cmd, "q") == 0) return 0; else printf("%s: unknown command\n", cmd); } }
/* Scans BLOCK for partitions of interest to Pintos. */ void partition_scan (struct block *block) { int part_nr = 0; read_partition_table (block, 0, 0, &part_nr); if (part_nr == 0) printf ("%s: Device contains no partitions\n", block_name (block)); }
int main(int argc, char *argv[]) { int p = 0; int rerrno = 0; int result; int parts; uint8_t temp[512]; struct partition *part_list; if(argc < 2) { printf("Please specify a disk image to work on.\n"); exit(-2); } block_pc_set_image_name(argv[1]); // int v; printf("Running FAT tests...\n\n"); printf("[%4d] start block device emulation...", p++); printf(" %d\n", block_init()); printf("[%4d] mount filesystem, FAT32", p++); result = fat_mount(0, block_get_volume_size(), PART_TYPE_FAT32); printf(" %d\n", result); if(result != 0) { // mounting failed. // try listing the partitions block_read(0, temp); parts = read_partition_table(temp, block_get_volume_size(), &part_list); printf("Found %d valid partitions.\n", parts); if(parts > 0) { result = fat_mount(part_list[0].start, part_list[0].length, part_list[0].type); } if(result != 0) { printf("Mount failed\n"); exit(-2); } } printf("Part type = %02X\n", fatfs.type); // p = test_open(p); int fd; int i; char block_o_data[1024]; uint32_t temp_uint = 0xDEADBEEF; memset(block_o_data, 0x42, 1024); // printf("Open\n"); // fd = fat_open("/newfile.txt", O_WRONLY | O_CREAT, 0777, &rerrno); // printf("fd = %d, errno=%d (%s)\n", fd, rerrno, strerror(rerrno)); // if(fd > -1) { // printf("Write\n"); // fat_write(fd, "Hello World\n", 12, &rerrno); // printf("errno=%d (%s)\n", rerrno, strerror(rerrno)); // printf("Close\n"); // fat_close(fd, &rerrno); // printf("errno=%d (%s)\n", rerrno, strerror(rerrno)); // } // printf("Open\n"); // fd = fat_open("/newfile.png", O_WRONLY | O_CREAT, 0777, &rerrno); // printf("fd = %d, errno=%d (%s)\n", fd, rerrno, strerror(rerrno)); // if(fd > -1) { // fp = fopen("gowrong_draft1.png", "rb"); // fseek(fp, 0, SEEK_END); // len = ftell(fp); // d = malloc(len); // fseek(fp, 0, SEEK_SET); // fread(d, 1, len, fp); // fclose(fp); // printf("Write PNG\n"); // fat_write(fd, d, len, &rerrno); // printf("errno=%d (%s)\n", rerrno, strerror(rerrno)); // printf("Close\n"); // fat_close(fd, &rerrno); // printf("errno=%d (%s)\n", rerrno, strerror(rerrno)); // } printf("errno = (%d) %s\n", rerrno, strerror(rerrno)); result = fat_mkdir("/foo", 0777, &rerrno); printf("mkdir /foo: %d (%d) %s\n", result, rerrno, strerror(rerrno)); result = fat_mkdir("/foo/bar", 0777, &rerrno); printf("mkdir /foo/bar: %d (%d) %s\n", result, rerrno, strerror(rerrno)); result = fat_mkdir("/web", 0777, &rerrno); printf("mkdir /web: %d (%d) %s\n", result, rerrno, strerror(rerrno)); if((fd = fat_open("/foo/bar/file.html", O_WRONLY | O_CREAT, 0777, &rerrno)) == -1) { printf("Couldn't open file (%d) %s\n", rerrno, strerror(rerrno)); exit(-1); } for(i=0; i<20; i++) { // printf("fd.cluster = %d\n", file_num[fd].full_first_cluster); if(fat_write(fd, block_o_data, 1024, &rerrno) == -1) { printf("Error writing to new file (%d) %s\n", rerrno, strerror(rerrno)); } } if(fat_close(fd, &rerrno)) { printf("Error closing file (%d) %s\n", rerrno, strerror(rerrno)); } printf("Open directory\n"); if((fd = fat_open("/foo/bar", O_RDONLY, 0777, &rerrno)) < 0) { printf("Failed to open directory (%d) %s\n", rerrno, strerror(rerrno)); exit(-1); } struct dirent de; while(!fat_get_next_dirent(fd, &de, &rerrno)) { printf("%s\n", de.d_name); } printf("Directory read failed. (%d) %s\n", rerrno, strerror(rerrno)); if(fat_close(fd, &rerrno)) { printf("Error closing directory, (%d) %s\n", rerrno, strerror(rerrno)); } if(fat_open("/web/version.txt", O_RDONLY, 0777, &rerrno) < 0) { printf("Error opening missing file (%d) %s\n", rerrno, strerror(rerrno)); } else { printf("success! opened non existent file for reading.\n"); } printf("Trying to write a big file.\n"); if((fd = fat_open("big_file.bin", O_WRONLY | O_CREAT, 0777, &rerrno))) { printf("Error opening a file for writing.\n"); } for(i=0; i<1024 * 1024 * 10; i++) { if((i & 0xfff) == 0) printf("Written %d bytes\n", i * 4); fat_write(fd, &temp_uint, 4, &rerrno); } fat_close(fd, &rerrno); // result = fat_rmdir("/foo/bar", &rerrno); // printf("rmdir /foo/bar: %d (%d) %s\n", result, rerrno, strerror(rerrno)); // // result = fat_rmdir("/foo", &rerrno); // printf("rmdir /foo: %d (%d) %s\n", result, rerrno, strerror(rerrno)); block_pc_snapshot_all("writenfs.img"); exit(0); }
/* Reads the partition table in the given SECTOR of BLOCK and scans it for partitions of interest to Pintos. If SECTOR is 0, so that this is the top-level partition table on BLOCK, then PRIMARY_EXTENDED_SECTOR is not meaningful; otherwise, it should designate the sector of the top-level extended partition table that was traversed to arrive at SECTOR, for use in finding logical partitions (see the large comment below). PART_NR points to the number of non-empty primary or logical partitions already encountered on BLOCK. It is incremented as partitions are found. */ static void read_partition_table (struct block *block, block_sector_t sector, block_sector_t primary_extended_sector, int *part_nr) { /* Format of a partition table entry. See [Partitions]. */ struct partition_table_entry { uint8_t bootable; /* 0x00=not bootable, 0x80=bootable. */ uint8_t start_chs[3]; /* Encoded starting cylinder, head, sector. */ uint8_t type; /* Partition type (see partition_type_name). */ uint8_t end_chs[3]; /* Encoded ending cylinder, head, sector. */ uint32_t offset; /* Start sector offset from partition table. */ uint32_t size; /* Number of sectors. */ } PACKED; /* Partition table sector. */ struct partition_table { uint8_t loader[446]; /* Loader, in top-level partition table. */ struct partition_table_entry partitions[4]; /* Table entries. */ uint16_t signature; /* Should be 0xaa55. */ } PACKED; struct partition_table *pt; size_t i; /* Check SECTOR validity. */ if (sector >= block_size (block)) { printf ("%s: Partition table at sector %"PRDSNu" past end of device.\n", block_name (block), sector); return; } /* Read sector. */ ASSERT (sizeof *pt == BLOCK_SECTOR_SIZE); pt = malloc (sizeof *pt); if (pt == NULL) PANIC ("Failed to allocate memory for partition table."); block_read (block, 0, pt); /* Check signature. */ if (pt->signature != 0xaa55) { if (primary_extended_sector == 0) printf ("%s: Invalid partition table signature\n", block_name (block)); else printf ("%s: Invalid extended partition table in sector %"PRDSNu"\n", block_name (block), sector); free (pt); return; } /* Parse partitions. */ for (i = 0; i < sizeof pt->partitions / sizeof *pt->partitions; i++) { struct partition_table_entry *e = &pt->partitions[i]; if (e->size == 0 || e->type == 0) { /* Ignore empty partition. */ } else if (e->type == 0x05 /* Extended partition. */ || e->type == 0x0f /* Windows 98 extended partition. */ || e->type == 0x85 /* Linux extended partition. */ || e->type == 0xc5) /* DR-DOS extended partition. */ { printf ("%s: Extended partition in sector %"PRDSNu"\n", block_name (block), sector); /* The interpretation of the offset field for extended partitions is bizarre. When the extended partition table entry is in the master boot record, that is, the device's primary partition table in sector 0, then the offset is an absolute sector number. Otherwise, no matter how deep the partition table we're reading is nested, the offset is relative to the start of the extended partition that the MBR points to. */ if (sector == 0) read_partition_table (block, e->offset, e->offset, part_nr); else read_partition_table (block, e->offset + primary_extended_sector, primary_extended_sector, part_nr); } else { ++*part_nr; found_partition (block, e->type, e->offset + sector, e->size, *part_nr); } } free (pt); }