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
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
/*
 * COPYRIGHT:       See COPYING in the top level directory
 * PROJECT:         ReactOS FS utility tool
 * FILE:            modules/rosapps/drivers/vcdrom/vcdrom.c
 * PURPOSE:         Virtual CD-ROM class driver
 * PROGRAMMERS:     Pierre Schweitzer <pierre@reactos.org>
 */

#include <ntddk.h>
#include <ntifs.h>
#include <ntdddisk.h>
#include <ntddcdrm.h>
#include <pseh/pseh2.h>

#define NDEBUG
#include <debug.h>

#include "vcdioctl.h"

typedef struct _DEVICE_EXTENSION
{
    PDEVICE_OBJECT DeviceObject;
    /* File object to the ISO when opened */
    PFILE_OBJECT VolumeObject;
    /* Size of the ISO */
    LARGE_INTEGER VolumeSize;
    /* Mandatory change count for verify */
    ULONG ChangeCount;
    /* Will contain the device name or the drive letter */
    UNICODE_STRING GlobalName;
    /* Will contain the image path */
    UNICODE_STRING ImageName;
    /* Flags on mount (supp. of UDF/Joliet) */
    ULONG Flags;
    /* Faking CD structure */
    ULONG SectorSize;
    ULONG SectorShift;
} DEVICE_EXTENSION, *PDEVICE_EXTENSION;

/* Taken from CDFS */
#define TOC_DATA_TRACK 0x04
#define TOC_LAST_TRACK 0xaa

/* Should cover most of our usages */
#define DEFAULT_STRING_SIZE 50

/* Increment on device creation, protection by the mutex */
FAST_MUTEX ViMutex;
ULONG ViDevicesCount;

VOID
ViInitializeDeviceExtension(PDEVICE_OBJECT DeviceObject)
{
    PDEVICE_EXTENSION DeviceExtension;

    DeviceExtension = DeviceObject->DeviceExtension;

    /* Zero mandatory fields, the rest will be zeroed when needed */
    DeviceExtension->DeviceObject = DeviceObject;
    DeviceExtension->VolumeObject = NULL;
    DeviceExtension->ChangeCount = 0;
    DeviceExtension->GlobalName.Buffer = NULL;
    DeviceExtension->GlobalName.MaximumLength = 0;
    DeviceExtension->GlobalName.Length = 0;
    DeviceExtension->ImageName.Buffer = NULL;
    DeviceExtension->ImageName.MaximumLength = 0;
    DeviceExtension->ImageName.Length = 0;
}

NTSTATUS
ViAllocateUnicodeString(USHORT BufferLength, PUNICODE_STRING UnicodeString)
{
    PVOID Buffer;

    /* Allocate the buffer */
    Buffer = ExAllocatePoolWithTag(NonPagedPool, BufferLength, ' dCV');
    /* Initialize */
    UnicodeString->Length = 0;
    UnicodeString->MaximumLength = BufferLength;
    UnicodeString->Buffer = Buffer;

    /* Return success if it went fine */
    if (Buffer != NULL)
    {
        return STATUS_SUCCESS;
    }

    return STATUS_NO_MEMORY;
}

VOID
ViFreeUnicodeString(PUNICODE_STRING UnicodeString)
{
    /* Only free if allocate, that allows using this
     * on cleanup in short memory situations
     */
    if (UnicodeString->Buffer != NULL)
    {
        ExFreePoolWithTag(UnicodeString->Buffer, 0);
        UnicodeString->Buffer = NULL;
    }

    /* Zero the rest */
    UnicodeString->Length = 0;
    UnicodeString->MaximumLength = 0;
}

NTSTATUS
ViSetIoStatus(NTSTATUS Status, ULONG_PTR Information, PIRP Irp)
{
    /* Only set what we got */
    Irp->IoStatus.Information = Information;
    Irp->IoStatus.Status = Status;

    /* And return the status, so that caller can return with us */
    return Status;
}

NTSTATUS
NTAPI
VcdHandle(PDEVICE_OBJECT DeviceObject, PIRP Irp)
{
    /* Stub for CREATE, CLEANUP, CLOSE, always a succes */
    ViSetIoStatus(STATUS_SUCCESS, 0, Irp);
    IoCompleteRequest(Irp, IO_DISK_INCREMENT);

    return STATUS_SUCCESS;
}

NTSTATUS
ViDeleteDevice(PDEVICE_OBJECT DeviceObject, PIRP Irp)
{
    NTSTATUS Status;
    PDEVICE_EXTENSION DeviceExtension;

    DeviceExtension = DeviceObject->DeviceExtension;

    /* Delete the drive letter */
    Status = IoDeleteSymbolicLink(&DeviceExtension->GlobalName);
    ViFreeUnicodeString(&DeviceExtension->GlobalName);

    /* Close the ISO */
    if (DeviceExtension->VolumeObject != NULL)
    {
        ObDereferenceObject(DeviceExtension->VolumeObject);
        DeviceExtension->VolumeObject = NULL;
    }

    /* Free the ISO name */
    ViFreeUnicodeString(&DeviceExtension->ImageName);

    /* And delete the device */
    IoDeleteDevice(DeviceObject);
    return ViSetIoStatus(Status, 0, Irp);
}

VOID
NTAPI
VcdUnload(PDRIVER_OBJECT DriverObject)
{
    IRP FakeIrp;
    PDEVICE_OBJECT DeviceObject;

    /* No device, nothing to free */
    DeviceObject = DriverObject->DeviceObject;
    if (DeviceObject == NULL)
    {
        return;
    }

    /* Delete any device we could have created */
    do
    {
        PDEVICE_OBJECT NextDevice;

        NextDevice = DeviceObject->NextDevice;
        /* This is normally called on IoCtl, so fake
         * the IRP so that status can be dummily set
         */
        ViDeleteDevice(DeviceObject, &FakeIrp);
        DeviceObject = NextDevice;
    } while (DeviceObject != NULL);
}

