1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
/*
 * COPYRIGHT:       See COPYING.ARM in the top level directory
 * PROJECT:         ReactOS UEFI OS Loader
 * FILE:            boot/environ/app/rosload/rosload.c
 * PURPOSE:         OS Loader Entrypoint
 * PROGRAMMER:      Alex Ionescu (alex.ionescu@reactos.org)
 */

/* INCLUDES ******************************************************************/

#include "rosload.h"

NTSTATUS
OslArchTransferToKernel (
    _In_ PLOADER_PARAMETER_BLOCK LoaderBlock,
    _In_ PVOID KernelEntrypoint
    );

/* DATA VARIABLES ************************************************************/

PLOADER_PARAMETER_BLOCK OslLoaderBlock;
PVOID OslEntryPoint;
PVOID UserSharedAddress;
ULONGLONG ArchXCr0BitsToClear;
ULONGLONG ArchCr4BitsToClear;
BOOLEAN BdDebugAfterExitBootServices;
KDESCRIPTOR OslKernelGdt;
KDESCRIPTOR OslKernelIdt;

ULONG_PTR OslImcHiveHandle;
ULONG_PTR OslMachineHiveHandle;
ULONG_PTR OslElamHiveHandle;
ULONG_PTR OslSystemHiveHandle;

PBL_DEVICE_DESCRIPTOR OslLoadDevice;
PCHAR OslLoadOptions;
PWCHAR OslSystemRoot;

LIST_ENTRY OslFreeMemoryDesctiptorsList;
LIST_ENTRY OslFinalMemoryMap;
LIST_ENTRY OslCoreExtensionSubGroups[2];
LIST_ENTRY OslLoadedFirmwareDriverList;

BL_BUFFER_DESCRIPTOR OslFinalMemoryMapDescriptorsBuffer;

GUID OslApplicationIdentifier;

ULONG OslResetBootStatus;
BOOLEAN OslImcProcessingValid;
ULONG OslFreeMemoryDesctiptorsListSize;
PVOID OslMemoryDescriptorBuffer;

BcdObjectType BlpSbdiCurrentApplicationType;

PRTL_BSD_DATA BsdBootStatusData;

OSL_BSD_ITEM_TABLE_ENTRY OslpBootStatusFields[RtlBsdItemMax] =
{
    {
        FIELD_OFFSET(RTL_BSD_DATA, Version),
        sizeof(&BsdBootStatusData->Version)
    },  // RtlBsdItemVersionNumber
    {
        FIELD_OFFSET(RTL_BSD_DATA, ProductType),
        sizeof(&BsdBootStatusData->ProductType)
    },  // RtlBsdItemProductType
    {
        FIELD_OFFSET(RTL_BSD_DATA, AabEnabled),
        sizeof(&BsdBootStatusData->AabEnabled)
    },  // RtlBsdItemAabEnabled
    {
        FIELD_OFFSET(RTL_BSD_DATA, AabTimeout),
        sizeof(&BsdBootStatusData->AabTimeout)
    },  // RtlBsdItemAabTimeout
    {
        FIELD_OFFSET(RTL_BSD_DATA, LastBootSucceeded),
        sizeof(&BsdBootStatusData->LastBootSucceeded)
    },  // RtlBsdItemBootGood
    {
        FIELD_OFFSET(RTL_BSD_DATA, LastBootShutdown),
        sizeof(&BsdBootStatusData->LastBootShutdown)
    },  // RtlBsdItemBootShutdown
    {
        FIELD_OFFSET(RTL_BSD_DATA, SleepInProgress),
        sizeof(&BsdBootStatusData->SleepInProgress)
    },  // RtlBsdSleepInProgress
    {
        FIELD_OFFSET(RTL_BSD_DATA, PowerTransition),
        sizeof(&BsdBootStatusData->PowerTransition)
    },  // RtlBsdPowerTransition
    {
        FIELD_OFFSET(RTL_BSD_DATA, BootAttemptCount),
        sizeof(&BsdBootStatusData->BootAttemptCount)
    },  // RtlBsdItemBootAttemptCount
    {
        FIELD_OFFSET(RTL_BSD_DATA, LastBootCheckpoint),
        sizeof(&BsdBootStatusData->LastBootCheckpoint)
    },  // RtlBsdItemBootCheckpoint
    {
        FIELD_OFFSET(RTL_BSD_DATA, LastBootId),
        sizeof(&BsdBootStatusData->LastBootId)
    },  // RtlBsdItemBootId
    {
        FIELD_OFFSET(RTL_BSD_DATA, LastSuccessfulShutdownBootId),
        sizeof(&BsdBootStatusData->LastSuccessfulShutdownBootId)
    },  // RtlBsdItemShutdownBootId
    {
        FIELD_OFFSET(RTL_BSD_DATA, LastReportedAbnormalShutdownBootId),
        sizeof(&BsdBootStatusData->LastReportedAbnormalShutdownBootId)
    },  // RtlBsdItemReportedAbnormalShutdownBootId
    {
        FIELD_OFFSET(RTL_BSD_DATA, ErrorInfo),
        sizeof(&BsdBootStatusData->ErrorInfo)
    },  // RtlBsdItemErrorInfo
    {
        FIELD_OFFSET(RTL_BSD_DATA, PowerButtonPressInfo),
        sizeof(&BsdBootStatusData->PowerButtonPressInfo)
    },  // RtlBsdItemPowerButtonPressInfo
    {
        FIELD_OFFSET(RTL_BSD_DATA, Checksum),
        sizeof(&BsdBootStatusData->Checksum)
    },  // RtlBsdItemChecksum
};

