summaryrefslogtreecommitdiff
path: root/src/list.c
blob: 773f4ec56a4aeffbe4f9c2a6a4178fed02f75a1c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/* 
 * AUTHOR
 * Stef Walter
 *
 * LICENSE
 * This software is in the public domain.
 *
 * The software is provided "as is", without warranty of any kind,
 * express or implied, including but not limited to the warranties
 * of merchantability, fitness for a particular purpose, and
 * noninfringement. In no event shall the author(s) be liable for any
 * claim, damages, or other liability, whether in an action of
 * contract, tort, or otherwise, arising from, out of, or in connection
 * with the software or the use or other dealings in the software.
 * 
 * SUPPORT
 * Send bug reports to: <stef@memberwebs.com>
 */

#include "usuals.h"
#include "drive.h"
#include "ntfs.h"

const char kPrintData[]		= "\
    Start Sector    End Sector      Cluster Size    MFT Offset    \n\
==================================================================\n\
";

const char kPrintDrive[]		= "\nDrive: %u\n";
const char kPrintDrivePath[]		= "\nDrive: %s\n";

int printNTFSInfo(int dd, uint64 tblSector)
{
	byte sector[kSectorSize];
  int64 pos;
  size_t sz;
  ntfs_bootsector* boot;

  /* TODO: Check the range and don't print if out of range */
	pos = SECTOR_TO_BYTES(tblSector);

  if(lseek64(dd, pos, SEEK_SET) == -1)
    err(1, "couldn't seek drive");

  sz = read(dd, &sector, kSectorSize);
  if(sz == -1)
    err(1, "couldn't read drive");

  if(sz != kSectorSize)
    errx(1, "unexpected end of drive");

  boot = (ntfs_bootsector*)sector;
	if(!memcmp(boot->sysId, kNTFS_SysId, sizeof(boot->sysId)))
	{
#ifdef _WIN32
		printf("%-15u %-15I64u", (unsigned int)boot->secPerClus,
		       boot->offMFT * (uint64)boot->secPerClus);
#else
		printf("%-15u %-15llu", (unsigned int)boot->secPerClus,
		       (unsigned long long)(boot->offMFT * (uint64)boot->secPerClus));
#endif
	}

	printf("\n");
	return 0;
}

int printPartitionInfo(int dd, uint64 tblSector)
{
	drive_mbr mbr;
  int64 pos;
  size_t sz;
  int i;

	ASSERT(sizeof(drive_mbr) == kSectorSize);

	pos = SECTOR_TO_BYTES(tblSector);

  if(lseek64(dd, pos, SEEK_SET) == -1)
    err(1, "couldn't seek drive");

  sz = read(dd, &mbr, sizeof(drive_mbr));
  if(sz == -1)
    err(1, "couldn't read drive");

  if(sz != sizeof(drive_mbr))
    errx(1, "unexpected end of drive");

	if(mbr.sig == kMBR_Sig)
	{
		for(i = 0; i < 4; i++)
		{
			if(mbr.partitions[i].system == kPartition_Extended ||
			   mbr.partitions[i].system == kPartition_ExtendedLBA)
			{
				printPartitionInfo(dd, tblSector + mbr.partitions[i].startsec);
			}
			else if(tblSector + mbr.partitions[i].startsec == 1UL &&
			        tblSector + mbr.partitions[i].endsec == 0xFFFFFFFFUL)
			{
				printf("    large partition, perhaps using gpt\n");
			}
			else if(!mbr.partitions[i].system == kPartition_Invalid)
			{
#ifdef _WIN32
				printf("    %-15I64u %-15I64u ",
				       tblSector + mbr.partitions[i].startsec,
				       tblSector + mbr.partitions[i].endsec);
#else
				printf("    %-15llu %-15llu ",
				       (unsigned long long)(tblSector + mbr.partitions[i].startsec),
				       (unsigned long long)(tblSector + mbr.partitions[i].endsec));
#endif

				printNTFSInfo(dd, tblSector + (uint64)mbr.partitions[i].startsec);
			}
		}
	}

	return 0;
}

#ifdef _WIN32
void scroungeList()
{
	char driveName[MAX_PATH];
  int dd = -1;
  int i;

  printf(kPrintData);

	/* LIMIT: 256 Drives */
	for(i = 0; i < 0x100; i++)
	{
    makeDriveName(driveName, i);

    dd = open(driveName, O_BINARY | O_RDONLY | OPEN_LARGE_OPTS);
    if(dd != -1)
    {
			printf(kPrintDrive, i);
			printPartitionInfo(dd, 0);
			close(dd);
		}
	}
}
#endif

void scroungeListDrive(char* drive)
{
  int dd = open(drive, O_BINARY | O_RDONLY | OPEN_LARGE_OPTS);
  if(dd == -1)
    err(1, "couldn't open drive: %s", drive);

  printf(kPrintData);
  printf(kPrintDrivePath, drive);
  printPartitionInfo(dd, 0);
  close(dd);
}