NTSTATUS
ViCreateDriveLetter(PDRIVER_OBJECT DriverObject, WCHAR Letter, WCHAR *EffectiveLetter, PDEVICE_OBJECT *DeviceObject)
{
    NTSTATUS Status;
    HANDLE LinkHandle;
    WCHAR DriveLetter[3], CurLetter;
    PDEVICE_OBJECT LocalDeviceObject;
    PDEVICE_EXTENSION DeviceExtension;
    OBJECT_ATTRIBUTES ObjectAttributes;
    UNICODE_STRING DeviceCount, DeviceName;

    *DeviceObject = NULL;

    /* Allocate our buffers */
    ViAllocateUnicodeString(DEFAULT_STRING_SIZE, &DeviceCount);
    ViAllocateUnicodeString(DEFAULT_STRING_SIZE, &DeviceName);

    /* For easier cleanup */
    _SEH2_TRY
    {
        /* Get our device number */
        ExAcquireFastMutex(&ViMutex);
        Status = RtlIntegerToUnicodeString(ViDevicesCount, 10, &DeviceCount);
        ++ViDevicesCount;
        ExReleaseFastMutex(&ViMutex);

        /* Conversion to string failed, bail out */
        if (!NT_SUCCESS(Status))
        {
            _SEH2_LEAVE;
        }

        /* Create our device name */
        Status = RtlAppendUnicodeToString(&DeviceName, L"\\Device\\VirtualCdRom");
        if (!NT_SUCCESS(Status))
        {
            _SEH2_LEAVE;
        }

        Status = RtlAppendUnicodeStringToString(&DeviceName, &DeviceCount);
        if (!NT_SUCCESS(Status))
        {
            _SEH2_LEAVE;
        }

        /* And create the device! */
        Status = IoCreateDevice(DriverObject, sizeof(DEVICE_EXTENSION), &DeviceName,
                                FILE_DEVICE_CD_ROM,
                                FILE_READ_ONLY_DEVICE | FILE_FLOPPY_DISKETTE,
                                FALSE, &LocalDeviceObject);
        if (!NT_SUCCESS(Status))
        {
            _SEH2_LEAVE;
        }

        /* Initialize our DE */
        ViInitializeDeviceExtension(LocalDeviceObject);
        DeviceExtension = LocalDeviceObject->DeviceExtension;
        ViAllocateUnicodeString(DEFAULT_STRING_SIZE, &DeviceExtension->GlobalName);

        /* Now, we'll try to find a free drive letter
         * We always start from Z and go backward
         */
        for (CurLetter = Letter; CurLetter >= 'A'; CurLetter--)
        {
            /* By default, no flags. These will be set
             * on mount, by the caller
             */
            DeviceExtension->Flags = 0;

            /* Create a drive letter name */
            DeviceExtension->GlobalName.Length = 0;
            Status = RtlAppendUnicodeToString(&DeviceExtension->GlobalName, L"\\??\\");
            if (!NT_SUCCESS(Status))
            {
                _SEH2_LEAVE;
            }

            DriveLetter[0] = CurLetter;
            DriveLetter[1] = L':';
            DriveLetter[2] = UNICODE_NULL;
            Status = RtlAppendUnicodeToString(&DeviceExtension->GlobalName, DriveLetter);
            if (!NT_SUCCESS(Status))
            {
                _SEH2_LEAVE;
            }

            /* And try to open it */
            InitializeObjectAttributes(&ObjectAttributes, &DeviceExtension->GlobalName, OBJ_CASE_INSENSITIVE | OBJ_PERMANENT, NULL, NULL);
            Status = ZwOpenSymbolicLinkObject(&LinkHandle, GENERIC_READ, &ObjectAttributes);
            /* It failed; good news, that letter is free, jump on it! */
            if (!NT_SUCCESS(Status))
            {
                /* Create a symbolic link to our device */
                Status = IoCreateSymbolicLink(&DeviceExtension->GlobalName, &DeviceName);
                /* If created... */
                if (NT_SUCCESS(Status))
                {
                    /* Return the drive letter to the caller */
                    *EffectiveLetter = CurLetter;
                    ClearFlag(LocalDeviceObject->Flags, DO_DEVICE_INITIALIZING);
                    /* No caching! */
                    SetFlag(LocalDeviceObject->Flags, DO_DIRECT_IO);

                    /* And return the DO */
                    *DeviceObject = LocalDeviceObject;
                    break;
                }
            }
            /* This letter is taken, try another one */
            else
            {
                ZwClose(LinkHandle);
                Status = STATUS_OBJECT_NAME_EXISTS;
            }
        }

        /* We're out of drive letters, so fail :-( */
        if (CurLetter == (L'A' - 1))
        {
            Status = STATUS_INSUFFICIENT_RESOURCES;
        }
    }
    _SEH2_FINALLY
    {
        /* No longer need these */
        ViFreeUnicodeString(&DeviceName);
        ViFreeUnicodeString(&DeviceCount);

        /* And delete in case of a failure */
        if (!NT_SUCCESS(Status) && Status != STATUS_VERIFY_REQUIRED && LocalDeviceObject != NULL)
        {
            IoDeleteDevice(LocalDeviceObject);
        }
    }
    _SEH2_END;

    return Status;
}