ULONG OslBootAttemptCount;
ULONG OslBootCountUpdateRequestForAbort;
ULONG OslBootAttemptMaximum;

ULONG OslBootCountUpdateIncrement;

BOOLEAN OslCurrentBootCheckpoint;
BOOLEAN OslCurrentBootSucceeded;
BOOLEAN OslCurrentBootShutdown;

/* FUNCTIONS *****************************************************************/

VOID
OslFatalErrorEx (
    _In_ ULONG ErrorCode,
    _In_ ULONG Parameter1,
    _In_ ULONG_PTR Parameter2,
    _In_ ULONG_PTR Parameter3
    )
{
    /* For now just do this */
    BlStatusPrint(L"FATAL ERROR IN ROSLOAD: %lx\n", ErrorCode);
}

VOID
OslAbortBoot (
    _In_ NTSTATUS Status
    )
{
    /* For now just do this */
    BlStatusPrint(L"BOOT ABORTED: %lx\n", Status);
}

NTSTATUS
OslBlStatusErrorHandler (
    _In_ ULONG ErrorCode,
    _In_ ULONG Parameter1,
    _In_ ULONG_PTR Parameter2,
    _In_ ULONG_PTR Parameter3,
    _In_ ULONG_PTR Parameter4
    )
{
    /* We only filter error code 4 */
    if (ErrorCode != 4)
    {
        return STATUS_NOT_IMPLEMENTED;
    }

    /* Handle error 4 as a fatal error 3 internally */
    OslFatalErrorEx(3, Parameter1, Parameter2, Parameter3);
    return STATUS_SUCCESS;
}

VOID
OslpSanitizeLoadOptionsString (
    _In_ PWCHAR OptionString,
    _In_ PWCHAR SanitizeString
    )
{
    /* TODO */
    return;
}

VOID
OslpSanitizeStringOptions (
    _In_ PBL_BCD_OPTION BcdOptions
    )
{
    /* TODO */
    return;
}

NTSTATUS
OslpRemoveInternalApplicationOptions (
    VOID
    )
{
    PWCHAR LoadString;
    NTSTATUS Status;

    /* Assume success */
    Status = STATUS_SUCCESS;<--- Status is assigned

    /* Remove attempts to disable integrity checks or ELAM driver load */
    BlRemoveBootOption(BlpApplicationEntry.BcdData, 
                       BcdLibraryBoolean_DisableIntegrityChecks);
    BlRemoveBootOption(BlpApplicationEntry.BcdData,
                       BcdOSLoaderBoolean_DisableElamDrivers);

    /* Get the command-line parameters, if any */
    Status = BlGetBootOptionString(BlpApplicationEntry.BcdData,<--- Status is overwritten
                                   BcdLibraryString_LoadOptionsString,
                                   &LoadString);
    if (NT_SUCCESS(Status))
    {
        /* Conver to upper case */
        _wcsupr(LoadString);

        /* Remove the existing one */
        BlRemoveBootOption(BlpApplicationEntry.BcdData,
                           BcdLibraryString_LoadOptionsString);

        /* Sanitize strings we don't want */
        OslpSanitizeLoadOptionsString(LoadString, L"DISABLE_INTEGRITY_CHECKS");
        OslpSanitizeLoadOptionsString(LoadString, L"NOINTEGRITYCHECKS");
        OslpSanitizeLoadOptionsString(LoadString, L"DISABLEELAMDRIVERS");

        /* Add the sanitized one back */
        Status = BlAppendBootOptionString(&BlpApplicationEntry,
                                          BcdLibraryString_LoadOptionsString,
                                          LoadString);

        /* Free the original BCD one */
        BlMmFreeHeap(LoadString);
    }

    /* One more pass for secure-boot options */
    OslpSanitizeStringOptions(BlpApplicationEntry.BcdData);

    /* All good */
    return Status;
}

