From 844f95f94824b17eca5762a0af5aeff552a6128a Mon Sep 17 00:00:00 2001 From: Bignaux Ronan Date: Fri, 3 Sep 2021 07:26:35 +0200 Subject: [PATCH] NBD: add gdefaultexport config option --- include/config.h | 27 +++++----- modules/network/lwnbdsvr/{lwNBD => }/LICENSE | 0 modules/network/lwnbdsvr/Makefile | 2 +- .../network/lwnbdsvr/{lwNBD => }/README.md | 5 ++ .../lwnbdsvr/drivers/{atad.c => atad_d.c} | 9 ++-- .../lwnbdsvr/drivers/{atad.h => atad_d.h} | 3 +- modules/network/lwnbdsvr/imports.lst | 1 + modules/network/lwnbdsvr/lwnbdsvr.c | 15 ++++-- .../lwnbdsvr/{lwNBD => }/nbd-protocol.h | 0 .../network/lwnbdsvr/{lwNBD => }/nbd_opts.h | 0 .../lwnbdsvr/{lwNBD => }/nbd_protocol.c | 4 +- .../network/lwnbdsvr/{lwNBD => }/nbd_server.c | 52 ++++++++++++++++++- .../network/lwnbdsvr/{lwNBD => }/nbd_server.h | 18 +++++-- src/opl.c | 5 +- 14 files changed, 111 insertions(+), 30 deletions(-) rename modules/network/lwnbdsvr/{lwNBD => }/LICENSE (100%) rename modules/network/lwnbdsvr/{lwNBD => }/README.md (85%) rename modules/network/lwnbdsvr/drivers/{atad.c => atad_d.c} (85%) rename modules/network/lwnbdsvr/drivers/{atad.h => atad_d.h} (82%) rename modules/network/lwnbdsvr/{lwNBD => }/nbd-protocol.h (100%) rename modules/network/lwnbdsvr/{lwNBD => }/nbd_opts.h (100%) rename modules/network/lwnbdsvr/{lwNBD => }/nbd_protocol.c (99%) rename modules/network/lwnbdsvr/{lwNBD => }/nbd_server.c (67%) rename modules/network/lwnbdsvr/{lwNBD => }/nbd_server.h (93%) diff --git a/include/config.h b/include/config.h index c37b00796..8ff05a5a9 100644 --- a/include/config.h +++ b/include/config.h @@ -97,19 +97,20 @@ enum CONFIG_INDEX { #define CONFIG_OPL_BOOT_SND_VOLUME "boot_snd_volume" // Network config keys -#define CONFIG_NET_ETH_LINKM "eth_linkmode" -#define CONFIG_NET_PS2_DHCP "ps2_ip_use_dhcp" -#define CONFIG_NET_PS2_IP "ps2_ip_addr" -#define CONFIG_NET_PS2_NETM "ps2_netmask" -#define CONFIG_NET_PS2_GATEW "ps2_gateway" -#define CONFIG_NET_PS2_DNS "ps2_dns" -#define CONFIG_NET_SMB_NB_ADDR "smb_share_nb_addr" -#define CONFIG_NET_SMB_IP_ADDR "smb_ip" -#define CONFIG_NET_SMB_NBNS "smb_share_use_nbns" -#define CONFIG_NET_SMB_SHARE "smb_share" -#define CONFIG_NET_SMB_USER "smb_user" -#define CONFIG_NET_SMB_PASSW "smb_pass" -#define CONFIG_NET_SMB_PORT "smb_port" +#define CONFIG_NET_ETH_LINKM "eth_linkmode" +#define CONFIG_NET_PS2_DHCP "ps2_ip_use_dhcp" +#define CONFIG_NET_PS2_IP "ps2_ip_addr" +#define CONFIG_NET_PS2_NETM "ps2_netmask" +#define CONFIG_NET_PS2_GATEW "ps2_gateway" +#define CONFIG_NET_PS2_DNS "ps2_dns" +#define CONFIG_NET_SMB_NB_ADDR "smb_share_nb_addr" +#define CONFIG_NET_SMB_IP_ADDR "smb_ip" +#define CONFIG_NET_SMB_NBNS "smb_share_use_nbns" +#define CONFIG_NET_SMB_SHARE "smb_share" +#define CONFIG_NET_SMB_USER "smb_user" +#define CONFIG_NET_SMB_PASSW "smb_pass" +#define CONFIG_NET_SMB_PORT "smb_port" +#define CONFIG_NET_NBD_DEFAULT_EXPORT "nbd_default_export" #define CONFIG_KEY_NAME_LEN 32 #define CONFIG_KEY_VALUE_LEN 256 diff --git a/modules/network/lwnbdsvr/lwNBD/LICENSE b/modules/network/lwnbdsvr/LICENSE similarity index 100% rename from modules/network/lwnbdsvr/lwNBD/LICENSE rename to modules/network/lwnbdsvr/LICENSE diff --git a/modules/network/lwnbdsvr/Makefile b/modules/network/lwnbdsvr/Makefile index de31db2a4..58e4aeb9b 100644 --- a/modules/network/lwnbdsvr/Makefile +++ b/modules/network/lwnbdsvr/Makefile @@ -1,5 +1,5 @@ IOP_BIN = lwnbdsvr.irx -IOP_OBJS = lwnbdsvr.o imports.o exports.o drivers/atad.o lwNBD/nbd_server.o lwNBD/nbd_protocol.o +IOP_OBJS = lwnbdsvr.o imports.o exports.o drivers/atad_d.o nbd_server.o nbd_protocol.o IOP_CFLAGS += -DPS2SDK IOP_INCS += -I../../iopcore/common -I../../../include/ diff --git a/modules/network/lwnbdsvr/lwNBD/README.md b/modules/network/lwnbdsvr/README.md similarity index 85% rename from modules/network/lwnbdsvr/lwNBD/README.md rename to modules/network/lwnbdsvr/README.md index 155898acf..82ce1c403 100644 --- a/modules/network/lwnbdsvr/lwNBD/README.md +++ b/modules/network/lwnbdsvr/README.md @@ -6,6 +6,11 @@ * Licence : BSD * Socket API: lwIP 2.0.0 and Linux supported. +Targeting first the use on Playstation 2 IOP, a 37.5 MHz MIPS processor +and 2 MB of RAM, lwNBD is designed to run on bare metal or OS embedded system. +It is developed according to several code standards, including +[Object-Oriented Programming in C](https://github.com/QuantumLeaps/OOP-in-C/). + ## History On Playstation 2, there is no standardized central partition table like GPT for hard disk partitioning, nor is there a standard file system but PFS and HDLoader. In fact, there are few tools capable of handling hard disks, especially under Linux, and the servers developed in the past to handle these disks via the network did not use a standard protocol, which required each software wishing to handle the disks to include a specific client part, which were broken when the toolchain was updated. The same goes for the memory cards and other block devices on this console, which is why I decided to implement NBD on this target first. diff --git a/modules/network/lwnbdsvr/drivers/atad.c b/modules/network/lwnbdsvr/drivers/atad_d.c similarity index 85% rename from modules/network/lwnbdsvr/drivers/atad.c rename to modules/network/lwnbdsvr/drivers/atad_d.c index 5f73e2824..b2fdce0dc 100644 --- a/modules/network/lwnbdsvr/drivers/atad.c +++ b/modules/network/lwnbdsvr/drivers/atad_d.c @@ -1,4 +1,5 @@ -#include "atad.h" +#include +#include "atad_d.h" static inline int atad_read_(nbd_context const *const me, void *buffer, uint64_t offset, uint32_t length) { @@ -25,14 +26,14 @@ int atad_ctor(atad_driver *const me, int device) &atad_write_, &atad_flush_, }; - - me->super.vptr = &vtbl; /* override the vptr */ + nbd_context_ctor(&me->super); /* call the superclass' ctor */ + me->super.vptr = &vtbl; /* override the vptr */ // int ata_device_sce_identify_drive(int device, void *data); strcpy(me->super.export_desc, "PlayStation 2 HDD via ATAD"); sprintf(me->super.export_name, "%s%d", "hdd", me->device); me->super.blocksize = 512; me->super.buffer = nbd_buffer; - me->super.eflags = NBD_FLAG_HAS_FLAGS; + me->super.eflags = NBD_FLAG_HAS_FLAGS | NBD_FLAG_SEND_FLUSH; if (dev_info != NULL && dev_info->exists) { me->super.export_size = (uint64_t)dev_info->total_sectors * me->super.blocksize; diff --git a/modules/network/lwnbdsvr/drivers/atad.h b/modules/network/lwnbdsvr/drivers/atad_d.h similarity index 82% rename from modules/network/lwnbdsvr/drivers/atad.h rename to modules/network/lwnbdsvr/drivers/atad_d.h index 64435cb08..d37fb2a33 100644 --- a/modules/network/lwnbdsvr/drivers/atad.h +++ b/modules/network/lwnbdsvr/drivers/atad_d.h @@ -1,8 +1,7 @@ #ifndef ATAD_DRIVERS_NBD_H #define ATAD_DRIVERS_NBD_H -#include "../irx_imports.h" -#include "../lwNBD/nbd_server.h" +#include "../nbd_server.h" #ifdef __cplusplus extern "C" { diff --git a/modules/network/lwnbdsvr/imports.lst b/modules/network/lwnbdsvr/imports.lst index d5fb7ac16..02d6420cc 100644 --- a/modules/network/lwnbdsvr/imports.lst +++ b/modules/network/lwnbdsvr/imports.lst @@ -26,6 +26,7 @@ thbase_IMPORTS_end sysclib_IMPORTS_start I_sprintf +I_strncmp I_strcpy I_strlen I_memset diff --git a/modules/network/lwnbdsvr/lwnbdsvr.c b/modules/network/lwnbdsvr/lwnbdsvr.c index 9f7752488..c0502496e 100644 --- a/modules/network/lwnbdsvr/lwnbdsvr.c +++ b/modules/network/lwnbdsvr/lwnbdsvr.c @@ -1,7 +1,7 @@ #include "irx_imports.h" -#include "lwNBD/nbd_server.h" -#include "drivers/atad.h" +#include "nbd_server.h" +#include "drivers/atad_d.h" #define MODNAME "lwnbdsvr" IRX_ID(MODNAME, 1, 1); @@ -17,6 +17,12 @@ int _start(int argc, char **argv) iop_thread_t nbd_thread; int ret, successed_exported_ctx = 0; + if (argc > 1) + { + strcpy(gdefaultexport, argv[1]); + printf("lwNBD: default export : %s\n", gdefaultexport); + } + for (int i = 0; i < 2; i++) { ret = atad_ctor(&hdd[i], i); if (ret == 0) { @@ -27,11 +33,11 @@ int _start(int argc, char **argv) nbd_contexts[successed_exported_ctx] = NULL; if (!successed_exported_ctx) { - printf("lwnbdsvr: nothing to export.\n"); + printf("lwNBD: nothing to export.\n"); return -1; } - printf("lwnbdsvr: init nbd_contexts ok.\n"); + printf("lwNBD: init %d exports.\n", successed_exported_ctx); // register exports RegisterLibraryEntries(&_exp_lwnbdsvr); @@ -43,6 +49,7 @@ int _start(int argc, char **argv) nbd_thread.option = 0; nbd_tid = CreateThread(&nbd_thread); + // int StartThreadArgs(int thid, int args, void *argp); StartThread(nbd_tid, (struct nbd_context **)nbd_contexts); return MODULE_RESIDENT_END; } diff --git a/modules/network/lwnbdsvr/lwNBD/nbd-protocol.h b/modules/network/lwnbdsvr/nbd-protocol.h similarity index 100% rename from modules/network/lwnbdsvr/lwNBD/nbd-protocol.h rename to modules/network/lwnbdsvr/nbd-protocol.h diff --git a/modules/network/lwnbdsvr/lwNBD/nbd_opts.h b/modules/network/lwnbdsvr/nbd_opts.h similarity index 100% rename from modules/network/lwnbdsvr/lwNBD/nbd_opts.h rename to modules/network/lwnbdsvr/nbd_opts.h diff --git a/modules/network/lwnbdsvr/lwNBD/nbd_protocol.c b/modules/network/lwnbdsvr/nbd_protocol.c similarity index 99% rename from modules/network/lwnbdsvr/lwNBD/nbd_protocol.c rename to modules/network/lwnbdsvr/nbd_protocol.c index 84205c33b..5a06991ec 100644 --- a/modules/network/lwnbdsvr/lwNBD/nbd_protocol.c +++ b/modules/network/lwnbdsvr/nbd_protocol.c @@ -58,7 +58,9 @@ nbd_context *negotiation_phase(const int client_socket, nbd_context **ctxs) nbd_context **ptr_ctx = ctxs; //temporary workaround - nbd_context const *ctx = ctxs[0]; + nbd_context const *ctx = nbd_context_getDefaultExportByName(ctxs, gdefaultexport); + if (ctx == NULL) + ctx = ctxs[0]; /*** handshake ***/ diff --git a/modules/network/lwnbdsvr/lwNBD/nbd_server.c b/modules/network/lwnbdsvr/nbd_server.c similarity index 67% rename from modules/network/lwnbdsvr/lwNBD/nbd_server.c rename to modules/network/lwnbdsvr/nbd_server.c index 977d773bb..9ab0c63c3 100644 --- a/modules/network/lwnbdsvr/lwNBD/nbd_server.c +++ b/modules/network/lwnbdsvr/nbd_server.c @@ -45,12 +45,62 @@ * This is simple NBD server for the lwIP Socket API. */ +#include #include "nbd_server.h" +char gdefaultexport[32]; + +static int nbd_context_read_(nbd_context const *const me, void *buffer, uint64_t offset, uint32_t length); +static int nbd_context_write_(nbd_context const *const me, void *buffer, uint64_t offset, uint32_t length); +static int nbd_context_flush_(nbd_context const *const me); + +/* constructor */ +void nbd_context_ctor(nbd_context *const me) +{ + static struct nbd_context_Vtbl const vtbl = {/* vtbl of the nbd_context class */ + &nbd_context_read_, + &nbd_context_write_, + &nbd_context_flush_}; + me->vptr = &vtbl; /* "hook" the vptr to the vtbl */ +} + +/* nbd_context class implementations of its virtual functions... */ +static int nbd_context_read_(nbd_context const *const me, void *buffer, uint64_t offset, uint32_t length) +{ + // assert(0); /* purely-virtual function should never be called */ + return 0U; /* to avoid compiler warnings */ +} + +static int nbd_context_write_(nbd_context const *const me, void *buffer, uint64_t offset, uint32_t length) +{ + // assert(0); /* purely-virtual function should never be called */ + return 0U; /* to avoid compiler warnings */ +} + +static int nbd_context_flush_(nbd_context const *const me) +{ + // assert(0); /* purely-virtual function should never be called */ + return 0U; /* to avoid compiler warnings */ +} + +/* search for the default export by name + return NULL if not found any. +*/ +nbd_context *nbd_context_getDefaultExportByName(nbd_context **nbd_contexts, const char *exportname) +{ + nbd_context **ptr_ctx = nbd_contexts; + while (*ptr_ctx) { + if (strncmp((*ptr_ctx)->export_name, exportname, 32) == 0) + break; + ptr_ctx++; + } + return *ptr_ctx; +} + /* * https://lwip.fandom.com/wiki/Receiving_data_with_LWIP */ -int nbd_recv(int s, void *mem, size_t len, int flags) +uint32_t nbd_recv(int s, void *mem, size_t len, int flags) { uint32_t bytesRead = 0; uint32_t left = len; diff --git a/modules/network/lwnbdsvr/lwNBD/nbd_server.h b/modules/network/lwnbdsvr/nbd_server.h similarity index 93% rename from modules/network/lwnbdsvr/lwNBD/nbd_server.h rename to modules/network/lwnbdsvr/nbd_server.h index d33c932b1..28e3ec847 100644 --- a/modules/network/lwnbdsvr/lwNBD/nbd_server.h +++ b/modules/network/lwnbdsvr/nbd_server.h @@ -65,6 +65,8 @@ #include #include #include + +#include //todo: move in .c //TODO : manage endianess #define htonll(x) htobe64(x) #define ntohll(x) be64toh(x) @@ -74,6 +76,11 @@ #include #include + +#define assert(expr) \ + ((expr) || \ + dbgprintf(F_NUM, __LINE__)) + //#include //#include @@ -107,8 +114,6 @@ static inline uint64_t bswap64(uint64_t x) extern "C" { #endif -extern uint8_t nbd_buffer[]; - /** @ingroup nbd * NBD context containing callback functions for NBD transfers * https://github.com/QuantumLeaps/OOP-in-C/blob/master/AN_OOP_in_C.pdf @@ -163,14 +168,16 @@ struct nbd_context_Vtbl int (*flush)(nbd_context const *const me); }; -int nbd_recv(int s, void *mem, size_t len, int flags); +uint32_t nbd_recv(int s, void *mem, size_t len, int flags); int nbd_init(nbd_context **ctx); + // in nbd_protocol.c //todo: const ctxs nbd_context *negotiation_phase(const int client_socket, nbd_context **ctxs); int transmission_phase(const int client_socket, const nbd_context *ctx); +void nbd_context_ctor(nbd_context *const me); static inline int nbd_read(nbd_context const *const me, void *buffer, uint64_t offset, uint32_t length) { return (*me->vptr->read)(me, buffer, offset, length); @@ -186,6 +193,11 @@ static inline int nbd_flush(nbd_context const *const me) return (*me->vptr->flush)(me); } +//Todo: make a real server object +extern char gdefaultexport[]; +extern uint8_t nbd_buffer[]; +nbd_context *nbd_context_getDefaultExportByName(nbd_context **nbd_contexts, const char *exportname); + #ifdef __cplusplus } #endif diff --git a/src/opl.c b/src/opl.c index 922316115..a82bfb817 100644 --- a/src/opl.c +++ b/src/opl.c @@ -191,6 +191,7 @@ unsigned char gDefaultUITextColor[3]; hdl_game_info_t *gAutoLaunchGame; char gOPLPart[128]; char *gHDDPrefix; +char gExportName[32]; void moduleUpdateMenu(int mode, int themeChanged, int langChanged) { @@ -898,6 +899,8 @@ static void _loadConfig() sscanf(temp, "%d.%d.%d.%d", &ps2_gateway[0], &ps2_gateway[1], &ps2_gateway[2], &ps2_gateway[3]); if (configGetStr(configNet, CONFIG_NET_PS2_DNS, &temp)) sscanf(temp, "%d.%d.%d.%d", &ps2_dns[0], &ps2_dns[1], &ps2_dns[2], &ps2_dns[3]); + + configGetStrCopy(configNet, CONFIG_NET_NBD_DEFAULT_EXPORT, gExportName, sizeof(gExportName)); } } @@ -1401,7 +1404,7 @@ static int loadLwnbdSvr(void) if (ret == 0) { ret = sysLoadModuleBuffer(&ps2atad_irx, size_ps2atad_irx, 0, NULL); if (ret >= 0) { - ret = sysLoadModuleBuffer(&lwnbdsvr_irx, size_lwnbdsvr_irx, 0, NULL); + ret = sysLoadModuleBuffer(&lwnbdsvr_irx, size_lwnbdsvr_irx, 4, (char *)&gExportName); if (ret >= 0) ret = 0; }