diff --git a/modules/network/lwnbdsvr/drivers/atad.c b/modules/network/lwnbdsvr/drivers/atad.c index f94978e44..5f73e2824 100644 --- a/modules/network/lwnbdsvr/drivers/atad.c +++ b/modules/network/lwnbdsvr/drivers/atad.c @@ -29,7 +29,7 @@ int atad_ctor(atad_driver *const me, int device) 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"); - strcpy(me->super.export_name, "hdd0"); + 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; diff --git a/modules/network/lwnbdsvr/imports.lst b/modules/network/lwnbdsvr/imports.lst index c27158f54..d5fb7ac16 100644 --- a/modules/network/lwnbdsvr/imports.lst +++ b/modules/network/lwnbdsvr/imports.lst @@ -25,6 +25,7 @@ I_DeleteThread thbase_IMPORTS_end sysclib_IMPORTS_start +I_sprintf I_strcpy I_strlen I_memset diff --git a/modules/network/lwnbdsvr/lwNBD/nbd_protocol.c b/modules/network/lwnbdsvr/lwNBD/nbd_protocol.c index 831fa141a..84205c33b 100644 --- a/modules/network/lwnbdsvr/lwNBD/nbd_protocol.c +++ b/modules/network/lwnbdsvr/lwNBD/nbd_protocol.c @@ -58,7 +58,7 @@ nbd_context *negotiation_phase(const int client_socket, nbd_context **ctxs) nbd_context **ptr_ctx = ctxs; //temporary workaround - nbd_context *ctx = ctxs[0]; + nbd_context const *ctx = ctxs[0]; /*** handshake ***/ @@ -99,7 +99,21 @@ nbd_context *negotiation_phase(const int client_socket, nbd_context **ctxs) nbd_buffer[new_opt.optlen] = '\0'; } - printf("%d\n", new_opt.option); +#ifdef DEBUG + static const char *NBD_OPTIONS[] = { + NULL, + "NBD_OPT_EXPORT_NAME", + "NBD_OPT_ABORT", + "NBD_OPT_LIST", + "NBD_OPT_STARTTLS", + "NBD_OPT_INFO", + "NBD_OPT_GO", + "NBD_OPT_STRUCTURED_REPLY", + "NBD_OPT_LIST_META_CONTEXT", + "NBD_OPT_SET_META_CONTEXT", + }; + printf("lwNBD: %s\n", NBD_OPTIONS[new_opt.option]); +#endif switch (new_opt.option) { @@ -144,6 +158,7 @@ nbd_context *negotiation_phase(const int client_socket, nbd_context **ctxs) ptr_ctx++; } fixed_new_option_reply.reply = htonl(NBD_REP_ACK); + fixed_new_option_reply.replylen = 0; size = send(client_socket, &fixed_new_option_reply, sizeof(struct nbd_fixed_new_option_reply), 0); break; @@ -175,7 +190,7 @@ nbd_context *negotiation_phase(const int client_socket, nbd_context **ctxs) } abort: - return ctx; + return (nbd_context *)ctx; soft_disconnect: error: return NULL; @@ -203,7 +218,7 @@ int transmission_phase(const int client_socket, const nbd_context *ctx) // TODO : blocking here if no proper NBD_CMD_DISC, bad threading design ? size = nbd_recv(client_socket, &request, sizeof(struct nbd_request), 0); if (size < sizeof(struct nbd_request)) { - printf("lwNBD : sizeof NOK\n"); + printf("lwNBD: sizeof NOK\n"); goto error; } @@ -211,7 +226,7 @@ int transmission_phase(const int client_socket, const nbd_context *ctx) request.magic = ntohl(request.magic); if (request.magic != NBD_REQUEST_MAGIC) { - printf("lwNBD : wrong NBD_REQUEST_MAGIC\n"); + printf("lwNBD: wrong NBD_REQUEST_MAGIC\n"); goto error; } @@ -222,7 +237,19 @@ int transmission_phase(const int client_socket, const nbd_context *ctx) reply.handle = request.handle; - // printf("lwNBD: entering NBD_CMD %d.\n", request.type); +#ifdef DEBUG + static const char *NBD_CMD[] = { + "NBD_CMD_READ", + "NBD_CMD_WRITE", + "NBD_CMD_DISC", + "NBD_CMD_FLUSH", + "NBD_CMD_TRIM", + "NBD_CMD_CACHE", + "NBD_CMD_WRITE_ZEROES", + "NBD_CMD_BLOCK_STATUS", + }; + printf("lwNBD: %s\n", NBD_CMD[request.type]); +#endif switch (request.type) { diff --git a/modules/network/lwnbdsvr/lwNBD/nbd_server.h b/modules/network/lwnbdsvr/lwNBD/nbd_server.h index a71e67819..d33c932b1 100644 --- a/modules/network/lwnbdsvr/lwNBD/nbd_server.h +++ b/modules/network/lwnbdsvr/lwNBD/nbd_server.h @@ -52,6 +52,14 @@ //#include "lwip/pbuf.h" //#include "lwip/mem.h" +#ifdef DEBUG +#define dbgprintf(args...) printf(args) +#else +#define dbgprintf(args...) \ + do { \ + } while (0) +#endif + #ifdef __linux__ #include #include @@ -73,14 +81,6 @@ // #define send(a, b, c, d) lwip_send(a, b, c, d) #define close(x) lwip_close(x) -#ifdef DEBUG -#define dbgprintf(args...) printf(args) -#else -#define dbgprintf(args...) \ - do { \ - } while (0) -#endif - //TODO: Missing in PS2SDK // pickup from https://gist.github.com/jtbr/7a43e6281e6cca353b33ee501421860c static inline uint64_t bswap64(uint64_t x) diff --git a/modules/network/lwnbdsvr/lwnbdsvr.c b/modules/network/lwnbdsvr/lwnbdsvr.c index e1d26b111..9f7752488 100644 --- a/modules/network/lwnbdsvr/lwnbdsvr.c +++ b/modules/network/lwnbdsvr/lwnbdsvr.c @@ -9,20 +9,22 @@ static int nbd_tid; extern struct irx_export_table _exp_lwnbdsvr; //need to be global to be accessible from thread -atad_driver hdd; -nbd_context *nbd_contexts[] = { - &hdd.super, - NULL, -}; +atad_driver hdd[2]; // could have 2 ATA disks +nbd_context *nbd_contexts[10]; int _start(int argc, char **argv) { iop_thread_t nbd_thread; int ret, successed_exported_ctx = 0; - ret = atad_ctor(&hdd, 0); - if (ret == 0) - successed_exported_ctx = 1; + for (int i = 0; i < 2; i++) { + ret = atad_ctor(&hdd[i], i); + if (ret == 0) { + nbd_contexts[successed_exported_ctx] = &hdd[i].super; + successed_exported_ctx++; + } + } + nbd_contexts[successed_exported_ctx] = NULL; if (!successed_exported_ctx) { printf("lwnbdsvr: nothing to export.\n");