NTSTATUS
OslpCheckForcedFailure (
    VOID
    )
{
    ULONG64 ForceReason;
    NTSTATUS Status;

    /* Read the option */
    Status = BlGetBootOptionInteger(BlpApplicationEntry.BcdData,
                                    BcdOSLoaderInteger_ForceFailure,
                                    &ForceReason);
    if (NT_SUCCESS(Status) && (ForceReason < 4))
    {
        /* For reasons above 3, don't actually do anything */
        if (ForceReason > 3)
        {
            return STATUS_SUCCESS;
        }
    }

    /* If the option isn't there or invalid, always return success */
    return STATUS_SUCCESS;
}

VOID
OslpInitializeBootStatusDataLog (
    VOID
    )
{
    /* TODO */
    return;
}

NTSTATUS
OslpReadWriteBootStatusData (
    _In_ BOOLEAN WriteAccess
    )
{
    /* Are you trying to write? */
    if (WriteAccess)
    {
        /* Have we already read? */
        if (!BsdBootStatusData)
        {
            /* No -- fail */
            return STATUS_UNSUCCESSFUL;
        }
    }
    else if (BsdBootStatusData)
    {
        /* No -- you're trying to read and we already have the data: no-op */
        return STATUS_SUCCESS;
    }

    /* TODO */
    return STATUS_NOT_IMPLEMENTED;
}

NTSTATUS
OslpGetSetBootStatusData (
    _In_ BOOLEAN Read,
    _In_ RTL_BSD_ITEM_TYPE DataClass,
    _Out_ PVOID Buffer,
    _Inout_ PULONG Size
    )
{
    NTSTATUS Status;
    ULONG Length, Offset;

    /* No data has been read yet, fail */
    if (!BsdBootStatusData)
    {
        return STATUS_UNSUCCESSFUL;
    }

    /* Invalid data item, fail */
    if (DataClass >= RtlBsdItemMax)
    {
        return STATUS_INVALID_PARAMETER;
    }

    /* Capture the length and offset */
    Length = OslpBootStatusFields[DataClass].Size;
    Offset = OslpBootStatusFields[DataClass].Offset;

    /* Make sure it doesn't overflow past the structure we've read */
    if ((Length + Offset) > BsdBootStatusData->Version)
    {
        return STATUS_REVISION_MISMATCH;
    }

    /* Make sure we have enough space */
    if (*Size >= Length)
    {
        /* We do -- is this a read? */
        if (Read)
        {
            /* Yes, copy into the caller's buffer */
            RtlCopyMemory(Buffer,
                          (PVOID)((ULONG_PTR)BsdBootStatusData + Offset),
                          Length);
        }
        else
        {
            /* It's a write, copy from caller's buffer */
            RtlCopyMemory((PVOID)((ULONG_PTR)BsdBootStatusData + Offset),
                          Buffer,
                          Length);
        }

        /* Set success */
        Status = STATUS_SUCCESS;
    }
    else
    {
        /* Return size needed and failure code */
        *Size = Length;
        Status = STATUS_BUFFER_TOO_SMALL;
    }

    /* All good */
    return Status;
}

NTSTATUS
OslSetBootStatusData (
    _In_ BOOLEAN LastBootGood,
    _In_ BOOLEAN LastBootShutdown,
    _In_ BOOLEAN LastBootCheckpoint,
    _In_ ULONG UpdateIncrement,
    _In_ ULONG BootAttemptCount
    )
{
    NTSTATUS Status;
    ULONG Size;

    /* Capture the BSD data in our globals, if needed */
    Status = OslpReadWriteBootStatusData(FALSE);
    if (!NT_SUCCESS(Status))
    {
        goto Quickie;
    }

    /* Write last boot shutdown */
    Size = sizeof(LastBootShutdown);
    Status = OslpGetSetBootStatusData(FALSE,
                                      RtlBsdItemBootShutdown,
                                      &LastBootShutdown,
                                      &Size);
    if (!NT_SUCCESS(Status))
    {
        goto Quickie;
    }

    /* Write last boot good */
    Size = sizeof(LastBootGood);
    Status = OslpGetSetBootStatusData(FALSE,
                                      RtlBsdItemBootGood,
                                      &LastBootGood,
                                      &Size);
    if (!NT_SUCCESS(Status))
    {
        goto Quickie;
    }

    /* Write last boot checkpoint */
    Size = sizeof(LastBootCheckpoint);
    Status = OslpGetSetBootStatusData(FALSE,
                                      RtlBsdItemBootCheckpoint,
                                      &LastBootCheckpoint,
                                      &Size);
    if (!NT_SUCCESS(Status))
    {
        goto Quickie;
    }

    /* Write boot attempt count */
    Size = sizeof(BootAttemptCount);
    Status = OslpGetSetBootStatusData(FALSE,
                                      RtlBsdItemBootAttemptCount,
                                      &BootAttemptCount,
                                      &Size);
    if (!NT_SUCCESS(Status))
    {
        goto Quickie;
    }

    /* TODO: Update Boot ID*/

    /* Now write the data */
    Status = OslpReadWriteBootStatusData(TRUE);

Quickie:
    return Status;
}

