Skip to content

Commit

Permalink
NBD: add gdefaultexport config option
Browse files Browse the repository at this point in the history
  • Loading branch information
bignaux committed Sep 5, 2021
1 parent 0265d2a commit 5b82898
Show file tree
Hide file tree
Showing 15 changed files with 111 additions and 31 deletions.
2 changes: 1 addition & 1 deletion .clang-format-ignore
@@ -1 +1 @@
./modules/network/lwnbdsvr/lwNBD/nbd-protocol.h
./modules/network/lwnbdsvr/nbd-protocol.h
27 changes: 14 additions & 13 deletions include/config.h
Expand Up @@ -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
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion 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/
Expand Down
Expand Up @@ -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.
Expand Down
@@ -1,4 +1,5 @@
#include "atad.h"
#include <atad.h>
#include "atad_d.h"

static inline int atad_read_(nbd_context const *const me, void *buffer, uint64_t offset, uint32_t length)
{
Expand All @@ -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;
Expand Down
@@ -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" {
Expand Down
1 change: 1 addition & 0 deletions modules/network/lwnbdsvr/imports.lst
Expand Up @@ -26,6 +26,7 @@ thbase_IMPORTS_end

sysclib_IMPORTS_start
I_sprintf
I_strncmp
I_strcpy
I_strlen
I_memset
Expand Down
14 changes: 10 additions & 4 deletions 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);
Expand All @@ -17,6 +17,11 @@ 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) {
Expand All @@ -27,11 +32,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);
Expand All @@ -43,6 +48,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;
}
Expand Down
File renamed without changes.
File renamed without changes.
Expand Up @@ -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 ***/

Expand Down
Expand Up @@ -45,12 +45,62 @@
* This is simple NBD server for the lwIP Socket API.
*/

#include <string.h>
#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;
Expand Down
Expand Up @@ -65,6 +65,8 @@
#include <netinet/in.h>
#include <endian.h>
#include <unistd.h>

#include <assert.h> //todo: move in .c
//TODO : manage endianess
#define htonll(x) htobe64(x)
#define ntohll(x) be64toh(x)
Expand All @@ -74,6 +76,11 @@
#include <ps2ip.h>
#include <sysclib.h>


#define assert(expr) \
((expr) || \
dbgprintf(F_NUM, __LINE__))

//#include <errno.h>
//#include <malloc.h>

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand All @@ -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
Expand Down
5 changes: 4 additions & 1 deletion src/opl.c
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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));
}
}

Expand Down Expand Up @@ -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;
}
Expand Down

0 comments on commit 5b82898

Please sign in to comment.