[FS_REC]
[reactos.git] / reactos / base / setup / usetup / bootsup.c
1 /*
2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS text-mode setup
4 * FILE: base/setup/usetup/bootsup.c
5 * PURPOSE: Bootloader support functions
6 * PROGRAMMER: Eric Kohl
7 */
8
9 #include "usetup.h"
10
11 #define NDEBUG
12 #include <debug.h>
13
14 #define SECTORSIZE 512
15
16 #include <pshpack1.h>
17 typedef struct _FAT_BOOTSECTOR
18 {
19 UCHAR JumpBoot[3]; // Jump instruction to boot code
20 CHAR OemName[8]; // "MSWIN4.1" for MS formatted volumes
21 USHORT BytesPerSector; // Bytes per sector
22 UCHAR SectorsPerCluster; // Number of sectors in a cluster
23 USHORT ReservedSectors; // Reserved sectors, usually 1 (the bootsector)
24 UCHAR NumberOfFats; // Number of FAT tables
25 USHORT RootDirEntries; // Number of root directory entries (fat12/16)
26 USHORT TotalSectors; // Number of total sectors on the drive, 16-bit
27 UCHAR MediaDescriptor; // Media descriptor byte
28 USHORT SectorsPerFat; // Sectors per FAT table (fat12/16)
29 USHORT SectorsPerTrack; // Number of sectors in a track
30 USHORT NumberOfHeads; // Number of heads on the disk
31 ULONG HiddenSectors; // Hidden sectors (sectors before the partition start like the partition table)
32 ULONG TotalSectorsBig; // This field is the new 32-bit total count of sectors on the volume
33 UCHAR DriveNumber; // Int 0x13 drive number (e.g. 0x80)
34 UCHAR Reserved1; // Reserved (used by Windows NT). Code that formats FAT volumes should always set this byte to 0.
35 UCHAR BootSignature; // Extended boot signature (0x29). This is a signature byte that indicates that the following three fields in the boot sector are present.
36 ULONG VolumeSerialNumber; // Volume serial number
37 CHAR VolumeLabel[11]; // Volume label. This field matches the 11-byte volume label recorded in the root directory
38 CHAR FileSystemType[8]; // One of the strings "FAT12 ", "FAT16 ", or "FAT "
39
40 UCHAR BootCodeAndData[448]; // The remainder of the boot sector
41
42 USHORT BootSectorMagic; // 0xAA55
43
44 } FAT_BOOTSECTOR, *PFAT_BOOTSECTOR;
45
46 typedef struct _FAT32_BOOTSECTOR
47 {
48 UCHAR JumpBoot[3]; // Jump instruction to boot code
49 CHAR OemName[8]; // "MSWIN4.1" for MS formatted volumes
50 USHORT BytesPerSector; // Bytes per sector
51 UCHAR SectorsPerCluster; // Number of sectors in a cluster
52 USHORT ReservedSectors; // Reserved sectors, usually 1 (the bootsector)
53 UCHAR NumberOfFats; // Number of FAT tables
54 USHORT RootDirEntries; // Number of root directory entries (fat12/16)
55 USHORT TotalSectors; // Number of total sectors on the drive, 16-bit
56 UCHAR MediaDescriptor; // Media descriptor byte
57 USHORT SectorsPerFat; // Sectors per FAT table (fat12/16)
58 USHORT SectorsPerTrack; // Number of sectors in a track
59 USHORT NumberOfHeads; // Number of heads on the disk
60 ULONG HiddenSectors; // Hidden sectors (sectors before the partition start like the partition table)
61 ULONG TotalSectorsBig; // This field is the new 32-bit total count of sectors on the volume
62 ULONG SectorsPerFatBig; // This field is the FAT32 32-bit count of sectors occupied by ONE FAT. BPB_FATSz16 must be 0
63 USHORT ExtendedFlags; // Extended flags (fat32)
64 USHORT FileSystemVersion; // File system version (fat32)
65 ULONG RootDirStartCluster; // Starting cluster of the root directory (fat32)
66 USHORT FsInfo; // Sector number of FSINFO structure in the reserved area of the FAT32 volume. Usually 1.
67 USHORT BackupBootSector; // If non-zero, indicates the sector number in the reserved area of the volume of a copy of the boot record. Usually 6.
68 UCHAR Reserved[12]; // Reserved for future expansion
69 UCHAR DriveNumber; // Int 0x13 drive number (e.g. 0x80)
70 UCHAR Reserved1; // Reserved (used by Windows NT). Code that formats FAT volumes should always set this byte to 0.
71 UCHAR BootSignature; // Extended boot signature (0x29). This is a signature byte that indicates that the following three fields in the boot sector are present.
72 ULONG VolumeSerialNumber; // Volume serial number
73 CHAR VolumeLabel[11]; // Volume label. This field matches the 11-byte volume label recorded in the root directory
74 CHAR FileSystemType[8]; // Always set to the string "FAT32 "
75
76 UCHAR BootCodeAndData[420]; // The remainder of the boot sector
77
78 USHORT BootSectorMagic; // 0xAA55
79
80 } FAT32_BOOTSECTOR, *PFAT32_BOOTSECTOR;
81
82 typedef struct _EXT2_BOOTSECTOR
83 {
84 // The EXT2 bootsector is completely user-specific.
85 // No FS data is stored there.
86 UCHAR Fill[1024];
87 } EXT2_BOOTSECTOR, *PEXT2_BOOTSECTOR;
88
89 // TODO: Add more bootsector structures!
90
91 #include <poppack.h>
92
93 extern PPARTLIST PartitionList;
94
95 /* FUNCTIONS ****************************************************************/
96
97
98 static
99 VOID
100 CreateCommonFreeLoaderSections(
101 PINICACHE IniCache)
102 {
103 PINICACHESECTION IniSection;
104
105 /* Create "FREELOADER" section */
106 IniSection = IniCacheAppendSection(IniCache, L"FREELOADER");
107
108 #if DBG
109 if (IsUnattendedSetup)
110 {
111 /* DefaultOS=ReactOS */
112 IniCacheInsertKey(IniSection,
113 NULL,
114 INSERT_LAST,
115 L"DefaultOS",
116 #ifndef _WINKD_
117 L"ReactOS_KdSerial");
118 #else
119 L"ReactOS_Debug");
120 #endif
121 }
122 else
123 #endif
124 {
125 /* DefaultOS=ReactOS */
126 IniCacheInsertKey(IniSection,
127 NULL,
128 INSERT_LAST,
129 L"DefaultOS",
130 L"ReactOS");
131 }
132
133 #if DBG
134 if (IsUnattendedSetup)
135 #endif
136 {
137 /* Timeout=0 for unattended or non debug*/
138 IniCacheInsertKey(IniSection,
139 NULL,
140 INSERT_LAST,
141 L"TimeOut",
142 L"0");
143 }
144 #if DBG
145 else
146 {
147 /* Timeout=0 or 10 */
148 IniCacheInsertKey(IniSection,
149 NULL,
150 INSERT_LAST,
151 L"TimeOut",
152 L"10");
153 }
154 #endif
155
156 /* Create "Display" section */
157 IniSection = IniCacheAppendSection(IniCache, L"Display");
158
159 /* TitleText=ReactOS Boot Manager */
160 IniCacheInsertKey(IniSection,
161 NULL,
162 INSERT_LAST,
163 L"TitleText",
164 L"ReactOS Boot Manager");
165
166 /* StatusBarColor=Cyan */
167 IniCacheInsertKey(IniSection,
168 NULL,
169 INSERT_LAST,
170 L"StatusBarColor",
171 L"Cyan");
172
173 /* StatusBarTextColor=Black */
174 IniCacheInsertKey(IniSection,
175 NULL,
176 INSERT_LAST,
177 L"StatusBarTextColor",
178 L"Black");
179
180 /* BackdropTextColor=White */
181 IniCacheInsertKey(IniSection,
182 NULL,
183 INSERT_LAST,
184 L"BackdropTextColor",
185 L"White");
186
187 /* BackdropColor=Blue */
188 IniCacheInsertKey(IniSection,
189 NULL,
190 INSERT_LAST,
191 L"BackdropColor",
192 L"Blue");
193
194 /* BackdropFillStyle=Medium */
195 IniCacheInsertKey(IniSection,
196 NULL,
197 INSERT_LAST,
198 L"BackdropFillStyle",
199 L"Medium");
200
201 /* TitleBoxTextColor=White */
202 IniCacheInsertKey(IniSection,
203 NULL,
204 INSERT_LAST,
205 L"TitleBoxTextColor",
206 L"White");
207
208 /* TitleBoxColor=Red */
209 IniCacheInsertKey(IniSection,
210 NULL,
211 INSERT_LAST,
212 L"TitleBoxColor",
213 L"Red");
214
215 /* MessageBoxTextColor=White */
216 IniCacheInsertKey(IniSection,
217 NULL,
218 INSERT_LAST,
219 L"MessageBoxTextColor",
220 L"White");
221
222 /* MessageBoxColor=Blue */
223 IniCacheInsertKey(IniSection,
224 NULL,
225 INSERT_LAST,
226 L"MessageBoxColor",
227 L"Blue");
228
229 /* MenuTextColor=White */
230 IniCacheInsertKey(IniSection,
231 NULL,
232 INSERT_LAST,
233 L"MenuTextColor",
234 L"Gray");
235
236 /* MenuColor=Blue */
237 IniCacheInsertKey(IniSection,
238 NULL,
239 INSERT_LAST,
240 L"MenuColor",
241 L"Black");
242
243 /* TextColor=Yellow */
244 IniCacheInsertKey(IniSection,
245 NULL,
246 INSERT_LAST,
247 L"TextColor",
248 L"Gray");
249
250 /* SelectedTextColor=Black */
251 IniCacheInsertKey(IniSection,
252 NULL,
253 INSERT_LAST,
254 L"SelectedTextColor",
255 L"Black");
256
257 /* SelectedColor=Gray */
258 IniCacheInsertKey(IniSection,
259 NULL,
260 INSERT_LAST,
261 L"SelectedColor",
262 L"Gray");
263
264 /* SelectedColor=Gray */
265 IniCacheInsertKey(IniSection,
266 NULL,
267 INSERT_LAST,
268 L"ShowTime",
269 L"No");
270
271 /* SelectedColor=Gray */
272 IniCacheInsertKey(IniSection,
273 NULL,
274 INSERT_LAST,
275 L"MenuBox",
276 L"No");
277
278 /* SelectedColor=Gray */
279 IniCacheInsertKey(IniSection,
280 NULL,
281 INSERT_LAST,
282 L"CenterMenu",
283 L"No");
284
285 /* SelectedColor=Gray */
286 IniCacheInsertKey(IniSection,
287 NULL,
288 INSERT_LAST,
289 L"MinimalUI",
290 L"Yes");
291
292 /* SelectedColor=Gray */
293 IniCacheInsertKey(IniSection,
294 NULL,
295 INSERT_LAST,
296 L"TimeText",
297 L"Seconds until highlighted choice will be started automatically: ");
298 }
299
300 static
301 NTSTATUS
302 CreateNTOSEntry(
303 PINICACHE IniCache,
304 PINICACHESECTION OSSection,
305 PWCHAR Section,
306 PWCHAR Description,
307 PWCHAR BootType,
308 PWCHAR ArcPath,
309 PWCHAR Options)
310 {
311 PINICACHESECTION IniSection;
312
313 /* Insert entry into "Operating Systems" section */
314 IniCacheInsertKey(OSSection,
315 NULL,
316 INSERT_LAST,
317 Section,
318 Description);
319
320 /* Create new section */
321 IniSection = IniCacheAppendSection(IniCache, Section);
322
323 /* BootType= */
324 IniCacheInsertKey(IniSection,
325 NULL,
326 INSERT_LAST,
327 L"BootType",
328 BootType);
329
330 /* SystemPath= */
331 IniCacheInsertKey(IniSection,
332 NULL,
333 INSERT_LAST,
334 L"SystemPath",
335 ArcPath);
336
337 /* Options= */
338 IniCacheInsertKey(IniSection,
339 NULL,
340 INSERT_LAST,
341 L"Options",
342 Options);
343
344 return STATUS_SUCCESS;
345 }
346
347 static
348 VOID
349 CreateFreeLoaderReactOSEntries(
350 PINICACHE IniCache,
351 PWCHAR ArcPath)
352 {
353 PINICACHESECTION IniSection;
354
355 /* Create "Operating Systems" section */
356 IniSection = IniCacheAppendSection(IniCache, L"Operating Systems");
357
358 /* ReactOS */
359 CreateNTOSEntry(IniCache, IniSection,
360 L"ReactOS", L"\"ReactOS\"",
361 L"Windows2003", ArcPath,
362 L"");
363
364 /* ReactOS_Debug */
365 CreateNTOSEntry(IniCache, IniSection,
366 L"ReactOS_Debug", L"\"ReactOS (Debug)\"",
367 L"Windows2003", ArcPath,
368 L"/DEBUG /DEBUGPORT=COM1 /BAUDRATE=115200 /SOS");
369 #ifdef _WINKD_
370 /* ReactOS_VBoxDebug */
371 CreateNTOSEntry(IniCache, IniSection,
372 L"ReactOS_VBoxDebug", L"\"ReactOS (VBoxDebug)\"",
373 L"Windows2003", ArcPath,
374 L"/DEBUG /DEBUGPORT=VBOX /SOS");
375 #endif
376 #if DBG
377 #ifndef _WINKD_
378 /* ReactOS_KdSerial */
379 CreateNTOSEntry(IniCache, IniSection,
380 L"ReactOS_KdSerial", L"\"ReactOS (RosDbg)\"",
381 L"Windows2003", ArcPath,
382 L"/DEBUG /DEBUGPORT=COM1 /BAUDRATE=115200 /SOS /KDSERIAL");
383 #endif
384
385 /* ReactOS_Screen */
386 CreateNTOSEntry(IniCache, IniSection,
387 L"ReactOS_Screen", L"\"ReactOS (Screen)\"",
388 L"Windows2003", ArcPath,
389 L"/DEBUG /DEBUGPORT=SCREEN /SOS");
390
391 /* ReactOS_LogFile */
392 CreateNTOSEntry(IniCache, IniSection,
393 L"ReactOS_LogFile", L"\"ReactOS (Log file)\"",
394 L"Windows2003", ArcPath,
395 L"/DEBUG /DEBUGPORT=FILE /SOS");
396
397 /* ReactOS_Ram */
398 CreateNTOSEntry(IniCache, IniSection,
399 L"ReactOS_Ram", L"\"ReactOS (RAM Disk)\"",
400 L"Windows2003", L"ramdisk(0)\\ReactOS",
401 L"/DEBUG /DEBUGPORT=COM1 /BAUDRATE=115200 /SOS /RDPATH=reactos.img /RDIMAGEOFFSET=32256");
402
403 /* ReactOS_EMS */
404 CreateNTOSEntry(IniCache, IniSection,
405 L"ReactOS_EMS", L"\"ReactOS (Emergency Management Services)\"",
406 L"Windows2003", ArcPath,
407 L"/DEBUG /DEBUGPORT=COM1 /BAUDRATE=115200 /SOS /redirect=com2 /redirectbaudrate=115200");
408 #endif
409 }
410
411 static
412 NTSTATUS
413 CreateFreeLoaderIniForReactOS(
414 PWCHAR IniPath,
415 PWCHAR ArcPath)
416 {
417 PINICACHE IniCache;
418
419 /* Initialize the INI file */
420 IniCache = IniCacheCreate();
421
422 /* Create the common FreeLdr sections */
423 CreateCommonFreeLoaderSections(IniCache);
424
425 /* Add the ReactOS entries */
426 CreateFreeLoaderReactOSEntries(IniCache, ArcPath);
427
428 /* Save the INI file */
429 IniCacheSave(IniCache, IniPath);
430 IniCacheDestroy(IniCache);
431
432 return STATUS_SUCCESS;
433 }
434
435 static
436 NTSTATUS
437 CreateFreeLoaderIniForReactOSAndBootSector(
438 PWCHAR IniPath,
439 PWCHAR ArcPath,
440 PWCHAR Section,
441 PWCHAR Description,
442 PWCHAR BootDrive,
443 PWCHAR BootPartition,
444 PWCHAR BootSector)
445 {
446 PINICACHE IniCache;
447 PINICACHESECTION IniSection;
448
449 /* Initialize the INI file */
450 IniCache = IniCacheCreate();
451
452 /* Create the common FreeLdr sections */
453 CreateCommonFreeLoaderSections(IniCache);
454
455 /* Add the ReactOS entries */
456 CreateFreeLoaderReactOSEntries(IniCache, ArcPath);
457
458 /* Get "Operating Systems" section */
459 IniSection = IniCacheGetSection(IniCache, L"Operating Systems");
460
461 /* Insert entry into "Operating Systems" section */
462 IniCacheInsertKey(IniSection,
463 NULL,
464 INSERT_LAST,
465 Section,
466 Description);
467
468 /* Create new section */
469 IniSection = IniCacheAppendSection(IniCache, Section);
470
471 /* BootType=BootSector */
472 IniCacheInsertKey(IniSection,
473 NULL,
474 INSERT_LAST,
475 L"BootType",
476 L"BootSector");
477
478 /* BootDrive= */
479 IniCacheInsertKey(IniSection,
480 NULL,
481 INSERT_LAST,
482 L"BootDrive",
483 BootDrive);
484
485 /* BootPartition= */
486 IniCacheInsertKey(IniSection,
487 NULL,
488 INSERT_LAST,
489 L"BootPartition",
490 BootPartition);
491
492 /* BootSector= */
493 IniCacheInsertKey(IniSection,
494 NULL,
495 INSERT_LAST,
496 L"BootSectorFile",
497 BootSector);
498
499 /* Save the INI file */
500 IniCacheSave(IniCache, IniPath);
501 IniCacheDestroy(IniCache);
502
503 return STATUS_SUCCESS;
504 }
505
506 static
507 NTSTATUS
508 UpdateFreeLoaderIni(
509 PWCHAR IniPath,
510 PWCHAR ArcPath)
511 {
512 NTSTATUS Status;
513 PINICACHE IniCache;
514 PINICACHESECTION IniSection;
515 PINICACHESECTION OsIniSection;
516 WCHAR SectionName[80];
517 WCHAR OsName[80];
518 WCHAR SystemPath[200];
519 WCHAR SectionName2[200];
520 PWCHAR KeyData;
521 ULONG i,j;
522
523 Status = IniCacheLoad(&IniCache, IniPath, FALSE);
524 if (!NT_SUCCESS(Status))
525 return Status;
526
527 /* Get "Operating Systems" section */
528 IniSection = IniCacheGetSection(IniCache, L"Operating Systems");
529 if (IniSection == NULL)
530 {
531 IniCacheDestroy(IniCache);
532 return STATUS_UNSUCCESSFUL;
533 }
534
535 /* Find an existing usable or an unused section name */
536 i = 1;
537 wcscpy(SectionName, L"ReactOS");
538 wcscpy(OsName, L"\"ReactOS\"");
539 while(TRUE)
540 {
541 Status = IniCacheGetKey(IniSection, SectionName, &KeyData);
542 if (!NT_SUCCESS(Status))
543 break;
544
545 /* Get operation system section */
546 if (KeyData[0] == '"')
547 {
548 wcscpy(SectionName2, &KeyData[1]);
549 j = wcslen(SectionName2);
550 if (j > 0)
551 {
552 SectionName2[j-1] = 0;
553 }
554 }
555 else
556 {
557 wcscpy(SectionName2, KeyData);
558 }
559
560 /* Search for an existing ReactOS entry */
561 OsIniSection = IniCacheGetSection(IniCache, SectionName2);
562 if (OsIniSection != NULL)
563 {
564 BOOLEAN UseExistingEntry = TRUE;
565
566 /* Check for boot type "Windows2003" */
567 Status = IniCacheGetKey(OsIniSection, L"BootType", &KeyData);
568 if (NT_SUCCESS(Status))
569 {
570 if ((KeyData == NULL) ||
571 ( (_wcsicmp(KeyData, L"Windows2003") != 0) &&
572 (_wcsicmp(KeyData, L"\"Windows2003\"") != 0) ))
573 {
574 /* This is not a ReactOS entry */
575 UseExistingEntry = FALSE;
576 }
577 }
578 else
579 {
580 UseExistingEntry = FALSE;
581 }
582
583 if (UseExistingEntry)
584 {
585 /* BootType is Windows2003. Now check SystemPath. */
586 Status = IniCacheGetKey(OsIniSection, L"SystemPath", &KeyData);
587 if (NT_SUCCESS(Status))
588 {
589 swprintf(SystemPath, L"\"%s\"", ArcPath);
590 if ((KeyData == NULL) ||
591 ( (_wcsicmp(KeyData, ArcPath) != 0) &&
592 (_wcsicmp(KeyData, SystemPath) != 0) ))
593 {
594 /* This entry is a ReactOS entry, but the SystemRoot
595 does not match the one we are looking for. */
596 UseExistingEntry = FALSE;
597 }
598 }
599 else
600 {
601 UseExistingEntry = FALSE;
602 }
603 }
604
605 if (UseExistingEntry)
606 {
607 IniCacheDestroy(IniCache);
608 return STATUS_SUCCESS;
609 }
610 }
611
612 swprintf(SectionName, L"ReactOS_%lu", i);
613 swprintf(OsName, L"\"ReactOS %lu\"", i);
614 i++;
615 }
616
617 /* Create a new "ReactOS" entry */
618 CreateNTOSEntry(IniCache, IniSection,
619 SectionName, OsName,
620 L"Windows2003", ArcPath,
621 L"");
622
623 IniCacheSave(IniCache, IniPath);
624 IniCacheDestroy(IniCache);
625
626 return STATUS_SUCCESS;
627 }
628
629 BOOLEAN
630 IsThereAValidBootSector(PWSTR RootPath)
631 {
632 /*
633 * Check the first DWORD (4 bytes) of the bootsector for a potential
634 * "valid" instruction (the BIOS starts execution of the bootsector
635 * at its beginning). Currently the criterium is that this DWORD must
636 * be non-zero.
637 */
638
639 NTSTATUS Status;
640 UNICODE_STRING Name;
641 OBJECT_ATTRIBUTES ObjectAttributes;
642 IO_STATUS_BLOCK IoStatusBlock;
643 HANDLE FileHandle;
644 LARGE_INTEGER FileOffset;
645 PUCHAR BootSector;
646 ULONG Instruction;
647
648 /* Allocate buffer for bootsector */
649 BootSector = RtlAllocateHeap(ProcessHeap, 0, SECTORSIZE);
650 if (BootSector == NULL)
651 return FALSE; // STATUS_INSUFFICIENT_RESOURCES;
652
653 /* Read current boot sector into buffer */
654 RtlInitUnicodeString(&Name, RootPath);
655
656 InitializeObjectAttributes(&ObjectAttributes,
657 &Name,
658 OBJ_CASE_INSENSITIVE,
659 NULL,
660 NULL);
661
662 Status = NtOpenFile(&FileHandle,
663 GENERIC_READ | SYNCHRONIZE,
664 &ObjectAttributes,
665 &IoStatusBlock,
666 0,
667 FILE_SYNCHRONOUS_IO_NONALERT);
668 if (!NT_SUCCESS(Status))
669 {
670 RtlFreeHeap(ProcessHeap, 0, BootSector);
671 return FALSE; // Status;
672 }
673
674 FileOffset.QuadPart = 0ULL;
675 Status = NtReadFile(FileHandle,
676 NULL,
677 NULL,
678 NULL,
679 &IoStatusBlock,
680 BootSector,
681 SECTORSIZE,
682 &FileOffset,
683 NULL);
684 NtClose(FileHandle);
685
686 Instruction = *(PULONG)BootSector;
687
688 /* Free the boot sector */
689 RtlFreeHeap(ProcessHeap, 0, BootSector);
690
691 if (!NT_SUCCESS(Status))
692 return FALSE; // Status;
693
694 return (Instruction != 0x00000000);
695 }
696
697 NTSTATUS
698 SaveBootSector(
699 PWSTR RootPath,
700 PWSTR DstPath,
701 ULONG Length)
702 {
703 NTSTATUS Status;
704 UNICODE_STRING Name;
705 OBJECT_ATTRIBUTES ObjectAttributes;
706 IO_STATUS_BLOCK IoStatusBlock;
707 HANDLE FileHandle;
708 LARGE_INTEGER FileOffset;
709 PUCHAR BootSector;
710
711 /* Allocate buffer for bootsector */
712 BootSector = RtlAllocateHeap(ProcessHeap, 0, Length);
713 if (BootSector == NULL)
714 return STATUS_INSUFFICIENT_RESOURCES;
715
716 /* Read current boot sector into buffer */
717 RtlInitUnicodeString(&Name, RootPath);
718
719 InitializeObjectAttributes(&ObjectAttributes,
720 &Name,
721 OBJ_CASE_INSENSITIVE,
722 NULL,
723 NULL);
724
725 Status = NtOpenFile(&FileHandle,
726 GENERIC_READ | SYNCHRONIZE,
727 &ObjectAttributes,
728 &IoStatusBlock,
729 0,
730 FILE_SYNCHRONOUS_IO_NONALERT);
731 if (!NT_SUCCESS(Status))
732 {
733 RtlFreeHeap(ProcessHeap, 0, BootSector);
734 return Status;
735 }
736
737 FileOffset.QuadPart = 0ULL;
738 Status = NtReadFile(FileHandle,
739 NULL,
740 NULL,
741 NULL,
742 &IoStatusBlock,
743 BootSector,
744 Length,
745 &FileOffset,
746 NULL);
747 NtClose(FileHandle);
748 if (!NT_SUCCESS(Status))
749 {
750 RtlFreeHeap(ProcessHeap, 0, BootSector);
751 return Status;
752 }
753
754 /* Write bootsector to DstPath */
755 RtlInitUnicodeString(&Name, DstPath);
756
757 InitializeObjectAttributes(&ObjectAttributes,
758 &Name,
759 0,
760 NULL,
761 NULL);
762
763 Status = NtCreateFile(&FileHandle,
764 GENERIC_WRITE | SYNCHRONIZE,
765 &ObjectAttributes,
766 &IoStatusBlock,
767 NULL,
768 FILE_ATTRIBUTE_NORMAL,
769 0,
770 FILE_SUPERSEDE,
771 FILE_SYNCHRONOUS_IO_NONALERT | FILE_SEQUENTIAL_ONLY,
772 NULL,
773 0);
774 if (!NT_SUCCESS(Status))
775 {
776 RtlFreeHeap(ProcessHeap, 0, BootSector);
777 return Status;
778 }
779
780 Status = NtWriteFile(FileHandle,
781 NULL,
782 NULL,
783 NULL,
784 &IoStatusBlock,
785 BootSector,
786 Length,
787 NULL,
788 NULL);
789 NtClose(FileHandle);
790
791 /* Free the boot sector */
792 RtlFreeHeap(ProcessHeap, 0, BootSector);
793
794 return Status;
795 }
796
797 static
798 NTSTATUS
799 InstallFat16BootCodeToFile(
800 PWSTR SrcPath,
801 PWSTR DstPath,
802 PWSTR RootPath)
803 {
804 NTSTATUS Status;
805 UNICODE_STRING Name;
806 OBJECT_ATTRIBUTES ObjectAttributes;
807 IO_STATUS_BLOCK IoStatusBlock;
808 HANDLE FileHandle;
809 LARGE_INTEGER FileOffset;
810 PFAT_BOOTSECTOR OrigBootSector;
811 PFAT_BOOTSECTOR NewBootSector;
812
813 /* Allocate buffer for original bootsector */
814 OrigBootSector = RtlAllocateHeap(ProcessHeap, 0, SECTORSIZE);
815 if (OrigBootSector == NULL)
816 return STATUS_INSUFFICIENT_RESOURCES;
817
818 /* Read current boot sector into buffer */
819 RtlInitUnicodeString(&Name, RootPath);
820
821 InitializeObjectAttributes(&ObjectAttributes,
822 &Name,
823 OBJ_CASE_INSENSITIVE,
824 NULL,
825 NULL);
826
827 Status = NtOpenFile(&FileHandle,
828 GENERIC_READ | SYNCHRONIZE,
829 &ObjectAttributes,
830 &IoStatusBlock,
831 0,
832 FILE_SYNCHRONOUS_IO_NONALERT);
833 if (!NT_SUCCESS(Status))
834 {
835 RtlFreeHeap(ProcessHeap, 0, OrigBootSector);
836 return Status;
837 }
838
839 FileOffset.QuadPart = 0ULL;
840 Status = NtReadFile(FileHandle,
841 NULL,
842 NULL,
843 NULL,
844 &IoStatusBlock,
845 OrigBootSector,
846 SECTORSIZE,
847 &FileOffset,
848 NULL);
849 NtClose(FileHandle);
850 if (!NT_SUCCESS(Status))
851 {
852 RtlFreeHeap(ProcessHeap, 0, OrigBootSector);
853 return Status;
854 }
855
856 /* Allocate buffer for new bootsector */
857 NewBootSector = RtlAllocateHeap(ProcessHeap, 0, SECTORSIZE);
858 if (NewBootSector == NULL)
859 {
860 RtlFreeHeap(ProcessHeap, 0, OrigBootSector);
861 return STATUS_INSUFFICIENT_RESOURCES;
862 }
863
864 /* Read new bootsector from SrcPath */
865 RtlInitUnicodeString(&Name, SrcPath);
866
867 InitializeObjectAttributes(&ObjectAttributes,
868 &Name,
869 OBJ_CASE_INSENSITIVE,
870 NULL,
871 NULL);
872
873 Status = NtOpenFile(&FileHandle,
874 GENERIC_READ | SYNCHRONIZE,
875 &ObjectAttributes,
876 &IoStatusBlock,
877 0,
878 FILE_SYNCHRONOUS_IO_NONALERT);
879 if (!NT_SUCCESS(Status))
880 {
881 RtlFreeHeap(ProcessHeap, 0, OrigBootSector);
882 RtlFreeHeap(ProcessHeap, 0, NewBootSector);
883 return Status;
884 }
885
886 FileOffset.QuadPart = 0ULL;
887 Status = NtReadFile(FileHandle,
888 NULL,
889 NULL,
890 NULL,
891 &IoStatusBlock,
892 NewBootSector,
893 SECTORSIZE,
894 &FileOffset,
895 NULL);
896 NtClose(FileHandle);
897 if (!NT_SUCCESS(Status))
898 {
899 RtlFreeHeap(ProcessHeap, 0, OrigBootSector);
900 RtlFreeHeap(ProcessHeap, 0, NewBootSector);
901 return Status;
902 }
903
904 /* Adjust bootsector (copy a part of the FAT BPB) */
905 memcpy(&NewBootSector->OemName,
906 &OrigBootSector->OemName,
907 FIELD_OFFSET(FAT_BOOTSECTOR, BootCodeAndData) -
908 FIELD_OFFSET(FAT_BOOTSECTOR, OemName));
909
910 /* Free the original boot sector */
911 RtlFreeHeap(ProcessHeap, 0, OrigBootSector);
912
913 /* Write new bootsector to DstPath */
914 RtlInitUnicodeString(&Name, DstPath);
915
916 InitializeObjectAttributes(&ObjectAttributes,
917 &Name,
918 0,
919 NULL,
920 NULL);
921
922 Status = NtCreateFile(&FileHandle,
923 GENERIC_WRITE | SYNCHRONIZE,
924 &ObjectAttributes,
925 &IoStatusBlock,
926 NULL,
927 FILE_ATTRIBUTE_NORMAL,
928 0,
929 FILE_OVERWRITE_IF,
930 FILE_SYNCHRONOUS_IO_NONALERT | FILE_SEQUENTIAL_ONLY,
931 NULL,
932 0);
933 if (!NT_SUCCESS(Status))
934 {
935 RtlFreeHeap(ProcessHeap, 0, NewBootSector);
936 return Status;
937 }
938
939 FileOffset.QuadPart = 0ULL;
940 Status = NtWriteFile(FileHandle,
941 NULL,
942 NULL,
943 NULL,
944 &IoStatusBlock,
945 NewBootSector,
946 SECTORSIZE,
947 NULL,
948 NULL);
949 NtClose(FileHandle);
950
951 /* Free the new boot sector */
952 RtlFreeHeap(ProcessHeap, 0, NewBootSector);
953
954 return Status;
955 }
956
957 static
958 NTSTATUS
959 InstallFat32BootCodeToFile(
960 PWSTR SrcPath,
961 PWSTR DstPath,
962 PWSTR RootPath)
963 {
964 NTSTATUS Status;
965 UNICODE_STRING Name;
966 OBJECT_ATTRIBUTES ObjectAttributes;
967 IO_STATUS_BLOCK IoStatusBlock;
968 HANDLE FileHandle;
969 LARGE_INTEGER FileOffset;
970 PFAT32_BOOTSECTOR OrigBootSector;
971 PFAT32_BOOTSECTOR NewBootSector;
972
973 /* Allocate buffer for original bootsector */
974 OrigBootSector = RtlAllocateHeap(ProcessHeap, 0, SECTORSIZE);
975 if (OrigBootSector == NULL)
976 return STATUS_INSUFFICIENT_RESOURCES;
977
978 /* Read current boot sector into buffer */
979 RtlInitUnicodeString(&Name, RootPath);
980
981 InitializeObjectAttributes(&ObjectAttributes,
982 &Name,
983 OBJ_CASE_INSENSITIVE,
984 NULL,
985 NULL);
986
987 Status = NtOpenFile(&FileHandle,
988 GENERIC_READ | SYNCHRONIZE,
989 &ObjectAttributes,
990 &IoStatusBlock,
991 0,
992 FILE_SYNCHRONOUS_IO_NONALERT);
993 if (!NT_SUCCESS(Status))
994 {
995 RtlFreeHeap(ProcessHeap, 0, OrigBootSector);
996 return Status;
997 }
998
999 FileOffset.QuadPart = 0ULL;
1000 Status = NtReadFile(FileHandle,
1001 NULL,
1002 NULL,
1003 NULL,
1004 &IoStatusBlock,
1005 OrigBootSector,
1006 SECTORSIZE,
1007 NULL,
1008 NULL);
1009 NtClose(FileHandle);
1010 if (!NT_SUCCESS(Status))
1011 {
1012 RtlFreeHeap(ProcessHeap, 0, OrigBootSector);
1013 return Status;
1014 }
1015
1016 /* Allocate buffer for new bootsector (2 sectors) */
1017 NewBootSector = RtlAllocateHeap(ProcessHeap, 0, 2 * SECTORSIZE);
1018 if (NewBootSector == NULL)
1019 {
1020 RtlFreeHeap(ProcessHeap, 0, OrigBootSector);
1021 return STATUS_INSUFFICIENT_RESOURCES;
1022 }
1023
1024 /* Read new bootsector from SrcPath */
1025 RtlInitUnicodeString(&Name, SrcPath);
1026
1027 InitializeObjectAttributes(&ObjectAttributes,
1028 &Name,
1029 OBJ_CASE_INSENSITIVE,
1030 NULL,
1031 NULL);
1032
1033 Status = NtOpenFile(&FileHandle,
1034 GENERIC_READ | SYNCHRONIZE,
1035 &ObjectAttributes,
1036 &IoStatusBlock,
1037 0,
1038 FILE_SYNCHRONOUS_IO_NONALERT);
1039 if (!NT_SUCCESS(Status))
1040 {
1041 RtlFreeHeap(ProcessHeap, 0, OrigBootSector);
1042 RtlFreeHeap(ProcessHeap, 0, NewBootSector);
1043 return Status;
1044 }
1045
1046 Status = NtReadFile(FileHandle,
1047 NULL,
1048 NULL,
1049 NULL,
1050 &IoStatusBlock,
1051 NewBootSector,
1052 2 * SECTORSIZE,
1053 NULL,
1054 NULL);
1055 NtClose(FileHandle);
1056 if (!NT_SUCCESS(Status))
1057 {
1058 RtlFreeHeap(ProcessHeap, 0, OrigBootSector);
1059 RtlFreeHeap(ProcessHeap, 0, NewBootSector);
1060 return Status;
1061 }
1062
1063 /* Adjust bootsector (copy a part of the FAT32 BPB) */
1064 memcpy(&NewBootSector->OemName,
1065 &OrigBootSector->OemName,
1066 FIELD_OFFSET(FAT32_BOOTSECTOR, BootCodeAndData) -
1067 FIELD_OFFSET(FAT32_BOOTSECTOR, OemName));
1068
1069 /* Disable the backup boot sector */
1070 NewBootSector->BackupBootSector = 0;
1071
1072 /* Free the original boot sector */
1073 RtlFreeHeap(ProcessHeap, 0, OrigBootSector);
1074
1075 /* Write the first sector of the new bootcode to DstPath */
1076 RtlInitUnicodeString(&Name, DstPath);
1077
1078 InitializeObjectAttributes(&ObjectAttributes,
1079 &Name,
1080 0,
1081 NULL,
1082 NULL);
1083
1084 Status = NtCreateFile(&FileHandle,
1085 GENERIC_WRITE | SYNCHRONIZE,
1086 &ObjectAttributes,
1087 &IoStatusBlock,
1088 NULL,
1089 FILE_ATTRIBUTE_NORMAL,
1090 0,
1091 FILE_SUPERSEDE,
1092 FILE_SYNCHRONOUS_IO_NONALERT | FILE_SEQUENTIAL_ONLY,
1093 NULL,
1094 0);
1095 if (!NT_SUCCESS(Status))
1096 {
1097 RtlFreeHeap(ProcessHeap, 0, NewBootSector);
1098 return Status;
1099 }
1100
1101 FileOffset.QuadPart = 0ULL;
1102 Status = NtWriteFile(FileHandle,
1103 NULL,
1104 NULL,
1105 NULL,
1106 &IoStatusBlock,
1107 NewBootSector,
1108 SECTORSIZE,
1109 NULL,
1110 NULL);
1111 NtClose(FileHandle);
1112 if (!NT_SUCCESS(Status))
1113 {
1114 RtlFreeHeap(ProcessHeap, 0, NewBootSector);
1115 return Status;
1116 }
1117
1118 /* Write the second sector of the new bootcode to boot disk sector 14 */
1119 RtlInitUnicodeString(&Name, RootPath);
1120
1121 InitializeObjectAttributes(&ObjectAttributes,
1122 &Name,
1123 0,
1124 NULL,
1125 NULL);
1126
1127 Status = NtOpenFile(&FileHandle,
1128 GENERIC_WRITE | SYNCHRONIZE,
1129 &ObjectAttributes,
1130 &IoStatusBlock,
1131 0,
1132 FILE_SYNCHRONOUS_IO_NONALERT);
1133 if (!NT_SUCCESS(Status))
1134 {
1135 RtlFreeHeap(ProcessHeap, 0, NewBootSector);
1136 return Status;
1137 }
1138
1139 FileOffset.QuadPart = (ULONGLONG)(14 * SECTORSIZE);
1140 Status = NtWriteFile(FileHandle,
1141 NULL,
1142 NULL,
1143 NULL,
1144 &IoStatusBlock,
1145 ((PUCHAR)NewBootSector + SECTORSIZE),
1146 SECTORSIZE,
1147 &FileOffset,
1148 NULL);
1149 if (!NT_SUCCESS(Status))
1150 {
1151 }
1152 NtClose(FileHandle);
1153
1154 /* Free the new boot sector */
1155 RtlFreeHeap(ProcessHeap, 0, NewBootSector);
1156
1157 return Status;
1158 }
1159
1160
1161 NTSTATUS
1162 InstallMbrBootCodeToDisk(
1163 PWSTR SrcPath,
1164 PWSTR RootPath)
1165 {
1166 NTSTATUS Status;
1167 UNICODE_STRING Name;
1168 OBJECT_ATTRIBUTES ObjectAttributes;
1169 IO_STATUS_BLOCK IoStatusBlock;
1170 HANDLE FileHandle;
1171 LARGE_INTEGER FileOffset;
1172 PPARTITION_SECTOR OrigBootSector;
1173 PPARTITION_SECTOR NewBootSector;
1174
1175 /* Allocate buffer for original bootsector */
1176 OrigBootSector = (PPARTITION_SECTOR)RtlAllocateHeap(ProcessHeap,
1177 0,
1178 sizeof(PARTITION_SECTOR));
1179 if (OrigBootSector == NULL)
1180 return STATUS_INSUFFICIENT_RESOURCES;
1181
1182 /* Read current boot sector into buffer */
1183 RtlInitUnicodeString(&Name,
1184 RootPath);
1185
1186 InitializeObjectAttributes(&ObjectAttributes,
1187 &Name,
1188 OBJ_CASE_INSENSITIVE,
1189 NULL,
1190 NULL);
1191
1192 Status = NtOpenFile(&FileHandle,
1193 GENERIC_READ | SYNCHRONIZE,
1194 &ObjectAttributes,
1195 &IoStatusBlock,
1196 0,
1197 FILE_SYNCHRONOUS_IO_NONALERT);
1198 if (!NT_SUCCESS(Status))
1199 {
1200 RtlFreeHeap(ProcessHeap, 0, OrigBootSector);
1201 return Status;
1202 }
1203
1204 FileOffset.QuadPart = 0ULL;
1205 Status = NtReadFile(FileHandle,
1206 NULL,
1207 NULL,
1208 NULL,
1209 &IoStatusBlock,
1210 OrigBootSector,
1211 sizeof(PARTITION_SECTOR),
1212 &FileOffset,
1213 NULL);
1214 NtClose(FileHandle);
1215 if (!NT_SUCCESS(Status))
1216 {
1217 RtlFreeHeap(ProcessHeap, 0, OrigBootSector);
1218 return Status;
1219 }
1220
1221 /* Allocate buffer for new bootsector */
1222 NewBootSector = (PPARTITION_SECTOR)RtlAllocateHeap(ProcessHeap,
1223 0,
1224 sizeof(PARTITION_SECTOR));
1225 if (NewBootSector == NULL)
1226 {
1227 RtlFreeHeap(ProcessHeap, 0, OrigBootSector);
1228 return STATUS_INSUFFICIENT_RESOURCES;
1229 }
1230
1231 /* Read new bootsector from SrcPath */
1232 RtlInitUnicodeString(&Name, SrcPath);
1233
1234 InitializeObjectAttributes(&ObjectAttributes,
1235 &Name,
1236 OBJ_CASE_INSENSITIVE,
1237 NULL,
1238 NULL);
1239
1240 Status = NtOpenFile(&FileHandle,
1241 GENERIC_READ | SYNCHRONIZE,
1242 &ObjectAttributes,
1243 &IoStatusBlock,
1244 0,
1245 FILE_SYNCHRONOUS_IO_NONALERT);
1246 if (!NT_SUCCESS(Status))
1247 {
1248 RtlFreeHeap(ProcessHeap, 0, OrigBootSector);
1249 RtlFreeHeap(ProcessHeap, 0, NewBootSector);
1250 return Status;
1251 }
1252
1253 Status = NtReadFile(FileHandle,
1254 NULL,
1255 NULL,
1256 NULL,
1257 &IoStatusBlock,
1258 NewBootSector,
1259 sizeof(PARTITION_SECTOR),
1260 NULL,
1261 NULL);
1262 NtClose(FileHandle);
1263 if (!NT_SUCCESS(Status))
1264 {
1265 RtlFreeHeap(ProcessHeap, 0, OrigBootSector);
1266 RtlFreeHeap(ProcessHeap, 0, NewBootSector);
1267 return Status;
1268 }
1269
1270 /* Copy partition table from old MBR to new */
1271 RtlCopyMemory(&NewBootSector->Signature,
1272 &OrigBootSector->Signature,
1273 sizeof(PARTITION_SECTOR) - offsetof(PARTITION_SECTOR, Signature) /* Length of partition table */);
1274
1275 /* Free the original boot sector */
1276 RtlFreeHeap(ProcessHeap, 0, OrigBootSector);
1277
1278 /* Write new bootsector to RootPath */
1279 RtlInitUnicodeString(&Name, RootPath);
1280
1281 InitializeObjectAttributes(&ObjectAttributes,
1282 &Name,
1283 0,
1284 NULL,
1285 NULL);
1286
1287 Status = NtOpenFile(&FileHandle,
1288 GENERIC_WRITE | SYNCHRONIZE,
1289 &ObjectAttributes,
1290 &IoStatusBlock,
1291 0,
1292 FILE_SYNCHRONOUS_IO_NONALERT | FILE_SEQUENTIAL_ONLY);
1293 if (!NT_SUCCESS(Status))
1294 {
1295 DPRINT1("NtOpenFile() failed (Status %lx)\n", Status);
1296 RtlFreeHeap(ProcessHeap, 0, NewBootSector);
1297 return Status;
1298 }
1299
1300 FileOffset.QuadPart = 0ULL;
1301 Status = NtWriteFile(FileHandle,
1302 NULL,
1303 NULL,
1304 NULL,
1305 &IoStatusBlock,
1306 NewBootSector,
1307 sizeof(PARTITION_SECTOR),
1308 &FileOffset,
1309 NULL);
1310 NtClose(FileHandle);
1311
1312 /* Free the new boot sector */
1313 RtlFreeHeap(ProcessHeap, 0, NewBootSector);
1314
1315 return Status;
1316 }
1317
1318 static
1319 NTSTATUS
1320 InstallFat12BootCodeToFloppy(
1321 PWSTR SrcPath,
1322 PWSTR RootPath)
1323 {
1324 NTSTATUS Status;
1325 UNICODE_STRING Name;
1326 OBJECT_ATTRIBUTES ObjectAttributes;
1327 IO_STATUS_BLOCK IoStatusBlock;
1328 HANDLE FileHandle;
1329 LARGE_INTEGER FileOffset;
1330 PFAT_BOOTSECTOR OrigBootSector;
1331 PFAT_BOOTSECTOR NewBootSector;
1332
1333 /* Allocate buffer for original bootsector */
1334 OrigBootSector = RtlAllocateHeap(ProcessHeap, 0, SECTORSIZE);
1335 if (OrigBootSector == NULL)
1336 return STATUS_INSUFFICIENT_RESOURCES;
1337
1338 /* Read current boot sector into buffer */
1339 RtlInitUnicodeString(&Name, RootPath);
1340
1341 InitializeObjectAttributes(&ObjectAttributes,
1342 &Name,
1343 OBJ_CASE_INSENSITIVE,
1344 NULL,
1345 NULL);
1346
1347 Status = NtOpenFile(&FileHandle,
1348 GENERIC_READ | SYNCHRONIZE,
1349 &ObjectAttributes,
1350 &IoStatusBlock,
1351 0,
1352 FILE_SYNCHRONOUS_IO_NONALERT);
1353 if (!NT_SUCCESS(Status))
1354 {
1355 RtlFreeHeap(ProcessHeap, 0, OrigBootSector);
1356 return Status;
1357 }
1358
1359 FileOffset.QuadPart = 0ULL;
1360 Status = NtReadFile(FileHandle,
1361 NULL,
1362 NULL,
1363 NULL,
1364 &IoStatusBlock,
1365 OrigBootSector,
1366 SECTORSIZE,
1367 &FileOffset,
1368 NULL);
1369 NtClose(FileHandle);
1370 if (!NT_SUCCESS(Status))
1371 {
1372 RtlFreeHeap(ProcessHeap, 0, OrigBootSector);
1373 return Status;
1374 }
1375
1376 /* Allocate buffer for new bootsector */
1377 NewBootSector = RtlAllocateHeap(ProcessHeap,
1378 0,
1379 SECTORSIZE);
1380 if (NewBootSector == NULL)
1381 {
1382 RtlFreeHeap(ProcessHeap, 0, OrigBootSector);
1383 return STATUS_INSUFFICIENT_RESOURCES;
1384 }
1385
1386 /* Read new bootsector from SrcPath */
1387 RtlInitUnicodeString(&Name, SrcPath);
1388
1389 InitializeObjectAttributes(&ObjectAttributes,
1390 &Name,
1391 OBJ_CASE_INSENSITIVE,
1392 NULL,
1393 NULL);
1394
1395 Status = NtOpenFile(&FileHandle,
1396 GENERIC_READ | SYNCHRONIZE,
1397 &ObjectAttributes,
1398 &IoStatusBlock,
1399 0,
1400 FILE_SYNCHRONOUS_IO_NONALERT);
1401 if (!NT_SUCCESS(Status))
1402 {
1403 RtlFreeHeap(ProcessHeap, 0, OrigBootSector);
1404 RtlFreeHeap(ProcessHeap, 0, NewBootSector);
1405 return Status;
1406 }
1407
1408 Status = NtReadFile(FileHandle,
1409 NULL,
1410 NULL,
1411 NULL,
1412 &IoStatusBlock,
1413 NewBootSector,
1414 SECTORSIZE,
1415 NULL,
1416 NULL);
1417 NtClose(FileHandle);
1418 if (!NT_SUCCESS(Status))
1419 {
1420 RtlFreeHeap(ProcessHeap, 0, OrigBootSector);
1421 RtlFreeHeap(ProcessHeap, 0, NewBootSector);
1422 return Status;
1423 }
1424
1425 /* Adjust bootsector (copy a part of the FAT16 BPB) */
1426 memcpy(&NewBootSector->OemName,
1427 &OrigBootSector->OemName,
1428 FIELD_OFFSET(FAT_BOOTSECTOR, BootCodeAndData) -
1429 FIELD_OFFSET(FAT_BOOTSECTOR, OemName));
1430
1431 /* Free the original boot sector */
1432 RtlFreeHeap(ProcessHeap, 0, OrigBootSector);
1433
1434 /* Write new bootsector to RootPath */
1435 RtlInitUnicodeString(&Name, RootPath);
1436
1437 InitializeObjectAttributes(&ObjectAttributes,
1438 &Name,
1439 0,
1440 NULL,
1441 NULL);
1442
1443 Status = NtOpenFile(&FileHandle,
1444 GENERIC_WRITE | SYNCHRONIZE,
1445 &ObjectAttributes,
1446 &IoStatusBlock,
1447 0,
1448 FILE_SYNCHRONOUS_IO_NONALERT | FILE_SEQUENTIAL_ONLY);
1449 if (!NT_SUCCESS(Status))
1450 {
1451 DPRINT1("NtOpenFile() failed (Status %lx)\n", Status);
1452 RtlFreeHeap(ProcessHeap, 0, NewBootSector);
1453 return Status;
1454 }
1455
1456 FileOffset.QuadPart = 0ULL;
1457 Status = NtWriteFile(FileHandle,
1458 NULL,
1459 NULL,
1460 NULL,
1461 &IoStatusBlock,
1462 NewBootSector,
1463 SECTORSIZE,
1464 &FileOffset,
1465 NULL);
1466 NtClose(FileHandle);
1467
1468 /* Free the new boot sector */
1469 RtlFreeHeap(ProcessHeap, 0, NewBootSector);
1470
1471 return Status;
1472 }
1473
1474 static
1475 NTSTATUS
1476 InstallFat16BootCodeToDisk(
1477 PWSTR SrcPath,
1478 PWSTR RootPath)
1479 {
1480 NTSTATUS Status;
1481 UNICODE_STRING Name;
1482 OBJECT_ATTRIBUTES ObjectAttributes;
1483 IO_STATUS_BLOCK IoStatusBlock;
1484 HANDLE FileHandle;
1485 LARGE_INTEGER FileOffset;
1486 PFAT_BOOTSECTOR OrigBootSector;
1487 PFAT_BOOTSECTOR NewBootSector;
1488
1489 /* Allocate buffer for original bootsector */
1490 OrigBootSector = RtlAllocateHeap(ProcessHeap, 0, SECTORSIZE);
1491 if (OrigBootSector == NULL)
1492 return STATUS_INSUFFICIENT_RESOURCES;
1493
1494 /* Read current boot sector into buffer */
1495 RtlInitUnicodeString(&Name, RootPath);
1496
1497 InitializeObjectAttributes(&ObjectAttributes,
1498 &Name,
1499 OBJ_CASE_INSENSITIVE,
1500 NULL,
1501 NULL);
1502
1503 Status = NtOpenFile(&FileHandle,
1504 GENERIC_READ | SYNCHRONIZE,
1505 &ObjectAttributes,
1506 &IoStatusBlock,
1507 0,
1508 FILE_SYNCHRONOUS_IO_NONALERT);
1509 if (!NT_SUCCESS(Status))
1510 {
1511 RtlFreeHeap(ProcessHeap, 0, OrigBootSector);
1512 return Status;
1513 }
1514
1515 FileOffset.QuadPart = 0ULL;
1516 Status = NtReadFile(FileHandle,
1517 NULL,
1518 NULL,
1519 NULL,
1520 &IoStatusBlock,
1521 OrigBootSector,
1522 SECTORSIZE,
1523 &FileOffset,
1524 NULL);
1525 NtClose(FileHandle);
1526 if (!NT_SUCCESS(Status))
1527 {
1528 RtlFreeHeap(ProcessHeap, 0, OrigBootSector);
1529 return Status;
1530 }
1531
1532 /* Allocate buffer for new bootsector */
1533 NewBootSector = RtlAllocateHeap(ProcessHeap, 0, SECTORSIZE);
1534 if (NewBootSector == NULL)
1535 {
1536 RtlFreeHeap(ProcessHeap, 0, OrigBootSector);
1537 return STATUS_INSUFFICIENT_RESOURCES;
1538 }
1539
1540 /* Read new bootsector from SrcPath */
1541 RtlInitUnicodeString(&Name, SrcPath);
1542
1543 InitializeObjectAttributes(&ObjectAttributes,
1544 &Name,
1545 OBJ_CASE_INSENSITIVE,
1546 NULL,
1547 NULL);
1548
1549 Status = NtOpenFile(&FileHandle,
1550 GENERIC_READ | SYNCHRONIZE,
1551 &ObjectAttributes,
1552 &IoStatusBlock,
1553 0,
1554 FILE_SYNCHRONOUS_IO_NONALERT);
1555 if (!NT_SUCCESS(Status))
1556 {
1557 RtlFreeHeap(ProcessHeap, 0, OrigBootSector);
1558 RtlFreeHeap(ProcessHeap, 0, NewBootSector);
1559 return Status;
1560 }
1561
1562 Status = NtReadFile(FileHandle,
1563 NULL,
1564 NULL,
1565 NULL,
1566 &IoStatusBlock,
1567 NewBootSector,
1568 SECTORSIZE,
1569 NULL,
1570 NULL);
1571 NtClose(FileHandle);
1572 if (!NT_SUCCESS(Status))
1573 {
1574 RtlFreeHeap(ProcessHeap, 0, OrigBootSector);
1575 RtlFreeHeap(ProcessHeap, 0, NewBootSector);
1576 return Status;
1577 }
1578
1579 /* Adjust bootsector (copy a part of the FAT16 BPB) */
1580 memcpy(&NewBootSector->OemName,
1581 &OrigBootSector->OemName,
1582 FIELD_OFFSET(FAT_BOOTSECTOR, BootCodeAndData) -
1583 FIELD_OFFSET(FAT_BOOTSECTOR, OemName));
1584
1585 NewBootSector->HiddenSectors = PartitionList->CurrentDisk->SectorsPerTrack;
1586
1587 /* Free the original boot sector */
1588 RtlFreeHeap(ProcessHeap, 0, OrigBootSector);
1589
1590 /* Write new bootsector to RootPath */
1591 RtlInitUnicodeString(&Name, RootPath);
1592
1593 InitializeObjectAttributes(&ObjectAttributes,
1594 &Name,
1595 0,
1596 NULL,
1597 NULL);
1598
1599 Status = NtOpenFile(&FileHandle,
1600 GENERIC_WRITE | SYNCHRONIZE,
1601 &ObjectAttributes,
1602 &IoStatusBlock,
1603 0,
1604 FILE_SYNCHRONOUS_IO_NONALERT | FILE_SEQUENTIAL_ONLY);
1605 if (!NT_SUCCESS(Status))
1606 {
1607 DPRINT1("NtOpenFile() failed (Status %lx)\n", Status);
1608 RtlFreeHeap(ProcessHeap, 0, NewBootSector);
1609 return Status;
1610 }
1611
1612 FileOffset.QuadPart = 0ULL;
1613 Status = NtWriteFile(FileHandle,
1614 NULL,
1615 NULL,
1616 NULL,
1617 &IoStatusBlock,
1618 NewBootSector,
1619 SECTORSIZE,
1620 &FileOffset,
1621 NULL);
1622 NtClose(FileHandle);
1623
1624 /* Free the new boot sector */
1625 RtlFreeHeap(ProcessHeap, 0, NewBootSector);
1626
1627 return Status;
1628 }
1629
1630 static
1631 NTSTATUS
1632 InstallFat32BootCodeToDisk(
1633 PWSTR SrcPath,
1634 PWSTR RootPath)
1635 {
1636 NTSTATUS Status;
1637 UNICODE_STRING Name;
1638 OBJECT_ATTRIBUTES ObjectAttributes;
1639 IO_STATUS_BLOCK IoStatusBlock;
1640 HANDLE FileHandle;
1641 LARGE_INTEGER FileOffset;
1642 PFAT32_BOOTSECTOR OrigBootSector;
1643 PFAT32_BOOTSECTOR NewBootSector;
1644 USHORT BackupBootSector;
1645
1646 /* Allocate buffer for original bootsector */
1647 OrigBootSector = RtlAllocateHeap(ProcessHeap, 0, SECTORSIZE);
1648 if (OrigBootSector == NULL)
1649 return STATUS_INSUFFICIENT_RESOURCES;
1650
1651 /* Read current boot sector into buffer */
1652 RtlInitUnicodeString(&Name, RootPath);
1653
1654 InitializeObjectAttributes(&ObjectAttributes,
1655 &Name,
1656 OBJ_CASE_INSENSITIVE,
1657 NULL,
1658 NULL);
1659
1660 Status = NtOpenFile(&FileHandle,
1661 GENERIC_READ | SYNCHRONIZE,
1662 &ObjectAttributes,
1663 &IoStatusBlock,
1664 0,
1665 FILE_SYNCHRONOUS_IO_NONALERT);
1666 if (!NT_SUCCESS(Status))
1667 {
1668 RtlFreeHeap(ProcessHeap, 0, OrigBootSector);
1669 return Status;
1670 }
1671
1672 FileOffset.QuadPart = 0ULL;
1673 Status = NtReadFile(FileHandle,
1674 NULL,
1675 NULL,
1676 NULL,
1677 &IoStatusBlock,
1678 OrigBootSector,
1679 SECTORSIZE,
1680 &FileOffset,
1681 NULL);
1682 NtClose(FileHandle);
1683 if (!NT_SUCCESS(Status))
1684 {
1685 RtlFreeHeap(ProcessHeap, 0, OrigBootSector);
1686 return Status;
1687 }
1688
1689
1690 /* Allocate buffer for new bootsector (2 sectors) */
1691 NewBootSector = RtlAllocateHeap(ProcessHeap, 0, 2 * SECTORSIZE);
1692 if (NewBootSector == NULL)
1693 {
1694 RtlFreeHeap(ProcessHeap, 0, OrigBootSector);
1695 return STATUS_INSUFFICIENT_RESOURCES;
1696 }
1697
1698 /* Read new bootsector from SrcPath */
1699 RtlInitUnicodeString(&Name, SrcPath);
1700
1701 InitializeObjectAttributes(&ObjectAttributes,
1702 &Name,
1703 OBJ_CASE_INSENSITIVE,
1704 NULL,
1705 NULL);
1706
1707 Status = NtOpenFile(&FileHandle,
1708 GENERIC_READ | SYNCHRONIZE,
1709 &ObjectAttributes,
1710 &IoStatusBlock,
1711 0,
1712 FILE_SYNCHRONOUS_IO_NONALERT);
1713 if (!NT_SUCCESS(Status))
1714 {
1715 RtlFreeHeap(ProcessHeap, 0, OrigBootSector);
1716 RtlFreeHeap(ProcessHeap, 0, NewBootSector);
1717 return Status;
1718 }
1719
1720 Status = NtReadFile(FileHandle,
1721 NULL,
1722 NULL,
1723 NULL,
1724 &IoStatusBlock,
1725 NewBootSector,
1726 2 * SECTORSIZE,
1727 NULL,
1728 NULL);
1729 NtClose(FileHandle);
1730 if (!NT_SUCCESS(Status))
1731 {
1732 RtlFreeHeap(ProcessHeap, 0, OrigBootSector);
1733 RtlFreeHeap(ProcessHeap, 0, NewBootSector);
1734 return Status;
1735 }
1736
1737 /* Adjust bootsector (copy a part of the FAT32 BPB) */
1738 memcpy(&NewBootSector->OemName,
1739 &OrigBootSector->OemName,
1740 FIELD_OFFSET(FAT32_BOOTSECTOR, BootCodeAndData) -
1741 FIELD_OFFSET(FAT32_BOOTSECTOR, OemName));
1742
1743 NewBootSector->HiddenSectors = PartitionList->CurrentDisk->SectorsPerTrack;
1744
1745 /* Get the location of the backup boot sector */
1746 BackupBootSector = OrigBootSector->BackupBootSector;
1747
1748 /* Free the original boot sector */
1749 RtlFreeHeap(ProcessHeap, 0, OrigBootSector);
1750
1751 /* Write the first sector of the new bootcode to DstPath */
1752 RtlInitUnicodeString(&Name, RootPath);
1753
1754 InitializeObjectAttributes(&ObjectAttributes,
1755 &Name,
1756 0,
1757 NULL,
1758 NULL);
1759
1760 Status = NtOpenFile(&FileHandle,
1761 GENERIC_WRITE | SYNCHRONIZE,
1762 &ObjectAttributes,
1763 &IoStatusBlock,
1764 0,
1765 FILE_SYNCHRONOUS_IO_NONALERT | FILE_SEQUENTIAL_ONLY);
1766 if (!NT_SUCCESS(Status))
1767 {
1768 DPRINT1("NtOpenFile() failed (Status %lx)\n", Status);
1769 RtlFreeHeap(ProcessHeap, 0, NewBootSector);
1770 return Status;
1771 }
1772
1773 /* Write sector 0 */
1774 FileOffset.QuadPart = 0ULL;
1775 Status = NtWriteFile(FileHandle,
1776 NULL,
1777 NULL,
1778 NULL,
1779 &IoStatusBlock,
1780 NewBootSector,
1781 SECTORSIZE,
1782 &FileOffset,
1783 NULL);
1784 if (!NT_SUCCESS(Status))
1785 {
1786 DPRINT1("NtWriteFile() failed (Status %lx)\n", Status);
1787 NtClose(FileHandle);
1788 RtlFreeHeap(ProcessHeap, 0, NewBootSector);
1789 return Status;
1790 }
1791
1792 /* Write backup boot sector */
1793 if ((BackupBootSector != 0x0000) && (BackupBootSector != 0xFFFF))
1794 {
1795 FileOffset.QuadPart = (ULONGLONG)((ULONG)BackupBootSector * SECTORSIZE);
1796 Status = NtWriteFile(FileHandle,
1797 NULL,
1798 NULL,
1799 NULL,
1800 &IoStatusBlock,
1801 NewBootSector,
1802 SECTORSIZE,
1803 &FileOffset,
1804 NULL);
1805 if (!NT_SUCCESS(Status))
1806 {
1807 DPRINT1("NtWriteFile() failed (Status %lx)\n", Status);
1808 NtClose(FileHandle);
1809 RtlFreeHeap(ProcessHeap, 0, NewBootSector);
1810 return Status;
1811 }
1812 }
1813
1814 /* Write sector 14 */
1815 FileOffset.QuadPart = 14 * SECTORSIZE;
1816 Status = NtWriteFile(FileHandle,
1817 NULL,
1818 NULL,
1819 NULL,
1820 &IoStatusBlock,
1821 ((PUCHAR)NewBootSector + SECTORSIZE),
1822 SECTORSIZE,
1823 &FileOffset,
1824 NULL);
1825 if (!NT_SUCCESS(Status))
1826 {
1827 DPRINT1("NtWriteFile() failed (Status %lx)\n", Status);
1828 }
1829 NtClose(FileHandle);
1830
1831 /* Free the new boot sector */
1832 RtlFreeHeap(ProcessHeap, 0, NewBootSector);
1833
1834 return Status;
1835 }
1836
1837 static
1838 NTSTATUS
1839 InstallExt2BootCodeToDisk(
1840 PWSTR SrcPath,
1841 PWSTR RootPath)
1842 {
1843 NTSTATUS Status;
1844 UNICODE_STRING Name;
1845 OBJECT_ATTRIBUTES ObjectAttributes;
1846 IO_STATUS_BLOCK IoStatusBlock;
1847 HANDLE FileHandle;
1848 LARGE_INTEGER FileOffset;
1849 // PEXT2_BOOTSECTOR OrigBootSector;
1850 PEXT2_BOOTSECTOR NewBootSector;
1851 // USHORT BackupBootSector;
1852
1853 #if 0
1854 /* Allocate buffer for original bootsector */
1855 OrigBootSector = RtlAllocateHeap(ProcessHeap, 0, SECTORSIZE);
1856 if (OrigBootSector == NULL)
1857 return STATUS_INSUFFICIENT_RESOURCES;
1858
1859 /* Read current boot sector into buffer */
1860 RtlInitUnicodeString(&Name, RootPath);
1861
1862 InitializeObjectAttributes(&ObjectAttributes,
1863 &Name,
1864 OBJ_CASE_INSENSITIVE,
1865 NULL,
1866 NULL);
1867
1868 Status = NtOpenFile(&FileHandle,
1869 GENERIC_READ | SYNCHRONIZE,
1870 &ObjectAttributes,
1871 &IoStatusBlock,
1872 0,
1873 FILE_SYNCHRONOUS_IO_NONALERT);
1874 if (!NT_SUCCESS(Status))
1875 {
1876 RtlFreeHeap(ProcessHeap, 0, OrigBootSector);
1877 return Status;
1878 }
1879
1880 FileOffset.QuadPart = 0ULL;
1881 Status = NtReadFile(FileHandle,
1882 NULL,
1883 NULL,
1884 NULL,
1885 &IoStatusBlock,
1886 OrigBootSector,
1887 SECTORSIZE,
1888 &FileOffset,
1889 NULL);
1890 NtClose(FileHandle);
1891 if (!NT_SUCCESS(Status))
1892 {
1893 RtlFreeHeap(ProcessHeap, 0, OrigBootSector);
1894 return Status;
1895 }
1896 #endif
1897
1898 /* Allocate buffer for new bootsector */
1899 NewBootSector = RtlAllocateHeap(ProcessHeap, 0, sizeof(EXT2_BOOTSECTOR));
1900 if (NewBootSector == NULL)
1901 {
1902 // RtlFreeHeap(ProcessHeap, 0, OrigBootSector);
1903 return STATUS_INSUFFICIENT_RESOURCES;
1904 }
1905
1906 /* Read new bootsector from SrcPath */
1907 RtlInitUnicodeString(&Name, SrcPath);
1908
1909 InitializeObjectAttributes(&ObjectAttributes,
1910 &Name,
1911 OBJ_CASE_INSENSITIVE,
1912 NULL,
1913 NULL);
1914
1915 Status = NtOpenFile(&FileHandle,
1916 GENERIC_READ | SYNCHRONIZE,
1917 &ObjectAttributes,
1918 &IoStatusBlock,
1919 0,
1920 FILE_SYNCHRONOUS_IO_NONALERT);
1921 if (!NT_SUCCESS(Status))
1922 {
1923 // RtlFreeHeap(ProcessHeap, 0, OrigBootSector);
1924 RtlFreeHeap(ProcessHeap, 0, NewBootSector);
1925 return Status;
1926 }
1927
1928 Status = NtReadFile(FileHandle,
1929 NULL,
1930 NULL,
1931 NULL,
1932 &IoStatusBlock,
1933 NewBootSector,
1934 sizeof(EXT2_BOOTSECTOR),
1935 NULL,
1936 NULL);
1937 NtClose(FileHandle);
1938 if (!NT_SUCCESS(Status))
1939 {
1940 // RtlFreeHeap(ProcessHeap, 0, OrigBootSector);
1941 RtlFreeHeap(ProcessHeap, 0, NewBootSector);
1942 return Status;
1943 }
1944
1945 #if 0
1946 /* Adjust bootsector (copy a part of the FAT32 BPB) */
1947 memcpy(&NewBootSector->OemName,
1948 &OrigBootSector->OemName,
1949 FIELD_OFFSET(FAT32_BOOTSECTOR, BootCodeAndData) -
1950 FIELD_OFFSET(FAT32_BOOTSECTOR, OemName));
1951
1952 NewBootSector->HiddenSectors = PartitionList->CurrentDisk->SectorsPerTrack;
1953
1954 /* Get the location of the backup boot sector */
1955 BackupBootSector = OrigBootSector->BackupBootSector;
1956
1957 /* Free the original boot sector */
1958 // RtlFreeHeap(ProcessHeap, 0, OrigBootSector);
1959 #endif
1960
1961 /* Write new bootsector to RootPath */
1962 RtlInitUnicodeString(&Name, RootPath);
1963
1964 InitializeObjectAttributes(&ObjectAttributes,
1965 &Name,
1966 0,
1967 NULL,
1968 NULL);
1969
1970 Status = NtOpenFile(&FileHandle,
1971 GENERIC_WRITE | SYNCHRONIZE,
1972 &ObjectAttributes,
1973 &IoStatusBlock,
1974 0,
1975 FILE_SYNCHRONOUS_IO_NONALERT | FILE_SEQUENTIAL_ONLY);
1976 if (!NT_SUCCESS(Status))
1977 {
1978 DPRINT1("NtOpenFile() failed (Status %lx)\n", Status);
1979 RtlFreeHeap(ProcessHeap, 0, NewBootSector);
1980 return Status;
1981 }
1982
1983 /* Write sector 0 */
1984 FileOffset.QuadPart = 0ULL;
1985 Status = NtWriteFile(FileHandle,
1986 NULL,
1987 NULL,
1988 NULL,
1989 &IoStatusBlock,
1990 NewBootSector,
1991 sizeof(EXT2_BOOTSECTOR),
1992 &FileOffset,
1993 NULL);
1994 #if 0
1995 if (!NT_SUCCESS(Status))
1996 {
1997 DPRINT1("NtWriteFile() failed (Status %lx)\n", Status);
1998 NtClose(FileHandle);
1999 RtlFreeHeap(ProcessHeap, 0, NewBootSector);
2000 return Status;
2001 }
2002
2003 /* Write backup boot sector */
2004 if ((BackupBootSector != 0x0000) && (BackupBootSector != 0xFFFF))
2005 {
2006 FileOffset.QuadPart = (ULONGLONG)((ULONG)BackupBootSector * SECTORSIZE);
2007 Status = NtWriteFile(FileHandle,
2008 NULL,
2009 NULL,
2010 NULL,
2011 &IoStatusBlock,
2012 NewBootSector,
2013 SECTORSIZE,
2014 &FileOffset,
2015 NULL);
2016 if (!NT_SUCCESS(Status))
2017 {
2018 DPRINT1("NtWriteFile() failed (Status %lx)\n", Status);
2019 NtClose(FileHandle);
2020 RtlFreeHeap(ProcessHeap, 0, NewBootSector);
2021 return Status;
2022 }
2023 }
2024
2025 /* Write sector 14 */
2026 FileOffset.QuadPart = 14 * SECTORSIZE;
2027 Status = NtWriteFile(FileHandle,
2028 NULL,
2029 NULL,
2030 NULL,
2031 &IoStatusBlock,
2032 ((PUCHAR)NewBootSector + SECTORSIZE),
2033 SECTORSIZE,
2034 &FileOffset,
2035 NULL);
2036 if (!NT_SUCCESS(Status))
2037 {
2038 DPRINT1("NtWriteFile() failed (Status %lx)\n", Status);
2039 }
2040 #endif
2041 NtClose(FileHandle);
2042
2043 /* Free the new boot sector */
2044 RtlFreeHeap(ProcessHeap, 0, NewBootSector);
2045
2046 return Status;
2047 }
2048
2049 static
2050 NTSTATUS
2051 UnprotectBootIni(
2052 PWSTR FileName,
2053 PULONG Attributes)
2054 {
2055 NTSTATUS Status;
2056 UNICODE_STRING Name;
2057 OBJECT_ATTRIBUTES ObjectAttributes;
2058 IO_STATUS_BLOCK IoStatusBlock;
2059 FILE_BASIC_INFORMATION FileInfo;
2060 HANDLE FileHandle;
2061
2062 RtlInitUnicodeString(&Name, FileName);
2063
2064 InitializeObjectAttributes(&ObjectAttributes,
2065 &Name,
2066 OBJ_CASE_INSENSITIVE,
2067 NULL,
2068 NULL);
2069
2070 Status = NtOpenFile(&FileHandle,
2071 GENERIC_READ | GENERIC_WRITE | SYNCHRONIZE,
2072 &ObjectAttributes,
2073 &IoStatusBlock,
2074 0,
2075 FILE_SYNCHRONOUS_IO_NONALERT);
2076 if (Status == STATUS_NO_SUCH_FILE)
2077 {
2078 DPRINT1("NtOpenFile() failed (Status %lx)\n", Status);
2079 *Attributes = 0;
2080 return STATUS_SUCCESS;
2081 }
2082 if (!NT_SUCCESS(Status))
2083 {
2084 DPRINT1("NtOpenFile() failed (Status %lx)\n", Status);
2085 return Status;
2086 }
2087
2088 Status = NtQueryInformationFile(FileHandle,
2089 &IoStatusBlock,
2090 &FileInfo,
2091 sizeof(FILE_BASIC_INFORMATION),
2092 FileBasicInformation);
2093 if (!NT_SUCCESS(Status))
2094 {
2095 DPRINT1("NtQueryInformationFile() failed (Status %lx)\n", Status);
2096 NtClose(FileHandle);
2097 return Status;
2098 }
2099
2100 *Attributes = FileInfo.FileAttributes;
2101
2102 /* Delete attributes SYSTEM, HIDDEN and READONLY */
2103 FileInfo.FileAttributes = FileInfo.FileAttributes &
2104 ~(FILE_ATTRIBUTE_SYSTEM |
2105 FILE_ATTRIBUTE_HIDDEN |
2106 FILE_ATTRIBUTE_READONLY);
2107
2108 Status = NtSetInformationFile(FileHandle,
2109 &IoStatusBlock,
2110 &FileInfo,
2111 sizeof(FILE_BASIC_INFORMATION),
2112 FileBasicInformation);
2113 if (!NT_SUCCESS(Status))
2114 {
2115 DPRINT1("NtSetInformationFile() failed (Status %lx)\n", Status);
2116 }
2117
2118 NtClose(FileHandle);
2119 return Status;
2120 }
2121
2122 static
2123 NTSTATUS
2124 ProtectBootIni(
2125 PWSTR FileName,
2126 ULONG Attributes)
2127 {
2128 NTSTATUS Status;
2129 UNICODE_STRING Name;
2130 OBJECT_ATTRIBUTES ObjectAttributes;
2131 IO_STATUS_BLOCK IoStatusBlock;
2132 FILE_BASIC_INFORMATION FileInfo;
2133 HANDLE FileHandle;
2134
2135 RtlInitUnicodeString(&Name, FileName);
2136
2137 InitializeObjectAttributes(&ObjectAttributes,
2138 &Name,
2139 OBJ_CASE_INSENSITIVE,
2140 NULL,
2141 NULL);
2142
2143 Status = NtOpenFile(&FileHandle,
2144 GENERIC_READ | GENERIC_WRITE | SYNCHRONIZE,
2145 &ObjectAttributes,
2146 &IoStatusBlock,
2147 0,
2148 FILE_SYNCHRONOUS_IO_NONALERT);
2149 if (!NT_SUCCESS(Status))
2150 {
2151 DPRINT1("NtOpenFile() failed (Status %lx)\n", Status);
2152 return Status;
2153 }
2154
2155 Status = NtQueryInformationFile(FileHandle,
2156 &IoStatusBlock,
2157 &FileInfo,
2158 sizeof(FILE_BASIC_INFORMATION),
2159 FileBasicInformation);
2160 if (!NT_SUCCESS(Status))
2161 {
2162 DPRINT1("NtQueryInformationFile() failed (Status %lx)\n", Status);
2163 NtClose(FileHandle);
2164 return Status;
2165 }
2166
2167 FileInfo.FileAttributes = FileInfo.FileAttributes | Attributes;
2168
2169 Status = NtSetInformationFile(FileHandle,
2170 &IoStatusBlock,
2171 &FileInfo,
2172 sizeof(FILE_BASIC_INFORMATION),
2173 FileBasicInformation);
2174 if (!NT_SUCCESS(Status))
2175 {
2176 DPRINT1("NtSetInformationFile() failed (Status %lx)\n", Status);
2177 }
2178
2179 NtClose(FileHandle);
2180 return Status;
2181 }
2182
2183 static
2184 NTSTATUS
2185 UpdateBootIni(
2186 PWSTR BootIniPath,
2187 PWSTR EntryName,
2188 PWSTR EntryValue)
2189 {
2190 NTSTATUS Status;
2191 PINICACHE Cache = NULL;
2192 PINICACHESECTION Section = NULL;
2193 ULONG FileAttribute;
2194 PWCHAR OldValue = NULL;
2195
2196 Status = IniCacheLoad(&Cache, BootIniPath, FALSE);
2197 if (!NT_SUCCESS(Status))
2198 {
2199 return Status;
2200 }
2201
2202 Section = IniCacheGetSection(Cache,
2203 L"operating systems");
2204 if (Section == NULL)
2205 {
2206 IniCacheDestroy(Cache);
2207 return STATUS_UNSUCCESSFUL;
2208 }
2209
2210 /* Check - maybe record already exists */
2211 Status = IniCacheGetKey(Section, EntryName, &OldValue);
2212
2213 /* If either key was not found, or contains something else - add new one */
2214 if (!NT_SUCCESS(Status) || wcscmp(OldValue, EntryValue))
2215 {
2216 IniCacheInsertKey(Section,
2217 NULL,
2218 INSERT_LAST,
2219 EntryName,
2220 EntryValue);
2221 }
2222
2223 Status = UnprotectBootIni(BootIniPath,
2224 &FileAttribute);
2225 if (!NT_SUCCESS(Status))
2226 {
2227 IniCacheDestroy(Cache);
2228 return Status;
2229 }
2230
2231 Status = IniCacheSave(Cache, BootIniPath);
2232 if (!NT_SUCCESS(Status))
2233 {
2234 IniCacheDestroy(Cache);
2235 return Status;
2236 }
2237
2238 FileAttribute |= (FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_READONLY);
2239 Status = ProtectBootIni(BootIniPath, FileAttribute);
2240
2241 IniCacheDestroy(Cache);
2242
2243 return Status;
2244 }
2245
2246 static
2247 NTSTATUS
2248 InstallFatBootcodeToPartition(
2249 PUNICODE_STRING SystemRootPath,
2250 PUNICODE_STRING SourceRootPath,
2251 PUNICODE_STRING DestinationArcPath,
2252 UCHAR PartitionType)
2253 {
2254 #ifdef __REACTOS__
2255 NTSTATUS Status;
2256 BOOLEAN DoesFreeLdrExist;
2257 WCHAR SrcPath[MAX_PATH];
2258 WCHAR DstPath[MAX_PATH];
2259
2260 /* FAT or FAT32 partition */
2261 DPRINT("System path: '%wZ'\n", SystemRootPath);
2262
2263 /* Copy FreeLoader to the system partition */
2264 wcscpy(SrcPath, SourceRootPath->Buffer);
2265 wcscat(SrcPath, L"\\loader\\freeldr.sys");
2266 wcscpy(DstPath, SystemRootPath->Buffer);
2267 wcscat(DstPath, L"\\freeldr.sys");
2268
2269 DPRINT("Copy: %S ==> %S\n", SrcPath, DstPath);
2270 Status = SetupCopyFile(SrcPath, DstPath);
2271 if (!NT_SUCCESS(Status))
2272 {
2273 DPRINT1("SetupCopyFile() failed (Status %lx)\n", Status);
2274 return Status;
2275 }
2276
2277 /* Prepare for possibly copying 'freeldr.ini' */
2278 wcscpy(DstPath, SystemRootPath->Buffer);
2279 wcscat(DstPath, L"\\freeldr.ini");
2280
2281 DoesFreeLdrExist = DoesFileExist(SystemRootPath->Buffer, L"freeldr.ini");
2282 if (DoesFreeLdrExist)
2283 {
2284 /* Update existing 'freeldr.ini' */
2285 DPRINT1("Update existing 'freeldr.ini'\n");
2286
2287 Status = UpdateFreeLoaderIni(DstPath, DestinationArcPath->Buffer);
2288 if (!NT_SUCCESS(Status))
2289 {
2290 DPRINT1("UpdateFreeLoaderIni() failed (Status %lx)\n", Status);
2291 return Status;
2292 }
2293 }
2294
2295 /* Check for NT and other bootloaders */
2296
2297 // FIXME: Check for Vista+ bootloader!
2298 if (DoesFileExist(SystemRootPath->Buffer, L"ntldr") == TRUE ||
2299 DoesFileExist(SystemRootPath->Buffer, L"boot.ini") == TRUE)
2300 {
2301 /* Search root directory for 'ntldr' and 'boot.ini' */
2302 DPRINT1("Found Microsoft Windows NT/2000/XP boot loader\n");
2303
2304 /* Create or update 'freeldr.ini' */
2305 if (DoesFreeLdrExist == FALSE)
2306 {
2307 /* Create new 'freeldr.ini' */
2308 DPRINT1("Create new 'freeldr.ini'\n");
2309 // wcscpy(DstPath, SystemRootPath->Buffer);
2310 // wcscat(DstPath, L"\\freeldr.ini");
2311
2312 Status = CreateFreeLoaderIniForReactOS(DstPath, DestinationArcPath->Buffer);
2313 if (!NT_SUCCESS(Status))
2314 {
2315 DPRINT1("CreateFreeLoaderIniForReactOS() failed (Status %lx)\n", Status);
2316 return Status;
2317 }
2318
2319 /* Install new bootcode into a file */
2320 wcscpy(DstPath, SystemRootPath->Buffer);
2321 wcscat(DstPath, L"\\bootsect.ros");
2322
2323 if (PartitionType == PARTITION_FAT32 ||
2324 PartitionType == PARTITION_FAT32_XINT13)
2325 {
2326 /* Install FAT32 bootcode */
2327 wcscpy(SrcPath, SourceRootPath->Buffer);
2328 wcscat(SrcPath, L"\\loader\\fat32.bin");
2329
2330 DPRINT1("Install FAT32 bootcode: %S ==> %S\n", SrcPath, DstPath);
2331 Status = InstallFat32BootCodeToFile(SrcPath, DstPath,
2332 SystemRootPath->Buffer);
2333 if (!NT_SUCCESS(Status))
2334 {
2335 DPRINT1("InstallFat32BootCodeToFile() failed (Status %lx)\n", Status);
2336 return Status;
2337 }
2338 }
2339 else
2340 {
2341 /* Install FAT16 bootcode */
2342 wcscpy(SrcPath, SourceRootPath->Buffer);
2343 wcscat(SrcPath, L"\\loader\\fat.bin");
2344
2345 DPRINT1("Install FAT bootcode: %S ==> %S\n", SrcPath, DstPath);
2346 Status = InstallFat16BootCodeToFile(SrcPath, DstPath,
2347 SystemRootPath->Buffer);
2348 if (!NT_SUCCESS(Status))
2349 {
2350 DPRINT1("InstallFat16BootCodeToFile() failed (Status %lx)\n", Status);
2351 return Status;
2352 }
2353 }
2354 }
2355
2356 /* Update 'boot.ini' */
2357 wcscpy(DstPath, SystemRootPath->Buffer);
2358 wcscat(DstPath, L"\\boot.ini");
2359
2360 DPRINT1("Update 'boot.ini': %S\n", DstPath);
2361 Status = UpdateBootIni(DstPath,
2362 L"C:\\bootsect.ros",
2363 L"\"ReactOS\"");
2364 if (!NT_SUCCESS(Status))
2365 {
2366 DPRINT1("UpdateBootIni() failed (Status %lx)\n", Status);
2367 return Status;
2368 }
2369 }
2370 else
2371 {
2372 /* Non-NT bootloaders: install our own bootloader */
2373
2374 PWCHAR Section;
2375 PWCHAR Description;
2376 PWCHAR BootDrive;
2377 PWCHAR BootPartition;
2378 PWCHAR BootSector;
2379 PWCHAR BootSectorFileName;
2380
2381 if (DoesFileExist(SystemRootPath->Buffer, L"io.sys") == TRUE ||
2382 DoesFileExist(SystemRootPath->Buffer, L"msdos.sys") == TRUE)
2383 {
2384 /* Search for root directory for 'io.sys' and 'msdos.sys' */
2385 DPRINT1("Found Microsoft DOS or Windows 9x boot loader\n");
2386
2387 Section = L"DOS";
2388 Description = L"\"DOS/Windows\"";
2389 BootDrive = L"hd0";
2390 BootPartition = L"1";
2391 BootSector = L"BOOTSECT.DOS";
2392
2393 BootSectorFileName = L"\\bootsect.dos";
2394 }
2395 else
2396 if (DoesFileExist(SystemRootPath->Buffer, L"kernel.sys") == TRUE)
2397 {
2398 /* Search for root directory for 'kernel.sys' */
2399 DPRINT1("Found FreeDOS boot loader\n");
2400
2401 Section = L"DOS";
2402 Description = L"\"FreeDOS\"";
2403 BootDrive = L"hd0";
2404 BootPartition = L"1";
2405 BootSector = L"BOOTSECT.DOS";
2406
2407 BootSectorFileName = L"\\bootsect.dos";
2408 }
2409 else
2410 {
2411 /* No or unknown boot loader */
2412 DPRINT1("No or unknown boot loader found\n");
2413
2414 Section = L"Unknown";
2415 Description = L"\"Unknown Operating System\"";
2416 BootDrive = L"hd0";
2417 BootPartition = L"1";
2418 BootSector = L"BOOTSECT.OLD";
2419
2420 BootSectorFileName = L"\\bootsect.old";
2421 }
2422
2423 /* Create or update 'freeldr.ini' */
2424 if (DoesFreeLdrExist == FALSE)
2425 {
2426 /* Create new 'freeldr.ini' */
2427 DPRINT1("Create new 'freeldr.ini'\n");
2428 // wcscpy(DstPath, SystemRootPath->Buffer);
2429 // wcscat(DstPath, L"\\freeldr.ini");
2430
2431 if (IsThereAValidBootSector(SystemRootPath->Buffer))
2432 {
2433 Status = CreateFreeLoaderIniForReactOSAndBootSector(
2434 DstPath, DestinationArcPath->Buffer,
2435 Section, Description,
2436 BootDrive, BootPartition, BootSector);
2437 if (!NT_SUCCESS(Status))
2438 {
2439 DPRINT1("CreateFreeLoaderIniForReactOSAndBootSector() failed (Status %lx)\n", Status);
2440 return Status;
2441 }
2442
2443 /* Save current bootsector */
2444 wcscpy(DstPath, SystemRootPath->Buffer);
2445 wcscat(DstPath, BootSectorFileName);
2446
2447 DPRINT1("Save bootsector: %S ==> %S\n", SystemRootPath->Buffer, DstPath);
2448 Status = SaveBootSector(SystemRootPath->Buffer, DstPath, SECTORSIZE);
2449 if (!NT_SUCCESS(Status))
2450 {
2451 DPRINT1("SaveBootSector() failed (Status %lx)\n", Status);
2452 return Status;
2453 }
2454 }
2455 else
2456 {
2457 Status = CreateFreeLoaderIniForReactOS(DstPath, DestinationArcPath->Buffer);
2458 if (!NT_SUCCESS(Status))
2459 {
2460 DPRINT1("CreateFreeLoaderIniForReactOS() failed (Status %lx)\n", Status);
2461 return Status;
2462 }
2463 }
2464
2465 /* Install new bootsector on the disk */
2466 if (PartitionType == PARTITION_FAT32 ||
2467 PartitionType == PARTITION_FAT32_XINT13)
2468 {
2469 /* Install FAT32 bootcode */
2470 wcscpy(SrcPath, SourceRootPath->Buffer);
2471 wcscat(SrcPath, L"\\loader\\fat32.bin");
2472
2473 DPRINT1("Install FAT32 bootcode: %S ==> %S\n", SrcPath, SystemRootPath->Buffer);
2474 Status = InstallFat32BootCodeToDisk(SrcPath, SystemRootPath->Buffer);
2475 if (!NT_SUCCESS(Status))
2476 {
2477 DPRINT1("InstallFat32BootCodeToDisk() failed (Status %lx)\n", Status);
2478 return Status;
2479 }
2480 }
2481 else
2482 {
2483 /* Install FAT16 bootcode */
2484 wcscpy(SrcPath, SourceRootPath->Buffer);
2485 wcscat(SrcPath, L"\\loader\\fat.bin");
2486
2487 DPRINT1("Install FAT16 bootcode: %S ==> %S\n", SrcPath, SystemRootPath->Buffer);
2488 Status = InstallFat16BootCodeToDisk(SrcPath, SystemRootPath->Buffer);
2489 if (!NT_SUCCESS(Status))
2490 {
2491 DPRINT1("InstallFat16BootCodeToDisk() failed (Status %lx)\n", Status);
2492 return Status;
2493 }
2494 }
2495 }
2496 }
2497
2498 return STATUS_SUCCESS;
2499 #else
2500 return STATUS_NOT_IMPLEMENTED;
2501 #endif
2502 }
2503
2504 static
2505 NTSTATUS
2506 InstallExt2BootcodeToPartition(
2507 PUNICODE_STRING SystemRootPath,
2508 PUNICODE_STRING SourceRootPath,
2509 PUNICODE_STRING DestinationArcPath,
2510 UCHAR PartitionType)
2511 {
2512 #ifdef __REACTOS__
2513 NTSTATUS Status;
2514 BOOLEAN DoesFreeLdrExist;
2515 WCHAR SrcPath[MAX_PATH];
2516 WCHAR DstPath[MAX_PATH];
2517
2518 /* EXT2 partition */
2519 DPRINT("System path: '%wZ'\n", SystemRootPath);
2520
2521 /* Copy FreeLoader to the system partition */
2522 wcscpy(SrcPath, SourceRootPath->Buffer);
2523 wcscat(SrcPath, L"\\loader\\freeldr.sys");
2524 wcscpy(DstPath, SystemRootPath->Buffer);
2525 wcscat(DstPath, L"\\freeldr.sys");
2526
2527 DPRINT("Copy: %S ==> %S\n", SrcPath, DstPath);
2528 Status = SetupCopyFile(SrcPath, DstPath);
2529 if (!NT_SUCCESS(Status))
2530 {
2531 DPRINT1("SetupCopyFile() failed (Status %lx)\n", Status);
2532 return Status;
2533 }
2534
2535 /* Prepare for possibly copying 'freeldr.ini' */
2536 wcscpy(DstPath, SystemRootPath->Buffer);
2537 wcscat(DstPath, L"\\freeldr.ini");
2538
2539 DoesFreeLdrExist = DoesFileExist(SystemRootPath->Buffer, L"freeldr.ini");
2540 if (DoesFreeLdrExist)
2541 {
2542 /* Update existing 'freeldr.ini' */
2543 DPRINT1("Update existing 'freeldr.ini'\n");
2544
2545 Status = UpdateFreeLoaderIni(DstPath, DestinationArcPath->Buffer);
2546 if (!NT_SUCCESS(Status))
2547 {
2548 DPRINT1("UpdateFreeLoaderIni() failed (Status %lx)\n", Status);
2549 return Status;
2550 }
2551 }
2552
2553 /* Check for *nix bootloaders */
2554
2555 /* Create or update 'freeldr.ini' */
2556 if (DoesFreeLdrExist == FALSE)
2557 {
2558 /* Create new 'freeldr.ini' */
2559 DPRINT1("Create new 'freeldr.ini'\n");
2560 wcscpy(DstPath, SystemRootPath->Buffer);
2561 wcscat(DstPath, L"\\freeldr.ini");
2562
2563 /* Certainly SysLinux, GRUB, LILO... or an unknown boot loader */
2564 DPRINT1("*nix or unknown boot loader found\n");
2565
2566 if (IsThereAValidBootSector(SystemRootPath->Buffer))
2567 {
2568 Status = CreateFreeLoaderIniForReactOSAndBootSector(
2569 DstPath, DestinationArcPath->Buffer,
2570 L"Linux", L"\"Linux\"",
2571 L"hd0", L"1", L"BOOTSECT.OLD");
2572 if (!NT_SUCCESS(Status))
2573 {
2574 DPRINT1("CreateFreeLoaderIniForReactOSAndBootSector() failed (Status %lx)\n", Status);
2575 return Status;
2576 }
2577
2578 /* Save current bootsector */
2579 wcscpy(DstPath, SystemRootPath->Buffer);
2580 wcscat(DstPath, L"\\bootsect.old");
2581
2582 DPRINT1("Save bootsector: %S ==> %S\n", SystemRootPath->Buffer, DstPath);
2583 Status = SaveBootSector(SystemRootPath->Buffer, DstPath, sizeof(EXT2_BOOTSECTOR));
2584 if (!NT_SUCCESS(Status))
2585 {
2586 DPRINT1("SaveBootSector() failed (Status %lx)\n", Status);
2587 return Status;
2588 }
2589 }
2590 else
2591 {
2592 Status = CreateFreeLoaderIniForReactOS(DstPath, DestinationArcPath->Buffer);
2593 if (!NT_SUCCESS(Status))
2594 {
2595 DPRINT1("CreateFreeLoaderIniForReactOS() failed (Status %lx)\n", Status);
2596 return Status;
2597 }
2598 }
2599
2600 /* Install new bootsector on the disk */
2601 // if (PartitionType == PARTITION_EXT2)
2602 {
2603 /* Install EXT2 bootcode */
2604 wcscpy(SrcPath, SourceRootPath->Buffer);
2605 wcscat(SrcPath, L"\\loader\\ext2.bin");
2606
2607 DPRINT1("Install EXT2 bootcode: %S ==> %S\n", SrcPath, SystemRootPath->Buffer);
2608 Status = InstallExt2BootCodeToDisk(SrcPath, SystemRootPath->Buffer);
2609 if (!NT_SUCCESS(Status))
2610 {
2611 DPRINT1("InstallExt2BootCodeToDisk() failed (Status %lx)\n", Status);
2612 return Status;
2613 }
2614 }
2615 }
2616
2617 return STATUS_SUCCESS;
2618 #else
2619 return STATUS_NOT_IMPLEMENTED;
2620 #endif
2621 }
2622
2623
2624 NTSTATUS
2625 InstallVBRToPartition(
2626 PUNICODE_STRING SystemRootPath,
2627 PUNICODE_STRING SourceRootPath,
2628 PUNICODE_STRING DestinationArcPath,
2629 UCHAR PartitionType)
2630 {
2631 switch (PartitionType)
2632 {
2633 case PARTITION_FAT_12:
2634 case PARTITION_FAT_16:
2635 case PARTITION_HUGE:
2636 case PARTITION_XINT13:
2637 case PARTITION_FAT32:
2638 case PARTITION_FAT32_XINT13:
2639 {
2640 return InstallFatBootcodeToPartition(SystemRootPath,
2641 SourceRootPath,
2642 DestinationArcPath,
2643 PartitionType);
2644 }
2645
2646 case PARTITION_EXT2:
2647 {
2648 return InstallExt2BootcodeToPartition(SystemRootPath,
2649 SourceRootPath,
2650 DestinationArcPath,
2651 PartitionType);
2652 }
2653
2654 case PARTITION_IFS:
2655 break;
2656
2657 default:
2658 DPRINT1("PartitionType 0x%02X unknown!\n", PartitionType);
2659 break;
2660 }
2661
2662 return STATUS_UNSUCCESSFUL;
2663 }
2664
2665
2666 NTSTATUS
2667 InstallFatBootcodeToFloppy(
2668 PUNICODE_STRING SourceRootPath,
2669 PUNICODE_STRING DestinationArcPath)
2670 {
2671 #ifdef __REACTOS__
2672 NTSTATUS Status;
2673 UNICODE_STRING FloppyDevice = RTL_CONSTANT_STRING(L"\\Device\\Floppy0");
2674 WCHAR SrcPath[MAX_PATH];
2675 WCHAR DstPath[MAX_PATH];
2676
2677 /* Format the floppy first */
2678 Status = VfatFormat(&FloppyDevice,
2679 FMIFS_FLOPPY,
2680 NULL,
2681 TRUE,
2682 0,
2683 NULL);
2684 if (!NT_SUCCESS(Status))
2685 {
2686 DPRINT1("VfatFormat() failed (Status %lx)\n", Status);
2687 return Status;
2688 }
2689
2690 /* Copy FreeLoader to the boot partition */
2691 wcscpy(SrcPath, SourceRootPath->Buffer);
2692 wcscat(SrcPath, L"\\loader\\freeldr.sys");
2693
2694 wcscpy(DstPath, L"\\Device\\Floppy0\\freeldr.sys");
2695
2696 DPRINT("Copy: %S ==> %S\n", SrcPath, DstPath);
2697 Status = SetupCopyFile(SrcPath, DstPath);
2698 if (!NT_SUCCESS(Status))
2699 {
2700 DPRINT1("SetupCopyFile() failed (Status %lx)\n", Status);
2701 return Status;
2702 }
2703
2704 /* Create new 'freeldr.ini' */
2705 wcscpy(DstPath, L"\\Device\\Floppy0\\freeldr.ini");
2706
2707 DPRINT("Create new 'freeldr.ini'\n");
2708 Status = CreateFreeLoaderIniForReactOS(DstPath, DestinationArcPath->Buffer);
2709 if (!NT_SUCCESS(Status))
2710 {
2711 DPRINT1("CreateFreeLoaderIniForReactOS() failed (Status %lx)\n", Status);
2712 return Status;
2713 }
2714
2715 /* Install FAT12/16 boosector */
2716 wcscpy(SrcPath, SourceRootPath->Buffer);
2717 wcscat(SrcPath, L"\\loader\\fat.bin");
2718
2719 wcscpy(DstPath, L"\\Device\\Floppy0");
2720
2721 DPRINT("Install FAT bootcode: %S ==> %S\n", SrcPath, DstPath);
2722 Status = InstallFat12BootCodeToFloppy(SrcPath, DstPath);
2723 if (!NT_SUCCESS(Status))
2724 {
2725 DPRINT1("InstallFat16BootCodeToDisk() failed (Status %lx)\n", Status);
2726 return Status;
2727 }
2728
2729 return STATUS_SUCCESS;
2730 #else
2731 return STATUS_NOT_IMPLEMENTED;
2732 #endif
2733 }
2734
2735 /* EOF */