NTSTATUS
OslGetBootStatusData (
    _Out_ PBOOLEAN LastBootGood,
    _Out_ PBOOLEAN LastBootShutdown,
    _Out_ PBOOLEAN LastBootCheckpoint,
    _Out_ PULONG LastBootId,
    _Out_ PBOOLEAN BootGood,
    _Out_ PBOOLEAN BootShutdown
    )
{
    NTSTATUS Status;
    ULONG Size;
    ULONG64 BootStatusPolicy;
    BOOLEAN localBootShutdown, localBootGood;

    /* Capture the BSD data in our globals, if needed */
    Status = OslpReadWriteBootStatusData(FALSE);
    if (!NT_SUCCESS(Status))
    {
        goto Quickie;
    }

    /* Read the last boot ID */
    Size = sizeof(*LastBootId);
    Status = OslpGetSetBootStatusData(TRUE, RtlBsdItemBootId, LastBootId, &Size);
    if (!NT_SUCCESS(Status))
    {
        /* Set to zero if we couldn't find it */
        *LastBootId = 0;
    }

    /* Get the boot status policy */
    Status = BlGetBootOptionInteger(BlpApplicationEntry.BcdData,
                                    BcdOSLoaderInteger_BootStatusPolicy,
                                    &BootStatusPolicy);
    if (!NT_SUCCESS(Status))
    {
        /* Apply a default if none exists */
        BootStatusPolicy = IgnoreShutdownFailures;
    }

    /* Check if this was a good shutdown */
    Size = sizeof(localBootShutdown);
    Status = OslpGetSetBootStatusData(TRUE,
                                      RtlBsdItemBootShutdown,
                                      &localBootShutdown,
                                      &Size);
    if (!NT_SUCCESS(Status))
    {
        goto Quickie;
    }

    /* Tell the caller */
    *BootShutdown = localBootShutdown;

    /* Check if this was a good boot */
    Size = sizeof(localBootGood);
    Status = OslpGetSetBootStatusData(TRUE,
                                      RtlBsdItemBootGood,
                                      &localBootGood,
                                      &Size);
    if (!NT_SUCCESS(Status))
    {
        goto Quickie;
    }

    /* Tell the caller*/
    *BootGood = localBootGood;

    /* TODO: Additional logic for checkpoints and such */
    Status = STATUS_NOT_IMPLEMENTED;

Quickie:
    return Status;
}

BOOLEAN
OslpAdvancedOptionsRequested (
    VOID
    )
{
    /* TODO */
    return FALSE;
}

