2020-04-16 01:06:41 +01:00
/*
2021-03-26 04:35:14 +00:00
* nxdt_utils . h
2020-04-16 01:06:41 +01:00
*
2020-12-23 17:48:57 +00:00
* Copyright ( c ) 2020 - 2021 , DarkMatterCore < pabloacurielz @ gmail . com > .
2020-07-03 10:31:22 +01:00
*
* This file is part of nxdumptool ( https : //github.com/DarkMatterCore/nxdumptool).
*
2021-03-25 19:26:58 +00:00
* nxdumptool 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 , either version 3 of the License , or
* ( at your option ) any later version .
2020-04-16 01:06:41 +01:00
*
2021-03-25 19:26:58 +00:00
* nxdumptool 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 .
2020-04-16 01:06:41 +01:00
*
* You should have received a copy of the GNU General Public License
2021-03-25 19:26:58 +00:00
* along with this program . If not , see < https : //www.gnu.org/licenses/>.
2020-04-16 01:06:41 +01:00
*/
2020-04-15 21:50:07 +01:00
# pragma once
2021-03-26 04:35:14 +00:00
# ifndef __NXDT_UTILS_H__
# define __NXDT_UTILS_H__
2020-04-15 21:50:07 +01:00
2020-10-08 19:31:09 +01:00
# include "common.h"
2021-03-23 14:06:52 +00:00
# ifdef __cplusplus
extern " C " {
# endif
2020-07-22 09:03:28 +01:00
# define APP_BASE_PATH "sdmc: / switch / " APP_TITLE " / "
2020-04-15 21:50:07 +01:00
2020-04-17 22:59:05 +01:00
# define MEMBER_SIZE(type, member) sizeof(((type*)NULL)->member)
2020-04-15 21:50:07 +01:00
2020-04-17 22:59:05 +01:00
# define MAX_ELEMENTS(x) ((sizeof((x))) / (sizeof((x)[0])))
2020-04-15 21:50:07 +01:00
2020-10-08 22:52:31 +01:00
# define BIT_LONG(n) (1UL << (n))
2021-02-13 23:53:30 +00:00
# define ALIGN_UP(x, y) (((x) + ((y) - 1)) & ~((y) - 1))
2021-03-20 07:13:16 +00:00
# define ALIGN_DOWN(x, y) ((x) & ~((y) - 1))
2020-05-11 20:12:03 +01:00
# define IS_ALIGNED(x, y) (((x) & ((y) - 1)) == 0)
2021-02-13 06:09:18 +00:00
2020-10-04 08:05:05 +01:00
# define IS_POWER_OF_TWO(x) (((x) & ((x) - 1)) == 0)
2020-04-15 21:50:07 +01:00
2020-04-17 22:59:05 +01:00
# define BIS_SYSTEM_PARTITION_MOUNT_NAME "sys:"
2020-04-15 21:50:07 +01:00
2020-12-02 06:28:35 +00:00
/// Used to determine which CFW is the application running under.
2020-04-15 21:50:07 +01:00
typedef enum {
2020-04-16 11:13:11 +01:00
UtilsCustomFirmwareType_Unknown = 0 ,
UtilsCustomFirmwareType_Atmosphere = 1 ,
UtilsCustomFirmwareType_SXOS = 2 ,
UtilsCustomFirmwareType_ReiNX = 3
2020-04-15 21:50:07 +01:00
} UtilsCustomFirmwareType ;
2020-12-02 06:28:35 +00:00
/// Resource (de)initialization.
2021-03-07 23:22:49 +00:00
/// Called at program startup and exit.
2020-05-03 00:40:50 +01:00
bool utilsInitializeResources ( void ) ;
void utilsCloseResources ( void ) ;
2020-04-15 21:50:07 +01:00
2020-12-02 06:28:35 +00:00
/// Thread management functions.
2020-08-18 06:04:13 +01:00
bool utilsCreateThread ( Thread * out_thread , ThreadFunc func , void * arg , int cpu_id ) ;
void utilsJoinThread ( Thread * thread ) ;
2020-12-02 06:28:35 +00:00
/// Returns true if the application is running under a development unit.
2020-08-15 22:22:49 +01:00
bool utilsIsDevelopmentUnit ( void ) ;
2020-12-02 06:28:35 +00:00
/// Formats a string and appends it to the provided buffer.
/// If the buffer isn't big enough to hold both its current contents and the new formatted string, it will be resized.
2021-03-29 19:27:35 +01:00
__attribute__ ( ( format ( printf , 3 , 4 ) ) ) bool utilsAppendFormattedStringToBuffer ( char * * dst , size_t * dst_size , const char * fmt , . . . ) ;
2020-10-02 10:53:58 +01:00
2020-12-02 06:28:35 +00:00
/// Replaces illegal FAT characters in the provided string with underscores.
/// If 'ascii_only' is set to true, all characters outside the (0x20,0x7E] range will also be replaced with underscores.
2020-05-05 19:04:23 +01:00
void utilsReplaceIllegalCharacters ( char * str , bool ascii_only ) ;
2020-04-15 21:50:07 +01:00
2020-12-02 06:28:35 +00:00
/// Trims whitespace characters from the provided string.
2020-05-03 00:40:50 +01:00
void utilsTrimString ( char * str ) ;
2020-04-16 11:13:11 +01:00
2021-05-11 23:36:15 +01:00
/// Generates a hex string representation of the binary data stored in 'src' and stores it in 'dst'.
/// If 'uppercase' is true, uppercase characters will be used to generate the hex string. Otherwise, lowercase characters will be used.
void utilsGenerateHexStringFromData ( char * dst , size_t dst_size , const void * src , size_t src_size , bool uppercase ) ;
2020-04-15 21:50:07 +01:00
2021-03-07 23:22:49 +00:00
/// Formats the provided 'size' value to a human-readable size string and stores it in 'dst'.
2020-07-26 05:57:12 +01:00
void utilsGenerateFormattedSizeString ( u64 size , char * dst , size_t dst_size ) ;
2020-12-24 08:49:38 +00:00
/// Saves the total size and free space available from the filesystem pointed to by the input path (e.g. "sdmc:/") to 'out_total' and 'out_free', respectively.
2021-03-07 23:22:49 +00:00
/// Either 'out_total' or 'out_free' can be NULL, but at least one of them must be a valid pointer.
2020-12-02 06:28:35 +00:00
/// Returns false if there's an error.
2020-12-24 08:49:38 +00:00
bool utilsGetFileSystemStatsByPath ( const char * path , u64 * out_total , u64 * out_free ) ;
2020-12-02 06:28:35 +00:00
2021-03-07 23:22:49 +00:00
/// Returns a pointer to the FsFileSystem object for the SD card.
FsFileSystem * utilsGetSdCardFileSystemObject ( void ) ;
2020-12-02 06:28:35 +00:00
/// Commits SD card filesystem changes.
/// Must be used after closing a file handle from the SD card.
2020-08-01 05:43:55 +01:00
bool utilsCommitSdCardFileSystemChanges ( void ) ;
2020-05-03 00:40:50 +01:00
2020-12-02 06:28:35 +00:00
/// Returns true if a file exists.
2020-05-03 00:40:50 +01:00
bool utilsCheckIfFileExists ( const char * path ) ;
2020-12-02 06:28:35 +00:00
/// Deletes a ConcatenationFile located at the input path.
2020-10-22 05:38:14 +01:00
void utilsRemoveConcatenationFile ( const char * path ) ;
2020-12-02 06:28:35 +00:00
/// Creates a ConcatenationFile at the input path.
2020-05-03 00:40:50 +01:00
bool utilsCreateConcatenationFile ( const char * path ) ;
2020-12-02 06:28:35 +00:00
/// Creates a full directory tree using the provided path.
/// If 'create_last_element' is true, the last element from the provided path will be created as well.
2020-07-29 22:02:21 +01:00
void utilsCreateDirectoryTree ( const char * path , bool create_last_element ) ;
2020-12-02 06:28:35 +00:00
/// Returns a pointer to a dynamically allocated string that holds the full path formed by the provided arguments.
2020-10-21 05:27:48 +01:00
char * utilsGeneratePath ( const char * prefix , const char * filename , const char * extension ) ;
2020-12-02 06:28:35 +00:00
/// Returns true if the application is running under Applet Mode.
2020-05-03 00:40:50 +01:00
bool utilsAppletModeCheck ( void ) ;
2020-12-02 06:28:35 +00:00
/// (Un)blocks HOME button presses.
2020-05-03 00:40:50 +01:00
void utilsChangeHomeButtonBlockStatus ( bool block ) ;
2020-12-02 06:28:35 +00:00
/// Returns a UtilsCustomFirmwareType value.
u8 utilsGetCustomFirmwareType ( void ) ;
2020-05-03 00:40:50 +01:00
2020-12-02 06:28:35 +00:00
/// Returns a pointer to the FsStorage object for the eMMC BIS System partition.
2020-04-17 22:59:05 +01:00
FsStorage * utilsGetEmmcBisSystemPartitionStorage ( void ) ;
2020-04-15 21:50:07 +01:00
2020-12-02 06:28:35 +00:00
/// Enables/disables CPU/MEM overclocking.
2020-05-03 00:40:50 +01:00
void utilsOverclockSystem ( bool overclock ) ;
2020-12-02 06:28:35 +00:00
/// Simple wrapper to sleep the current thread for a specific number of full seconds.
2020-05-03 00:40:50 +01:00
NX_INLINE void utilsSleep ( u64 seconds )
2020-04-15 21:50:07 +01:00
{
2020-04-17 22:59:05 +01:00
if ( seconds ) svcSleepThread ( seconds * ( u64 ) 1000000000 ) ;
2020-04-15 21:50:07 +01:00
}
2021-03-23 14:06:52 +00:00
# ifdef __cplusplus
}
# endif
2021-03-26 04:35:14 +00:00
# endif /* __NXDT_UTILS_H__ */