Home

Awesome

libusbhsfs

USB Mass Storage Class Host + Filesystem Mounter static library for Nintendo Switch homebrew applications.

Main features

Limitations

Licensing

Dual licensing is provided for this project depending on the way it is built:

How to install

This section assumes you've already installed devkitA64, libnx and devkitPro pacman. If not, please follow the steps from the devkitPro wiki.

Regardless of the build type you choose, libusbhsfs will be installed into the portlibs directory from devkitPro, and it'll be ready to use by any homebrew application.

Installing the filesystem support libraries beforehand isn't needed if you intend to use the ISC licensed build -- it is guaranteed to not use any GPL licensed code and/or dependency at all.

How to use

This section assumes you've already built the library by following the steps from the previous section.

  1. Update the Makefile from your homebrew application to reference the library.
    • Two different builds can be generated: a release build (-lusbhsfs) and a debug build with logging enabled (-lusbhsfsd).
    • If you're using a GPLv2+ licensed build, you'll also need to link your application against both NTFS-3G and lwext4: -lusbhsfs -lntfs-3g -llwext4.
    • In case you need to report any bugs, please make sure you're using the debug build and provide its logfile.
  2. Include the usbhsfs.h header file somewhere in your code.
  3. Initialize the USB Mass Storage Class Host interface with usbHsFsInitialize().
  4. Choose the population system you'll use to retrieve information from the library. For further details about the pros and cons of each system, please refer to the usbhsfs.h header file.
    • Event-driven system:
      • Retrieve a pointer to the user-mode UMS status change event with usbHsFsGetStatusChangeUserEvent() and wait for that event to be signaled (e.g. under a different thread).
      • Get the mounted device count with usbHsFsGetMountedDeviceCount().
      • List mounted devices with usbHsFsListMountedDevices().
    • Callback-based system:
      • Set a pointer to a callback function of your own with usbHsFsSetPopulateCallback(), which will receive a short-lived array of mounted devices and their count.
  5. Perform I/O operations using the returned mount names from the listed devices.
  6. If, for some reason, you need to safely unmount a UMS device at runtime before disconnecting it and without shutting down the whole library interface, use usbHsFsUnmountDevice().
  7. Close the USB Mass Storage Class Host interface with usbHsFsExit() when you're done.

Please check both the header file located at /include/usbhsfs.h and the provided test applications in /example_event (event-driven system) and /example_callback (callback-based system) for additional information.

Relative path support

Disclaimer #1: all fsdevMount*() calls from libnx (and any wrappers around them) can and will override the default devoptab device if used after a successful chdir() call using an absolute path from a mounted volume in a UMS device. If such thing occurs, and you still need to perform additional operations with relative paths, just call chdir() again.

Disclaimer #2: relative path support is not available under SX OS!

A chdir() call using an absolute path to a directory from a mounted volume (e.g. "ums0:/") must be issued to change both the default devoptab device and the current working directory. This will effectively place you at the provided directory, and all I/O operations performed with relative paths shall work on it.

The SD card will be set as the new default devoptab device under two different conditions:

For an example, please check the provided test application in /example.

Credits

Thanks to