diff options
author | Luke T. Shumaker <lukeshu@lukeshu.com> | 2024-12-13 18:49:15 -0500 |
---|---|---|
committer | Luke T. Shumaker <lukeshu@lukeshu.com> | 2024-12-13 18:49:15 -0500 |
commit | c578a300c7d0d46662fcd0bdce69af95a821bc18 (patch) | |
tree | a99de333f2812d7c018820f39d78b8c4e744f705 /cmd | |
parent | 6a719c63ecb83a850c317ea94b8932aa0c857770 (diff) | |
parent | 57cc0523f600575feda09bd9fae13f685ce85b0f (diff) |
Merge commit '57cc0523f600575feda09bd9fae13f685ce85b0f'
Diffstat (limited to 'cmd')
-rw-r--r-- | cmd/sbc_harness/CMakeLists.txt | 5 | ||||
-rw-r--r-- | cmd/sbc_harness/main.c | 33 | ||||
-rw-r--r-- | cmd/sbc_harness/usb_keyboard.c | 9 | ||||
-rw-r--r-- | cmd/srv9p/CMakeLists.txt | 35 | ||||
-rw-r--r-- | cmd/srv9p/main.c | 33 | ||||
-rw-r--r-- | cmd/srv9p/static9p.c | 241 | ||||
-rw-r--r-- | cmd/srv9p/static9p.h | 47 |
7 files changed, 76 insertions, 327 deletions
diff --git a/cmd/sbc_harness/CMakeLists.txt b/cmd/sbc_harness/CMakeLists.txt index 96c2a2c..f1b1be2 100644 --- a/cmd/sbc_harness/CMakeLists.txt +++ b/cmd/sbc_harness/CMakeLists.txt @@ -24,6 +24,7 @@ target_link_libraries(sbc_harness_objs libusb libdhcp libhw + lib9p ) pico_minimize_runtime(sbc_harness_objs INCLUDE PRINTF PRINTF_MINIMAL PRINTF_LONG_LONG PRINTF_PTRDIFF_T @@ -38,10 +39,6 @@ add_stack_analysis(sbc_harness_stack.c sbc_harness_objs) # Link ######################################################################### add_executable(sbc_harness) -set_source_files_properties("$<TARGET_OBJECTS:sbc_harness_objs>" PROPERTIES - EXTERNAL_OBJECT true - GENERATED true -) target_sources(sbc_harness PRIVATE sbc_harness_stack.c "$<TARGET_OBJECTS:sbc_harness_objs>" diff --git a/cmd/sbc_harness/main.c b/cmd/sbc_harness/main.c index fb4b696..00f1c4a 100644 --- a/cmd/sbc_harness/main.c +++ b/cmd/sbc_harness/main.c @@ -13,17 +13,19 @@ #include <libhw/rp2040_hwspi.h> #include <libhw/w5500.h> #include <libmisc/hash.h> +#include <libmisc/vcall.h> #include <libusb/usb_common.h> #include <libdhcp/client.h> +#include <lib9p/srv.h> #define LOG_NAME MAIN #include <libmisc/log.h> #include "usb_keyboard.h" -#define ARRAY_LEN(arr) (sizeof(arr)/sizeof((arr)[0])) +#include "config.h" -COROUTINE hello_world_cr(void *_chan) { +static COROUTINE hello_world_cr(void *_chan) { const char *msg = "Hello world!\n"; usb_keyboard_rpc_t *chan = _chan; cr_begin(); @@ -39,7 +41,7 @@ COROUTINE hello_world_cr(void *_chan) { cr_end(); } -COROUTINE dhcp_cr(void *_chip) { +static COROUTINE dhcp_cr(void *_chip) { struct w5500 *chip = _chip; cr_begin(); @@ -53,9 +55,20 @@ struct { struct w5500 dev_w5500; usb_keyboard_rpc_t keyboard_chan; uint16_t usb_serial[sizeof(uint64_t)*2]; /* UTF-16 */ + struct lib9p_srv srv; } globals; +static COROUTINE read9p_cr(void *) { + cr_begin(); + + lib9p_srv_read_cr(&globals.srv, + VCALL(&globals.dev_w5500, tcp_listen, CONFIG_9P_PORT)); + + cr_end(); +} + const char *hexdig = "0123456789ABCDEF"; +static_assert(CONFIG_9P_MAX_REQS*_CONFIG_9P_NUM_SOCKS <= 16); COROUTINE init_cr(void *) { cr_begin(); @@ -91,8 +104,8 @@ COROUTINE init_cr(void *) { flash_id24[0], flash_id24[1], flash_id24[2], }})); - static_assert(sizeof(flash_id64)*2 == ARRAY_LEN(globals.usb_serial)); - for (size_t i = 0; i < ARRAY_LEN(globals.usb_serial); i++) + static_assert(sizeof(flash_id64)*2 == LM_ARRAY_LEN(globals.usb_serial)); + for (size_t i = 0; i < LM_ARRAY_LEN(globals.usb_serial); i++) globals.usb_serial[i] = hexdig[(flash_id64 >> ((sizeof(flash_id64)*8)-((i+1)*4))) & 0xF]; usb_common_earlyinit(globals.usb_serial, sizeof(globals.usb_serial)); usb_keyboard_init(); @@ -103,8 +116,16 @@ COROUTINE init_cr(void *) { /* set up coroutines **************************************************/ coroutine_add("usb_common", usb_common_cr, NULL); coroutine_add("usb_keyboard", usb_keyboard_cr, &globals.keyboard_chan); - //coroutine_add("hello_world", hello_world_cr, &keyboard_chan); + coroutine_add("hello_world", hello_world_cr, &globals.keyboard_chan); coroutine_add_with_stack_size(4*1024, "dhcp", dhcp_cr, &globals.dev_w5500); + for (int i = 0; i < _CONFIG_9P_NUM_SOCKS; i++) { + char name[] = {'r', 'e', 'a', 'd', '-', hexdig[i], '\0'}; + coroutine_add(name, read9p_cr, NULL); + } + for (int i = 0; i < CONFIG_9P_MAX_REQS*_CONFIG_9P_NUM_SOCKS; i++) { + char name[] = {'w', 'r', 'i', 't', 'e', '-', hexdig[i], '\0'}; + coroutine_add(name, lib9p_srv_write_cr, &globals.srv); + } cr_exit(); } diff --git a/cmd/sbc_harness/usb_keyboard.c b/cmd/sbc_harness/usb_keyboard.c index f4816c1..b781d4d 100644 --- a/cmd/sbc_harness/usb_keyboard.c +++ b/cmd/sbc_harness/usb_keyboard.c @@ -8,11 +8,10 @@ #include <libusb/tusb_helpers.h> /* for TUD_ENDPOINT_IN */ #include <libusb/usb_common.h> +#include <libmisc/macro.h> #include "usb_keyboard.h" -#define UNUSED(name) - /** * A USB-HID "Report Descriptor" (see USB-HID 1.11 ยง6.2.2 "Report * Descriptor") describing a keyboard. @@ -108,7 +107,11 @@ uint16_t tud_hid_get_report_cb(uint8_t instance, uint8_t report_id, hid_report_t // Invoked when received SET_REPORT control request or // received data on OUT endpoint ( Report ID = 0, Type = 0 ) -void tud_hid_set_report_cb(uint8_t UNUSED(instance), uint8_t UNUSED(report_id), hid_report_type_t UNUSED(report_type), uint8_t const *UNUSED(buffer), uint16_t UNUSED(bufsize)) +void tud_hid_set_report_cb(uint8_t LM_UNUSED(instance), + uint8_t LM_UNUSED(report_id), + hid_report_type_t LM_UNUSED(report_type), + uint8_t const *LM_UNUSED(buffer), + uint16_t LM_UNUSED(bufsize)) { // TODO not implemented } diff --git a/cmd/srv9p/CMakeLists.txt b/cmd/srv9p/CMakeLists.txt index b747882..0d8e320 100644 --- a/cmd/srv9p/CMakeLists.txt +++ b/cmd/srv9p/CMakeLists.txt @@ -5,21 +5,38 @@ if (PICO_PLATFORM STREQUAL "host") -add_executable(srv9p +# Compile ###################################################################### + +add_library(srv9p_objs OBJECT main.c - static9p.c -) -target_embed_sources(srv9p static.h - static/README.md - static/Documentation/x ) -target_include_directories(srv9p PRIVATE ${CMAKE_CURRENT_BINARY_DIR}) -target_include_directories(srv9p PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/config) -target_link_libraries(srv9p +target_include_directories(srv9p_objs PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/config) +target_include_directories(srv9p_objs PRIVATE ${CMAKE_CURRENT_BINARY_DIR}) +target_link_libraries(srv9p_objs libcr libcr_ipc libmisc lib9p + lib9p_util +) + +# Analyze the stack ############################################################ + +add_stack_analysis(srv9p_stack.c srv9p_objs) + +# Link ######################################################################### + +add_executable(srv9p) +target_sources(srv9p PRIVATE + srv9p_stack.c + "$<TARGET_OBJECTS:srv9p_objs>" +) + +# Embed ######################################################################## + +target_embed_sources(srv9p_objs srv9p static.h + static/README.md + static/Documentation/x ) endif() diff --git a/cmd/srv9p/main.c b/cmd/srv9p/main.c index 05368e5..b5cb122 100644 --- a/cmd/srv9p/main.c +++ b/cmd/srv9p/main.c @@ -11,8 +11,9 @@ #include <libhw/generic/net.h> #include <libhw/generic/alarmclock.h> #include <libhw/host_net.h> +#include <libmisc/macro.h> +#include <util9p/static.h> -#include "static9p.h" #include "static.h" /* configuration **************************************************************/ @@ -25,12 +26,10 @@ /* implementation *************************************************************/ -#define UNUSED(name) - /* file tree ******************************************************************/ -#define FILE_COMMON(NAME) { \ - .vtable = &static_file_vtable, \ +#define FILE_COMMON(NAME) { \ + .vtable = &util9p_static_file_vtable, \ \ .u_name = "root", .u_num = 0, /* owner user */ \ .g_name = "root", .g_num = 0, /* owner group */ \ @@ -43,8 +42,8 @@ .mtime = 1728337904, \ } -#define DIR_COMMON(NAME) { \ - .vtable = &static_dir_vtable, \ +#define DIR_COMMON(NAME) { \ + .vtable = &util9p_static_dir_vtable, \ \ .u_name = "root", .u_num = 0, /* owner user */ \ .g_name = "root", .g_num = 0, /* owner group */ \ @@ -57,18 +56,18 @@ .mtime = 1728337904, \ } -#define STATIC_FILE(STRNAME, SYMNAME) \ - &((static struct static_file){ \ - ._static_common = FILE_COMMON(STRNAME), \ - .data_start = _binary_static_##SYMNAME##_start, \ - .data_end = _binary_static_##SYMNAME##_end, \ +#define STATIC_FILE(STRNAME, SYMNAME) \ + &((static struct util9p_static_file){ \ + ._util9p_static_common = FILE_COMMON(STRNAME), \ + .data_start = _binary_static_##SYMNAME##_start, \ + .data_end = _binary_static_##SYMNAME##_end, \ }) -static struct static_dir root = { - ._static_common = DIR_COMMON(""), +static struct util9p_static_dir root = { + ._util9p_static_common = DIR_COMMON(""), .members = { - &((static struct static_dir){ - ._static_common = DIR_COMMON("Documentation"), + &((static struct util9p_static_dir){ + ._util9p_static_common = DIR_COMMON("Documentation"), .members = { STATIC_FILE("x", Documentation_x), NULL @@ -79,7 +78,7 @@ static struct static_dir root = { }, }; -static implements_lib9p_srv_file *get_root(struct lib9p_srv_ctx *UNUSED(ctx), char *UNUSED(treename)) { +static implements_lib9p_srv_file *get_root(struct lib9p_srv_ctx *LM_UNUSED(ctx), char *LM_UNUSED(treename)) { return &root; } diff --git a/cmd/srv9p/static9p.c b/cmd/srv9p/static9p.c deleted file mode 100644 index b5b18e0..0000000 --- a/cmd/srv9p/static9p.c +++ /dev/null @@ -1,241 +0,0 @@ -/* srv9p/static9p.c - Serve static files over 9P - * - * Copyright (C) 2024 Luke T. Shumaker <lukeshu@lukeshu.com> - * SPDX-License-Identifier: AGPL-3.0-or-later - */ - -#include <libmisc/assert.h> -#include <libmisc/vcall.h> - -#include "static9p.h" - -#define UNUSED(name) -#define p9_str(cstr) ((struct lib9p_s){ .len = strlen(cstr), .utf8 = cstr }) -#define p9_nulstr ((struct lib9p_s){ .len = 0, .utf8 = NULL }) - -/* common *********************************************************************/ - -static implements_lib9p_srv_file *static_common_clone(implements_lib9p_srv_file *_self, struct lib9p_srv_ctx *ctx) { - _static_common *self = VCALL_SELF(_static_common, implements_lib9p_srv_file, _self); - assert(self); - assert(ctx); - - return self; -} - -static void static_common_free(implements_lib9p_srv_file *_self, struct lib9p_srv_ctx *ctx) { - _static_common *self = VCALL_SELF(_static_common, implements_lib9p_srv_file, _self); - assert(self); - assert(ctx); - - /* do nothing */ -} - -static uint32_t static_common_io(implements_lib9p_srv_file *_self, struct lib9p_srv_ctx *ctx, lib9p_o_t UNUSED(flags)) { - _static_common *self = VCALL_SELF(_static_common, implements_lib9p_srv_file, _self); - assert(self); - assert(ctx); - - return 0; -} - -static void static_common_wstat(implements_lib9p_srv_file *_self, struct lib9p_srv_ctx *ctx, - struct lib9p_stat UNUSED(new)) { - _static_common *self = VCALL_SELF(_static_common, implements_lib9p_srv_file, _self); - assert(self); - assert(ctx); - - lib9p_error(&ctx->basectx, LINUX_EROFS, "read-only part of filesystem"); -} - -static void static_common_remove(implements_lib9p_srv_file *_self, struct lib9p_srv_ctx *ctx) { - _static_common *self = VCALL_SELF(_static_common, implements_lib9p_srv_file, _self); - assert(self); - assert(ctx); - - lib9p_error(&ctx->basectx, LINUX_EROFS, "read-only part of filesystem"); -} - -/* dir ************************************************************************/ - -static struct lib9p_stat static_dir_stat(implements_lib9p_srv_file *_self, struct lib9p_srv_ctx *ctx) { - struct static_dir *self = VCALL_SELF(struct static_dir, implements_lib9p_srv_file, _self); - assert(self); - assert(ctx); - - return (struct lib9p_stat){ - .kern_type = 0, - .kern_dev = 0, - .file_qid = { - .type = LIB9P_QT_DIR, - .vers = 1, - .path = self->pathnum, - }, - .file_mode = LIB9P_DM_DIR | (self->perm & 0555), - .file_atime = self->atime, - .file_mtime = self->mtime, - .file_size = 0, - .file_name = p9_str(self->name), - .file_owner_uid = p9_str(self->u_name), - .file_owner_gid = p9_str(self->g_name), - .file_last_modified_uid = p9_str(self->m_name), - .file_extension = p9_nulstr, - .file_owner_n_uid = self->u_num, - .file_owner_n_gid = self->g_num, - .file_last_modified_n_uid = self->m_num, - }; -} - -static implements_lib9p_srv_file *static_dir_dopen(implements_lib9p_srv_file *_self, struct lib9p_srv_ctx *ctx, - char *childname) { - struct static_dir *self = VCALL_SELF(struct static_dir, implements_lib9p_srv_file, _self); - assert(self); - assert(ctx); - - for (size_t i = 0; self->members[i]; i++) { - implements_lib9p_srv_file *filep = self->members[i]; - struct lib9p_stat stat = VCALL(filep, stat, ctx); - if (lib9p_ctx_has_error(&ctx->basectx)) - break; - lib9p_assert_stat(stat); - if (strcmp(stat.file_name.utf8, childname) == 0) - return filep; - } - lib9p_error(&ctx->basectx, - LINUX_ENOENT, "no such file or directory"); - return NULL; -} - -static implements_lib9p_srv_file *static_dir_dcreate(implements_lib9p_srv_file *_self, struct lib9p_srv_ctx *ctx, - char *UNUSED(childname), - lib9p_dm_t UNUSED(perm), lib9p_o_t UNUSED(flags)) { - struct static_dir *self = VCALL_SELF(struct static_dir, implements_lib9p_srv_file, _self); - assert(self); - assert(ctx); - - lib9p_error(&ctx->basectx, LINUX_EROFS, "read-only part of filesystem"); - return NULL; -} - -static size_t static_dir_dread(implements_lib9p_srv_file *_self, struct lib9p_srv_ctx *ctx, - uint8_t *buf, - uint32_t byte_count, - size_t _obj_offset) { - struct static_dir *self = VCALL_SELF(struct static_dir, implements_lib9p_srv_file, _self); - assert(self); - assert(ctx); - - uint32_t byte_offset = 0; - size_t obj_offset = _obj_offset; - while (self->members[obj_offset]) { - implements_lib9p_srv_file *filep = self->members[obj_offset]; - struct lib9p_stat stat = VCALL(filep, stat, ctx); - if (lib9p_ctx_has_error(&ctx->basectx)) - break; - lib9p_assert_stat(stat); - uint32_t nbytes = lib9p_marshal_stat(&ctx->basectx, byte_count-byte_offset, &stat, - &buf[byte_offset]); - if (!nbytes) { - if (obj_offset == _obj_offset) - lib9p_error(&ctx->basectx, - LINUX_ERANGE, "stat object does not fit into negotiated max message size"); - break; - } - byte_offset += nbytes; - obj_offset++; - } - return obj_offset - _obj_offset; -} - -struct lib9p_srv_file_vtable static_dir_vtable = { - .clone = static_common_clone, - .free = static_common_free, - - .io = static_common_io, - .stat = static_dir_stat, - .wstat = static_common_wstat, - .remove = static_common_remove, - - .dopen = static_dir_dopen, - .dcreate = static_dir_dcreate, - - .dread = static_dir_dread, -}; - -/* file ***********************************************************************/ - -static inline size_t static_file_size(struct static_file *file) { - assert(file); - -#if __unix__ - assert(file->data_start); -#endif - if (!file->data_end) - return file->data_size; - return (size_t)((uintptr_t)file->data_end - (uintptr_t)file->data_start); -} - -static struct lib9p_stat static_file_stat(implements_lib9p_srv_file *_self, struct lib9p_srv_ctx *ctx) { - struct static_file *self = VCALL_SELF(struct static_file, implements_lib9p_srv_file, _self); - assert(self); - assert(ctx); - - return (struct lib9p_stat){ - .kern_type = 0, - .kern_dev = 0, - .file_qid = { - .type = LIB9P_QT_FILE, - .vers = 1, - .path = self->pathnum, - }, - .file_mode = self->perm & 0444, - .file_atime = self->atime, - .file_mtime = self->mtime, - .file_size = (uint64_t)static_file_size(self), - .file_name = p9_str(self->name), - .file_owner_uid = p9_str(self->u_name), - .file_owner_gid = p9_str(self->g_name), - .file_last_modified_uid = p9_str(self->m_name), - .file_extension = p9_nulstr, - .file_owner_n_uid = self->u_num, - .file_owner_n_gid = self->g_num, - .file_last_modified_n_uid = self->m_num, - }; -} - -static uint32_t static_file_pread(implements_lib9p_srv_file *_self, struct lib9p_srv_ctx *ctx, - void *buf, - uint32_t byte_count, - uint64_t byte_offset) { - struct static_file *self = VCALL_SELF(struct static_file, implements_lib9p_srv_file, _self); - assert(self); - assert(ctx); - - size_t data_size = static_file_size(self); - - if (byte_offset > (uint64_t)data_size) { - lib9p_error(&ctx->basectx, - LINUX_EINVAL, "offset is past end-of-file length"); - return 0; - } - - size_t beg_off = (size_t)byte_offset; - size_t end_off = beg_off + (size_t)byte_count; - if (end_off > data_size) - end_off = data_size; - memcpy(buf, &self->data_start[beg_off], end_off-beg_off); - return (uint32_t)(end_off-beg_off); -} - -struct lib9p_srv_file_vtable static_file_vtable = { - .clone = static_common_clone, - .free = static_common_free, - - .io = static_common_io, - .stat = static_file_stat, - .wstat = static_common_wstat, - .remove = static_common_remove, - - .pread = static_file_pread, - .pwrite = NULL, -}; diff --git a/cmd/srv9p/static9p.h b/cmd/srv9p/static9p.h deleted file mode 100644 index 7a3d476..0000000 --- a/cmd/srv9p/static9p.h +++ /dev/null @@ -1,47 +0,0 @@ -/* srv9p/static9p.h - Serve static files over 9P - * - * Copyright (C) 2024 Luke T. Shumaker <lukeshu@lukeshu.com> - * SPDX-License-Identifier: AGPL-3.0-or-later - */ - -#ifndef _SRV9P_STATIC9P_H_ -#define _SRV9P_STATIC9P_H_ - -#include <lib9p/srv.h> - -typedef struct { - implements_lib9p_srv_file; - - char *u_name; - uint32_t u_num; - char *g_name; - uint32_t g_num; - char *m_name; - uint32_t m_num; - - uint64_t pathnum; - char *name; - lib9p_dm_t perm; - uint32_t atime, mtime; -} _static_common; - -struct static_dir { - _static_common; - - /* NULL-terminated */ - implements_lib9p_srv_file *members[]; -}; - - -struct static_file { - _static_common; - - char *data_start; /* must not be NULL */ - char *data_end; /* may be NULL, in which case data_size is used */ - size_t data_size; /* only used if .data_end==NULL */ -}; - -extern struct lib9p_srv_file_vtable static_dir_vtable; -extern struct lib9p_srv_file_vtable static_file_vtable; - -#endif /* _SRV9P_STATIC9P_H_ */ |