NTSTATUS
OslPrepareTarget (
    _Out_ PULONG ReturnFlags,
    _Out_ PBOOLEAN Jump
    )
{
    PGUID AppId;
    NTSTATUS Status;
    PBL_DEVICE_DESCRIPTOR OsDevice;
    PWCHAR SystemRoot;
    SIZE_T RootLength, RootLengthWithSep;
    ULONG i;
    ULONG64 StartPerf, EndPerf;
    RTL_BSD_DATA_POWER_TRANSITION PowerTransitionData;
    PRTL_BSD_DATA_POWER_TRANSITION PowerBuffer;
    ULONG OsDeviceHandle;
    BOOLEAN LastBootGood, LastBootShutdown, LastBootCheckpoint;
    ULONG BootId;
    BOOLEAN BootGood, BootShutdown;
    ULONG BsdSize;

    /* Initialize locals */
    PowerBuffer = NULL;

    /* Assume no flags */
    *ReturnFlags = 0;

    /* Make all registry handles invalid */
    OslImcHiveHandle = -1;
    OslMachineHiveHandle = -1;
    OslElamHiveHandle = -1;
    OslSystemHiveHandle = -1;

    /* Initialize memory lists */
    InitializeListHead(&OslFreeMemoryDesctiptorsList);
    InitializeListHead(&OslFinalMemoryMap);
    InitializeListHead(&OslLoadedFirmwareDriverList);
    for (i = 0; i < RTL_NUMBER_OF(OslCoreExtensionSubGroups); i++)
    {
        InitializeListHead(&OslCoreExtensionSubGroups[i]);
    }

    /* Initialize the memory map descriptor buffer */
    RtlZeroMemory(&OslFinalMemoryMapDescriptorsBuffer,
                  sizeof(OslFinalMemoryMapDescriptorsBuffer));

    /* Initialize general pointers */
    OslLoadDevice = NULL;
    OslLoadOptions = NULL;
    OslSystemRoot = NULL;
    OslLoaderBlock = NULL;
    OslMemoryDescriptorBuffer = NULL;

    /* Initialize general variables */
    OslResetBootStatus = 0;
    OslImcProcessingValid = FALSE;
    OslFreeMemoryDesctiptorsListSize = 0;

    /* Capture the current TSC */
    StartPerf = BlArchGetPerformanceCounter();

    /* Set our application type for SecureBoot/TPM purposes */
    BlpSbdiCurrentApplicationType.Application.ObjectCode =
        BCD_OBJECT_TYPE_APPLICATION;
    BlpSbdiCurrentApplicationType.Application.ImageCode =
        BCD_IMAGE_TYPE_BOOT_APP;
    BlpSbdiCurrentApplicationType.Application.ApplicationCode =
        BCD_APPLICATION_TYPE_OSLOADER;
    BlpSbdiCurrentApplicationType.Application.Reserved = 0;

    /* Register an error handler */
    BlpStatusErrorHandler = OslBlStatusErrorHandler;

    /* Get the application identifier and save it */
    AppId = BlGetApplicationIdentifier();
    if (AppId)
    {
        OslApplicationIdentifier = *AppId;
    }

    /* Enable tracing */
#ifdef BL_ETW_SUPPORT
    TraceLoggingRegister(&TlgOslBootProviderProv);
#endif

    /* Remove dangerous BCD options */
    Status = OslpRemoveInternalApplicationOptions();
    if (!NT_SUCCESS(Status))
    {
        EfiPrintf(L"Fail here: %d\r\n", __LINE__);
        goto Quickie;
    }

    /* Get the OS device */
    Status = BlGetBootOptionDevice(BlpApplicationEntry.BcdData,
                                   BcdOSLoaderDevice_OSDevice,
                                   &OsDevice,
                                   0);
    if (!NT_SUCCESS(Status))
    {
        EfiPrintf(L"Fail here: %d\r\n", __LINE__);
        goto Quickie;
    }

    /* If the OS device is the boot device, use the one provided by bootlib */
    if (OsDevice->DeviceType == BootDevice)
    {
        OsDevice = BlpBootDevice;
    }

    /* Save it as a global for later */
    OslLoadDevice = OsDevice;

    /* Get the system root */
    Status = BlGetBootOptionString(BlpApplicationEntry.BcdData,
                                   BcdOSLoaderString_SystemRoot,
                                   &SystemRoot);
    if (!NT_SUCCESS(Status))
    {
        EfiPrintf(L"Fail here: %d\r\n", __LINE__);
        goto Quickie;
    }

    EfiPrintf(L"System root: %s\r\n", SystemRoot);

    /* Get the system root length and make sure it's slash-terminated */
    RootLength = wcslen(SystemRoot);
    if (SystemRoot[RootLength - 1] == OBJ_NAME_PATH_SEPARATOR)
    {
        /* Perfect, set it */
        OslSystemRoot = SystemRoot;
    }
    else
    {
        /* Allocate a new buffer large enough to contain the slash */
        RootLengthWithSep = RootLength + sizeof(OBJ_NAME_PATH_SEPARATOR);
        OslSystemRoot = BlMmAllocateHeap(RootLengthWithSep * sizeof(WCHAR));
        if (!OslSystemRoot)
        {
            /* Bail out if we're out of memory */
            Status = STATUS_NO_MEMORY;
            EfiPrintf(L"Fail here: %d\r\n", __LINE__);
            goto Quickie;
        }

        /* Make a copy of the path, adding the separator */
        wcscpy(OslSystemRoot, SystemRoot);
        wcscat(OslSystemRoot, L"\\");

        /* Free the original one from the BCD library */
        BlMmFreeHeap(SystemRoot);
    }

    /* Initialize access to the BSD */
    OslpInitializeBootStatusDataLog();

    /* Check if we're supposed to fail on purpose */
    Status = OslpCheckForcedFailure();
    if (!NT_SUCCESS(Status))
    {
        EfiPrintf(L"Fail here: %d\r\n", __LINE__);
        goto Quickie;
    }

    /* Always disable VGA mode */
    Status = BlAppendBootOptionBoolean(&BlpApplicationEntry,
                                       BcdOSLoaderBoolean_DisableVgaMode,
                                       TRUE);
    if (!NT_SUCCESS(Status))
    {
        EfiPrintf(L"Fail here: %d\r\n", __LINE__);
        goto Quickie;
    }

    /* Get telemetry data from the last boot */
    Status = OslGetBootStatusData(&LastBootGood,
                                  &LastBootShutdown,
                                  &LastBootCheckpoint,
                                  &BootId,
                                  &BootGood,
                                  &BootShutdown);
    if (!NT_SUCCESS(Status))
    {
        /* Assume this is the very first boot and everything went well */
        BootId = 0;
        LastBootGood = TRUE;
        LastBootShutdown = TRUE;
        LastBootCheckpoint = TRUE;
        BootGood = TRUE;
        BootShutdown = TRUE;

        /* Set 0 boot attempts */
        OslBootAttemptCount = 0;
    }

    /* Set more attempt variables to their initial state */
    OslResetBootStatus = TRUE;
    OslBootCountUpdateRequestForAbort = 0;

    /* Read the current BSD data into the global buffer */
    Status = OslpReadWriteBootStatusData(FALSE);
    if (NT_SUCCESS(Status))
    {
        /* Get the power transition buffer from the BSD */
        BsdSize = sizeof(PowerTransitionData);
        Status = OslpGetSetBootStatusData(TRUE,
                                          RtlBsdPowerTransition,
                                          &PowerTransitionData,
                                          &BsdSize);
        if (NT_SUCCESS(Status))
        {
            /* Save the buffer */
            PowerBuffer = &PowerTransitionData;
        }
    }

    /* Check if this is VHD boot, which gets 3 boot attempts instead of 2 */
    OslBootAttemptMaximum = 2;
    OslBootAttemptMaximum += BlDeviceIsVirtualPartitionDevice(OslLoadDevice, NULL);

    /* Check if the user wants to see the advanced menu */
    if (!OslpAdvancedOptionsRequested())
    {
        /* The last boot failed more than the maximum */
        if (!(LastBootGood) &&
            (OslBootAttemptCount >= OslBootAttemptMaximum))
        {
            /* Return failure due to boot -- launch recovery */
            *ReturnFlags |= 8;

            /* Update the attempt count and status variables */
            OslBootAttemptCount = OslBootAttemptMaximum - 1;
            OslCurrentBootCheckpoint = LastBootCheckpoint;
            OslCurrentBootSucceeded = FALSE;
            OslCurrentBootShutdown = LastBootShutdown;

            /* Crash with code 15 and abort boot */
            OslFatalErrorEx(15, 0, 0, 0);
            Status = STATUS_UNSUCCESSFUL;
            EfiPrintf(L"Fail here: %d\r\n", __LINE__);
            goto Quickie;
        }

        /* We never made it far enough, more than the maximum */
        if (!(LastBootCheckpoint) &&
            (OslBootAttemptCount >= OslBootAttemptMaximum))
        {
            /* Return crash/dirty shutdown during boot attempt */
            *ReturnFlags |= 0x10;

            /* Update the attempt count and status variables */
            OslBootAttemptCount = OslBootAttemptMaximum - 1;
            OslCurrentBootSucceeded = LastBootGood;
            OslCurrentBootShutdown = LastBootShutdown;
            OslCurrentBootCheckpoint = FALSE;

            /* Crash with code 16 and abort boot */
            OslFatalErrorEx(16, 0, 0, 0);
            Status = STATUS_UNSUCCESSFUL;
            EfiPrintf(L"Fail here: %d\r\n", __LINE__);
            goto Quickie;
        }

        /* We failed to shutdown cleanly, and haven't booted yet */
        if (!(LastBootShutdown) && !(OslBootAttemptCount))
        {
            /* Return crash/dirty shutdown */
            *ReturnFlags |= 0x10;

            /* There's no boot attempt, so only update shutdown variables */
            OslCurrentBootSucceeded = LastBootGood;
            OslCurrentBootShutdown = TRUE;
            OslCurrentBootCheckpoint = LastBootCheckpoint;

            /* Crash with code 16 and abort boot */
            OslFatalErrorEx(16, 0, 0, 0);
            Status = STATUS_UNSUCCESSFUL;
            EfiPrintf(L"Fail here: %d\r\n", __LINE__);
            goto Quickie;
        }
    }

    /* Officially increment the number of boot attempts */
    OslBootAttemptCount++;

    /* No success yet, write to boot status file */
    OslCurrentBootCheckpoint = FALSE;
    OslCurrentBootSucceeded = FALSE;
    OslCurrentBootShutdown = FALSE;
    OslSetBootStatusData(FALSE,
                         FALSE,
                         FALSE,
                         OslBootCountUpdateIncrement,
                         OslBootAttemptCount);

    /* Open the OS Loader Device for Read/Write access */
    Status = BlpDeviceOpen(OslLoadDevice,
                           BL_DEVICE_READ_ACCESS | BL_DEVICE_WRITE_ACCESS,
                           0,
                           &OsDeviceHandle);
    if (!NT_SUCCESS(Status))
    {
        EfiPrintf(L"Fail here: %d\r\n", __LINE__);
        goto Quickie;
    }

    /* That's all for now, folks */
    Status = STATUS_NOT_IMPLEMENTED;
    DBG_UNREFERENCED_LOCAL_VARIABLE(PowerBuffer);

    /* Printf perf */
    EndPerf = BlArchGetPerformanceCounter();
    EfiPrintf(L"Delta: %lld\r\n", EndPerf - StartPerf);

Quickie:
#if BL_BITLOCKER_SUPPORT
    /* Destroy the RNG/AES library for BitLocker */
    SymCryptRngAesUninstantiate();
#endif

    /* Abort the boot */
    OslAbortBoot(Status);

    /* This is a failure path, so never do the jump */
    *Jump = FALSE;

    /* Return error code */
    return Status;
}

