// Practic1Lab1.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
// ossp01.cpp : This file contains the 'main' function. Program execution begins and ends there.
//
#include "pch.h"
struct BPB {
WORD sectorSize;
BYTE clusterSize;
WORD reservedSectors;
BYTE fatCount;
WORD rootSize;
WORD totalSectors;
BYTE media;
WORD fatSize;
WORD sectors;
WORD heads;
WORD hiddenLow;
WORD hiddenHigh;
BYTE largeTotalSectors[4];
};
struct bootRecord {
BYTE jmpCommand[3];
BYTE vendorName[8];
BPB extendedBlock;
BYTE drivePhysicalNumber;
BYTE rezerved;
BYTE symbolBrackets;
BYTE volumeSerialNumber[4];
BYTE volumeLabel[11];
BYTE identifierFAT[8];
BYTE bootCode[450];
} boot;
typedef struct root_Entries
{
BYTE short_FileName[11];
BYTE fileAttributes;
BYTE reserved;
BYTE createTime_ms;
WORD createTime;
WORD createDate;
WORD accessedDate;
WORD clusterNumber_High;
WORD modifiedTime;
WORD modifiedDate;
WORD firstClusterAddress_FAT12;
DWORD sizeofFile;
} root;
void ScanBootSector();
void ScanRoot();
void ScanFat();
WORD FAT[2880];
BYTE Buffer[512 * 9];
WORD* Val;
HANDLE hDisk;
int main()
{
hDisk = CreateFile(TEXT("C:\\Tmp\\Dos6.22.img"), // drive to open
GENERIC_READ, // Access mode
FILE_SHARE_READ, // Share Mode
NULL, // Security Descriptor
OPEN_EXISTING, // How to create
0, // File attributes
NULL); // Handle to template
if (hDisk != NULL)
{
ScanBootSector();
ScanRoot();
ScanFat();
int cluster = 2619;
int count = 1;
while (FAT[cluster] != 0xfff)
{
printf("%6x\t", FAT[cluster]);
cluster = FAT[cluster];
count++;
}
printf("\n%d", count);
}
CloseHandle(hDisk);
return 0;
}
void ScanFat() {
DWORD dwBytesRead;
WORD fatElement;
int j = 0;
SetFilePointer(hDisk, 512, NULL, FILE_BEGIN);
ReadFile(hDisk, Buffer, 512 * 9, &dwBytesRead, NULL);
if (Buffer[0] != boot.extendedBlock.media) {
printf("Error 1 \n");
return;
}
//FAT table entries are packed so that two cluster entries occupy three bytes with the following general format :
//yz Zx XY
// where
// xyz is the one pointer entry and
// XYZ is the second pointer entry.
// E.g., bytes :
// 2d e0 02
// refer to clusters 0x02d (45) and 0x02e (46)
for (int i = 0, j = 0; i < 512 * 9; i += 3, j += 2) {
FAT[j] = (Buffer[i] + (Buffer[i + 1] << 8)) & 0x0FFF;
FAT[j+1] = (Buffer[i + 1] + (Buffer[i + 2] << 8)) >> 4;
}
}
void ScanRoot() {
DWORD dwFilePointer;
DWORD dwBytesRead;
root stRoot;
BYTE byteRoot[512];
memset(&byteRoot, 0, 512);
dwFilePointer = SetFilePointer(hDisk, (512 * 19), NULL, FILE_BEGIN);
if (dwFilePointer == INVALID_SET_FILE_POINTER && GetLastError() != NO_ERROR)
{
// Deal with failure
// . . .
} // End of error handler
int iSector = 19;
BOOL bNoEntry = FALSE;
// Iterate through root directory sectors
do
{
if (!ReadFile(hDisk, byteRoot, 512, &dwBytesRead, NULL))
{
printf("Error in Reading Root Entry.\n");
}
else
{
BYTE *pByteRoot = byteRoot;
// Iterate through 32 byte entries
for (int i = 0; i < (512 / 32); i++)
{
ZeroMemory(&stRoot, 32);
memcpy(&stRoot, pByteRoot, 32);
// No entry beyond this used.
if (stRoot.short_FileName[0] == 0x00)
{
// Stop iteration
bNoEntry = TRUE;
break;
}
else
{
// This if loops checks for file deletion
// status
if (stRoot.short_FileName[0] == 0xE5)
{
printf("First character of file after deletion :0x%x\n",
stRoot.short_FileName[0]);
printf("File status: Deleted.\n");
}
puts("File Name :");
for (size_t idx = 0; idx < 8; idx++)
{
putchar(stRoot.short_FileName[idx]);
}
putchar('\n');
if (stRoot.fileAttributes & 0x01)
printf("File Attribute : Read Only File\n");
if (stRoot.fileAttributes & 0x02)
printf("File Attribute : Hidden File\n");
if (stRoot.fileAttributes & 0x04)
printf("File Attribute : System File\n");
if (stRoot.fileAttributes & 0x08)
printf("File Attribute : Volume Label\n");
if (stRoot.fileAttributes & 0x0f)
printf("File Attribute : Long File Name\n");
if (stRoot.fileAttributes & 0x10)
printf("File Attribute : Directory\n");
if (stRoot.fileAttributes & 0x20)
printf("File Attribute : Archive\n");
WORD nYear = (stRoot.createDate >> 9);
WORD nMonth = (stRoot.createDate << 7);
nMonth = nMonth >> 12;
WORD nDay = (stRoot.createDate << 11);
nDay = nDay >> 11;
//printf("Create Date : %d/%d/%d\n", nDay, nMonth, (nYear + 1980));
nYear = (stRoot.modifiedDate >> 9);
nMonth = (stRoot.modifiedDate << 7);
nMonth = nMonth >> 12;
nDay = (stRoot.modifiedDate << 11);
nDay = nDay >> 11;
printf("Modification Date : %d/%d/%d\n", nDay, nMonth, (nYear + 1980));
nYear = (stRoot.accessedDate >> 9);
nMonth = (stRoot.accessedDate << 7);
nMonth = nMonth >> 12;
nDay = (stRoot.accessedDate << 11);
nDay = nDay >> 11;
//printf("Accessed Date : %d/%d/%d\n", nDay, nMonth, (nYear + 1980));
printf("Start Cluster Address: %d\n",
stRoot.firstClusterAddress_FAT12);
printf("File Size : %d bytes\n",
stRoot.sizeofFile);
pByteRoot += 32;
} // End of else
}
if (bNoEntry)
break;
else
{
iSector += 1;
}
}
} while (iSector <= 33);
}
void ScanBootSector()
{
DWORD dwBytesRead;
char str[128];
unsigned short num1, num2;
bool check;
check = ReadFile(hDisk, (void *)&boot, sizeof(bootRecord), &dwBytesRead, NULL);
if (!check) {
printf("Error %d", GetLastError());
}
strncpy_s(str, (char *)boot.vendorName, 8);
str[8] = '\0';
printf("Vendor Name: %s\n", str);
if (boot.drivePhysicalNumber >= 0x80)
puts("Hard disk");
else
puts("Floppy disk");
if (boot.symbolBrackets == '(')
puts("Extended block present.");
else
puts("Extended block absent.");
memcpy(&num1, boot.volumeSerialNumber, 2);
memcpy(&num2, boot.volumeSerialNumber + 2, 2);
printf("Serial num=%X-%X\n", num1, num2);
strncpy_s(str, (char *)boot.volumeLabel, 11);
str[11] = '\0';
printf("Label: %s\n", str);
strncpy_s(str, (char *)boot.identifierFAT, 8);
str[8] = '\0';
printf("Ident FAT: %s\n", str);
//printf("%d", sizeof(struct param_bios));
/*printf("Byte in sector = %d\n", boot.extendedBlock.sectorSize);
printf("Sector in cluster = %d\n", boot.extendedBlock.clusterSize);
printf("Reserved sector = %d\n", boot.extendedBlock.reservedSectors);
printf("FAT Count = %d\n", boot.extendedBlock.fatCount);
printf("root size = %d\n", boot.extendedBlock.rootSize);
printf("Total sector = %d\n", boot.extendedBlock.totalSectors);
printf("Media = %d\n", boot.extendedBlock.media);
printf("Fat size = %d\n", boot.extendedBlock.fatSize);
printf("Sectors = %d\n", boot.extendedBlock.sectors);
printf("Heads = %d\n", boot.extendedBlock.heads);
printf("hidden Low = %d\n", boot.extendedBlock.hiddenLow);
printf("hidden High = %d\n", boot.extendedBlock.hiddenHigh);
printf("largeTotal Sectors = %d\n", boot.extendedBlock.largeTotalSectors[4]);*/
}
Comments