libgui 2.01
libgui is a GUI library for the GameCube, Wii, and Wii U
 
Loading...
Searching...
No Matches
dvm_wut.c
1/****************************************************************************
2 * Platform Abstraction Layer (WUT driver)
3 * Daryl Borth 2026
4 * dvm_wut.c
5 *
6 * libdvm - wut backend integration
7 ***************************************************************************/
8#include "dvm_wut.h"
9#include <dvm.h>
10#include <fat.h>
11#include <ntfs.h>
12#include <stdalign.h>
13#include <string.h>
14
15static DvmWutVolume volumes[DVM_WUT_MAX_VOLUMES];
16static bool driversRegistered = false;
17
18bool dvmWutInit(void)
19{
20 if(driversRegistered)
21 return true;
22
23 // Both register idempotently (dvmRegisterFsDriver() no-ops on a
24 // driver that's already registered).
25 bool vfatOk = dvmRegisterFsDriver(&g_vfatFsDriver);
26 bool exfatOk = dvmRegisterFsDriver(&g_exfatFsDriver);
27 bool ntfsOk = dvmRegisterFsDriver(&g_ntfsFsDriver);
28
29 driversRegistered = vfatOk && exfatOk && ntfsOk;
30 return driversRegistered;
31}
32
33static DvmWutVolume * findVolume(const char * name)
34{
35 for(int i = 0; i < DVM_WUT_MAX_VOLUMES; i++)
36 if(volumes[i].name[0] != '\0' && strcmp(volumes[i].name, name) == 0)
37 return &volumes[i];
38 return NULL;
39}
40
41static DvmWutVolume * claimVolumeSlot(const char * name)
42{
43 DvmWutVolume * vol = findVolume(name);
44 if(vol)
45 return vol;
46
47 for(int i = 0; i < DVM_WUT_MAX_VOLUMES; i++)
48 {
49 if(volumes[i].name[0] == '\0')
50 {
51 strncpy(volumes[i].name, name, sizeof(volumes[i].name) - 1);
52 volumes[i].name[sizeof(volumes[i].name) - 1] = '\0';
53 return &volumes[i];
54 }
55 }
56 return NULL;
57}
58
59bool dvmWutMountVolume(const char * name, DISC_INTERFACE * iface, unsigned cachePages, unsigned sectorsPerPage)
60{
61 if(!driversRegistered || !name || !iface)
62 return false;
63
64 DvmWutVolume * vol = claimVolumeSlot(name);
65 if(!vol || vol->isMounted)
66 return false;
67
68 DvmDisc * disc = dvmDiscCreate(iface);
69 if(!disc)
70 return false;
71
72 if(cachePages)
73 {
74 DvmDisc * cachedDisc = dvmDiscCacheCreate(disc, cachePages, sectorsPerPage);
75 if(!cachedDisc)
76 {
77 // Wrapping failed (eg. allocation failure) - fall back to the
78 // raw, uncached disc rather than leaking it and handing a NULL
79 // disc pointer down to dvmProbeMountDisc().
80 cachedDisc = disc;
81 }
82 disc = cachedDisc;
83 }
84
85 // dvmProbeMountDisc() mounts every partition it recognizes, naming the
86 // first "name" and any further ones "name2", "name3"... (see
87 // dvm_prober.c).
88 unsigned mounted = dvmProbeMountDisc(name, disc);
89 if(!mounted)
90 {
91 disc->vt->destroy(disc);
92 return false;
93 }
94
95 vol->disc = disc;
96 vol->isMounted = true;
97 return true;
98}
99
100void dvmWutUnmountVolume(const char * name)
101{
102 DvmWutVolume * vol = findVolume(name);
103 if(!vol || !vol->isMounted)
104 return;
105
106 // Drops the fat driver's reference on vol->disc; once that's the last
107 // reference, this destroys the disc (flushing the cache first) and
108 // shuts down the underlying DISC_INTERFACE - see dvmDiscAddUser()/
109 // dvmDiscRemoveUser() in fat_driver.c and dvm_disc.c.
110 dvmUnmountVolume(name);
111
112 vol->disc = NULL;
113 vol->isMounted = false;
114}
115
116bool dvmWutVolumeStillPresent(const char * name)
117{
118 DvmWutVolume * vol = findVolume(name);
119 if(!vol || !vol->isMounted || !vol->disc)
120 return false;
121
122 alignas(LIBDVM_BUFFER_ALIGN) uint8_t scratch[512];
123 return dvmDiscProbePresence((DvmDisc *)vol->disc, scratch);
124}
125
126bool dvmWutGetVolumeLabel(const char * name, char * labelOut, size_t labelOutSize)
127{
128 if(!name || !labelOut || labelOutSize < 12)
129 return false;
130
131 labelOut[0] = '\0';
132
133 // vfat/exfat
134 if(fatGetVolumeLabel(name, labelOut) && labelOut[0] != '\0')
135 return true;
136
137 // ntfs
138 labelOut[0] = '\0';
139 return ntfsGetVolumeLabel(name, labelOut, labelOutSize) && labelOut[0] != '\0';
140}
One storage slot's dvm mount state.
Definition dvm_wut.h:25
void * disc
opaque DvmDisc* - valid only while isMounted, NULL otherwise
Definition dvm_wut.h:28
bool isMounted
true while the volume is mounted through libdvm
Definition dvm_wut.h:27