NTSTATUS
OslFwpKernelSetupPhase1 (
    _In_ PLOADER_PARAMETER_BLOCK LoaderBlock
    )
{
    return STATUS_NOT_IMPLEMENTED;
}

NTSTATUS
OslArchpKernelSetupPhase0 (
    _In_ PLOADER_PARAMETER_BLOCK LoaderBlock
    )
{
    return STATUS_NOT_IMPLEMENTED;
}

VOID
ArchRestoreProcessorFeatures (
    VOID
    )
{
    /* Any XCR0 bits to clear? */
    if (ArchXCr0BitsToClear)
    {
        /* Clear them */
#if defined(_MSC_VER) && !defined(__clang__) && !defined(_M_ARM)
        __xsetbv(0, __xgetbv(0) & ~ArchXCr0BitsToClear);
#endif
        ArchXCr0BitsToClear = 0;
    }

    /* Any CR4 bits to clear? */
    if (ArchCr4BitsToClear)
    {
        /* Clear them */
#if !defined(_M_ARM)
        __writecr4(__readcr4() & ~ArchCr4BitsToClear);
#endif
        ArchCr4BitsToClear = 0;
    }
}

NTSTATUS
OslArchKernelSetup (
    _In_ ULONG Phase,
    _In_ PLOADER_PARAMETER_BLOCK LoaderBlock
    )
{
    /* For phase 0, do architectural setup */
    if (Phase == 0)
    {
        return OslArchpKernelSetupPhase0(LoaderBlock);
    }

    /* Nothing to do for Phase 1 */
    if (Phase == 1)
    {
        return STATUS_SUCCESS;
    }

    /* Relocate the self map */
    BlMmRelocateSelfMap();

    /* Zero out the HAL Heap */
    BlMmZeroVirtualAddressRange((PVOID)MM_HAL_VA_START,
                                MM_HAL_VA_END - MM_HAL_VA_START + 1);

    /* Move shared user data in its place */
    BlMmMoveVirtualAddressRange((PVOID)KI_USER_SHARED_DATA,
                                UserSharedAddress,
                                PAGE_SIZE);

    /* Clear XCR0/CR4 CPU features that should be disabled before boot */
    ArchRestoreProcessorFeatures();

    /* Good to go */
    return STATUS_SUCCESS;
}

