fff97244d285c0d79843d409e40d64d9b23f76ed
[reactos.git] / reactos / base / setup / usetup / partlist.c
1 /*
2 * ReactOS kernel
3 * Copyright (C) 2002, 2003, 2004, 2005 ReactOS Team
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19 /* COPYRIGHT: See COPYING in the top level directory
20 * PROJECT: ReactOS text-mode setup
21 * FILE: subsys/system/usetup/partlist.c
22 * PURPOSE: Partition list functions
23 * PROGRAMMER: Eric Kohl
24 * Casper S. Hornstrup (chorns@users.sourceforge.net)
25 */
26
27 #include "usetup.h"
28
29 #include <ntddscsi.h>
30
31 #define NDEBUG
32 #include <debug.h>
33
34 #define DUMP_PARTITION_TABLE
35
36 /* FUNCTIONS ****************************************************************/
37
38 #ifdef DUMP_PARTITION_TABLE
39 static
40 VOID
41 DumpPartitionTable(
42 PDISKENTRY DiskEntry)
43 {
44 PPARTITION_INFORMATION PartitionInfo;
45 ULONG i;
46
47 for (i = 0; i < DiskEntry->LayoutBuffer->PartitionCount; i++)
48 {
49 PartitionInfo = &DiskEntry->LayoutBuffer->PartitionEntry[i];
50 DPRINT("\n%lu: %12I64u %12I64u %10lu %2lu %2x %c %c\n",
51 i,
52 PartitionInfo->StartingOffset.QuadPart,
53 PartitionInfo->PartitionLength.QuadPart,
54 PartitionInfo->HiddenSectors,
55 PartitionInfo->PartitionNumber,
56 PartitionInfo->PartitionType,
57 PartitionInfo->BootIndicator ? '*': ' ',
58 PartitionInfo->RewritePartition ? 'Y': 'N');
59 }
60 }
61 #endif
62
63
64 ULONGLONG
65 Align(
66 IN ULONGLONG Value,
67 IN ULONG Alignment)
68 {
69 ULONGLONG Temp;
70
71 Temp = Value / Alignment;
72
73 return Temp * Alignment;
74 }
75
76
77 ULONGLONG
78 RoundingDivide(
79 IN ULONGLONG Dividend,
80 IN ULONGLONG Divisor)
81 {
82 return (Dividend + Divisor / 2) / Divisor;
83 }
84
85
86 static
87 VOID
88 GetDriverName(
89 PDISKENTRY DiskEntry)
90 {
91 RTL_QUERY_REGISTRY_TABLE QueryTable[2];
92 WCHAR KeyName[32];
93 NTSTATUS Status;
94
95 RtlInitUnicodeString(&DiskEntry->DriverName,
96 NULL);
97
98 swprintf(KeyName,
99 L"\\Scsi\\Scsi Port %lu",
100 DiskEntry->Port);
101
102 RtlZeroMemory(&QueryTable,
103 sizeof(QueryTable));
104
105 QueryTable[0].Name = L"Driver";
106 QueryTable[0].Flags = RTL_QUERY_REGISTRY_DIRECT;
107 QueryTable[0].EntryContext = &DiskEntry->DriverName;
108
109 Status = RtlQueryRegistryValues(RTL_REGISTRY_DEVICEMAP,
110 KeyName,
111 QueryTable,
112 NULL,
113 NULL);
114 if (!NT_SUCCESS(Status))
115 {
116 DPRINT1("RtlQueryRegistryValues() failed (Status %lx)\n", Status);
117 }
118 }
119
120
121 static
122 VOID
123 AssignDriveLetters(
124 PPARTLIST List)
125 {
126 PDISKENTRY DiskEntry;
127 PPARTENTRY PartEntry;
128 PLIST_ENTRY Entry1;
129 PLIST_ENTRY Entry2;
130 CHAR Letter;
131
132 Letter = 'C';
133
134 /* Assign drive letters to primary partitions */
135 Entry1 = List->DiskListHead.Flink;
136 while (Entry1 != &List->DiskListHead)
137 {
138 DiskEntry = CONTAINING_RECORD(Entry1, DISKENTRY, ListEntry);
139
140 Entry2 = DiskEntry->PrimaryPartListHead.Flink;
141 while (Entry2 != &DiskEntry->PrimaryPartListHead)
142 {
143 PartEntry = CONTAINING_RECORD(Entry2, PARTENTRY, ListEntry);
144
145 PartEntry->DriveLetter = 0;
146
147 if (PartEntry->IsPartitioned &&
148 !IsContainerPartition(PartEntry->PartitionType))
149 {
150 if (IsRecognizedPartition(PartEntry->PartitionType) ||
151 (PartEntry->PartitionType == PARTITION_ENTRY_UNUSED &&
152 PartEntry->SectorCount.QuadPart != 0LL))
153 {
154 if (Letter <= 'Z')
155 {
156 PartEntry->DriveLetter = Letter;
157 Letter++;
158 }
159 }
160 }
161
162 Entry2 = Entry2->Flink;
163 }
164
165 Entry1 = Entry1->Flink;
166 }
167
168 /* Assign drive letters to logical drives */
169 #if 0
170 Entry1 = List->DiskListHead.Flink;
171 while (Entry1 != &List->DiskListHead)
172 {
173 DiskEntry = CONTAINING_RECORD(Entry1, DISKENTRY, ListEntry);
174
175 Entry2 = DiskEntry->PartListHead.Flink;
176 if (Entry2 != &DiskEntry->PartListHead)
177 {
178 Entry2 = Entry2->Flink;
179 while (Entry2 != &DiskEntry->PartListHead)
180 {
181 PartEntry = CONTAINING_RECORD(Entry2,
182 PARTENTRY,
183 ListEntry);
184
185 PartEntry->DriveLetter = 0;
186
187 if (PartEntry->Unpartitioned == FALSE &&
188 !IsContainerPartition(PartEntry->PartInfo[0].PartitionType))
189 {
190 if (IsRecognizedPartition(PartEntry->PartInfo[0].PartitionType) ||
191 (PartEntry->PartInfo[0].PartitionType == PARTITION_ENTRY_UNUSED &&
192 PartEntry->PartInfo[0].PartitionLength.QuadPart != 0LL))
193 {
194 if (Letter <= 'Z')
195 {
196 PartEntry->DriveLetter = Letter;
197 Letter++;
198 }
199 }
200 }
201
202 Entry2 = Entry2->Flink;
203 }
204 }
205
206 Entry1 = Entry1->Flink;
207 }
208 #endif
209 }
210
211
212 static
213 VOID
214 UpdatePartitionNumbers(
215 PDISKENTRY DiskEntry)
216 {
217 PPARTENTRY PartEntry;
218 PLIST_ENTRY Entry;
219 // ULONG PartitionNumber = 1;
220 ULONG PartitionIndex = 0;
221
222 Entry = DiskEntry->PrimaryPartListHead.Flink;
223 while (Entry != &DiskEntry->PrimaryPartListHead)
224 {
225 PartEntry = CONTAINING_RECORD(Entry,
226 PARTENTRY,
227 ListEntry);
228
229 if (PartEntry->IsPartitioned == FALSE)
230 {
231 // PartEntry->PartitionNumber = 0;
232 PartEntry->PartitionIndex = (ULONG)-1;
233 }
234 else
235 {
236 if (IsContainerPartition(PartEntry->PartitionType))
237 {
238 // PartEntry->PartitionNumber = 0;
239 }
240 else if (PartEntry->PartitionType == PARTITION_ENTRY_UNUSED &&
241 PartEntry->SectorCount.QuadPart == 0ULL)
242 {
243 // PartEntry->PartitionNumber = 0;
244 }
245 else
246 {
247 // PartEntry->PartitionNumber = PartitionNumber++;
248 }
249
250 PartEntry->PartitionIndex = PartitionIndex++;
251 }
252
253 Entry = Entry->Flink;
254 }
255 }
256
257
258 NTSTATUS
259 NTAPI
260 DiskIdentifierQueryRoutine(
261 PWSTR ValueName,
262 ULONG ValueType,
263 PVOID ValueData,
264 ULONG ValueLength,
265 PVOID Context,
266 PVOID EntryContext)
267 {
268 PBIOSDISKENTRY BiosDiskEntry = (PBIOSDISKENTRY)Context;
269 UNICODE_STRING NameU;
270
271 if (ValueType == REG_SZ &&
272 ValueLength == 20 * sizeof(WCHAR))
273 {
274 NameU.Buffer = (PWCHAR)ValueData;
275 NameU.Length = NameU.MaximumLength = 8 * sizeof(WCHAR);
276 RtlUnicodeStringToInteger(&NameU, 16, &BiosDiskEntry->Checksum);
277
278 NameU.Buffer = (PWCHAR)ValueData + 9;
279 RtlUnicodeStringToInteger(&NameU, 16, &BiosDiskEntry->Signature);
280
281 return STATUS_SUCCESS;
282 }
283
284 return STATUS_UNSUCCESSFUL;
285 }
286
287
288 NTSTATUS
289 NTAPI
290 DiskConfigurationDataQueryRoutine(
291 PWSTR ValueName,
292 ULONG ValueType,
293 PVOID ValueData,
294 ULONG ValueLength,
295 PVOID Context,
296 PVOID EntryContext)
297 {
298 PBIOSDISKENTRY BiosDiskEntry = (PBIOSDISKENTRY)Context;
299 PCM_FULL_RESOURCE_DESCRIPTOR FullResourceDescriptor;
300 PCM_DISK_GEOMETRY_DEVICE_DATA DiskGeometry;
301 ULONG i;
302
303 if (ValueType != REG_FULL_RESOURCE_DESCRIPTOR ||
304 ValueLength < sizeof(CM_FULL_RESOURCE_DESCRIPTOR))
305 return STATUS_UNSUCCESSFUL;
306
307 FullResourceDescriptor = (PCM_FULL_RESOURCE_DESCRIPTOR)ValueData;
308
309 /* Hm. Version and Revision are not set on Microsoft Windows XP... */
310 #if 0
311 if (FullResourceDescriptor->PartialResourceList.Version != 1 ||
312 FullResourceDescriptor->PartialResourceList.Revision != 1)
313 return STATUS_UNSUCCESSFUL;
314 #endif
315
316 for (i = 0; i < FullResourceDescriptor->PartialResourceList.Count; i++)
317 {
318 if (FullResourceDescriptor->PartialResourceList.PartialDescriptors[i].Type != CmResourceTypeDeviceSpecific ||
319 FullResourceDescriptor->PartialResourceList.PartialDescriptors[i].u.DeviceSpecificData.DataSize != sizeof(CM_DISK_GEOMETRY_DEVICE_DATA))
320 continue;
321
322 DiskGeometry = (PCM_DISK_GEOMETRY_DEVICE_DATA)&FullResourceDescriptor->PartialResourceList.PartialDescriptors[i + 1];
323 BiosDiskEntry->DiskGeometry = *DiskGeometry;
324
325 return STATUS_SUCCESS;
326 }
327
328 return STATUS_UNSUCCESSFUL;
329 }
330
331
332 NTSTATUS
333 NTAPI
334 SystemConfigurationDataQueryRoutine(
335 PWSTR ValueName,
336 ULONG ValueType,
337 PVOID ValueData,
338 ULONG ValueLength,
339 PVOID Context,
340 PVOID EntryContext)
341 {
342 PCM_FULL_RESOURCE_DESCRIPTOR FullResourceDescriptor;
343 PCM_INT13_DRIVE_PARAMETER* Int13Drives = (PCM_INT13_DRIVE_PARAMETER*)Context;
344 ULONG i;
345
346 if (ValueType != REG_FULL_RESOURCE_DESCRIPTOR ||
347 ValueLength < sizeof (CM_FULL_RESOURCE_DESCRIPTOR))
348 return STATUS_UNSUCCESSFUL;
349
350 FullResourceDescriptor = (PCM_FULL_RESOURCE_DESCRIPTOR)ValueData;
351
352 /* Hm. Version and Revision are not set on Microsoft Windows XP... */
353 #if 0
354 if (FullResourceDescriptor->PartialResourceList.Version != 1 ||
355 FullResourceDescriptor->PartialResourceList.Revision != 1)
356 return STATUS_UNSUCCESSFUL;
357 #endif
358
359 for (i = 0; i < FullResourceDescriptor->PartialResourceList.Count; i++)
360 {
361 if (FullResourceDescriptor->PartialResourceList.PartialDescriptors[i].Type != CmResourceTypeDeviceSpecific ||
362 FullResourceDescriptor->PartialResourceList.PartialDescriptors[i].u.DeviceSpecificData.DataSize % sizeof(CM_INT13_DRIVE_PARAMETER) != 0)
363 continue;
364
365 *Int13Drives = (CM_INT13_DRIVE_PARAMETER*) RtlAllocateHeap(ProcessHeap, 0, FullResourceDescriptor->PartialResourceList.PartialDescriptors[i].u.DeviceSpecificData.DataSize);
366 if (*Int13Drives == NULL)
367 return STATUS_NO_MEMORY;
368
369 memcpy(*Int13Drives,
370 &FullResourceDescriptor->PartialResourceList.PartialDescriptors[i + 1],
371 FullResourceDescriptor->PartialResourceList.PartialDescriptors[i].u.DeviceSpecificData.DataSize);
372 return STATUS_SUCCESS;
373 }
374
375 return STATUS_UNSUCCESSFUL;
376 }
377
378
379 #define ROOT_NAME L"\\Registry\\Machine\\HARDWARE\\DESCRIPTION\\System\\MultifunctionAdapter"
380
381 static VOID
382 EnumerateBiosDiskEntries(
383 PPARTLIST PartList)
384 {
385 RTL_QUERY_REGISTRY_TABLE QueryTable[3];
386 WCHAR Name[120];
387 ULONG AdapterCount;
388 ULONG DiskCount;
389 NTSTATUS Status;
390 PCM_INT13_DRIVE_PARAMETER Int13Drives;
391 PBIOSDISKENTRY BiosDiskEntry;
392
393 memset(QueryTable, 0, sizeof(QueryTable));
394
395 QueryTable[1].Name = L"Configuration Data";
396 QueryTable[1].QueryRoutine = SystemConfigurationDataQueryRoutine;
397 Int13Drives = NULL;
398 Status = RtlQueryRegistryValues(RTL_REGISTRY_ABSOLUTE,
399 L"\\Registry\\Machine\\HARDWARE\\DESCRIPTION\\System",
400 &QueryTable[1],
401 (PVOID)&Int13Drives,
402 NULL);
403 if (!NT_SUCCESS(Status))
404 {
405 DPRINT1("Unable to query the 'Configuration Data' key in '\\Registry\\Machine\\HARDWARE\\DESCRIPTION\\System', status=%lx\n", Status);
406 return;
407 }
408
409 AdapterCount = 0;
410 while (1)
411 {
412 swprintf(Name, L"%s\\%lu", ROOT_NAME, AdapterCount);
413 Status = RtlQueryRegistryValues(RTL_REGISTRY_ABSOLUTE,
414 Name,
415 &QueryTable[2],
416 NULL,
417 NULL);
418 if (!NT_SUCCESS(Status))
419 {
420 break;
421 }
422
423 swprintf(Name, L"%s\\%lu\\DiskController", ROOT_NAME, AdapterCount);
424 Status = RtlQueryRegistryValues(RTL_REGISTRY_ABSOLUTE,
425 Name,
426 &QueryTable[2],
427 NULL,
428 NULL);
429 if (NT_SUCCESS(Status))
430 {
431 while (1)
432 {
433 swprintf(Name, L"%s\\%lu\\DiskController\\0", ROOT_NAME, AdapterCount);
434 Status = RtlQueryRegistryValues(RTL_REGISTRY_ABSOLUTE,
435 Name,
436 &QueryTable[2],
437 NULL,
438 NULL);
439 if (!NT_SUCCESS(Status))
440 {
441 RtlFreeHeap(ProcessHeap, 0, Int13Drives);
442 return;
443 }
444
445 swprintf(Name, L"%s\\%lu\\DiskController\\0\\DiskPeripheral", ROOT_NAME, AdapterCount);
446 Status = RtlQueryRegistryValues(RTL_REGISTRY_ABSOLUTE,
447 Name,
448 &QueryTable[2],
449 NULL,
450 NULL);
451 if (NT_SUCCESS(Status))
452 {
453 QueryTable[0].Name = L"Identifier";
454 QueryTable[0].QueryRoutine = DiskIdentifierQueryRoutine;
455 QueryTable[1].Name = L"Configuration Data";
456 QueryTable[1].QueryRoutine = DiskConfigurationDataQueryRoutine;
457
458 DiskCount = 0;
459 while (1)
460 {
461 BiosDiskEntry = (BIOSDISKENTRY*) RtlAllocateHeap(ProcessHeap, HEAP_ZERO_MEMORY, sizeof(BIOSDISKENTRY));
462 if (BiosDiskEntry == NULL)
463 {
464 break;
465 }
466
467 swprintf(Name, L"%s\\%lu\\DiskController\\0\\DiskPeripheral\\%lu", ROOT_NAME, AdapterCount, DiskCount);
468 Status = RtlQueryRegistryValues(RTL_REGISTRY_ABSOLUTE,
469 Name,
470 QueryTable,
471 (PVOID)BiosDiskEntry,
472 NULL);
473 if (!NT_SUCCESS(Status))
474 {
475 RtlFreeHeap(ProcessHeap, 0, BiosDiskEntry);
476 break;
477 }
478
479 BiosDiskEntry->DiskNumber = DiskCount;
480 BiosDiskEntry->Recognized = FALSE;
481
482 if (DiskCount < Int13Drives[0].NumberDrives)
483 {
484 BiosDiskEntry->Int13DiskData = Int13Drives[DiskCount];
485 }
486 else
487 {
488 DPRINT1("Didn't find int13 drive datas for disk %u\n", DiskCount);
489 }
490
491 InsertTailList(&PartList->BiosDiskListHead, &BiosDiskEntry->ListEntry);
492
493 DPRINT("DiskNumber: %lu\n", BiosDiskEntry->DiskNumber);
494 DPRINT("Signature: %08lx\n", BiosDiskEntry->Signature);
495 DPRINT("Checksum: %08lx\n", BiosDiskEntry->Checksum);
496 DPRINT("BytesPerSector: %lu\n", BiosDiskEntry->DiskGeometry.BytesPerSector);
497 DPRINT("NumberOfCylinders: %lu\n", BiosDiskEntry->DiskGeometry.NumberOfCylinders);
498 DPRINT("NumberOfHeads: %lu\n", BiosDiskEntry->DiskGeometry.NumberOfHeads);
499 DPRINT("DriveSelect: %02x\n", BiosDiskEntry->Int13DiskData.DriveSelect);
500 DPRINT("MaxCylinders: %lu\n", BiosDiskEntry->Int13DiskData.MaxCylinders);
501 DPRINT("SectorsPerTrack: %d\n", BiosDiskEntry->Int13DiskData.SectorsPerTrack);
502 DPRINT("MaxHeads: %d\n", BiosDiskEntry->Int13DiskData.MaxHeads);
503 DPRINT("NumberDrives: %d\n", BiosDiskEntry->Int13DiskData.NumberDrives);
504
505 DiskCount++;
506 }
507 }
508
509 RtlFreeHeap(ProcessHeap, 0, Int13Drives);
510 return;
511 }
512 }
513
514 AdapterCount++;
515 }
516
517 RtlFreeHeap(ProcessHeap, 0, Int13Drives);
518 }
519
520
521 static
522 VOID
523 AddPartitionToDisk(
524 ULONG DiskNumber,
525 PDISKENTRY DiskEntry,
526 ULONG PartitionIndex,
527 BOOLEAN LogicalPartition)
528 {
529 PPARTITION_INFORMATION PartitionInfo;
530 PPARTENTRY PartEntry;
531
532 PartitionInfo = &DiskEntry->LayoutBuffer->PartitionEntry[PartitionIndex];
533 if (PartitionInfo->PartitionType == 0 ||
534 (LogicalPartition == TRUE && IsContainerPartition(PartitionInfo->PartitionType)))
535 return;
536
537 PartEntry = RtlAllocateHeap(ProcessHeap,
538 HEAP_ZERO_MEMORY,
539 sizeof(PARTENTRY));
540 if (PartEntry == NULL)
541 {
542 return;
543 }
544
545 PartEntry->DiskEntry = DiskEntry;
546
547 PartEntry->StartSector.QuadPart = (ULONGLONG)PartitionInfo->StartingOffset.QuadPart / DiskEntry->BytesPerSector;
548 PartEntry->SectorCount.QuadPart = (ULONGLONG)PartitionInfo->PartitionLength.QuadPart / DiskEntry->BytesPerSector;
549
550 PartEntry->BootIndicator = PartitionInfo->BootIndicator;
551 PartEntry->PartitionType = PartitionInfo->PartitionType;
552 PartEntry->HiddenSectors = PartitionInfo->HiddenSectors;
553
554 PartEntry->LogicalPartition = LogicalPartition;
555 PartEntry->IsPartitioned = TRUE;
556 PartEntry->PartitionNumber = PartitionInfo->PartitionNumber;
557 PartEntry->PartitionIndex = PartitionIndex;
558
559 if (IsContainerPartition(PartEntry->PartitionType))
560 {
561 PartEntry->FormatState = Unformatted;
562
563 if (LogicalPartition == FALSE && DiskEntry->ExtendedPartition == NULL)
564 DiskEntry->ExtendedPartition = PartEntry;
565 }
566 else if ((PartEntry->PartitionType == PARTITION_FAT_12) ||
567 (PartEntry->PartitionType == PARTITION_FAT_16) ||
568 (PartEntry->PartitionType == PARTITION_HUGE) ||
569 (PartEntry->PartitionType == PARTITION_XINT13) ||
570 (PartEntry->PartitionType == PARTITION_FAT32) ||
571 (PartEntry->PartitionType == PARTITION_FAT32_XINT13))
572 {
573 #if 0
574 if (CheckFatFormat())
575 {
576 PartEntry->FormatState = Preformatted;
577 }
578 else
579 {
580 PartEntry->FormatState = Unformatted;
581 }
582 #endif
583 PartEntry->FormatState = Preformatted;
584 }
585 else if (PartEntry->PartitionType == PARTITION_EXT2)
586 {
587 #if 0
588 if (CheckExt2Format())
589 {
590 PartEntry->FormatState = Preformatted;
591 }
592 else
593 {
594 PartEntry->FormatState = Unformatted;
595 }
596 #endif
597 PartEntry->FormatState = Preformatted;
598 }
599 else if (PartEntry->PartitionType == PARTITION_IFS)
600 {
601 #if 0
602 if (CheckNtfsFormat())
603 {
604 PartEntry->FormatState = Preformatted;
605 }
606 else if (CheckHpfsFormat())
607 {
608 PartEntry->FormatState = Preformatted;
609 }
610 else
611 {
612 PartEntry->FormatState = Unformatted;
613 }
614 #endif
615 PartEntry->FormatState = Preformatted;
616 }
617 else
618 {
619 PartEntry->FormatState = UnknownFormat;
620 }
621
622 if (LogicalPartition)
623 InsertTailList(&DiskEntry->LogicalPartListHead,
624 &PartEntry->ListEntry);
625 else
626 InsertTailList(&DiskEntry->PrimaryPartListHead,
627 &PartEntry->ListEntry);
628 }
629
630
631 static
632 VOID
633 ScanForUnpartitionedDiskSpace(
634 PDISKENTRY DiskEntry)
635 {
636 ULONGLONG LastStartSector;
637 ULONGLONG LastSectorCount;
638 ULONGLONG LastUnusedSectorCount;
639 PPARTENTRY PartEntry;
640 PPARTENTRY NewPartEntry;
641 PLIST_ENTRY Entry;
642
643 DPRINT("ScanForUnpartitionedDiskSpace()\n");
644
645 if (IsListEmpty(&DiskEntry->PrimaryPartListHead))
646 {
647 DPRINT1("No primary partition!\n");
648
649 /* Create a partition table that represents the empty disk */
650 NewPartEntry = RtlAllocateHeap(ProcessHeap,
651 HEAP_ZERO_MEMORY,
652 sizeof(PARTENTRY));
653 if (NewPartEntry == NULL)
654 return;
655
656 NewPartEntry->DiskEntry = DiskEntry;
657
658 NewPartEntry->IsPartitioned = FALSE;
659 NewPartEntry->StartSector.QuadPart = (ULONGLONG)DiskEntry->SectorAlignment;
660 NewPartEntry->SectorCount.QuadPart = Align(DiskEntry->SectorCount.QuadPart, DiskEntry->SectorAlignment) -
661 NewPartEntry->StartSector.QuadPart;
662
663 DPRINT1("First Sector: %I64u\n", NewPartEntry->StartSector.QuadPart);
664 DPRINT1("Last Sector: %I64u\n", NewPartEntry->StartSector.QuadPart + NewPartEntry->SectorCount.QuadPart - 1);
665 DPRINT1("Total Sectors: %I64u\n", NewPartEntry->SectorCount.QuadPart);
666
667 NewPartEntry->FormatState = Unformatted;
668
669 InsertTailList(&DiskEntry->PrimaryPartListHead,
670 &NewPartEntry->ListEntry);
671
672 return;
673 }
674
675 /* Start partition at head 1, cylinder 0 */
676 LastStartSector = DiskEntry->SectorAlignment;
677 LastSectorCount = 0ULL;
678 LastUnusedSectorCount = 0ULL;
679
680 Entry = DiskEntry->PrimaryPartListHead.Flink;
681 while (Entry != &DiskEntry->PrimaryPartListHead)
682 {
683 PartEntry = CONTAINING_RECORD(Entry, PARTENTRY, ListEntry);
684
685 if (PartEntry->PartitionType != PARTITION_ENTRY_UNUSED ||
686 PartEntry->SectorCount.QuadPart != 0ULL)
687 {
688 LastUnusedSectorCount =
689 PartEntry->StartSector.QuadPart - (LastStartSector + LastSectorCount);
690
691 if (PartEntry->StartSector.QuadPart > (LastStartSector + LastSectorCount) &&
692 LastUnusedSectorCount >= (ULONGLONG)DiskEntry->SectorAlignment)
693 {
694 DPRINT("Unpartitioned disk space %I64u sectors\n", LastUnusedSectorCount);
695
696 NewPartEntry = RtlAllocateHeap(ProcessHeap,
697 HEAP_ZERO_MEMORY,
698 sizeof(PARTENTRY));
699 if (NewPartEntry == NULL)
700 return;
701
702 NewPartEntry->DiskEntry = DiskEntry;
703
704 NewPartEntry->IsPartitioned = FALSE;
705 NewPartEntry->StartSector.QuadPart = LastStartSector + LastSectorCount;
706 NewPartEntry->SectorCount.QuadPart = Align(NewPartEntry->StartSector.QuadPart + LastUnusedSectorCount, DiskEntry->SectorAlignment) -
707 NewPartEntry->StartSector.QuadPart;
708
709 DPRINT1("First Sector: %I64u\n", NewPartEntry->StartSector.QuadPart);
710 DPRINT1("Last Sector: %I64u\n", NewPartEntry->StartSector.QuadPart + NewPartEntry->SectorCount.QuadPart - 1);
711 DPRINT1("Total Sectors: %I64u\n", NewPartEntry->SectorCount.QuadPart);
712
713 NewPartEntry->FormatState = Unformatted;
714
715 /* Insert the table into the list */
716 InsertTailList(&PartEntry->ListEntry,
717 &NewPartEntry->ListEntry);
718 }
719
720 LastStartSector = PartEntry->StartSector.QuadPart;
721 LastSectorCount = PartEntry->SectorCount.QuadPart;
722 }
723
724 Entry = Entry->Flink;
725 }
726
727 /* Check for trailing unpartitioned disk space */
728 if ((LastStartSector + LastSectorCount) < DiskEntry->SectorCount.QuadPart)
729 {
730 LastUnusedSectorCount = Align(DiskEntry->SectorCount.QuadPart - (LastStartSector + LastSectorCount), DiskEntry->SectorAlignment);
731
732 if (LastUnusedSectorCount >= (ULONGLONG)DiskEntry->SectorAlignment)
733 {
734 DPRINT("Unpartitioned disk space: %I64u sectors\n", LastUnusedSectorCount);
735
736 NewPartEntry = RtlAllocateHeap(ProcessHeap,
737 HEAP_ZERO_MEMORY,
738 sizeof(PARTENTRY));
739 if (NewPartEntry == NULL)
740 return;
741
742 NewPartEntry->DiskEntry = DiskEntry;
743
744 NewPartEntry->IsPartitioned = FALSE;
745 NewPartEntry->StartSector.QuadPart = LastStartSector + LastSectorCount;
746 NewPartEntry->SectorCount.QuadPart = Align(NewPartEntry->StartSector.QuadPart + LastUnusedSectorCount, DiskEntry->SectorAlignment) -
747 NewPartEntry->StartSector.QuadPart;
748
749 DPRINT("First Sector: %I64u\n", NewPartEntry->StartSector.QuadPart);
750 DPRINT("Last Sector: %I64u\n", NewPartEntry->StartSector.QuadPart + NewPartEntry->SectorCount.QuadPart - 1);
751 DPRINT("Total Sectors: %I64u\n", NewPartEntry->SectorCount.QuadPart);
752
753 NewPartEntry->FormatState = Unformatted;
754
755 /* Append the table to the list */
756 InsertTailList(&DiskEntry->PrimaryPartListHead,
757 &NewPartEntry->ListEntry);
758 }
759 }
760
761 if (DiskEntry->ExtendedPartition != NULL)
762 {
763 if (IsListEmpty(&DiskEntry->LogicalPartListHead))
764 {
765 DPRINT1("No logical partition!\n");
766
767 /* Create a partition table entry that represents the empty extended partition */
768 NewPartEntry = RtlAllocateHeap(ProcessHeap,
769 HEAP_ZERO_MEMORY,
770 sizeof(PARTENTRY));
771 if (NewPartEntry == NULL)
772 return;
773
774 NewPartEntry->DiskEntry = DiskEntry;
775 NewPartEntry->LogicalPartition = TRUE;
776
777 NewPartEntry->IsPartitioned = FALSE;
778 NewPartEntry->StartSector.QuadPart = DiskEntry->ExtendedPartition->StartSector.QuadPart + (ULONGLONG)DiskEntry->SectorAlignment;
779 NewPartEntry->SectorCount.QuadPart = DiskEntry->ExtendedPartition->SectorCount.QuadPart - (ULONGLONG)DiskEntry->SectorAlignment;
780
781 DPRINT1("First Sector: %I64u\n", NewPartEntry->StartSector.QuadPart);
782 DPRINT1("Last Sector: %I64u\n", NewPartEntry->StartSector.QuadPart + NewPartEntry->SectorCount.QuadPart - 1);
783 DPRINT1("Total Sectors: %I64u\n", NewPartEntry->SectorCount.QuadPart);
784
785 NewPartEntry->FormatState = Unformatted;
786
787 InsertTailList(&DiskEntry->LogicalPartListHead,
788 &NewPartEntry->ListEntry);
789
790 return;
791 }
792
793 /* Start partition at head 1, cylinder 0 */
794 LastStartSector = DiskEntry->ExtendedPartition->StartSector.QuadPart + (ULONGLONG)DiskEntry->SectorAlignment;
795 LastSectorCount = 0ULL;
796 LastUnusedSectorCount = 0ULL;
797
798 Entry = DiskEntry->LogicalPartListHead.Flink;
799 while (Entry != &DiskEntry->LogicalPartListHead)
800 {
801 PartEntry = CONTAINING_RECORD(Entry, PARTENTRY, ListEntry);
802
803 if (PartEntry->PartitionType != PARTITION_ENTRY_UNUSED ||
804 PartEntry->SectorCount.QuadPart != 0ULL)
805 {
806 LastUnusedSectorCount =
807 PartEntry->StartSector.QuadPart - (ULONGLONG)DiskEntry->SectorAlignment - (LastStartSector + LastSectorCount);
808
809 if ((PartEntry->StartSector.QuadPart - (ULONGLONG)DiskEntry->SectorAlignment) > (LastStartSector + LastSectorCount) &&
810 LastUnusedSectorCount >= (ULONGLONG)DiskEntry->SectorAlignment)
811 {
812 DPRINT("Unpartitioned disk space %I64u sectors\n", LastUnusedSectorCount);
813
814 NewPartEntry = RtlAllocateHeap(ProcessHeap,
815 HEAP_ZERO_MEMORY,
816 sizeof(PARTENTRY));
817 if (NewPartEntry == NULL)
818 return;
819
820 NewPartEntry->DiskEntry = DiskEntry;
821 NewPartEntry->LogicalPartition = TRUE;
822
823 NewPartEntry->IsPartitioned = FALSE;
824 NewPartEntry->StartSector.QuadPart = LastStartSector + LastSectorCount;
825 NewPartEntry->SectorCount.QuadPart = Align(NewPartEntry->StartSector.QuadPart + LastUnusedSectorCount, DiskEntry->SectorAlignment) -
826 NewPartEntry->StartSector.QuadPart;
827
828 DPRINT("First Sector: %I64u\n", NewPartEntry->StartSector.QuadPart);
829 DPRINT("Last Sector: %I64u\n", NewPartEntry->StartSector.QuadPart + NewPartEntry->SectorCount.QuadPart - 1);
830 DPRINT("Total Sectors: %I64u\n", NewPartEntry->SectorCount.QuadPart);
831
832 NewPartEntry->FormatState = Unformatted;
833
834 /* Insert the table into the list */
835 InsertTailList(&PartEntry->ListEntry,
836 &NewPartEntry->ListEntry);
837 }
838
839 LastStartSector = PartEntry->StartSector.QuadPart;
840 LastSectorCount = PartEntry->SectorCount.QuadPart;
841 }
842
843 Entry = Entry->Flink;
844 }
845
846 /* Check for trailing unpartitioned disk space */
847 if ((LastStartSector + LastSectorCount) < DiskEntry->ExtendedPartition->StartSector.QuadPart + DiskEntry->ExtendedPartition->SectorCount.QuadPart)
848 {
849 LastUnusedSectorCount = Align(DiskEntry->ExtendedPartition->StartSector.QuadPart + DiskEntry->ExtendedPartition->SectorCount.QuadPart - (LastStartSector + LastSectorCount), DiskEntry->SectorAlignment);
850
851 if (LastUnusedSectorCount >= (ULONGLONG)DiskEntry->SectorAlignment)
852 {
853 DPRINT("Unpartitioned disk space: %I64u sectors\n", LastUnusedSectorCount);
854
855 NewPartEntry = RtlAllocateHeap(ProcessHeap,
856 HEAP_ZERO_MEMORY,
857 sizeof(PARTENTRY));
858 if (NewPartEntry == NULL)
859 return;
860
861 NewPartEntry->DiskEntry = DiskEntry;
862 NewPartEntry->LogicalPartition = TRUE;
863
864 NewPartEntry->IsPartitioned = FALSE;
865 NewPartEntry->StartSector.QuadPart = LastStartSector + LastSectorCount;
866 NewPartEntry->SectorCount.QuadPart = Align(NewPartEntry->StartSector.QuadPart + LastUnusedSectorCount, DiskEntry->SectorAlignment) -
867 NewPartEntry->StartSector.QuadPart;
868
869 DPRINT("First Sector: %I64u\n", NewPartEntry->StartSector.QuadPart);
870 DPRINT("Last Sector: %I64u\n", NewPartEntry->StartSector.QuadPart + NewPartEntry->SectorCount.QuadPart - 1);
871 DPRINT("Total Sectors: %I64u\n", NewPartEntry->SectorCount.QuadPart);
872
873 NewPartEntry->FormatState = Unformatted;
874
875 /* Append the table to the list */
876 InsertTailList(&DiskEntry->LogicalPartListHead,
877 &NewPartEntry->ListEntry);
878 }
879 }
880 }
881
882 DPRINT("ScanForUnpartitionedDiskSpace() done\n");
883 }
884
885
886 static
887 VOID
888 SetDiskSignature(
889 IN PPARTLIST List,
890 IN PDISKENTRY DiskEntry)
891 {
892 LARGE_INTEGER SystemTime;
893 TIME_FIELDS TimeFields;
894 PLIST_ENTRY Entry2;
895 PDISKENTRY DiskEntry2;
896 PUCHAR Buffer;
897
898 Buffer = (PUCHAR)&DiskEntry->LayoutBuffer->Signature;
899
900 while (1)
901 {
902 NtQuerySystemTime(&SystemTime);
903 RtlTimeToTimeFields(&SystemTime, &TimeFields);
904
905 Buffer[0] = (UCHAR)(TimeFields.Year & 0xFF) + (UCHAR)(TimeFields.Hour & 0xFF);
906 Buffer[1] = (UCHAR)(TimeFields.Year >> 8) + (UCHAR)(TimeFields.Minute & 0xFF);
907 Buffer[2] = (UCHAR)(TimeFields.Month & 0xFF) + (UCHAR)(TimeFields.Second & 0xFF);
908 Buffer[3] = (UCHAR)(TimeFields.Day & 0xFF) + (UCHAR)(TimeFields.Milliseconds & 0xFF);
909
910 if (DiskEntry->LayoutBuffer->Signature == 0)
911 {
912 continue;
913 }
914
915 /* check if the signature already exist */
916 /* FIXME:
917 * Check also signatures from disks, which are
918 * not visible (bootable) by the bios.
919 */
920 Entry2 = List->DiskListHead.Flink;
921 while (Entry2 != &List->DiskListHead)
922 {
923 DiskEntry2 = CONTAINING_RECORD(Entry2, DISKENTRY, ListEntry);
924
925 if (DiskEntry != DiskEntry2 &&
926 DiskEntry->LayoutBuffer->Signature == DiskEntry2->LayoutBuffer->Signature)
927 break;
928
929 Entry2 = Entry2->Flink;
930 }
931
932 if (Entry2 == &List->DiskListHead)
933 break;
934 }
935 }
936
937
938 static
939 VOID
940 UpdateDiskSignatures(
941 PPARTLIST List)
942 {
943 PLIST_ENTRY Entry;
944 PDISKENTRY DiskEntry;
945
946 /* Print partition lines*/
947 Entry = List->DiskListHead.Flink;
948 while (Entry != &List->DiskListHead)
949 {
950 DiskEntry = CONTAINING_RECORD(Entry, DISKENTRY, ListEntry);
951
952 if (DiskEntry->LayoutBuffer &&
953 DiskEntry->LayoutBuffer->Signature == 0)
954 {
955 SetDiskSignature(List, DiskEntry);
956 DiskEntry->LayoutBuffer->PartitionEntry[0].RewritePartition = TRUE;
957 }
958
959 Entry = Entry->Flink;
960 }
961 }
962
963
964 static
965 VOID
966 AddDiskToList(
967 HANDLE FileHandle,
968 ULONG DiskNumber,
969 PPARTLIST List)
970 {
971 DISK_GEOMETRY DiskGeometry;
972 SCSI_ADDRESS ScsiAddress;
973 PDISKENTRY DiskEntry;
974 IO_STATUS_BLOCK Iosb;
975 NTSTATUS Status;
976 PPARTITION_SECTOR Mbr;
977 PULONG Buffer;
978 LARGE_INTEGER FileOffset;
979 WCHAR Identifier[20];
980 ULONG Checksum;
981 ULONG Signature;
982 ULONG i;
983 PLIST_ENTRY ListEntry;
984 PBIOSDISKENTRY BiosDiskEntry;
985 ULONG LayoutBufferSize;
986
987 Status = NtDeviceIoControlFile(FileHandle,
988 NULL,
989 NULL,
990 NULL,
991 &Iosb,
992 IOCTL_DISK_GET_DRIVE_GEOMETRY,
993 NULL,
994 0,
995 &DiskGeometry,
996 sizeof(DISK_GEOMETRY));
997 if (!NT_SUCCESS(Status))
998 {
999 return;
1000 }
1001
1002 if (DiskGeometry.MediaType != FixedMedia &&
1003 DiskGeometry.MediaType != RemovableMedia)
1004 {
1005 return;
1006 }
1007
1008 Status = NtDeviceIoControlFile(FileHandle,
1009 NULL,
1010 NULL,
1011 NULL,
1012 &Iosb,
1013 IOCTL_SCSI_GET_ADDRESS,
1014 NULL,
1015 0,
1016 &ScsiAddress,
1017 sizeof(SCSI_ADDRESS));
1018 if (!NT_SUCCESS(Status))
1019 {
1020 return;
1021 }
1022
1023 Mbr = (PARTITION_SECTOR*)RtlAllocateHeap(ProcessHeap,
1024 0,
1025 DiskGeometry.BytesPerSector);
1026 if (Mbr == NULL)
1027 {
1028 return;
1029 }
1030
1031 FileOffset.QuadPart = 0;
1032 Status = NtReadFile(FileHandle,
1033 NULL,
1034 NULL,
1035 NULL,
1036 &Iosb,
1037 (PVOID)Mbr,
1038 DiskGeometry.BytesPerSector,
1039 &FileOffset,
1040 NULL);
1041 if (!NT_SUCCESS(Status))
1042 {
1043 RtlFreeHeap(ProcessHeap,
1044 0,
1045 Mbr);
1046 DPRINT1("NtReadFile failed, status=%x\n", Status);
1047 return;
1048 }
1049 Signature = Mbr->Signature;
1050
1051 /* Calculate the MBR checksum */
1052 Checksum = 0;
1053 Buffer = (PULONG)Mbr;
1054 for (i = 0; i < 128; i++)
1055 {
1056 Checksum += Buffer[i];
1057 }
1058 Checksum = ~Checksum + 1;
1059
1060 swprintf(Identifier, L"%08x-%08x-A", Checksum, Signature);
1061 DPRINT("Identifier: %S\n", Identifier);
1062
1063 DiskEntry = RtlAllocateHeap(ProcessHeap,
1064 HEAP_ZERO_MEMORY,
1065 sizeof(DISKENTRY));
1066 if (DiskEntry == NULL)
1067 {
1068 return;
1069 }
1070
1071 // DiskEntry->Checksum = Checksum;
1072 // DiskEntry->Signature = Signature;
1073 DiskEntry->BiosFound = FALSE;
1074
1075 /* Check if this disk has a valid MBR */
1076 if (Mbr->BootCode[0] == 0 && Mbr->BootCode[1] == 0)
1077 DiskEntry->NoMbr = TRUE;
1078 else
1079 DiskEntry->NoMbr = FALSE;
1080
1081 /* Free Mbr sector buffer */
1082 RtlFreeHeap(ProcessHeap,
1083 0,
1084 Mbr);
1085
1086 ListEntry = List->BiosDiskListHead.Flink;
1087 while(ListEntry != &List->BiosDiskListHead)
1088 {
1089 BiosDiskEntry = CONTAINING_RECORD(ListEntry, BIOSDISKENTRY, ListEntry);
1090 /* FIXME:
1091 * Compare the size from bios and the reported size from driver.
1092 * If we have more than one disk with a zero or with the same signatur
1093 * we must create new signatures and reboot. After the reboot,
1094 * it is possible to identify the disks.
1095 */
1096 if (BiosDiskEntry->Signature == Signature &&
1097 BiosDiskEntry->Checksum == Checksum &&
1098 !BiosDiskEntry->Recognized)
1099 {
1100 if (!DiskEntry->BiosFound)
1101 {
1102 DiskEntry->BiosDiskNumber = BiosDiskEntry->DiskNumber;
1103 DiskEntry->BiosFound = TRUE;
1104 BiosDiskEntry->Recognized = TRUE;
1105 }
1106 else
1107 {
1108 }
1109 }
1110 ListEntry = ListEntry->Flink;
1111 }
1112
1113 if (!DiskEntry->BiosFound)
1114 {
1115 #if 0
1116 RtlFreeHeap(ProcessHeap, 0, DiskEntry);
1117 return;
1118 #else
1119 DPRINT1("WARNING: Setup could not find a matching BIOS disk entry. Disk %d is not be bootable by the BIOS!\n", DiskNumber);
1120 #endif
1121 }
1122
1123 InitializeListHead(&DiskEntry->PrimaryPartListHead);
1124 InitializeListHead(&DiskEntry->LogicalPartListHead);
1125
1126 DiskEntry->Cylinders = DiskGeometry.Cylinders.QuadPart;
1127 DiskEntry->TracksPerCylinder = DiskGeometry.TracksPerCylinder;
1128 DiskEntry->SectorsPerTrack = DiskGeometry.SectorsPerTrack;
1129 DiskEntry->BytesPerSector = DiskGeometry.BytesPerSector;
1130
1131 DPRINT("Cylinders %I64u\n", DiskEntry->Cylinders);
1132 DPRINT("TracksPerCylinder %I64u\n", DiskEntry->TracksPerCylinder);
1133 DPRINT("SectorsPerTrack %I64u\n", DiskEntry->SectorsPerTrack);
1134 DPRINT("BytesPerSector %I64u\n", DiskEntry->BytesPerSector);
1135
1136 DiskEntry->SectorCount.QuadPart = DiskGeometry.Cylinders.QuadPart *
1137 (ULONGLONG)DiskGeometry.TracksPerCylinder *
1138 (ULONGLONG)DiskGeometry.SectorsPerTrack;
1139
1140 DiskEntry->SectorAlignment = DiskGeometry.SectorsPerTrack;
1141
1142 DPRINT("SectorCount %I64u\n", DiskEntry->SectorCount);
1143 DPRINT("SectorAlignment %lu\n", DiskEntry->SectorAlignment);
1144
1145 DiskEntry->DiskNumber = DiskNumber;
1146 DiskEntry->Port = ScsiAddress.PortNumber;
1147 DiskEntry->Bus = ScsiAddress.PathId;
1148 DiskEntry->Id = ScsiAddress.TargetId;
1149
1150 GetDriverName(DiskEntry);
1151
1152 InsertAscendingList(&List->DiskListHead, DiskEntry, DISKENTRY, ListEntry, DiskNumber);
1153
1154 /*
1155 * Allocate a buffer for 26 logical drives (2 entries each == 52)
1156 * plus the main partiton table (4 entries). Total 56 entries.
1157 */
1158 LayoutBufferSize = sizeof(DRIVE_LAYOUT_INFORMATION) +
1159 ((56 - ANYSIZE_ARRAY) * sizeof(PARTITION_INFORMATION));
1160 DiskEntry->LayoutBuffer = RtlAllocateHeap(ProcessHeap,
1161 HEAP_ZERO_MEMORY,
1162 LayoutBufferSize);
1163 if (DiskEntry->LayoutBuffer == NULL)
1164 {
1165 return;
1166 }
1167
1168 Status = NtDeviceIoControlFile(FileHandle,
1169 NULL,
1170 NULL,
1171 NULL,
1172 &Iosb,
1173 IOCTL_DISK_GET_DRIVE_LAYOUT,
1174 NULL,
1175 0,
1176 DiskEntry->LayoutBuffer,
1177 LayoutBufferSize);
1178 if (NT_SUCCESS(Status))
1179 {
1180 #ifdef DUMP_PARTITION_TABLE
1181 DumpPartitionTable(DiskEntry);
1182 #endif
1183
1184 if (DiskEntry->LayoutBuffer->PartitionEntry[0].StartingOffset.QuadPart != 0 &&
1185 DiskEntry->LayoutBuffer->PartitionEntry[0].PartitionLength.QuadPart != 0 &&
1186 DiskEntry->LayoutBuffer->PartitionEntry[0].PartitionType != 0)
1187 {
1188 if ((DiskEntry->LayoutBuffer->PartitionEntry[0].StartingOffset.QuadPart / DiskEntry->BytesPerSector) % DiskEntry->SectorsPerTrack == 0)
1189 {
1190 DPRINT("Use %lu Sector alignment!\n", DiskEntry->SectorsPerTrack);
1191 }
1192 else if (DiskEntry->LayoutBuffer->PartitionEntry[0].StartingOffset.QuadPart % 1048756 == 0)
1193 {
1194 DPRINT1("Use megabyte (%lu Sectors) alignment!\n", 1048756 / DiskEntry->BytesPerSector);
1195 }
1196 else
1197 {
1198 DPRINT1("No matching aligment found! Partiton 1 starts at %I64u\n", DiskEntry->LayoutBuffer->PartitionEntry[0].StartingOffset.QuadPart);
1199 }
1200 }
1201 else
1202 {
1203 DPRINT1("No valid partiton table found! Use megabyte (%lu Sectors) alignment!\n", 1048756 / DiskEntry->BytesPerSector);
1204 }
1205
1206
1207 if (DiskEntry->LayoutBuffer->PartitionCount == 0)
1208 {
1209 DiskEntry->NewDisk = TRUE;
1210 DiskEntry->LayoutBuffer->PartitionCount = 4;
1211
1212 for (i = 0; i < 4; i++)
1213 DiskEntry->LayoutBuffer->PartitionEntry[i].RewritePartition = TRUE;
1214 }
1215 else
1216 {
1217 for (i = 0; i < 4; i++)
1218 {
1219 AddPartitionToDisk(DiskNumber,
1220 DiskEntry,
1221 i,
1222 FALSE);
1223 }
1224
1225 for (i = 4; i < DiskEntry->LayoutBuffer->PartitionCount; i += 4)
1226 {
1227 AddPartitionToDisk(DiskNumber,
1228 DiskEntry,
1229 i,
1230 TRUE);
1231 }
1232 }
1233 }
1234
1235 ScanForUnpartitionedDiskSpace(DiskEntry);
1236 }
1237
1238
1239 PPARTLIST
1240 CreatePartitionList(
1241 SHORT Left,
1242 SHORT Top,
1243 SHORT Right,
1244 SHORT Bottom)
1245 {
1246 PPARTLIST List;
1247 OBJECT_ATTRIBUTES ObjectAttributes;
1248 SYSTEM_DEVICE_INFORMATION Sdi;
1249 IO_STATUS_BLOCK Iosb;
1250 ULONG ReturnSize;
1251 NTSTATUS Status;
1252 ULONG DiskNumber;
1253 WCHAR Buffer[MAX_PATH];
1254 UNICODE_STRING Name;
1255 HANDLE FileHandle;
1256
1257 List = (PPARTLIST)RtlAllocateHeap(ProcessHeap,
1258 0,
1259 sizeof (PARTLIST));
1260 if (List == NULL)
1261 return NULL;
1262
1263 List->Left = Left;
1264 List->Top = Top;
1265 List->Right = Right;
1266 List->Bottom = Bottom;
1267
1268 List->Line = 0;
1269 List->Offset = 0;
1270
1271 List->TopDisk = (ULONG)-1;
1272 List->TopPartition = (ULONG)-1;
1273
1274 List->CurrentDisk = NULL;
1275 List->CurrentPartition = NULL;
1276
1277 InitializeListHead(&List->DiskListHead);
1278 InitializeListHead(&List->BiosDiskListHead);
1279
1280 EnumerateBiosDiskEntries(List);
1281
1282 Status = NtQuerySystemInformation(SystemDeviceInformation,
1283 &Sdi,
1284 sizeof(SYSTEM_DEVICE_INFORMATION),
1285 &ReturnSize);
1286 if (!NT_SUCCESS(Status))
1287 {
1288 RtlFreeHeap(ProcessHeap, 0, List);
1289 return NULL;
1290 }
1291
1292 for (DiskNumber = 0; DiskNumber < Sdi.NumberOfDisks; DiskNumber++)
1293 {
1294 swprintf(Buffer,
1295 L"\\Device\\Harddisk%d\\Partition0",
1296 DiskNumber);
1297 RtlInitUnicodeString(&Name,
1298 Buffer);
1299
1300 InitializeObjectAttributes(&ObjectAttributes,
1301 &Name,
1302 0,
1303 NULL,
1304 NULL);
1305
1306 Status = NtOpenFile(&FileHandle,
1307 FILE_READ_DATA | FILE_READ_ATTRIBUTES | SYNCHRONIZE,
1308 &ObjectAttributes,
1309 &Iosb,
1310 FILE_SHARE_READ,
1311 FILE_SYNCHRONOUS_IO_NONALERT);
1312 if (NT_SUCCESS(Status))
1313 {
1314 AddDiskToList(FileHandle,
1315 DiskNumber,
1316 List);
1317
1318 NtClose(FileHandle);
1319 }
1320 }
1321
1322 UpdateDiskSignatures(List);
1323
1324 AssignDriveLetters(List);
1325
1326 List->TopDisk = 0;
1327 List->TopPartition = 0;
1328
1329 /* Search for first usable disk and partition */
1330 if (IsListEmpty(&List->DiskListHead))
1331 {
1332 List->CurrentDisk = NULL;
1333 List->CurrentPartition = NULL;
1334 }
1335 else
1336 {
1337 List->CurrentDisk = CONTAINING_RECORD(List->DiskListHead.Flink,
1338 DISKENTRY,
1339 ListEntry);
1340
1341 if (IsListEmpty(&List->CurrentDisk->PrimaryPartListHead))
1342 {
1343 List->CurrentPartition = 0;
1344 }
1345 else
1346 {
1347 List->CurrentPartition = CONTAINING_RECORD(List->CurrentDisk->PrimaryPartListHead.Flink,
1348 PARTENTRY,
1349 ListEntry);
1350 }
1351 }
1352
1353 return List;
1354 }
1355
1356
1357 VOID
1358 DestroyPartitionList(
1359 PPARTLIST List)
1360 {
1361 PDISKENTRY DiskEntry;
1362 PBIOSDISKENTRY BiosDiskEntry;
1363 PPARTENTRY PartEntry;
1364 PLIST_ENTRY Entry;
1365
1366 /* Release disk and partition info */
1367 while (!IsListEmpty(&List->DiskListHead))
1368 {
1369 Entry = RemoveHeadList(&List->DiskListHead);
1370 DiskEntry = CONTAINING_RECORD(Entry, DISKENTRY, ListEntry);
1371
1372 /* Release driver name */
1373 RtlFreeUnicodeString(&DiskEntry->DriverName);
1374
1375 /* Release primary partition list */
1376 while (!IsListEmpty(&DiskEntry->PrimaryPartListHead))
1377 {
1378 Entry = RemoveHeadList(&DiskEntry->PrimaryPartListHead);
1379 PartEntry = CONTAINING_RECORD(Entry, PARTENTRY, ListEntry);
1380
1381 RtlFreeHeap(ProcessHeap, 0, PartEntry);
1382 }
1383
1384 /* Release logical partition list */
1385 while (!IsListEmpty(&DiskEntry->LogicalPartListHead))
1386 {
1387 Entry = RemoveHeadList(&DiskEntry->LogicalPartListHead);
1388 PartEntry = CONTAINING_RECORD(Entry, PARTENTRY, ListEntry);
1389
1390 RtlFreeHeap(ProcessHeap, 0, PartEntry);
1391 }
1392
1393 /* Release layout buffer */
1394 if (DiskEntry->LayoutBuffer != NULL)
1395 RtlFreeHeap(ProcessHeap, 0, DiskEntry->LayoutBuffer);
1396
1397
1398 /* Release disk entry */
1399 RtlFreeHeap(ProcessHeap, 0, DiskEntry);
1400 }
1401
1402 /* release the bios disk info */
1403 while(!IsListEmpty(&List->BiosDiskListHead))
1404 {
1405 Entry = RemoveHeadList(&List->BiosDiskListHead);
1406 BiosDiskEntry = CONTAINING_RECORD(Entry, BIOSDISKENTRY, ListEntry);
1407
1408 RtlFreeHeap(ProcessHeap, 0, BiosDiskEntry);
1409 }
1410
1411 /* Release list head */
1412 RtlFreeHeap(ProcessHeap, 0, List);
1413 }
1414
1415
1416 static
1417 VOID
1418 PrintEmptyLine(
1419 PPARTLIST List)
1420 {
1421 COORD coPos;
1422 DWORD Written;
1423 USHORT Width;
1424 USHORT Height;
1425
1426 Width = List->Right - List->Left - 1;
1427 Height = List->Bottom - List->Top - 2;
1428
1429 coPos.X = List->Left + 1;
1430 coPos.Y = List->Top + 1 + List->Line;
1431
1432 if (List->Line >= 0 && List->Line <= Height)
1433 {
1434 FillConsoleOutputAttribute(StdOutput,
1435 FOREGROUND_WHITE | BACKGROUND_BLUE,
1436 Width,
1437 coPos,
1438 &Written);
1439
1440 FillConsoleOutputCharacterA(StdOutput,
1441 ' ',
1442 Width,
1443 coPos,
1444 &Written);
1445 }
1446
1447 List->Line++;
1448 }
1449
1450
1451 static
1452 VOID
1453 PrintPartitionData(
1454 PPARTLIST List,
1455 PDISKENTRY DiskEntry,
1456 PPARTENTRY PartEntry)
1457 {
1458 CHAR LineBuffer[128];
1459 COORD coPos;
1460 DWORD Written;
1461 USHORT Width;
1462 USHORT Height;
1463 LARGE_INTEGER PartSize;
1464 PCHAR Unit;
1465 UCHAR Attribute;
1466 PCHAR PartType;
1467
1468 Width = List->Right - List->Left - 1;
1469 Height = List->Bottom - List->Top - 2;
1470
1471 coPos.X = List->Left + 1;
1472 coPos.Y = List->Top + 1 + List->Line;
1473
1474 if (PartEntry->IsPartitioned == FALSE)
1475 {
1476 PartSize.QuadPart = PartEntry->SectorCount.QuadPart * DiskEntry->BytesPerSector;
1477 #if 0
1478 if (PartSize.QuadPart >= 10737418240) /* 10 GB */
1479 {
1480 PartSize.QuadPart = RoundingDivide(PartSize.QuadPart, 1073741824);
1481 Unit = MUIGetString(STRING_GB);
1482 }
1483 else
1484 #endif
1485 if (PartSize.QuadPart >= 10485760) /* 10 MB */
1486 {
1487 PartSize.QuadPart = RoundingDivide(PartSize.QuadPart, 1048576);
1488 Unit = MUIGetString(STRING_MB);
1489 }
1490 else
1491 {
1492 PartSize.QuadPart = RoundingDivide(PartSize.QuadPart, 1024);
1493 Unit = MUIGetString(STRING_KB);
1494 }
1495
1496 sprintf(LineBuffer,
1497 MUIGetString(STRING_UNPSPACE),
1498 PartEntry->LogicalPartition ? " " : "",
1499 PartEntry->LogicalPartition ? "" : " ",
1500 PartSize.u.LowPart,
1501 Unit);
1502 }
1503 else
1504 {
1505 /* Determine partition type */
1506 PartType = NULL;
1507 if (PartEntry->New == TRUE)
1508 {
1509 PartType = MUIGetString(STRING_UNFORMATTED);
1510 }
1511 else if (PartEntry->IsPartitioned == TRUE)
1512 {
1513 if ((PartEntry->PartitionType == PARTITION_FAT_12) ||
1514 (PartEntry->PartitionType == PARTITION_FAT_16) ||
1515 (PartEntry->PartitionType == PARTITION_HUGE) ||
1516 (PartEntry->PartitionType == PARTITION_XINT13))
1517 {
1518 PartType = "FAT";
1519 }
1520 else if ((PartEntry->PartitionType == PARTITION_FAT32) ||
1521 (PartEntry->PartitionType == PARTITION_FAT32_XINT13))
1522 {
1523 PartType = "FAT32";
1524 }
1525 else if (PartEntry->PartitionType == PARTITION_EXT2)
1526 {
1527 PartType = "EXT2";
1528 }
1529 else if (PartEntry->PartitionType == PARTITION_IFS)
1530 {
1531 PartType = "NTFS"; /* FIXME: Not quite correct! */
1532 }
1533 else if ((PartEntry->PartitionType == PARTITION_EXTENDED) ||
1534 (PartEntry->PartitionType == PARTITION_XINT13_EXTENDED))
1535 {
1536 PartType = MUIGetString(STRING_EXTENDED_PARTITION);
1537 }
1538 }
1539
1540 PartSize.QuadPart = PartEntry->SectorCount.QuadPart * DiskEntry->BytesPerSector;
1541 #if 0
1542 if (PartSize.QuadPart >= 10737418240) /* 10 GB */
1543 {
1544 PartSize.QuadPart = RoundingDivide(PartSize.QuadPart, 1073741824);
1545 Unit = MUIGetString(STRING_GB);
1546 }
1547 else
1548 #endif
1549 if (PartSize.QuadPart >= 10485760) /* 10 MB */
1550 {
1551 PartSize.QuadPart = RoundingDivide(PartSize.QuadPart, 1048576);
1552 Unit = MUIGetString(STRING_MB);
1553 }
1554 else
1555 {
1556 PartSize.QuadPart = RoundingDivide(PartSize.QuadPart, 1024);
1557 Unit = MUIGetString(STRING_KB);
1558 }
1559
1560 if (PartType == NULL)
1561 {
1562 sprintf(LineBuffer,
1563 MUIGetString(STRING_HDDINFOUNK5),
1564 (PartEntry->DriveLetter == 0) ? '-' : PartEntry->DriveLetter,
1565 (PartEntry->DriveLetter == 0) ? '-' : ':',
1566 PartEntry->BootIndicator ? '*' : ' ',
1567 PartEntry->LogicalPartition ? " " : "",
1568 PartEntry->PartitionType,
1569 PartEntry->LogicalPartition ? "" : " ",
1570 PartSize.u.LowPart,
1571 Unit);
1572 }
1573 else
1574 {
1575 sprintf(LineBuffer,
1576 "%c%c %c %s%-24s%s %6lu %s",
1577 (PartEntry->DriveLetter == 0) ? '-' : PartEntry->DriveLetter,
1578 (PartEntry->DriveLetter == 0) ? '-' : ':',
1579 PartEntry->BootIndicator ? '*' : ' ',
1580 PartEntry->LogicalPartition ? " " : "",
1581 PartType,
1582 PartEntry->LogicalPartition ? "" : " ",
1583 PartSize.u.LowPart,
1584 Unit);
1585 }
1586 }
1587
1588 Attribute = (List->CurrentDisk == DiskEntry &&
1589 List->CurrentPartition == PartEntry) ?
1590 FOREGROUND_BLUE | BACKGROUND_WHITE :
1591 FOREGROUND_WHITE | BACKGROUND_BLUE;
1592
1593 if (List->Line >= 0 && List->Line <= Height)
1594 {
1595 FillConsoleOutputCharacterA(StdOutput,
1596 ' ',
1597 Width,
1598 coPos,
1599 &Written);
1600 }
1601 coPos.X += 4;
1602 Width -= 8;
1603 if (List->Line >= 0 && List->Line <= Height)
1604 {
1605 FillConsoleOutputAttribute(StdOutput,
1606 Attribute,
1607 Width,
1608 coPos,
1609 &Written);
1610 }
1611 coPos.X++;
1612 Width -= 2;
1613 if (List->Line >= 0 && List->Line <= Height)
1614 {
1615 WriteConsoleOutputCharacterA(StdOutput,
1616 LineBuffer,
1617 min(strlen(LineBuffer), Width),
1618 coPos,
1619 &Written);
1620 }
1621
1622 List->Line++;
1623 }
1624
1625
1626 static
1627 VOID
1628 PrintDiskData(
1629 PPARTLIST List,
1630 PDISKENTRY DiskEntry)
1631 {
1632 PPARTENTRY PrimaryPartEntry, LogicalPartEntry;
1633 PLIST_ENTRY PrimaryEntry, LogicalEntry;
1634 CHAR LineBuffer[128];
1635 COORD coPos;
1636 DWORD Written;
1637 USHORT Width;
1638 USHORT Height;
1639 ULARGE_INTEGER DiskSize;
1640 PCHAR Unit;
1641
1642 Width = List->Right - List->Left - 1;
1643 Height = List->Bottom - List->Top - 2;
1644
1645 coPos.X = List->Left + 1;
1646 coPos.Y = List->Top + 1 + List->Line;
1647
1648 DiskSize.QuadPart = DiskEntry->SectorCount.QuadPart * DiskEntry->BytesPerSector;
1649 if (DiskSize.QuadPart >= 10737418240) /* 10 GB */
1650 {
1651 DiskSize.QuadPart = RoundingDivide(DiskSize.QuadPart, 1073741824);
1652 Unit = MUIGetString(STRING_GB);
1653 }
1654 else
1655 {
1656 DiskSize.QuadPart = RoundingDivide(DiskSize.QuadPart, 1048576);
1657 if (DiskSize.QuadPart == 0)
1658 DiskSize.QuadPart = 1;
1659 Unit = MUIGetString(STRING_MB);
1660 }
1661
1662 if (DiskEntry->DriverName.Length > 0)
1663 {
1664 sprintf(LineBuffer,
1665 MUIGetString(STRING_HDINFOPARTSELECT),
1666 DiskSize.u.LowPart,
1667 Unit,
1668 DiskEntry->DiskNumber,
1669 DiskEntry->Port,
1670 DiskEntry->Bus,
1671 DiskEntry->Id,
1672 DiskEntry->DriverName.Buffer);
1673 }
1674 else
1675 {
1676 sprintf(LineBuffer,
1677 MUIGetString(STRING_HDDINFOUNK6),
1678 DiskSize.u.LowPart,
1679 Unit,
1680 DiskEntry->DiskNumber,
1681 DiskEntry->Port,
1682 DiskEntry->Bus,
1683 DiskEntry->Id);
1684 }
1685
1686 if (List->Line >= 0 && List->Line <= Height)
1687 {
1688 FillConsoleOutputAttribute(StdOutput,
1689 FOREGROUND_WHITE | BACKGROUND_BLUE,
1690 Width,
1691 coPos,
1692 &Written);
1693
1694 FillConsoleOutputCharacterA(StdOutput,
1695 ' ',
1696 Width,
1697 coPos,
1698 &Written);
1699 }
1700
1701 coPos.X++;
1702 if (List->Line >= 0 && List->Line <= Height)
1703 {
1704 WriteConsoleOutputCharacterA(StdOutput,
1705 LineBuffer,
1706 min((USHORT)strlen(LineBuffer), Width - 2),
1707 coPos,
1708 &Written);
1709 }
1710
1711 List->Line++;
1712
1713 /* Print separator line */
1714 PrintEmptyLine(List);
1715
1716 /* Print partition lines*/
1717 PrimaryEntry = DiskEntry->PrimaryPartListHead.Flink;
1718 while (PrimaryEntry != &DiskEntry->PrimaryPartListHead)
1719 {
1720 PrimaryPartEntry = CONTAINING_RECORD(PrimaryEntry, PARTENTRY, ListEntry);
1721
1722 PrintPartitionData(List,
1723 DiskEntry,
1724 PrimaryPartEntry);
1725
1726 if (IsContainerPartition(PrimaryPartEntry->PartitionType))
1727 {
1728 LogicalEntry = DiskEntry->LogicalPartListHead.Flink;
1729 while (LogicalEntry != &DiskEntry->LogicalPartListHead)
1730 {
1731 LogicalPartEntry = CONTAINING_RECORD(LogicalEntry, PARTENTRY, ListEntry);
1732
1733 PrintPartitionData(List,
1734 DiskEntry,
1735 LogicalPartEntry);
1736
1737 LogicalEntry = LogicalEntry->Flink;
1738 }
1739 }
1740
1741 PrimaryEntry = PrimaryEntry->Flink;
1742 }
1743
1744 /* Print separator line */
1745 PrintEmptyLine(List);
1746 }
1747
1748
1749 VOID
1750 DrawPartitionList(
1751 PPARTLIST List)
1752 {
1753 PLIST_ENTRY Entry, Entry2;
1754 PDISKENTRY DiskEntry;
1755 PPARTENTRY PartEntry = NULL;
1756 COORD coPos;
1757 DWORD Written;
1758 SHORT i;
1759 SHORT CurrentDiskLine;
1760 SHORT CurrentPartLine;
1761 SHORT LastLine;
1762 BOOL CurrentPartLineFound = FALSE;
1763 BOOL CurrentDiskLineFound = FALSE;
1764
1765 /* Calculate the line of the current disk and partition */
1766 CurrentDiskLine = 0;
1767 CurrentPartLine = 0;
1768 LastLine = 0;
1769
1770 Entry = List->DiskListHead.Flink;
1771 while (Entry != &List->DiskListHead)
1772 {
1773 DiskEntry = CONTAINING_RECORD(Entry, DISKENTRY, ListEntry);
1774
1775 LastLine += 2;
1776 if (CurrentPartLineFound == FALSE)
1777 {
1778 CurrentPartLine += 2;
1779 }
1780
1781 Entry2 = DiskEntry->PrimaryPartListHead.Flink;
1782 while (Entry2 != &DiskEntry->PrimaryPartListHead)
1783 {
1784 PartEntry = CONTAINING_RECORD(Entry2, PARTENTRY, ListEntry);
1785 if (PartEntry == List->CurrentPartition)
1786 {
1787 CurrentPartLineFound = TRUE;
1788 }
1789
1790 Entry2 = Entry2->Flink;
1791 if (CurrentPartLineFound == FALSE)
1792 {
1793 CurrentPartLine++;
1794 }
1795
1796 LastLine++;
1797 }
1798
1799 if (DiskEntry == List->CurrentDisk)
1800 {
1801 CurrentDiskLineFound = TRUE;
1802 }
1803
1804 Entry = Entry->Flink;
1805 if (Entry != &List->DiskListHead)
1806 {
1807 if (CurrentDiskLineFound == FALSE)
1808 {
1809 CurrentPartLine ++;
1810 CurrentDiskLine = CurrentPartLine;
1811 }
1812
1813 LastLine++;
1814 }
1815 else
1816 {
1817 LastLine--;
1818 }
1819 }
1820
1821 /* If it possible, make the disk name visible */
1822 if (CurrentPartLine < List->Offset)
1823 {
1824 List->Offset = CurrentPartLine;
1825 }
1826 else if (CurrentPartLine - List->Offset > List->Bottom - List->Top - 2)
1827 {
1828 List->Offset = CurrentPartLine - (List->Bottom - List->Top - 2);
1829 }
1830
1831 if (CurrentDiskLine < List->Offset && CurrentPartLine - CurrentDiskLine < List->Bottom - List->Top - 2)
1832 {
1833 List->Offset = CurrentDiskLine;
1834 }
1835
1836 /* draw upper left corner */
1837 coPos.X = List->Left;
1838 coPos.Y = List->Top;
1839 FillConsoleOutputCharacterA(StdOutput,
1840 0xDA, // '+',
1841 1,
1842 coPos,
1843 &Written);
1844
1845 /* draw upper edge */
1846 coPos.X = List->Left + 1;
1847 coPos.Y = List->Top;
1848 if (List->Offset == 0)
1849 {
1850 FillConsoleOutputCharacterA(StdOutput,
1851 0xC4, // '-',
1852 List->Right - List->Left - 1,
1853 coPos,
1854 &Written);
1855 }
1856 else
1857 {
1858 FillConsoleOutputCharacterA(StdOutput,
1859 0xC4, // '-',
1860 List->Right - List->Left - 5,
1861 coPos,
1862 &Written);
1863 coPos.X = List->Right - 5;
1864 WriteConsoleOutputCharacterA(StdOutput,
1865 "(\x18)", // "(up)"
1866 3,
1867 coPos,
1868 &Written);
1869 coPos.X = List->Right - 2;
1870 FillConsoleOutputCharacterA(StdOutput,
1871 0xC4, // '-',
1872 2,
1873 coPos,
1874 &Written);
1875 }
1876
1877 /* draw upper right corner */
1878 coPos.X = List->Right;
1879 coPos.Y = List->Top;
1880 FillConsoleOutputCharacterA(StdOutput,
1881 0xBF, // '+',
1882 1,
1883 coPos,
1884 &Written);
1885
1886 /* draw left and right edge */
1887 for (i = List->Top + 1; i < List->Bottom; i++)
1888 {
1889 coPos.X = List->Left;
1890 coPos.Y = i;
1891 FillConsoleOutputCharacterA(StdOutput,
1892 0xB3, // '|',
1893 1,
1894 coPos,
1895 &Written);
1896
1897 coPos.X = List->Right;
1898 FillConsoleOutputCharacterA(StdOutput,
1899 0xB3, //'|',
1900 1,
1901 coPos,
1902 &Written);
1903 }
1904
1905 /* draw lower left corner */
1906 coPos.X = List->Left;
1907 coPos.Y = List->Bottom;
1908 FillConsoleOutputCharacterA(StdOutput,
1909 0xC0, // '+',
1910 1,
1911 coPos,
1912 &Written);
1913
1914 /* draw lower edge */
1915 coPos.X = List->Left + 1;
1916 coPos.Y = List->Bottom;
1917 if (LastLine - List->Offset <= List->Bottom - List->Top - 2)
1918 {
1919 FillConsoleOutputCharacterA(StdOutput,
1920 0xC4, // '-',
1921 List->Right - List->Left - 1,
1922 coPos,
1923 &Written);
1924 }
1925 else
1926 {
1927 FillConsoleOutputCharacterA(StdOutput,
1928 0xC4, // '-',
1929 List->Right - List->Left - 5,
1930 coPos,
1931 &Written);
1932 coPos.X = List->Right - 5;
1933 WriteConsoleOutputCharacterA(StdOutput,
1934 "(\x19)", // "(down)"
1935 3,
1936 coPos,
1937 &Written);
1938 coPos.X = List->Right - 2;
1939 FillConsoleOutputCharacterA(StdOutput,
1940 0xC4, // '-',
1941 2,
1942 coPos,
1943 &Written);
1944 }
1945
1946 /* draw lower right corner */
1947 coPos.X = List->Right;
1948 coPos.Y = List->Bottom;
1949 FillConsoleOutputCharacterA(StdOutput,
1950 0xD9, // '+',
1951 1,
1952 coPos,
1953 &Written);
1954
1955 /* print list entries */
1956 List->Line = - List->Offset;
1957
1958 Entry = List->DiskListHead.Flink;
1959 while (Entry != &List->DiskListHead)
1960 {
1961 DiskEntry = CONTAINING_RECORD(Entry, DISKENTRY, ListEntry);
1962
1963 /* Print disk entry */
1964 PrintDiskData(List,
1965 DiskEntry);
1966
1967 Entry = Entry->Flink;
1968 }
1969 }
1970
1971
1972 DWORD
1973 SelectPartition(
1974 PPARTLIST List,
1975 ULONG DiskNumber,
1976 ULONG PartitionNumber)
1977 {
1978 PDISKENTRY DiskEntry;
1979 PPARTENTRY PartEntry;
1980 PLIST_ENTRY Entry1;
1981 PLIST_ENTRY Entry2;
1982
1983 /* Check for empty disks */
1984 if (IsListEmpty(&List->DiskListHead))
1985 return FALSE;
1986
1987 /* Check for first usable entry on next disk */
1988 Entry1 = List->CurrentDisk->ListEntry.Flink;
1989 while (Entry1 != &List->DiskListHead)
1990 {
1991 DiskEntry = CONTAINING_RECORD(Entry1, DISKENTRY, ListEntry);
1992
1993 if (DiskEntry->DiskNumber == DiskNumber)
1994 {
1995 Entry2 = DiskEntry->PrimaryPartListHead.Flink;
1996 while (Entry2 != &DiskEntry->PrimaryPartListHead)
1997 {
1998 PartEntry = CONTAINING_RECORD(Entry2, PARTENTRY, ListEntry);
1999
2000 if (PartEntry->PartitionNumber == PartitionNumber)
2001 {
2002 List->CurrentDisk = DiskEntry;
2003 List->CurrentPartition = PartEntry;
2004 DrawPartitionList(List);
2005 return TRUE;
2006 }
2007
2008 Entry2 = Entry2->Flink;
2009 }
2010
2011 return FALSE;
2012 }
2013
2014 Entry1 = Entry1->Flink;
2015 }
2016
2017 return FALSE;
2018 }
2019
2020
2021 BOOL
2022 ScrollDownPartitionList(
2023 PPARTLIST List)
2024 {
2025 PLIST_ENTRY DiskListEntry;
2026 PLIST_ENTRY PartListEntry;
2027 PDISKENTRY DiskEntry;
2028 PPARTENTRY PartEntry;
2029
2030 /* Fail, if no disks are available */
2031 if (IsListEmpty(&List->DiskListHead))
2032 return FALSE;
2033
2034 /* Check for next usable entry on current disk */
2035 if (List->CurrentPartition != NULL)
2036 {
2037 if (List->CurrentPartition->LogicalPartition)
2038 {
2039 /* Logical partition */
2040
2041 PartListEntry = List->CurrentPartition->ListEntry.Flink;
2042 if (PartListEntry != &List->CurrentDisk->LogicalPartListHead)
2043 {
2044 /* Next logical partition */
2045 PartEntry = CONTAINING_RECORD(PartListEntry, PARTENTRY, ListEntry);
2046
2047 List->CurrentPartition = PartEntry;
2048 return TRUE;
2049 }
2050 else
2051 {
2052 PartListEntry = List->CurrentDisk->ExtendedPartition->ListEntry.Flink;
2053 if (PartListEntry != &List->CurrentDisk->PrimaryPartListHead)
2054 {
2055 PartEntry = CONTAINING_RECORD(PartListEntry, PARTENTRY, ListEntry);
2056
2057 List->CurrentPartition = PartEntry;
2058 return TRUE;
2059 }
2060 }
2061 }
2062 else
2063 {
2064 /* Primary or extended partition */
2065
2066 if (List->CurrentPartition->IsPartitioned == TRUE &&
2067 IsContainerPartition(List->CurrentPartition->PartitionType))
2068 {
2069 /* First logical partition */
2070 PartListEntry = List->CurrentDisk->LogicalPartListHead.Flink;
2071 if (PartListEntry != &List->CurrentDisk->LogicalPartListHead)
2072 {
2073 PartEntry = CONTAINING_RECORD(PartListEntry, PARTENTRY, ListEntry);
2074
2075 List->CurrentPartition = PartEntry;
2076 return TRUE;
2077 }
2078 }
2079 else
2080 {
2081 /* Next primary partition */
2082 PartListEntry = List->CurrentPartition->ListEntry.Flink;
2083 if (PartListEntry != &List->CurrentDisk->PrimaryPartListHead)
2084 {
2085 PartEntry = CONTAINING_RECORD(PartListEntry, PARTENTRY, ListEntry);
2086
2087 List->CurrentPartition = PartEntry;
2088 return TRUE;
2089 }
2090 }
2091 }
2092 }
2093
2094 /* Search for the first partition entry on the next disk */
2095 DiskListEntry = List->CurrentDisk->ListEntry.Flink;
2096 while (DiskListEntry != &List->DiskListHead)
2097 {
2098 DiskEntry = CONTAINING_RECORD(DiskListEntry, DISKENTRY, ListEntry);
2099
2100 PartListEntry = DiskEntry->PrimaryPartListHead.Flink;
2101 if (PartListEntry != &DiskEntry->PrimaryPartListHead)
2102 {
2103 PartEntry = CONTAINING_RECORD(PartListEntry, PARTENTRY, ListEntry);
2104
2105 List->CurrentDisk = DiskEntry;
2106 List->CurrentPartition = PartEntry;
2107 return TRUE;
2108 }
2109
2110 DiskListEntry = DiskListEntry->Flink;
2111 }
2112
2113 return FALSE;
2114 }
2115
2116
2117 BOOL
2118 ScrollUpPartitionList(
2119 PPARTLIST List)
2120 {
2121 PLIST_ENTRY DiskListEntry;
2122 PLIST_ENTRY PartListEntry;
2123 PDISKENTRY DiskEntry;
2124 PPARTENTRY PartEntry;
2125
2126 /* Fail, if no disks are available */
2127 if (IsListEmpty(&List->DiskListHead))
2128 return FALSE;
2129
2130 /* Check for previous usable entry on current disk */
2131 if (List->CurrentPartition != NULL)
2132 {
2133 if (List->CurrentPartition->LogicalPartition)
2134 {
2135 /* Logical partition */
2136 PartListEntry = List->CurrentPartition->ListEntry.Blink;
2137 if (PartListEntry != &List->CurrentDisk->LogicalPartListHead)
2138 {
2139 /* Previous logical partition */
2140 PartEntry = CONTAINING_RECORD(PartListEntry, PARTENTRY, ListEntry);
2141 }
2142 else
2143 {
2144 /* Extended partition*/
2145 PartEntry = List->CurrentDisk->ExtendedPartition;
2146 }
2147
2148 List->CurrentPartition = PartEntry;
2149 return TRUE;
2150 }
2151 else
2152 {
2153 /* Primary or extended partition */
2154
2155 PartListEntry = List->CurrentPartition->ListEntry.Blink;
2156 if (PartListEntry != &List->CurrentDisk->PrimaryPartListHead)
2157 {
2158 PartEntry = CONTAINING_RECORD(PartListEntry, PARTENTRY, ListEntry);
2159
2160 if (PartEntry->IsPartitioned == TRUE &&
2161 IsContainerPartition(PartEntry->PartitionType))
2162 {
2163 PartListEntry = List->CurrentDisk->LogicalPartListHead.Blink;
2164 PartEntry = CONTAINING_RECORD(PartListEntry, PARTENTRY, ListEntry);
2165 }
2166
2167 List->CurrentPartition = PartEntry;
2168 return TRUE;
2169 }
2170
2171 }
2172 }
2173
2174 /* Search for the last partition entry on the previous disk */
2175 DiskListEntry = List->CurrentDisk->ListEntry.Blink;
2176 while (DiskListEntry != &List->DiskListHead)
2177 {
2178 DiskEntry = CONTAINING_RECORD(DiskListEntry, DISKENTRY, ListEntry);
2179
2180 PartListEntry = DiskEntry->PrimaryPartListHead.Blink;
2181 if (PartListEntry != &DiskEntry->PrimaryPartListHead)
2182 {
2183 PartEntry = CONTAINING_RECORD(PartListEntry, PARTENTRY, ListEntry);
2184
2185 if (PartEntry->IsPartitioned == TRUE &&
2186 IsContainerPartition(PartEntry->PartitionType))
2187 {
2188 PartListEntry = DiskEntry->LogicalPartListHead.Blink;
2189 if (PartListEntry != &DiskEntry->LogicalPartListHead)
2190 {
2191 PartEntry = CONTAINING_RECORD(PartListEntry, PARTENTRY, ListEntry);
2192
2193 List->CurrentDisk = DiskEntry;
2194 List->CurrentPartition = PartEntry;
2195 return TRUE;
2196 }
2197 }
2198 else
2199 {
2200 List->CurrentDisk = DiskEntry;
2201 List->CurrentPartition = PartEntry;
2202 return TRUE;
2203 }
2204 }
2205
2206 DiskListEntry = DiskListEntry->Blink;
2207 }
2208
2209 return FALSE;
2210 }
2211
2212
2213 static
2214 BOOLEAN
2215 IsEmptyLayoutEntry(
2216 PPARTITION_INFORMATION PartitionInfo)
2217 {
2218 if (PartitionInfo->StartingOffset.QuadPart == 0 &&
2219 PartitionInfo->PartitionLength.QuadPart == 0)
2220 // PartitionInfo->PartitionType == 0)
2221 return TRUE;
2222
2223 return FALSE;
2224 }
2225
2226
2227 static
2228 BOOLEAN
2229 IsSamePrimaryLayoutEntry(
2230 IN PPARTITION_INFORMATION PartitionInfo,
2231 IN PDISKENTRY DiskEntry,
2232 IN PPARTENTRY PartEntry)
2233 {
2234 if (PartitionInfo->StartingOffset.QuadPart == PartEntry->StartSector.QuadPart * DiskEntry->BytesPerSector &&
2235 PartitionInfo->PartitionLength.QuadPart == PartEntry->SectorCount.QuadPart * DiskEntry->BytesPerSector)
2236 // PartitionInfo->PartitionNumber = PartEntry->PartitionNumber &&
2237 // PartitionInfo->PartitionType == PartEntry->PartitionType
2238 return TRUE;
2239
2240 return FALSE;
2241 }
2242
2243
2244 static
2245 VOID
2246 UpdateDiskLayout(
2247 IN PDISKENTRY DiskEntry)
2248 {
2249 PPARTITION_INFORMATION PartitionInfo;
2250 PLIST_ENTRY ListEntry;
2251 PPARTENTRY PartEntry;
2252 ULONG Index = 0;
2253 ULONG PartitionNumber = 1;
2254
2255 DPRINT1("UpdateDiskLayout()\n");
2256
2257 ListEntry = DiskEntry->PrimaryPartListHead.Flink;
2258 while (ListEntry != &DiskEntry->PrimaryPartListHead)
2259 {
2260 PartEntry = CONTAINING_RECORD(ListEntry, PARTENTRY, ListEntry);
2261
2262 if (PartEntry->IsPartitioned == TRUE)
2263 {
2264 PartitionInfo = &DiskEntry->LayoutBuffer->PartitionEntry[Index];
2265
2266 if (!IsSamePrimaryLayoutEntry(PartitionInfo, DiskEntry, PartEntry))
2267 {
2268 DPRINT1("Updating partition entry %lu\n", Index);
2269
2270 PartitionInfo->StartingOffset.QuadPart = PartEntry->StartSector.QuadPart * DiskEntry->BytesPerSector;
2271 PartitionInfo->PartitionLength.QuadPart = PartEntry->SectorCount.QuadPart * DiskEntry->BytesPerSector;
2272 PartitionInfo->HiddenSectors = 0;
2273 PartitionInfo->PartitionNumber = (!IsContainerPartition(PartEntry->PartitionType)) ? PartitionNumber : 0;
2274 PartitionInfo->PartitionType = PartEntry->PartitionType;
2275 PartitionInfo->BootIndicator = PartEntry->BootIndicator;
2276 PartitionInfo->RecognizedPartition = FALSE;
2277 PartitionInfo->RewritePartition = TRUE;
2278
2279 PartEntry->PartitionNumber = PartitionNumber;
2280 PartEntry->PartitionIndex = Index;
2281
2282 PartitionNumber++;
2283 }
2284 else if (!IsEmptyLayoutEntry(PartitionInfo))
2285 {
2286 PartitionNumber++;
2287 }
2288
2289 Index++;
2290 }
2291
2292 ListEntry = ListEntry->Flink;
2293 }
2294
2295 for (;Index < 4; Index++)
2296 {
2297 PartitionInfo = &DiskEntry->LayoutBuffer->PartitionEntry[Index];
2298
2299 if (!IsEmptyLayoutEntry(PartitionInfo))
2300 {
2301 DPRINT1("Wiping partition entry %lu\n", Index);
2302
2303 PartitionInfo->StartingOffset.QuadPart = 0;
2304 PartitionInfo->PartitionLength.QuadPart = 0;
2305 PartitionInfo->HiddenSectors = 0;
2306 PartitionInfo->PartitionNumber = 0;
2307 PartitionInfo->PartitionType = 0;
2308 PartitionInfo->BootIndicator = FALSE;
2309 PartitionInfo->RecognizedPartition = FALSE;
2310 PartitionInfo->RewritePartition = TRUE;
2311 }
2312 }
2313
2314 #ifdef DUMP_PARTITION_TABLE
2315 DumpPartitionTable(DiskEntry);
2316 #endif
2317 }
2318
2319
2320 static
2321 PPARTENTRY
2322 GetPrevUnpartitionedEntry(
2323 PDISKENTRY DiskEntry,
2324 PPARTENTRY PartEntry)
2325 {
2326 PPARTENTRY PrevPartEntry;
2327
2328 if (PartEntry->ListEntry.Blink != &DiskEntry->PrimaryPartListHead)
2329 {
2330 PrevPartEntry = CONTAINING_RECORD(PartEntry->ListEntry.Blink,
2331 PARTENTRY,
2332 ListEntry);
2333 if (PrevPartEntry->IsPartitioned == FALSE)
2334 return PrevPartEntry;
2335 }
2336
2337 return NULL;
2338 }
2339
2340
2341 static
2342 PPARTENTRY
2343 GetNextUnpartitionedEntry(
2344 PDISKENTRY DiskEntry,
2345 PPARTENTRY PartEntry)
2346 {
2347 PPARTENTRY NextPartEntry;
2348
2349 if (PartEntry->ListEntry.Flink != &DiskEntry->PrimaryPartListHead)
2350 {
2351 NextPartEntry = CONTAINING_RECORD(PartEntry->ListEntry.Flink,
2352 PARTENTRY,
2353 ListEntry);
2354 if (NextPartEntry->IsPartitioned == FALSE)
2355 return NextPartEntry;
2356 }
2357
2358 return NULL;
2359 }
2360
2361
2362 VOID
2363 CreatePrimaryPartition(
2364 PPARTLIST List,
2365 ULONGLONG SectorCount,
2366 BOOLEAN AutoCreate)
2367 {
2368 PDISKENTRY DiskEntry;
2369 PPARTENTRY PartEntry;
2370 PPARTENTRY NewPartEntry;
2371
2372 DPRINT1("CreatePrimaryPartition(%I64u)\n", SectorCount);
2373
2374 if (List == NULL ||
2375 List->CurrentDisk == NULL ||
2376 List->CurrentPartition == NULL ||
2377 List->CurrentPartition->IsPartitioned == TRUE)
2378 {
2379 return;
2380 }
2381
2382 DiskEntry = List->CurrentDisk;
2383 PartEntry = List->CurrentPartition;
2384
2385 DPRINT1("Current partition sector count: %I64u\n", PartEntry->SectorCount.QuadPart);
2386
2387 if (AutoCreate == TRUE ||
2388 Align(PartEntry->StartSector.QuadPart + SectorCount, DiskEntry->SectorAlignment) - PartEntry->StartSector.QuadPart == PartEntry->SectorCount.QuadPart)
2389 {
2390 DPRINT1("Convert existing partition entry\n");
2391
2392 /* Convert current entry to 'new (unformatted)' */
2393 PartEntry->IsPartitioned = TRUE;
2394 PartEntry->PartitionType = PARTITION_ENTRY_UNUSED;
2395 PartEntry->FormatState = Unformatted;
2396 PartEntry->AutoCreate = AutoCreate;
2397 PartEntry->New = TRUE;
2398 PartEntry->BootIndicator = FALSE;
2399
2400 DPRINT1("First Sector: %I64u\n", PartEntry->StartSector.QuadPart);
2401 DPRINT1("Last Sector: %I64u\n", PartEntry->StartSector.QuadPart + PartEntry->SectorCount.QuadPart - 1);
2402 DPRINT1("Total Sectors: %I64u\n", PartEntry->SectorCount.QuadPart);
2403 }
2404 else
2405 {
2406 DPRINT1("Add new partition entry\n");
2407
2408 /* Insert and initialize a new partition entry */
2409 NewPartEntry = RtlAllocateHeap(ProcessHeap,
2410 HEAP_ZERO_MEMORY,
2411 sizeof(PARTENTRY));
2412 if (NewPartEntry == NULL)
2413 return;
2414
2415 /* Insert the new entry into the list */
2416 InsertTailList(&PartEntry->ListEntry,
2417 &NewPartEntry->ListEntry);
2418
2419 NewPartEntry->DiskEntry = DiskEntry;
2420
2421 NewPartEntry->IsPartitioned = TRUE;
2422 NewPartEntry->StartSector.QuadPart = PartEntry->StartSector.QuadPart;
2423 NewPartEntry->SectorCount.QuadPart = Align(NewPartEntry->StartSector.QuadPart + SectorCount, DiskEntry->SectorAlignment) -
2424 NewPartEntry->StartSector.QuadPart;
2425 NewPartEntry->PartitionType = PARTITION_ENTRY_UNUSED;
2426
2427 DPRINT1("First Sector: %I64u\n", NewPartEntry->StartSector.QuadPart);
2428 DPRINT1("Last Sector: %I64u\n", NewPartEntry->StartSector.QuadPart + NewPartEntry->SectorCount.QuadPart - 1);
2429 DPRINT1("Total Sectors: %I64u\n", NewPartEntry->SectorCount.QuadPart);
2430
2431 NewPartEntry->New = TRUE;
2432 NewPartEntry->FormatState = Unformatted;
2433 NewPartEntry->BootIndicator = FALSE;
2434
2435 PartEntry->StartSector.QuadPart = NewPartEntry->StartSector.QuadPart + NewPartEntry->SectorCount.QuadPart;
2436 PartEntry->SectorCount.QuadPart -= (PartEntry->StartSector.QuadPart - NewPartEntry->StartSector.QuadPart);
2437 }
2438
2439 UpdateDiskLayout(DiskEntry);
2440
2441 DiskEntry->Dirty = TRUE;
2442
2443 UpdatePartitionNumbers(DiskEntry);
2444
2445 AssignDriveLetters(List);
2446 }
2447
2448
2449 static
2450 VOID
2451 AddLogicalDiskSpace(
2452 PDISKENTRY DiskEntry)
2453 {
2454 PPARTENTRY NewPartEntry;
2455
2456 DPRINT1("AddLogicalDiskSpace()\n");
2457
2458 /* Create a partition table entry that represents the empty space in the container partition */
2459 NewPartEntry = RtlAllocateHeap(ProcessHeap,
2460 HEAP_ZERO_MEMORY,
2461 sizeof(PARTENTRY));
2462 if (NewPartEntry == NULL)
2463 return;
2464
2465 NewPartEntry->DiskEntry = DiskEntry;
2466 NewPartEntry->LogicalPartition = TRUE;
2467
2468 NewPartEntry->IsPartitioned = FALSE;
2469 NewPartEntry->StartSector.QuadPart = DiskEntry->ExtendedPartition->StartSector.QuadPart + (ULONGLONG)DiskEntry->SectorAlignment;
2470 NewPartEntry->SectorCount.QuadPart = DiskEntry->ExtendedPartition->SectorCount.QuadPart - (ULONGLONG)DiskEntry->SectorAlignment;
2471
2472 DPRINT1("First Sector: %I64u\n", NewPartEntry->StartSector.QuadPart);
2473 DPRINT1("Last Sector: %I64u\n", NewPartEntry->StartSector.QuadPart + NewPartEntry->SectorCount.QuadPart - 1);
2474 DPRINT1("Total Sectors: %I64u\n", NewPartEntry->SectorCount.QuadPart);
2475
2476 NewPartEntry->FormatState = Unformatted;
2477
2478 InsertTailList(&DiskEntry->LogicalPartListHead,
2479 &NewPartEntry->ListEntry);
2480 }
2481
2482
2483 VOID
2484 CreateExtendedPartition(
2485 PPARTLIST List,
2486 ULONGLONG SectorCount)
2487 {
2488 PDISKENTRY DiskEntry;
2489 PPARTENTRY PartEntry;
2490 PPARTENTRY NewPartEntry;
2491
2492 DPRINT1("CreateExtendedPartition(%I64u)\n", SectorCount);
2493
2494 if (List == NULL ||
2495 List->CurrentDisk == NULL ||
2496 List->CurrentPartition == NULL ||
2497 List->CurrentPartition->IsPartitioned == TRUE)
2498 {
2499 return;
2500 }
2501
2502 DiskEntry = List->CurrentDisk;
2503 PartEntry = List->CurrentPartition;
2504
2505 DPRINT1("Current partition sector count: %I64u\n", PartEntry->SectorCount.QuadPart);
2506
2507 if (Align(PartEntry->StartSector.QuadPart + SectorCount, DiskEntry->SectorAlignment) - PartEntry->StartSector.QuadPart == PartEntry->SectorCount.QuadPart)
2508 {
2509 DPRINT1("Convert existing partition entry\n");
2510
2511 /* Convert current entry to 'new (unformatted)' */
2512 PartEntry->IsPartitioned = TRUE;
2513 PartEntry->FormatState = Formatted;
2514 PartEntry->AutoCreate = FALSE;
2515 PartEntry->New = FALSE;
2516 PartEntry->BootIndicator = FALSE;
2517
2518 if (PartEntry->StartSector.QuadPart < 1450560)
2519 {
2520 /* Partition starts below the 8.4GB boundary ==> CHS partition */
2521 PartEntry->PartitionType = PARTITION_EXTENDED;
2522 }
2523 else
2524 {
2525 /* Partition starts above the 8.4GB boundary ==> LBA partition */
2526 PartEntry->PartitionType = PARTITION_XINT13_EXTENDED;
2527 }
2528
2529 DiskEntry->ExtendedPartition = PartEntry;
2530
2531 DPRINT1("First Sector: %I64u\n", PartEntry->StartSector.QuadPart);
2532 DPRINT1("Last Sector: %I64u\n", PartEntry->StartSector.QuadPart + PartEntry->SectorCount.QuadPart - 1);
2533 DPRINT1("Total Sectors: %I64u\n", PartEntry->SectorCount.QuadPart);
2534 }
2535 else
2536 {
2537 DPRINT1("Add new partition entry\n");
2538
2539 /* Insert and initialize a new partition entry */
2540 NewPartEntry = RtlAllocateHeap(ProcessHeap,
2541 HEAP_ZERO_MEMORY,
2542 sizeof(PARTENTRY));
2543 if (NewPartEntry == NULL)
2544 return;
2545
2546 /* Insert the new entry into the list */
2547 InsertTailList(&PartEntry->ListEntry,
2548 &NewPartEntry->ListEntry);
2549
2550 NewPartEntry->DiskEntry = DiskEntry;
2551
2552 NewPartEntry->IsPartitioned = TRUE;
2553 NewPartEntry->StartSector.QuadPart = PartEntry->StartSector.QuadPart;
2554 NewPartEntry->SectorCount.QuadPart = Align(NewPartEntry->StartSector.QuadPart + SectorCount, DiskEntry->SectorAlignment) -
2555 NewPartEntry->StartSector.QuadPart;
2556
2557 NewPartEntry->New = FALSE;
2558 NewPartEntry->FormatState = Formatted;
2559 NewPartEntry->BootIndicator = FALSE;
2560
2561 if (NewPartEntry->StartSector.QuadPart < 1450560)
2562 {
2563 /* Partition starts below the 8.4GB boundary ==> CHS partition */
2564 NewPartEntry->PartitionType = PARTITION_EXTENDED;
2565 }
2566 else
2567 {
2568 /* Partition starts above the 8.4GB boundary ==> LBA partition */
2569 NewPartEntry->PartitionType = PARTITION_XINT13_EXTENDED;
2570 }
2571
2572 DiskEntry->ExtendedPartition = NewPartEntry;
2573
2574 PartEntry->StartSector.QuadPart = NewPartEntry->StartSector.QuadPart + NewPartEntry->SectorCount.QuadPart;
2575 PartEntry->SectorCount.QuadPart -= (PartEntry->StartSector.QuadPart - NewPartEntry->StartSector.QuadPart);
2576
2577 DPRINT1("First Sector: %I64u\n", NewPartEntry->StartSector.QuadPart);
2578 DPRINT1("Last Sector: %I64u\n", NewPartEntry->StartSector.QuadPart + NewPartEntry->SectorCount.QuadPart - 1);
2579 DPRINT1("Total Sectors: %I64u\n", NewPartEntry->SectorCount.QuadPart);
2580 }
2581
2582 AddLogicalDiskSpace(DiskEntry);
2583
2584 UpdateDiskLayout(DiskEntry);
2585
2586 DiskEntry->Dirty = TRUE;
2587
2588 UpdatePartitionNumbers(DiskEntry);
2589
2590 AssignDriveLetters(List);
2591 }
2592
2593
2594 VOID
2595 CreateLogicalPartition(
2596 PPARTLIST List,
2597 ULONGLONG SectorCount)
2598 {
2599 // PDISKENTRY DiskEntry;
2600 PPARTENTRY PartEntry;
2601 // PPARTENTRY NewPartEntry;
2602
2603 DPRINT1("CreateLogicalPartition(%I64u)\n", SectorCount);
2604
2605 if (List == NULL ||
2606 List->CurrentDisk == NULL ||
2607 List->CurrentPartition == NULL ||
2608 List->CurrentPartition->IsPartitioned == TRUE)
2609 {
2610 return;
2611 }
2612
2613 // DiskEntry = List->CurrentDisk;
2614 PartEntry = List->CurrentPartition;
2615
2616 DPRINT1("Current partition sector count: %I64u\n", PartEntry->SectorCount.QuadPart);
2617 }
2618
2619
2620 VOID
2621 DeleteCurrentPartition(
2622 PPARTLIST List)
2623 {
2624 PDISKENTRY DiskEntry;
2625 PPARTENTRY PartEntry;
2626 PPARTENTRY PrevPartEntry;
2627 PPARTENTRY NextPartEntry;
2628 PPARTENTRY LogicalPartEntry;
2629 PLIST_ENTRY Entry;
2630
2631 if (List == NULL ||
2632 List->CurrentDisk == NULL ||
2633 List->CurrentPartition == NULL ||
2634 List->CurrentPartition->IsPartitioned == FALSE)
2635 {
2636 return;
2637 }
2638
2639 DiskEntry = List->CurrentDisk;
2640 PartEntry = List->CurrentPartition;
2641
2642 /* Delete all logical partiton entries if an extended partition will be deleted */
2643 if (DiskEntry->ExtendedPartition == PartEntry)
2644 {
2645 while (!IsListEmpty(&DiskEntry->LogicalPartListHead))
2646 {
2647 Entry = RemoveHeadList(&DiskEntry->LogicalPartListHead);
2648 LogicalPartEntry = CONTAINING_RECORD(Entry, PARTENTRY, ListEntry);
2649
2650 RtlFreeHeap(ProcessHeap, 0, LogicalPartEntry);
2651 }
2652
2653 DiskEntry->ExtendedPartition = NULL;
2654 }
2655
2656 /* Adjust unpartitioned disk space entries */
2657
2658 /* Get pointer to previous and next unpartitioned entries */
2659 PrevPartEntry = GetPrevUnpartitionedEntry(DiskEntry,
2660 PartEntry);
2661
2662 NextPartEntry = GetNextUnpartitionedEntry(DiskEntry,
2663 PartEntry);
2664
2665 if (PrevPartEntry != NULL && NextPartEntry != NULL)
2666 {
2667 /* Merge previous, current and next unpartitioned entry */
2668
2669 /* Adjust the previous entries length */
2670 PrevPartEntry->SectorCount.QuadPart += (PartEntry->SectorCount.QuadPart + NextPartEntry->SectorCount.QuadPart);
2671
2672 /* Remove the current entry */
2673 RemoveEntryList(&PartEntry->ListEntry);
2674 RtlFreeHeap(ProcessHeap, 0, PartEntry);
2675
2676 /* Remove the next entry */
2677 RemoveEntryList (&NextPartEntry->ListEntry);
2678 RtlFreeHeap(ProcessHeap, 0, NextPartEntry);
2679
2680 /* Update current partition */
2681 List->CurrentPartition = PrevPartEntry;
2682 }
2683 else if (PrevPartEntry != NULL && NextPartEntry == NULL)
2684 {
2685 /* Merge current and previous unpartitioned entry */
2686
2687 /* Adjust the previous entries length */
2688 PrevPartEntry->SectorCount.QuadPart += PartEntry->SectorCount.QuadPart;
2689
2690 /* Remove the current entry */
2691 RemoveEntryList(&PartEntry->ListEntry);
2692 RtlFreeHeap(ProcessHeap, 0, PartEntry);
2693
2694 /* Update current partition */
2695 List->CurrentPartition = PrevPartEntry;
2696 }
2697 else if (PrevPartEntry == NULL && NextPartEntry != NULL)
2698 {
2699 /* Merge current and next unpartitioned entry */
2700
2701 /* Adjust the next entries offset and length */
2702 NextPartEntry->StartSector.QuadPart = PartEntry->StartSector.QuadPart;
2703 NextPartEntry->SectorCount.QuadPart += PartEntry->SectorCount.QuadPart;
2704
2705 /* Remove the current entry */
2706 RemoveEntryList(&PartEntry->ListEntry);
2707 RtlFreeHeap(ProcessHeap, 0, PartEntry);
2708
2709 /* Update current partition */
2710 List->CurrentPartition = NextPartEntry;
2711 }
2712 else
2713 {
2714 /* Nothing to merge but change current entry */
2715 PartEntry->IsPartitioned = FALSE;
2716 PartEntry->PartitionType = PARTITION_ENTRY_UNUSED;
2717 PartEntry->FormatState = Unformatted;
2718 PartEntry->DriveLetter = 0;
2719 }
2720
2721 UpdateDiskLayout(DiskEntry);
2722
2723 DiskEntry->Dirty = TRUE;
2724
2725 UpdatePartitionNumbers(DiskEntry);
2726
2727 AssignDriveLetters(List);
2728 }
2729
2730
2731 VOID
2732 CheckActiveBootPartition(
2733 PPARTLIST List)
2734 {
2735 PDISKENTRY DiskEntry;
2736 PPARTENTRY PartEntry;
2737 PLIST_ENTRY ListEntry;
2738
2739 /* Check for empty disk list */
2740 if (IsListEmpty (&List->DiskListHead))
2741 {
2742 List->ActiveBootDisk = NULL;
2743 List->ActiveBootPartition = NULL;
2744 return;
2745 }
2746
2747 #if 0
2748 if (List->ActiveBootDisk != NULL &&
2749 List->ActiveBootPartition != NULL)
2750 {
2751 /* We already have an active boot partition */
2752 return;
2753 }
2754 #endif
2755
2756 /* Choose the currently selected disk */
2757 DiskEntry = List->CurrentDisk;
2758
2759 /* Check for empty partition list */
2760 if (IsListEmpty (&DiskEntry->PrimaryPartListHead))
2761 {
2762 List->ActiveBootDisk = NULL;
2763 List->ActiveBootPartition = NULL;
2764 return;
2765 }
2766
2767 PartEntry = CONTAINING_RECORD(DiskEntry->PrimaryPartListHead.Flink,
2768 PARTENTRY,
2769 ListEntry);
2770
2771 /* Set active boot partition */
2772 if ((DiskEntry->NewDisk == TRUE) ||
2773 (PartEntry->BootIndicator == FALSE))
2774 {
2775 PartEntry->BootIndicator = TRUE;
2776 DiskEntry->LayoutBuffer->PartitionEntry[PartEntry->PartitionIndex].BootIndicator = TRUE;
2777 DiskEntry->LayoutBuffer->PartitionEntry[PartEntry->PartitionIndex].RewritePartition = TRUE;
2778 DiskEntry->Dirty = TRUE;
2779
2780 /* FIXME: Might be incorrect if partitions were created by Linux FDISK */
2781 List->ActiveBootDisk = DiskEntry;
2782 List->ActiveBootPartition = PartEntry;
2783
2784 return;
2785 }
2786
2787 /* Disk is not new, scan all partitions to find a bootable one */
2788 List->ActiveBootDisk = NULL;
2789 List->ActiveBootPartition = NULL;
2790
2791 ListEntry = DiskEntry->PrimaryPartListHead.Flink;
2792 while (ListEntry != &DiskEntry->PrimaryPartListHead)
2793 {
2794 PartEntry = CONTAINING_RECORD(ListEntry,
2795 PARTENTRY,
2796 ListEntry);
2797
2798 /* Check if it is partitioned */
2799 if (PartEntry->IsPartitioned)
2800 {
2801 if (PartEntry->PartitionType != PARTITION_ENTRY_UNUSED &&
2802 PartEntry->BootIndicator)
2803 {
2804 /* Yes, we found it */
2805 List->ActiveBootDisk = DiskEntry;
2806 List->ActiveBootPartition = PartEntry;
2807
2808 DPRINT("Found bootable partition disk %d, drive letter %c\n",
2809 DiskEntry->DiskNumber, PartEntry->DriveLetter);
2810 break;
2811 }
2812 }
2813
2814 /* Go to the next one */
2815 ListEntry = ListEntry->Flink;
2816 }
2817 }
2818
2819
2820 BOOLEAN
2821 CheckForLinuxFdiskPartitions(
2822 PPARTLIST List)
2823 {
2824 #if 0
2825 PDISKENTRY DiskEntry;
2826 PPARTENTRY PartEntry;
2827 PLIST_ENTRY Entry1;
2828 PLIST_ENTRY Entry2;
2829 ULONG PartitionCount;
2830 ULONG i;
2831
2832 Entry1 = List->DiskListHead.Flink;
2833 while (Entry1 != &List->DiskListHead)
2834 {
2835 DiskEntry = CONTAINING_RECORD(Entry1,
2836 DISKENTRY,
2837 ListEntry);
2838
2839 Entry2 = DiskEntry->PartListHead.Flink;
2840 while (Entry2 != &DiskEntry->PartListHead)
2841 {
2842 PartEntry = CONTAINING_RECORD(Entry2,
2843 PARTENTRY,
2844 ListEntry);
2845
2846 if (PartEntry->Unpartitioned == FALSE)
2847 {
2848 PartitionCount = 0;
2849
2850 for (i = 0; i < 4; i++)
2851 {
2852 if (!IsContainerPartition(PartEntry->PartInfo[i].PartitionType) &&
2853 PartEntry->PartInfo[i].PartitionLength.QuadPart != 0ULL)
2854 {
2855 PartitionCount++;
2856 }
2857 }
2858
2859 if (PartitionCount > 1)
2860 {
2861 return TRUE;
2862 }
2863 }
2864
2865 Entry2 = Entry2->Flink;
2866 }
2867
2868 Entry1 = Entry1->Flink;
2869 }
2870 #endif
2871
2872 return FALSE;
2873 }
2874
2875
2876 static
2877 NTSTATUS
2878 WritePartitons(
2879 IN PPARTLIST List,
2880 IN PDISKENTRY DiskEntry)
2881 {
2882 WCHAR DstPath[MAX_PATH];
2883 OBJECT_ATTRIBUTES ObjectAttributes;
2884 IO_STATUS_BLOCK Iosb;
2885 UNICODE_STRING Name;
2886 ULONG BufferSize;
2887 HANDLE FileHandle = NULL;
2888 NTSTATUS Status;
2889
2890 DPRINT("WritePartitions() Disk: %lu\n", DiskEntry->DiskNumber);
2891
2892 swprintf(DstPath,
2893 L"\\Device\\Harddisk%d\\Partition0",
2894 DiskEntry->DiskNumber);
2895 RtlInitUnicodeString(&Name,
2896 DstPath);
2897 InitializeObjectAttributes(&ObjectAttributes,
2898 &Name,
2899 0,
2900 NULL,
2901 NULL);
2902
2903 Status = NtOpenFile(&FileHandle,
2904 GENERIC_READ | GENERIC_WRITE | SYNCHRONIZE,
2905 &ObjectAttributes,
2906 &Iosb,
2907 0,
2908 FILE_SYNCHRONOUS_IO_NONALERT);
2909 if (!NT_SUCCESS(Status))
2910 {
2911 DPRINT1("NtOpenFile() failed (Status %lx)\n", Status);
2912 return Status;
2913 }
2914
2915 #ifdef DUMP_PARTITION_TABLE
2916 DumpPartitionTable(DiskEntry);
2917 #endif
2918
2919 BufferSize = sizeof(DRIVE_LAYOUT_INFORMATION) +
2920 ((DiskEntry->LayoutBuffer->PartitionCount - 1) * sizeof(PARTITION_INFORMATION));
2921 Status = NtDeviceIoControlFile(FileHandle,
2922 NULL,
2923 NULL,
2924 NULL,
2925 &Iosb,
2926 IOCTL_DISK_SET_DRIVE_LAYOUT,
2927 DiskEntry->LayoutBuffer,
2928 BufferSize,
2929 NULL,
2930 0);
2931 if (!NT_SUCCESS(Status))
2932 {
2933 DPRINT1("IOCTL_DISK_SET_DRIVE_LAYOUT failed (Status 0x%08lx)\n", Status);
2934 }
2935
2936 if (FileHandle != NULL)
2937 NtClose(FileHandle);
2938
2939 return Status;
2940 }
2941
2942
2943 BOOLEAN
2944 WritePartitionsToDisk(
2945 PPARTLIST List)
2946 {
2947 PLIST_ENTRY Entry;
2948 PDISKENTRY DiskEntry;
2949
2950 if (List == NULL)
2951 return TRUE;
2952
2953 Entry = List->DiskListHead.Flink;
2954 while (Entry != &List->DiskListHead)
2955 {
2956 DiskEntry = CONTAINING_RECORD(Entry, DISKENTRY, ListEntry);
2957
2958 if (DiskEntry->Dirty == TRUE)
2959 {
2960 WritePartitons(List, DiskEntry);
2961 }
2962
2963 Entry = Entry->Flink;
2964 }
2965
2966 return TRUE;
2967 }
2968
2969
2970 BOOL
2971 SetMountedDeviceValues(
2972 PPARTLIST List)
2973 {
2974 PLIST_ENTRY Entry1, Entry2;
2975 PDISKENTRY DiskEntry;
2976 PPARTENTRY PartEntry;
2977 LARGE_INTEGER StartingOffset;
2978
2979 if (List == NULL)
2980 {
2981 return FALSE;
2982 }
2983
2984 Entry1 = List->DiskListHead.Flink;
2985 while (Entry1 != &List->DiskListHead)
2986 {
2987 DiskEntry = CONTAINING_RECORD(Entry1,
2988 DISKENTRY,
2989 ListEntry);
2990
2991 Entry2 = DiskEntry->PrimaryPartListHead.Flink;
2992 while (Entry2 != &DiskEntry->PrimaryPartListHead)
2993 {
2994 PartEntry = CONTAINING_RECORD(Entry2, PARTENTRY, ListEntry);
2995 if (PartEntry->IsPartitioned)
2996 {
2997 if (PartEntry->DriveLetter)
2998 {
2999 StartingOffset.QuadPart = PartEntry->StartSector.QuadPart * DiskEntry->BytesPerSector;
3000 if (!SetMountedDeviceValue(PartEntry->DriveLetter,
3001 DiskEntry->LayoutBuffer->Signature,
3002 StartingOffset))
3003 {
3004 return FALSE;
3005 }
3006 }
3007 }
3008
3009 Entry2 = Entry2->Flink;
3010 }
3011
3012 Entry1 = Entry1->Flink;
3013 }
3014
3015 return TRUE;
3016 }
3017
3018
3019 static
3020 ULONG
3021 GetPrimaryPartitionCount(
3022 IN PDISKENTRY DiskEntry)
3023 {
3024 PLIST_ENTRY Entry;
3025 PPARTENTRY PartEntry;
3026 UINT nCount = 0;
3027
3028 Entry = DiskEntry->PrimaryPartListHead.Flink;
3029 while (Entry != &DiskEntry->PrimaryPartListHead)
3030 {
3031 PartEntry = CONTAINING_RECORD(Entry, PARTENTRY, ListEntry);
3032 if (PartEntry->IsPartitioned == TRUE)
3033 nCount++;
3034
3035 Entry = Entry->Flink;
3036 }
3037
3038 return nCount;
3039 }
3040
3041
3042 ULONG
3043 PrimaryPartitionCreationChecks(
3044 IN PPARTLIST List)
3045 {
3046 PDISKENTRY DiskEntry;
3047 PPARTENTRY PartEntry;
3048
3049 DiskEntry = List->CurrentDisk;
3050 PartEntry = List->CurrentPartition;
3051
3052 /* Fail if partition is already in use */
3053 if (PartEntry->IsPartitioned == TRUE)
3054 return ERROR_NEW_PARTITION;
3055
3056 /* Fail if there are more than 4 partitions in the list */
3057 if (GetPrimaryPartitionCount(DiskEntry) > 4)
3058 return ERROR_PARTITION_TABLE_FULL;
3059
3060 return ERROR_SUCCESS;
3061 }
3062
3063
3064 ULONG
3065 ExtendedPartitionCreationChecks(
3066 IN PPARTLIST List)
3067 {
3068 PDISKENTRY DiskEntry;
3069 PPARTENTRY PartEntry;
3070
3071 DiskEntry = List->CurrentDisk;
3072 PartEntry = List->CurrentPartition;
3073
3074 /* Fail if partition is already in use */
3075 if (PartEntry->IsPartitioned == TRUE)
3076 return ERROR_NEW_PARTITION;
3077
3078 /* Fail if there are more than 4 partitions in the list */
3079 if (GetPrimaryPartitionCount(DiskEntry) > 4)
3080 return ERROR_PARTITION_TABLE_FULL;
3081
3082 /* Fail if there is another extended partition in the list */
3083 if (DiskEntry->ExtendedPartition != NULL)
3084 return ERROR_ONLY_ONE_EXTENDED;
3085
3086 return ERROR_SUCCESS;
3087 }
3088
3089
3090 ULONG
3091 LogicalPartitionCreationChecks(
3092 IN PPARTLIST List)
3093 {
3094 // PDISKENTRY DiskEntry;
3095 PPARTENTRY PartEntry;
3096
3097 // DiskEntry = List->CurrentDisk;
3098 PartEntry = List->CurrentPartition;
3099
3100 /* Fail if partition is already in use */
3101 if (PartEntry->IsPartitioned == TRUE)
3102 return ERROR_NEW_PARTITION;
3103
3104 return ERROR_SUCCESS;
3105 }
3106
3107 /* EOF */