├── meta ├── icon.png ├── icon2.png └── meta.xml ├── src ├── common │ ├── types.h │ ├── os_defs.h │ ├── common.h │ └── fs_defs.h ├── main.h ├── entry.c ├── utils │ ├── logger.h │ ├── logger.c │ └── utils.h ├── fs │ ├── fs_utils.h │ ├── sd_fat_devoptab.h │ ├── fs_utils.c │ └── sd_fat_devoptab.c ├── link.ld ├── ftp.h ├── system │ ├── memory.h │ └── memory.c ├── dynamic_libs │ ├── sys_functions.h │ ├── vpad_functions.c │ ├── sys_functions.c │ ├── padscore_functions.c │ ├── ax_functions.h │ ├── socket_functions.h │ ├── ax_functions.c │ ├── socket_functions.c │ ├── vpad_functions.h │ ├── padscore_functions.h │ ├── fs_functions.h │ ├── fs_functions.c │ ├── os_functions.h │ ├── gx2_functions.c │ ├── gx2_functions.h │ └── os_functions.c ├── net.h ├── vrt.h ├── virtualpath.h ├── virtualpath.c ├── main.c ├── net.c ├── vrt.c └── ftp.c ├── .gitignore ├── LICENSE.txt └── Makefile /meta/icon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/FIX94/ftpiiu/HEAD/meta/icon.png -------------------------------------------------------------------------------- /meta/icon2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/FIX94/ftpiiu/HEAD/meta/icon2.png -------------------------------------------------------------------------------- /src/common/types.h: -------------------------------------------------------------------------------- 1 | #ifndef TYPES_H 2 | #define TYPES_H 3 | 4 | #include 5 | 6 | #endif /* TYPES_H */ 7 | 8 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /fs/build 2 | /installer/bin 3 | /loader/build 4 | /menu/build 5 | /server/logs/*.txt 6 | /build 7 | /*.elf 8 | /fs/*.elf 9 | /loader/*.elf 10 | /sd_loader/build 11 | /sd_loader/*.elf 12 | /udp_debug_reader/obj 13 | /udp_debug_reader/GeckoLog.txt -------------------------------------------------------------------------------- /src/main.h: -------------------------------------------------------------------------------- 1 | #ifndef _MAIN_H_ 2 | #define _MAIN_H_ 3 | 4 | #include "common/types.h" 5 | #include "dynamic_libs/os_functions.h" 6 | 7 | /* Main */ 8 | #ifdef __cplusplus 9 | extern "C" { 10 | #endif 11 | 12 | //! C wrapper for our C++ functions 13 | int Menu_Main(void); 14 | 15 | #ifdef __cplusplus 16 | } 17 | #endif 18 | 19 | #endif 20 | -------------------------------------------------------------------------------- /meta/meta.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | FTPiiU 4 | joedj 5 | 0.4 6 | 20161101200000 7 | An FTP server 8 | ftpiiU is an FTP server for WiiU. 9 | 10 | Ported to WiiU by Dimok. 11 | Based on FTPii from joedj 12 | 13 | 14 | 15 | -------------------------------------------------------------------------------- /src/entry.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "dynamic_libs/os_functions.h" 3 | #include "dynamic_libs/sys_functions.h" 4 | #include "common/common.h" 5 | #include "utils/utils.h" 6 | #include "main.h" 7 | 8 | int __entry_menu(int argc, char **argv) 9 | { 10 | //! ******************************************************************* 11 | //! * Jump to our application * 12 | //! ******************************************************************* 13 | return Menu_Main(); 14 | } 15 | -------------------------------------------------------------------------------- /src/utils/logger.h: -------------------------------------------------------------------------------- 1 | #ifndef __LOGGER_H_ 2 | #define __LOGGER_H_ 3 | 4 | #ifdef __cplusplus 5 | extern "C" { 6 | #endif 7 | 8 | //#define DEBUG_LOGGER 1 9 | 10 | #ifdef DEBUG_LOGGER 11 | void log_init(const char * ip); 12 | void log_deinit(void); 13 | void log_print(const char *str); 14 | void log_printf(const char *format, ...); 15 | #else 16 | #define log_init(x) 17 | #define log_deinit() 18 | #define log_print(x) 19 | #define log_printf(x, ...) 20 | #endif 21 | 22 | #ifdef __cplusplus 23 | } 24 | #endif 25 | 26 | #endif 27 | -------------------------------------------------------------------------------- /src/fs/fs_utils.h: -------------------------------------------------------------------------------- 1 | #ifndef __FS_UTILS_H_ 2 | #define __FS_UTILS_H_ 3 | 4 | #ifdef __cplusplus 5 | extern "C" { 6 | #endif 7 | 8 | #include 9 | 10 | int MountFS(void *pClient, void *pCmd, char **mount_path); 11 | int UmountFS(void *pClient, void *pCmd, const char *mountPath); 12 | 13 | int LoadFileToMem(const char *filepath, u8 **inbuffer, u32 *size); 14 | 15 | //! todo: C++ class 16 | int CreateSubfolder(const char * fullpath); 17 | int CheckFile(const char * filepath); 18 | 19 | #ifdef __cplusplus 20 | } 21 | #endif 22 | 23 | #endif // __FS_UTILS_H_ 24 | -------------------------------------------------------------------------------- /src/common/os_defs.h: -------------------------------------------------------------------------------- 1 | #ifndef __OS_DEFS_H_ 2 | #define __OS_DEFS_H_ 3 | 4 | #ifdef __cplusplus 5 | extern "C" { 6 | #endif 7 | 8 | typedef struct _OsSpecifics 9 | { 10 | unsigned int addr_OSDynLoad_Acquire; 11 | unsigned int addr_OSDynLoad_FindExport; 12 | unsigned int addr_OSTitle_main_entry; 13 | 14 | unsigned int addr_KernSyscallTbl1; 15 | unsigned int addr_KernSyscallTbl2; 16 | unsigned int addr_KernSyscallTbl3; 17 | unsigned int addr_KernSyscallTbl4; 18 | unsigned int addr_KernSyscallTbl5; 19 | } OsSpecifics; 20 | 21 | #ifdef __cplusplus 22 | } 23 | #endif 24 | 25 | #endif // __OS_DEFS_H_ 26 | -------------------------------------------------------------------------------- /src/link.ld: -------------------------------------------------------------------------------- 1 | OUTPUT(ftpiiu.elf); 2 | 3 | /* Tell linker where our application entry is so the garbage collect can work correct */ 4 | ENTRY(__entry_menu); 5 | 6 | SECTIONS { 7 | . = 0x00802000; 8 | .text : { 9 | *(.text*); 10 | } 11 | .rodata : { 12 | *(.rodata*); 13 | } 14 | .data : { 15 | *(.data*); 16 | 17 | __sdata_start = .; 18 | *(.sdata*); 19 | __sdata_end = .; 20 | 21 | __sdata2_start = .; 22 | *(.sdata2*); 23 | __sdata2_end = .; 24 | } 25 | .bss : { 26 | __bss_start = .; 27 | *(.bss*); 28 | *(.sbss*); 29 | *(COMMON); 30 | __bss_end = .; 31 | } 32 | __CODE_END = .; 33 | 34 | /DISCARD/ : { 35 | *(*); 36 | } 37 | } 38 | 39 | /******************************************************** FS ********************************************************/ 40 | /* coreinit.rpl difference in addresses 0xFE3C00 */ 41 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | Copyright (C) 2008 Joseph Jordan 2 | 3 | This software is provided 'as-is', without any express or implied warranty. 4 | In no event will the authors be held liable for any damages arising from 5 | the use of this software. 6 | 7 | Permission is granted to anyone to use this software for any purpose, 8 | including commercial applications, and to alter it and redistribute it 9 | freely, subject to the following restrictions: 10 | 11 | 1.The origin of this software must not be misrepresented; you must not 12 | claim that you wrote the original software. If you use this software in a 13 | product, an acknowledgment in the product documentation would be 14 | appreciated but is not required. 15 | 16 | 2.Altered source versions must be plainly marked as such, and must not be 17 | misrepresented as being the original software. 18 | 19 | 3.This notice may not be removed or altered from any source distribution. 20 | -------------------------------------------------------------------------------- /src/common/common.h: -------------------------------------------------------------------------------- 1 | #ifndef COMMON_H 2 | #define COMMON_H 3 | 4 | #ifdef __cplusplus 5 | extern "C" { 6 | #endif 7 | 8 | #include "os_defs.h" 9 | 10 | #define CAFE_OS_SD_PATH "/vol/external01" 11 | #define SD_PATH "sd:" 12 | #define WIIU_PATH "/wiiu" 13 | 14 | #ifndef MEM_BASE 15 | #define MEM_BASE (0x00800000) 16 | #endif 17 | 18 | #define ELF_DATA_ADDR (*(volatile unsigned int*)(MEM_BASE + 0x1300 + 0x00)) 19 | #define ELF_DATA_SIZE (*(volatile unsigned int*)(MEM_BASE + 0x1300 + 0x04)) 20 | #define MAIN_ENTRY_ADDR (*(volatile unsigned int*)(MEM_BASE + 0x1400 + 0x00)) 21 | #define OS_FIRMWARE (*(volatile unsigned int*)(MEM_BASE + 0x1400 + 0x04)) 22 | 23 | #define OS_SPECIFICS ((OsSpecifics*)(MEM_BASE + 0x1500)) 24 | 25 | #ifndef EXIT_SUCCESS 26 | #define EXIT_SUCCESS 0 27 | #endif 28 | #define EXIT_HBL_EXIT 0xFFFFFFFE 29 | #define EXIT_RELAUNCH_ON_LOAD 0xFFFFFFFD 30 | 31 | #ifdef __cplusplus 32 | } 33 | #endif 34 | 35 | #endif /* COMMON_H */ 36 | 37 | -------------------------------------------------------------------------------- /src/ftp.h: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | ftpii -- an FTP server for the Wii 4 | 5 | Copyright (C) 2008 Joseph Jordan 6 | 7 | This software is provided 'as-is', without any express or implied warranty. 8 | In no event will the authors be held liable for any damages arising from 9 | the use of this software. 10 | 11 | Permission is granted to anyone to use this software for any purpose, 12 | including commercial applications, and to alter it and redistribute it 13 | freely, subject to the following restrictions: 14 | 15 | 1.The origin of this software must not be misrepresented; you must not 16 | claim that you wrote the original software. If you use this software in a 17 | product, an acknowledgment in the product documentation would be 18 | appreciated but is not required. 19 | 20 | 2.Altered source versions must be plainly marked as such, and must not be 21 | misrepresented as being the original software. 22 | 23 | 3.This notice may not be removed or altered from any source distribution. 24 | 25 | */ 26 | #ifndef _FTP_H_ 27 | #define _FTP_H_ 28 | 29 | #ifdef __cplusplus 30 | extern "C"{ 31 | #endif 32 | 33 | void accept_ftp_client(s32 server); 34 | void set_ftp_password(char *new_password); 35 | bool process_ftp_events(s32 server); 36 | void cleanup_ftp(); 37 | 38 | #ifdef __cplusplus 39 | } 40 | #endif 41 | 42 | #endif /* _FTP_H_ */ 43 | -------------------------------------------------------------------------------- /src/fs/sd_fat_devoptab.h: -------------------------------------------------------------------------------- 1 | /*************************************************************************** 2 | * Copyright (C) 2015 3 | * by Dimok 4 | * 5 | * This software is provided 'as-is', without any express or implied 6 | * warranty. In no event will the authors be held liable for any 7 | * damages arising from the use of this software. 8 | * 9 | * Permission is granted to anyone to use this software for any 10 | * purpose, including commercial applications, and to alter it and 11 | * redistribute it freely, subject to the following restrictions: 12 | * 13 | * 1. The origin of this software must not be misrepresented; you 14 | * must not claim that you wrote the original software. If you use 15 | * this software in a product, an acknowledgment in the product 16 | * documentation would be appreciated but is not required. 17 | * 18 | * 2. Altered source versions must be plainly marked as such, and 19 | * must not be misrepresented as being the original software. 20 | * 21 | * 3. This notice may not be removed or altered from any source 22 | * distribution. 23 | ***************************************************************************/ 24 | #ifndef __SD_FAT_DEVOPTAB_H_ 25 | #define __SD_FAT_DEVOPTAB_H_ 26 | 27 | #ifdef __cplusplus 28 | extern "C" { 29 | #endif 30 | 31 | int mount_sd_fat(const char *path); 32 | int unmount_sd_fat(const char *path); 33 | 34 | #ifdef __cplusplus 35 | } 36 | #endif 37 | 38 | #endif // __SD_FAT_DEVOPTAB_H_ 39 | -------------------------------------------------------------------------------- /src/system/memory.h: -------------------------------------------------------------------------------- 1 | /**************************************************************************** 2 | * Copyright (C) 2015 Dimok 3 | * 4 | * This program is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU General Public License as published by 6 | * the Free Software Foundation, either version 3 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with this program. If not, see . 16 | ****************************************************************************/ 17 | #ifndef __MEMORY_H_ 18 | #define __MEMORY_H_ 19 | 20 | #ifdef __cplusplus 21 | extern "C" { 22 | #endif 23 | 24 | #include 25 | 26 | void memoryInitialize(void); 27 | void memoryRelease(void); 28 | 29 | void * MEM2_alloc(unsigned int size, unsigned int align); 30 | void MEM2_free(void *ptr); 31 | 32 | void * MEM1_alloc(unsigned int size, unsigned int align); 33 | void MEM1_free(void *ptr); 34 | 35 | void * MEMBucket_alloc(unsigned int size, unsigned int align); 36 | void MEMBucket_free(void *ptr); 37 | 38 | #ifdef __cplusplus 39 | } 40 | #endif 41 | 42 | #endif // __MEMORY_H_ 43 | -------------------------------------------------------------------------------- /src/dynamic_libs/sys_functions.h: -------------------------------------------------------------------------------- 1 | /**************************************************************************** 2 | * Copyright (C) 2015 3 | * by Dimok 4 | * 5 | * This software is provided 'as-is', without any express or implied 6 | * warranty. In no event will the authors be held liable for any 7 | * damages arising from the use of this software. 8 | * 9 | * Permission is granted to anyone to use this software for any 10 | * purpose, including commercial applications, and to alter it and 11 | * redistribute it freely, subject to the following restrictions: 12 | * 13 | * 1. The origin of this software must not be misrepresented; you 14 | * must not claim that you wrote the original software. If you use 15 | * this software in a product, an acknowledgment in the product 16 | * documentation would be appreciated but is not required. 17 | * 18 | * 2. Altered source versions must be plainly marked as such, and 19 | * must not be misrepresented as being the original software. 20 | * 21 | * 3. This notice may not be removed or altered from any source 22 | * distribution. 23 | ***************************************************************************/ 24 | #ifndef __SYS_FUNCTIONS_H_ 25 | #define __SYS_FUNCTIONS_H_ 26 | 27 | #ifdef __cplusplus 28 | extern "C" { 29 | #endif 30 | 31 | void InitSysFunctionPointers(void); 32 | 33 | extern void(*_SYSLaunchTitleByPathFromLauncher)(const char* path, int len, int zero); 34 | extern int (* SYSRelaunchTitle)(int argc, char* argv); 35 | extern int (* SYSLaunchMenu)(void); 36 | 37 | 38 | #ifdef __cplusplus 39 | } 40 | #endif 41 | 42 | #endif // __SYS_FUNCTIONS_H_ 43 | -------------------------------------------------------------------------------- /src/dynamic_libs/vpad_functions.c: -------------------------------------------------------------------------------- 1 | /**************************************************************************** 2 | * Copyright (C) 2015 3 | * by Dimok 4 | * 5 | * This software is provided 'as-is', without any express or implied 6 | * warranty. In no event will the authors be held liable for any 7 | * damages arising from the use of this software. 8 | * 9 | * Permission is granted to anyone to use this software for any 10 | * purpose, including commercial applications, and to alter it and 11 | * redistribute it freely, subject to the following restrictions: 12 | * 13 | * 1. The origin of this software must not be misrepresented; you 14 | * must not claim that you wrote the original software. If you use 15 | * this software in a product, an acknowledgment in the product 16 | * documentation would be appreciated but is not required. 17 | * 18 | * 2. Altered source versions must be plainly marked as such, and 19 | * must not be misrepresented as being the original software. 20 | * 21 | * 3. This notice may not be removed or altered from any source 22 | * distribution. 23 | ***************************************************************************/ 24 | #include "os_functions.h" 25 | #include "vpad_functions.h" 26 | 27 | EXPORT_DECL(void, VPADInit, void); 28 | EXPORT_DECL(void, VPADRead, int chan, VPADData *buffer, u32 buffer_size, s32 *error); 29 | 30 | void InitVPadFunctionPointers(void) 31 | { 32 | unsigned int *funcPointer = 0; 33 | unsigned int vpad_handle; 34 | OSDynLoad_Acquire("vpad.rpl", &vpad_handle); 35 | 36 | OS_FIND_EXPORT(vpad_handle, VPADInit); 37 | OS_FIND_EXPORT(vpad_handle, VPADRead); 38 | } 39 | 40 | -------------------------------------------------------------------------------- /src/common/fs_defs.h: -------------------------------------------------------------------------------- 1 | #ifndef FS_DEFS_H 2 | #define FS_DEFS_H 3 | 4 | #include "types.h" 5 | 6 | #ifdef __cplusplus 7 | extern "C" { 8 | #endif 9 | 10 | 11 | /* FS defines and types */ 12 | #define FS_MAX_LOCALPATH_SIZE 511 13 | #define FS_MAX_MOUNTPATH_SIZE 128 14 | #define FS_MAX_FULLPATH_SIZE (FS_MAX_LOCALPATH_SIZE + FS_MAX_MOUNTPATH_SIZE) 15 | #define FS_MAX_ARGPATH_SIZE FS_MAX_FULLPATH_SIZE 16 | 17 | #define FS_STATUS_OK 0 18 | #define FS_RET_UNSUPPORTED_CMD 0x0400 19 | #define FS_RET_NO_ERROR 0x0000 20 | #define FS_RET_ALL_ERROR (unsigned int)(-1) 21 | 22 | #define FS_STAT_FLAG_IS_DIRECTORY 0x80000000 23 | 24 | /* max length of file/dir name */ 25 | #define FS_MAX_ENTNAME_SIZE 256 26 | 27 | #define FS_SOURCETYPE_EXTERNAL 0 28 | #define FS_SOURCETYPE_HFIO 1 29 | #define FS_SOURCETYPE_HFIO 1 30 | 31 | #define FS_MOUNT_SOURCE_SIZE 0x300 32 | #define FS_CLIENT_SIZE 0x1700 33 | #define FS_CMD_BLOCK_SIZE 0xA80 34 | 35 | typedef struct 36 | { 37 | uint32_t flag; 38 | uint32_t permission; 39 | uint32_t owner_id; 40 | uint32_t group_id; 41 | uint32_t size; 42 | uint32_t alloc_size; 43 | uint64_t quota_size; 44 | uint32_t ent_id; 45 | uint64_t ctime; 46 | uint64_t mtime; 47 | uint8_t attributes[48]; 48 | } __attribute__((packed)) FSStat; 49 | 50 | typedef struct 51 | { 52 | FSStat stat; 53 | char name[FS_MAX_ENTNAME_SIZE]; 54 | } FSDirEntry; 55 | 56 | 57 | #ifdef __cplusplus 58 | } 59 | #endif 60 | 61 | #endif /* FS_DEFS_H */ 62 | 63 | -------------------------------------------------------------------------------- /src/dynamic_libs/sys_functions.c: -------------------------------------------------------------------------------- 1 | /**************************************************************************** 2 | * Copyright (C) 2015 3 | * by Dimok 4 | * 5 | * This software is provided 'as-is', without any express or implied 6 | * warranty. In no event will the authors be held liable for any 7 | * damages arising from the use of this software. 8 | * 9 | * Permission is granted to anyone to use this software for any 10 | * purpose, including commercial applications, and to alter it and 11 | * redistribute it freely, subject to the following restrictions: 12 | * 13 | * 1. The origin of this software must not be misrepresented; you 14 | * must not claim that you wrote the original software. If you use 15 | * this software in a product, an acknowledgment in the product 16 | * documentation would be appreciated but is not required. 17 | * 18 | * 2. Altered source versions must be plainly marked as such, and 19 | * must not be misrepresented as being the original software. 20 | * 21 | * 3. This notice may not be removed or altered from any source 22 | * distribution. 23 | ***************************************************************************/ 24 | #include "os_functions.h" 25 | 26 | EXPORT_DECL(void, _SYSLaunchTitleByPathFromLauncher, const char* path, int len, int zero); 27 | EXPORT_DECL(int, SYSRelaunchTitle, int argc, char* argv); 28 | EXPORT_DECL(int, SYSLaunchMenu, void); 29 | 30 | void InitSysFunctionPointers(void) 31 | { 32 | unsigned int *funcPointer = 0; 33 | unsigned int sysapp_handle; 34 | OSDynLoad_Acquire("sysapp.rpl", &sysapp_handle); 35 | 36 | OS_FIND_EXPORT(sysapp_handle, _SYSLaunchTitleByPathFromLauncher); 37 | OS_FIND_EXPORT(sysapp_handle, SYSRelaunchTitle); 38 | OS_FIND_EXPORT(sysapp_handle, SYSLaunchMenu); 39 | } 40 | 41 | -------------------------------------------------------------------------------- /src/net.h: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Copyright (C) 2008 Joseph Jordan 4 | 5 | This software is provided 'as-is', without any express or implied warranty. 6 | In no event will the authors be held liable for any damages arising from 7 | the use of this software. 8 | 9 | Permission is granted to anyone to use this software for any purpose, 10 | including commercial applications, and to alter it and redistribute it 11 | freely, subject to the following restrictions: 12 | 13 | 1.The origin of this software must not be misrepresented; you must not 14 | claim that you wrote the original software. If you use this software in a 15 | product, an acknowledgment in the product documentation would be 16 | appreciated but is not required. 17 | 18 | 2.Altered source versions must be plainly marked as such, and must not be 19 | misrepresented as being the original software. 20 | 21 | 3.This notice may not be removed or altered from any source distribution. 22 | 23 | */ 24 | #ifndef _NET_H_ 25 | #define _NET_H_ 26 | 27 | #ifdef __cplusplus 28 | extern "C"{ 29 | #endif 30 | 31 | #include 32 | 33 | #if 0 34 | void initialise_network(); 35 | #endif 36 | 37 | s32 network_socket(u32 domain,u32 type,u32 protocol); 38 | s32 network_bind(s32 s,struct sockaddr *name,s32 namelen); 39 | s32 network_listen(s32 s,u32 backlog); 40 | s32 network_accept(s32 s,struct sockaddr *addr,s32 *addrlen); 41 | s32 network_connect(s32 s,struct sockaddr *,s32); 42 | s32 network_read(s32 s,void *mem,s32 len); 43 | s32 network_close(s32 s); 44 | u32 network_gethostip(); 45 | 46 | s32 set_blocking(s32 s, bool blocking); 47 | 48 | s32 network_close_blocking(s32 s); 49 | 50 | s32 create_server(u16 port); 51 | 52 | s32 send_exact(s32 s, char *buf, s32 length); 53 | 54 | s32 send_from_file(s32 s, FILE *f); 55 | 56 | s32 recv_to_file(s32 s, FILE *f); 57 | 58 | #ifdef __cplusplus 59 | } 60 | #endif 61 | 62 | #endif /* _NET_H_ */ 63 | -------------------------------------------------------------------------------- /src/vrt.h: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Copyright (C) 2008 Joseph Jordan 4 | This work is derived from Daniel Ehlers' srg_vrt branch. 5 | 6 | This software is provided 'as-is', without any express or implied warranty. 7 | In no event will the authors be held liable for any damages arising from 8 | the use of this software. 9 | 10 | Permission is granted to anyone to use this software for any purpose, 11 | including commercial applications, and to alter it and redistribute it 12 | freely, subject to the following restrictions: 13 | 14 | 1.The origin of this software must not be misrepresented; you must not 15 | claim that you wrote the original software. If you use this software in a 16 | product, an acknowledgment in the product documentation would be 17 | appreciated but is not required. 18 | 19 | 2.Altered source versions must be plainly marked as such, and must not be 20 | misrepresented as being the original software. 21 | 22 | 3.This notice may not be removed or altered from any source distribution. 23 | 24 | */ 25 | #ifndef _VRT_H_ 26 | #define _VRT_H_ 27 | 28 | #ifdef __cplusplus 29 | extern "C"{ 30 | #endif 31 | 32 | #include 33 | #include 34 | 35 | typedef struct 36 | { 37 | DIR *dir; 38 | char *path; 39 | u8 virt_root; 40 | } DIR_P; 41 | 42 | char *to_real_path(char *virtual_cwd, char *virtual_path); 43 | 44 | FILE *vrt_fopen(char *cwd, char *path, char *mode); 45 | int vrt_stat(char *cwd, char *path, struct stat *st); 46 | int vrt_chdir(char *cwd, char *path); 47 | int vrt_unlink(char *cwd, char *path); 48 | int vrt_mkdir(char *cwd, char *path, mode_t mode); 49 | int vrt_rename(char *cwd, char *from_path, char *to_path); 50 | DIR_P *vrt_opendir(char *cwd, char *path); 51 | struct dirent *vrt_readdir(DIR_P *iter); 52 | int vrt_closedir(DIR_P *iter); 53 | 54 | #ifdef __cplusplus 55 | } 56 | #endif 57 | 58 | #endif /* _VRT_H_ */ 59 | -------------------------------------------------------------------------------- /src/virtualpath.h: -------------------------------------------------------------------------------- 1 | /**************************************************************************** 2 | * Copyright (C) 2010 3 | * by Dimok 4 | * 5 | * Original VIRTUAL_PART Struct 6 | * Copyright (C) 2008 7 | * Joseph Jordan 8 | * 9 | * This software is provided 'as-is', without any express or implied 10 | * warranty. In no event will the authors be held liable for any 11 | * damages arising from the use of this software. 12 | * 13 | * Permission is granted to anyone to use this software for any 14 | * purpose, including commercial applications, and to alter it and 15 | * redistribute it freely, subject to the following restrictions: 16 | * 17 | * 1. The origin of this software must not be misrepresented; you 18 | * must not claim that you wrote the original software. If you use 19 | * this software in a product, an acknowledgment in the product 20 | * documentation would be appreciated but is not required. 21 | * 22 | * 2. Altered source versions must be plainly marked as such, and 23 | * must not be misrepresented as being the original software. 24 | * 25 | * 3. This notice may not be removed or altered from any source 26 | * distribution. 27 | * 28 | * for WiiXplorer 2010 29 | ***************************************************************************/ 30 | #ifndef _VIRTUALPATH_H_ 31 | #define _VIRTUALPATH_H_ 32 | 33 | #ifdef __cplusplus 34 | extern "C"{ 35 | #endif 36 | 37 | #include 38 | 39 | typedef struct { 40 | char *name; 41 | char *alias; 42 | char *prefix; 43 | bool inserted; 44 | } VIRTUAL_PARTITION; 45 | 46 | extern VIRTUAL_PARTITION * VIRTUAL_PARTITIONS; 47 | extern u8 MAX_VIRTUAL_PARTITIONS; 48 | 49 | void VirtualMountDevice(const char * devicepath); 50 | void AddVirtualPath(const char *name, const char *alias, const char *prefix); 51 | void MountVirtualDevices(); 52 | void UnmountVirtualPaths(); 53 | 54 | #ifdef __cplusplus 55 | } 56 | #endif 57 | 58 | #endif /* _VIRTUALPART_H_ */ 59 | -------------------------------------------------------------------------------- /src/utils/logger.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include "common/common.h" 6 | #include "dynamic_libs/os_functions.h" 7 | #include "dynamic_libs/socket_functions.h" 8 | #include "logger.h" 9 | 10 | #ifdef DEBUG_LOGGER 11 | static int log_socket = -1; 12 | static volatile int log_lock = 0; 13 | 14 | 15 | void log_init(const char * ipString) 16 | { 17 | log_socket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); 18 | if (log_socket < 0) 19 | return; 20 | 21 | struct sockaddr_in connect_addr; 22 | memset(&connect_addr, 0, sizeof(connect_addr)); 23 | connect_addr.sin_family = AF_INET; 24 | connect_addr.sin_port = 4405; 25 | inet_aton(ipString, &connect_addr.sin_addr); 26 | 27 | if(connect(log_socket, (struct sockaddr*)&connect_addr, sizeof(connect_addr)) < 0) 28 | { 29 | socketclose(log_socket); 30 | log_socket = -1; 31 | } 32 | } 33 | 34 | void log_deinit(void) 35 | { 36 | if(log_socket >= 0) 37 | { 38 | socketclose(log_socket); 39 | log_socket = -1; 40 | } 41 | } 42 | 43 | void log_print(const char *str) 44 | { 45 | // socket is always 0 initially as it is in the BSS 46 | if(log_socket < 0) { 47 | return; 48 | } 49 | 50 | while(log_lock) 51 | usleep(1000); 52 | log_lock = 1; 53 | 54 | int len = strlen(str); 55 | int ret; 56 | while (len > 0) { 57 | int block = len < 1400 ? len : 1400; // take max 1400 bytes per UDP packet 58 | ret = send(log_socket, str, block, 0); 59 | if(ret < 0) 60 | break; 61 | 62 | len -= ret; 63 | str += ret; 64 | } 65 | 66 | log_lock = 0; 67 | } 68 | 69 | void log_printf(const char *format, ...) 70 | { 71 | if(log_socket < 0) { 72 | return; 73 | } 74 | 75 | char * tmp = NULL; 76 | 77 | va_list va; 78 | va_start(va, format); 79 | if((vasprintf(&tmp, format, va) >= 0) && tmp) 80 | { 81 | log_print(tmp); 82 | } 83 | va_end(va); 84 | 85 | if(tmp) 86 | free(tmp); 87 | } 88 | #endif 89 | -------------------------------------------------------------------------------- /src/utils/utils.h: -------------------------------------------------------------------------------- 1 | #ifndef __UTILS_H_ 2 | #define __UTILS_H_ 3 | 4 | #include 5 | #include "../common/types.h" 6 | 7 | #ifdef __cplusplus 8 | extern "C" { 9 | #endif 10 | 11 | #define FlushBlock(addr) asm volatile("dcbf %0, %1\n" \ 12 | "icbi %0, %1\n" \ 13 | "sync\n" \ 14 | "eieio\n" \ 15 | "isync\n" \ 16 | : \ 17 | :"r"(0), "r"(((addr) & ~31)) \ 18 | :"memory", "ctr", "lr", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12" \ 19 | ); 20 | 21 | #define LIMIT(x, min, max) \ 22 | ({ \ 23 | typeof( x ) _x = x; \ 24 | typeof( min ) _min = min; \ 25 | typeof( max ) _max = max; \ 26 | ( ( ( _x ) < ( _min ) ) ? ( _min ) : ( ( _x ) > ( _max ) ) ? ( _max) : ( _x ) ); \ 27 | }) 28 | 29 | #define DegToRad(a) ( (a) * 0.01745329252f ) 30 | #define RadToDeg(a) ( (a) * 57.29577951f ) 31 | 32 | #define ALIGN4(x) (((x) + 3) & ~3) 33 | #define ALIGN32(x) (((x) + 31) & ~31) 34 | 35 | // those work only in powers of 2 36 | #define ROUNDDOWN(val, align) ((val) & ~(align-1)) 37 | #define ROUNDUP(val, align) ROUNDDOWN(((val) + (align-1)), align) 38 | 39 | #define le16(i) ((((u16) ((i) & 0xFF)) << 8) | ((u16) (((i) & 0xFF00) >> 8))) 40 | #define le32(i) ((((u32)le16((i) & 0xFFFF)) << 16) | ((u32)le16(((i) & 0xFFFF0000) >> 16))) 41 | #define le64(i) ((((u64)le32((i) & 0xFFFFFFFFLL)) << 32) | ((u64)le32(((i) & 0xFFFFFFFF00000000LL) >> 32))) 42 | 43 | #ifdef __cplusplus 44 | } 45 | #endif 46 | 47 | #endif // __UTILS_H_ 48 | -------------------------------------------------------------------------------- /src/dynamic_libs/padscore_functions.c: -------------------------------------------------------------------------------- 1 | /**************************************************************************** 2 | * Copyright (C) 2015 3 | * by Dimok 4 | * 5 | * This software is provided 'as-is', without any express or implied 6 | * warranty. In no event will the authors be held liable for any 7 | * damages arising from the use of this software. 8 | * 9 | * Permission is granted to anyone to use this software for any 10 | * purpose, including commercial applications, and to alter it and 11 | * redistribute it freely, subject to the following restrictions: 12 | * 13 | * 1. The origin of this software must not be misrepresented; you 14 | * must not claim that you wrote the original software. If you use 15 | * this software in a product, an acknowledgment in the product 16 | * documentation would be appreciated but is not required. 17 | * 18 | * 2. Altered source versions must be plainly marked as such, and 19 | * must not be misrepresented as being the original software. 20 | * 21 | * 3. This notice may not be removed or altered from any source 22 | * distribution. 23 | ***************************************************************************/ 24 | #include "os_functions.h" 25 | #include "padscore_functions.h" 26 | 27 | EXPORT_DECL(void, KPADInit, void); 28 | EXPORT_DECL(s32, WPADProbe, s32 chan, u32 * pad_type); 29 | EXPORT_DECL(s32, WPADSetDataFormat, s32 chan, s32 format); 30 | EXPORT_DECL(void, WPADEnableURCC, s32 enable); 31 | EXPORT_DECL(void, WPADRead, s32 chan, void * data); 32 | EXPORT_DECL(s32, KPADRead, s32 chan, void * data, u32 size); 33 | 34 | void InitPadScoreFunctionPointers(void) 35 | { 36 | unsigned int *funcPointer = 0; 37 | unsigned int padscore_handle; 38 | OSDynLoad_Acquire("padscore.rpl", &padscore_handle); 39 | 40 | OS_FIND_EXPORT(padscore_handle, KPADInit); 41 | OS_FIND_EXPORT(padscore_handle, WPADProbe); 42 | OS_FIND_EXPORT(padscore_handle, WPADSetDataFormat); 43 | OS_FIND_EXPORT(padscore_handle, WPADEnableURCC); 44 | OS_FIND_EXPORT(padscore_handle, WPADRead); 45 | OS_FIND_EXPORT(padscore_handle, KPADRead); 46 | 47 | KPADInit(); 48 | WPADEnableURCC(1); 49 | } 50 | 51 | -------------------------------------------------------------------------------- /src/dynamic_libs/ax_functions.h: -------------------------------------------------------------------------------- 1 | /**************************************************************************** 2 | * Copyright (C) 2015 3 | * by Dimok 4 | * 5 | * This software is provided 'as-is', without any express or implied 6 | * warranty. In no event will the authors be held liable for any 7 | * damages arising from the use of this software. 8 | * 9 | * Permission is granted to anyone to use this software for any 10 | * purpose, including commercial applications, and to alter it and 11 | * redistribute it freely, subject to the following restrictions: 12 | * 13 | * 1. The origin of this software must not be misrepresented; you 14 | * must not claim that you wrote the original software. If you use 15 | * this software in a product, an acknowledgment in the product 16 | * documentation would be appreciated but is not required. 17 | * 18 | * 2. Altered source versions must be plainly marked as such, and 19 | * must not be misrepresented as being the original software. 20 | * 21 | * 3. This notice may not be removed or altered from any source 22 | * distribution. 23 | ***************************************************************************/ 24 | #ifndef __AX_FUNCTIONS_H_ 25 | #define __AX_FUNCTIONS_H_ 26 | 27 | #ifdef __cplusplus 28 | extern "C" { 29 | #endif 30 | 31 | #include 32 | 33 | void InitAXFunctionPointers(void); 34 | 35 | extern void (* AXInitWithParams)(u32 * params); 36 | extern void (* AXQuit)(void); 37 | extern u32 (* AXGetInputSamplesPerSec)(void); 38 | extern s32 (* AXVoiceBegin)(void *v); 39 | extern s32 (* AXVoiceEnd)(void *v); 40 | extern void (* AXSetVoiceType)(void *v, u16 type); 41 | extern void (* AXSetVoiceOffsets)(void *v, const void *buf); 42 | extern void (* AXSetVoiceSrcType)(void *v, u32 type); 43 | extern void (* AXSetVoiceVe)(void *v, const void *vol); 44 | extern s32 (* AXSetVoiceDeviceMix)(void *v, s32 device, u32 id, void *mix); 45 | extern void (* AXSetVoiceState)(void *v, u16 state); 46 | extern void (* AXSetVoiceSrc)(void *v, const void *src); 47 | extern s32 (* AXSetVoiceSrcRatio)(void *v, f32 ratio); 48 | extern void * (* AXAcquireVoice)(u32 prio, void * callback, u32 arg); 49 | extern void (* AXFreeVoice)(void *v); 50 | extern void (* AXRegisterFrameCallback)(void * callback); 51 | extern u32 (* AXGetVoiceLoopCount)(void * v); 52 | extern void (* AXSetVoiceEndOffset)(void * v, u32 offset); 53 | extern void (* AXSetVoiceLoopOffset)(void * v, u32 offset); 54 | 55 | #ifdef __cplusplus 56 | } 57 | #endif 58 | 59 | #endif // __VPAD_FUNCTIONS_H_ 60 | -------------------------------------------------------------------------------- /src/virtualpath.c: -------------------------------------------------------------------------------- 1 | /**************************************************************************** 2 | * Copyright (C) 2008 3 | * Joseph Jordan 4 | * 5 | * Copyright (C) 2010 6 | * by Dimok 7 | * 8 | * This software is provided 'as-is', without any express or implied 9 | * warranty. In no event will the authors be held liable for any 10 | * damages arising from the use of this software. 11 | * 12 | * Permission is granted to anyone to use this software for any 13 | * purpose, including commercial applications, and to alter it and 14 | * redistribute it freely, subject to the following restrictions: 15 | * 16 | * 1. The origin of this software must not be misrepresented; you 17 | * must not claim that you wrote the original software. If you use 18 | * this software in a product, an acknowledgment in the product 19 | * documentation would be appreciated but is not required. 20 | * 21 | * 2. Altered source versions must be plainly marked as such, and 22 | * must not be misrepresented as being the original software. 23 | * 24 | * 3. This notice may not be removed or altered from any source 25 | * distribution. 26 | * 27 | * for WiiXplorer 2010 28 | ***************************************************************************/ 29 | #include 30 | #include 31 | #include "virtualpath.h" 32 | 33 | u8 MAX_VIRTUAL_PARTITIONS = 0; 34 | VIRTUAL_PARTITION * VIRTUAL_PARTITIONS = NULL; 35 | 36 | void VirtualMountDevice(const char * path) 37 | { 38 | if(!path) 39 | return; 40 | 41 | int i = 0; 42 | char name[255]; 43 | char alias[255]; 44 | char prefix[255]; 45 | bool namestop = false; 46 | 47 | alias[0] = '/'; 48 | 49 | do 50 | { 51 | if(path[i] == ':') 52 | namestop = true; 53 | 54 | if(!namestop) 55 | { 56 | name[i] = path[i]; 57 | name[i+1] = '\0'; 58 | alias[i+1] = path[i]; 59 | alias[i+2] = '\0'; 60 | } 61 | 62 | prefix[i] = path[i]; 63 | prefix[i+1] = '\0'; 64 | i++; 65 | } 66 | while(path[i-1] != '/'); 67 | 68 | AddVirtualPath(name, alias, prefix); 69 | } 70 | 71 | void AddVirtualPath(const char *name, const char *alias, const char *prefix) 72 | { 73 | if(!VIRTUAL_PARTITIONS) 74 | VIRTUAL_PARTITIONS = (VIRTUAL_PARTITION *) malloc(sizeof(VIRTUAL_PARTITION)); 75 | 76 | VIRTUAL_PARTITION * tmp = realloc(VIRTUAL_PARTITIONS, sizeof(VIRTUAL_PARTITION)*(MAX_VIRTUAL_PARTITIONS+1)); 77 | if(!tmp) 78 | { 79 | free(VIRTUAL_PARTITIONS); 80 | MAX_VIRTUAL_PARTITIONS = 0; 81 | return; 82 | } 83 | 84 | VIRTUAL_PARTITIONS = tmp; 85 | 86 | VIRTUAL_PARTITIONS[MAX_VIRTUAL_PARTITIONS].name = strdup(name); 87 | VIRTUAL_PARTITIONS[MAX_VIRTUAL_PARTITIONS].alias = strdup(alias); 88 | VIRTUAL_PARTITIONS[MAX_VIRTUAL_PARTITIONS].prefix = strdup(prefix); 89 | VIRTUAL_PARTITIONS[MAX_VIRTUAL_PARTITIONS].inserted = true; 90 | 91 | MAX_VIRTUAL_PARTITIONS++; 92 | } 93 | 94 | void MountVirtualDevices() 95 | { 96 | VirtualMountDevice("sd:/"); 97 | } 98 | 99 | void UnmountVirtualPaths() 100 | { 101 | u32 i = 0; 102 | for(i = 0; i < MAX_VIRTUAL_PARTITIONS; i++) 103 | { 104 | if(VIRTUAL_PARTITIONS[i].name) 105 | free(VIRTUAL_PARTITIONS[i].name); 106 | if(VIRTUAL_PARTITIONS[i].alias) 107 | free(VIRTUAL_PARTITIONS[i].alias); 108 | if(VIRTUAL_PARTITIONS[i].prefix) 109 | free(VIRTUAL_PARTITIONS[i].prefix); 110 | } 111 | 112 | if(VIRTUAL_PARTITIONS) 113 | free(VIRTUAL_PARTITIONS); 114 | VIRTUAL_PARTITIONS = NULL; 115 | MAX_VIRTUAL_PARTITIONS = 0; 116 | } 117 | -------------------------------------------------------------------------------- /src/dynamic_libs/socket_functions.h: -------------------------------------------------------------------------------- 1 | /**************************************************************************** 2 | * Copyright (C) 2015 3 | * by Dimok 4 | * 5 | * This software is provided 'as-is', without any express or implied 6 | * warranty. In no event will the authors be held liable for any 7 | * damages arising from the use of this software. 8 | * 9 | * Permission is granted to anyone to use this software for any 10 | * purpose, including commercial applications, and to alter it and 11 | * redistribute it freely, subject to the following restrictions: 12 | * 13 | * 1. The origin of this software must not be misrepresented; you 14 | * must not claim that you wrote the original software. If you use 15 | * this software in a product, an acknowledgment in the product 16 | * documentation would be appreciated but is not required. 17 | * 18 | * 2. Altered source versions must be plainly marked as such, and 19 | * must not be misrepresented as being the original software. 20 | * 21 | * 3. This notice may not be removed or altered from any source 22 | * distribution. 23 | ***************************************************************************/ 24 | #ifndef __SOCKET_FUNCTIONS_H_ 25 | #define __SOCKET_FUNCTIONS_H_ 26 | 27 | #ifdef __cplusplus 28 | extern "C" { 29 | #endif 30 | 31 | #include 32 | 33 | #define INADDR_ANY 0 34 | 35 | #define AF_INET 2 36 | 37 | #define SOCK_STREAM 1 38 | #define SOCK_DGRAM 2 39 | 40 | #define IPPROTO_IP 0 41 | #define IPPROTO_TCP 6 42 | #define IPPROTO_UDP 17 43 | 44 | #define TCP_NODELAY 0x2004 45 | 46 | #define SOL_SOCKET -1 47 | #define SO_REUSEADDR 0x0004 48 | #define SO_NONBLOCK 0x1016 49 | #define SO_MYADDR 0x1013 50 | 51 | #define ENODATA 1 52 | #define EISCONN 3 53 | #define EWOULDBLOCK 6 54 | #define EALREADY 10 55 | #define EAGAIN EWOULDBLOCK 56 | #define EINVAL 11 57 | #define ENOMEM 18 58 | #define EINPROGRESS 22 59 | 60 | #define htonl(x) x 61 | #define htons(x) x 62 | #define ntohl(x) x 63 | #define ntohs(x) x 64 | 65 | 66 | struct in_addr { 67 | unsigned int s_addr; 68 | }; 69 | struct sockaddr_in { 70 | short sin_family; 71 | unsigned short sin_port; 72 | struct in_addr sin_addr; 73 | char sin_zero[8]; 74 | }; 75 | 76 | struct sockaddr 77 | { 78 | unsigned short sa_family; 79 | char sa_data[14]; 80 | }; 81 | 82 | 83 | void InitSocketFunctionPointers(void); 84 | 85 | extern void (*socket_lib_init)(void); 86 | extern int (*socket)(int domain, int type, int protocol); 87 | extern int (*socketclose)(int s); 88 | extern int (*connect)(int s, void *addr, int addrlen); 89 | extern int (*bind)(s32 s,struct sockaddr *name,s32 namelen); 90 | extern int (*listen)(s32 s,u32 backlog); 91 | extern int (*accept)(s32 s,struct sockaddr *addr,s32 *addrlen); 92 | extern int (*send)(int s, const void *buffer, int size, int flags); 93 | extern int (*recv)(int s, void *buffer, int size, int flags); 94 | extern int (*sendto)(int s, const void *buffer, int size, int flags, const struct sockaddr *dest, int dest_len); 95 | extern int (*setsockopt)(int s, int level, int optname, void *optval, int optlen); 96 | 97 | extern char * (*inet_ntoa)(struct in_addr in); 98 | extern int (*inet_aton)(const char *cp, struct in_addr *inp); 99 | 100 | extern int (*socketlasterr)(void); 101 | 102 | #define geterrno() (socketlasterr()) 103 | 104 | #ifdef __cplusplus 105 | } 106 | #endif 107 | 108 | #endif // __SOCKET_FUNCTIONS_H_ 109 | -------------------------------------------------------------------------------- /src/dynamic_libs/ax_functions.c: -------------------------------------------------------------------------------- 1 | /**************************************************************************** 2 | * Copyright (C) 2015 3 | * by Dimok 4 | * 5 | * This software is provided 'as-is', without any express or implied 6 | * warranty. In no event will the authors be held liable for any 7 | * damages arising from the use of this software. 8 | * 9 | * Permission is granted to anyone to use this software for any 10 | * purpose, including commercial applications, and to alter it and 11 | * redistribute it freely, subject to the following restrictions: 12 | * 13 | * 1. The origin of this software must not be misrepresented; you 14 | * must not claim that you wrote the original software. If you use 15 | * this software in a product, an acknowledgment in the product 16 | * documentation would be appreciated but is not required. 17 | * 18 | * 2. Altered source versions must be plainly marked as such, and 19 | * must not be misrepresented as being the original software. 20 | * 21 | * 3. This notice may not be removed or altered from any source 22 | * distribution. 23 | ***************************************************************************/ 24 | #include "os_functions.h" 25 | #include "ax_functions.h" 26 | 27 | EXPORT_DECL(void, AXInitWithParams, u32 * params); 28 | EXPORT_DECL(void, AXQuit, void); 29 | EXPORT_DECL(u32, AXGetInputSamplesPerSec, void); 30 | EXPORT_DECL(u32, AXGetInputSamplesPerFrame, void); 31 | EXPORT_DECL(s32, AXVoiceBegin, void *v); 32 | EXPORT_DECL(s32, AXVoiceEnd, void *v); 33 | EXPORT_DECL(void, AXSetVoiceType, void *v, u16 type); 34 | EXPORT_DECL(void, AXSetVoiceOffsets, void *v, const void *buf); 35 | EXPORT_DECL(void, AXSetVoiceSrcType, void *v, u32 type); 36 | EXPORT_DECL(void, AXSetVoiceVe, void *v, const void *vol); 37 | EXPORT_DECL(s32, AXSetVoiceDeviceMix, void *v, s32 device, u32 id, void *mix); 38 | EXPORT_DECL(void, AXSetVoiceState, void *v, u16 state); 39 | EXPORT_DECL(void, AXSetVoiceSrc, void *v, const void *src); 40 | EXPORT_DECL(s32, AXSetVoiceSrcRatio, void *v,f32 ratio) 41 | EXPORT_DECL(void *, AXAcquireVoice, u32 prio, void * callback, u32 arg); 42 | EXPORT_DECL(void, AXFreeVoice, void *v); 43 | EXPORT_DECL(void, AXRegisterFrameCallback, void * callback); 44 | EXPORT_DECL(u32, AXGetVoiceLoopCount, void *v); 45 | EXPORT_DECL(void, AXSetVoiceEndOffset, void *v, u32 offset); 46 | EXPORT_DECL(void, AXSetVoiceLoopOffset, void *v, u32 offset); 47 | 48 | void InitAXFunctionPointers(void) 49 | { 50 | unsigned int *funcPointer = 0; 51 | unsigned int sound_handle; 52 | OSDynLoad_Acquire("sndcore2.rpl", &sound_handle); 53 | 54 | OS_FIND_EXPORT(sound_handle, AXInitWithParams); 55 | OS_FIND_EXPORT(sound_handle, AXQuit); 56 | OS_FIND_EXPORT(sound_handle, AXGetInputSamplesPerSec); 57 | OS_FIND_EXPORT(sound_handle, AXVoiceBegin); 58 | OS_FIND_EXPORT(sound_handle, AXVoiceEnd); 59 | OS_FIND_EXPORT(sound_handle, AXSetVoiceType); 60 | OS_FIND_EXPORT(sound_handle, AXSetVoiceOffsets); 61 | OS_FIND_EXPORT(sound_handle, AXSetVoiceSrcType); 62 | OS_FIND_EXPORT(sound_handle, AXSetVoiceVe); 63 | OS_FIND_EXPORT(sound_handle, AXSetVoiceDeviceMix); 64 | OS_FIND_EXPORT(sound_handle, AXSetVoiceState); 65 | OS_FIND_EXPORT(sound_handle, AXSetVoiceSrc); 66 | OS_FIND_EXPORT(sound_handle, AXSetVoiceSrcRatio); 67 | OS_FIND_EXPORT(sound_handle, AXAcquireVoice); 68 | OS_FIND_EXPORT(sound_handle, AXFreeVoice); 69 | OS_FIND_EXPORT(sound_handle, AXRegisterFrameCallback); 70 | OS_FIND_EXPORT(sound_handle, AXGetVoiceLoopCount); 71 | OS_FIND_EXPORT(sound_handle, AXSetVoiceEndOffset); 72 | OS_FIND_EXPORT(sound_handle, AXSetVoiceLoopOffset); 73 | } 74 | 75 | -------------------------------------------------------------------------------- /src/dynamic_libs/socket_functions.c: -------------------------------------------------------------------------------- 1 | /**************************************************************************** 2 | * Copyright (C) 2015 3 | * by Dimok 4 | * 5 | * This software is provided 'as-is', without any express or implied 6 | * warranty. In no event will the authors be held liable for any 7 | * damages arising from the use of this software. 8 | * 9 | * Permission is granted to anyone to use this software for any 10 | * purpose, including commercial applications, and to alter it and 11 | * redistribute it freely, subject to the following restrictions: 12 | * 13 | * 1. The origin of this software must not be misrepresented; you 14 | * must not claim that you wrote the original software. If you use 15 | * this software in a product, an acknowledgment in the product 16 | * documentation would be appreciated but is not required. 17 | * 18 | * 2. Altered source versions must be plainly marked as such, and 19 | * must not be misrepresented as being the original software. 20 | * 21 | * 3. This notice may not be removed or altered from any source 22 | * distribution. 23 | ***************************************************************************/ 24 | #include "os_functions.h" 25 | #include "socket_functions.h" 26 | 27 | u32 hostIpAddress = 0; 28 | 29 | EXPORT_DECL(void, socket_lib_init, void); 30 | EXPORT_DECL(int, socket, int domain, int type, int protocol); 31 | EXPORT_DECL(int, socketclose, int s); 32 | EXPORT_DECL(int, connect, int s, void *addr, int addrlen); 33 | EXPORT_DECL(int, bind, s32 s,struct sockaddr *name,s32 namelen); 34 | EXPORT_DECL(int, listen, s32 s,u32 backlog); 35 | EXPORT_DECL(int, accept, s32 s,struct sockaddr *addr,s32 *addrlen); 36 | EXPORT_DECL(int, send, int s, const void *buffer, int size, int flags); 37 | EXPORT_DECL(int, recv, int s, void *buffer, int size, int flags); 38 | EXPORT_DECL(int, sendto, int s, const void *buffer, int size, int flags, const struct sockaddr *dest, int dest_len); 39 | EXPORT_DECL(int, setsockopt, int s, int level, int optname, void *optval, int optlen); 40 | EXPORT_DECL(char *, inet_ntoa, struct in_addr in); 41 | EXPORT_DECL(int, inet_aton, const char *cp, struct in_addr *inp); 42 | EXPORT_DECL(int, socketlasterr, void); 43 | 44 | void InitSocketFunctionPointers(void) 45 | { 46 | unsigned int nsysnet_handle; 47 | unsigned int *funcPointer = 0; 48 | OSDynLoad_Acquire("nsysnet.rpl", &nsysnet_handle); 49 | 50 | unsigned int nn_ac_handle; 51 | int(*ACInitialize)(); 52 | int(*ACGetStartupId) (unsigned int *id); 53 | int(*ACConnectWithConfigId) (unsigned int id); 54 | int(*ACGetAssignedAddress) (u32 * ip); 55 | OSDynLoad_Acquire("nn_ac.rpl", &nn_ac_handle); 56 | OSDynLoad_FindExport(nn_ac_handle, 0, "ACInitialize", &ACInitialize); 57 | OSDynLoad_FindExport(nn_ac_handle, 0, "ACGetStartupId", &ACGetStartupId); 58 | OSDynLoad_FindExport(nn_ac_handle, 0, "ACConnectWithConfigId",&ACConnectWithConfigId); 59 | OSDynLoad_FindExport(nn_ac_handle, 0, "ACGetAssignedAddress",&ACGetAssignedAddress); 60 | 61 | OS_FIND_EXPORT(nsysnet_handle, socket_lib_init); 62 | OS_FIND_EXPORT(nsysnet_handle, socket); 63 | OS_FIND_EXPORT(nsysnet_handle, socketclose); 64 | OS_FIND_EXPORT(nsysnet_handle, connect); 65 | OS_FIND_EXPORT(nsysnet_handle, bind); 66 | OS_FIND_EXPORT(nsysnet_handle, listen); 67 | OS_FIND_EXPORT(nsysnet_handle, accept); 68 | OS_FIND_EXPORT(nsysnet_handle, send); 69 | OS_FIND_EXPORT(nsysnet_handle, recv); 70 | OS_FIND_EXPORT(nsysnet_handle, sendto); 71 | OS_FIND_EXPORT(nsysnet_handle, setsockopt); 72 | OS_FIND_EXPORT(nsysnet_handle, inet_ntoa); 73 | OS_FIND_EXPORT(nsysnet_handle, inet_aton); 74 | OS_FIND_EXPORT(nsysnet_handle, socketlasterr); 75 | 76 | unsigned int nn_startupid; 77 | ACInitialize(); 78 | ACGetStartupId(&nn_startupid); 79 | ACConnectWithConfigId(nn_startupid); 80 | ACGetAssignedAddress(&hostIpAddress); 81 | 82 | socket_lib_init(); 83 | } 84 | 85 | -------------------------------------------------------------------------------- /src/dynamic_libs/vpad_functions.h: -------------------------------------------------------------------------------- 1 | /**************************************************************************** 2 | * Copyright (C) 2015 3 | * by Dimok 4 | * 5 | * This software is provided 'as-is', without any express or implied 6 | * warranty. In no event will the authors be held liable for any 7 | * damages arising from the use of this software. 8 | * 9 | * Permission is granted to anyone to use this software for any 10 | * purpose, including commercial applications, and to alter it and 11 | * redistribute it freely, subject to the following restrictions: 12 | * 13 | * 1. The origin of this software must not be misrepresented; you 14 | * must not claim that you wrote the original software. If you use 15 | * this software in a product, an acknowledgment in the product 16 | * documentation would be appreciated but is not required. 17 | * 18 | * 2. Altered source versions must be plainly marked as such, and 19 | * must not be misrepresented as being the original software. 20 | * 21 | * 3. This notice may not be removed or altered from any source 22 | * distribution. 23 | ***************************************************************************/ 24 | #ifndef __VPAD_FUNCTIONS_H_ 25 | #define __VPAD_FUNCTIONS_H_ 26 | 27 | #ifdef __cplusplus 28 | extern "C" { 29 | #endif 30 | 31 | #include 32 | 33 | #define VPAD_BUTTON_A 0x8000 34 | #define VPAD_BUTTON_B 0x4000 35 | #define VPAD_BUTTON_X 0x2000 36 | #define VPAD_BUTTON_Y 0x1000 37 | #define VPAD_BUTTON_LEFT 0x0800 38 | #define VPAD_BUTTON_RIGHT 0x0400 39 | #define VPAD_BUTTON_UP 0x0200 40 | #define VPAD_BUTTON_DOWN 0x0100 41 | #define VPAD_BUTTON_ZL 0x0080 42 | #define VPAD_BUTTON_ZR 0x0040 43 | #define VPAD_BUTTON_L 0x0020 44 | #define VPAD_BUTTON_R 0x0010 45 | #define VPAD_BUTTON_PLUS 0x0008 46 | #define VPAD_BUTTON_MINUS 0x0004 47 | #define VPAD_BUTTON_HOME 0x0002 48 | #define VPAD_BUTTON_SYNC 0x0001 49 | #define VPAD_BUTTON_STICK_R 0x00020000 50 | #define VPAD_BUTTON_STICK_L 0x00040000 51 | #define VPAD_BUTTON_TV 0x00010000 52 | 53 | #define VPAD_STICK_R_EMULATION_LEFT 0x04000000 54 | #define VPAD_STICK_R_EMULATION_RIGHT 0x02000000 55 | #define VPAD_STICK_R_EMULATION_UP 0x01000000 56 | #define VPAD_STICK_R_EMULATION_DOWN 0x00800000 57 | 58 | #define VPAD_STICK_L_EMULATION_LEFT 0x40000000 59 | #define VPAD_STICK_L_EMULATION_RIGHT 0x20000000 60 | #define VPAD_STICK_L_EMULATION_UP 0x10000000 61 | #define VPAD_STICK_L_EMULATION_DOWN 0x08000000 62 | 63 | 64 | typedef struct 65 | { 66 | f32 x,y; 67 | } Vec2D; 68 | 69 | typedef struct 70 | { 71 | u16 x, y; /* Touch coordinates */ 72 | u16 touched; /* 1 = Touched, 0 = Not touched */ 73 | u16 invalid; /* 0 = All valid, 1 = X invalid, 2 = Y invalid, 3 = Both invalid? */ 74 | } VPADTPData; 75 | 76 | typedef struct 77 | { 78 | u32 btns_h; /* Held buttons */ 79 | u32 btns_d; /* Buttons that are pressed at that instant */ 80 | u32 btns_r; /* Released buttons */ 81 | Vec2D lstick, rstick; /* Each contains 4-byte X and Y components */ 82 | char unknown1c[0x52 - 0x1c]; /* Contains accelerometer and gyroscope data somewhere */ 83 | VPADTPData tpdata; /* Normal touchscreen data */ 84 | VPADTPData tpdata1; /* Modified touchscreen data 1 */ 85 | VPADTPData tpdata2; /* Modified touchscreen data 2 */ 86 | char unknown6a[0xa0 - 0x6a]; 87 | uint8_t volume; 88 | uint8_t battery; /* 0 to 6 */ 89 | uint8_t unk_volume; /* One less than volume */ 90 | char unknowna4[0xac - 0xa4]; 91 | } VPADData; 92 | 93 | void InitVPadFunctionPointers(void); 94 | 95 | extern void (* VPADInit)(void); 96 | extern void (* VPADRead)(int chan, VPADData *buffer, u32 buffer_size, s32 *error); 97 | 98 | #ifdef __cplusplus 99 | } 100 | #endif 101 | 102 | #endif // __VPAD_FUNCTIONS_H_ 103 | -------------------------------------------------------------------------------- /src/fs/fs_utils.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include "common/fs_defs.h" 7 | #include "dynamic_libs/fs_functions.h" 8 | 9 | 10 | int MountFS(void *pClient, void *pCmd, char **mount_path) 11 | { 12 | int result = -1; 13 | 14 | void *mountSrc = malloc(FS_MOUNT_SOURCE_SIZE); 15 | if(!mountSrc) 16 | return -3; 17 | 18 | char* mountPath = (char*) malloc(FS_MAX_MOUNTPATH_SIZE); 19 | if(!mountPath) { 20 | free(mountSrc); 21 | return -4; 22 | } 23 | 24 | memset(mountSrc, 0, FS_MOUNT_SOURCE_SIZE); 25 | memset(mountPath, 0, FS_MAX_MOUNTPATH_SIZE); 26 | 27 | // Mount sdcard 28 | if (FSGetMountSource(pClient, pCmd, FS_SOURCETYPE_EXTERNAL, mountSrc, -1) == 0) 29 | { 30 | result = FSMount(pClient, pCmd, mountSrc, mountPath, FS_MAX_MOUNTPATH_SIZE, -1); 31 | if((result == 0) && mount_path) { 32 | *mount_path = (char*)malloc(strlen(mountPath) + 1); 33 | if(*mount_path) 34 | strcpy(*mount_path, mountPath); 35 | } 36 | } 37 | 38 | free(mountPath); 39 | free(mountSrc); 40 | return result; 41 | } 42 | 43 | int UmountFS(void *pClient, void *pCmd, const char *mountPath) 44 | { 45 | int result = -1; 46 | result = FSUnmount(pClient, pCmd, mountPath, -1); 47 | 48 | return result; 49 | } 50 | 51 | int LoadFileToMem(const char *filepath, u8 **inbuffer, u32 *size) 52 | { 53 | //! always initialze input 54 | *inbuffer = NULL; 55 | if(size) 56 | *size = 0; 57 | 58 | int iFd = open(filepath, O_RDONLY); 59 | if (iFd < 0) 60 | return -1; 61 | 62 | u32 filesize = lseek(iFd, 0, SEEK_END); 63 | lseek(iFd, 0, SEEK_SET); 64 | 65 | u8 *buffer = (u8 *) malloc(filesize); 66 | if (buffer == NULL) 67 | { 68 | close(iFd); 69 | return -2; 70 | } 71 | 72 | u32 blocksize = 0x4000; 73 | u32 done = 0; 74 | int readBytes = 0; 75 | 76 | while(done < filesize) 77 | { 78 | if(done + blocksize > filesize) { 79 | blocksize = filesize - done; 80 | } 81 | readBytes = read(iFd, buffer + done, blocksize); 82 | if(readBytes <= 0) 83 | break; 84 | done += readBytes; 85 | } 86 | 87 | close(iFd); 88 | 89 | if (done != filesize) 90 | { 91 | free(buffer); 92 | return -3; 93 | } 94 | 95 | *inbuffer = buffer; 96 | 97 | //! sign is optional input 98 | if(size) 99 | *size = filesize; 100 | 101 | return filesize; 102 | } 103 | 104 | int CheckFile(const char * filepath) 105 | { 106 | if(!filepath) 107 | return 0; 108 | 109 | struct stat filestat; 110 | 111 | char dirnoslash[strlen(filepath)+2]; 112 | snprintf(dirnoslash, sizeof(dirnoslash), "%s", filepath); 113 | 114 | while(dirnoslash[strlen(dirnoslash)-1] == '/') 115 | dirnoslash[strlen(dirnoslash)-1] = '\0'; 116 | 117 | char * notRoot = strrchr(dirnoslash, '/'); 118 | if(!notRoot) 119 | { 120 | strcat(dirnoslash, "/"); 121 | } 122 | 123 | if (stat(dirnoslash, &filestat) == 0) 124 | return 1; 125 | 126 | return 0; 127 | } 128 | 129 | int CreateSubfolder(const char * fullpath) 130 | { 131 | if(!fullpath) 132 | return 0; 133 | 134 | int result = 0; 135 | 136 | char dirnoslash[strlen(fullpath)+1]; 137 | strcpy(dirnoslash, fullpath); 138 | 139 | int pos = strlen(dirnoslash)-1; 140 | while(dirnoslash[pos] == '/') 141 | { 142 | dirnoslash[pos] = '\0'; 143 | pos--; 144 | } 145 | 146 | if(CheckFile(dirnoslash)) 147 | { 148 | return 1; 149 | } 150 | else 151 | { 152 | char parentpath[strlen(dirnoslash)+2]; 153 | strcpy(parentpath, dirnoslash); 154 | char * ptr = strrchr(parentpath, '/'); 155 | 156 | if(!ptr) 157 | { 158 | //!Device root directory (must be with '/') 159 | strcat(parentpath, "/"); 160 | struct stat filestat; 161 | if (stat(parentpath, &filestat) == 0) 162 | return 1; 163 | 164 | return 0; 165 | } 166 | 167 | ptr++; 168 | ptr[0] = '\0'; 169 | 170 | result = CreateSubfolder(parentpath); 171 | } 172 | 173 | if(!result) 174 | return 0; 175 | 176 | if (mkdir(dirnoslash, 0777) == -1) 177 | { 178 | return 0; 179 | } 180 | 181 | return 1; 182 | } 183 | -------------------------------------------------------------------------------- /src/dynamic_libs/padscore_functions.h: -------------------------------------------------------------------------------- 1 | /**************************************************************************** 2 | * Copyright (C) 2015 3 | * by Dimok 4 | * 5 | * This software is provided 'as-is', without any express or implied 6 | * warranty. In no event will the authors be held liable for any 7 | * damages arising from the use of this software. 8 | * 9 | * Permission is granted to anyone to use this software for any 10 | * purpose, including commercial applications, and to alter it and 11 | * redistribute it freely, subject to the following restrictions: 12 | * 13 | * 1. The origin of this software must not be misrepresented; you 14 | * must not claim that you wrote the original software. If you use 15 | * this software in a product, an acknowledgment in the product 16 | * documentation would be appreciated but is not required. 17 | * 18 | * 2. Altered source versions must be plainly marked as such, and 19 | * must not be misrepresented as being the original software. 20 | * 21 | * 3. This notice may not be removed or altered from any source 22 | * distribution. 23 | ***************************************************************************/ 24 | #ifndef __PAD_SCORE_FUNCTIONS_H_ 25 | #define __PAD_SCORE_FUNCTIONS_H_ 26 | 27 | #ifdef __cplusplus 28 | extern "C" { 29 | #endif 30 | 31 | #include 32 | 33 | #define WPAD_BUTTON_LEFT 0x0001 34 | #define WPAD_BUTTON_RIGHT 0x0002 35 | #define WPAD_BUTTON_DOWN 0x0004 36 | #define WPAD_BUTTON_UP 0x0008 37 | #define WPAD_BUTTON_PLUS 0x0010 38 | #define WPAD_BUTTON_2 0x0100 39 | #define WPAD_BUTTON_1 0x0200 40 | #define WPAD_BUTTON_B 0x0400 41 | #define WPAD_BUTTON_A 0x0800 42 | #define WPAD_BUTTON_MINUS 0x1000 43 | #define WPAD_BUTTON_Z 0x2000 44 | #define WPAD_BUTTON_C 0x4000 45 | #define WPAD_BUTTON_HOME 0x8000 46 | 47 | #define WPAD_CLASSIC_BUTTON_UP 0x0001 48 | #define WPAD_CLASSIC_BUTTON_LEFT 0x0002 49 | #define WPAD_CLASSIC_BUTTON_ZR 0x0004 50 | #define WPAD_CLASSIC_BUTTON_X 0x0008 51 | #define WPAD_CLASSIC_BUTTON_A 0x0010 52 | #define WPAD_CLASSIC_BUTTON_Y 0x0020 53 | #define WPAD_CLASSIC_BUTTON_B 0x0040 54 | #define WPAD_CLASSIC_BUTTON_ZL 0x0080 55 | #define WPAD_CLASSIC_BUTTON_R 0x0200 56 | #define WPAD_CLASSIC_BUTTON_PLUS 0x0400 57 | #define WPAD_CLASSIC_BUTTON_HOME 0x0800 58 | #define WPAD_CLASSIC_BUTTON_MINUS 0x1000 59 | #define WPAD_CLASSIC_BUTTON_L 0x2000 60 | #define WPAD_CLASSIC_BUTTON_DOWN 0x4000 61 | #define WPAD_CLASSIC_BUTTON_RIGHT 0x8000 62 | 63 | void InitPadScoreFunctionPointers(void); 64 | 65 | 66 | typedef struct _KPADData 67 | { 68 | u32 btns_h; 69 | u32 btns_d; 70 | u32 btns_r; 71 | u32 unused_1[5]; 72 | f32 pos_x; 73 | f32 pos_y; 74 | u32 unused_2[3]; 75 | f32 angle_x; 76 | f32 angle_y; 77 | u32 unused_3[8]; 78 | u8 device_type; 79 | u8 wpad_error; 80 | u8 pos_valid; 81 | u8 unused_4[1]; 82 | 83 | union 84 | { 85 | struct 86 | { 87 | f32 stick_x; 88 | f32 stick_y; 89 | } nunchuck; 90 | 91 | struct 92 | { 93 | u32 btns_h; 94 | u32 btns_d; 95 | u32 btns_r; 96 | f32 lstick_x; 97 | f32 lstick_y; 98 | f32 rstick_x; 99 | f32 rstick_y; 100 | f32 ltrigger; 101 | f32 rtrigger; 102 | } classic; 103 | 104 | u32 unused_6[20]; 105 | }; 106 | u32 unused_7[16]; 107 | } KPADData; 108 | 109 | typedef void (* wpad_connect_callback_t)(s32 chan, s32 status); 110 | 111 | extern void (* KPADInit)(void); 112 | extern s32 (* WPADProbe)(s32 chan, u32 * pad_type); 113 | extern s32 (* WPADSetDataFormat)(s32 chan, s32 format); 114 | extern void (* WPADEnableURCC)(s32 enable); 115 | extern void (* WPADRead)(s32 chan, void * data); 116 | extern s32 (* KPADRead)(s32 chan, void * data, u32 size); 117 | 118 | #ifdef __cplusplus 119 | } 120 | #endif 121 | 122 | #endif // __PAD_SCORE_FUNCTIONS_H_ 123 | -------------------------------------------------------------------------------- /src/dynamic_libs/fs_functions.h: -------------------------------------------------------------------------------- 1 | /**************************************************************************** 2 | * Copyright (C) 2015 3 | * by Dimok 4 | * 5 | * This software is provided 'as-is', without any express or implied 6 | * warranty. In no event will the authors be held liable for any 7 | * damages arising from the use of this software. 8 | * 9 | * Permission is granted to anyone to use this software for any 10 | * purpose, including commercial applications, and to alter it and 11 | * redistribute it freely, subject to the following restrictions: 12 | * 13 | * 1. The origin of this software must not be misrepresented; you 14 | * must not claim that you wrote the original software. If you use 15 | * this software in a product, an acknowledgment in the product 16 | * documentation would be appreciated but is not required. 17 | * 18 | * 2. Altered source versions must be plainly marked as such, and 19 | * must not be misrepresented as being the original software. 20 | * 21 | * 3. This notice may not be removed or altered from any source 22 | * distribution. 23 | ***************************************************************************/ 24 | #ifndef __FS_FUNCTIONS_H_ 25 | #define __FS_FUNCTIONS_H_ 26 | 27 | #ifdef __cplusplus 28 | extern "C" { 29 | #endif 30 | 31 | #include "common/fs_defs.h" 32 | 33 | void InitFSFunctionPointers(void); 34 | 35 | extern int (* FSInit)(void); 36 | extern int (* FSShutdown)(void); 37 | extern int (* FSAddClientEx)(void *pClient, int unk_zero_param, int errHandling); 38 | extern int (* FSDelClient)(void *pClient); 39 | extern void (* FSInitCmdBlock)(void *pCmd); 40 | extern int (* FSGetMountSource)(void *pClient, void *pCmd, int type, void *source, int errHandling); 41 | 42 | extern int (* FSMount)(void *pClient, void *pCmd, void *source, char *target, uint32_t bytes, int errHandling); 43 | extern int (* FSUnmount)(void *pClient, void *pCmd, const char *target, int errHandling); 44 | extern int (* FSRename)(void *pClient, void *pCmd, const char *oldPath, const char *newPath, int error); 45 | extern int (* FSRenameAsync)(void *pClient, void *pCmd, const char *oldPath, const char *newPath, int error, void *asyncParams); 46 | extern int (* FSRemove)(void *pClient, void *pCmd, const char *path, int error); 47 | extern int (* FSRemoveAsync)(void *pClient, void *pCmd, const char *path, int error, void *asyncParams); 48 | 49 | extern int (* FSGetStat)(void *pClient, void *pCmd, const char *path, FSStat *stats, int errHandling); 50 | extern int (* FSGetStatAsync)(void *pClient, void *pCmd, const char *path, void *stats, int error, void *asyncParams); 51 | extern int (* FSRename)(void *pClient, void *pCmd, const char *oldPath, const char *newPath, int error); 52 | extern int (* FSRenameAsync)(void *pClient, void *pCmd, const char *oldPath, const char *newPath, int error, void *asyncParams); 53 | extern int (* FSRemove)(void *pClient, void *pCmd, const char *path, int error); 54 | extern int (* FSRemoveAsync)(void *pClient, void *pCmd, const char *path, int error, void *asyncParams); 55 | extern int (* FSFlushQuota)(void *pClient, void *pCmd, const char* path, int error); 56 | extern int (* FSFlushQuotaAsync)(void *pClient, void *pCmd, const char *path, int error, void *asyncParams); 57 | extern int (* FSGetFreeSpaceSize)(void *pClient, void *pCmd, const char *path, uint64_t *returnedFreeSize, int error); 58 | extern int (* FSGetFreeSpaceSizeAsync)(void *pClient, void *pCmd, const char *path, uint64_t *returnedFreeSize, int error, void *asyncParams); 59 | extern int (* FSRollbackQuota)(void *pClient, void *pCmd, const char *path, int error); 60 | extern int (* FSRollbackQuotaAsync)(void *pClient, void *pCmd, const char *path, int error, void *asyncParams); 61 | 62 | extern int (* FSOpenDir)(void *pClient, void *pCmd, const char *path, int *dh, int errHandling); 63 | extern int (* FSOpenDirAsync)(void *pClient, void* pCmd, const char *path, int *handle, int error, void *asyncParams); 64 | extern int (* FSReadDir)(void *pClient, void *pCmd, int dh, FSDirEntry *dir_entry, int errHandling); 65 | extern int (* FSRewindDir)(void *pClient, void *pCmd, int dh, int errHandling); 66 | extern int (* FSCloseDir)(void *pClient, void *pCmd, int dh, int errHandling); 67 | extern int (* FSChangeDir)(void *pClient, void *pCmd, const char *path, int errHandling); 68 | extern int (* FSChangeDirAsync)(void *pClient, void *pCmd, const char *path, int error, void *asyncParams); 69 | extern int (* FSMakeDir)(void *pClient, void *pCmd, const char *path, int errHandling); 70 | extern int (* FSMakeDirAsync)(void *pClient, void *pCmd, const char *path, int error, void *asyncParams); 71 | 72 | extern int (* FSOpenFile)(void *pClient, void *pCmd, const char *path, const char *mode, int *fd, int errHandling); 73 | extern int (* FSOpenFileAsync)(void *pClient, void *pCmd, const char *path, const char *mode, int *handle, int error, const void *asyncParams); 74 | extern int (* FSReadFile)(void *pClient, void *pCmd, void *buffer, int size, int count, int fd, int flag, int errHandling); 75 | extern int (* FSCloseFile)(void *pClient, void *pCmd, int fd, int errHandling); 76 | 77 | extern int (* FSFlushFile)(void *pClient, void *pCmd, int fd, int error); 78 | extern int (* FSTruncateFile)(void *pClient, void *pCmd, int fd, int error); 79 | extern int (* FSGetStatFile)(void *pClient, void *pCmd, int fd, void *buffer, int error); 80 | extern int (* FSSetPosFile)(void *pClient, void *pCmd, int fd, int pos, int error); 81 | extern int (* FSWriteFile)(void *pClient, void *pCmd, const void *source, int block_size, int block_count, int fd, int flag, int error); 82 | 83 | #ifdef __cplusplus 84 | } 85 | #endif 86 | 87 | #endif // __FS_FUNCTIONS_H_ 88 | -------------------------------------------------------------------------------- /src/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include "dynamic_libs/os_functions.h" 6 | #include "dynamic_libs/fs_functions.h" 7 | #include "dynamic_libs/gx2_functions.h" 8 | #include "dynamic_libs/sys_functions.h" 9 | #include "dynamic_libs/vpad_functions.h" 10 | #include "dynamic_libs/padscore_functions.h" 11 | #include "dynamic_libs/socket_functions.h" 12 | #include "dynamic_libs/ax_functions.h" 13 | #include "fs/fs_utils.h" 14 | #include "fs/sd_fat_devoptab.h" 15 | #include "system/memory.h" 16 | #include "utils/logger.h" 17 | #include "utils/utils.h" 18 | #include "common/common.h" 19 | #include "ftp.h" 20 | #include "virtualpath.h" 21 | #include "net.h" 22 | 23 | #define PORT 21 24 | #define MAX_CONSOLE_LINES_TV 27 25 | #define MAX_CONSOLE_LINES_DRC 18 26 | 27 | static char * consoleArrayTv[MAX_CONSOLE_LINES_TV]; 28 | static char * consoleArrayDrc[MAX_CONSOLE_LINES_DRC]; 29 | 30 | void console_printf(const char *format, ...) 31 | { 32 | char * tmp = NULL; 33 | 34 | va_list va; 35 | va_start(va, format); 36 | if((vasprintf(&tmp, format, va) >= 0) && tmp) 37 | { 38 | if(consoleArrayTv[0]) 39 | free(consoleArrayTv[0]); 40 | if(consoleArrayDrc[0]) 41 | free(consoleArrayDrc[0]); 42 | 43 | for(int i = 1; i < MAX_CONSOLE_LINES_TV; i++) 44 | consoleArrayTv[i-1] = consoleArrayTv[i]; 45 | 46 | for(int i = 1; i < MAX_CONSOLE_LINES_DRC; i++) 47 | consoleArrayDrc[i-1] = consoleArrayDrc[i]; 48 | 49 | if(strlen(tmp) > 79) 50 | tmp[79] = 0; 51 | 52 | consoleArrayTv[MAX_CONSOLE_LINES_TV-1] = (char*)malloc(strlen(tmp) + 1); 53 | if(consoleArrayTv[MAX_CONSOLE_LINES_TV-1]) 54 | strcpy(consoleArrayTv[MAX_CONSOLE_LINES_TV-1], tmp); 55 | 56 | consoleArrayDrc[MAX_CONSOLE_LINES_DRC-1] = (tmp); 57 | } 58 | va_end(va); 59 | 60 | // Clear screens 61 | OSScreenClearBufferEx(0, 0); 62 | OSScreenClearBufferEx(1, 0); 63 | 64 | 65 | for(int i = 0; i < MAX_CONSOLE_LINES_TV; i++) 66 | { 67 | if(consoleArrayTv[i]) 68 | OSScreenPutFontEx(0, 0, i, consoleArrayTv[i]); 69 | } 70 | 71 | for(int i = 0; i < MAX_CONSOLE_LINES_DRC; i++) 72 | { 73 | if(consoleArrayDrc[i]) 74 | OSScreenPutFontEx(1, 0, i, consoleArrayDrc[i]); 75 | } 76 | 77 | OSScreenFlipBuffersEx(0); 78 | OSScreenFlipBuffersEx(1); 79 | } 80 | 81 | /* Entry point */ 82 | int Menu_Main(void) 83 | { 84 | //!******************************************************************* 85 | //! Initialize function pointers * 86 | //!******************************************************************* 87 | //! do OS (for acquire) and sockets first so we got logging 88 | InitOSFunctionPointers(); 89 | InitSocketFunctionPointers(); 90 | 91 | log_init("192.168.178.3"); 92 | log_print("Starting launcher\n"); 93 | 94 | InitFSFunctionPointers(); 95 | InitVPadFunctionPointers(); 96 | 97 | log_print("Function exports loaded\n"); 98 | 99 | //!******************************************************************* 100 | //! Initialize heap memory * 101 | //!******************************************************************* 102 | log_print("Initialize memory management\n"); 103 | //! We don't need bucket and MEM1 memory so no need to initialize 104 | //memoryInitialize(); 105 | 106 | //!******************************************************************* 107 | //! Initialize FS * 108 | //!******************************************************************* 109 | log_printf("Mount SD partition\n"); 110 | mount_sd_fat("sd"); 111 | 112 | for(int i = 0; i < MAX_CONSOLE_LINES_TV; i++) 113 | consoleArrayTv[i] = NULL; 114 | 115 | for(int i = 0; i < MAX_CONSOLE_LINES_DRC; i++) 116 | consoleArrayDrc[i] = NULL; 117 | 118 | VPADInit(); 119 | 120 | // Prepare screen 121 | int screen_buf0_size = 0; 122 | 123 | // Init screen and screen buffers 124 | OSScreenInit(); 125 | screen_buf0_size = OSScreenGetBufferSizeEx(0); 126 | OSScreenSetBufferEx(0, (void *)0xF4000000); 127 | OSScreenSetBufferEx(1, (void *)(0xF4000000 + screen_buf0_size)); 128 | 129 | OSScreenEnableEx(0, 1); 130 | OSScreenEnableEx(1, 1); 131 | 132 | // Clear screens 133 | OSScreenClearBufferEx(0, 0); 134 | OSScreenClearBufferEx(1, 0); 135 | 136 | // Flip buffers 137 | OSScreenFlipBuffersEx(0); 138 | OSScreenFlipBuffersEx(1); 139 | 140 | console_printf("FTPiiU v0.4 is listening on %u.%u.%u.%u:%i", (network_gethostip() >> 24) & 0xFF, (network_gethostip() >> 16) & 0xFF, (network_gethostip() >> 8) & 0xFF, (network_gethostip() >> 0) & 0xFF, PORT); 141 | 142 | MountVirtualDevices(); 143 | 144 | int serverSocket = create_server(PORT); 145 | 146 | int network_down = 0; 147 | int vpadError = -1; 148 | VPADData vpad; 149 | int vpadReadCounter = 0; 150 | 151 | while(serverSocket >= 0 && !network_down) 152 | { 153 | network_down = process_ftp_events(serverSocket); 154 | if(network_down) 155 | { 156 | break; 157 | } 158 | 159 | //! update only at 50 Hz, thats more than enough 160 | if(++vpadReadCounter >= 20) 161 | { 162 | vpadReadCounter = 0; 163 | 164 | VPADRead(0, &vpad, 1, &vpadError); 165 | 166 | if(vpadError == 0 && ((vpad.btns_d | vpad.btns_h) & VPAD_BUTTON_HOME)) 167 | break; 168 | } 169 | 170 | usleep(1000); 171 | } 172 | 173 | cleanup_ftp(); 174 | if(serverSocket >= 0) 175 | network_close(serverSocket); 176 | UnmountVirtualPaths(); 177 | 178 | //! free memory 179 | for(int i = 0; i < MAX_CONSOLE_LINES_TV; i++) 180 | { 181 | if(consoleArrayTv[i]) 182 | free(consoleArrayTv[i]); 183 | } 184 | 185 | for(int i = 0; i < MAX_CONSOLE_LINES_DRC; i++) 186 | { 187 | if(consoleArrayDrc[i]) 188 | free(consoleArrayDrc[i]); 189 | } 190 | 191 | //!******************************************************************* 192 | //! Enter main application * 193 | //!******************************************************************* 194 | 195 | log_printf("Unmount SD\n"); 196 | unmount_sd_fat("sd"); 197 | log_printf("Release memory\n"); 198 | //memoryRelease(); 199 | log_deinit(); 200 | 201 | return EXIT_SUCCESS; 202 | } 203 | 204 | -------------------------------------------------------------------------------- /src/system/memory.c: -------------------------------------------------------------------------------- 1 | /**************************************************************************** 2 | * Copyright (C) 2015 Dimok 3 | * 4 | * This program is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU General Public License as published by 6 | * the Free Software Foundation, either version 3 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with this program. If not, see . 16 | ****************************************************************************/ 17 | #include 18 | #include 19 | #include "dynamic_libs/os_functions.h" 20 | #include "common/common.h" 21 | #include "memory.h" 22 | 23 | #define MEMORY_ARENA_1 0 24 | #define MEMORY_ARENA_2 1 25 | #define MEMORY_ARENA_3 2 26 | #define MEMORY_ARENA_4 3 27 | #define MEMORY_ARENA_5 4 28 | #define MEMORY_ARENA_6 5 29 | #define MEMORY_ARENA_7 6 30 | #define MEMORY_ARENA_8 7 31 | #define MEMORY_ARENA_FG_BUCKET 8 32 | 33 | //!---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 34 | //! Memory functions 35 | //! This is the only place where those are needed so lets keep them more or less private 36 | //!---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 37 | extern unsigned int * pMEMAllocFromDefaultHeapEx; 38 | extern unsigned int * pMEMAllocFromDefaultHeap; 39 | extern unsigned int * pMEMFreeToDefaultHeap; 40 | 41 | extern int (* MEMGetBaseHeapHandle)(int mem_arena); 42 | extern unsigned int (* MEMGetAllocatableSizeForFrmHeapEx)(int heap, int align); 43 | extern void *(* MEMAllocFromFrmHeapEx)(int heap, unsigned int size, int align); 44 | extern void (* MEMFreeToFrmHeap)(int heap, int mode); 45 | extern void *(* MEMAllocFromExpHeapEx)(int heap, unsigned int size, int align); 46 | extern int (* MEMCreateExpHeapEx)(void* address, unsigned int size, unsigned short flags); 47 | extern void *(* MEMDestroyExpHeap)(int heap); 48 | extern void (* MEMFreeToExpHeap)(int heap, void* ptr); 49 | 50 | static int mem1_heap = -1; 51 | static int bucket_heap = -1; 52 | 53 | void memoryInitialize(void) 54 | { 55 | int mem1_heap_handle = MEMGetBaseHeapHandle(MEMORY_ARENA_1); 56 | unsigned int mem1_allocatable_size = MEMGetAllocatableSizeForFrmHeapEx(mem1_heap_handle, 4); 57 | void *mem1_memory = MEMAllocFromFrmHeapEx(mem1_heap_handle, mem1_allocatable_size, 4); 58 | if(mem1_memory) 59 | mem1_heap = MEMCreateExpHeapEx(mem1_memory, mem1_allocatable_size, 0); 60 | 61 | int bucket_heap_handle = MEMGetBaseHeapHandle(MEMORY_ARENA_FG_BUCKET); 62 | unsigned int bucket_allocatable_size = MEMGetAllocatableSizeForFrmHeapEx(bucket_heap_handle, 4); 63 | void *bucket_memory = MEMAllocFromFrmHeapEx(bucket_heap_handle, bucket_allocatable_size, 4); 64 | if(bucket_memory) 65 | bucket_heap = MEMCreateExpHeapEx(bucket_memory, bucket_allocatable_size, 0); 66 | } 67 | 68 | void memoryRelease(void) 69 | { 70 | MEMDestroyExpHeap(mem1_heap); 71 | MEMFreeToFrmHeap(MEMGetBaseHeapHandle(MEMORY_ARENA_1), 3); 72 | mem1_heap = -1; 73 | 74 | MEMDestroyExpHeap(bucket_heap); 75 | MEMFreeToFrmHeap(MEMGetBaseHeapHandle(MEMORY_ARENA_FG_BUCKET), 3); 76 | bucket_heap = -1; 77 | } 78 | 79 | //!------------------------------------------------------------------------------------------- 80 | //! wraps 81 | //!------------------------------------------------------------------------------------------- 82 | void *__wrap_malloc(size_t size) 83 | { 84 | // pointer to a function resolve 85 | return ((void * (*)(size_t))(*pMEMAllocFromDefaultHeap))(size); 86 | } 87 | 88 | void *__wrap_memalign(size_t align, size_t size) 89 | { 90 | if (align < 4) 91 | align = 4; 92 | 93 | // pointer to a function resolve 94 | return ((void * (*)(size_t, size_t))(*pMEMAllocFromDefaultHeapEx))(size, align); 95 | } 96 | 97 | void __wrap_free(void *p) 98 | { 99 | // pointer to a function resolve 100 | if(p != 0) 101 | ((void (*)(void *))(*pMEMFreeToDefaultHeap))(p); 102 | } 103 | 104 | void *__wrap_calloc(size_t n, size_t size) 105 | { 106 | void *p = __wrap_malloc(n * size); 107 | if (p != 0) { 108 | memset(p, 0, n * size); 109 | } 110 | return p; 111 | } 112 | 113 | size_t __wrap_malloc_usable_size(void *p) 114 | { 115 | //! TODO: this is totally wrong and needs to be addressed 116 | return 0x7FFFFFFF; 117 | } 118 | 119 | void *__wrap_realloc(void *p, size_t size) 120 | { 121 | void *new_ptr = __wrap_malloc(size); 122 | if (new_ptr != 0) 123 | { 124 | memcpy(new_ptr, p, __wrap_malloc_usable_size(p) < size ? __wrap_malloc_usable_size(p) : size); 125 | __wrap_free(p); 126 | } 127 | return new_ptr; 128 | } 129 | 130 | //!------------------------------------------------------------------------------------------- 131 | //! reent versions 132 | //!------------------------------------------------------------------------------------------- 133 | void *__wrap__malloc_r(struct _reent *r, size_t size) 134 | { 135 | return __wrap_malloc(size); 136 | } 137 | 138 | void *__wrap__calloc_r(struct _reent *r, size_t n, size_t size) 139 | { 140 | return __wrap_calloc(n, size); 141 | } 142 | 143 | void *__wrap__memalign_r(struct _reent *r, size_t align, size_t size) 144 | { 145 | return __wrap_memalign(align, size); 146 | } 147 | 148 | void __wrap__free_r(struct _reent *r, void *p) 149 | { 150 | __wrap_free(p); 151 | } 152 | 153 | size_t __wrap__malloc_usable_size_r(struct _reent *r, void *p) 154 | { 155 | return __wrap_malloc_usable_size(p); 156 | } 157 | 158 | void *__wrap__realloc_r(struct _reent *r, void *p, size_t size) 159 | { 160 | return __wrap_realloc(p, size); 161 | } 162 | 163 | //!------------------------------------------------------------------------------------------- 164 | //! some wrappers 165 | //!------------------------------------------------------------------------------------------- 166 | void * MEM2_alloc(unsigned int size, unsigned int align) 167 | { 168 | return __wrap_memalign(align, size); 169 | } 170 | 171 | void MEM2_free(void *ptr) 172 | { 173 | __wrap_free(ptr); 174 | } 175 | 176 | void * MEM1_alloc(unsigned int size, unsigned int align) 177 | { 178 | if (align < 4) 179 | align = 4; 180 | return MEMAllocFromExpHeapEx(mem1_heap, size, align); 181 | } 182 | 183 | void MEM1_free(void *ptr) 184 | { 185 | MEMFreeToExpHeap(mem1_heap, ptr); 186 | } 187 | 188 | void * MEMBucket_alloc(unsigned int size, unsigned int align) 189 | { 190 | if (align < 4) 191 | align = 4; 192 | return MEMAllocFromExpHeapEx(bucket_heap, size, align); 193 | } 194 | 195 | void MEMBucket_free(void *ptr) 196 | { 197 | MEMFreeToExpHeap(bucket_heap, ptr); 198 | } 199 | -------------------------------------------------------------------------------- /src/dynamic_libs/fs_functions.c: -------------------------------------------------------------------------------- 1 | /**************************************************************************** 2 | * Copyright (C) 2015 3 | * by Dimok 4 | * 5 | * This software is provided 'as-is', without any express or implied 6 | * warranty. In no event will the authors be held liable for any 7 | * damages arising from the use of this software. 8 | * 9 | * Permission is granted to anyone to use this software for any 10 | * purpose, including commercial applications, and to alter it and 11 | * redistribute it freely, subject to the following restrictions: 12 | * 13 | * 1. The origin of this software must not be misrepresented; you 14 | * must not claim that you wrote the original software. If you use 15 | * this software in a product, an acknowledgment in the product 16 | * documentation would be appreciated but is not required. 17 | * 18 | * 2. Altered source versions must be plainly marked as such, and 19 | * must not be misrepresented as being the original software. 20 | * 21 | * 3. This notice may not be removed or altered from any source 22 | * distribution. 23 | ***************************************************************************/ 24 | #include "fs_functions.h" 25 | #include "os_functions.h" 26 | #include "utils/utils.h" 27 | 28 | EXPORT_DECL(int, FSInit, void); 29 | EXPORT_DECL(int, FSShutdown, void); 30 | EXPORT_DECL(int, FSAddClientEx, void *pClient, int unk_zero_param, int errHandling); 31 | EXPORT_DECL(int, FSDelClient, void *pClient); 32 | EXPORT_DECL(void, FSInitCmdBlock, void *pCmd); 33 | EXPORT_DECL(int, FSGetMountSource, void *pClient, void *pCmd, int type, void *source, int errHandling); 34 | 35 | EXPORT_DECL(int, FSMount, void *pClient, void *pCmd, void *source, char *target, uint32_t bytes, int errHandling); 36 | EXPORT_DECL(int, FSUnmount, void *pClient, void *pCmd, const char *target, int errHandling); 37 | 38 | EXPORT_DECL(int, FSGetStat, void *pClient, void *pCmd, const char *path, FSStat *stats, int errHandling); 39 | EXPORT_DECL(int, FSGetStatAsync, void *pClient, void *pCmd, const char *path, void *stats, int error, void *asyncParams); 40 | EXPORT_DECL(int, FSRename, void *pClient, void *pCmd, const char *oldPath, const char *newPath, int error); 41 | EXPORT_DECL(int, FSRenameAsync, void *pClient, void *pCmd, const char *oldPath, const char *newPath, int error, void *asyncParams); 42 | EXPORT_DECL(int, FSRemove, void *pClient, void *pCmd, const char *path, int error); 43 | EXPORT_DECL(int, FSRemoveAsync, void *pClient, void *pCmd, const char *path, int error, void *asyncParams); 44 | EXPORT_DECL(int, FSFlushQuota, void *pClient, void *pCmd, const char* path, int error); 45 | EXPORT_DECL(int, FSFlushQuotaAsync, void *pClient, void *pCmd, const char *path, int error, void *asyncParams); 46 | EXPORT_DECL(int, FSGetFreeSpaceSize, void *pClient, void *pCmd, const char *path, uint64_t *returnedFreeSize, int error); 47 | EXPORT_DECL(int, FSGetFreeSpaceSizeAsync, void *pClient, void *pCmd, const char *path, uint64_t *returnedFreeSize, int error, void *asyncParams); 48 | EXPORT_DECL(int, FSRollbackQuota, void *pClient, void *pCmd, const char *path, int error); 49 | EXPORT_DECL(int, FSRollbackQuotaAsync, void *pClient, void *pCmd, const char *path, int error, void *asyncParams); 50 | 51 | EXPORT_DECL(int, FSOpenDir, void *pClient, void *pCmd, const char *path, int *dh, int errHandling); 52 | EXPORT_DECL(int, FSOpenDirAsync, void *pClient, void* pCmd, const char *path, int *handle, int error, void *asyncParams); 53 | EXPORT_DECL(int, FSReadDir, void *pClient, void *pCmd, int dh, FSDirEntry *dir_entry, int errHandling); 54 | EXPORT_DECL(int, FSRewindDir, void *pClient, void *pCmd, int dh, int errHandling); 55 | EXPORT_DECL(int, FSCloseDir, void *pClient, void *pCmd, int dh, int errHandling); 56 | EXPORT_DECL(int, FSChangeDir, void *pClient, void *pCmd, const char *path, int errHandling); 57 | EXPORT_DECL(int, FSChangeDirAsync, void *pClient, void *pCmd, const char *path, int error, void *asyncParams); 58 | EXPORT_DECL(int, FSMakeDir, void *pClient, void *pCmd, const char *path, int errHandling); 59 | EXPORT_DECL(int, FSMakeDirAsync, void *pClient, void *pCmd, const char *path, int error, void *asyncParams); 60 | 61 | EXPORT_DECL(int, FSOpenFile, void *pClient, void *pCmd, const char *path, const char *mode, int *fd, int errHandling); 62 | EXPORT_DECL(int, FSOpenFileAsync, void *pClient, void *pCmd, const char *path, const char *mode, int *handle, int error, const void *asyncParams); 63 | EXPORT_DECL(int, FSReadFile, void *pClient, void *pCmd, void *buffer, int size, int count, int fd, int flag, int errHandling); 64 | EXPORT_DECL(int, FSCloseFile, void *pClient, void *pCmd, int fd, int errHandling); 65 | 66 | EXPORT_DECL(int, FSFlushFile, void *pClient, void *pCmd, int fd, int error); 67 | EXPORT_DECL(int, FSTruncateFile, void *pClient, void *pCmd, int fd, int error); 68 | EXPORT_DECL(int, FSGetStatFile, void *pClient, void *pCmd, int fd, void *buffer, int error); 69 | EXPORT_DECL(int, FSSetPosFile, void *pClient, void *pCmd, int fd, int pos, int error); 70 | EXPORT_DECL(int, FSWriteFile, void *pClient, void *pCmd, const void *source, int block_size, int block_count, int fd, int flag, int error); 71 | 72 | void InitFSFunctionPointers(void) 73 | { 74 | unsigned int *funcPointer = 0; 75 | 76 | OS_FIND_EXPORT(coreinit_handle, FSInit); 77 | OS_FIND_EXPORT(coreinit_handle, FSShutdown); 78 | OS_FIND_EXPORT(coreinit_handle, FSAddClientEx); 79 | OS_FIND_EXPORT(coreinit_handle, FSDelClient); 80 | OS_FIND_EXPORT(coreinit_handle, FSInitCmdBlock); 81 | OS_FIND_EXPORT(coreinit_handle, FSGetMountSource); 82 | 83 | OS_FIND_EXPORT(coreinit_handle, FSMount); 84 | OS_FIND_EXPORT(coreinit_handle, FSUnmount); 85 | 86 | OS_FIND_EXPORT(coreinit_handle, FSGetStat); 87 | OS_FIND_EXPORT(coreinit_handle, FSGetStatAsync); 88 | OS_FIND_EXPORT(coreinit_handle, FSRename); 89 | OS_FIND_EXPORT(coreinit_handle, FSRenameAsync); 90 | OS_FIND_EXPORT(coreinit_handle, FSRemove); 91 | OS_FIND_EXPORT(coreinit_handle, FSRemoveAsync); 92 | OS_FIND_EXPORT(coreinit_handle, FSFlushQuota); 93 | OS_FIND_EXPORT(coreinit_handle, FSFlushQuotaAsync); 94 | OS_FIND_EXPORT(coreinit_handle, FSGetFreeSpaceSize); 95 | OS_FIND_EXPORT(coreinit_handle, FSGetFreeSpaceSizeAsync); 96 | OS_FIND_EXPORT(coreinit_handle, FSRollbackQuota); 97 | OS_FIND_EXPORT(coreinit_handle, FSRollbackQuotaAsync); 98 | 99 | OS_FIND_EXPORT(coreinit_handle, FSOpenDir); 100 | OS_FIND_EXPORT(coreinit_handle, FSOpenDirAsync); 101 | OS_FIND_EXPORT(coreinit_handle, FSReadDir); 102 | OS_FIND_EXPORT(coreinit_handle, FSRewindDir); 103 | OS_FIND_EXPORT(coreinit_handle, FSCloseDir); 104 | OS_FIND_EXPORT(coreinit_handle, FSChangeDir); 105 | OS_FIND_EXPORT(coreinit_handle, FSChangeDirAsync); 106 | OS_FIND_EXPORT(coreinit_handle, FSMakeDir); 107 | OS_FIND_EXPORT(coreinit_handle, FSMakeDirAsync); 108 | 109 | 110 | OS_FIND_EXPORT(coreinit_handle, FSOpenFile); 111 | OS_FIND_EXPORT(coreinit_handle, FSOpenFileAsync); 112 | OS_FIND_EXPORT(coreinit_handle, FSReadFile); 113 | OS_FIND_EXPORT(coreinit_handle, FSCloseFile); 114 | 115 | OS_FIND_EXPORT(coreinit_handle, FSFlushFile); 116 | OS_FIND_EXPORT(coreinit_handle, FSTruncateFile); 117 | OS_FIND_EXPORT(coreinit_handle, FSGetStatFile); 118 | OS_FIND_EXPORT(coreinit_handle, FSSetPosFile); 119 | OS_FIND_EXPORT(coreinit_handle, FSWriteFile); 120 | } 121 | -------------------------------------------------------------------------------- /src/dynamic_libs/os_functions.h: -------------------------------------------------------------------------------- 1 | /**************************************************************************** 2 | * Copyright (C) 2015 3 | * by Dimok 4 | * 5 | * This software is provided 'as-is', without any express or implied 6 | * warranty. In no event will the authors be held liable for any 7 | * damages arising from the use of this software. 8 | * 9 | * Permission is granted to anyone to use this software for any 10 | * purpose, including commercial applications, and to alter it and 11 | * redistribute it freely, subject to the following restrictions: 12 | * 13 | * 1. The origin of this software must not be misrepresented; you 14 | * must not claim that you wrote the original software. If you use 15 | * this software in a product, an acknowledgment in the product 16 | * documentation would be appreciated but is not required. 17 | * 18 | * 2. Altered source versions must be plainly marked as such, and 19 | * must not be misrepresented as being the original software. 20 | * 21 | * 3. This notice may not be removed or altered from any source 22 | * distribution. 23 | ***************************************************************************/ 24 | #ifndef __OS_FUNCTIONS_H_ 25 | #define __OS_FUNCTIONS_H_ 26 | 27 | #include 28 | #include "common/os_defs.h" 29 | 30 | #ifdef __cplusplus 31 | extern "C" { 32 | #endif 33 | 34 | #define BUS_SPEED 248625000 35 | #define SECS_TO_TICKS(sec) (((unsigned long long)(sec)) * (BUS_SPEED/4)) 36 | #define MILLISECS_TO_TICKS(msec) (SECS_TO_TICKS(msec) / 1000) 37 | #define MICROSECS_TO_TICKS(usec) (SECS_TO_TICKS(usec) / 1000000) 38 | 39 | #define usleep(usecs) OSSleepTicks(MICROSECS_TO_TICKS(usecs)) 40 | #define sleep(secs) OSSleepTicks(SECS_TO_TICKS(secs)) 41 | 42 | #define FLUSH_DATA_BLOCK(addr) asm volatile("dcbf 0, %0; sync" : : "r"(((addr) & ~31))) 43 | #define INVAL_DATA_BLOCK(addr) asm volatile("dcbi 0, %0; sync" : : "r"(((addr) & ~31))) 44 | 45 | #define EXPORT_DECL(res, func, ...) res (* func)(__VA_ARGS__) __attribute__((section(".data"))) = 0; 46 | #define EXPORT_VAR(type, var) type var __attribute__((section(".data"))); 47 | 48 | 49 | #define EXPORT_FUNC_WRITE(func, val) *(u32*)(((u32)&func) + 0) = (u32)val 50 | 51 | #define OS_FIND_EXPORT(handle, func) funcPointer = 0; \ 52 | OSDynLoad_FindExport(handle, 0, # func, &funcPointer); \ 53 | if(!funcPointer) \ 54 | OSFatal("Function " # func " is NULL"); \ 55 | EXPORT_FUNC_WRITE(func, funcPointer); 56 | 57 | #define OS_FIND_EXPORT_EX(handle, func, func_p) \ 58 | funcPointer = 0; \ 59 | OSDynLoad_FindExport(handle, 0, # func, &funcPointer); \ 60 | if(!funcPointer) \ 61 | OSFatal("Function " # func " is NULL"); \ 62 | EXPORT_FUNC_WRITE(func_p, funcPointer); 63 | 64 | #define OS_MUTEX_SIZE 44 65 | 66 | /* Handle for coreinit */ 67 | extern unsigned int coreinit_handle; 68 | void InitOSFunctionPointers(void); 69 | 70 | //!---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 71 | //! Lib handle functions 72 | //!---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 73 | extern int (* OSDynLoad_Acquire)(const char* rpl, u32 *handle); 74 | extern int (* OSDynLoad_FindExport)(u32 handle, int isdata, const char *symbol, void *address); 75 | 76 | //!---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 77 | //! Thread functions 78 | //!---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 79 | extern int (* OSCreateThread)(void *thread, s32 (*callback)(s32, void*), s32 argc, void *args, u32 stack, u32 stack_size, s32 priority, u32 attr); 80 | extern int (* OSResumeThread)(void *thread); 81 | extern int (* OSSuspendThread)(void *thread); 82 | extern int (* OSIsThreadTerminated)(void *thread); 83 | extern int (* OSIsThreadSuspended)(void *thread); 84 | extern int (* OSJoinThread)(void * thread, int * ret_val); 85 | extern int (* OSSetThreadPriority)(void * thread, int priority); 86 | extern void (* OSDetachThread)(void * thread); 87 | extern void (* OSSleepTicks)(u64 ticks); 88 | extern u64 (* OSGetTick)(void); 89 | 90 | //!---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 91 | //! Mutex functions 92 | //!---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 93 | extern void (* OSInitMutex)(void* mutex); 94 | extern void (* OSLockMutex)(void* mutex); 95 | extern void (* OSUnlockMutex)(void* mutex); 96 | extern int (* OSTryLockMutex)(void* mutex); 97 | 98 | //!---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 99 | //! System functions 100 | //!---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 101 | extern u64 (* OSGetTitleID)(void); 102 | extern void (* __Exit)(void); 103 | extern void (* OSFatal)(const char* msg); 104 | extern void (* DCFlushRange)(const void *addr, u32 length); 105 | extern void (* ICInvalidateRange)(const void *addr, u32 length); 106 | extern void* (* OSEffectiveToPhysical)(const void*); 107 | extern int (* __os_snprintf)(char* s, int n, const char * format, ...); 108 | 109 | extern void (*OSScreenInit)(void); 110 | extern unsigned int (*OSScreenGetBufferSizeEx)(unsigned int bufferNum); 111 | extern int (*OSScreenSetBufferEx)(unsigned int bufferNum, void * addr); 112 | extern int (*OSScreenClearBufferEx)(unsigned int bufferNum, unsigned int temp); 113 | extern int (*OSScreenFlipBuffersEx)(unsigned int bufferNum); 114 | extern int (*OSScreenPutFontEx)(unsigned int bufferNum, unsigned int posX, unsigned int posY, const char * buffer); 115 | extern int (*OSScreenEnableEx)(unsigned int bufferNum, int enable); 116 | 117 | typedef unsigned char (*exception_callback)(void * interruptedContext); 118 | extern void (* OSSetExceptionCallback)(u8 exceptionType, exception_callback newCallback); 119 | 120 | extern int (* LiWaitIopComplete)(int unknown_syscall_arg_r3, int * remaining_bytes); 121 | extern int (* LiWaitIopCompleteWithInterrupts)(int unknown_syscall_arg_r3, int * remaining_bytes); 122 | 123 | 124 | #ifdef __cplusplus 125 | } 126 | #endif 127 | 128 | #endif // __OS_FUNCTIONS_H_ 129 | -------------------------------------------------------------------------------- /src/net.c: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Copyright (C) 2008 Joseph Jordan 4 | 5 | This software is provided 'as-is', without any express or implied warranty. 6 | In no event will the authors be held liable for any damages arising from 7 | the use of this software. 8 | 9 | Permission is granted to anyone to use this software for any purpose, 10 | including commercial applications, and to alter it and redistribute it 11 | freely, subject to the following restrictions: 12 | 13 | 1.The origin of this software must not be misrepresented; you must not 14 | claim that you wrote the original software. If you use this software in a 15 | product, an acknowledgment in the product documentation would be 16 | appreciated but is not required. 17 | 18 | 2.Altered source versions must be plainly marked as such, and must not be 19 | misrepresented as being the original software. 20 | 21 | 3.This notice may not be removed or altered from any source distribution. 22 | 23 | */ 24 | #include 25 | #include 26 | #include 27 | #include 28 | #include 29 | #include 30 | 31 | #define MIN(x, y) ((x) < (y) ? (x) : (y)) 32 | 33 | #include "dynamic_libs/os_functions.h" 34 | #include "dynamic_libs/socket_functions.h" 35 | #include "net.h" 36 | 37 | #define MAX_NET_BUFFER_SIZE (60*1024) 38 | #define MIN_NET_BUFFER_SIZE 4096 39 | #define FREAD_BUFFER_SIZE (60*1024) 40 | 41 | extern u32 hostIpAddress; 42 | 43 | static u32 NET_BUFFER_SIZE = MAX_NET_BUFFER_SIZE; 44 | 45 | #if 0 46 | void initialise_network() { 47 | printf("Waiting for network to initialise...\n"); 48 | s32 result = -1; 49 | while (!check_reset_synchronous() && result < 0) { 50 | net_deinit(); 51 | while (!check_reset_synchronous() && (result = net_init()) == -EAGAIN); 52 | if (result < 0) printf("net_init() failed: [%i] %s, retrying...\n", result, strerror(-result)); 53 | } 54 | if (result >= 0) { 55 | u32 ip = 0; 56 | do { 57 | ip = net_gethostip(); 58 | if (!ip) printf("net_gethostip() failed, retrying...\n"); 59 | } while (!check_reset_synchronous() && !ip); 60 | if (ip) { 61 | struct in_addr addr; 62 | addr.s_addr = ip; 63 | printf("Network initialised. Wii IP address: %s\n", inet_ntoa(addr)); 64 | } 65 | } 66 | } 67 | #endif 68 | 69 | s32 network_socket(u32 domain,u32 type,u32 protocol) 70 | { 71 | int sock = socket(domain, type, protocol); 72 | if(sock < 0) 73 | { 74 | int err = -geterrno(); 75 | return (err < 0) ? err : sock; 76 | } 77 | return sock; 78 | } 79 | 80 | s32 network_bind(s32 s,struct sockaddr *name,s32 namelen) 81 | { 82 | int res = bind(s, name, namelen); 83 | if(res < 0) 84 | { 85 | int err = -geterrno(); 86 | return (err < 0) ? err : res; 87 | } 88 | return res; 89 | } 90 | 91 | s32 network_listen(s32 s,u32 backlog) 92 | { 93 | int res = listen(s, backlog); 94 | if(res < 0) 95 | { 96 | int err = -geterrno(); 97 | return (err < 0) ? err : res; 98 | } 99 | return res; 100 | } 101 | 102 | s32 network_accept(s32 s,struct sockaddr *addr,s32 *addrlen) 103 | { 104 | int res = accept(s, addr, addrlen); 105 | if(res < 0) 106 | { 107 | int err = -geterrno(); 108 | return (err < 0) ? err : res; 109 | } 110 | return res; 111 | } 112 | 113 | s32 network_connect(s32 s,struct sockaddr *addr, s32 addrlen) 114 | { 115 | int res = connect(s, addr, addrlen); 116 | if(res < 0) 117 | { 118 | int err = -geterrno(); 119 | return (err < 0) ? err : res; 120 | } 121 | return res; 122 | } 123 | 124 | s32 network_read(s32 s,void *mem,s32 len) 125 | { 126 | int res = recv(s, mem, len, 0); 127 | if(res < 0) 128 | { 129 | int err = -geterrno(); 130 | return (err < 0) ? err : res; 131 | } 132 | return res; 133 | } 134 | 135 | u32 network_gethostip() 136 | { 137 | return hostIpAddress; 138 | } 139 | 140 | s32 network_write(s32 s, const void *mem,s32 len) 141 | { 142 | s32 transfered = 0; 143 | 144 | while(len) 145 | { 146 | int ret = send(s, mem, len, 0); 147 | if(ret < 0) 148 | { 149 | int err = -geterrno(); 150 | transfered = (err < 0) ? err : ret; 151 | break; 152 | } 153 | 154 | mem += ret; 155 | transfered += ret; 156 | len -= ret; 157 | } 158 | return transfered; 159 | } 160 | 161 | s32 network_close(s32 s) 162 | { 163 | if(s < 0) 164 | return -1; 165 | 166 | return socketclose(s); 167 | } 168 | 169 | s32 set_blocking(s32 s, bool blocking) { 170 | s32 block = !blocking; 171 | setsockopt(s, SOL_SOCKET, SO_NONBLOCK, &block, sizeof(block)); 172 | return 0; 173 | } 174 | 175 | s32 network_close_blocking(s32 s) { 176 | set_blocking(s, true); 177 | return network_close(s); 178 | } 179 | 180 | s32 create_server(u16 port) { 181 | s32 server = network_socket(AF_INET, SOCK_STREAM, IPPROTO_IP); 182 | if (server < 0) 183 | return -1; 184 | 185 | 186 | set_blocking(server, false); 187 | u32 enable = 1; 188 | setsockopt(server, SOL_SOCKET, SO_REUSEADDR, &enable, sizeof(enable)); 189 | 190 | struct sockaddr_in bindAddress; 191 | memset(&bindAddress, 0, sizeof(bindAddress)); 192 | bindAddress.sin_family = AF_INET; 193 | bindAddress.sin_port = htons(port); 194 | bindAddress.sin_addr.s_addr = htonl(INADDR_ANY); 195 | 196 | s32 ret; 197 | if ((ret = network_bind(server, (struct sockaddr *)&bindAddress, sizeof(bindAddress))) < 0) { 198 | network_close(server); 199 | //gxprintf("Error binding socket: [%i] %s\n", -ret, strerror(-ret)); 200 | return ret; 201 | } 202 | if ((ret = network_listen(server, 3)) < 0) { 203 | network_close(server); 204 | //gxprintf("Error listening on socket: [%i] %s\n", -ret, strerror(-ret)); 205 | return ret; 206 | } 207 | 208 | return server; 209 | } 210 | 211 | typedef s32 (*transferrer_type)(s32 s, void *mem, s32 len); 212 | static s32 transfer_exact(s32 s, char *buf, s32 length, transferrer_type transferrer) { 213 | s32 result = 0; 214 | s32 remaining = length; 215 | s32 bytes_transferred; 216 | set_blocking(s, true); 217 | while (remaining) { 218 | try_again_with_smaller_buffer: 219 | bytes_transferred = transferrer(s, buf, MIN(remaining, (int) NET_BUFFER_SIZE)); 220 | if (bytes_transferred > 0) { 221 | remaining -= bytes_transferred; 222 | buf += bytes_transferred; 223 | } else if (bytes_transferred < 0) { 224 | if (bytes_transferred == -EINVAL && NET_BUFFER_SIZE == MAX_NET_BUFFER_SIZE) { 225 | NET_BUFFER_SIZE = MIN_NET_BUFFER_SIZE; 226 | usleep(1000); 227 | goto try_again_with_smaller_buffer; 228 | } 229 | result = bytes_transferred; 230 | break; 231 | } else { 232 | result = -ENODATA; 233 | break; 234 | } 235 | } 236 | set_blocking(s, false); 237 | return result; 238 | } 239 | 240 | s32 send_exact(s32 s, char *buf, s32 length) { 241 | return transfer_exact(s, buf, length, (transferrer_type)network_write); 242 | } 243 | 244 | s32 send_from_file(s32 s, FILE *f) { 245 | char * buf = (char *) malloc(FREAD_BUFFER_SIZE); 246 | if(!buf) 247 | return -1; 248 | 249 | s32 bytes_read; 250 | s32 result = 0; 251 | 252 | bytes_read = fread(buf, 1, FREAD_BUFFER_SIZE, f); 253 | if (bytes_read > 0) { 254 | result = send_exact(s, buf, bytes_read); 255 | if (result < 0) goto end; 256 | } 257 | if (bytes_read < FREAD_BUFFER_SIZE) { 258 | result = -!feof(f); 259 | goto end; 260 | } 261 | free(buf); 262 | return -EAGAIN; 263 | end: 264 | free(buf); 265 | return result; 266 | } 267 | 268 | s32 recv_to_file(s32 s, FILE *f) { 269 | char * buf = (char *) malloc(NET_BUFFER_SIZE); 270 | if(!buf) 271 | return -1; 272 | 273 | s32 bytes_read; 274 | while (1) { 275 | try_again_with_smaller_buffer: 276 | bytes_read = network_read(s, buf, NET_BUFFER_SIZE); 277 | if (bytes_read < 0) { 278 | if (bytes_read == -EINVAL && NET_BUFFER_SIZE == MAX_NET_BUFFER_SIZE) { 279 | NET_BUFFER_SIZE = MIN_NET_BUFFER_SIZE; 280 | usleep(1000); 281 | goto try_again_with_smaller_buffer; 282 | } 283 | free(buf); 284 | return bytes_read; 285 | } else if (bytes_read == 0) { 286 | free(buf); 287 | return 0; 288 | } 289 | 290 | s32 bytes_written = fwrite(buf, 1, bytes_read, f); 291 | if (bytes_written < bytes_read) 292 | { 293 | free(buf); 294 | return -1; 295 | } 296 | } 297 | return -1; 298 | } 299 | -------------------------------------------------------------------------------- /src/vrt.c: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Copyright (C) 2008 Joseph Jordan 4 | This work is derived from Daniel Ehlers' srg_vrt branch. 5 | 6 | This software is provided 'as-is', without any express or implied warranty. 7 | In no event will the authors be held liable for any damages arising from 8 | the use of this software. 9 | 10 | Permission is granted to anyone to use this software for any purpose, 11 | including commercial applications, and to alter it and redistribute it 12 | freely, subject to the following restrictions: 13 | 14 | 1.The origin of this software must not be misrepresented; you must not 15 | claim that you wrote the original software. If you use this software in a 16 | product, an acknowledgment in the product documentation would be 17 | appreciated but is not required. 18 | 19 | 2.Altered source versions must be plainly marked as such, and must not be 20 | misrepresented as being the original software. 21 | 22 | 3.This notice may not be removed or altered from any source distribution. 23 | 24 | */ 25 | #include 26 | #include 27 | #include 28 | #include 29 | #include 30 | #include 31 | #include 32 | 33 | #include "virtualpath.h" 34 | #include "vrt.h" 35 | 36 | static char *virtual_abspath(char *virtual_cwd, char *virtual_path) { 37 | char *path; 38 | if (virtual_path[0] == '/') { 39 | path = virtual_path; 40 | } else { 41 | size_t path_size = strlen(virtual_cwd) + strlen(virtual_path) + 1; 42 | if (path_size > MAXPATHLEN || !(path = malloc(path_size))) return NULL; 43 | strcpy(path, virtual_cwd); 44 | strcat(path, virtual_path); 45 | } 46 | 47 | char *normalised_path = malloc(strlen(path) + 1); 48 | if (!normalised_path) goto end; 49 | *normalised_path = '\0'; 50 | char *curr_dir = normalised_path; 51 | 52 | u32 state = 0; // 0:start, 1:slash, 2:dot, 3:dotdot 53 | char *token = path; 54 | while (1) { 55 | switch (state) { 56 | case 0: 57 | if (*token == '/') { 58 | state = 1; 59 | curr_dir = normalised_path + strlen(normalised_path); 60 | strncat(normalised_path, token, 1); 61 | } 62 | break; 63 | case 1: 64 | if (*token == '.') state = 2; 65 | else if (*token != '/') state = 0; 66 | break; 67 | case 2: 68 | if (*token == '/' || !*token) { 69 | state = 1; 70 | *(curr_dir + 1) = '\0'; 71 | } else if (*token == '.') state = 3; 72 | else state = 0; 73 | break; 74 | case 3: 75 | if (*token == '/' || !*token) { 76 | state = 1; 77 | *curr_dir = '\0'; 78 | char *prev_dir = strrchr(normalised_path, '/'); 79 | if (prev_dir) curr_dir = prev_dir; 80 | else *curr_dir = '/'; 81 | *(curr_dir + 1) = '\0'; 82 | } else state = 0; 83 | break; 84 | } 85 | if (!*token) break; 86 | if (state == 0 || *token != '/') strncat(normalised_path, token, 1); 87 | token++; 88 | } 89 | 90 | u32 end = strlen(normalised_path); 91 | while (end > 1 && normalised_path[end - 1] == '/') { 92 | normalised_path[--end] = '\x00'; 93 | } 94 | 95 | end: 96 | if (path != virtual_path) free(path); 97 | return normalised_path; 98 | } 99 | 100 | /* 101 | Converts a client-visible path to a real absolute path 102 | E.g. "/sd/foo" -> "sd:/foo" 103 | "/sd" -> "sd:/" 104 | "/sd/../usb" -> "usb:/" 105 | The resulting path will fit in an array of size MAXPATHLEN 106 | Returns NULL to indicate that the client-visible path is invalid 107 | */ 108 | char *to_real_path(char *virtual_cwd, char *virtual_path) { 109 | errno = ENOENT; 110 | if (strchr(virtual_path, ':')) { 111 | return NULL; // colon is not allowed in virtual path, i've decided =P 112 | } 113 | 114 | virtual_path = virtual_abspath(virtual_cwd, virtual_path); 115 | if (!virtual_path) return NULL; 116 | 117 | char *path = NULL; 118 | char *rest = virtual_path; 119 | 120 | if (!strcmp("/", virtual_path)) { 121 | // indicate vfs-root with "" 122 | path = ""; 123 | goto end; 124 | } 125 | 126 | const char *prefix = NULL; 127 | u32 i; 128 | for (i = 0; i < MAX_VIRTUAL_PARTITIONS; i++) { 129 | VIRTUAL_PARTITION *partition = VIRTUAL_PARTITIONS + i; 130 | const char *alias = partition->alias; 131 | size_t alias_len = strlen(alias); 132 | if (!strcasecmp(alias, virtual_path) || (!strncasecmp(alias, virtual_path, alias_len) && virtual_path[alias_len] == '/')) { 133 | prefix = partition->prefix; 134 | rest += alias_len; 135 | if (*rest == '/') rest++; 136 | break; 137 | } 138 | } 139 | if (!prefix) { 140 | errno = ENODEV; 141 | goto end; 142 | } 143 | 144 | size_t real_path_size = strlen(prefix) + strlen(rest) + 1; 145 | if (real_path_size > MAXPATHLEN) goto end; 146 | 147 | path = malloc(real_path_size); 148 | if (!path) goto end; 149 | strcpy(path, prefix); 150 | strcat(path, rest); 151 | 152 | end: 153 | free(virtual_path); 154 | return path; 155 | } 156 | 157 | typedef void * (*path_func)(char *path, ...); 158 | 159 | static void *with_virtual_path(void *virtual_cwd, void *void_f, char *virtual_path, s32 failed, ...) { 160 | char *path = to_real_path(virtual_cwd, virtual_path); 161 | if (!path || !*path) return (void *)failed; 162 | 163 | path_func f = (path_func)void_f; 164 | va_list ap; 165 | void *args[3]; 166 | unsigned int num_args = 0; 167 | va_start(ap, failed); 168 | do { 169 | void *arg = va_arg(ap, void *); 170 | if (!arg) break; 171 | args[num_args++] = arg; 172 | } while (1); 173 | va_end(ap); 174 | 175 | void *result; 176 | switch (num_args) { 177 | case 0: result = f(path); break; 178 | case 1: result = f(path, args[0]); break; 179 | case 2: result = f(path, args[0], args[1]); break; 180 | case 3: result = f(path, args[0], args[1], args[2]); break; 181 | default: result = (void *)failed; break; 182 | } 183 | 184 | free(path); 185 | return result; 186 | } 187 | 188 | FILE *vrt_fopen(char *cwd, char *path, char *mode) { 189 | return with_virtual_path(cwd, fopen, path, 0, mode, NULL); 190 | } 191 | 192 | int vrt_stat(char *cwd, char *path, struct stat *st) { 193 | char *real_path = to_real_path(cwd, path); 194 | if (!real_path) return -1; 195 | else if (!*real_path) { 196 | st->st_mode = S_IFDIR; 197 | st->st_size = 31337; 198 | return 0; 199 | } 200 | free(real_path); 201 | return (int)with_virtual_path(cwd, stat, path, -1, st, NULL); 202 | } 203 | 204 | int vrt_chdir(char *cwd, char *path) { 205 | 206 | struct stat st; 207 | if (vrt_stat(cwd, path, &st)) { 208 | return -1; 209 | } else if (!(st.st_mode & S_IFDIR)) { 210 | errno = ENOTDIR; 211 | return -1; 212 | } 213 | char *abspath = virtual_abspath(cwd, path); 214 | if (!abspath) { 215 | errno = ENOMEM; 216 | return -1; 217 | } 218 | strcpy(cwd, abspath); 219 | if (cwd[1]) strcat(cwd, "/"); 220 | free(abspath); 221 | return 0; 222 | } 223 | 224 | int vrt_unlink(char *cwd, char *path) { 225 | return (int)with_virtual_path(cwd, unlink, path, -1, NULL); 226 | } 227 | 228 | int vrt_mkdir(char *cwd, char *path, mode_t mode) { 229 | return (int)with_virtual_path(cwd, mkdir, path, -1, mode, NULL); 230 | } 231 | 232 | int vrt_rename(char *cwd, char *from_path, char *to_path) { 233 | char *real_to_path = to_real_path(cwd, to_path); 234 | if (!real_to_path || !*real_to_path) return -1; 235 | int result = (int)with_virtual_path(cwd, rename, from_path, -1, real_to_path, NULL); 236 | free(real_to_path); 237 | return result; 238 | } 239 | 240 | /* 241 | When in vfs-root this creates a fake DIR_ITER. 242 | */ 243 | DIR_P *vrt_opendir(char *cwd, char *path) 244 | { 245 | char *real_path = to_real_path(cwd, path); 246 | if (!real_path) return NULL; 247 | 248 | DIR_P *iter = malloc(sizeof(DIR_P)); 249 | if (!iter) 250 | { 251 | if (*real_path != 0) 252 | free(real_path); 253 | return NULL; 254 | } 255 | 256 | iter->virt_root = 0; 257 | iter->path = real_path; 258 | 259 | if (*iter->path == 0) { 260 | iter->dir = malloc(sizeof(DIR)); 261 | if(!iter->dir) { 262 | // root path is not allocated 263 | free(iter); 264 | return NULL; 265 | } 266 | memset(iter->dir, 0, sizeof(DIR)); 267 | iter->virt_root = 1; // we are at the virtual root 268 | return iter; 269 | } 270 | 271 | iter->dir = with_virtual_path(cwd, opendir, path, 0, NULL); 272 | if(!iter->dir) 273 | { 274 | free(iter->path); 275 | free(iter); 276 | return NULL; 277 | } 278 | 279 | return iter; 280 | } 281 | 282 | /* 283 | Yields virtual aliases when pDir->virt_root 284 | */ 285 | struct dirent *vrt_readdir(DIR_P *pDir) { 286 | if(!pDir || !pDir->dir) return NULL; 287 | 288 | DIR *iter = pDir->dir; 289 | if (pDir->virt_root) { 290 | for (; (u32)iter->position < MAX_VIRTUAL_PARTITIONS; iter->position++) { 291 | VIRTUAL_PARTITION *partition = VIRTUAL_PARTITIONS + (int)iter->position; 292 | if (partition->inserted) { 293 | iter->fileData.d_type = DT_DIR; 294 | strcpy(iter->fileData.d_name, partition->alias + 1); 295 | iter->position++; 296 | return &iter->fileData; 297 | } 298 | } 299 | return NULL; 300 | } 301 | return readdir(iter); 302 | } 303 | 304 | int vrt_closedir(DIR_P *iter) { 305 | if(!iter) return -1; 306 | 307 | if(iter->dir) 308 | { 309 | if (iter->virt_root) 310 | free(iter->dir); 311 | else 312 | closedir(iter->dir); 313 | } 314 | 315 | // root path is not allocated 316 | if(iter->path && *iter->path != 0) 317 | free(iter->path); 318 | 319 | free(iter); 320 | 321 | return 0; 322 | } 323 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | #--------------------------------------------------------------------------------- 2 | # Clear the implicit built in rules 3 | #--------------------------------------------------------------------------------- 4 | .SUFFIXES: 5 | #--------------------------------------------------------------------------------- 6 | ifeq ($(strip $(DEVKITPPC)),) 7 | $(error "Please set DEVKITPPC in your environment. export DEVKITPPC=devkitPPC") 8 | endif 9 | ifeq ($(strip $(DEVKITPRO)),) 10 | $(error "Please set DEVKITPRO in your environment. export DEVKITPRO=devkitPRO") 11 | endif 12 | export PATH := $(DEVKITPPC)/bin:$(PORTLIBS)/bin:$(PATH) 13 | export LIBOGC_INC := $(DEVKITPRO)/libogc/include 14 | export LIBOGC_LIB := $(DEVKITPRO)/libogc/lib/wii 15 | export PORTLIBS := $(DEVKITPRO)/portlibs/ppc 16 | 17 | PREFIX := powerpc-eabi- 18 | 19 | export AS := $(PREFIX)as 20 | export CC := $(PREFIX)gcc 21 | export CXX := $(PREFIX)g++ 22 | export AR := $(PREFIX)ar 23 | export OBJCOPY := $(PREFIX)objcopy 24 | 25 | #--------------------------------------------------------------------------------- 26 | # TARGET is the name of the output 27 | # BUILD is the directory where object files & intermediate files will be placed 28 | # SOURCES is a list of directories containing source code 29 | # INCLUDES is a list of directories containing extra header files 30 | #--------------------------------------------------------------------------------- 31 | TARGET := ftpiiu 32 | BUILD := build 33 | BUILD_DBG := $(TARGET)_dbg 34 | SOURCES := src \ 35 | src/dynamic_libs \ 36 | src/fs \ 37 | src/system \ 38 | src/utils 39 | DATA := 40 | 41 | INCLUDES := src 42 | 43 | #--------------------------------------------------------------------------------- 44 | # options for code generation 45 | #--------------------------------------------------------------------------------- 46 | CFLAGS := -std=gnu11 -mrvl -mcpu=750 -meabi -mhard-float -ffast-math \ 47 | -O3 -Wall -Wextra -Wno-unused-parameter -Wno-strict-aliasing $(INCLUDE) 48 | CXXFLAGS := -std=gnu++11 -mrvl -mcpu=750 -meabi -mhard-float -ffast-math \ 49 | -O3 -Wall -Wextra -Wno-unused-parameter -Wno-strict-aliasing $(INCLUDE) 50 | ASFLAGS := -mregnames 51 | LDFLAGS := -nostartfiles -Wl,-Map,$(notdir $@).map,-wrap,malloc,-wrap,free,-wrap,memalign,-wrap,calloc,-wrap,realloc,-wrap,malloc_usable_size,-wrap,_malloc_r,-wrap,_free_r,-wrap,_realloc_r,-wrap,_calloc_r,-wrap,_memalign_r,-wrap,_malloc_usable_size_r,-wrap,valloc,-wrap,_valloc_r,-wrap,_pvalloc_r,--gc-sections 52 | 53 | #--------------------------------------------------------------------------------- 54 | Q := @ 55 | MAKEFLAGS += --no-print-directory 56 | #--------------------------------------------------------------------------------- 57 | # any extra libraries we wish to link with the project 58 | #--------------------------------------------------------------------------------- 59 | LIBS := -lgcc -lgd -lpng -lz -lfreetype -lvorbisidec 60 | 61 | #--------------------------------------------------------------------------------- 62 | # list of directories containing libraries, this must be the top level containing 63 | # include and lib 64 | #--------------------------------------------------------------------------------- 65 | LIBDIRS := $(CURDIR) \ 66 | $(DEVKITPPC)/lib \ 67 | $(DEVKITPPC)/lib/gcc/powerpc-eabi/4.8.2 68 | 69 | 70 | #--------------------------------------------------------------------------------- 71 | # no real need to edit anything past this point unless you need to add additional 72 | # rules for different file extensions 73 | #--------------------------------------------------------------------------------- 74 | ifneq ($(BUILD),$(notdir $(CURDIR))) 75 | #--------------------------------------------------------------------------------- 76 | export PROJECTDIR := $(CURDIR) 77 | export OUTPUT := $(CURDIR)/$(TARGETDIR)/$(TARGET) 78 | export VPATH := $(foreach dir,$(SOURCES),$(CURDIR)/$(dir)) \ 79 | $(foreach dir,$(DATA),$(CURDIR)/$(dir)) 80 | export DEPSDIR := $(CURDIR)/$(BUILD) 81 | 82 | #--------------------------------------------------------------------------------- 83 | # automatically build a list of object files for our project 84 | #--------------------------------------------------------------------------------- 85 | CFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.c))) 86 | CPPFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.cpp))) 87 | sFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.s))) 88 | SFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.S))) 89 | BINFILES := $(foreach dir,$(DATA),$(notdir $(wildcard $(dir)/*.*))) 90 | TTFFILES := $(foreach dir,$(DATA),$(notdir $(wildcard $(dir)/*.ttf))) 91 | PNGFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.png))) 92 | 93 | #--------------------------------------------------------------------------------- 94 | # use CXX for linking C++ projects, CC for standard C 95 | #--------------------------------------------------------------------------------- 96 | ifeq ($(strip $(CPPFILES)),) 97 | export LD := $(CC) 98 | else 99 | export LD := $(CXX) 100 | endif 101 | 102 | export OFILES := $(CPPFILES:.cpp=.o) $(CFILES:.c=.o) \ 103 | $(sFILES:.s=.o) $(SFILES:.S=.o) \ 104 | $(PNGFILES:.png=.png.o) $(addsuffix .o,$(BINFILES)) 105 | 106 | #--------------------------------------------------------------------------------- 107 | # build a list of include paths 108 | #--------------------------------------------------------------------------------- 109 | export INCLUDE := $(foreach dir,$(INCLUDES),-I$(CURDIR)/$(dir)) \ 110 | $(foreach dir,$(LIBDIRS),-I$(dir)/include) \ 111 | -I$(CURDIR)/$(BUILD) -I$(LIBOGC_INC) \ 112 | -I$(PORTLIBS)/include -I$(PORTLIBS)/include/freetype2 113 | 114 | #--------------------------------------------------------------------------------- 115 | # build a list of library paths 116 | #--------------------------------------------------------------------------------- 117 | export LIBPATHS := $(foreach dir,$(LIBDIRS),-L$(dir)/lib) \ 118 | -L$(LIBOGC_LIB) -L$(PORTLIBS)/lib 119 | 120 | export OUTPUT := $(CURDIR)/$(TARGET) 121 | .PHONY: $(BUILD) clean install 122 | 123 | #--------------------------------------------------------------------------------- 124 | $(BUILD): 125 | @[ -d $@ ] || mkdir -p $@ 126 | @$(MAKE) --no-print-directory -C $(BUILD) -f $(CURDIR)/Makefile 127 | 128 | #--------------------------------------------------------------------------------- 129 | clean: 130 | @echo clean ... 131 | @rm -fr $(BUILD) $(OUTPUT).elf $(OUTPUT).bin $(BUILD_DBG).elf 132 | 133 | #--------------------------------------------------------------------------------- 134 | else 135 | 136 | DEPENDS := $(OFILES:.o=.d) 137 | 138 | #--------------------------------------------------------------------------------- 139 | # main targets 140 | #--------------------------------------------------------------------------------- 141 | $(OUTPUT).elf: $(OFILES) 142 | 143 | #--------------------------------------------------------------------------------- 144 | # This rule links in binary data with the .jpg extension 145 | #--------------------------------------------------------------------------------- 146 | %.elf: link.ld $(OFILES) 147 | @echo "linking ... $(TARGET).elf" 148 | $(Q)$(LD) -n -T $^ $(LDFLAGS) -o ../$(BUILD_DBG).elf $(LIBPATHS) $(LIBS) 149 | $(Q)$(OBJCOPY) -S -R .comment -R .gnu.attributes ../$(BUILD_DBG).elf $@ 150 | 151 | ../data/loader.bin: 152 | $(MAKE) -C ../loader clean 153 | $(MAKE) -C ../loader 154 | #--------------------------------------------------------------------------------- 155 | %.a: 156 | #--------------------------------------------------------------------------------- 157 | @echo $(notdir $@) 158 | @rm -f $@ 159 | @$(AR) -rc $@ $^ 160 | 161 | #--------------------------------------------------------------------------------- 162 | %.o: %.cpp 163 | @echo $(notdir $<) 164 | @$(CXX) -MMD -MP -MF $(DEPSDIR)/$*.d $(CXXFLAGS) -c $< -o $@ $(ERROR_FILTER) 165 | 166 | #--------------------------------------------------------------------------------- 167 | %.o: %.c 168 | @echo $(notdir $<) 169 | @$(CC) -MMD -MP -MF $(DEPSDIR)/$*.d $(CFLAGS) -c $< -o $@ $(ERROR_FILTER) 170 | 171 | #--------------------------------------------------------------------------------- 172 | %.o: %.S 173 | @echo $(notdir $<) 174 | @$(CC) -MMD -MP -MF $(DEPSDIR)/$*.d -x assembler-with-cpp $(ASFLAGS) -c $< -o $@ $(ERROR_FILTER) 175 | 176 | #--------------------------------------------------------------------------------- 177 | %.png.o : %.png 178 | @echo $(notdir $<) 179 | @bin2s -a 32 $< | $(AS) -o $(@) 180 | 181 | #--------------------------------------------------------------------------------- 182 | %.jpg.o : %.jpg 183 | @echo $(notdir $<) 184 | @bin2s -a 32 $< | $(AS) -o $(@) 185 | 186 | #--------------------------------------------------------------------------------- 187 | %.ttf.o : %.ttf 188 | @echo $(notdir $<) 189 | @bin2s -a 32 $< | $(AS) -o $(@) 190 | 191 | #--------------------------------------------------------------------------------- 192 | %.bin.o : %.bin 193 | @echo $(notdir $<) 194 | @bin2s -a 32 $< | $(AS) -o $(@) 195 | 196 | #--------------------------------------------------------------------------------- 197 | %.wav.o : %.wav 198 | @echo $(notdir $<) 199 | @bin2s -a 32 $< | $(AS) -o $(@) 200 | 201 | #--------------------------------------------------------------------------------- 202 | %.mp3.o : %.mp3 203 | @echo $(notdir $<) 204 | @bin2s -a 32 $< | $(AS) -o $(@) 205 | 206 | #--------------------------------------------------------------------------------- 207 | %.ogg.o : %.ogg 208 | @echo $(notdir $<) 209 | @bin2s -a 32 $< | $(AS) -o $(@) 210 | 211 | -include $(DEPENDS) 212 | 213 | #--------------------------------------------------------------------------------- 214 | endif 215 | #--------------------------------------------------------------------------------- 216 | -------------------------------------------------------------------------------- /src/dynamic_libs/gx2_functions.c: -------------------------------------------------------------------------------- 1 | /**************************************************************************** 2 | * Copyright (C) 2015 3 | * by Dimok 4 | * 5 | * This software is provided 'as-is', without any express or implied 6 | * warranty. In no event will the authors be held liable for any 7 | * damages arising from the use of this software. 8 | * 9 | * Permission is granted to anyone to use this software for any 10 | * purpose, including commercial applications, and to alter it and 11 | * redistribute it freely, subject to the following restrictions: 12 | * 13 | * 1. The origin of this software must not be misrepresented; you 14 | * must not claim that you wrote the original software. If you use 15 | * this software in a product, an acknowledgment in the product 16 | * documentation would be appreciated but is not required. 17 | * 18 | * 2. Altered source versions must be plainly marked as such, and 19 | * must not be misrepresented as being the original software. 20 | * 21 | * 3. This notice may not be removed or altered from any source 22 | * distribution. 23 | ***************************************************************************/ 24 | #include "os_functions.h" 25 | #include "gx2_types.h" 26 | #include "utils/utils.h" 27 | 28 | EXPORT_DECL(void, GX2Init, u32 * init_attribs); 29 | EXPORT_DECL(void, GX2Shutdown, void); 30 | EXPORT_DECL(void, GX2Flush, void); 31 | EXPORT_DECL(s32, GX2GetMainCoreId, void) ; 32 | EXPORT_DECL(s32, GX2DrawDone, void); 33 | EXPORT_DECL(void, GX2ClearColor, GX2ColorBuffer *colorBuffer, f32 r, f32 g, f32 b, f32 a); 34 | EXPORT_DECL(void, GX2SetViewport, f32 x, f32 y, f32 w, f32 h, f32 nearZ, f32 farZ); 35 | EXPORT_DECL(void, GX2SetScissor, u32 x_orig, u32 y_orig, u32 wd, u32 ht); 36 | EXPORT_DECL(void, GX2SetContextState, const GX2ContextState* state); 37 | EXPORT_DECL(void, GX2DrawEx, s32 primitive_type, u32 count, u32 first_vertex, u32 instances_count); 38 | EXPORT_DECL(void, GX2DrawIndexedEx, s32 primitive_type, u32 count, s32 index_format, const void* idx, u32 first_vertex, u32 instances_count); 39 | EXPORT_DECL(void, GX2ClearDepthStencilEx, GX2DepthBuffer *depthBuffer, f32 depth_value, u8 stencil_value, s32 clear_mode); 40 | EXPORT_DECL(void, GX2CopyColorBufferToScanBuffer, const GX2ColorBuffer *colorBuffer, s32 scan_target); 41 | EXPORT_DECL(void, GX2SwapScanBuffers, void); 42 | EXPORT_DECL(void, GX2SetTVEnable, s32 enable); 43 | EXPORT_DECL(void, GX2SetSwapInterval, u32 swap_interval); 44 | EXPORT_DECL(u32, GX2GetSwapInterval, void); 45 | EXPORT_DECL(void, GX2WaitForVsync, void); 46 | EXPORT_DECL(void, GX2CalcTVSize, s32 tv_render_mode, s32 format, s32 buffering_mode, u32 * size, s32 * scale_needed); 47 | EXPORT_DECL(void, GX2Invalidate, s32 invalidate_type, void * ptr, u32 buffer_size); 48 | EXPORT_DECL(void, GX2SetTVBuffer, void *buffer, u32 buffer_size, s32 tv_render_mode, s32 format, s32 buffering_mode); 49 | EXPORT_DECL(void, GX2CalcSurfaceSizeAndAlignment, GX2Surface *surface); 50 | EXPORT_DECL(void, GX2InitDepthBufferRegs, GX2DepthBuffer *depthBuffer); 51 | EXPORT_DECL(void, GX2InitColorBufferRegs, GX2ColorBuffer *colorBuffer); 52 | EXPORT_DECL(void, GX2CalcColorBufferAuxInfo, GX2ColorBuffer *colorBuffer, u32 *size, u32 *align); 53 | EXPORT_DECL(void, GX2CalcDepthBufferHiZInfo, GX2DepthBuffer *depthBuffer, u32 *size, u32 *align); 54 | EXPORT_DECL(void, GX2InitDepthBufferHiZEnable, GX2DepthBuffer *depthBuffer, s32 hiZ_enable); 55 | EXPORT_DECL(void, GX2SetupContextStateEx, GX2ContextState* state, s32 enable_profiling); 56 | EXPORT_DECL(void, GX2SetColorBuffer, const GX2ColorBuffer *colorBuffer, s32 target); 57 | EXPORT_DECL(void, GX2SetDepthBuffer, const GX2DepthBuffer *depthBuffer); 58 | EXPORT_DECL(void, GX2SetAttribBuffer, u32 attr_index, u32 attr_size, u32 stride, const void* attr); 59 | EXPORT_DECL(void, GX2InitTextureRegs, GX2Texture *texture); 60 | EXPORT_DECL(void, GX2InitSampler, GX2Sampler *sampler, s32 tex_clamp, s32 min_mag_filter); 61 | EXPORT_DECL(u32, GX2CalcFetchShaderSizeEx, u32 num_attrib, s32 fetch_shader_type, s32 tessellation_mode); 62 | EXPORT_DECL(void, GX2InitFetchShaderEx, GX2FetchShader* fs, void* fs_buffer, u32 count, const GX2AttribStream* attribs, s32 fetch_shader_type, s32 tessellation_mode); 63 | EXPORT_DECL(void, GX2SetFetchShader, const GX2FetchShader* fs); 64 | EXPORT_DECL(void, GX2SetVertexUniformReg, u32 offset, u32 count, const void *values); 65 | EXPORT_DECL(void, GX2SetPixelUniformReg, u32 offset, u32 count, const void *values); 66 | EXPORT_DECL(void, GX2SetPixelTexture, const GX2Texture *texture, u32 texture_hw_location); 67 | EXPORT_DECL(void, GX2SetVertexTexture, const GX2Texture *texture, u32 texture_hw_location); 68 | EXPORT_DECL(void, GX2SetPixelSampler, const GX2Sampler *sampler, u32 sampler_hw_location); 69 | EXPORT_DECL(void, GX2SetVertexSampler, const GX2Sampler *sampler, u32 sampler_hw_location); 70 | EXPORT_DECL(void, GX2SetPixelShader, const GX2PixelShader* pixelShader); 71 | EXPORT_DECL(void, GX2SetVertexShader, const GX2VertexShader* vertexShader); 72 | EXPORT_DECL(void, GX2InitSamplerZMFilter, GX2Sampler *sampler, s32 z_filter, s32 mip_filter); 73 | EXPORT_DECL(void, GX2SetColorControl, s32 lop, u8 blend_enable_mask, s32 enable_multi_write, s32 enable_color_buffer); 74 | EXPORT_DECL(void, GX2SetDepthOnlyControl, s32 enable_depth, s32 enable_depth_write, s32 depth_comp_function); 75 | EXPORT_DECL(void, GX2SetBlendControl, s32 target, s32 color_src_blend, s32 color_dst_blend, s32 color_combine, s32 separate_alpha_blend, s32 alpha_src_blend, s32 alpha_dst_blend, s32 alpha_combine); 76 | EXPORT_DECL(void, GX2CalcDRCSize, s32 drc_mode, s32 format, s32 buffering_mode, u32 *size, s32 *scale_needed); 77 | EXPORT_DECL(void, GX2SetDRCBuffer, void *buffer, u32 buffer_size, s32 drc_mode, s32 surface_format, s32 buffering_mode); 78 | EXPORT_DECL(void, GX2SetDRCScale, u32 width, u32 height); 79 | EXPORT_DECL(void, GX2SetDRCEnable, s32 enable); 80 | EXPORT_DECL(void, GX2SetPolygonControl, s32 front_face_mode, s32 cull_front, s32 cull_back, s32 enable_mode, s32 mode_font, s32 mode_back, s32 poly_offset_front, s32 poly_offset_back, s32 point_line_offset); 81 | EXPORT_DECL(void, GX2SetCullOnlyControl, s32 front_face_mode, s32 cull_front, s32 cull_back); 82 | EXPORT_DECL(void, GX2SetDepthStencilControl, s32 enable_depth_test, s32 enable_depth_write, s32 depth_comp_function, s32 stencil_test_enable, s32 back_stencil_enable, 83 | s32 font_stencil_func, s32 front_stencil_z_pass, s32 front_stencil_z_fail, s32 front_stencil_fail, 84 | s32 back_stencil_func, s32 back_stencil_z_pass, s32 back_stencil_z_fail, s32 back_stencil_fail); 85 | EXPORT_DECL(void, GX2SetStencilMask, u8 mask_front, u8 write_mask_front, u8 ref_front, u8 mask_back, u8 write_mask_back, u8 ref_back); 86 | EXPORT_DECL(void, GX2SetLineWidth, f32 width); 87 | EXPORT_DECL(void, GX2SetTVGamma, f32 val); 88 | EXPORT_DECL(void, GX2SetDRCGamma, f32 gam); 89 | EXPORT_DECL(s32, GX2GetSystemTVScanMode, void); 90 | EXPORT_DECL(s32, GX2GetSystemDRCScanMode, void); 91 | EXPORT_DECL(void, GX2RSetAllocator, void * (* allocFunc)(u32, u32, u32), void (* freeFunc)(u32, void*)); 92 | 93 | 94 | void InitGX2FunctionPointers(void) 95 | { 96 | unsigned int *funcPointer = 0; 97 | unsigned int gx2_handle; 98 | OSDynLoad_Acquire("gx2.rpl", &gx2_handle); 99 | 100 | OS_FIND_EXPORT(gx2_handle, GX2Init); 101 | OS_FIND_EXPORT(gx2_handle, GX2Shutdown); 102 | OS_FIND_EXPORT(gx2_handle, GX2Flush); 103 | OS_FIND_EXPORT(gx2_handle, GX2GetMainCoreId); 104 | OS_FIND_EXPORT(gx2_handle, GX2DrawDone); 105 | OS_FIND_EXPORT(gx2_handle, GX2ClearColor); 106 | OS_FIND_EXPORT(gx2_handle, GX2SetViewport); 107 | OS_FIND_EXPORT(gx2_handle, GX2SetScissor); 108 | OS_FIND_EXPORT(gx2_handle, GX2SetContextState); 109 | OS_FIND_EXPORT(gx2_handle, GX2DrawEx); 110 | OS_FIND_EXPORT(gx2_handle, GX2DrawIndexedEx); 111 | OS_FIND_EXPORT(gx2_handle, GX2ClearDepthStencilEx); 112 | OS_FIND_EXPORT(gx2_handle, GX2CopyColorBufferToScanBuffer); 113 | OS_FIND_EXPORT(gx2_handle, GX2SwapScanBuffers); 114 | OS_FIND_EXPORT(gx2_handle, GX2SetTVEnable); 115 | OS_FIND_EXPORT(gx2_handle, GX2SetSwapInterval); 116 | OS_FIND_EXPORT(gx2_handle, GX2GetSwapInterval); 117 | OS_FIND_EXPORT(gx2_handle, GX2WaitForVsync); 118 | OS_FIND_EXPORT(gx2_handle, GX2CalcTVSize); 119 | OS_FIND_EXPORT(gx2_handle, GX2Invalidate); 120 | OS_FIND_EXPORT(gx2_handle, GX2SetTVBuffer); 121 | OS_FIND_EXPORT(gx2_handle, GX2CalcSurfaceSizeAndAlignment); 122 | OS_FIND_EXPORT(gx2_handle, GX2InitDepthBufferRegs); 123 | OS_FIND_EXPORT(gx2_handle, GX2InitColorBufferRegs); 124 | OS_FIND_EXPORT(gx2_handle, GX2CalcColorBufferAuxInfo); 125 | OS_FIND_EXPORT(gx2_handle, GX2CalcDepthBufferHiZInfo); 126 | OS_FIND_EXPORT(gx2_handle, GX2InitDepthBufferHiZEnable); 127 | OS_FIND_EXPORT(gx2_handle, GX2SetupContextStateEx); 128 | OS_FIND_EXPORT(gx2_handle, GX2SetColorBuffer); 129 | OS_FIND_EXPORT(gx2_handle, GX2SetDepthBuffer); 130 | OS_FIND_EXPORT(gx2_handle, GX2SetAttribBuffer); 131 | OS_FIND_EXPORT(gx2_handle, GX2InitTextureRegs); 132 | OS_FIND_EXPORT(gx2_handle, GX2InitSampler); 133 | OS_FIND_EXPORT(gx2_handle, GX2CalcFetchShaderSizeEx); 134 | OS_FIND_EXPORT(gx2_handle, GX2InitFetchShaderEx); 135 | OS_FIND_EXPORT(gx2_handle, GX2SetFetchShader); 136 | OS_FIND_EXPORT(gx2_handle, GX2SetVertexUniformReg); 137 | OS_FIND_EXPORT(gx2_handle, GX2SetPixelUniformReg); 138 | OS_FIND_EXPORT(gx2_handle, GX2SetPixelTexture); 139 | OS_FIND_EXPORT(gx2_handle, GX2SetVertexTexture); 140 | OS_FIND_EXPORT(gx2_handle, GX2SetPixelSampler); 141 | OS_FIND_EXPORT(gx2_handle, GX2SetVertexSampler); 142 | OS_FIND_EXPORT(gx2_handle, GX2SetPixelShader); 143 | OS_FIND_EXPORT(gx2_handle, GX2SetVertexShader); 144 | OS_FIND_EXPORT(gx2_handle, GX2InitSamplerZMFilter); 145 | OS_FIND_EXPORT(gx2_handle, GX2SetColorControl); 146 | OS_FIND_EXPORT(gx2_handle, GX2SetDepthOnlyControl); 147 | OS_FIND_EXPORT(gx2_handle, GX2SetBlendControl); 148 | OS_FIND_EXPORT(gx2_handle, GX2CalcDRCSize); 149 | OS_FIND_EXPORT(gx2_handle, GX2SetDRCBuffer); 150 | OS_FIND_EXPORT(gx2_handle, GX2SetDRCScale); 151 | OS_FIND_EXPORT(gx2_handle, GX2SetDRCEnable); 152 | OS_FIND_EXPORT(gx2_handle, GX2SetPolygonControl); 153 | OS_FIND_EXPORT(gx2_handle, GX2SetCullOnlyControl); 154 | OS_FIND_EXPORT(gx2_handle, GX2SetDepthStencilControl); 155 | OS_FIND_EXPORT(gx2_handle, GX2SetStencilMask); 156 | OS_FIND_EXPORT(gx2_handle, GX2SetLineWidth); 157 | OS_FIND_EXPORT(gx2_handle, GX2SetDRCGamma); 158 | OS_FIND_EXPORT(gx2_handle, GX2SetTVGamma); 159 | OS_FIND_EXPORT(gx2_handle, GX2GetSystemTVScanMode); 160 | OS_FIND_EXPORT(gx2_handle, GX2GetSystemDRCScanMode); 161 | OS_FIND_EXPORT(gx2_handle, GX2RSetAllocator); 162 | } 163 | -------------------------------------------------------------------------------- /src/dynamic_libs/gx2_functions.h: -------------------------------------------------------------------------------- 1 | /**************************************************************************** 2 | * Copyright (C) 2015 3 | * by Dimok 4 | * 5 | * This software is provided 'as-is', without any express or implied 6 | * warranty. In no event will the authors be held liable for any 7 | * damages arising from the use of this software. 8 | * 9 | * Permission is granted to anyone to use this software for any 10 | * purpose, including commercial applications, and to alter it and 11 | * redistribute it freely, subject to the following restrictions: 12 | * 13 | * 1. The origin of this software must not be misrepresented; you 14 | * must not claim that you wrote the original software. If you use 15 | * this software in a product, an acknowledgment in the product 16 | * documentation would be appreciated but is not required. 17 | * 18 | * 2. Altered source versions must be plainly marked as such, and 19 | * must not be misrepresented as being the original software. 20 | * 21 | * 3. This notice may not be removed or altered from any source 22 | * distribution. 23 | ***************************************************************************/ 24 | #ifndef __GX2_FUNCTIONS_H_ 25 | #define __GX2_FUNCTIONS_H_ 26 | 27 | #ifdef __cplusplus 28 | extern "C" { 29 | #endif 30 | 31 | #include "gx2_types.h" 32 | 33 | void InitGX2FunctionPointers(void); 34 | 35 | extern void (* GX2Init)(u32 * init_attribs); 36 | extern void (* GX2Shutdown)(void); 37 | extern void (* GX2Flush)(void); 38 | extern s32 (* GX2GetMainCoreId)(void) ; 39 | extern s32 (* GX2DrawDone)(void); 40 | extern void (* GX2ClearColor)(GX2ColorBuffer *colorBuffer, f32 r, f32 g, f32 b, f32 a); 41 | extern void (* GX2SetViewport)(f32 x, f32 y, f32 w, f32 h, f32 nearZ, f32 farZ); 42 | extern void (* GX2SetScissor)(u32 x_orig, u32 y_orig, u32 wd, u32 ht); 43 | extern void (* GX2SetContextState)(const GX2ContextState* state); 44 | extern void (* GX2DrawEx)(s32 primitive_type, u32 count, u32 first_vertex, u32 instances_count); 45 | extern void (* GX2DrawIndexedEx)(s32 primitive_type, u32 count, s32 index_format, const void* idx, u32 first_vertex, u32 instances_count); 46 | extern void (* GX2ClearDepthStencilEx)(GX2DepthBuffer *depthBuffer, f32 depth_value, u8 stencil_value, s32 clear_mode); 47 | extern void (* GX2CopyColorBufferToScanBuffer)(const GX2ColorBuffer *colorBuffer, s32 scan_target); 48 | extern void (* GX2SwapScanBuffers)(void); 49 | extern void (* GX2SetTVEnable)(s32 enable); 50 | extern void (* GX2SetSwapInterval)(u32 swap_interval); 51 | extern u32 (* GX2GetSwapInterval)(void); 52 | extern void (* GX2WaitForVsync)(void); 53 | extern void (* GX2CalcTVSize)(s32 tv_render_mode, s32 format, s32 buffering_mode, u32 * size, s32 * scale_needed); 54 | extern void (* GX2Invalidate)(s32 invalidate_type, void * ptr, u32 buffer_size); 55 | extern void (* GX2SetTVBuffer)(void *buffer, u32 buffer_size, s32 tv_render_mode, s32 format, s32 buffering_mode); 56 | extern void (* GX2CalcSurfaceSizeAndAlignment)(GX2Surface *surface); 57 | extern void (* GX2InitDepthBufferRegs)(GX2DepthBuffer *depthBuffer); 58 | extern void (* GX2InitColorBufferRegs)(GX2ColorBuffer *colorBuffer); 59 | extern void (* GX2CalcColorBufferAuxInfo)(GX2ColorBuffer *colorBuffer, u32 *size, u32 *align); 60 | extern void (* GX2CalcDepthBufferHiZInfo)(GX2DepthBuffer *depthBuffer, u32 *size, u32 *align); 61 | extern void (* GX2InitDepthBufferHiZEnable)(GX2DepthBuffer *depthBuffer, s32 hiZ_enable); 62 | extern void (* GX2SetupContextStateEx)(GX2ContextState* state, s32 enable_profiling); 63 | extern void (* GX2SetColorBuffer)(const GX2ColorBuffer *colorBuffer, s32 target); 64 | extern void (* GX2SetDepthBuffer)(const GX2DepthBuffer *depthBuffer); 65 | extern void (* GX2SetAttribBuffer)(u32 attr_index, u32 attr_size, u32 stride, const void* attr); 66 | extern void (* GX2InitTextureRegs)(GX2Texture *texture); 67 | extern void (* GX2InitSampler)(GX2Sampler *sampler, s32 tex_clamp, s32 min_mag_filter); 68 | extern u32 (* GX2CalcFetchShaderSizeEx)(u32 num_attrib, s32 fetch_shader_type, s32 tessellation_mode); 69 | extern void (* GX2InitFetchShaderEx)(GX2FetchShader* fs, void* fs_buffer, u32 count, const GX2AttribStream* attribs, s32 fetch_shader_type, s32 tessellation_mode); 70 | extern void (* GX2SetFetchShader)(const GX2FetchShader* fs); 71 | extern void (* GX2SetVertexUniformReg)(u32 offset, u32 count, const void *values); 72 | extern void (* GX2SetPixelUniformReg)(u32 offset, u32 count, const void *values); 73 | extern void (* GX2SetPixelTexture)(const GX2Texture *texture, u32 texture_hw_location); 74 | extern void (* GX2SetVertexTexture)(const GX2Texture *texture, u32 texture_hw_location); 75 | extern void (* GX2SetPixelSampler)(const GX2Sampler *sampler, u32 sampler_hw_location); 76 | extern void (* GX2SetVertexSampler)(const GX2Sampler *sampler, u32 sampler_hw_location); 77 | extern void (* GX2SetPixelShader)(const GX2PixelShader* pixelShader); 78 | extern void (* GX2SetVertexShader)(const GX2VertexShader* vertexShader); 79 | extern void (* GX2InitSamplerZMFilter)(GX2Sampler *sampler, s32 z_filter, s32 mip_filter); 80 | extern void (* GX2SetColorControl)(s32 lop, u8 blend_enable_mask, s32 enable_multi_write, s32 enable_color_buffer); 81 | extern void (* GX2SetDepthOnlyControl)(s32 enable_depth, s32 enable_depth_write, s32 depth_comp_function); 82 | extern void (* GX2SetBlendControl)(s32 target, s32 color_src_blend, s32 color_dst_blend, s32 color_combine, s32 separate_alpha_blend, s32 alpha_src_blend, s32 alpha_dst_blend, s32 alpha_combine); 83 | extern void (* GX2CalcDRCSize)(s32 drc_mode, s32 format, s32 buffering_mode, u32 *size, s32 *scale_needed); 84 | extern void (* GX2SetDRCBuffer)(void *buffer, u32 buffer_size, s32 drc_mode, s32 surface_format, s32 buffering_mode); 85 | extern void (* GX2SetDRCScale)(u32 width, u32 height); 86 | extern void (* GX2SetDRCEnable)(s32 enable); 87 | extern void (* GX2SetPolygonControl)(s32 front_face_mode, s32 cull_front, s32 cull_back, s32 enable_mode, s32 mode_font, s32 mode_back, s32 poly_offset_front, s32 poly_offset_back, s32 point_line_offset); 88 | extern void (* GX2SetCullOnlyControl)(s32 front_face_mode, s32 cull_front, s32 cull_back); 89 | extern void (* GX2SetDepthStencilControl)(s32 enable_depth_test, s32 enable_depth_write, s32 depth_comp_function, s32 stencil_test_enable, s32 back_stencil_enable, 90 | s32 font_stencil_func, s32 front_stencil_z_pass, s32 front_stencil_z_fail, s32 front_stencil_fail, 91 | s32 back_stencil_func, s32 back_stencil_z_pass, s32 back_stencil_z_fail, s32 back_stencil_fail); 92 | extern void (* GX2SetStencilMask)(u8 mask_front, u8 write_mask_front, u8 ref_front, u8 mask_back, u8 write_mask_back, u8 ref_back); 93 | extern void (* GX2SetLineWidth)(f32 width); 94 | extern void (* GX2SetTVGamma)(f32 val); 95 | extern void (* GX2SetDRCGamma)(f32 val); 96 | extern s32 (* GX2GetSystemTVScanMode)(void); 97 | extern s32 (* GX2GetSystemDRCScanMode)(void); 98 | extern void (* GX2RSetAllocator)(void * (*allocFunc)(u32, u32, u32), void (*freeFunc)(u32, void*)); 99 | 100 | static inline void GX2InitDepthBuffer(GX2DepthBuffer *depthBuffer, s32 dimension, u32 width, u32 height, u32 depth, s32 format, s32 aa) 101 | { 102 | depthBuffer->surface.dimension = dimension; 103 | depthBuffer->surface.width = width; 104 | depthBuffer->surface.height = height; 105 | depthBuffer->surface.depth = depth; 106 | depthBuffer->surface.num_mips = 1; 107 | depthBuffer->surface.format = format; 108 | depthBuffer->surface.aa = aa; 109 | depthBuffer->surface.use = ((format==GX2_SURFACE_FORMAT_D_D24_S8_UNORM) || (format==GX2_SURFACE_FORMAT_D_D24_S8_FLOAT)) ? GX2_SURFACE_USE_DEPTH_BUFFER : GX2_SURFACE_USE_DEPTH_BUFFER_TEXTURE; 110 | depthBuffer->surface.tile = GX2_TILE_MODE_DEFAULT; 111 | depthBuffer->surface.swizzle = 0; 112 | depthBuffer->view_mip = 0; 113 | depthBuffer->view_first_slice = 0; 114 | depthBuffer->view_slices_count = depth; 115 | depthBuffer->clear_depth = 1.0f; 116 | depthBuffer->clear_stencil = 0; 117 | depthBuffer->hiZ_data = NULL; 118 | depthBuffer->hiZ_size = 0; 119 | GX2CalcSurfaceSizeAndAlignment(&depthBuffer->surface); 120 | GX2InitDepthBufferRegs(depthBuffer); 121 | } 122 | 123 | static inline void GX2InitColorBuffer(GX2ColorBuffer *colorBuffer, s32 dimension, u32 width, u32 height, u32 depth, s32 format, s32 aa) 124 | { 125 | colorBuffer->surface.dimension = dimension; 126 | colorBuffer->surface.width = width; 127 | colorBuffer->surface.height = height; 128 | colorBuffer->surface.depth = depth; 129 | colorBuffer->surface.num_mips = 1; 130 | colorBuffer->surface.format = format; 131 | colorBuffer->surface.aa = aa; 132 | colorBuffer->surface.use = GX2_SURFACE_USE_COLOR_BUFFER_TEXTURE_FTV; 133 | colorBuffer->surface.image_size = 0; 134 | colorBuffer->surface.image_data = NULL; 135 | colorBuffer->surface.mip_size = 0; 136 | colorBuffer->surface.mip_data = NULL; 137 | colorBuffer->surface.tile = GX2_TILE_MODE_DEFAULT; 138 | colorBuffer->surface.swizzle = 0; 139 | colorBuffer->surface.align = 0; 140 | colorBuffer->surface.pitch = 0; 141 | u32 i; 142 | for(i = 0; i < 13; i++) 143 | colorBuffer->surface.mip_offset[i] = 0; 144 | colorBuffer->view_mip = 0; 145 | colorBuffer->view_first_slice = 0; 146 | colorBuffer->view_slices_count = depth; 147 | colorBuffer->aux_data = NULL; 148 | colorBuffer->aux_size = 0; 149 | for(i = 0; i < 5; i++) 150 | colorBuffer->regs[i] = 0; 151 | 152 | GX2CalcSurfaceSizeAndAlignment(&colorBuffer->surface); 153 | GX2InitColorBufferRegs(colorBuffer); 154 | } 155 | 156 | static inline void GX2InitAttribStream(GX2AttribStream* attr, u32 location, u32 buffer, u32 offset, s32 format) 157 | { 158 | attr->location = location; 159 | attr->buffer = buffer; 160 | attr->offset = offset; 161 | attr->format = format; 162 | attr->index_type = 0; 163 | attr->divisor = 0; 164 | attr->destination_selector = attribute_dest_comp_selector[format & 0xff]; 165 | attr->endian_swap = GX2_ENDIANSWAP_DEFAULT; 166 | } 167 | 168 | static inline void GX2InitTexture(GX2Texture *tex, u32 width, u32 height, u32 depth, u32 num_mips, s32 format, s32 dimension, s32 tile) 169 | { 170 | tex->surface.dimension = dimension; 171 | tex->surface.width = width; 172 | tex->surface.height = height; 173 | tex->surface.depth = depth; 174 | tex->surface.num_mips = num_mips; 175 | tex->surface.format = format; 176 | tex->surface.aa = GX2_AA_MODE_1X; 177 | tex->surface.use = GX2_SURFACE_USE_TEXTURE; 178 | tex->surface.image_size = 0; 179 | tex->surface.image_data = NULL; 180 | tex->surface.mip_size = 0; 181 | tex->surface.mip_data = NULL; 182 | tex->surface.tile = tile; 183 | tex->surface.swizzle = 0; 184 | tex->surface.align = 0; 185 | tex->surface.pitch = 0; 186 | u32 i; 187 | for(i = 0; i < 13; i++) 188 | tex->surface.mip_offset[i] = 0; 189 | tex->view_first_mip = 0; 190 | tex->view_mips_count = num_mips; 191 | tex->view_first_slice = 0; 192 | tex->view_slices_count = depth; 193 | tex->component_selector = texture_comp_selector[format & 0x3f]; 194 | for(i = 0; i < 5; i++) 195 | tex->regs[i] = 0; 196 | 197 | GX2CalcSurfaceSizeAndAlignment(&tex->surface); 198 | GX2InitTextureRegs(tex); 199 | } 200 | 201 | #ifdef __cplusplus 202 | } 203 | #endif 204 | 205 | #endif // __GX2_FUNCTIONS_H_ 206 | -------------------------------------------------------------------------------- /src/dynamic_libs/os_functions.c: -------------------------------------------------------------------------------- 1 | /**************************************************************************** 2 | * Copyright (C) 2015 3 | * by Dimok 4 | * 5 | * This software is provided 'as-is', without any express or implied 6 | * warranty. In no event will the authors be held liable for any 7 | * damages arising from the use of this software. 8 | * 9 | * Permission is granted to anyone to use this software for any 10 | * purpose, including commercial applications, and to alter it and 11 | * redistribute it freely, subject to the following restrictions: 12 | * 13 | * 1. The origin of this software must not be misrepresented; you 14 | * must not claim that you wrote the original software. If you use 15 | * this software in a product, an acknowledgment in the product 16 | * documentation would be appreciated but is not required. 17 | * 18 | * 2. Altered source versions must be plainly marked as such, and 19 | * must not be misrepresented as being the original software. 20 | * 21 | * 3. This notice may not be removed or altered from any source 22 | * distribution. 23 | ***************************************************************************/ 24 | #include "common/common.h" 25 | #include "os_functions.h" 26 | 27 | unsigned int coreinit_handle = 0; 28 | 29 | //!---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 30 | //! Lib handle functions 31 | //!---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 32 | EXPORT_DECL(int, OSDynLoad_Acquire, const char* rpl, u32 *handle); 33 | EXPORT_DECL(int, OSDynLoad_FindExport, u32 handle, int isdata, const char *symbol, void *address); 34 | 35 | //!---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 36 | //! Thread functions 37 | //!---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 38 | EXPORT_DECL(int, OSCreateThread, void *thread, s32 (*callback)(s32, void*), s32 argc, void *args, u32 stack, u32 stack_size, s32 priority, u32 attr); 39 | EXPORT_DECL(int, OSResumeThread, void *thread); 40 | EXPORT_DECL(int, OSSuspendThread, void *thread); 41 | EXPORT_DECL(int, OSIsThreadTerminated, void *thread); 42 | EXPORT_DECL(int, OSIsThreadSuspended, void *thread); 43 | EXPORT_DECL(int, OSSetThreadPriority, void * thread, int priority); 44 | EXPORT_DECL(int, OSJoinThread, void * thread, int * ret_val); 45 | EXPORT_DECL(void, OSDetachThread, void * thread); 46 | EXPORT_DECL(void, OSSleepTicks, u64 ticks); 47 | EXPORT_DECL(u64, OSGetTick, void); 48 | 49 | //!---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 50 | //! Mutex functions 51 | //!---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 52 | EXPORT_DECL(void, OSInitMutex, void* mutex); 53 | EXPORT_DECL(void, OSLockMutex, void* mutex); 54 | EXPORT_DECL(void, OSUnlockMutex, void* mutex); 55 | EXPORT_DECL(int, OSTryLockMutex, void* mutex); 56 | 57 | //!---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 58 | //! System functions 59 | //!---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 60 | EXPORT_DECL(u64, OSGetTitleID, void); 61 | EXPORT_DECL(void, __Exit, void); 62 | EXPORT_DECL(void, OSFatal, const char* msg); 63 | EXPORT_DECL(void, OSSetExceptionCallback, u8 exceptionType, exception_callback newCallback); 64 | EXPORT_DECL(void, DCFlushRange, const void *addr, u32 length); 65 | EXPORT_DECL(void, ICInvalidateRange, const void *addr, u32 length); 66 | EXPORT_DECL(void*, OSEffectiveToPhysical, const void*); 67 | EXPORT_DECL(int, __os_snprintf, char* s, int n, const char * format, ...); 68 | 69 | EXPORT_DECL(void, OSScreenInit, void); 70 | EXPORT_DECL(unsigned int, OSScreenGetBufferSizeEx, unsigned int bufferNum); 71 | EXPORT_DECL(int, OSScreenSetBufferEx, unsigned int bufferNum, void * addr); 72 | EXPORT_DECL(int, OSScreenClearBufferEx, unsigned int bufferNum, unsigned int temp); 73 | EXPORT_DECL(int, OSScreenFlipBuffersEx, unsigned int bufferNum); 74 | EXPORT_DECL(int, OSScreenPutFontEx, unsigned int bufferNum, unsigned int posX, unsigned int posY, const char * buffer); 75 | EXPORT_DECL(int, OSScreenEnableEx, unsigned int bufferNum, int enable); 76 | 77 | //!---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 78 | //! Memory functions 79 | //!---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 80 | EXPORT_VAR(unsigned int *, pMEMAllocFromDefaultHeapEx); 81 | EXPORT_VAR(unsigned int *, pMEMAllocFromDefaultHeap); 82 | EXPORT_VAR(unsigned int *, pMEMFreeToDefaultHeap); 83 | 84 | EXPORT_DECL(int, MEMGetBaseHeapHandle, int mem_arena); 85 | EXPORT_DECL(unsigned int, MEMGetAllocatableSizeForFrmHeapEx, int heap, int align); 86 | EXPORT_DECL(void *, MEMAllocFromFrmHeapEx, int heap, unsigned int size, int align); 87 | EXPORT_DECL(void, MEMFreeToFrmHeap, int heap, int mode); 88 | EXPORT_DECL(void *, MEMAllocFromExpHeapEx, int heap, unsigned int size, int align); 89 | EXPORT_DECL(int , MEMCreateExpHeapEx, void* address, unsigned int size, unsigned short flags); 90 | EXPORT_DECL(void *, MEMDestroyExpHeap, int heap); 91 | EXPORT_DECL(void, MEMFreeToExpHeap, int heap, void* ptr); 92 | 93 | //!---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 94 | //! Loader functions (not real rpl) 95 | //!---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 96 | EXPORT_DECL(int, LiWaitIopComplete, int unknown_syscall_arg_r3, int * remaining_bytes); 97 | EXPORT_DECL(int, LiWaitIopCompleteWithInterrupts, int unknown_syscall_arg_r3, int * remaining_bytes); 98 | 99 | void InitOSFunctionPointers(void) 100 | { 101 | unsigned int *funcPointer = 0; 102 | //!---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 103 | //! Lib handle functions 104 | //!---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 105 | EXPORT_FUNC_WRITE(OSDynLoad_Acquire, (int (*)(const char*, unsigned *))OS_SPECIFICS->addr_OSDynLoad_Acquire); 106 | EXPORT_FUNC_WRITE(OSDynLoad_FindExport, (int (*)(u32, int, const char *, void *))OS_SPECIFICS->addr_OSDynLoad_FindExport); 107 | 108 | OSDynLoad_Acquire("coreinit.rpl", &coreinit_handle); 109 | 110 | //!---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 111 | //! System functions 112 | //!---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 113 | OS_FIND_EXPORT(coreinit_handle, OSFatal); 114 | OS_FIND_EXPORT(coreinit_handle, OSGetTitleID); 115 | OS_FIND_EXPORT(coreinit_handle, OSSetExceptionCallback); 116 | OS_FIND_EXPORT(coreinit_handle, DCFlushRange); 117 | OS_FIND_EXPORT(coreinit_handle, ICInvalidateRange); 118 | OS_FIND_EXPORT(coreinit_handle, OSEffectiveToPhysical); 119 | OS_FIND_EXPORT(coreinit_handle, __os_snprintf); 120 | OSDynLoad_FindExport(coreinit_handle, 0, "_Exit", &__Exit); 121 | 122 | OS_FIND_EXPORT(coreinit_handle, OSScreenInit); 123 | OS_FIND_EXPORT(coreinit_handle, OSScreenGetBufferSizeEx); 124 | OS_FIND_EXPORT(coreinit_handle, OSScreenSetBufferEx); 125 | OS_FIND_EXPORT(coreinit_handle, OSScreenClearBufferEx); 126 | OS_FIND_EXPORT(coreinit_handle, OSScreenFlipBuffersEx); 127 | OS_FIND_EXPORT(coreinit_handle, OSScreenPutFontEx); 128 | OS_FIND_EXPORT(coreinit_handle, OSScreenEnableEx); 129 | //!---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 130 | //! Thread functions 131 | //!---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 132 | OS_FIND_EXPORT(coreinit_handle, OSCreateThread); 133 | OS_FIND_EXPORT(coreinit_handle, OSResumeThread); 134 | OS_FIND_EXPORT(coreinit_handle, OSSuspendThread); 135 | OS_FIND_EXPORT(coreinit_handle, OSIsThreadTerminated); 136 | OS_FIND_EXPORT(coreinit_handle, OSIsThreadSuspended); 137 | OS_FIND_EXPORT(coreinit_handle, OSJoinThread); 138 | OS_FIND_EXPORT(coreinit_handle, OSSetThreadPriority); 139 | OS_FIND_EXPORT(coreinit_handle, OSDetachThread); 140 | OS_FIND_EXPORT(coreinit_handle, OSSleepTicks); 141 | OS_FIND_EXPORT(coreinit_handle, OSGetTick); 142 | //!---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 143 | //! Mutex functions 144 | //!---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 145 | OS_FIND_EXPORT(coreinit_handle, OSInitMutex); 146 | OS_FIND_EXPORT(coreinit_handle, OSLockMutex); 147 | OS_FIND_EXPORT(coreinit_handle, OSUnlockMutex); 148 | OS_FIND_EXPORT(coreinit_handle, OSTryLockMutex); 149 | 150 | //!---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 151 | //! Memory functions 152 | //!---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 153 | OSDynLoad_FindExport(coreinit_handle, 1, "MEMAllocFromDefaultHeapEx", &pMEMAllocFromDefaultHeapEx); 154 | OSDynLoad_FindExport(coreinit_handle, 1, "MEMAllocFromDefaultHeap", &pMEMAllocFromDefaultHeap); 155 | OSDynLoad_FindExport(coreinit_handle, 1, "MEMFreeToDefaultHeap", &pMEMFreeToDefaultHeap); 156 | 157 | OS_FIND_EXPORT(coreinit_handle, MEMGetBaseHeapHandle); 158 | OS_FIND_EXPORT(coreinit_handle, MEMGetAllocatableSizeForFrmHeapEx); 159 | OS_FIND_EXPORT(coreinit_handle, MEMAllocFromFrmHeapEx); 160 | OS_FIND_EXPORT(coreinit_handle, MEMFreeToFrmHeap); 161 | OS_FIND_EXPORT(coreinit_handle, MEMAllocFromExpHeapEx); 162 | OS_FIND_EXPORT(coreinit_handle, MEMCreateExpHeapEx); 163 | OS_FIND_EXPORT(coreinit_handle, MEMDestroyExpHeap); 164 | OS_FIND_EXPORT(coreinit_handle, MEMFreeToExpHeap); 165 | } 166 | 167 | -------------------------------------------------------------------------------- /src/fs/sd_fat_devoptab.c: -------------------------------------------------------------------------------- 1 | /*************************************************************************** 2 | * Copyright (C) 2015 3 | * by Dimok 4 | * 5 | * This software is provided 'as-is', without any express or implied 6 | * warranty. In no event will the authors be held liable for any 7 | * damages arising from the use of this software. 8 | * 9 | * Permission is granted to anyone to use this software for any 10 | * purpose, including commercial applications, and to alter it and 11 | * redistribute it freely, subject to the following restrictions: 12 | * 13 | * 1. The origin of this software must not be misrepresented; you 14 | * must not claim that you wrote the original software. If you use 15 | * this software in a product, an acknowledgment in the product 16 | * documentation would be appreciated but is not required. 17 | * 18 | * 2. Altered source versions must be plainly marked as such, and 19 | * must not be misrepresented as being the original software. 20 | * 21 | * 3. This notice may not be removed or altered from any source 22 | * distribution. 23 | ***************************************************************************/ 24 | #include 25 | #include 26 | #include 27 | #include 28 | #include 29 | #include 30 | #include 31 | #include 32 | #include "dynamic_libs/fs_functions.h" 33 | #include "dynamic_libs/os_functions.h" 34 | #include "fs_utils.h" 35 | 36 | #define FS_ALIGNMENT 0x40 37 | #define FS_ALIGN(x) (((x) + FS_ALIGNMENT - 1) & ~(FS_ALIGNMENT - 1)) 38 | 39 | typedef struct _sd_fat_private_t { 40 | char *mount_path; 41 | void *pClient; 42 | void *pCmd; 43 | void *pMutex; 44 | } sd_fat_private_t; 45 | 46 | typedef struct _sd_fat_file_state_t { 47 | sd_fat_private_t *dev; 48 | int fd; /* File descriptor */ 49 | int flags; /* Opening flags */ 50 | bool read; /* True if allowed to read from file */ 51 | bool write; /* True if allowed to write to file */ 52 | bool append; /* True if allowed to append to file */ 53 | u64 pos; /* Current position within the file (in bytes) */ 54 | u64 len; /* Total length of the file (in bytes) */ 55 | struct _sd_fat_file_state_t *prevOpenFile; /* The previous entry in a double-linked FILO list of open files */ 56 | struct _sd_fat_file_state_t *nextOpenFile; /* The next entry in a double-linked FILO list of open files */ 57 | } sd_fat_file_state_t; 58 | 59 | typedef struct _sd_fat_dir_entry_t { 60 | sd_fat_private_t *dev; 61 | int dirHandle; 62 | } sd_fat_dir_entry_t; 63 | 64 | static sd_fat_private_t *sd_fat_get_device_data(const char *path) 65 | { 66 | const devoptab_t *devoptab = NULL; 67 | char name[128] = {0}; 68 | int i; 69 | 70 | // Get the device name from the path 71 | strncpy(name, path, 127); 72 | strtok(name, ":/"); 73 | 74 | // Search the devoptab table for the specified device name 75 | // NOTE: We do this manually due to a 'bug' in GetDeviceOpTab 76 | // which ignores names with suffixes and causes names 77 | // like "ntfs" and "ntfs1" to be seen as equals 78 | for (i = 3; i < STD_MAX; i++) { 79 | devoptab = devoptab_list[i]; 80 | if (devoptab && devoptab->name) { 81 | if (strcmp(name, devoptab->name) == 0) { 82 | return (sd_fat_private_t *)devoptab->deviceData; 83 | } 84 | } 85 | } 86 | 87 | return NULL; 88 | } 89 | 90 | static char *sd_fat_real_path (const char *path, sd_fat_private_t *dev) 91 | { 92 | // Sanity check 93 | if (!path) 94 | return NULL; 95 | 96 | // Move the path pointer to the start of the actual path 97 | if (strchr(path, ':') != NULL) { 98 | path = strchr(path, ':') + 1; 99 | } 100 | 101 | int mount_len = strlen(dev->mount_path); 102 | 103 | char *new_name = (char*)malloc(mount_len + strlen(path) + 1); 104 | if(new_name) { 105 | strcpy(new_name, dev->mount_path); 106 | strcpy(new_name + mount_len, path); 107 | return new_name; 108 | } 109 | return new_name; 110 | } 111 | 112 | static int sd_fat_open_r (struct _reent *r, void *fileStruct, const char *path, int flags, int mode) 113 | { 114 | sd_fat_private_t *dev = sd_fat_get_device_data(path); 115 | if(!dev) { 116 | r->_errno = ENODEV; 117 | return -1; 118 | } 119 | 120 | sd_fat_file_state_t *file = (sd_fat_file_state_t *)fileStruct; 121 | 122 | file->dev = dev; 123 | // Determine which mode the file is opened for 124 | file->flags = flags; 125 | 126 | const char *mode_str; 127 | 128 | if ((flags & 0x03) == O_RDONLY) { 129 | file->read = true; 130 | file->write = false; 131 | file->append = false; 132 | mode_str = "r"; 133 | } else if ((flags & 0x03) == O_WRONLY) { 134 | file->read = false; 135 | file->write = true; 136 | file->append = (flags & O_APPEND); 137 | mode_str = file->append ? "a" : "w"; 138 | } else if ((flags & 0x03) == O_RDWR) { 139 | file->read = true; 140 | file->write = true; 141 | file->append = (flags & O_APPEND); 142 | mode_str = file->append ? "a+" : "r+"; 143 | } else { 144 | r->_errno = EACCES; 145 | return -1; 146 | } 147 | 148 | int fd = -1; 149 | 150 | OSLockMutex(dev->pMutex); 151 | 152 | char *real_path = sd_fat_real_path(path, dev); 153 | if(!path) { 154 | r->_errno = ENOMEM; 155 | OSUnlockMutex(dev->pMutex); 156 | return -1; 157 | } 158 | 159 | int result = FSOpenFile(dev->pClient, dev->pCmd, real_path, mode_str, &fd, -1); 160 | 161 | free(real_path); 162 | 163 | if(result == 0) 164 | { 165 | FSStat stats; 166 | result = FSGetStatFile(dev->pClient, dev->pCmd, fd, &stats, -1); 167 | if(result != 0) { 168 | FSCloseFile(dev->pClient, dev->pCmd, fd, -1); 169 | r->_errno = result; 170 | OSUnlockMutex(dev->pMutex); 171 | return -1; 172 | } 173 | file->fd = fd; 174 | file->pos = 0; 175 | file->len = stats.size; 176 | OSUnlockMutex(dev->pMutex); 177 | return (int)file; 178 | } 179 | 180 | r->_errno = result; 181 | OSUnlockMutex(dev->pMutex); 182 | return -1; 183 | } 184 | 185 | 186 | static int sd_fat_close_r (struct _reent *r, int fd) 187 | { 188 | sd_fat_file_state_t *file = (sd_fat_file_state_t *)fd; 189 | if(!file->dev) { 190 | r->_errno = ENODEV; 191 | return -1; 192 | } 193 | 194 | OSLockMutex(file->dev->pMutex); 195 | 196 | int result = FSCloseFile(file->dev->pClient, file->dev->pCmd, file->fd, -1); 197 | 198 | OSUnlockMutex(file->dev->pMutex); 199 | 200 | if(result < 0) 201 | { 202 | r->_errno = result; 203 | return -1; 204 | } 205 | return 0; 206 | } 207 | 208 | static off_t sd_fat_seek_r (struct _reent *r, int fd, off_t pos, int dir) 209 | { 210 | sd_fat_file_state_t *file = (sd_fat_file_state_t *)fd; 211 | if(!file->dev) { 212 | r->_errno = ENODEV; 213 | return 0; 214 | } 215 | 216 | OSLockMutex(file->dev->pMutex); 217 | 218 | switch(dir) 219 | { 220 | case SEEK_SET: 221 | file->pos = pos; 222 | break; 223 | case SEEK_CUR: 224 | file->pos += pos; 225 | break; 226 | case SEEK_END: 227 | file->pos = file->len + pos; 228 | break; 229 | default: 230 | r->_errno = EINVAL; 231 | return -1; 232 | } 233 | 234 | int result = FSSetPosFile(file->dev->pClient, file->dev->pCmd, file->fd, file->pos, -1); 235 | 236 | OSUnlockMutex(file->dev->pMutex); 237 | 238 | if(result == 0) 239 | { 240 | return file->pos; 241 | } 242 | 243 | return result; 244 | } 245 | 246 | static ssize_t sd_fat_write_r (struct _reent *r, int fd, const char *ptr, size_t len) 247 | { 248 | sd_fat_file_state_t *file = (sd_fat_file_state_t *)fd; 249 | if(!file->dev) { 250 | r->_errno = ENODEV; 251 | return 0; 252 | } 253 | 254 | if(!file->write) 255 | { 256 | r->_errno = EACCES; 257 | return 0; 258 | } 259 | 260 | OSLockMutex(file->dev->pMutex); 261 | 262 | size_t len_aligned = FS_ALIGN(len); 263 | if(len_aligned > 0x4000) 264 | len_aligned = 0x4000; 265 | 266 | unsigned char *tmpBuf = (unsigned char *)memalign(FS_ALIGNMENT, len_aligned); 267 | if(!tmpBuf) { 268 | r->_errno = ENOMEM; 269 | OSUnlockMutex(file->dev->pMutex); 270 | return 0; 271 | } 272 | 273 | size_t done = 0; 274 | 275 | while(done < len) 276 | { 277 | size_t write_size = (len_aligned < (len - done)) ? len_aligned : (len - done); 278 | memcpy(tmpBuf, ptr + done, write_size); 279 | 280 | int result = FSWriteFile(file->dev->pClient, file->dev->pCmd, tmpBuf, 0x01, write_size, file->fd, 0, -1); 281 | if(result < 0) 282 | { 283 | r->_errno = result; 284 | break; 285 | } 286 | else if(result == 0) 287 | { 288 | if(write_size > 0) 289 | done = 0; 290 | break; 291 | } 292 | else 293 | { 294 | done += result; 295 | file->pos += result; 296 | } 297 | } 298 | 299 | free(tmpBuf); 300 | OSUnlockMutex(file->dev->pMutex); 301 | return done; 302 | } 303 | 304 | static ssize_t sd_fat_read_r (struct _reent *r, int fd, char *ptr, size_t len) 305 | { 306 | sd_fat_file_state_t *file = (sd_fat_file_state_t *)fd; 307 | if(!file->dev) { 308 | r->_errno = ENODEV; 309 | return 0; 310 | } 311 | 312 | if(!file->read) 313 | { 314 | r->_errno = EACCES; 315 | return 0; 316 | } 317 | 318 | OSLockMutex(file->dev->pMutex); 319 | 320 | size_t len_aligned = FS_ALIGN(len); 321 | if(len_aligned > 0x4000) 322 | len_aligned = 0x4000; 323 | 324 | unsigned char *tmpBuf = (unsigned char *)memalign(FS_ALIGNMENT, len_aligned); 325 | if(!tmpBuf) { 326 | r->_errno = ENOMEM; 327 | OSUnlockMutex(file->dev->pMutex); 328 | return 0; 329 | } 330 | 331 | size_t done = 0; 332 | 333 | while(done < len) 334 | { 335 | size_t read_size = (len_aligned < (len - done)) ? len_aligned : (len - done); 336 | 337 | int result = FSReadFile(file->dev->pClient, file->dev->pCmd, tmpBuf, 0x01, read_size, file->fd, 0, -1); 338 | if(result < 0) 339 | { 340 | r->_errno = result; 341 | done = 0; 342 | break; 343 | } 344 | else if(result == 0) 345 | { 346 | //! TODO: error on read_size > 0 347 | break; 348 | } 349 | else 350 | { 351 | memcpy(ptr + done, tmpBuf, read_size); 352 | done += result; 353 | file->pos += result; 354 | } 355 | } 356 | 357 | free(tmpBuf); 358 | OSUnlockMutex(file->dev->pMutex); 359 | return done; 360 | } 361 | 362 | 363 | static int sd_fat_fstat_r (struct _reent *r, int fd, struct stat *st) 364 | { 365 | sd_fat_file_state_t *file = (sd_fat_file_state_t *)fd; 366 | if(!file->dev) { 367 | r->_errno = ENODEV; 368 | return -1; 369 | } 370 | 371 | OSLockMutex(file->dev->pMutex); 372 | 373 | // Zero out the stat buffer 374 | memset(st, 0, sizeof(struct stat)); 375 | 376 | FSStat stats; 377 | int result = FSGetStatFile(file->dev->pClient, file->dev->pCmd, file->fd, &stats, -1); 378 | if(result != 0) { 379 | r->_errno = result; 380 | OSUnlockMutex(file->dev->pMutex); 381 | return -1; 382 | } 383 | 384 | st->st_mode = S_IFREG; 385 | st->st_size = stats.size; 386 | st->st_blocks = (stats.size + 511) >> 9; 387 | st->st_nlink = 1; 388 | 389 | // Fill in the generic entry stats 390 | st->st_dev = stats.ent_id; 391 | st->st_uid = stats.owner_id; 392 | st->st_gid = stats.group_id; 393 | st->st_ino = stats.ent_id; 394 | st->st_atime = stats.mtime; 395 | st->st_ctime = stats.ctime; 396 | st->st_mtime = stats.mtime; 397 | OSUnlockMutex(file->dev->pMutex); 398 | return 0; 399 | } 400 | 401 | static int sd_fat_ftruncate_r (struct _reent *r, int fd, off_t len) 402 | { 403 | sd_fat_file_state_t *file = (sd_fat_file_state_t *)fd; 404 | if(!file->dev) { 405 | r->_errno = ENODEV; 406 | return -1; 407 | } 408 | 409 | OSLockMutex(file->dev->pMutex); 410 | 411 | int result = FSTruncateFile(file->dev->pClient, file->dev->pCmd, file->fd, -1); 412 | 413 | OSUnlockMutex(file->dev->pMutex); 414 | 415 | if(result < 0) { 416 | r->_errno = result; 417 | return -1; 418 | } 419 | 420 | return 0; 421 | } 422 | 423 | static int sd_fat_fsync_r (struct _reent *r, int fd) 424 | { 425 | sd_fat_file_state_t *file = (sd_fat_file_state_t *)fd; 426 | if(!file->dev) { 427 | r->_errno = ENODEV; 428 | return -1; 429 | } 430 | 431 | OSLockMutex(file->dev->pMutex); 432 | 433 | int result = FSFlushFile(file->dev->pClient, file->dev->pCmd, file->fd, -1); 434 | 435 | OSUnlockMutex(file->dev->pMutex); 436 | 437 | if(result < 0) { 438 | r->_errno = result; 439 | return -1; 440 | } 441 | 442 | return 0; 443 | } 444 | 445 | static int sd_fat_stat_r (struct _reent *r, const char *path, struct stat *st) 446 | { 447 | sd_fat_private_t *dev = sd_fat_get_device_data(path); 448 | if(!dev) { 449 | r->_errno = ENODEV; 450 | return -1; 451 | } 452 | 453 | OSLockMutex(dev->pMutex); 454 | 455 | // Zero out the stat buffer 456 | memset(st, 0, sizeof(struct stat)); 457 | 458 | char *real_path = sd_fat_real_path(path, dev); 459 | if(!real_path) { 460 | r->_errno = ENOMEM; 461 | OSUnlockMutex(dev->pMutex); 462 | return -1; 463 | } 464 | 465 | FSStat stats; 466 | 467 | int result = FSGetStat(dev->pClient, dev->pCmd, real_path, &stats, -1); 468 | 469 | free(real_path); 470 | 471 | if(result < 0) { 472 | r->_errno = result; 473 | OSUnlockMutex(dev->pMutex); 474 | return -1; 475 | } 476 | 477 | // mark root also as directory 478 | st->st_mode = ((stats.flag & 0x80000000) || (strlen(dev->mount_path) + 1 == strlen(real_path)))? S_IFDIR : S_IFREG; 479 | st->st_nlink = 1; 480 | st->st_size = stats.size; 481 | st->st_blocks = (stats.size + 511) >> 9; 482 | // Fill in the generic entry stats 483 | st->st_dev = stats.ent_id; 484 | st->st_uid = stats.owner_id; 485 | st->st_gid = stats.group_id; 486 | st->st_ino = stats.ent_id; 487 | st->st_atime = stats.mtime; 488 | st->st_ctime = stats.ctime; 489 | st->st_mtime = stats.mtime; 490 | 491 | OSUnlockMutex(dev->pMutex); 492 | 493 | return 0; 494 | } 495 | 496 | static int sd_fat_link_r (struct _reent *r, const char *existing, const char *newLink) 497 | { 498 | r->_errno = ENOTSUP; 499 | return -1; 500 | } 501 | 502 | static int sd_fat_unlink_r (struct _reent *r, const char *name) 503 | { 504 | sd_fat_private_t *dev = sd_fat_get_device_data(name); 505 | if(!dev) { 506 | r->_errno = ENODEV; 507 | return -1; 508 | } 509 | 510 | OSLockMutex(dev->pMutex); 511 | 512 | char *real_path = sd_fat_real_path(name, dev); 513 | if(!real_path) { 514 | r->_errno = ENOMEM; 515 | OSUnlockMutex(dev->pMutex); 516 | return -1; 517 | } 518 | 519 | 520 | int result = FSRemove(dev->pClient, dev->pCmd, real_path, -1); 521 | 522 | free(real_path); 523 | 524 | OSUnlockMutex(dev->pMutex); 525 | 526 | if(result < 0) { 527 | r->_errno = result; 528 | return -1; 529 | } 530 | 531 | return 0; 532 | } 533 | 534 | static int sd_fat_chdir_r (struct _reent *r, const char *name) 535 | { 536 | sd_fat_private_t *dev = sd_fat_get_device_data(name); 537 | if(!dev) { 538 | r->_errno = ENODEV; 539 | return -1; 540 | } 541 | 542 | OSLockMutex(dev->pMutex); 543 | 544 | char *real_path = sd_fat_real_path(name, dev); 545 | if(!real_path) { 546 | r->_errno = ENOMEM; 547 | OSUnlockMutex(dev->pMutex); 548 | return -1; 549 | } 550 | 551 | int result = FSChangeDir(dev->pClient, dev->pCmd, real_path, -1); 552 | 553 | free(real_path); 554 | 555 | OSUnlockMutex(dev->pMutex); 556 | 557 | if(result < 0) { 558 | r->_errno = result; 559 | return -1; 560 | } 561 | 562 | return 0; 563 | } 564 | 565 | static int sd_fat_rename_r (struct _reent *r, const char *oldName, const char *newName) 566 | { 567 | sd_fat_private_t *dev = sd_fat_get_device_data(oldName); 568 | if(!dev) { 569 | r->_errno = ENODEV; 570 | return -1; 571 | } 572 | 573 | OSLockMutex(dev->pMutex); 574 | 575 | char *real_oldpath = sd_fat_real_path(oldName, dev); 576 | if(!real_oldpath) { 577 | r->_errno = ENOMEM; 578 | OSUnlockMutex(dev->pMutex); 579 | return -1; 580 | } 581 | char *real_newpath = sd_fat_real_path(newName, dev); 582 | if(!real_newpath) { 583 | r->_errno = ENOMEM; 584 | free(real_oldpath); 585 | OSUnlockMutex(dev->pMutex); 586 | return -1; 587 | } 588 | 589 | int result = FSRename(dev->pClient, dev->pCmd, real_oldpath, real_newpath, -1); 590 | 591 | free(real_oldpath); 592 | free(real_newpath); 593 | 594 | OSUnlockMutex(dev->pMutex); 595 | 596 | if(result < 0) { 597 | r->_errno = result; 598 | return -1; 599 | } 600 | 601 | return 0; 602 | 603 | } 604 | 605 | static int sd_fat_mkdir_r (struct _reent *r, const char *path, int mode) 606 | { 607 | sd_fat_private_t *dev = sd_fat_get_device_data(path); 608 | if(!dev) { 609 | r->_errno = ENODEV; 610 | return -1; 611 | } 612 | 613 | OSLockMutex(dev->pMutex); 614 | 615 | char *real_path = sd_fat_real_path(path, dev); 616 | if(!real_path) { 617 | r->_errno = ENOMEM; 618 | OSUnlockMutex(dev->pMutex); 619 | return -1; 620 | } 621 | 622 | int result = FSMakeDir(dev->pClient, dev->pCmd, real_path, -1); 623 | 624 | free(real_path); 625 | 626 | OSUnlockMutex(dev->pMutex); 627 | 628 | if(result < 0) { 629 | r->_errno = result; 630 | return -1; 631 | } 632 | 633 | return 0; 634 | } 635 | 636 | static int sd_fat_statvfs_r (struct _reent *r, const char *path, struct statvfs *buf) 637 | { 638 | sd_fat_private_t *dev = sd_fat_get_device_data(path); 639 | if(!dev) { 640 | r->_errno = ENODEV; 641 | return -1; 642 | } 643 | 644 | OSLockMutex(dev->pMutex); 645 | 646 | // Zero out the stat buffer 647 | memset(buf, 0, sizeof(struct statvfs)); 648 | 649 | char *real_path = sd_fat_real_path(path, dev); 650 | if(!real_path) { 651 | r->_errno = ENOMEM; 652 | OSUnlockMutex(dev->pMutex); 653 | return -1; 654 | } 655 | 656 | u64 size; 657 | 658 | int result = FSGetFreeSpaceSize(dev->pClient, dev->pCmd, real_path, &size, -1); 659 | 660 | free(real_path); 661 | 662 | if(result < 0) { 663 | r->_errno = result; 664 | OSUnlockMutex(dev->pMutex); 665 | return -1; 666 | } 667 | 668 | // File system block size 669 | buf->f_bsize = 512; 670 | 671 | // Fundamental file system block size 672 | buf->f_frsize = 512; 673 | 674 | // Total number of blocks on file system in units of f_frsize 675 | buf->f_blocks = size >> 9; // this is unknown 676 | 677 | // Free blocks available for all and for non-privileged processes 678 | buf->f_bfree = buf->f_bavail = size >> 9; 679 | 680 | // Number of inodes at this point in time 681 | buf->f_files = 0xffffffff; 682 | 683 | // Free inodes available for all and for non-privileged processes 684 | buf->f_ffree = 0xffffffff; 685 | 686 | // File system id 687 | buf->f_fsid = (int)dev; 688 | 689 | // Bit mask of f_flag values. 690 | buf->f_flag = 0; 691 | 692 | // Maximum length of filenames 693 | buf->f_namemax = 255; 694 | 695 | OSUnlockMutex(dev->pMutex); 696 | 697 | return 0; 698 | } 699 | 700 | static DIR_ITER *sd_fat_diropen_r (struct _reent *r, DIR_ITER *dirState, const char *path) 701 | { 702 | sd_fat_private_t *dev = sd_fat_get_device_data(path); 703 | if(!dev) { 704 | r->_errno = ENODEV; 705 | return NULL; 706 | } 707 | 708 | sd_fat_dir_entry_t *dirIter = (sd_fat_dir_entry_t *)dirState->dirStruct; 709 | 710 | OSLockMutex(dev->pMutex); 711 | 712 | char *real_path = sd_fat_real_path(path, dev); 713 | if(!real_path) { 714 | r->_errno = ENOMEM; 715 | OSUnlockMutex(dev->pMutex); 716 | return NULL; 717 | } 718 | 719 | int dirHandle; 720 | 721 | int result = FSOpenDir(dev->pClient, dev->pCmd, real_path, &dirHandle, -1); 722 | 723 | free(real_path); 724 | 725 | OSUnlockMutex(dev->pMutex); 726 | 727 | if(result < 0) 728 | { 729 | r->_errno = result; 730 | return NULL; 731 | } 732 | 733 | dirIter->dev = dev; 734 | dirIter->dirHandle = dirHandle; 735 | 736 | return dirState; 737 | } 738 | 739 | static int sd_fat_dirclose_r (struct _reent *r, DIR_ITER *dirState) 740 | { 741 | sd_fat_dir_entry_t *dirIter = (sd_fat_dir_entry_t *)dirState->dirStruct; 742 | if(!dirIter->dev) { 743 | r->_errno = ENODEV; 744 | return -1; 745 | } 746 | 747 | OSLockMutex(dirIter->dev->pMutex); 748 | 749 | int result = FSCloseDir(dirIter->dev->pClient, dirIter->dev->pCmd, dirIter->dirHandle, -1); 750 | 751 | OSUnlockMutex(dirIter->dev->pMutex); 752 | 753 | if(result < 0) 754 | { 755 | r->_errno = result; 756 | return -1; 757 | } 758 | return 0; 759 | } 760 | 761 | static int sd_fat_dirreset_r (struct _reent *r, DIR_ITER *dirState) 762 | { 763 | sd_fat_dir_entry_t *dirIter = (sd_fat_dir_entry_t *)dirState->dirStruct; 764 | if(!dirIter->dev) { 765 | r->_errno = ENODEV; 766 | return -1; 767 | } 768 | 769 | OSLockMutex(dirIter->dev->pMutex); 770 | 771 | int result = FSRewindDir(dirIter->dev->pClient, dirIter->dev->pCmd, dirIter->dirHandle, -1); 772 | 773 | OSUnlockMutex(dirIter->dev->pMutex); 774 | 775 | if(result < 0) 776 | { 777 | r->_errno = result; 778 | return -1; 779 | } 780 | return 0; 781 | } 782 | 783 | static int sd_fat_dirnext_r (struct _reent *r, DIR_ITER *dirState, char *filename, struct stat *st) 784 | { 785 | sd_fat_dir_entry_t *dirIter = (sd_fat_dir_entry_t *)dirState->dirStruct; 786 | if(!dirIter->dev) { 787 | r->_errno = ENODEV; 788 | return -1; 789 | } 790 | 791 | OSLockMutex(dirIter->dev->pMutex); 792 | 793 | FSDirEntry * dir_entry = malloc(sizeof(FSDirEntry)); 794 | 795 | int result = FSReadDir(dirIter->dev->pClient, dirIter->dev->pCmd, dirIter->dirHandle, dir_entry, -1); 796 | if(result < 0) 797 | { 798 | free(dir_entry); 799 | r->_errno = result; 800 | OSUnlockMutex(dirIter->dev->pMutex); 801 | return -1; 802 | } 803 | 804 | // Fetch the current entry 805 | strcpy(filename, dir_entry->name); 806 | 807 | if(st) 808 | { 809 | memset(st, 0, sizeof(struct stat)); 810 | st->st_mode = (dir_entry->stat.flag & 0x80000000) ? S_IFDIR : S_IFREG; 811 | st->st_nlink = 1; 812 | st->st_size = dir_entry->stat.size; 813 | st->st_blocks = (dir_entry->stat.size + 511) >> 9; 814 | st->st_dev = dir_entry->stat.ent_id; 815 | st->st_uid = dir_entry->stat.owner_id; 816 | st->st_gid = dir_entry->stat.group_id; 817 | st->st_ino = dir_entry->stat.ent_id; 818 | st->st_atime = dir_entry->stat.mtime; 819 | st->st_ctime = dir_entry->stat.ctime; 820 | st->st_mtime = dir_entry->stat.mtime; 821 | } 822 | 823 | free(dir_entry); 824 | OSUnlockMutex(dirIter->dev->pMutex); 825 | return 0; 826 | } 827 | 828 | // NTFS device driver devoptab 829 | static const devoptab_t devops_sd_fat = { 830 | NULL, /* Device name */ 831 | sizeof (sd_fat_file_state_t), 832 | sd_fat_open_r, 833 | sd_fat_close_r, 834 | sd_fat_write_r, 835 | sd_fat_read_r, 836 | sd_fat_seek_r, 837 | sd_fat_fstat_r, 838 | sd_fat_stat_r, 839 | sd_fat_link_r, 840 | sd_fat_unlink_r, 841 | sd_fat_chdir_r, 842 | sd_fat_rename_r, 843 | sd_fat_mkdir_r, 844 | sizeof (sd_fat_dir_entry_t), 845 | sd_fat_diropen_r, 846 | sd_fat_dirreset_r, 847 | sd_fat_dirnext_r, 848 | sd_fat_dirclose_r, 849 | sd_fat_statvfs_r, 850 | sd_fat_ftruncate_r, 851 | sd_fat_fsync_r, 852 | NULL, /* sd_fat_chmod_r */ 853 | NULL, /* sd_fat_fchmod_r */ 854 | NULL /* Device data */ 855 | }; 856 | 857 | static int sd_fat_add_device (const char *name, const char *mount_path, void *pClient, void *pCmd) 858 | { 859 | devoptab_t *dev = NULL; 860 | char *devname = NULL; 861 | char *devpath = NULL; 862 | int i; 863 | 864 | // Sanity check 865 | if (!name) { 866 | errno = EINVAL; 867 | return -1; 868 | } 869 | 870 | // Allocate a devoptab for this device 871 | dev = (devoptab_t *) malloc(sizeof(devoptab_t) + strlen(name) + 1); 872 | if (!dev) { 873 | errno = ENOMEM; 874 | return -1; 875 | } 876 | 877 | // Use the space allocated at the end of the devoptab for storing the device name 878 | devname = (char*)(dev + 1); 879 | strcpy(devname, name); 880 | 881 | // create private data 882 | sd_fat_private_t *priv = (sd_fat_private_t *)malloc(sizeof(sd_fat_private_t) + strlen(mount_path) + 1); 883 | if(!priv) { 884 | free(dev); 885 | errno = ENOMEM; 886 | return -1; 887 | } 888 | 889 | devpath = (char*)(priv+1); 890 | strcpy(devpath, mount_path); 891 | 892 | // setup private data 893 | priv->mount_path = devpath; 894 | priv->pClient = pClient; 895 | priv->pCmd = pCmd; 896 | priv->pMutex = malloc(OS_MUTEX_SIZE); 897 | 898 | if(!priv->pMutex) { 899 | free(dev); 900 | free(priv); 901 | errno = ENOMEM; 902 | return -1; 903 | } 904 | 905 | OSInitMutex(priv->pMutex); 906 | 907 | // Setup the devoptab 908 | memcpy(dev, &devops_sd_fat, sizeof(devoptab_t)); 909 | dev->name = devname; 910 | dev->deviceData = priv; 911 | 912 | // Add the device to the devoptab table (if there is a free slot) 913 | for (i = 3; i < STD_MAX; i++) { 914 | if (devoptab_list[i] == devoptab_list[0]) { 915 | devoptab_list[i] = dev; 916 | return 0; 917 | } 918 | } 919 | 920 | // failure, free all memory 921 | free(priv); 922 | free(dev); 923 | 924 | // If we reach here then there are no free slots in the devoptab table for this device 925 | errno = EADDRNOTAVAIL; 926 | return -1; 927 | } 928 | 929 | static int sd_fat_remove_device (const char *path, void **pClient, void **pCmd, char **mountPath) 930 | { 931 | const devoptab_t *devoptab = NULL; 932 | char name[128] = {0}; 933 | int i; 934 | 935 | // Get the device name from the path 936 | strncpy(name, path, 127); 937 | strtok(name, ":/"); 938 | 939 | // Find and remove the specified device from the devoptab table 940 | // NOTE: We do this manually due to a 'bug' in RemoveDevice 941 | // which ignores names with suffixes and causes names 942 | // like "ntfs" and "ntfs1" to be seen as equals 943 | for (i = 3; i < STD_MAX; i++) { 944 | devoptab = devoptab_list[i]; 945 | if (devoptab && devoptab->name) { 946 | if (strcmp(name, devoptab->name) == 0) { 947 | devoptab_list[i] = devoptab_list[0]; 948 | 949 | if(devoptab->deviceData) 950 | { 951 | sd_fat_private_t *priv = (sd_fat_private_t *)devoptab->deviceData; 952 | *pClient = priv->pClient; 953 | *pCmd = priv->pCmd; 954 | *mountPath = (char*) malloc(strlen(priv->mount_path)+1); 955 | if(*mountPath) 956 | strcpy(*mountPath, priv->mount_path); 957 | if(priv->pMutex) 958 | free(priv->pMutex); 959 | free(devoptab->deviceData); 960 | } 961 | 962 | free((devoptab_t*)devoptab); 963 | return 0; 964 | } 965 | } 966 | } 967 | 968 | return -1; 969 | } 970 | 971 | int mount_sd_fat(const char *path) 972 | { 973 | int result = -1; 974 | 975 | // get command and client 976 | void* pClient = malloc(FS_CLIENT_SIZE); 977 | void* pCmd = malloc(FS_CMD_BLOCK_SIZE); 978 | 979 | if(!pClient || !pCmd) { 980 | // just in case free if not 0 981 | if(pClient) 982 | free(pClient); 983 | if(pCmd) 984 | free(pCmd); 985 | return -2; 986 | } 987 | 988 | FSInit(); 989 | FSInitCmdBlock(pCmd); 990 | FSAddClientEx(pClient, 0, -1); 991 | 992 | char *mountPath = NULL; 993 | 994 | if(MountFS(pClient, pCmd, &mountPath) == 0) { 995 | result = sd_fat_add_device(path, mountPath, pClient, pCmd); 996 | free(mountPath); 997 | } 998 | 999 | return result; 1000 | } 1001 | 1002 | int unmount_sd_fat(const char *path) 1003 | { 1004 | void *pClient = 0; 1005 | void *pCmd = 0; 1006 | char *mountPath = 0; 1007 | 1008 | int result = sd_fat_remove_device(path, &pClient, &pCmd, &mountPath); 1009 | if(result == 0) 1010 | { 1011 | UmountFS(pClient, pCmd, mountPath); 1012 | FSDelClient(pClient); 1013 | free(pClient); 1014 | free(pCmd); 1015 | free(mountPath); 1016 | //FSShutdown(); 1017 | } 1018 | return result; 1019 | } 1020 | -------------------------------------------------------------------------------- /src/ftp.c: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | ftpii -- an FTP server for the Wii 4 | 5 | Copyright (C) 2008 Joseph Jordan 6 | 7 | This software is provided 'as-is', without any express or implied warranty. 8 | In no event will the authors be held liable for any damages arising from 9 | the use of this software. 10 | 11 | Permission is granted to anyone to use this software for any purpose, 12 | including commercial applications, and to alter it and redistribute it 13 | freely, subject to the following restrictions: 14 | 15 | 1.The origin of this software must not be misrepresented; you must not 16 | claim that you wrote the original software. If you use this software in a 17 | product, an acknowledgment in the product documentation would be 18 | appreciated but is not required. 19 | 20 | 2.Altered source versions must be plainly marked as such, and must not be 21 | misrepresented as being the original software. 22 | 23 | 3.This notice may not be removed or altered from any source distribution. 24 | 25 | */ 26 | #include 27 | #include 28 | #include 29 | #include 30 | #include 31 | #include "dynamic_libs/os_functions.h" 32 | #include "dynamic_libs/socket_functions.h" 33 | 34 | //! TODO: fix those function 35 | #define gettime() OSGetTick() 36 | #define errno geterrno() 37 | 38 | #include "ftp.h" 39 | #include "virtualpath.h" 40 | #include "net.h" 41 | #include "vrt.h" 42 | 43 | #define UNUSED __attribute__((unused)) 44 | 45 | #define FTP_BUFFER_SIZE 1024 46 | #define MAX_CLIENTS 5 47 | 48 | extern void console_printf(const char *format, ...); 49 | 50 | static const u16 SRC_PORT = 20; 51 | static const s32 EQUIT = 696969; 52 | static const char *CRLF = "\r\n"; 53 | static const u32 CRLF_LENGTH = 2; 54 | 55 | static u8 num_clients = 0; 56 | static u16 passive_port = 1024; 57 | static char *password = NULL; 58 | 59 | typedef s32 (*data_connection_callback)(s32 data_socket, void *arg); 60 | 61 | struct client_struct { 62 | s32 socket; 63 | char representation_type; 64 | s32 passive_socket; 65 | s32 data_socket; 66 | char cwd[MAXPATHLEN]; 67 | char pending_rename[MAXPATHLEN]; 68 | off_t restart_marker; 69 | struct sockaddr_in address; 70 | bool authenticated; 71 | char buf[FTP_BUFFER_SIZE]; 72 | s32 offset; 73 | bool data_connection_connected; 74 | data_connection_callback data_callback; 75 | void *data_connection_callback_arg; 76 | void (*data_connection_cleanup)(void *arg); 77 | u64 data_connection_timer; 78 | }; 79 | 80 | typedef struct client_struct client_t; 81 | 82 | static client_t *clients[MAX_CLIENTS] = { NULL }; 83 | 84 | void set_ftp_password(char *new_password) { 85 | if (password) free(password); 86 | if (new_password) { 87 | password = malloc(strlen(new_password) + 1); 88 | if (!password) 89 | return; 90 | 91 | strcpy((char *)password, new_password); 92 | } else { 93 | password = NULL; 94 | } 95 | } 96 | 97 | static bool compare_ftp_password(char *password_attempt) { 98 | return !password || !strcmp((char *)password, password_attempt); 99 | } 100 | 101 | /* 102 | TODO: support multi-line reply 103 | */ 104 | static s32 write_reply(client_t *client, u16 code, char *msg) { 105 | u32 msglen = 4 + strlen(msg) + CRLF_LENGTH; 106 | char * msgbuf = (char *) malloc(msglen + 1); 107 | if (msgbuf == NULL) return -ENOMEM; 108 | sprintf(msgbuf, "%u %s\r\n", code, msg); 109 | console_printf("Wrote reply: %s", msgbuf); 110 | s32 ret = send_exact(client->socket, msgbuf, msglen); 111 | free(msgbuf); 112 | return ret; 113 | } 114 | 115 | static void close_passive_socket(client_t *client) { 116 | if (client->passive_socket >= 0) { 117 | network_close_blocking(client->passive_socket); 118 | client->passive_socket = -1; 119 | } 120 | } 121 | 122 | /* 123 | result must be able to hold up to maxsplit+1 null-terminated strings of length strlen(s) 124 | returns the number of strings stored in the result array (up to maxsplit+1) 125 | */ 126 | static u32 split(char *s, char sep, u32 maxsplit, char *result[]) { 127 | u32 num_results = 0; 128 | u32 result_pos = 0; 129 | u32 trim_pos = 0; 130 | bool in_word = false; 131 | for (; *s; s++) { 132 | if (*s == sep) { 133 | if (num_results <= maxsplit) { 134 | in_word = false; 135 | continue; 136 | } else if (!trim_pos) { 137 | trim_pos = result_pos; 138 | } 139 | } else if (trim_pos) { 140 | trim_pos = 0; 141 | } 142 | if (!in_word) { 143 | in_word = true; 144 | if (num_results <= maxsplit) { 145 | num_results++; 146 | result_pos = 0; 147 | } 148 | } 149 | result[num_results - 1][result_pos++] = *s; 150 | result[num_results - 1][result_pos] = '\0'; 151 | } 152 | if (trim_pos) { 153 | result[num_results - 1][trim_pos] = '\0'; 154 | } 155 | u32 i = num_results; 156 | for (i = num_results; i <= maxsplit; i++) { 157 | result[i][0] = '\0'; 158 | } 159 | return num_results; 160 | } 161 | 162 | static s32 ftp_USER(client_t *client, char *username UNUSED) { 163 | return write_reply(client, 331, "User name okay, need password."); 164 | } 165 | 166 | static s32 ftp_PASS(client_t *client, char *password_attempt) { 167 | if (compare_ftp_password(password_attempt)) { 168 | client->authenticated = true; 169 | return write_reply(client, 230, "User logged in, proceed."); 170 | } else { 171 | return write_reply(client, 530, "Login incorrect."); 172 | } 173 | } 174 | 175 | static s32 ftp_REIN(client_t *client, char *rest UNUSED) { 176 | close_passive_socket(client); 177 | strcpy(client->cwd, "/"); 178 | client->representation_type = 'A'; 179 | client->authenticated = false; 180 | return write_reply(client, 220, "Service ready for new user."); 181 | } 182 | 183 | static s32 ftp_QUIT(client_t *client, char *rest UNUSED) { 184 | // TODO: dont quit if xfer in progress 185 | s32 result = write_reply(client, 221, "Service closing control connection."); 186 | return result < 0 ? result : -EQUIT; 187 | } 188 | 189 | static s32 ftp_SYST(client_t *client, char *rest UNUSED) { 190 | return write_reply(client, 215, "UNIX Type: L8 Version: ftpii"); 191 | } 192 | 193 | static s32 ftp_TYPE(client_t *client, char *rest) { 194 | char representation_type[FTP_BUFFER_SIZE], param[FTP_BUFFER_SIZE]; 195 | char *args[] = { representation_type, param }; 196 | u32 num_args = split(rest, ' ', 1, args); 197 | if (num_args == 0) { 198 | return write_reply(client, 501, "Syntax error in parameters."); 199 | } else if ((!strcasecmp("A", representation_type) && (!*param || !strcasecmp("N", param))) || 200 | (!strcasecmp("I", representation_type) && num_args == 1)) { 201 | client->representation_type = *representation_type; 202 | } else { 203 | return write_reply(client, 501, "Syntax error in parameters."); 204 | } 205 | char msg[15]; 206 | sprintf(msg, "Type set to %s.", representation_type); 207 | return write_reply(client, 200, msg); 208 | } 209 | 210 | static s32 ftp_MODE(client_t *client, char *rest) { 211 | if (!strcasecmp("S", rest)) { 212 | return write_reply(client, 200, "Mode S ok."); 213 | } else { 214 | return write_reply(client, 501, "Syntax error in parameters."); 215 | } 216 | } 217 | 218 | static s32 ftp_PWD(client_t *client, char *rest UNUSED) { 219 | char msg[MAXPATHLEN + 24]; 220 | // TODO: escape double-quotes 221 | sprintf(msg, "\"%s\" is current directory.", client->cwd); 222 | return write_reply(client, 257, msg); 223 | } 224 | 225 | static s32 ftp_CWD(client_t *client, char *path) { 226 | s32 result; 227 | if (!vrt_chdir(client->cwd, path)) { 228 | result = write_reply(client, 250, "CWD command successful."); 229 | } else { 230 | result = write_reply(client, 550, strerror(errno)); 231 | } 232 | return result; 233 | } 234 | 235 | static s32 ftp_CDUP(client_t *client, char *rest UNUSED) { 236 | s32 result; 237 | if (!vrt_chdir(client->cwd, "..")) { 238 | result = write_reply(client, 250, "CDUP command successful."); 239 | } else { 240 | result = write_reply(client, 550, strerror(errno)); 241 | } 242 | return result; 243 | } 244 | 245 | static s32 ftp_DELE(client_t *client, char *path) { 246 | if (!vrt_unlink(client->cwd, path)) { 247 | return write_reply(client, 250, "File or directory removed."); 248 | } else { 249 | return write_reply(client, 550, strerror(errno)); 250 | } 251 | } 252 | 253 | static s32 ftp_MKD(client_t *client, char *path) { 254 | if (!*path) { 255 | return write_reply(client, 501, "Syntax error in parameters."); 256 | } 257 | if (!vrt_mkdir(client->cwd, path, 0777)) { 258 | char msg[MAXPATHLEN + 21]; 259 | char abspath[MAXPATHLEN]; 260 | strcpy(abspath, client->cwd); 261 | vrt_chdir(abspath, path); // TODO: error checking 262 | // TODO: escape double-quotes 263 | sprintf(msg, "\"%s\" directory created.", abspath); 264 | return write_reply(client, 257, msg); 265 | } else { 266 | return write_reply(client, 550, strerror(errno)); 267 | } 268 | } 269 | 270 | static s32 ftp_RNFR(client_t *client, char *path) { 271 | strcpy(client->pending_rename, path); 272 | return write_reply(client, 350, "Ready for RNTO."); 273 | } 274 | 275 | static s32 ftp_RNTO(client_t *client, char *path) { 276 | if (!*client->pending_rename) { 277 | return write_reply(client, 503, "RNFR required first."); 278 | } 279 | s32 result; 280 | if (!vrt_rename(client->cwd, client->pending_rename, path)) { 281 | result = write_reply(client, 250, "Rename successful."); 282 | } else { 283 | result = write_reply(client, 550, strerror(errno)); 284 | } 285 | *client->pending_rename = '\0'; 286 | return result; 287 | } 288 | 289 | static s32 ftp_SIZE(client_t *client, char *path) { 290 | struct stat st; 291 | if (!vrt_stat(client->cwd, path, &st)) { 292 | char size_buf[12]; 293 | sprintf(size_buf, "%llu", st.st_size); 294 | return write_reply(client, 213, size_buf); 295 | } else { 296 | return write_reply(client, 550, strerror(errno)); 297 | } 298 | } 299 | 300 | static s32 ftp_PASV(client_t *client, char *rest UNUSED) { 301 | close_passive_socket(client); 302 | client->passive_socket = network_socket(AF_INET, SOCK_STREAM, IPPROTO_IP); 303 | if (client->passive_socket < 0) { 304 | return write_reply(client, 520, "Unable to create listening socket."); 305 | } 306 | set_blocking(client->passive_socket, false); 307 | struct sockaddr_in bindAddress; 308 | memset(&bindAddress, 0, sizeof(bindAddress)); 309 | bindAddress.sin_family = AF_INET; 310 | bindAddress.sin_port = htons(passive_port++); // XXX: BUG: This will overflow eventually, with interesting results... 311 | bindAddress.sin_addr.s_addr = htonl(INADDR_ANY); 312 | s32 result; 313 | if ((result = network_bind(client->passive_socket, (struct sockaddr *)&bindAddress, sizeof(bindAddress))) < 0) { 314 | close_passive_socket(client); 315 | return write_reply(client, 520, "Unable to bind listening socket."); 316 | } 317 | if ((result = network_listen(client->passive_socket, 1)) < 0) { 318 | close_passive_socket(client); 319 | return write_reply(client, 520, "Unable to listen on socket."); 320 | } 321 | char reply[49]; 322 | u16 port = bindAddress.sin_port; 323 | u32 ip = network_gethostip(); 324 | struct in_addr addr; 325 | addr.s_addr = ip; 326 | console_printf("Listening for data connections at %s:%u...\n", inet_ntoa(addr), port); 327 | sprintf(reply, "Entering Passive Mode (%u,%u,%u,%u,%u,%u).", (ip >> 24) & 0xff, (ip >> 16) & 0xff, (ip >> 8) & 0xff, ip & 0xff, (port >> 8) & 0xff, port & 0xff); 328 | return write_reply(client, 227, reply); 329 | } 330 | 331 | static s32 ftp_PORT(client_t *client, char *portspec) { 332 | u32 h1, h2, h3, h4, p1, p2; 333 | if (sscanf(portspec, "%3u,%3u,%3u,%3u,%3u,%3u", &h1, &h2, &h3, &h4, &p1, &p2) < 6) { 334 | return write_reply(client, 501, "Syntax error in parameters."); 335 | } 336 | char addr_str[44]; 337 | sprintf(addr_str, "%u.%u.%u.%u", h1, h2, h3, h4); 338 | struct in_addr sin_addr; 339 | if (!inet_aton(addr_str, &sin_addr)) { 340 | return write_reply(client, 501, "Syntax error in parameters."); 341 | } 342 | close_passive_socket(client); 343 | u16 port = ((p1 &0xff) << 8) | (p2 & 0xff); 344 | client->address.sin_addr = sin_addr; 345 | client->address.sin_port = htons(port); 346 | console_printf("Set client address to %s:%u\n", addr_str, port); 347 | return write_reply(client, 200, "PORT command successful."); 348 | } 349 | 350 | typedef s32 (*data_connection_handler)(client_t *client, data_connection_callback callback, void *arg); 351 | 352 | static s32 prepare_data_connection_active(client_t *client, data_connection_callback callback UNUSED, void *arg UNUSED) { 353 | s32 data_socket = network_socket(AF_INET, SOCK_STREAM, IPPROTO_IP); 354 | if (data_socket < 0) return data_socket; 355 | set_blocking(data_socket, false); 356 | struct sockaddr_in bindAddress; 357 | memset(&bindAddress, 0, sizeof(bindAddress)); 358 | bindAddress.sin_family = AF_INET; 359 | bindAddress.sin_port = htons(SRC_PORT); 360 | bindAddress.sin_addr.s_addr = htonl(INADDR_ANY); 361 | s32 result; 362 | if ((result = network_bind(data_socket, (struct sockaddr *)&bindAddress, sizeof(bindAddress))) < 0) { 363 | network_close(data_socket); 364 | return result; 365 | } 366 | 367 | client->data_socket = data_socket; 368 | console_printf("Attempting to connect to client at %s:%u\n", inet_ntoa(client->address.sin_addr), client->address.sin_port); 369 | return 0; 370 | } 371 | 372 | static s32 prepare_data_connection_passive(client_t *client, data_connection_callback callback UNUSED, void *arg UNUSED) { 373 | client->data_socket = client->passive_socket; 374 | console_printf("Waiting for data connections...\n"); 375 | return 0; 376 | } 377 | 378 | static s32 prepare_data_connection(client_t *client, void *callback, void *arg, void *cleanup) { 379 | s32 result = write_reply(client, 150, "Transferring data."); 380 | if (result >= 0) { 381 | data_connection_handler handler = prepare_data_connection_active; 382 | if (client->passive_socket >= 0) handler = prepare_data_connection_passive; 383 | result = handler(client, (data_connection_callback)callback, arg); 384 | if (result < 0) { 385 | result = write_reply(client, 520, "Closing data connection, error occurred during transfer."); 386 | } else { 387 | client->data_connection_connected = false; 388 | client->data_callback = callback; 389 | client->data_connection_callback_arg = arg; 390 | client->data_connection_cleanup = cleanup; 391 | client->data_connection_timer = gettime() + SECS_TO_TICKS(10); 392 | } 393 | } 394 | return result; 395 | } 396 | 397 | static s32 send_nlst(s32 data_socket, DIR_P *iter) { 398 | s32 result = 0; 399 | char filename[MAXPATHLEN]; 400 | struct dirent *dirent = NULL; 401 | while ((dirent = vrt_readdir(iter)) != 0) { 402 | size_t end_index = strlen(dirent->d_name); 403 | if(end_index + 2 >= MAXPATHLEN) 404 | continue; 405 | strcpy(filename, dirent->d_name); 406 | filename[end_index] = CRLF[0]; 407 | filename[end_index + 1] = CRLF[1]; 408 | filename[end_index + 2] = '\0'; 409 | if ((result = send_exact(data_socket, filename, strlen(filename))) < 0) { 410 | break; 411 | } 412 | } 413 | return result < 0 ? result : 0; 414 | } 415 | 416 | static s32 send_list(s32 data_socket, DIR_P *iter) { 417 | struct stat st; 418 | s32 result = 0; 419 | time_t mtime = 0; 420 | u64 size = 0; 421 | char filename[MAXPATHLEN]; 422 | char line[MAXPATHLEN + 56 + CRLF_LENGTH + 1]; 423 | struct dirent *dirent = NULL; 424 | while ((dirent = vrt_readdir(iter)) != 0) { 425 | 426 | snprintf(filename, sizeof(filename), "%s/%s", iter->path, dirent->d_name); 427 | if(stat(filename, &st) == 0) 428 | { 429 | mtime = st.st_mtime; 430 | size = st.st_size; 431 | } 432 | else 433 | { 434 | mtime = time(0); 435 | size = 0; 436 | } 437 | 438 | char timestamp[13]; 439 | strftime(timestamp, sizeof(timestamp), "%b %d %Y", localtime(&mtime)); 440 | snprintf(line, sizeof(line), "%crwxr-xr-x 1 0 0 %10llu %s %s\r\n", (dirent->d_type & DT_DIR) ? 'd' : '-', size, timestamp, dirent->d_name); 441 | if ((result = send_exact(data_socket, line, strlen(line))) < 0) { 442 | break; 443 | } 444 | } 445 | return result < 0 ? result : 0; 446 | } 447 | 448 | static s32 ftp_NLST(client_t *client, char *path) { 449 | if (!*path) { 450 | path = "."; 451 | } 452 | 453 | DIR_P *dir = vrt_opendir(client->cwd, path); 454 | if (dir == NULL) { 455 | return write_reply(client, 550, strerror(errno)); 456 | } 457 | 458 | s32 result = prepare_data_connection(client, send_nlst, dir, vrt_closedir); 459 | if (result < 0) vrt_closedir(dir); 460 | return result; 461 | } 462 | 463 | static s32 ftp_LIST(client_t *client, char *path) { 464 | if (*path == '-') { 465 | // handle buggy clients that use "LIST -aL" or similar, at the expense of breaking paths that begin with '-' 466 | char flags[FTP_BUFFER_SIZE]; 467 | char rest[FTP_BUFFER_SIZE]; 468 | char *args[] = { flags, rest }; 469 | split(path, ' ', 1, args); 470 | path = rest; 471 | } 472 | if (!*path) { 473 | path = "."; 474 | } 475 | 476 | if(path && client->cwd) 477 | { 478 | if(strcmp(path, ".") == 0 && strcmp(client->cwd, "/") == 0) 479 | { 480 | UnmountVirtualPaths(); 481 | MountVirtualDevices(); 482 | } 483 | } 484 | 485 | DIR_P *dir = vrt_opendir(client->cwd, path); 486 | if (dir == NULL) { 487 | return write_reply(client, 550, strerror(errno)); 488 | } 489 | 490 | s32 result = prepare_data_connection(client, send_list, dir, vrt_closedir); 491 | if (result < 0) vrt_closedir(dir); 492 | return result; 493 | } 494 | 495 | static s32 ftp_RETR(client_t *client, char *path) { 496 | FILE *f = vrt_fopen(client->cwd, path, "rb"); 497 | if (!f) { 498 | return write_reply(client, 550, strerror(errno)); 499 | } 500 | 501 | int fd = fileno(f); 502 | if (client->restart_marker && lseek(fd, client->restart_marker, SEEK_SET) != client->restart_marker) { 503 | s32 lseek_error = errno; 504 | fclose(f); 505 | client->restart_marker = 0; 506 | return write_reply(client, 550, strerror(lseek_error)); 507 | } 508 | client->restart_marker = 0; 509 | 510 | s32 result = prepare_data_connection(client, send_from_file, f, fclose); 511 | if (result < 0) fclose(f); 512 | return result; 513 | } 514 | 515 | static s32 stor_or_append(client_t *client, FILE *f) { 516 | if (!f) { 517 | return write_reply(client, 550, strerror(errno)); 518 | } 519 | s32 result = prepare_data_connection(client, recv_to_file, f, fclose); 520 | if (result < 0) fclose(f); 521 | return result; 522 | } 523 | 524 | static s32 ftp_STOR(client_t *client, char *path) { 525 | FILE *f = vrt_fopen(client->cwd, path, "wb"); 526 | int fd; 527 | if (f) fd = fileno(f); 528 | if (f && client->restart_marker && lseek(fd, client->restart_marker, SEEK_SET) != client->restart_marker) { 529 | s32 lseek_error = errno; 530 | fclose(f); 531 | client->restart_marker = 0; 532 | return write_reply(client, 550, strerror(lseek_error)); 533 | } 534 | client->restart_marker = 0; 535 | 536 | return stor_or_append(client, f); 537 | } 538 | 539 | static s32 ftp_APPE(client_t *client, char *path) { 540 | return stor_or_append(client, vrt_fopen(client->cwd, path, "ab")); 541 | } 542 | 543 | static s32 ftp_REST(client_t *client, char *offset_str) { 544 | off_t offset; 545 | if (sscanf(offset_str, "%lli", &offset) < 1 || offset < 0) { 546 | return write_reply(client, 501, "Syntax error in parameters."); 547 | } 548 | client->restart_marker = offset; 549 | char msg[FTP_BUFFER_SIZE]; 550 | sprintf(msg, "Restart position accepted (%lli).", offset); 551 | return write_reply(client, 350, msg); 552 | } 553 | 554 | static s32 ftp_SITE_LOADER(client_t *client, char *rest UNUSED) { 555 | s32 result = write_reply(client, 200, "Exiting to loader."); 556 | //set_reset_flag(); 557 | return result; 558 | } 559 | 560 | static s32 ftp_SITE_CLEAR(client_t *client, char *rest UNUSED) { 561 | s32 result = write_reply(client, 200, "Cleared."); 562 | u32 i; 563 | for (i = 0; i < 18; i++) console_printf("\n"); 564 | //console_printf("\x1b[2;0H"); 565 | return result; 566 | } 567 | 568 | /* 569 | This is implemented as a no-op to prevent some FTP clients 570 | from displaying skip/abort/retry type prompts. 571 | */ 572 | static s32 ftp_SITE_CHMOD(client_t *client, char *rest UNUSED) { 573 | return write_reply(client, 250, "SITE CHMOD command ok."); 574 | } 575 | 576 | static s32 ftp_SITE_PASSWD(client_t *client, char *new_password) { 577 | set_ftp_password(new_password); 578 | return write_reply(client, 200, "Password changed."); 579 | } 580 | 581 | static s32 ftp_SITE_NOPASSWD(client_t *client, char *rest UNUSED) { 582 | set_ftp_password(NULL); 583 | return write_reply(client, 200, "Authentication disabled."); 584 | } 585 | 586 | static s32 ftp_SITE_EJECT(client_t *client, char *rest UNUSED) { 587 | //if (dvd_eject()) return write_reply(client, 550, "Unable to eject DVD."); 588 | return write_reply(client, 200, "DVD ejected."); 589 | } 590 | 591 | static s32 ftp_SITE_MOUNT(client_t *client, char *path UNUSED) { 592 | //if (!mount_virtual(path)) return write_reply(client, 550, "Unable to mount."); 593 | return write_reply(client, 250, "Mounted."); 594 | } 595 | 596 | static s32 ftp_SITE_UNMOUNT(client_t *client, char *path UNUSED) { 597 | //if (!unmount_virtual(path)) return write_reply(client, 550, "Unable to unmount."); 598 | return write_reply(client, 250, "Unmounted."); 599 | } 600 | 601 | static s32 ftp_SITE_UNKNOWN(client_t *client, char *rest UNUSED) { 602 | return write_reply(client, 501, "Unknown SITE command."); 603 | } 604 | 605 | static s32 ftp_SITE_LOAD(client_t *client, char *path UNUSED) { 606 | // FILE *f = vrt_fopen(client->cwd, path, "rb"); 607 | // if (!f) return write_reply(client, 550, strerror(errno)); 608 | // char *real_path = to_real_path(client->cwd, path); 609 | // if (!real_path) goto end; 610 | // load_from_file(f, real_path); 611 | // free(real_path); 612 | // end: 613 | // fclose(f); 614 | return write_reply(client, 500, "Unable to load."); 615 | } 616 | 617 | typedef s32 (*ftp_command_handler)(client_t *client, char *args); 618 | 619 | static s32 dispatch_to_handler(client_t *client, char *cmd_line, const char **commands, const ftp_command_handler *handlers) { 620 | char cmd[FTP_BUFFER_SIZE], rest[FTP_BUFFER_SIZE]; 621 | char *args[] = { cmd, rest }; 622 | split(cmd_line, ' ', 1, args); 623 | s32 i; 624 | for (i = 0; commands[i]; i++) { 625 | if (!strcasecmp(commands[i], cmd)) break; 626 | } 627 | return handlers[i](client, rest); 628 | } 629 | 630 | static const char *site_commands[] = { "LOADER", "CLEAR", "CHMOD", "PASSWD", "NOPASSWD", "EJECT", "MOUNT", "UNMOUNT", "LOAD", NULL }; 631 | static const ftp_command_handler site_handlers[] = { ftp_SITE_LOADER, ftp_SITE_CLEAR, ftp_SITE_CHMOD, ftp_SITE_PASSWD, ftp_SITE_NOPASSWD, ftp_SITE_EJECT, ftp_SITE_MOUNT, ftp_SITE_UNMOUNT, ftp_SITE_LOAD, ftp_SITE_UNKNOWN }; 632 | 633 | static s32 ftp_SITE(client_t *client, char *cmd_line) { 634 | return dispatch_to_handler(client, cmd_line, site_commands, site_handlers); 635 | } 636 | 637 | static s32 ftp_NOOP(client_t *client, char *rest UNUSED) { 638 | return write_reply(client, 200, "NOOP command successful."); 639 | } 640 | 641 | static s32 ftp_SUPERFLUOUS(client_t *client, char *rest UNUSED) { 642 | return write_reply(client, 202, "Command not implemented, superfluous at this site."); 643 | } 644 | 645 | static s32 ftp_NEEDAUTH(client_t *client, char *rest UNUSED) { 646 | return write_reply(client, 530, "Please login with USER and PASS."); 647 | } 648 | 649 | static s32 ftp_UNKNOWN(client_t *client, char *rest UNUSED) { 650 | return write_reply(client, 502, "Command not implemented."); 651 | } 652 | 653 | static const char *unauthenticated_commands[] = { "USER", "PASS", "QUIT", "REIN", "NOOP", NULL }; 654 | static const ftp_command_handler unauthenticated_handlers[] = { ftp_USER, ftp_PASS, ftp_QUIT, ftp_REIN, ftp_NOOP, ftp_NEEDAUTH }; 655 | 656 | static const char *authenticated_commands[] = { 657 | "USER", "PASS", "LIST", "PWD", "CWD", "CDUP", 658 | "SIZE", "PASV", "PORT", "TYPE", "SYST", "MODE", 659 | "RETR", "STOR", "APPE", "REST", "DELE", "MKD", 660 | "RMD", "RNFR", "RNTO", "NLST", "QUIT", "REIN", 661 | "SITE", "NOOP", "ALLO", NULL 662 | }; 663 | static const ftp_command_handler authenticated_handlers[] = { 664 | ftp_USER, ftp_PASS, ftp_LIST, ftp_PWD, ftp_CWD, ftp_CDUP, 665 | ftp_SIZE, ftp_PASV, ftp_PORT, ftp_TYPE, ftp_SYST, ftp_MODE, 666 | ftp_RETR, ftp_STOR, ftp_APPE, ftp_REST, ftp_DELE, ftp_MKD, 667 | ftp_DELE, ftp_RNFR, ftp_RNTO, ftp_NLST, ftp_QUIT, ftp_REIN, 668 | ftp_SITE, ftp_NOOP, ftp_SUPERFLUOUS, ftp_UNKNOWN 669 | }; 670 | 671 | /* 672 | returns negative to signal an error that requires closing the connection 673 | */ 674 | static s32 process_command(client_t *client, char *cmd_line) { 675 | if (strlen(cmd_line) == 0) { 676 | return 0; 677 | } 678 | 679 | console_printf("Got command: %s\n", cmd_line); 680 | 681 | const char **commands = unauthenticated_commands; 682 | const ftp_command_handler *handlers = unauthenticated_handlers; 683 | 684 | if (client->authenticated) { 685 | commands = authenticated_commands; 686 | handlers = authenticated_handlers; 687 | } 688 | 689 | return dispatch_to_handler(client, cmd_line, commands, handlers); 690 | } 691 | 692 | static void cleanup_data_resources(client_t *client) { 693 | if (client->data_socket >= 0 && client->data_socket != client->passive_socket) { 694 | network_close_blocking(client->data_socket); 695 | } 696 | client->data_socket = -1; 697 | client->data_connection_connected = false; 698 | client->data_callback = NULL; 699 | if (client->data_connection_cleanup) { 700 | client->data_connection_cleanup(client->data_connection_callback_arg); 701 | } 702 | client->data_connection_callback_arg = NULL; 703 | client->data_connection_cleanup = NULL; 704 | client->data_connection_timer = 0; 705 | } 706 | 707 | static void cleanup_client(client_t *client) { 708 | network_close_blocking(client->socket); 709 | cleanup_data_resources(client); 710 | close_passive_socket(client); 711 | int client_index; 712 | for (client_index = 0; client_index < MAX_CLIENTS; client_index++) { 713 | if (clients[client_index] == client) { 714 | clients[client_index] = NULL; 715 | break; 716 | } 717 | } 718 | free(client); 719 | num_clients--; 720 | console_printf("Client disconnected.\n"); 721 | } 722 | 723 | void cleanup_ftp() { 724 | int client_index; 725 | for (client_index = 0; client_index < MAX_CLIENTS; client_index++) { 726 | client_t *client = clients[client_index]; 727 | if (client) { 728 | write_reply(client, 421, "Service not available, closing control connection."); 729 | cleanup_client(client); 730 | } 731 | } 732 | } 733 | 734 | static bool process_accept_events(s32 server) { 735 | s32 peer; 736 | struct sockaddr_in client_address; 737 | s32 addrlen = sizeof(client_address); 738 | while ((peer = network_accept(server, (struct sockaddr *)&client_address, &addrlen)) != -EAGAIN) { 739 | if (peer < 0) { 740 | console_printf("Error accepting connection: [%i] %s\n", -peer, strerror(-peer)); 741 | return false; 742 | } 743 | 744 | console_printf("Accepted connection from %s!\n", inet_ntoa(client_address.sin_addr)); 745 | 746 | if (num_clients == MAX_CLIENTS) { 747 | console_printf("Maximum of %u clients reached, not accepting client.\n", MAX_CLIENTS); 748 | network_close(peer); 749 | return true; 750 | } 751 | 752 | client_t *client = malloc(sizeof(client_t)); 753 | if (!client) { 754 | console_printf("Could not allocate memory for client state, not accepting client.\n"); 755 | network_close(peer); 756 | return true; 757 | } 758 | client->socket = peer; 759 | client->representation_type = 'A'; 760 | client->passive_socket = -1; 761 | client->data_socket = -1; 762 | strcpy(client->cwd, "/"); 763 | *client->pending_rename = '\0'; 764 | client->restart_marker = 0; 765 | client->authenticated = false; 766 | client->offset = 0; 767 | client->data_connection_connected = false; 768 | client->data_callback = NULL; 769 | client->data_connection_callback_arg = NULL; 770 | client->data_connection_cleanup = NULL; 771 | client->data_connection_timer = 0; 772 | memcpy(&client->address, &client_address, sizeof(client_address)); 773 | int client_index; 774 | if (write_reply(client, 220, "ftpii") < 0) { 775 | console_printf("Error writing greeting.\n"); 776 | network_close_blocking(peer); 777 | free(client); 778 | } else { 779 | for (client_index = 0; client_index < MAX_CLIENTS; client_index++) { 780 | if (!clients[client_index]) { 781 | clients[client_index] = client; 782 | break; 783 | } 784 | } 785 | num_clients++; 786 | } 787 | } 788 | return true; 789 | } 790 | 791 | static void process_data_events(client_t *client) { 792 | s32 result; 793 | if (!client->data_connection_connected) { 794 | if (client->passive_socket >= 0) { 795 | struct sockaddr_in data_peer_address; 796 | s32 addrlen = sizeof(data_peer_address); 797 | result = network_accept(client->passive_socket, (struct sockaddr *)&data_peer_address ,&addrlen); 798 | if (result >= 0) { 799 | client->data_socket = result; 800 | client->data_connection_connected = true; 801 | } 802 | } else { 803 | if ((result = network_connect(client->data_socket, (struct sockaddr *)&client->address, sizeof(client->address))) < 0) { 804 | if (result == -EINPROGRESS || result == -EALREADY) result = -EAGAIN; 805 | if ((result != -EAGAIN) && (result != -EISCONN)) 806 | { 807 | console_printf("Unable to connect to client: [%i] %s\n", -result, strerror(-result)); 808 | } 809 | } 810 | if (result >= 0 || result == -EISCONN) { 811 | client->data_connection_connected = true; 812 | } 813 | } 814 | if (client->data_connection_connected) { 815 | result = 1; 816 | console_printf("Connected to client! Transferring data...\n"); 817 | } else if (gettime() > client->data_connection_timer) { 818 | result = -2; 819 | console_printf("Timed out waiting for data connection.\n"); 820 | } 821 | } else { 822 | result = client->data_callback(client->data_socket, client->data_connection_callback_arg); 823 | } 824 | 825 | if (result <= 0 && result != -EAGAIN) { 826 | cleanup_data_resources(client); 827 | if (result < 0) { 828 | result = write_reply(client, 520, "Closing data connection, error occurred during transfer."); 829 | } else { 830 | result = write_reply(client, 226, "Closing data connection, transfer successful."); 831 | } 832 | if (result < 0) { 833 | cleanup_client(client); 834 | } 835 | } 836 | } 837 | 838 | static void process_control_events(client_t *client) { 839 | s32 bytes_read; 840 | while (client->offset < (FTP_BUFFER_SIZE - 1)) { 841 | if (client->data_callback) { 842 | return; 843 | } 844 | char *offset_buf = client->buf + client->offset; 845 | if ((bytes_read = network_read(client->socket, offset_buf, FTP_BUFFER_SIZE - 1 - client->offset)) < 0) { 846 | if (bytes_read != -EAGAIN) { 847 | console_printf("Read error %i occurred, closing client.\n", bytes_read); 848 | goto recv_loop_end; 849 | } 850 | return; 851 | } else if (bytes_read == 0) { 852 | goto recv_loop_end; // EOF from client 853 | } 854 | client->offset += bytes_read; 855 | client->buf[client->offset] = '\0'; 856 | 857 | if (strchr(offset_buf, '\0') != (client->buf + client->offset)) { 858 | console_printf("Received a null byte from client, closing connection ;-)\n"); // i have decided this isn't allowed =P 859 | goto recv_loop_end; 860 | } 861 | 862 | char *next; 863 | char *end; 864 | for (next = client->buf; (end = strstr(next, CRLF)) && !client->data_callback; next = end + CRLF_LENGTH) { 865 | *end = '\0'; 866 | if (strchr(next, '\n')) { 867 | console_printf("Received a line-feed from client without preceding carriage return, closing connection ;-)\n"); // i have decided this isn't allowed =P 868 | goto recv_loop_end; 869 | } 870 | 871 | if (*next) { 872 | s32 result; 873 | if ((result = process_command(client, next)) < 0) { 874 | if (result != -EQUIT) { 875 | console_printf("Closing connection due to error while processing command: %s\n", next); 876 | } 877 | goto recv_loop_end; 878 | } 879 | } 880 | 881 | } 882 | 883 | if (next != client->buf) { // some lines were processed 884 | client->offset = strlen(next); 885 | char tmp_buf[client->offset]; 886 | memcpy(tmp_buf, next, client->offset); 887 | memcpy(client->buf, tmp_buf, client->offset); 888 | } 889 | } 890 | console_printf("Received line longer than %u bytes, closing client.\n", FTP_BUFFER_SIZE - 1); 891 | 892 | recv_loop_end: 893 | cleanup_client(client); 894 | } 895 | 896 | bool process_ftp_events(s32 server) { 897 | bool network_down = !process_accept_events(server); 898 | int client_index; 899 | for (client_index = 0; client_index < MAX_CLIENTS; client_index++) { 900 | client_t *client = clients[client_index]; 901 | if (client) { 902 | if (client->data_callback) { 903 | process_data_events(client); 904 | } else { 905 | process_control_events(client); 906 | } 907 | } 908 | } 909 | return network_down; 910 | } 911 | --------------------------------------------------------------------------------