NTSTATUS
OslExecuteTransition (
    VOID
    )
{
    NTSTATUS Status;

    /* Is the debugger meant to be kept enabled throughout the boot phase? */
    if (!BdDebugAfterExitBootServices)
    {
#ifdef BL_KD_SUPPORT
        /* No -- disable it */
        BlBdStop();
#endif
    }

    /* Setup Firmware for Phase 1 */
    Status = OslFwpKernelSetupPhase1(OslLoaderBlock);
    if (NT_SUCCESS(Status))
    {
        /* Setup kernel for Phase 2 */
        Status = OslArchKernelSetup(2, OslLoaderBlock);
        if (NT_SUCCESS(Status))
        {
#ifdef BL_KD_SUPPORT
            /* Stop the boot debugger */
            BlBdStop();
#endif
            /* Jump to the kernel entrypoint */
            OslArchTransferToKernel(OslLoaderBlock, OslEntryPoint);

            /* Infinite loop if we got here */
            for (;;);
        }
    }

    /* Return back with the failure code */
    return Status;
}

NTSTATUS
OslpMain (
    _Out_ PULONG ReturnFlags
    )
{
    NTSTATUS Status;
    BOOLEAN ExecuteJump;
#if !defined(_M_ARM)
    CPU_INFO CpuInfo;
    BOOLEAN NxEnabled;
    LARGE_INTEGER MiscMsr;

    /* Check if the CPU supports NX */
    BlArchCpuId(0x80000001, 0, &CpuInfo);
    if (!(CpuInfo.Edx & 0x10000))
    {
        /* It doesn't, check if this is Intel */
        EfiPrintf(L"NX disabled: %lx\r\n", CpuInfo.Edx);
        if (BlArchGetCpuVendor() == CPU_INTEL)
        {
            /* Then turn off the MSR disable feature for it, enabling NX */
            MiscMsr.QuadPart = __readmsr(MSR_IA32_MISC_ENABLE);
            EfiPrintf(L"NX being turned on: %llx\r\n", MiscMsr.QuadPart);
            MiscMsr.HighPart &= MSR_XD_ENABLE_MASK;
            MiscMsr.QuadPart = __readmsr(MSR_IA32_MISC_ENABLE);
            __writemsr(MSR_IA32_MISC_ENABLE, MiscMsr.QuadPart);
            NxEnabled = TRUE;
        }
    }

    /* Turn on NX support with the CPU-generic MSR */
    __writemsr(MSR_EFER, __readmsr(MSR_EFER) | MSR_NXE);

#endif

    /* Load the kernel */
    Status = OslPrepareTarget(ReturnFlags, &ExecuteJump);
    if (NT_SUCCESS(Status) && (ExecuteJump))
    {
        /* Jump to the kernel */
        Status = OslExecuteTransition();
    }

#if !defined(_M_ARM)
    /* Retore NX support */
    __writemsr(MSR_EFER, __readmsr(MSR_EFER) ^ MSR_NXE);

    /* Did we manually enable NX? */
    if (NxEnabled)<--- Uninitialized variable: NxEnabled
    {
        /* Turn it back off */
        MiscMsr.QuadPart = __readmsr(MSR_IA32_MISC_ENABLE);
        MiscMsr.HighPart |= ~MSR_XD_ENABLE_MASK;
        __writemsr(MSR_IA32_MISC_ENABLE, MiscMsr.QuadPart);
    }

#endif
    /* Go back */
    return Status;
}

/*++
 * @name OslMain
 *
 *     The OslMain function implements the Windows Boot Application entrypoint for
 *     the OS Loader.
 *
 * @param  BootParameters
 *         Pointer to the Boot Application Parameter Block.
 *
 * @return NT_SUCCESS if the image was loaded correctly, relevant error code
 *         otherwise.
 *
 *--*/
NTSTATUS
NTAPI
OslMain (
    _In_ PBOOT_APPLICATION_PARAMETER_BLOCK BootParameters
    )
{
    BL_LIBRARY_PARAMETERS LibraryParameters;
    NTSTATUS Status;
    PBL_RETURN_ARGUMENTS ReturnArguments;
    PBL_APPLICATION_ENTRY AppEntry;
    CPU_INFO CpuInfo;<--- Unused variable: CpuInfo
    ULONG Flags;

    /* Get the return arguments structure, and set our version */
    ReturnArguments = (PBL_RETURN_ARGUMENTS)((ULONG_PTR)BootParameters +
                                             BootParameters->ReturnArgumentsOffset);
    ReturnArguments->Version = BL_RETURN_ARGUMENTS_VERSION;

    /* Get the application entry, and validate it */
    AppEntry = (PBL_APPLICATION_ENTRY)((ULONG_PTR)BootParameters +
                                       BootParameters->AppEntryOffset);
    if (!RtlEqualMemory(AppEntry->Signature,
                        BL_APP_ENTRY_SIGNATURE,
                        sizeof(AppEntry->Signature)))
    {
        /* Unrecognized, bail out */
        Status = STATUS_INVALID_PARAMETER_9;
        goto Quickie;
    }

#if !defined(_M_ARM)
    /* Check if CPUID 01h is supported */
    if (BlArchIsCpuIdFunctionSupported(1))
    {
        /* Query CPU features */
        BlArchCpuId(1, 0, &CpuInfo);

        /* Check if PAE is supported */
        if (CpuInfo.Edx & 0x40)
        {
            EfiPrintf(L"PAE Supported, but won't be used\r\n");
        }
    }
#endif

    /* Setup the boot library parameters for this application */
    BlSetupDefaultParameters(&LibraryParameters);
    LibraryParameters.TranslationType = BlVirtual;
    LibraryParameters.LibraryFlags = BL_LIBRARY_FLAG_ZERO_HEAP_ALLOCATIONS_ON_FREE |
                                     BL_LIBRARY_FLAG_REINITIALIZE_ALL;
    LibraryParameters.MinimumAllocationCount = 1024;
    LibraryParameters.MinimumHeapSize = 2 * 1024 * 1024;
    LibraryParameters.HeapAllocationAttributes = BlMemoryKernelRange;
    LibraryParameters.FontBaseDirectory = L"\\Reactos\\Boot\\Fonts";
    LibraryParameters.DescriptorCount = 512;

    /* Initialize the boot library */
    Status = BlInitializeLibrary(BootParameters, &LibraryParameters);
    if (NT_SUCCESS(Status))
    {
        /* For testing, draw the logo */
        OslDrawLogo();

        /* Call the main routine */
        Status = OslpMain(&Flags);

        /* Return the flags, and destroy the boot library */
        ReturnArguments->Flags = Flags;
        BlDestroyLibrary();
    }

Quickie:
    /* Return back to boot manager */
    ReturnArguments->Status = Status;
    return Status;
}