NTSTATUS
ViMountImage(PDEVICE_OBJECT DeviceObject, PUNICODE_STRING Image)
{
    NTSTATUS Status;
    HANDLE ImgHandle;
    IO_STATUS_BLOCK IoStatus;
    ULONG Buffer[2], i, SectorShift;
    PDEVICE_EXTENSION DeviceExtension;
    OBJECT_ATTRIBUTES ObjectAttributes;
    FILE_STANDARD_INFORMATION FileStdInfo;

    /* Try to open the image */
    InitializeObjectAttributes(&ObjectAttributes, Image, OBJ_CASE_INSENSITIVE, NULL, NULL);
    Status = ZwCreateFile(&ImgHandle, FILE_READ_DATA | SYNCHRONIZE, &ObjectAttributes,
                          &IoStatus, NULL, FILE_ATTRIBUTE_NORMAL, FILE_SHARE_READ,
                          FILE_OPEN, FILE_SYNCHRONOUS_IO_NONALERT, NULL, 0);
    if (!NT_SUCCESS(Status))
    {
        DPRINT1("Failed to open image: %wZ\n", Image);
        return Status;
    }

    /* Query its information */
    Status = ZwQueryInformationFile(ImgHandle, &IoStatus, &FileStdInfo,
                                    sizeof(FileStdInfo), FileStandardInformation);
    if (!NT_SUCCESS(Status))
    {
        DPRINT1("Failed to query image information\n");
        ZwClose(ImgHandle);
        return Status;
    }

    /* Set image size */
    DeviceExtension = DeviceObject->DeviceExtension;
    DeviceExtension->VolumeSize.QuadPart = FileStdInfo.EndOfFile.QuadPart;

    /* Read the first 8-bytes to determine our sector size */
    Status = ZwReadFile(ImgHandle, NULL, NULL, NULL, &IoStatus, Buffer, sizeof(Buffer), NULL, NULL);
    if (!NT_SUCCESS(Status) || Buffer[1] == 0 || (Buffer[1] & 0x1FF) != 0)
    {
        DeviceExtension->SectorSize = 2048;
    }
    else
    {
        DeviceExtension->SectorSize = Buffer[1];
    }

    /* Now, compute the sector shift */
    if (DeviceExtension->SectorSize == 512)
    {
        DeviceExtension->SectorShift = 9;
    }
    else
    {
        for (i = 512, SectorShift = 9; i < DeviceExtension->SectorSize; i = i * 2, ++SectorShift);
        if (i == DeviceExtension->SectorSize)
        {
            DeviceExtension->SectorShift = SectorShift;
        }
        else
        {
            DeviceExtension->SectorShift = 11;
        }
    }

    /* We're good, get the image file object to close the handle */
    Status = ObReferenceObjectByHandle(ImgHandle, 0, NULL, KernelMode,
                                       (PVOID *)&DeviceExtension->VolumeObject, NULL);
    if (!NT_SUCCESS(Status))
    {
        ZwClose(ImgHandle);
        DeviceExtension->VolumeObject = NULL;
        return Status;
    }

    ZwClose(ImgHandle);
    /* Increase change count to force a verify */
    ++DeviceExtension->ChangeCount;

    /* And ask for a verify! */
    SetFlag(DeviceObject->Flags, DO_VERIFY_VOLUME);

    /* Free old string (if any) */
    ViFreeUnicodeString(&DeviceExtension->ImageName);
    /* And save our new image name */
    ViAllocateUnicodeString(Image->MaximumLength, &DeviceExtension->ImageName);
    RtlCopyUnicodeString(&DeviceExtension->ImageName, Image);

    return STATUS_SUCCESS;
}

VOID
ViCreateDriveAndMountImage(PDRIVER_OBJECT DriverObject, WCHAR Letter, LPCWSTR ImagePath)
{
    UNICODE_STRING Image;
    WCHAR EffectiveLetter;
    PDEVICE_OBJECT DeviceObject;

    /* Create string from path */
    DeviceObject = NULL;
    RtlInitUnicodeString(&Image, ImagePath);

    /* Create the drive letter (ignore output, it comes from registry, nothing to do */
    ViCreateDriveLetter(DriverObject, Letter, &EffectiveLetter, &DeviceObject);
    /* And mount the image on the created drive */
    ViMountImage(DeviceObject, &Image);
}

VOID
ViLoadImagesFromRegistry(PDRIVER_OBJECT DriverObject, LPCWSTR RegistryPath)
{
    WCHAR Letter[2];
    WCHAR PathBuffer[100], ResBuffer[100];

    /* We'll browse the registry for
     * DeviceX\IMAGE = {Path}
     * When found, we create (or at least, attempt to)
     * device X: and mount image Path
     */
    Letter[0] = L'A';
    Letter[1] = UNICODE_NULL;
    do
    {
        NTSTATUS Status;
        UNICODE_STRING Path, ResString;
        RTL_QUERY_REGISTRY_TABLE QueryTable[3];

        /* Our path is always Device + drive letter */
        RtlZeroMemory(PathBuffer, sizeof(PathBuffer));
        Path.Buffer = PathBuffer;
        Path.Length = 0;
        Path.MaximumLength = sizeof(PathBuffer);
        RtlAppendUnicodeToString(&Path, L"Parameters\\Device");
        RtlAppendUnicodeToString(&Path, Letter);

        ResString.Buffer = ResBuffer;
        ResString.Length = 0;
        ResString.MaximumLength = sizeof(ResBuffer);

        RtlZeroMemory(&QueryTable[0], sizeof(QueryTable));
        QueryTable[0].Name = Path.Buffer;
        QueryTable[0].Flags = RTL_QUERY_REGISTRY_SUBKEY;
        QueryTable[1].Name = L"IMAGE";
        QueryTable[1].Flags = RTL_QUERY_REGISTRY_DIRECT;
        QueryTable[1].EntryContext = &ResString;

        /* If query went fine, attempt to create and mount */
        Status = RtlQueryRegistryValues(RTL_REGISTRY_ABSOLUTE, RegistryPath, QueryTable, NULL, NULL);
        if (NT_SUCCESS(Status))
        {
            ViCreateDriveAndMountImage(DriverObject, Letter[0], ResString.Buffer);
        }

        ++(Letter[0]);
    } while (Letter[0] <= L'Z');
}

NTSTATUS
ViVerifyVolume(PDEVICE_OBJECT DeviceObject, PIRP Irp)
{
    /* Force verify */
    SetFlag(DeviceObject->Flags, DO_VERIFY_VOLUME);

    Irp->IoStatus.Information = 0;
    Irp->IoStatus.Status = STATUS_VERIFY_REQUIRED;

    IoSetHardErrorOrVerifyDevice(Irp, DeviceObject);

    return STATUS_VERIFY_REQUIRED;
}

NTSTATUS
ViReadFile(PFILE_OBJECT File, PMDL Mdl, PLARGE_INTEGER Offset, PKEVENT Event, ULONG Length, PIO_STATUS_BLOCK IoStatusBlock)
{
    PIRP LowerIrp;
    PIO_STACK_LOCATION Stack;
    PDEVICE_OBJECT DeviceObject;

    /* Get the lower DO */
    DeviceObject = IoGetRelatedDeviceObject(File);
    /* Allocate an IRP to deal with it */
    LowerIrp = IoAllocateIrp(DeviceObject->StackSize, 0);
    if (LowerIrp == NULL)
    {
        return STATUS_INSUFFICIENT_RESOURCES;
    }

    /* Initialize it */
    LowerIrp->RequestorMode = KernelMode;
    /* Our status block */
    LowerIrp->UserIosb = IoStatusBlock;
    LowerIrp->MdlAddress = Mdl;
    /* We don't cache! */
    LowerIrp->Flags = IRP_NOCACHE | IRP_PAGING_IO | IRP_SYNCHRONOUS_PAGING_IO;
    /* Sync event for us to know when read is done */
    LowerIrp->UserEvent = Event;
    LowerIrp->UserBuffer = (PVOID)((ULONG_PTR)Mdl->StartVa + Mdl->ByteOffset);
    /* The FO of our image */
    LowerIrp->Tail.Overlay.OriginalFileObject = File;
    LowerIrp->Tail.Overlay.Thread = PsGetCurrentThread();

    /* We basically perform a read operation, nothing complex here */
    Stack = IoGetNextIrpStackLocation(LowerIrp);
    Stack->Parameters.Read.Length = Length;
    Stack->MajorFunction = IRP_MJ_READ;
    Stack->FileObject = File;
    Stack->Parameters.Read.ByteOffset.QuadPart = Offset->QuadPart;

    /* And call lower driver */
    return IoCallDriver(DeviceObject, LowerIrp);
}

NTSTATUS
NTAPI
VcdRead(PDEVICE_OBJECT DeviceObject, PIRP Irp)
{
    KEVENT Event;
    NTSTATUS Status;
    IO_STATUS_BLOCK IoStatus;
    PIO_STACK_LOCATION Stack;
    PDEVICE_EXTENSION DeviceExtension;

    /* Catch any exception that could happen during read attempt */
    _SEH2_TRY
    {
        /* First of all, check if there's an image mounted */
        DeviceExtension = DeviceObject->DeviceExtension;
        if (DeviceExtension->VolumeObject == NULL)
        {
            Status = ViSetIoStatus(STATUS_NO_MEDIA_IN_DEVICE, 0, Irp);<--- Status is assigned
            _SEH2_LEAVE;
        }

        /* Check if volume has to be verified (or if we override, for instance
         * FSD performing initial reads for mouting FS)
         */
        Stack = IoGetCurrentIrpStackLocation(Irp);
        if (BooleanFlagOn(DeviceObject->Flags, DO_VERIFY_VOLUME) && 
            !BooleanFlagOn(Stack->Flags, SL_OVERRIDE_VERIFY_VOLUME))
        {
            Status = ViVerifyVolume(DeviceObject, Irp);<--- Status is assigned
            _SEH2_LEAVE;
        }

        /* Check if we have enough room for output */
        if (Stack->Parameters.Read.Length > Irp->MdlAddress->ByteCount)
        {
            Status = ViSetIoStatus(STATUS_BUFFER_TOO_SMALL, 0, Irp);<--- Status is assigned
            _SEH2_LEAVE;
        }

        /* Initialize our event */
        KeInitializeEvent(&Event, NotificationEvent, FALSE);

        /* Perform actual read */
        Status = ViReadFile(DeviceExtension->VolumeObject, Irp->MdlAddress,<--- Status is overwritten<--- Status is overwritten<--- Status is overwritten
                            &Stack->Parameters.Read.ByteOffset, &Event,
                            Stack->Parameters.Read.Length, &IoStatus);
        if (!NT_SUCCESS(Status))
        {
            Status = ViSetIoStatus(Status, 0, Irp);
            _SEH2_LEAVE;
        }

        /* Make sure we wait until its done */
        Status = KeWaitForSingleObject(&Event, Executive, KernelMode, FALSE, NULL);
        if (!NT_SUCCESS(Status))
        {
            Status = ViSetIoStatus(Status, 0, Irp);
            _SEH2_LEAVE;
        }

        /* Look whether we're reading first bytes of the volume, if so, our
         * "suppression" might be asked for
         */
        if (Stack->Parameters.Read.ByteOffset.QuadPart / DeviceExtension->SectorSize < 0x20)
        {
            /* Check our output buffer is in a state where we can play with it */
            if ((Irp->MdlAddress->MdlFlags & (MDL_ALLOCATED_FIXED_SIZE | MDL_PAGES_LOCKED | MDL_MAPPED_TO_SYSTEM_VA)) ==
                (MDL_ALLOCATED_FIXED_SIZE | MDL_PAGES_LOCKED | MDL_MAPPED_TO_SYSTEM_VA))
            {
                PUCHAR Buffer;

                /* Do we have to delete any UDF mark? */
                if (BooleanFlagOn(DeviceExtension->Flags, MOUNT_FLAG_SUPP_UDF))
                {
                    /* Kill any NSR0X mark from UDF */
                    Buffer = (PUCHAR)((ULONG_PTR)Irp->MdlAddress->StartVa + Irp->MdlAddress->ByteOffset);
                    if (Buffer != NULL)
                    {
                        if (Buffer[0] == 0 && Buffer[1] == 'N' && Buffer[2] == 'S' &&
                            Buffer[3] == 'R' && Buffer[4] == '0')
                        {
                            Buffer[5] = '0';
                        }
                    }
                }

                /* Do we have to delete any Joliet mark? */
                if (BooleanFlagOn(DeviceExtension->Flags, MOUNT_FLAG_SUPP_JOLIET))
                {
                    /* Kill the CD001 mark from Joliet */
                    Buffer = (PUCHAR)((ULONG_PTR)Irp->MdlAddress->StartVa + Irp->MdlAddress->ByteOffset);
                    if (Buffer != NULL)
                    {
                        if (Buffer[0] == 2 && Buffer[1] == 'C' && Buffer[2] == 'D' &&
                            Buffer[3] == '0' && Buffer[4] == '0' && Buffer[5] == '1')
                        {
                            Buffer[5] = '0';
                        }
                    }
                }
            }
        }

        /* Set status */
        Status = ViSetIoStatus(Status, Stack->Parameters.Read.Length, Irp);
    }
    _SEH2_FINALLY
    {
        /* And complete! */
        IoCompleteRequest(Irp, IO_DISK_INCREMENT);
    } _SEH2_END;

    return Status;
}

NTSTATUS
ViCreateDevice(PDRIVER_OBJECT DriverObject, PIRP Irp)
{
    NTSTATUS Status;
    PIO_STACK_LOCATION Stack;
    PDEVICE_OBJECT DeviceObject;

    Stack = IoGetCurrentIrpStackLocation(Irp);

    /* Make sure output buffer is big enough to receive the drive letter
     * when we create the drive
     */
    if (Stack->Parameters.DeviceIoControl.OutputBufferLength < sizeof(WCHAR))
    {
        return ViSetIoStatus(STATUS_BUFFER_TOO_SMALL, sizeof(WCHAR), Irp);
    }

    /* And start creation. We always start from bottom */
    Status = ViCreateDriveLetter(DriverObject, L'Z', Irp->AssociatedIrp.SystemBuffer, &DeviceObject);
    return ViSetIoStatus(Status, sizeof(WCHAR), Irp);
}

NTSTATUS
ViGetDriveGeometry(PDEVICE_OBJECT DeviceObject, PIRP Irp)
{
    PDISK_GEOMETRY DiskGeo;
    PIO_STACK_LOCATION Stack;
    PDEVICE_EXTENSION DeviceExtension;

    /* No geometry if no image mounted */
    DeviceExtension = DeviceObject->DeviceExtension;
    if (DeviceExtension->VolumeObject == NULL)
    {
        return ViSetIoStatus(STATUS_NO_MEDIA_IN_DEVICE, 0, Irp);
    }

    /* No geometry if volume is to be verified */
    Stack = IoGetCurrentIrpStackLocation(Irp);
    if (BooleanFlagOn(DeviceObject->Flags, DO_VERIFY_VOLUME) && 
        !BooleanFlagOn(Stack->Flags, SL_OVERRIDE_VERIFY_VOLUME))
    {
        return ViVerifyVolume(DeviceObject, Irp);
    }

    /* No geometry if too small output buffer */
    if (Stack->Parameters.DeviceIoControl.OutputBufferLength < sizeof(DISK_GEOMETRY))
    {
        return ViSetIoStatus(STATUS_BUFFER_TOO_SMALL, sizeof(DISK_GEOMETRY), Irp);
    }

    /* Finally, set what we're asked for */
    DiskGeo = Irp->AssociatedIrp.SystemBuffer;
    DiskGeo->MediaType = RemovableMedia;
    DiskGeo->BytesPerSector = DeviceExtension->SectorSize;
    DiskGeo->SectorsPerTrack = DeviceExtension->VolumeSize.QuadPart >> DeviceExtension->SectorShift;
    DiskGeo->Cylinders.QuadPart = 1;
    DiskGeo->TracksPerCylinder = 1;

    return ViSetIoStatus(STATUS_SUCCESS, sizeof(DISK_GEOMETRY), Irp);
}

NTSTATUS
ViCheckVerify(PDEVICE_OBJECT DeviceObject, PIRP Irp)
{
    PULONG Buffer;
    ULONG_PTR Information;
    PIO_STACK_LOCATION Stack;
    PDEVICE_EXTENSION DeviceExtension;

    /* Nothing to verify if no mounted */
    DeviceExtension = DeviceObject->DeviceExtension;
    if (DeviceExtension->VolumeObject == NULL)
    {
        return ViSetIoStatus(STATUS_NO_MEDIA_IN_DEVICE, 0, Irp);
    }

    /* Do we have to verify? */
    Stack = IoGetCurrentIrpStackLocation(Irp);
    if (BooleanFlagOn(DeviceObject->Flags, DO_VERIFY_VOLUME) && 
        !BooleanFlagOn(Stack->Flags, SL_OVERRIDE_VERIFY_VOLUME))
    {
        return ViSetIoStatus(STATUS_VERIFY_REQUIRED, 0, Irp);
    }

    /* If caller provided a buffer, that's to get the change count */
    Buffer = Irp->AssociatedIrp.SystemBuffer;
    if (Buffer != NULL)
    {
        *Buffer = DeviceExtension->ChangeCount;
        Information = sizeof(ULONG);
    }
    else
    {
        Information = 0;
    }

    /* Done */
    return ViSetIoStatus(STATUS_SUCCESS, Information, Irp);
}

NTSTATUS
ViIssueMountImage(PDEVICE_OBJECT DeviceObject, PUNICODE_STRING Image, PIRP Irp)
{
    NTSTATUS Status;
    PDEVICE_EXTENSION DeviceExtension;

    /* We cannot mount an image if there's already one mounted */
    DeviceExtension = DeviceObject->DeviceExtension;
    if (DeviceExtension->VolumeObject != NULL)
    {
        return ViSetIoStatus(STATUS_DEVICE_NOT_READY, 0, Irp);
    }

    /* Perform the mount */
    Status = ViMountImage(DeviceObject, Image);
    return ViSetIoStatus(Status, 0, Irp);
}

ULONG
ViComputeAddress(ULONG Address)
{
    UCHAR Local[4];

    /* Convert LBA to MSF */
    Local[0] = 0;
    Local[1] = Address / 4500;
    Local[2] = Address % 4500 / 75;
    Local[3] = Address + 108 * Local[1] - 75 * Local[2];

    return *(ULONG *)(&Local[0]);
}

VOID
ViFillInTrackData(PTRACK_DATA TrackData, UCHAR Control, UCHAR Adr, UCHAR TrackNumber, ULONG Address)
{
    /* Fill in our track data with provided information */
    TrackData->Reserved = 0;
    TrackData->Reserved1 = 0;
    TrackData->Control = Control & 0xF;
    TrackData->Adr = Adr;
    TrackData->TrackNumber = TrackNumber;
    *(ULONG *)(&TrackData->Address[0]) = ViComputeAddress(Address);
}

NTSTATUS
ViReadToc(PDEVICE_OBJECT DeviceObject, PIRP Irp)
{
    PCDROM_TOC Toc;
    PIO_STACK_LOCATION Stack;
    PDEVICE_EXTENSION DeviceExtension;

    /* No image mounted, no TOC */
    DeviceExtension = DeviceObject->DeviceExtension;
    if (DeviceExtension->VolumeObject == NULL)
    {
        return ViSetIoStatus(STATUS_NO_MEDIA_IN_DEVICE, 0, Irp);
    }

    /* No TOC if we have to verify */
    Stack = IoGetCurrentIrpStackLocation(Irp);
    if (BooleanFlagOn(DeviceObject->Flags, DO_VERIFY_VOLUME) && 
        !BooleanFlagOn(Stack->Flags, SL_OVERRIDE_VERIFY_VOLUME))
    {
        return ViVerifyVolume(DeviceObject, Irp);
    }

    /* Check we have enough room for TOC */
    if (Stack->Parameters.DeviceIoControl.OutputBufferLength < sizeof(CDROM_TOC))
    {
        return ViSetIoStatus(STATUS_BUFFER_TOO_SMALL, sizeof(CDROM_TOC), Irp);
    }

    /* Start filling the TOC */
    Toc = Irp->AssociatedIrp.SystemBuffer;
    Toc->Length[0] = 0;
    Toc->Length[1] = 8;
    Toc->FirstTrack = 1;
    Toc->LastTrack = 1;
    /* And fill our single (an ISO file always have a single track) track with 2sec gap */
    ViFillInTrackData(Toc->TrackData, TOC_DATA_TRACK, ADR_NO_MODE_INFORMATION, 1, 150);
    /* And add last track termination */
    ViFillInTrackData(&Toc->TrackData[1], TOC_DATA_TRACK, ADR_NO_MODE_INFORMATION, TOC_LAST_TRACK, (DeviceExtension->VolumeSize.QuadPart >> DeviceExtension->SectorShift) + 150);

    return ViSetIoStatus(STATUS_SUCCESS, sizeof(CDROM_TOC), Irp);
}

NTSTATUS
ViReadTocEx(PDEVICE_OBJECT DeviceObject, PIRP Irp)
{
    PCDROM_TOC Toc;
    PIO_STACK_LOCATION Stack;
    PCDROM_READ_TOC_EX TocEx;
    PDEVICE_EXTENSION DeviceExtension;

    /* No image mounted, no TOC */
    DeviceExtension = DeviceObject->DeviceExtension;
    if (DeviceExtension->VolumeObject == NULL)
    {
        return ViSetIoStatus(STATUS_NO_MEDIA_IN_DEVICE, 0, Irp);
    }

    /* No TOC if we have to verify */
    Stack = IoGetCurrentIrpStackLocation(Irp);
    if (BooleanFlagOn(DeviceObject->Flags, DO_VERIFY_VOLUME) && 
        !BooleanFlagOn(Stack->Flags, SL_OVERRIDE_VERIFY_VOLUME))
    {
        return ViVerifyVolume(DeviceObject, Irp);
    }

    /* We need an input buffer */
    if (Stack->Parameters.DeviceIoControl.InputBufferLength < sizeof(CDROM_READ_TOC_EX))
    {
        return ViSetIoStatus(STATUS_INFO_LENGTH_MISMATCH, sizeof(CDROM_READ_TOC_EX), Irp);
    }

    /* Validate output buffer is big enough */
    if (Stack->Parameters.DeviceIoControl.OutputBufferLength < MAXIMUM_CDROM_SIZE)
    {
        return ViSetIoStatus(STATUS_BUFFER_TOO_SMALL, MAXIMUM_CDROM_SIZE, Irp);
    }

    /* Validate the input buffer - see cdrom_new */
    TocEx = Irp->AssociatedIrp.SystemBuffer;
    if ((TocEx->Reserved1 != 0) || (TocEx->Reserved2 != 0) ||
        (TocEx->Reserved3 != 0)) 
    {
        return ViSetIoStatus(STATUS_INVALID_PARAMETER, 0, Irp);
    }

    if (((TocEx->Format == CDROM_READ_TOC_EX_FORMAT_SESSION) ||
         (TocEx->Format == CDROM_READ_TOC_EX_FORMAT_PMA) ||
         (TocEx->Format == CDROM_READ_TOC_EX_FORMAT_ATIP)) &&
        TocEx->SessionTrack != 0)
    {
        return ViSetIoStatus(STATUS_INVALID_PARAMETER, 0, Irp);
    }

    if ((TocEx->Format != CDROM_READ_TOC_EX_FORMAT_TOC) &&
        (TocEx->Format != CDROM_READ_TOC_EX_FORMAT_FULL_TOC) &&
        (TocEx->Format != CDROM_READ_TOC_EX_FORMAT_CDTEXT) &&
        (TocEx->Format != CDROM_READ_TOC_EX_FORMAT_SESSION) &&
        (TocEx->Format != CDROM_READ_TOC_EX_FORMAT_PMA) &&
        (TocEx->Format == CDROM_READ_TOC_EX_FORMAT_ATIP))
    {
        return ViSetIoStatus(STATUS_INVALID_PARAMETER, 0, Irp);
    }

    /* Start filling the TOC */
    Toc = Irp->AssociatedIrp.SystemBuffer;
    Toc->Length[0] = 0;
    Toc->Length[1] = 8;
    Toc->FirstTrack = 1;
    Toc->LastTrack = 1;
    /* And fill our single (an ISO file always have a single track) track with 2sec gap */
    ViFillInTrackData(Toc->TrackData, TOC_DATA_TRACK, ADR_NO_MODE_INFORMATION, 1, 150);
    /* And add last track termination */
    ViFillInTrackData(&Toc->TrackData[1], TOC_DATA_TRACK, ADR_NO_MODE_INFORMATION, TOC_LAST_TRACK, (DeviceExtension->VolumeSize.QuadPart >> DeviceExtension->SectorShift) + 150);

    return ViSetIoStatus(STATUS_SUCCESS, MAXIMUM_CDROM_SIZE, Irp);
}

NTSTATUS
ViGetLastSession(PDEVICE_OBJECT DeviceObject, PIRP Irp)
{

    PIO_STACK_LOCATION Stack;
    PCDROM_TOC_SESSION_DATA Toc;
    PDEVICE_EXTENSION DeviceExtension;

    /* No image, no last session */
    DeviceExtension = DeviceObject->DeviceExtension;
    if (DeviceExtension->VolumeObject == NULL)
    {
        return ViSetIoStatus(STATUS_NO_MEDIA_IN_DEVICE, 0, Irp);
    }

    /* No last session if we have to verify */
    Stack = IoGetCurrentIrpStackLocation(Irp);
    if (BooleanFlagOn(DeviceObject->Flags, DO_VERIFY_VOLUME) && 
        !BooleanFlagOn(Stack->Flags, SL_OVERRIDE_VERIFY_VOLUME))
    {
        return ViVerifyVolume(DeviceObject, Irp);
    }

    /* Check we have enough room for last session data */
    if (Stack->Parameters.DeviceIoControl.OutputBufferLength < sizeof(CDROM_TOC_SESSION_DATA))
    {
        return ViSetIoStatus(STATUS_BUFFER_TOO_SMALL, sizeof(CDROM_TOC_SESSION_DATA), Irp);
    }

    /* Fill in data */
    Toc = Irp->AssociatedIrp.SystemBuffer;
    Toc->Length[0] = 0;
    Toc->Length[1] = 8;
    Toc->FirstCompleteSession = 1;
    Toc->LastCompleteSession = 1;
    /* And return our track with 2sec gap (cf TOC function) */
    ViFillInTrackData(Toc->TrackData, TOC_DATA_TRACK, ADR_NO_MODE_INFORMATION, 1, 150);

    return ViSetIoStatus(STATUS_SUCCESS, sizeof(CDROM_TOC_SESSION_DATA), Irp);
}

NTSTATUS
ViEnumerateDrives(PDEVICE_OBJECT DeviceObject, PIRP Irp)
{
    PDRIVES_LIST DrivesList;
    PIO_STACK_LOCATION Stack;
    PDEVICE_OBJECT CurrentDO;
    PDEVICE_EXTENSION DeviceExtension;

    /* Check we have enough room for output */
    Stack = IoGetCurrentIrpStackLocation(Irp);
    if (Stack->Parameters.DeviceIoControl.OutputBufferLength < sizeof(DRIVES_LIST))
    {
        return ViSetIoStatus(STATUS_BUFFER_TOO_SMALL, sizeof(DRIVES_LIST), Irp);
    }

    /* Get the output buffer */
    DrivesList = Irp->AssociatedIrp.SystemBuffer;
    DrivesList->Count = 0;

    /* And now, starting from our main DO, start browsing all the DO we created */
    for (CurrentDO = DeviceObject->DriverObject->DeviceObject; CurrentDO != NULL;
         CurrentDO = CurrentDO->NextDevice)
    {
        /* Check we won't output our main DO */
        DeviceExtension = CurrentDO->DeviceExtension;
        if (DeviceExtension->GlobalName.Length !=
            RtlCompareMemory(DeviceExtension->GlobalName.Buffer,
                             L"\\??\\VirtualCdRom",
                             DeviceExtension->GlobalName.Length))
        {
            /* When we return, we extract the drive letter
             * See ViCreateDriveLetter(), it's \??\Z:
             */
            DrivesList->Drives[DrivesList->Count++] = DeviceExtension->GlobalName.Buffer[4];
        }
    }

    return ViSetIoStatus(STATUS_SUCCESS, sizeof(DRIVES_LIST), Irp);
}

NTSTATUS
ViGetImagePath(PDEVICE_OBJECT DeviceObject, PIRP Irp)
{
    PIMAGE_PATH ImagePath;
    PIO_STACK_LOCATION Stack;
    PDEVICE_EXTENSION DeviceExtension;

    /* Check we have enough room for output */
    Stack = IoGetCurrentIrpStackLocation(Irp);
    if (Stack->Parameters.DeviceIoControl.OutputBufferLength < sizeof(IMAGE_PATH))
    {
        return ViSetIoStatus(STATUS_BUFFER_TOO_SMALL, sizeof(IMAGE_PATH), Irp);
    }

    /* Get our image path from DO */
    DeviceExtension = DeviceObject->DeviceExtension;
    ImagePath = Irp->AssociatedIrp.SystemBuffer;
    ImagePath->Mounted = (DeviceExtension->VolumeObject != NULL);
    ImagePath->Length = DeviceExtension->ImageName.Length;
    /* And if it's set, copy it back to the caller */
    if (DeviceExtension->ImageName.Length != 0)
    {
        RtlCopyMemory(ImagePath->Path, DeviceExtension->ImageName.Buffer, DeviceExtension->ImageName.Length);
    }

    return ViSetIoStatus(STATUS_SUCCESS, sizeof(IMAGE_PATH), Irp);
}

NTSTATUS
ViEjectMedia(PDEVICE_OBJECT DeviceObject, PIRP Irp)
{
    PDEVICE_EXTENSION DeviceExtension;

    /* Eject will force a verify */
    SetFlag(DeviceObject->Flags, DO_VERIFY_VOLUME);

    /* If we have an image mounted, unmount it
     * But don't free anything related, so that
     * we can perform quick remount.
     * See: IOCTL_STORAGE_LOAD_MEDIA
     */
    DeviceExtension = DeviceObject->DeviceExtension;
    if (DeviceExtension->VolumeObject != NULL)
    {
        ObDereferenceObject(DeviceExtension->VolumeObject);
        /* Device changed, so mandatory increment */
        ++DeviceExtension->ChangeCount;
        DeviceExtension->VolumeObject = NULL;
    }

    return ViSetIoStatus(STATUS_SUCCESS, 0, Irp);
}

NTSTATUS
ViRemountMedia(PDEVICE_OBJECT DeviceObject, PIRP Irp)
{
    NTSTATUS Status;
    UNICODE_STRING Image;
    PDEVICE_EXTENSION DeviceExtension;

    /* Get the device extension */
    DeviceExtension = DeviceObject->DeviceExtension;
    /* Allocate a new string as mount parameter */
    Status = ViAllocateUnicodeString(DeviceExtension->ImageName.MaximumLength, &Image);
    if (!NT_SUCCESS(Status))
    {
        return ViSetIoStatus(Status, 0, Irp);
    }

    /* To allow cleanup in case of troubles */
    _SEH2_TRY
    {
        /* Copy our current image name and mount */
        RtlCopyUnicodeString(&Image, &DeviceExtension->ImageName);
        Status = ViIssueMountImage(DeviceObject, &Image, Irp);
    }
    _SEH2_FINALLY
    {
        ViFreeUnicodeString(&Image);
    }
    _SEH2_END;

    return ViSetIoStatus(Status, 0, Irp);
}

NTSTATUS
NTAPI
VcdDeviceControl(PDEVICE_OBJECT DeviceObject, PIRP Irp)
{
    NTSTATUS Status;
    UNICODE_STRING Image;
    PIO_STACK_LOCATION Stack;
    PMOUNT_PARAMETERS MountParameters;
    PDEVICE_EXTENSION DeviceExtension;

    DeviceExtension = DeviceObject->DeviceExtension;
    Stack = IoGetCurrentIrpStackLocation(Irp);

    _SEH2_TRY
    {
        switch (Stack->Parameters.DeviceIoControl.IoControlCode)
        {
            /* First of all, our private IOCTLs */
            case IOCTL_VCDROM_CREATE_DRIVE:
                Status = ViCreateDevice(DeviceObject->DriverObject, Irp);
                break;

            case IOCTL_VCDROM_DELETE_DRIVE:
                Status = ViDeleteDevice(DeviceObject, Irp);
                break;

            case IOCTL_VCDROM_MOUNT_IMAGE:
                MountParameters = Irp->AssociatedIrp.SystemBuffer;
                Image.MaximumLength = 255 * sizeof(WCHAR);
                Image.Length = MountParameters->Length;
                Image.Buffer = MountParameters->Path;
                DeviceExtension->Flags = MountParameters->Flags;
                Status = ViIssueMountImage(DeviceObject, &Image, Irp);
                break;

            case IOCTL_VCDROM_ENUMERATE_DRIVES:
                Status = ViEnumerateDrives(DeviceObject, Irp);
                break;

            case IOCTL_VCDROM_GET_IMAGE_PATH:
                Status = ViGetImagePath(DeviceObject, Irp);
                break;

            /* Now, IOCTLs we have to handle as class driver */
            case IOCTL_DISK_GET_DRIVE_GEOMETRY:
            case IOCTL_CDROM_GET_DRIVE_GEOMETRY:
                Status = ViGetDriveGeometry(DeviceObject, Irp);
                break;

            case IOCTL_DISK_CHECK_VERIFY:
            case IOCTL_CDROM_CHECK_VERIFY:
                Status = ViCheckVerify(DeviceObject, Irp);
                break;

            case IOCTL_CDROM_READ_TOC:
                Status = ViReadToc(DeviceObject, Irp);
                break;

            case IOCTL_CDROM_READ_TOC_EX:
                Status = ViReadTocEx(DeviceObject, Irp);
                break;

            case IOCTL_CDROM_GET_LAST_SESSION:
                Status = ViGetLastSession(DeviceObject, Irp);
                break;

            case IOCTL_STORAGE_EJECT_MEDIA:
            case IOCTL_CDROM_EJECT_MEDIA:
                Status = ViEjectMedia(DeviceObject, Irp);
                break;

            /* That one is a bit specific
             * It gets unmounted image mounted again
             */
            case IOCTL_STORAGE_LOAD_MEDIA:
                /* That means it can only be performed if:
                 * - We had an image previously
                 * - It's no longer mounted
                 * Otherwise, we just return success
                 */
                if (DeviceExtension->ImageName.Buffer == NULL || DeviceExtension->VolumeObject != NULL)
                {
                    Status = ViSetIoStatus(STATUS_SUCCESS, 0, Irp);
                }
                else
                {
                    Status = ViRemountMedia(DeviceObject, Irp);
                }
                break;

            default:
                Status = STATUS_INVALID_DEVICE_REQUEST;
                DPRINT1("IOCTL: %x not supported\n", Stack->Parameters.DeviceIoControl.IoControlCode);
                break;
        }
    } _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
    {
        Status = _SEH2_GetExceptionCode();
    } _SEH2_END;

    /* Depending on the failure code, we may force a verify */
    if (!NT_SUCCESS(Status))
    {
        if (Status == STATUS_DEVICE_NOT_READY || Status == STATUS_IO_TIMEOUT ||
            Status == STATUS_MEDIA_WRITE_PROTECTED || Status == STATUS_NO_MEDIA_IN_DEVICE ||
            Status == STATUS_VERIFY_REQUIRED || Status == STATUS_UNRECOGNIZED_MEDIA ||
            Status == STATUS_WRONG_VOLUME)
        {
            IoSetHardErrorOrVerifyDevice(Irp, DeviceObject);
        }
    }

    IoCompleteRequest(Irp, IO_DISK_INCREMENT);

    return Status;
}

NTSTATUS
NTAPI
DriverEntry(PDRIVER_OBJECT DriverObject, PUNICODE_STRING RegistryPath)
{
    NTSTATUS Status;
    UNICODE_STRING DeviceName;
    PDEVICE_OBJECT DeviceObject;
    PDEVICE_EXTENSION DeviceExtension;

    /* Set our entry points (rather limited :-)) */
    DriverObject->DriverUnload = VcdUnload;
    DriverObject->MajorFunction[IRP_MJ_CREATE] = VcdHandle;
    DriverObject->MajorFunction[IRP_MJ_CLOSE] = VcdHandle;
    DriverObject->MajorFunction[IRP_MJ_CLEANUP] = VcdHandle;
    DriverObject->MajorFunction[IRP_MJ_READ] = VcdRead;
    DriverObject->MajorFunction[IRP_MJ_DEVICE_CONTROL] = VcdDeviceControl;

    /* Create our main device to receive private IOCTLs */
    RtlInitUnicodeString(&DeviceName, L"\\Device\\VirtualCdRom");
    Status = IoCreateDevice(DriverObject, sizeof(DEVICE_EXTENSION), &DeviceName,
                            FILE_DEVICE_CD_ROM, FILE_READ_ONLY_DEVICE | FILE_FLOPPY_DISKETTE,
                            FALSE, &DeviceObject);
    if (!NT_SUCCESS(Status))
    {
        return Status;
    }

    /* Initialize our device extension */
    ViInitializeDeviceExtension(DeviceObject);
    DeviceExtension = DeviceObject->DeviceExtension;

    /* And create our accessible name from umode */
    ViAllocateUnicodeString(DEFAULT_STRING_SIZE, &DeviceExtension->GlobalName);
    RtlAppendUnicodeToString(&DeviceExtension->GlobalName, L"\\??\\VirtualCdRom");
    Status = IoCreateSymbolicLink(&DeviceExtension->GlobalName, &DeviceName);
    if (!NT_SUCCESS(Status))
    {
        IoDeleteDevice(DeviceObject);
        return Status;
    }

    /* Initialize our mutex for device count */
    ExInitializeFastMutex(&ViMutex);

    /* And try to load images that would have been stored in registry */
    ViLoadImagesFromRegistry(DriverObject, RegistryPath->Buffer);

    return STATUS_SUCCESS;
}