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
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
|
/* $Id: DrvHostBase.cpp $ */
/** @file
* DrvHostBase - Host base drive access driver.
*/
/*
* Copyright (C) 2006-2023 Oracle and/or its affiliates.
*
* This file is part of VirtualBox base platform packages, as
* available from https://www.virtualbox.org.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation, in version 3 of the
* License.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see <https://www.gnu.org/licenses>.
*
* SPDX-License-Identifier: GPL-3.0-only
*/
/*********************************************************************************************************************************
* Header Files *
*********************************************************************************************************************************/
#define LOG_GROUP LOG_GROUP_DRV_HOST_BASE
#include <VBox/vmm/pdmdrv.h>
#include <VBox/vmm/pdmstorageifs.h>
#include <VBox/err.h>
#include <iprt/assert.h>
#include <iprt/file.h>
#include <iprt/path.h>
#include <iprt/string.h>
#include <iprt/thread.h>
#include <iprt/semaphore.h>
#include <iprt/uuid.h>
#include <iprt/asm.h>
#include <iprt/critsect.h>
#include <iprt/ctype.h>
#include <iprt/mem.h>
#include "DrvHostBase.h"
/* -=-=-=-=- IBlock -=-=-=-=- */
/** @interface_method_impl{PDMIMEDIA,pfnRead} */
static DECLCALLBACK(int) drvHostBaseRead(PPDMIMEDIA pInterface, uint64_t off, void *pvBuf, size_t cbRead)
{
PDRVHOSTBASE pThis = RT_FROM_MEMBER(pInterface, DRVHOSTBASE, IMedia);
LogFlow(("%s-%d: drvHostBaseRead: off=%#llx pvBuf=%p cbRead=%#x (%s)\n",
pThis->pDrvIns->pReg->szName, pThis->pDrvIns->iInstance, off, pvBuf, cbRead, pThis->pszDevice));
RTCritSectEnter(&pThis->CritSect);
STAM_REL_COUNTER_INC(&pThis->StatReqsSubmitted);
STAM_REL_COUNTER_INC(&pThis->StatReqsRead);
/*
* Check the state.
*/
int rc;
if (pThis->fMediaPresent)
{
/*
* Seek and read.
*/
rc = drvHostBaseReadOs(pThis, off, pvBuf, cbRead);
if (RT_SUCCESS(rc))
{
Log2(("%s-%d: drvHostBaseReadOs: off=%#llx cbRead=%#x\n"
"%16.*Rhxd\n",
pThis->pDrvIns->pReg->szName, pThis->pDrvIns->iInstance, off, cbRead, cbRead, pvBuf));
}
else
Log(("%s-%d: drvHostBaseRead: drvHostBaseReadOs(%#llx, %p, %#x) -> %Rrc ('%s')\n",
pThis->pDrvIns->pReg->szName, pThis->pDrvIns->iInstance,
off, pvBuf, cbRead, rc, pThis->pszDevice));
}
else
rc = VERR_MEDIA_NOT_PRESENT;
if (RT_SUCCESS(rc))
{
STAM_REL_COUNTER_INC(&pThis->StatReqsSucceeded);
STAM_REL_COUNTER_ADD(&pThis->StatBytesRead, cbRead);
}
else
STAM_REL_COUNTER_INC(&pThis->StatReqsFailed);
RTCritSectLeave(&pThis->CritSect);
LogFlow(("%s-%d: drvHostBaseRead: returns %Rrc\n", pThis->pDrvIns->pReg->szName, pThis->pDrvIns->iInstance, rc));
return rc;
}
/** @interface_method_impl{PDMIMEDIA,pfnWrite} */
static DECLCALLBACK(int) drvHostBaseWrite(PPDMIMEDIA pInterface, uint64_t off, const void *pvBuf, size_t cbWrite)
{
PDRVHOSTBASE pThis = RT_FROM_MEMBER(pInterface, DRVHOSTBASE, IMedia);
LogFlow(("%s-%d: drvHostBaseWrite: off=%#llx pvBuf=%p cbWrite=%#x (%s)\n",
pThis->pDrvIns->pReg->szName, pThis->pDrvIns->iInstance, off, pvBuf, cbWrite, pThis->pszDevice));
Log2(("%s-%d: drvHostBaseWrite: off=%#llx cbWrite=%#x\n"
"%16.*Rhxd\n",
pThis->pDrvIns->pReg->szName, pThis->pDrvIns->iInstance, off, cbWrite, cbWrite, pvBuf));
RTCritSectEnter(&pThis->CritSect);
STAM_REL_COUNTER_INC(&pThis->StatReqsSubmitted);
STAM_REL_COUNTER_INC(&pThis->StatReqsWrite);
/*
* Check the state.
*/
int rc;
if (!pThis->fReadOnly)
{
if (pThis->fMediaPresent)
{
/*
* Seek and write.
*/
rc = drvHostBaseWriteOs(pThis, off, pvBuf, cbWrite);
if (RT_FAILURE(rc))
Log(("%s-%d: drvHostBaseWrite: drvHostBaseWriteOs(%#llx, %p, %#x) -> %Rrc ('%s')\n",
pThis->pDrvIns->pReg->szName, pThis->pDrvIns->iInstance,
off, pvBuf, cbWrite, rc, pThis->pszDevice));
}
else
rc = VERR_MEDIA_NOT_PRESENT;
}
else
rc = VERR_WRITE_PROTECT;
if (RT_SUCCESS(rc))
{
STAM_REL_COUNTER_INC(&pThis->StatReqsSucceeded);
STAM_REL_COUNTER_ADD(&pThis->StatBytesWritten, cbWrite);
}
else
STAM_REL_COUNTER_INC(&pThis->StatReqsFailed);
RTCritSectLeave(&pThis->CritSect);
LogFlow(("%s-%d: drvHostBaseWrite: returns %Rrc\n", pThis->pDrvIns->pReg->szName, pThis->pDrvIns->iInstance, rc));
return rc;
}
/** @interface_method_impl{PDMIMEDIA,pfnFlush} */
static DECLCALLBACK(int) drvHostBaseFlush(PPDMIMEDIA pInterface)
{
int rc;
PDRVHOSTBASE pThis = RT_FROM_MEMBER(pInterface, DRVHOSTBASE, IMedia);
LogFlow(("%s-%d: drvHostBaseFlush: (%s)\n",
pThis->pDrvIns->pReg->szName, pThis->pDrvIns->iInstance, pThis->pszDevice));
RTCritSectEnter(&pThis->CritSect);
STAM_REL_COUNTER_INC(&pThis->StatReqsSubmitted);
STAM_REL_COUNTER_INC(&pThis->StatReqsFlush);
if (pThis->fMediaPresent)
rc = drvHostBaseFlushOs(pThis);
else
rc = VERR_MEDIA_NOT_PRESENT;
if (RT_SUCCESS(rc))
STAM_REL_COUNTER_INC(&pThis->StatReqsSucceeded);
else
STAM_REL_COUNTER_INC(&pThis->StatReqsFailed);
RTCritSectLeave(&pThis->CritSect);
LogFlow(("%s-%d: drvHostBaseFlush: returns %Rrc\n", pThis->pDrvIns->pReg->szName, pThis->pDrvIns->iInstance, rc));
return rc;
}
/** @interface_method_impl{PDMIMEDIA,pfnIsReadOnly} */
static DECLCALLBACK(bool) drvHostBaseIsReadOnly(PPDMIMEDIA pInterface)
{
PDRVHOSTBASE pThis = RT_FROM_MEMBER(pInterface, DRVHOSTBASE, IMedia);
return pThis->fReadOnly;
}
/** @interface_method_impl{PDMIMEDIA,pfnIsNonRotational} */
static DECLCALLBACK(bool) drvHostBaseIsNonRotational(PPDMIMEDIA pInterface)
{
RT_NOREF1(pInterface);
return false;
}
/** @interface_method_impl{PDMIMEDIA,pfnGetSize} */
static DECLCALLBACK(uint64_t) drvHostBaseGetSize(PPDMIMEDIA pInterface)
{
PDRVHOSTBASE pThis = RT_FROM_MEMBER(pInterface, DRVHOSTBASE, IMedia);
RTCritSectEnter(&pThis->CritSect);
uint64_t cb = 0;
if (pThis->fMediaPresent)
cb = pThis->cbSize;
RTCritSectLeave(&pThis->CritSect);
LogFlow(("%s-%d: drvHostBaseGetSize: returns %llu\n", pThis->pDrvIns->pReg->szName, pThis->pDrvIns->iInstance, cb));
return cb;
}
/** @interface_method_impl{PDMIMEDIA,pfnGetType} */
static DECLCALLBACK(PDMMEDIATYPE) drvHostBaseGetType(PPDMIMEDIA pInterface)
{
PDRVHOSTBASE pThis = RT_FROM_MEMBER(pInterface, DRVHOSTBASE, IMedia);
LogFlow(("%s-%d: drvHostBaseGetType: returns %d\n", pThis->pDrvIns->pReg->szName, pThis->pDrvIns->iInstance, pThis->enmType));
return pThis->enmType;
}
/** @interface_method_impl{PDMIMEDIA,pfnGetUuid} */
static DECLCALLBACK(int) drvHostBaseGetUuid(PPDMIMEDIA pInterface, PRTUUID pUuid)
{
PDRVHOSTBASE pThis = RT_FROM_MEMBER(pInterface, DRVHOSTBASE, IMedia);
*pUuid = pThis->Uuid;
LogFlow(("%s-%d: drvHostBaseGetUuid: returns VINF_SUCCESS *pUuid=%RTuuid\n", pThis->pDrvIns->pReg->szName, pThis->pDrvIns->iInstance, pUuid));
return VINF_SUCCESS;
}
/** @interface_method_impl{PDMIMEDIA,pfnBiosGetPCHSGeometry} */
static DECLCALLBACK(int) drvHostBaseGetPCHSGeometry(PPDMIMEDIA pInterface, PPDMMEDIAGEOMETRY pPCHSGeometry)
{
PDRVHOSTBASE pThis = RT_FROM_MEMBER(pInterface, DRVHOSTBASE, IMedia);
RTCritSectEnter(&pThis->CritSect);
int rc = VINF_SUCCESS;
if (pThis->fMediaPresent)
{
if ( pThis->PCHSGeometry.cCylinders > 0
&& pThis->PCHSGeometry.cHeads > 0
&& pThis->PCHSGeometry.cSectors > 0)
{
*pPCHSGeometry = pThis->PCHSGeometry;
}
else
rc = VERR_PDM_GEOMETRY_NOT_SET;
}
else
rc = VERR_PDM_MEDIA_NOT_MOUNTED;
RTCritSectLeave(&pThis->CritSect);
LogFlow(("%s-%d: %s: returns %Rrc CHS={%d,%d,%d}\n",
pThis->pDrvIns->pReg->szName, pThis->pDrvIns->iInstance, __FUNCTION__, rc,
pThis->PCHSGeometry.cCylinders, pThis->PCHSGeometry.cHeads, pThis->PCHSGeometry.cSectors));
return rc;
}
/** @interface_method_impl{PDMIMEDIA,pfnBiosSetPCHSGeometry} */
static DECLCALLBACK(int) drvHostBaseSetPCHSGeometry(PPDMIMEDIA pInterface, PCPDMMEDIAGEOMETRY pPCHSGeometry)
{
PDRVHOSTBASE pThis = RT_FROM_MEMBER(pInterface, DRVHOSTBASE, IMedia);
LogFlow(("%s-%d: %s: cCylinders=%d cHeads=%d cSectors=%d\n",
pThis->pDrvIns->pReg->szName, pThis->pDrvIns->iInstance, __FUNCTION__,
pPCHSGeometry->cCylinders, pPCHSGeometry->cHeads, pPCHSGeometry->cSectors));
RTCritSectEnter(&pThis->CritSect);
int rc = VINF_SUCCESS;
if (pThis->fMediaPresent)
{
pThis->PCHSGeometry = *pPCHSGeometry;
}
else
{
AssertMsgFailed(("Invalid state! Not mounted!\n"));
rc = VERR_PDM_MEDIA_NOT_MOUNTED;
}
RTCritSectLeave(&pThis->CritSect);
return rc;
}
/** @interface_method_impl{PDMIMEDIA,pfnBiosGetLCHSGeometry} */
static DECLCALLBACK(int) drvHostBaseGetLCHSGeometry(PPDMIMEDIA pInterface, PPDMMEDIAGEOMETRY pLCHSGeometry)
{
PDRVHOSTBASE pThis = RT_FROM_MEMBER(pInterface, DRVHOSTBASE, IMedia);
RTCritSectEnter(&pThis->CritSect);
int rc = VINF_SUCCESS;
if (pThis->fMediaPresent)
{
if ( pThis->LCHSGeometry.cCylinders > 0
&& pThis->LCHSGeometry.cHeads > 0
&& pThis->LCHSGeometry.cSectors > 0)
{
*pLCHSGeometry = pThis->LCHSGeometry;
}
else
rc = VERR_PDM_GEOMETRY_NOT_SET;
}
else
rc = VERR_PDM_MEDIA_NOT_MOUNTED;
RTCritSectLeave(&pThis->CritSect);
LogFlow(("%s-%d: %s: returns %Rrc CHS={%d,%d,%d}\n",
pThis->pDrvIns->pReg->szName, pThis->pDrvIns->iInstance, __FUNCTION__, rc,
pThis->LCHSGeometry.cCylinders, pThis->LCHSGeometry.cHeads, pThis->LCHSGeometry.cSectors));
return rc;
}
/** @interface_method_impl{PDMIMEDIA,pfnBiosSetLCHSGeometry} */
static DECLCALLBACK(int) drvHostBaseSetLCHSGeometry(PPDMIMEDIA pInterface, PCPDMMEDIAGEOMETRY pLCHSGeometry)
{
PDRVHOSTBASE pThis = RT_FROM_MEMBER(pInterface, DRVHOSTBASE, IMedia);
LogFlow(("%s-%d: %s: cCylinders=%d cHeads=%d cSectors=%d\n",
pThis->pDrvIns->pReg->szName, pThis->pDrvIns->iInstance, __FUNCTION__,
pLCHSGeometry->cCylinders, pLCHSGeometry->cHeads, pLCHSGeometry->cSectors));
RTCritSectEnter(&pThis->CritSect);
int rc = VINF_SUCCESS;
if (pThis->fMediaPresent)
{
pThis->LCHSGeometry = *pLCHSGeometry;
}
else
{
AssertMsgFailed(("Invalid state! Not mounted!\n"));
rc = VERR_PDM_MEDIA_NOT_MOUNTED;
}
RTCritSectLeave(&pThis->CritSect);
return rc;
}
/** @interface_method_impl{PDMIMEDIA,pfnBiosIsVisible} */
static DECLCALLBACK(bool) drvHostBaseIsVisible(PPDMIMEDIA pInterface)
{
PDRVHOSTBASE pThis = RT_FROM_MEMBER(pInterface, DRVHOSTBASE, IMedia);
return pThis->fBiosVisible;
}
/** @interface_method_impl{PDMIMEDIA,pfnGetRegionCount} */
static DECLCALLBACK(uint32_t) drvHostBaseGetRegionCount(PPDMIMEDIA pInterface)
{
PDRVHOSTBASE pThis = RT_FROM_MEMBER(pInterface, DRVHOSTBASE, IMedia);
LogFlowFunc(("\n"));
uint32_t cRegions = pThis->fMediaPresent ? 1 : 0;
/* For now just return one region for all devices. */
/** @todo Handle CD/DVD passthrough properly. */
LogFlowFunc(("returns %u\n", cRegions));
return cRegions;
}
/** @interface_method_impl{PDMIMEDIA,pfnQueryRegionProperties} */
static DECLCALLBACK(int) drvHostBaseQueryRegionProperties(PPDMIMEDIA pInterface, uint32_t uRegion, uint64_t *pu64LbaStart,
uint64_t *pcBlocks, uint64_t *pcbBlock,
PVDREGIONDATAFORM penmDataForm)
{
LogFlowFunc(("\n"));
int rc = VINF_SUCCESS;
PDRVHOSTBASE pThis = RT_FROM_MEMBER(pInterface, DRVHOSTBASE, IMedia);
if (uRegion < 1 && pThis->fMediaPresent)
{
uint64_t cbMedia;
rc = drvHostBaseGetMediaSizeOs(pThis, &cbMedia);
if (RT_SUCCESS(rc))
{
uint64_t cbBlock = 0;
if (pThis->enmType == PDMMEDIATYPE_DVD)
cbBlock = 2048;
else
cbBlock = 512; /* Floppy. */
if (pu64LbaStart)
*pu64LbaStart = 0;
if (pcBlocks)
*pcBlocks = cbMedia / cbBlock;
if (pcbBlock)
*pcbBlock = cbBlock;
if (penmDataForm)
*penmDataForm = VDREGIONDATAFORM_RAW;
}
}
else
rc = VERR_NOT_FOUND;
LogFlowFunc(("returns %Rrc\n", rc));
return rc;
}
/** @interface_method_impl{PDMIMEDIA,pfnQueryRegionPropertiesForLba} */
static DECLCALLBACK(int) drvHostBaseQueryRegionPropertiesForLba(PPDMIMEDIA pInterface, uint64_t u64LbaStart,
uint32_t *puRegion, uint64_t *pcBlocks,
uint64_t *pcbBlock, PVDREGIONDATAFORM penmDataForm)
{
LogFlowFunc(("\n"));
int rc = VINF_SUCCESS;
PDRVHOSTBASE pThis = RT_FROM_MEMBER(pInterface, DRVHOSTBASE, IMedia);
uint64_t cbMedia;
uint64_t cbBlock = 0;
if (pThis->enmType == PDMMEDIATYPE_DVD)
cbBlock = 2048;
else
cbBlock = 512; /* Floppy. */
rc = drvHostBaseGetMediaSizeOs(pThis, &cbMedia);
if ( RT_SUCCESS(rc)
&& u64LbaStart < cbMedia / cbBlock)
{
if (puRegion)
*puRegion = 0;
if (pcBlocks)
*pcBlocks = cbMedia / cbBlock;
if (pcbBlock)
*pcbBlock = cbBlock;
if (penmDataForm)
*penmDataForm = VDREGIONDATAFORM_RAW;
}
else
rc = VERR_NOT_FOUND;
LogFlowFunc(("returns %Rrc\n", rc));
return rc;
}
/* -=-=-=-=- IMediaEx -=-=-=-=- */
DECLHIDDEN(int) drvHostBaseBufferRetain(PDRVHOSTBASE pThis, PDRVHOSTBASEREQ pReq, size_t cbBuf, bool fWrite, void **ppvBuf)
{
int rc = VINF_SUCCESS;
if (pThis->cbBuf < cbBuf)
{
RTMemFree(pThis->pvBuf);
pThis->cbBuf = 0;
pThis->pvBuf = RTMemAlloc(cbBuf);
if (pThis->pvBuf)
pThis->cbBuf = cbBuf;
else
rc = VERR_NO_MEMORY;
}
if (RT_SUCCESS(rc) && fWrite)
{
RTSGSEG Seg;
RTSGBUF SgBuf;
Seg.pvSeg = pThis->pvBuf;
Seg.cbSeg = cbBuf;
RTSgBufInit(&SgBuf, &Seg, 1);
rc = pThis->pDrvMediaExPort->pfnIoReqCopyToBuf(pThis->pDrvMediaExPort, (PDMMEDIAEXIOREQ)pReq,
&pReq->abAlloc[0], 0, &SgBuf, cbBuf);
}
if (RT_SUCCESS(rc))
*ppvBuf = pThis->pvBuf;
return rc;
}
DECLHIDDEN(int) drvHostBaseBufferRelease(PDRVHOSTBASE pThis, PDRVHOSTBASEREQ pReq, size_t cbBuf, bool fWrite, void *pvBuf)
{
int rc = VINF_SUCCESS;
if (!fWrite)
{
RTSGSEG Seg;
RTSGBUF SgBuf;
Seg.pvSeg = pvBuf;
Seg.cbSeg = cbBuf;
RTSgBufInit(&SgBuf, &Seg, 1);
rc = pThis->pDrvMediaExPort->pfnIoReqCopyFromBuf(pThis->pDrvMediaExPort, (PDMMEDIAEXIOREQ)pReq,
&pReq->abAlloc[0], 0, &SgBuf, cbBuf);
}
return rc;
}
/** @interface_method_impl{PDMIMEDIAEX,pfnQueryFeatures} */
static DECLCALLBACK(int) drvHostBaseQueryFeatures(PPDMIMEDIAEX pInterface, uint32_t *pfFeatures)
{
PDRVHOSTBASE pThis = RT_FROM_MEMBER(pInterface, DRVHOSTBASE, IMediaEx);
*pfFeatures = pThis->IMediaEx.pfnIoReqSendScsiCmd ? PDMIMEDIAEX_FEATURE_F_RAWSCSICMD : 0;
return VINF_SUCCESS;
}
/** @interface_method_impl{PDMIMEDIAEX,pfnNotifySuspend} */
static DECLCALLBACK(void) drvHostBaseNotifySuspend(PPDMIMEDIAEX pInterface)
{
RT_NOREF(pInterface); /* Nothing to do here. */
}
/** @interface_method_impl{PDMIMEDIAEX,pfnIoReqAllocSizeSet} */
static DECLCALLBACK(int) drvHostBaseIoReqAllocSizeSet(PPDMIMEDIAEX pInterface, size_t cbIoReqAlloc)
{
PDRVHOSTBASE pThis = RT_FROM_MEMBER(pInterface, DRVHOSTBASE, IMediaEx);
pThis->cbIoReqAlloc = RT_UOFFSETOF_DYN(DRVHOSTBASEREQ, abAlloc[cbIoReqAlloc]);
return VINF_SUCCESS;
}
/** @interface_method_impl{PDMIMEDIAEX,pfnIoReqAlloc} */
static DECLCALLBACK(int) drvHostBaseIoReqAlloc(PPDMIMEDIAEX pInterface, PPDMMEDIAEXIOREQ phIoReq, void **ppvIoReqAlloc,
PDMMEDIAEXIOREQID uIoReqId, uint32_t fFlags)
{
RT_NOREF2(uIoReqId, fFlags);
int rc = VINF_SUCCESS;
PDRVHOSTBASE pThis = RT_FROM_MEMBER(pInterface, DRVHOSTBASE, IMediaEx);
PDRVHOSTBASEREQ pReq = (PDRVHOSTBASEREQ)RTMemAllocZ(pThis->cbIoReqAlloc);
if (RT_LIKELY(pReq))
{
pReq->cbReq = 0;
pReq->cbResidual = 0;
*phIoReq = (PDMMEDIAEXIOREQ)pReq;
*ppvIoReqAlloc = &pReq->abAlloc[0];
}
else
rc = VERR_NO_MEMORY;
return rc;
}
/** @interface_method_impl{PDMIMEDIAEX,pfnIoReqFree} */
static DECLCALLBACK(int) drvHostBaseIoReqFree(PPDMIMEDIAEX pInterface, PDMMEDIAEXIOREQ hIoReq)
{
RT_NOREF1(pInterface);
PDRVHOSTBASEREQ pReq = (PDRVHOSTBASEREQ)hIoReq;
RTMemFree(pReq);
return VINF_SUCCESS;
}
/** @interface_method_impl{PDMIMEDIAEX,pfnIoReqQueryResidual} */
static DECLCALLBACK(int) drvHostBaseIoReqQueryResidual(PPDMIMEDIAEX pInterface, PDMMEDIAEXIOREQ hIoReq, size_t *pcbResidual)
{
RT_NOREF1(pInterface);
PDRVHOSTBASEREQ pReq = (PDRVHOSTBASEREQ)hIoReq;
*pcbResidual = pReq->cbResidual;
return VINF_SUCCESS;
}
/** @interface_method_impl{PDMIMEDIAEX,pfnIoReqQueryXferSize} */
static DECLCALLBACK(int) drvHostBaseIoReqQueryXferSize(PPDMIMEDIAEX pInterface, PDMMEDIAEXIOREQ hIoReq, size_t *pcbXfer)
{
RT_NOREF1(pInterface);
PDRVHOSTBASEREQ pReq = (PDRVHOSTBASEREQ)hIoReq;
*pcbXfer = pReq->cbReq;
return VINF_SUCCESS;
}
/** @interface_method_impl{PDMIMEDIAEX,pfnIoReqCancelAll} */
static DECLCALLBACK(int) drvHostBaseIoReqCancelAll(PPDMIMEDIAEX pInterface)
{
RT_NOREF1(pInterface);
return VINF_SUCCESS;
}
/** @interface_method_impl{PDMIMEDIAEX,pfnIoReqCancel} */
static DECLCALLBACK(int) drvHostBaseIoReqCancel(PPDMIMEDIAEX pInterface, PDMMEDIAEXIOREQID uIoReqId)
{
RT_NOREF2(pInterface, uIoReqId);
return VERR_PDM_MEDIAEX_IOREQID_NOT_FOUND;
}
/** @interface_method_impl{PDMIMEDIAEX,pfnIoReqRead} */
static DECLCALLBACK(int) drvHostBaseIoReqRead(PPDMIMEDIAEX pInterface, PDMMEDIAEXIOREQ hIoReq, uint64_t off, size_t cbRead)
{
PDRVHOSTBASE pThis = RT_FROM_MEMBER(pInterface, DRVHOSTBASE, IMediaEx);
PDRVHOSTBASEREQ pReq = (PDRVHOSTBASEREQ)hIoReq;
LogFlow(("%s-%d: drvHostBaseIoReqRead: off=%#llx cbRead=%#x (%s)\n",
pThis->pDrvIns->pReg->szName, pThis->pDrvIns->iInstance, off, cbRead, pThis->pszDevice));
RTCritSectEnter(&pThis->CritSect);
pReq->cbReq = cbRead;
pReq->cbResidual = cbRead;
STAM_REL_COUNTER_INC(&pThis->StatReqsSubmitted);
STAM_REL_COUNTER_INC(&pThis->StatReqsRead);
/*
* Check the state.
*/
int rc;
if (pThis->fMediaPresent)
{
void *pvBuf;
rc = drvHostBaseBufferRetain(pThis, pReq, cbRead, false, &pvBuf);
if (RT_SUCCESS(rc))
{
/*
* Seek and read.
*/
rc = drvHostBaseReadOs(pThis, off, pvBuf, cbRead);
if (RT_SUCCESS(rc))
{
Log2(("%s-%d: drvHostBaseReadOs: off=%#llx cbRead=%#x\n"
"%16.*Rhxd\n",
pThis->pDrvIns->pReg->szName, pThis->pDrvIns->iInstance, off, cbRead, cbRead, pvBuf));
pReq->cbResidual = 0;
}
else
Log(("%s-%d: drvHostBaseIoReqRead: drvHostBaseReadOs(%#llx, %p, %#x) -> %Rrc ('%s')\n",
pThis->pDrvIns->pReg->szName, pThis->pDrvIns->iInstance,
off, pvBuf, cbRead, rc, pThis->pszDevice));
rc = drvHostBaseBufferRelease(pThis, pReq, cbRead, false, pvBuf);
}
else
Log(("%s-%d: drvHostBaseIoReqRead: drvHostBaseBufferRetain(%#llx, %p, %#x) -> %Rrc ('%s')\n",
pThis->pDrvIns->pReg->szName, pThis->pDrvIns->iInstance,
off, pvBuf, cbRead, rc, pThis->pszDevice));
}
else
rc = VERR_MEDIA_NOT_PRESENT;
if (RT_SUCCESS(rc))
{
STAM_REL_COUNTER_INC(&pThis->StatReqsSucceeded);
STAM_REL_COUNTER_INC(&pThis->StatBytesRead);
}
else
STAM_REL_COUNTER_INC(&pThis->StatReqsFailed);
RTCritSectLeave(&pThis->CritSect);
LogFlow(("%s-%d: drvHostBaseIoReqRead: returns %Rrc\n", pThis->pDrvIns->pReg->szName, pThis->pDrvIns->iInstance, rc));
return rc;
}
/** @interface_method_impl{PDMIMEDIAEX,pfnIoReqWrite} */
static DECLCALLBACK(int) drvHostBaseIoReqWrite(PPDMIMEDIAEX pInterface, PDMMEDIAEXIOREQ hIoReq, uint64_t off, size_t cbWrite)
{
PDRVHOSTBASE pThis = RT_FROM_MEMBER(pInterface, DRVHOSTBASE, IMediaEx);
PDRVHOSTBASEREQ pReq = (PDRVHOSTBASEREQ)hIoReq;
LogFlow(("%s-%d: drvHostBaseIoReqWrite: off=%#llx cbWrite=%#x (%s)\n",
pThis->pDrvIns->pReg->szName, pThis->pDrvIns->iInstance, off, cbWrite, pThis->pszDevice));
RTCritSectEnter(&pThis->CritSect);
pReq->cbReq = cbWrite;
pReq->cbResidual = cbWrite;
STAM_REL_COUNTER_INC(&pThis->StatReqsSubmitted);
STAM_REL_COUNTER_INC(&pThis->StatReqsWrite);
/*
* Check the state.
*/
int rc;
if (!pThis->fReadOnly)
{
if (pThis->fMediaPresent)
{
void *pvBuf;
rc = drvHostBaseBufferRetain(pThis, pReq, cbWrite, true, &pvBuf);
if (RT_SUCCESS(rc))
{
Log2(("%s-%d: drvHostBaseIoReqWrite: off=%#llx cbWrite=%#x\n"
"%16.*Rhxd\n",
pThis->pDrvIns->pReg->szName, pThis->pDrvIns->iInstance, off, cbWrite, cbWrite, pvBuf));
/*
* Seek and write.
*/
rc = drvHostBaseWriteOs(pThis, off, pvBuf, cbWrite);
if (RT_FAILURE(rc))
Log(("%s-%d: drvHostBaseIoReqWrite: drvHostBaseWriteOs(%#llx, %p, %#x) -> %Rrc ('%s')\n",
pThis->pDrvIns->pReg->szName, pThis->pDrvIns->iInstance,
off, pvBuf, cbWrite, rc, pThis->pszDevice));
else
pReq->cbResidual = 0;
rc = drvHostBaseBufferRelease(pThis, pReq, cbWrite, true, pvBuf);
}
}
else
rc = VERR_MEDIA_NOT_PRESENT;
}
else
rc = VERR_WRITE_PROTECT;
if (RT_SUCCESS(rc))
{
STAM_REL_COUNTER_INC(&pThis->StatReqsSucceeded);
STAM_REL_COUNTER_INC(&pThis->StatBytesWritten);
}
else
STAM_REL_COUNTER_INC(&pThis->StatReqsFailed);
RTCritSectLeave(&pThis->CritSect);
LogFlow(("%s-%d: drvHostBaseIoReqWrite: returns %Rrc\n", pThis->pDrvIns->pReg->szName, pThis->pDrvIns->iInstance, rc));
return rc;
}
/** @interface_method_impl{PDMIMEDIAEX,pfnIoReqFlush} */
static DECLCALLBACK(int) drvHostBaseIoReqFlush(PPDMIMEDIAEX pInterface, PDMMEDIAEXIOREQ hIoReq)
{
RT_NOREF1(hIoReq);
int rc;
PDRVHOSTBASE pThis = RT_FROM_MEMBER(pInterface, DRVHOSTBASE, IMediaEx);
LogFlow(("%s-%d: drvHostBaseIoReqFlush: (%s)\n",
pThis->pDrvIns->pReg->szName, pThis->pDrvIns->iInstance, pThis->pszDevice));
RTCritSectEnter(&pThis->CritSect);
STAM_REL_COUNTER_INC(&pThis->StatReqsSubmitted);
STAM_REL_COUNTER_INC(&pThis->StatReqsFlush);
if (pThis->fMediaPresent)
rc = drvHostBaseFlushOs(pThis);
else
rc = VERR_MEDIA_NOT_PRESENT;
if (RT_SUCCESS(rc))
STAM_REL_COUNTER_INC(&pThis->StatReqsSucceeded);
else
STAM_REL_COUNTER_INC(&pThis->StatReqsFailed);
RTCritSectLeave(&pThis->CritSect);
LogFlow(("%s-%d: drvHostBaseFlush: returns %Rrc\n", pThis->pDrvIns->pReg->szName, pThis->pDrvIns->iInstance, rc));
return rc;
}
/** @interface_method_impl{PDMIMEDIAEX,pfnIoReqDiscard} */
static DECLCALLBACK(int) drvHostBaseIoReqDiscard(PPDMIMEDIAEX pInterface, PDMMEDIAEXIOREQ hIoReq, unsigned cRangesMax)
{
RT_NOREF3(pInterface, hIoReq, cRangesMax);
return VERR_NOT_SUPPORTED;
}
/** @interface_method_impl{PDMIMEDIAEX,pfnIoReqGetActiveCount} */
static DECLCALLBACK(uint32_t) drvHostBaseIoReqGetActiveCount(PPDMIMEDIAEX pInterface)
{
RT_NOREF1(pInterface);
return 0;
}
/** @interface_method_impl{PDMIMEDIAEX,pfnIoReqGetSuspendedCount} */
static DECLCALLBACK(uint32_t) drvHostBaseIoReqGetSuspendedCount(PPDMIMEDIAEX pInterface)
{
RT_NOREF1(pInterface);
return 0;
}
/** @interface_method_impl{PDMIMEDIAEX,pfnIoReqQuerySuspendedStart} */
static DECLCALLBACK(int) drvHostBaseIoReqQuerySuspendedStart(PPDMIMEDIAEX pInterface, PPDMMEDIAEXIOREQ phIoReq, void **ppvIoReqAlloc)
{
RT_NOREF3(pInterface, phIoReq, ppvIoReqAlloc);
return VERR_NOT_IMPLEMENTED;
}
/** @interface_method_impl{PDMIMEDIAEX,pfnIoReqQuerySuspendedNext} */
static DECLCALLBACK(int) drvHostBaseIoReqQuerySuspendedNext(PPDMIMEDIAEX pInterface, PDMMEDIAEXIOREQ hIoReq,
PPDMMEDIAEXIOREQ phIoReqNext, void **ppvIoReqAllocNext)
{
RT_NOREF4(pInterface, hIoReq, phIoReqNext, ppvIoReqAllocNext);
return VERR_NOT_IMPLEMENTED;
}
/** @interface_method_impl{PDMIMEDIAEX,pfnIoReqSuspendedSave} */
static DECLCALLBACK(int) drvHostBaseIoReqSuspendedSave(PPDMIMEDIAEX pInterface, PSSMHANDLE pSSM, PDMMEDIAEXIOREQ hIoReq)
{
RT_NOREF3(pInterface, pSSM, hIoReq);
return VERR_NOT_IMPLEMENTED;
}
/** @interface_method_impl{PDMIMEDIAEX,pfnIoReqSuspendedLoad} */
static DECLCALLBACK(int) drvHostBaseIoReqSuspendedLoad(PPDMIMEDIAEX pInterface, PSSMHANDLE pSSM, PDMMEDIAEXIOREQ hIoReq)
{
RT_NOREF3(pInterface, pSSM, hIoReq);
return VERR_NOT_IMPLEMENTED;
}
/* -=-=-=-=- IMount -=-=-=-=- */
/** @interface_method_impl{PDMIMOUNT,pfnUnmount} */
static DECLCALLBACK(int) drvHostBaseUnmount(PPDMIMOUNT pInterface, bool fForce, bool fEject)
{
RT_NOREF(fEject);
/* While we're not mountable (see drvHostBaseMount), we're unmountable. */
PDRVHOSTBASE pThis = RT_FROM_MEMBER(pInterface, DRVHOSTBASE, IMount);
RTCritSectEnter(&pThis->CritSect);
/*
* Validate state.
*/
int rc = VINF_SUCCESS;
if (!pThis->fLocked || fForce)
{
/* Unlock drive if necessary. */
if (pThis->fLocked)
{
if (pThis->pfnDoLock)
rc = pThis->pfnDoLock(pThis, false);
if (RT_SUCCESS(rc))
pThis->fLocked = false;
}
if (fEject)
{
/*
* Eject the disc.
*/
rc = drvHostBaseEjectOs(pThis);
}
/*
* Media is no longer present.
*/
DRVHostBaseMediaNotPresent(pThis);
}
else
{
Log(("drvHostBaseUnmount: Locked\n"));
rc = VERR_PDM_MEDIA_LOCKED;
}
RTCritSectLeave(&pThis->CritSect);
LogFlow(("drvHostBaseUnmount: returns %Rrc\n", rc));
return rc;
}
/** @interface_method_impl{PDMIMOUNT,pfnIsMounted} */
static DECLCALLBACK(bool) drvHostBaseIsMounted(PPDMIMOUNT pInterface)
{
PDRVHOSTBASE pThis = RT_FROM_MEMBER(pInterface, DRVHOSTBASE, IMount);
RTCritSectEnter(&pThis->CritSect);
bool fRc = pThis->fMediaPresent;
RTCritSectLeave(&pThis->CritSect);
return fRc;
}
/** @interface_method_impl{PDMIMOUNT,pfnIsLocked} */
static DECLCALLBACK(int) drvHostBaseLock(PPDMIMOUNT pInterface)
{
PDRVHOSTBASE pThis = RT_FROM_MEMBER(pInterface, DRVHOSTBASE, IMount);
RTCritSectEnter(&pThis->CritSect);
int rc = VINF_SUCCESS;
if (!pThis->fLocked)
{
if (pThis->pfnDoLock)
{
rc = pThis->pfnDoLock(pThis, true);
if (RT_SUCCESS(rc))
pThis->fLocked = true;
}
}
else
LogFlow(("%s-%d: drvHostBaseLock: already locked\n", pThis->pDrvIns->pReg->szName, pThis->pDrvIns->iInstance));
RTCritSectLeave(&pThis->CritSect);
LogFlow(("%s-%d: drvHostBaseLock: returns %Rrc\n", pThis->pDrvIns->pReg->szName, pThis->pDrvIns->iInstance, rc));
return rc;
}
/** @interface_method_impl{PDMIMOUNT,pfnIsLocked} */
static DECLCALLBACK(int) drvHostBaseUnlock(PPDMIMOUNT pInterface)
{
PDRVHOSTBASE pThis = RT_FROM_MEMBER(pInterface, DRVHOSTBASE, IMount);
RTCritSectEnter(&pThis->CritSect);
int rc = VINF_SUCCESS;
if (pThis->fLocked)
{
if (pThis->pfnDoLock)
rc = pThis->pfnDoLock(pThis, false);
if (RT_SUCCESS(rc))
pThis->fLocked = false;
}
else
LogFlow(("%s-%d: drvHostBaseUnlock: not locked\n", pThis->pDrvIns->pReg->szName, pThis->pDrvIns->iInstance));
RTCritSectLeave(&pThis->CritSect);
LogFlow(("%s-%d: drvHostBaseUnlock: returns %Rrc\n", pThis->pDrvIns->pReg->szName, pThis->pDrvIns->iInstance, rc));
return rc;
}
/** @interface_method_impl{PDMIMOUNT,pfnIsLocked} */
static DECLCALLBACK(bool) drvHostBaseIsLocked(PPDMIMOUNT pInterface)
{
PDRVHOSTBASE pThis = RT_FROM_MEMBER(pInterface, DRVHOSTBASE, IMount);
RTCritSectEnter(&pThis->CritSect);
bool fRc = pThis->fLocked;
RTCritSectLeave(&pThis->CritSect);
return fRc;
}
/* -=-=-=-=- IBase -=-=-=-=- */
/**
* @interface_method_impl{PDMIBASE,pfnQueryInterface}
*/
static DECLCALLBACK(void *) drvHostBaseQueryInterface(PPDMIBASE pInterface, const char *pszIID)
{
PPDMDRVINS pDrvIns = PDMIBASE_2_PDMDRV(pInterface);
PDRVHOSTBASE pThis = PDMINS_2_DATA(pDrvIns, PDRVHOSTBASE);
PDMIBASE_RETURN_INTERFACE(pszIID, PDMIBASE, &pDrvIns->IBase);
PDMIBASE_RETURN_INTERFACE(pszIID, PDMIMEDIA, &pThis->IMedia);
PDMIBASE_RETURN_INTERFACE(pszIID, PDMIMOUNT, &pThis->IMount);
PDMIBASE_RETURN_INTERFACE(pszIID, PDMIMEDIAEX, pThis->pDrvMediaExPort ? &pThis->IMediaEx : NULL);
return NULL;
}
/* -=-=-=-=- poller thread -=-=-=-=- */
/**
* Media present.
* Query the size and notify the above driver / device.
*
* @param pThis The instance data.
*/
DECLHIDDEN(int) DRVHostBaseMediaPresent(PDRVHOSTBASE pThis)
{
/*
* Open the drive.
*/
int rc = drvHostBaseMediaRefreshOs(pThis);
if (RT_FAILURE(rc))
return rc;
/*
* Determine the size.
*/
uint64_t cb;
rc = drvHostBaseGetMediaSizeOs(pThis, &cb);
if (RT_FAILURE(rc))
{
LogFlow(("%s-%d: failed to figure media size of %s, rc=%Rrc\n",
pThis->pDrvIns->pReg->szName, pThis->pDrvIns->iInstance, pThis->pszDevice, rc));
return rc;
}
/*
* Update the data and inform the unit.
*/
pThis->cbSize = cb;
pThis->fMediaPresent = true;
if (pThis->pDrvMountNotify)
pThis->pDrvMountNotify->pfnMountNotify(pThis->pDrvMountNotify);
LogFlow(("%s-%d: drvHostBaseMediaPresent: cbSize=%lld (%#llx)\n",
pThis->pDrvIns->pReg->szName, pThis->pDrvIns->iInstance, pThis->cbSize, pThis->cbSize));
return VINF_SUCCESS;
}
/**
* Media no longer present.
* @param pThis The instance data.
*/
DECLHIDDEN(void) DRVHostBaseMediaNotPresent(PDRVHOSTBASE pThis)
{
pThis->fMediaPresent = false;
pThis->fLocked = false;
pThis->PCHSGeometry.cCylinders = 0;
pThis->PCHSGeometry.cHeads = 0;
pThis->PCHSGeometry.cSectors = 0;
pThis->LCHSGeometry.cCylinders = 0;
pThis->LCHSGeometry.cHeads = 0;
pThis->LCHSGeometry.cSectors = 0;
if (pThis->pDrvMountNotify)
pThis->pDrvMountNotify->pfnUnmountNotify(pThis->pDrvMountNotify);
}
static int drvHostBaseMediaPoll(PDRVHOSTBASE pThis)
{
/*
* Poll for media change.
*/
bool fMediaPresent = false;
bool fMediaChanged = false;
drvHostBaseQueryMediaStatusOs(pThis, &fMediaChanged, &fMediaPresent);
RTCritSectEnter(&pThis->CritSect);
int rc = VINF_SUCCESS;
if (pThis->fMediaPresent != fMediaPresent)
{
LogFlow(("drvHostDvdPoll: %d -> %d\n", pThis->fMediaPresent, fMediaPresent));
pThis->fMediaPresent = false;
if (fMediaPresent)
rc = DRVHostBaseMediaPresent(pThis);
else
DRVHostBaseMediaNotPresent(pThis);
}
else if (fMediaPresent)
{
/*
* Poll for media change.
*/
if (fMediaChanged)
{
LogFlow(("drvHostDVDMediaThread: Media changed!\n"));
DRVHostBaseMediaNotPresent(pThis);
rc = DRVHostBaseMediaPresent(pThis);
}
}
RTCritSectLeave(&pThis->CritSect);
return rc;
}
/**
* This thread will periodically poll the device for media presence.
*
* @returns Ignored.
* @param ThreadSelf Handle of this thread. Ignored.
* @param pvUser Pointer to the driver instance structure.
*/
static DECLCALLBACK(int) drvHostBaseMediaThread(RTTHREAD ThreadSelf, void *pvUser)
{
PDRVHOSTBASE pThis = (PDRVHOSTBASE)pvUser;
LogFlow(("%s-%d: drvHostBaseMediaThread: ThreadSelf=%p pvUser=%p\n",
pThis->pDrvIns->pReg->szName, pThis->pDrvIns->iInstance, ThreadSelf, pvUser));
bool fFirst = true;
int cRetries = 10;
while (!pThis->fShutdownPoller)
{
/*
* Perform the polling (unless we've run out of 50ms retries).
*/
if (cRetries-- > 0)
{
int rc = drvHostBaseMediaPoll(pThis);
if (RT_FAILURE(rc))
{
RTSemEventWait(pThis->EventPoller, 50);
continue;
}
}
/*
* Signal EMT after the first go.
*/
if (fFirst)
{
RTThreadUserSignal(ThreadSelf);
fFirst = false;
}
/*
* Sleep.
*/
int rc = RTSemEventWait(pThis->EventPoller, pThis->cMilliesPoller);
if ( RT_FAILURE(rc)
&& rc != VERR_TIMEOUT)
{
AssertMsgFailed(("rc=%Rrc\n", rc));
pThis->ThreadPoller = NIL_RTTHREAD;
LogFlow(("drvHostBaseMediaThread: returns %Rrc\n", rc));
return rc;
}
cRetries = 10;
}
/* (Don't clear the thread handle here, the destructor thread is using it to wait.) */
LogFlow(("%s-%d: drvHostBaseMediaThread: returns VINF_SUCCESS\n", pThis->pDrvIns->pReg->szName, pThis->pDrvIns->iInstance));
return VINF_SUCCESS;
}
/**
* Registers statistics associated with the given media driver.
*
* @returns VBox status code.
* @param pThis The media driver instance.
*/
static int drvHostBaseStatsRegister(PDRVHOSTBASE pThis)
{
PPDMDRVINS pDrvIns = pThis->pDrvIns;
uint32_t iInstance, iLUN;
const char *pcszController;
int rc = pThis->pDrvMediaPort->pfnQueryDeviceLocation(pThis->pDrvMediaPort, &pcszController,
&iInstance, &iLUN);
if (RT_SUCCESS(rc))
{
char *pszCtrlUpper = RTStrDup(pcszController);
if (pszCtrlUpper)
{
RTStrToUpper(pszCtrlUpper);
PDMDrvHlpSTAMRegisterF(pDrvIns, &pThis->StatBytesRead, STAMTYPE_COUNTER, STAMVISIBILITY_USED, STAMUNIT_BYTES,
"Amount of data read.", "/Devices/%s%u/Port%u/ReadBytes", pszCtrlUpper, iInstance, iLUN);
PDMDrvHlpSTAMRegisterF(pDrvIns, &pThis->StatBytesWritten, STAMTYPE_COUNTER, STAMVISIBILITY_USED, STAMUNIT_BYTES,
"Amount of data written.", "/Devices/%s%u/Port%u/WrittenBytes", pszCtrlUpper, iInstance, iLUN);
PDMDrvHlpSTAMRegisterF(pDrvIns, &pThis->StatReqsSubmitted, STAMTYPE_COUNTER, STAMVISIBILITY_USED, STAMUNIT_COUNT,
"Number of I/O requests submitted.", "/Devices/%s%u/Port%u/ReqsSubmitted", pszCtrlUpper, iInstance, iLUN);
PDMDrvHlpSTAMRegisterF(pDrvIns, &pThis->StatReqsFailed, STAMTYPE_COUNTER, STAMVISIBILITY_USED, STAMUNIT_COUNT,
"Number of I/O requests failed.", "/Devices/%s%u/Port%u/ReqsFailed", pszCtrlUpper, iInstance, iLUN);
PDMDrvHlpSTAMRegisterF(pDrvIns, &pThis->StatReqsSucceeded, STAMTYPE_COUNTER, STAMVISIBILITY_USED, STAMUNIT_COUNT,
"Number of I/O requests succeeded.", "/Devices/%s%u/Port%u/ReqsSucceeded", pszCtrlUpper, iInstance, iLUN);
PDMDrvHlpSTAMRegisterF(pDrvIns, &pThis->StatReqsFlush, STAMTYPE_COUNTER, STAMVISIBILITY_USED, STAMUNIT_COUNT,
"Number of flush I/O requests submitted.", "/Devices/%s%u/Port%u/ReqsFlush", pszCtrlUpper, iInstance, iLUN);
PDMDrvHlpSTAMRegisterF(pDrvIns, &pThis->StatReqsWrite, STAMTYPE_COUNTER, STAMVISIBILITY_USED, STAMUNIT_COUNT,
"Number of write I/O requests submitted.", "/Devices/%s%u/Port%u/ReqsWrite", pszCtrlUpper, iInstance, iLUN);
PDMDrvHlpSTAMRegisterF(pDrvIns, &pThis->StatReqsRead, STAMTYPE_COUNTER, STAMVISIBILITY_USED, STAMUNIT_COUNT,
"Number of read I/O requests submitted.", "/Devices/%s%u/Port%u/ReqsRead", pszCtrlUpper, iInstance, iLUN);
RTStrFree(pszCtrlUpper);
}
else
rc = VERR_NO_STR_MEMORY;
}
return rc;
}
/**
* Deregisters statistics associated with the given media driver.
*
* @param pThis The media driver instance.
*/
static void drvhostBaseStatsDeregister(PDRVHOSTBASE pThis)
{
PPDMDRVINS pDrvIns = pThis->pDrvIns;
PDMDrvHlpSTAMDeregister(pDrvIns, &pThis->StatBytesRead);
PDMDrvHlpSTAMDeregister(pDrvIns, &pThis->StatBytesWritten);
PDMDrvHlpSTAMDeregister(pDrvIns, &pThis->StatReqsSubmitted);
PDMDrvHlpSTAMDeregister(pDrvIns, &pThis->StatReqsFailed);
PDMDrvHlpSTAMDeregister(pDrvIns, &pThis->StatReqsSucceeded);
PDMDrvHlpSTAMDeregister(pDrvIns, &pThis->StatReqsFlush);
PDMDrvHlpSTAMDeregister(pDrvIns, &pThis->StatReqsWrite);
PDMDrvHlpSTAMDeregister(pDrvIns, &pThis->StatReqsRead);
}
/* -=-=-=-=- driver interface -=-=-=-=- */
/**
* Done state load operation.
*
* @returns VBox load code.
* @param pDrvIns Driver instance of the driver which registered the data unit.
* @param pSSM SSM operation handle.
*/
static DECLCALLBACK(int) drvHostBaseLoadDone(PPDMDRVINS pDrvIns, PSSMHANDLE pSSM)
{
RT_NOREF(pSSM);
PDRVHOSTBASE pThis = PDMINS_2_DATA(pDrvIns, PDRVHOSTBASE);
LogFlow(("%s-%d: drvHostBaseMediaThread:\n", pThis->pDrvIns->pReg->szName, pThis->pDrvIns->iInstance));
RTCritSectEnter(&pThis->CritSect);
/*
* Tell the device/driver above us that the media status is uncertain.
*/
if (pThis->pDrvMountNotify)
{
pThis->pDrvMountNotify->pfnUnmountNotify(pThis->pDrvMountNotify);
if (pThis->fMediaPresent)
pThis->pDrvMountNotify->pfnMountNotify(pThis->pDrvMountNotify);
}
RTCritSectLeave(&pThis->CritSect);
return VINF_SUCCESS;
}
/** @copydoc FNPDMDRVDESTRUCT */
DECLCALLBACK(void) DRVHostBaseDestruct(PPDMDRVINS pDrvIns)
{
PDRVHOSTBASE pThis = PDMINS_2_DATA(pDrvIns, PDRVHOSTBASE);
LogFlow(("%s-%d: drvHostBaseDestruct: iInstance=%d\n", pDrvIns->pReg->szName, pDrvIns->iInstance, pDrvIns->iInstance));
/*
* Terminate the thread.
*/
if (pThis->ThreadPoller != NIL_RTTHREAD)
{
pThis->fShutdownPoller = true;
int rc;
int cTimes = 50;
do
{
RTSemEventSignal(pThis->EventPoller);
rc = RTThreadWait(pThis->ThreadPoller, 100, NULL);
} while (cTimes-- > 0 && rc == VERR_TIMEOUT);
if (!rc)
pThis->ThreadPoller = NIL_RTTHREAD;
}
/*
* Cleanup the other resources.
*/
drvHostBaseDestructOs(pThis);
if (pThis->EventPoller != NULL)
{
RTSemEventDestroy(pThis->EventPoller);
pThis->EventPoller = NULL;
}
if (pThis->pszDevice)
{
PDMDrvHlpMMHeapFree(pDrvIns, pThis->pszDevice);
pThis->pszDevice = NULL;
}
if (pThis->pszDeviceOpen)
{
RTStrFree(pThis->pszDeviceOpen);
pThis->pszDeviceOpen = NULL;
}
if (pThis->pvBuf)
{
RTMemFree(pThis->pvBuf);
pThis->pvBuf = NULL;
pThis->cbBuf = 0;
}
/* Forget about the notifications. */
pThis->pDrvMountNotify = NULL;
drvhostBaseStatsDeregister(pThis);
/* Leave the instance operational if this is just a cleanup of the state
* after an attach error happened. So don't destroy the critsect then. */
if (!pThis->fKeepInstance && RTCritSectIsInitialized(&pThis->CritSect))
RTCritSectDelete(&pThis->CritSect);
LogFlow(("%s-%d: drvHostBaseDestruct completed\n", pDrvIns->pReg->szName, pDrvIns->iInstance));
}
/**
* Initializes the instance data .
*
* On failure call DRVHostBaseDestruct().
*
* @returns VBox status code.
* @param pDrvIns Driver instance.
* @param pszCfgValid Pointer to a string of valid CFGM options.
* @param pCfg Configuration handle.
* @param enmType Device type.
*/
DECLHIDDEN(int) DRVHostBaseInit(PPDMDRVINS pDrvIns, PCFGMNODE pCfg, const char *pszCfgValid, PDMMEDIATYPE enmType)
{
int src = VINF_SUCCESS;
PDRVHOSTBASE pThis = PDMINS_2_DATA(pDrvIns, PDRVHOSTBASE);
PCPDMDRVHLPR3 pHlp = pDrvIns->pHlpR3;
LogFlow(("%s-%d: DRVHostBaseInit: iInstance=%d\n", pDrvIns->pReg->szName, pDrvIns->iInstance, pDrvIns->iInstance));
/*
* Initialize most of the data members.
*/
pThis->pDrvIns = pDrvIns;
pThis->fKeepInstance = false;
pThis->ThreadPoller = NIL_RTTHREAD;
pThis->enmType = enmType;
pThis->fAttachFailError = true; /* It's an error until we've read the config. */
/* IBase. */
pDrvIns->IBase.pfnQueryInterface = drvHostBaseQueryInterface;
/* IMedia. */
pThis->IMedia.pfnRead = drvHostBaseRead;
pThis->IMedia.pfnWrite = drvHostBaseWrite;
pThis->IMedia.pfnFlush = drvHostBaseFlush;
pThis->IMedia.pfnIsReadOnly = drvHostBaseIsReadOnly;
pThis->IMedia.pfnIsNonRotational = drvHostBaseIsNonRotational;
pThis->IMedia.pfnGetSize = drvHostBaseGetSize;
pThis->IMedia.pfnGetType = drvHostBaseGetType;
pThis->IMedia.pfnGetUuid = drvHostBaseGetUuid;
pThis->IMedia.pfnBiosGetPCHSGeometry = drvHostBaseGetPCHSGeometry;
pThis->IMedia.pfnBiosSetPCHSGeometry = drvHostBaseSetPCHSGeometry;
pThis->IMedia.pfnBiosGetLCHSGeometry = drvHostBaseGetLCHSGeometry;
pThis->IMedia.pfnBiosSetLCHSGeometry = drvHostBaseSetLCHSGeometry;
pThis->IMedia.pfnBiosIsVisible = drvHostBaseIsVisible;
pThis->IMedia.pfnGetRegionCount = drvHostBaseGetRegionCount;
pThis->IMedia.pfnQueryRegionProperties = drvHostBaseQueryRegionProperties;
pThis->IMedia.pfnQueryRegionPropertiesForLba = drvHostBaseQueryRegionPropertiesForLba;
/* IMediaEx */
pThis->IMediaEx.pfnQueryFeatures = drvHostBaseQueryFeatures;
pThis->IMediaEx.pfnNotifySuspend = drvHostBaseNotifySuspend;
pThis->IMediaEx.pfnIoReqAllocSizeSet = drvHostBaseIoReqAllocSizeSet;
pThis->IMediaEx.pfnIoReqAlloc = drvHostBaseIoReqAlloc;
pThis->IMediaEx.pfnIoReqFree = drvHostBaseIoReqFree;
pThis->IMediaEx.pfnIoReqQueryResidual = drvHostBaseIoReqQueryResidual;
pThis->IMediaEx.pfnIoReqQueryXferSize = drvHostBaseIoReqQueryXferSize;
pThis->IMediaEx.pfnIoReqCancelAll = drvHostBaseIoReqCancelAll;
pThis->IMediaEx.pfnIoReqCancel = drvHostBaseIoReqCancel;
pThis->IMediaEx.pfnIoReqRead = drvHostBaseIoReqRead;
pThis->IMediaEx.pfnIoReqWrite = drvHostBaseIoReqWrite;
pThis->IMediaEx.pfnIoReqFlush = drvHostBaseIoReqFlush;
pThis->IMediaEx.pfnIoReqDiscard = drvHostBaseIoReqDiscard;
pThis->IMediaEx.pfnIoReqGetActiveCount = drvHostBaseIoReqGetActiveCount;
pThis->IMediaEx.pfnIoReqGetSuspendedCount = drvHostBaseIoReqGetSuspendedCount;
pThis->IMediaEx.pfnIoReqQuerySuspendedStart = drvHostBaseIoReqQuerySuspendedStart;
pThis->IMediaEx.pfnIoReqQuerySuspendedNext = drvHostBaseIoReqQuerySuspendedNext;
pThis->IMediaEx.pfnIoReqSuspendedSave = drvHostBaseIoReqSuspendedSave;
pThis->IMediaEx.pfnIoReqSuspendedLoad = drvHostBaseIoReqSuspendedLoad;
/* IMount. */
pThis->IMount.pfnUnmount = drvHostBaseUnmount;
pThis->IMount.pfnIsMounted = drvHostBaseIsMounted;
pThis->IMount.pfnLock = drvHostBaseLock;
pThis->IMount.pfnUnlock = drvHostBaseUnlock;
pThis->IMount.pfnIsLocked = drvHostBaseIsLocked;
drvHostBaseInitOs(pThis);
if (!pHlp->pfnCFGMAreValuesValid(pCfg, pszCfgValid))
{
pThis->fAttachFailError = true;
return VERR_PDM_DRVINS_UNKNOWN_CFG_VALUES;
}
/*
* Get the IMediaPort & IMountNotify interfaces of the above driver/device.
*/
pThis->pDrvMediaPort = PDMIBASE_QUERY_INTERFACE(pDrvIns->pUpBase, PDMIMEDIAPORT);
if (!pThis->pDrvMediaPort)
{
AssertMsgFailed(("Configuration error: No media port interface above!\n"));
return VERR_PDM_MISSING_INTERFACE_ABOVE;
}
pThis->pDrvMediaExPort = PDMIBASE_QUERY_INTERFACE(pDrvIns->pUpBase, PDMIMEDIAEXPORT);
pThis->pDrvMountNotify = PDMIBASE_QUERY_INTERFACE(pDrvIns->pUpBase, PDMIMOUNTNOTIFY);
/*
* Query configuration.
*/
/* Device */
int rc = pHlp->pfnCFGMQueryStringAlloc(pCfg, "Path", &pThis->pszDevice);
if (RT_FAILURE(rc))
{
AssertMsgFailed(("Configuration error: query for \"Path\" string returned %Rra.\n", rc));
return rc;
}
/* Mountable */
uint32_t u32;
rc = pHlp->pfnCFGMQueryU32Def(pCfg, "Interval", &u32, 1000);
if (RT_SUCCESS(rc))
pThis->cMilliesPoller = u32;
else
{
AssertMsgFailed(("Configuration error: Query \"Mountable\" resulted in %Rrc.\n", rc));
return rc;
}
/* ReadOnly - passthrough mode requires read/write access in any case. */
if ( (pThis->enmType == PDMMEDIATYPE_CDROM || pThis->enmType == PDMMEDIATYPE_DVD)
&& pThis->IMedia.pfnSendCmd)
pThis->fReadOnlyConfig = false;
else
{
rc = pHlp->pfnCFGMQueryBoolDef(pCfg, "ReadOnly", &pThis->fReadOnlyConfig,
enmType == PDMMEDIATYPE_DVD || enmType == PDMMEDIATYPE_CDROM
? true
: false);
if (RT_FAILURE(rc))
{
AssertMsgFailed(("Configuration error: Query \"ReadOnly\" resulted in %Rrc.\n", rc));
return rc;
}
}
/* Locked */
rc = pHlp->pfnCFGMQueryBoolDef(pCfg, "Locked", &pThis->fLocked, false);
if (RT_FAILURE(rc))
{
AssertMsgFailed(("Configuration error: Query \"Locked\" resulted in %Rrc.\n", rc));
return rc;
}
/* BIOS visible */
rc = pHlp->pfnCFGMQueryBoolDef(pCfg, "BIOSVisible", &pThis->fBiosVisible, true);
if (RT_FAILURE(rc))
{
AssertMsgFailed(("Configuration error: Query \"BIOSVisible\" resulted in %Rrc.\n", rc));
return rc;
}
/* Uuid */
char *psz;
rc = pHlp->pfnCFGMQueryStringAlloc(pCfg, "Uuid", &psz);
if (rc == VERR_CFGM_VALUE_NOT_FOUND)
RTUuidClear(&pThis->Uuid);
else if (RT_SUCCESS(rc))
{
rc = RTUuidFromStr(&pThis->Uuid, psz);
if (RT_FAILURE(rc))
{
AssertMsgFailed(("Configuration error: Uuid from string failed on \"%s\", rc=%Rrc.\n", psz, rc));
PDMDrvHlpMMHeapFree(pDrvIns, psz);
return rc;
}
PDMDrvHlpMMHeapFree(pDrvIns, psz);
}
else
{
AssertMsgFailed(("Configuration error: Failed to obtain the uuid, rc=%Rrc.\n", rc));
return rc;
}
/* Define whether attach failure is an error (default) or not. */
bool fAttachFailError = true;
rc = pHlp->pfnCFGMQueryBoolDef(pCfg, "AttachFailError", &fAttachFailError, true);
pThis->fAttachFailError = fAttachFailError;
/* log config summary */
Log(("%s-%d: pszDevice='%s' (%s) cMilliesPoller=%d fReadOnlyConfig=%d fLocked=%d fBIOSVisible=%d Uuid=%RTuuid\n",
pDrvIns->pReg->szName, pDrvIns->iInstance, pThis->pszDevice, pThis->pszDeviceOpen, pThis->cMilliesPoller,
pThis->fReadOnlyConfig, pThis->fLocked, pThis->fBiosVisible, &pThis->Uuid));
/*
* Check that there are no drivers below us.
*/
AssertMsgReturn(PDMDrvHlpNoAttach(pDrvIns) == VERR_PDM_NO_ATTACHED_DRIVER,
("Configuration error: Not possible to attach anything to this driver!\n"),
VERR_PDM_DRVINS_NO_ATTACH);
/*
* Register saved state.
*/
rc = PDMDrvHlpSSMRegisterLoadDone(pDrvIns, drvHostBaseLoadDone);
if (RT_FAILURE(rc))
return rc;
/*
* Initialize the critical section used for serializing the access to the media.
*/
rc = RTCritSectInit(&pThis->CritSect);
if (RT_FAILURE(rc))
return rc;
/*
* Open the device.
*/
rc = drvHostBaseOpenOs(pThis, pThis->fReadOnlyConfig);
if (RT_FAILURE(rc))
{
char *pszDevice = pThis->pszDevice;
#ifndef RT_OS_DARWIN
char szPathReal[256];
if ( RTPathExists(pszDevice)
&& RT_SUCCESS(RTPathReal(pszDevice, szPathReal, sizeof(szPathReal))))
pszDevice = szPathReal;
#endif
/*
* Disable CD/DVD passthrough in case it was enabled. Would cause
* weird failures later when the guest issues commands. These would
* all fail because of the invalid file handle. So use the normal
* virtual CD/DVD code, which deals more gracefully with unavailable
* "media" - actually a complete drive in this case.
*/
pThis->IMedia.pfnSendCmd = NULL;
AssertMsgFailed(("Could not open host device %s, rc=%Rrc\n", pszDevice, rc));
switch (rc)
{
case VERR_ACCESS_DENIED:
return PDMDrvHlpVMSetError(pDrvIns, rc, RT_SRC_POS,
#ifdef RT_OS_LINUX
N_("Cannot open host device '%s' for %s access. Check the permissions "
"of that device ('/bin/ls -l %s'): Most probably you need to be member "
"of the device group. Make sure that you logout/login after changing "
"the group settings of the current user"),
#else
N_("Cannot open host device '%s' for %s access. Check the permissions "
"of that device"),
#endif
pszDevice, pThis->fReadOnlyConfig ? "readonly" : "read/write",
pszDevice);
default:
{
if (pThis->fAttachFailError)
return rc;
int erc = PDMDrvHlpVMSetRuntimeError(pDrvIns, 0 /*fFlags*/,
"DrvHost_MOUNTFAIL",
N_("Cannot attach to host device '%s'"), pszDevice);
AssertRC(erc);
src = rc;
}
}
}
/*
* Lock the drive if that's required by the configuration.
*/
if (pThis->fLocked)
{
if (pThis->pfnDoLock)
rc = pThis->pfnDoLock(pThis, true);
if (RT_FAILURE(rc))
{
AssertMsgFailed(("Failed to lock the dvd drive. rc=%Rrc\n", rc));
return rc;
}
}
if (RT_SUCCESS(src) && drvHostBaseIsMediaPollingRequiredOs(pThis))
{
/*
* Create the event semaphore which the poller thread will wait on.
*/
rc = RTSemEventCreate(&pThis->EventPoller);
if (RT_FAILURE(rc))
return rc;
/*
* Start the thread which will poll for the media.
*/
rc = RTThreadCreate(&pThis->ThreadPoller, drvHostBaseMediaThread, pThis, 0,
RTTHREADTYPE_INFREQUENT_POLLER, RTTHREADFLAGS_WAITABLE, "DVDMEDIA");
if (RT_FAILURE(rc))
{
AssertMsgFailed(("Failed to create poller thread. rc=%Rrc\n", rc));
return rc;
}
/*
* Wait for the thread to start up (!w32:) and do one detection loop.
*/
rc = RTThreadUserWait(pThis->ThreadPoller, 10000);
AssertRC(rc);
}
if (RT_SUCCESS(rc))
drvHostBaseStatsRegister(pThis);
if (RT_FAILURE(rc))
{
if (!pThis->fAttachFailError)
{
/* Suppressing the attach failure error must not affect the normal
* DRVHostBaseDestruct, so reset this flag below before leaving. */
pThis->fKeepInstance = true;
rc = VINF_SUCCESS;
}
DRVHostBaseDestruct(pDrvIns);
pThis->fKeepInstance = false;
}
if (RT_FAILURE(src))
return src;
return rc;
}
|