Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local int exPartitionNumber = 1; //must be 0, 1, 2, or 3 (or up to 127 on gpt)
- local char exDir1[9] = "FRED";
- local char exDir1_LENGTH = Strlen(exDir1);
- local char exDir2[9] = "";
- local char exDir2_LENGTH = Strlen(exDir2);
- local char exDir3[9] = "";
- local char exDir3_LENGTH = Strlen(exDir3);
- local uint32 exClusterList = 0x7205; // 0 is off, non-zero builds list
- typedef struct {
- char stuff[446];
- struct PARTs {
- uchar boot_flag;
- uchar CHS_Begin[3];
- uchar type_code <format = hex>;
- uchar CHS_End[3];
- uint32 LBA_Begin <format = hex>;
- uint32 Number_of_Sectors;
- } part[4];
- ushort expectAA55 <format = hex>;
- } MBR;
- typedef struct {
- char signature[8]; //must be EFI PART
- char revision[4];
- uint32 headersize;
- uint32 crc32;
- uint32 reserved;
- uint64 currentLBA <format = hex>;
- uint64 backupLBA <format = hex>;
- uint64 firstUsableLBA <format = hex>;
- uint64 lastUsableLBA <format = hex>;
- uchar disk_GUID[16] <format = hex>;
- uint64 startingLBAofPartitionEntries <format = hex>;
- uint32 numberOfPartitionEntries;
- uint32 sizeOfPartitionEntry;
- uint32 crc32x;
- } GPT_HEADER <size = 512>;
- typedef struct {
- byte partition_type_guid[16];
- byte unique_guid[16];
- uint64 first_lba;
- uint64 last_lba;
- uint64 flags;
- wchar_t name[36];
- } GPT_Partition_Entry;
- local int i;
- local uint64 FAT32_start=0;
- local uint32 Partition_LBA_Begin=0;
- local byte MS_GPT_Basic_data_partition[16] = {
- 0xa2, 0xa0, 0xd0, 0xeb, 0xe5, 0xb9, 0x33, 0x44,
- 0x87, 0xc0, 0x68, 0xb6, 0xb7, 0x26, 0x99, 0xc7 };
- LittleEndian();
- void ExploreGPT( )
- {
- FSeek(0x200);
- GPT_HEADER gpt;
- GPT_Partition_Entry partitions[128];
- if (Memcmp(gpt.signature, "EFI PART", 8)) {
- Printf("ERROR: EFI PART signature not found\n");
- Exit(3);
- }
- for (i=0; i<gpt.numberOfPartitionEntries; i++) {
- if (!Memcmp(partitions[i].partition_type_guid,
- MS_GPT_Basic_data_partition, 16)) {
- Printf("Partition %d is MS Basic data\n", i);
- if (i == exPartitionNumber) {
- Partition_LBA_Begin = partitions[i].first_lba;
- FAT32_start = Partition_LBA_Begin * 512;
- }
- }
- }
- }
- struct {
- MBR mbr;
- if (mbr.expectAA55 != 0xAA55) {
- Printf("error MBR record does not end with AA55!\n");
- Exit(1);
- } else {
- Printf("MBR seems okay, scanning partitions\n");
- }
- if (mbr.part[0].type_code == 0xEE) { //use routine above to find GPT stuff
- Printf("Protective MBR found, searching for GPT partitions instead\n");
- ExploreGPT();
- //all done, try to load VOLUME ID next
- } else { //MBR is home base, use contents for partition info
- for (i=0; i<4; i++) {
- if (mbr.part[i].type_code == 0x0b) {
- Printf("Partition %d is FAT32 and LBA Begins at 0x%08x offset in file\n",
- i, mbr.part[i].LBA_Begin * 512);
- if (i == exPartitionNumber) {
- Partition_LBA_Begin = mbr.part[i].LBA_Begin;
- FAT32_start = Partition_LBA_Begin * 512;
- }
- } else {
- Printf("Partition %d is type 0x%02x, LBA Begins: 0x%08x offset in file\n",
- i, mbr.part[i].type_code, mbr.part[i].LBA_Begin * 512);
- }
- }
- }
- if (Partition_LBA_Begin == 0) {
- Printf("WARNING: no partition selected for FAT32 exploration\n");
- Printf(" Do you need to set exPartitionNumber?\n");
- Exit(2);
- }
- } vhd_file;
- typedef struct {
- char pad[11];
- ushort BPB_BytsPerSec; /* offset 11,12 */
- uchar BPB_SecPerClus; /* offset 13 */
- ushort BPB_RsvdSecCnt; /* offset 14,15 */
- uchar BPB_NumFATs; /* offset 16 */
- ushort FAT16_RootEntries; //supposed to be 0 for FAT32
- ushort FAT16_SmallSectors; //supposed to be 0 for FAT32
- uchar pad2[11]; /* offset 17-31 */
- uchar pad3[4];
- uint32 BPB_FATSz32; /* offset 36 0x24 */
- uchar pad4[4];
- uint32 BPB_RootClus; /* offset 0x2c */
- uchar pad5[0x1ce];
- ushort expectAA55 <format = hex>;
- } FAT32VOLUMEID;
- local uint32 fat_begin_lba;
- local uint32 fat2_begin_lba;
- local uint32 cluster_begin_lba;
- local uint32 sectors_per_cluster;
- local uint32 clusters_per_fat;
- local uint32 root_dir_first_cluster;
- local uint64 ROOT_start;
- FSeek(FAT32_start);
- struct {
- FAT32VOLUMEID fvol;
- if (fvol.expectAA55 != 0xAA55) {
- Printf("error FAT32 VolumeID record does not end with AA55!\n");
- Exit(1);
- } else if ((fvol.FAT16_RootEntries !=0) || (fvol.FAT16_SmallSectors != 0)) {
- Printf("Error: FAT16 information found in VolumeID structure.\n");
- Printf(" This is NOT a valid FAT32 Volume.\n");
- Exit(1);
- } else {
- Printf("FAT32 VolumeID seems okay\n");
- }
- Printf("%d bytes per sector (MUST BE 512!!)\n", fvol.BPB_BytsPerSec);
- Printf("%d sectors per cluster\n", fvol.BPB_SecPerClus);
- Printf("0x%04x (%d) reserved sectors\n", fvol.BPB_RsvdSecCnt, fvol.BPB_RsvdSecCnt);
- Printf("%d FATs (There better be 2!!)\n", fvol.BPB_NumFATs);
- Printf("0x%08x (%d) sectors per FAT\n", fvol.BPB_FATSz32, fvol.BPB_FATSz32);
- Printf("Root Directory Cluster (supposed to be 2) == %d\n", fvol.BPB_RootClus);
- fat_begin_lba = Partition_LBA_Begin + fvol.BPB_RsvdSecCnt;
- fat2_begin_lba = fat_begin_lba + fvol.BPB_FATSz32;
- cluster_begin_lba = Partition_LBA_Begin + fvol.BPB_RsvdSecCnt + (fvol.BPB_NumFATs * fvol.BPB_FATSz32);
- sectors_per_cluster = fvol.BPB_SecPerClus;
- clusters_per_fat = fvol.BPB_FATSz32 * (512/4);
- Printf("There are %d. cluster indices in each FAT\n", clusters_per_fat);
- root_dir_first_cluster = fvol.BPB_RootClus;
- Printf("FAT 1 LBA = 0x%08x, FAT 1 raw file offset = 0x%08x\n", fat_begin_lba, fat_begin_lba * 512);
- Printf("FAT 2 LBA = 0x%08x, FAT 2 raw file offset = 0x%08x\n", fat2_begin_lba, fat2_begin_lba*512);
- Printf("First Cluster LBA = 0x%08x, First Cluster raw file offset = %08x\n", cluster_begin_lba, cluster_begin_lba*512);
- ROOT_start = cluster_begin_lba * 512;
- } fatvolume;
- FSeek(fat_begin_lba * 512);
- uint32 FAT1[clusters_per_fat];
- FSeek(fat2_begin_lba * 512);
- uint32 FAT2[clusters_per_fat];
- typedef struct {
- char filename[8];
- char extension[3];
- uchar attrib <format = hex>;
- uchar ulcase;
- uchar creationTimeMS;
- DOSTIME creationTime;
- DOSDATE creationDate;
- DOSDATE lastAccessDate;
- uint16 first_cluster_hi <format = hex>;
- DOSTIME timeStamp;
- DOSDATE dateStamp;
- uint16 first_cluster_lo <format= hex>;
- uint32 filesize;
- } DIRENTRY;
- void PrintDirEntry( DIRENTRY &ent)
- {
- local uchar slotkey;
- slotkey = ent.filename[0];
- if ((slotkey == 0) || (ent.attrib == 0x0f)) { // slot is empty OR long filename
- return;
- } else if (slotkey == 0xe5) {
- Printf(" DELETED ");
- }
- Printf(" %8s.%3s attrib:0x%02x ", ent.filename, ent.extension, ent.attrib);
- if (ent.attrib & 0x01) Printf("Read_Only ");
- if (ent.attrib & 0x02) Printf("Hidden ");
- if (ent.attrib & 0x04) Printf("System ");
- if (ent.attrib & 0x08) Printf("VolumeID ");
- if (ent.attrib & 0x10) Printf("Directory ");
- if (ent.attrib & 0x20) Printf("Archive ");
- Printf("\n");
- }
- uint32 ConvertClusterHiLo2LBA( uint16 clusterHi, uint16 clusterLo)
- {
- local uint32 lba, cluster;
- cluster = clusterHi << 16 | clusterLo;
- lba = cluster_begin_lba;
- lba = lba + ((cluster - 2) * sectors_per_cluster);
- return lba;
- }
- local uint64 DIR1_start=0;
- local uint32 DIR1_LBA = 0;
- local uint64 DIR2_start=0;
- local uint32 DIR2_LBA = 0;
- local uint64 DIR3_start=0;
- local uint32 DIR3_LBA = 0;
- local int NumDirEntriesPerCluster = (sectors_per_cluster * 512)/32;
- FSeek(ROOT_start);
- struct {
- DIRENTRY entry[NumDirEntriesPerCluster];
- Printf("Volume ID Name: %s\n", entry[0].filename);
- Printf("Root Directory Contents:\n");
- for (i=0; i<NumDirEntriesPerCluster; i++) {
- PrintDirEntry( entry[i]);
- if (exDir1_LENGTH != 0) {
- if (!Memcmp(entry[i].filename, exDir1, exDir1_LENGTH)) { //look for dir1 in root
- //Printf("Found %s!, 0x%04x, 0x%04x\n", exDir1, entry[i].first_cluster_hi, entry[i].first_cluster_lo);
- DIR1_LBA = ConvertClusterHiLo2LBA(entry[i].first_cluster_hi, entry[i].first_cluster_lo);
- DIR1_start = DIR1_LBA * 512;
- }
- }
- }
- } root;
- if (DIR1_start != 0) {
- Printf("DIR 1 is %s, at LBA 0x%08x, at raw file offset 0x%08x\n", exDir1, DIR1_LBA, DIR1_start);
- FSeek(DIR1_start);
- struct {
- DIRENTRY entry[NumDirEntriesPerCluster];
- for (i=0; i<NumDirEntriesPerCluster; i++) {
- PrintDirEntry( entry[i]);
- if (exDir2_LENGTH != 0) {
- if (!Memcmp(entry[i].filename, exDir2, exDir2_LENGTH)) { //look for dir2 in dir1
- //Printf("Found %s!\n", exDir2);
- DIR2_LBA = ConvertClusterHiLo2LBA(entry[i].first_cluster_hi, entry[i].first_cluster_lo);
- DIR2_start = DIR2_LBA * 512;
- }
- }
- }
- } DirOne;
- }
- if (DIR2_start != 0) {
- Printf("DIR 2 is %s, at LBA 0x%08x, at raw file offset 0x%08x\n", exDir2, DIR2_LBA, DIR2_start);
- FSeek(DIR2_start);
- struct {
- DIRENTRY entry[NumDirEntriesPerCluster];
- for (i=0; i<NumDirEntriesPerCluster; i++) {
- PrintDirEntry( entry[i]);
- if (exDir3_LENGTH != 0) {
- if (!Memcmp(entry[i].filename, exDir3, exDir3_LENGTH)) { //look for dir3 in dir2
- //Printf("Found %s!\n", exDir3);
- DIR3_LBA = ConvertClusterHiLo2LBA(entry[i].first_cluster_hi, entry[i].first_cluster_lo);
- DIR3_start = DIR3_LBA * 512;
- }
- }
- }
- } DirTwo;
- }
- if (DIR3_start != 0) {
- Printf("DIR 3 is %s, at LBA 0x%08x, at raw file offset 0x%08x\n", exDir3, DIR3_LBA, DIR3_start);
- FSeek(DIR3_start);
- struct {
- DIRENTRY entry[NumDirEntriesPerCluster];
- for (i=0; i<NumDirEntriesPerCluster; i++) {
- PrintDirEntry( entry[i]);
- }
- } DirTwo;
- }
- void PrintClusterList( uint32 first)
- {
- local uint32 c, nextAddress;
- local uint32 fat_start;
- local uint32 fat_end;
- fat_start = fat_begin_lba * 512;
- fat_end = fat2_begin_lba * 512;
- c = first;
- Printf("Cluster # FAT start ClusterIdx: Next Cluster\n");
- Printf(" 0x%08x (%d.)\n", c, c);
- while (1) {
- if ((c == 0x0fffffff) || (c == 0xffffffff)) { // normal end of cluster list
- return;
- }
- if (c > clusters_per_fat) {
- Printf("ERROR: unable to follow cluster list. It goes beyond FAT limit\n");
- return;
- }
- nextAddress = (c * 4) + fat_start;
- FSeek(nextAddress);
- uint32 nextCluster;
- Printf("0x%08x (* 4) + 0x%08x = 0x%08x: 0x%08x (%d.)\n", c, (fat_begin_lba *512), nextAddress, nextCluster, nextCluster);
- c = nextCluster;
- }
- }
- if (exClusterList != 0) {
- PrintClusterList( exClusterList);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement