|
| 1 | +/* |
| 2 | + * MIT License |
| 3 | + * Copyright (c) 2026 VIFEX |
| 4 | + * |
| 5 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 6 | + * of this software and associated documentation files (the "Software"), to deal |
| 7 | + * in the Software without restriction, including without limitation the rights |
| 8 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 9 | + * copies of the Software, and to permit persons to whom the Software is |
| 10 | + * furnished to do so, subject to the following conditions: |
| 11 | + * |
| 12 | + * The above copyright notice and this permission notice shall be included in |
| 13 | + * all copies or substantial portions of the Software. |
| 14 | + * |
| 15 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 18 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 20 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 21 | + * SOFTWARE. |
| 22 | + */ |
| 23 | + |
| 24 | +/** |
| 25 | + * @file fl_cmd.h |
| 26 | + * @brief Internal header shared by fl_cmd_*.c command modules |
| 27 | + */ |
| 28 | + |
| 29 | +#ifndef FL_CMD_H |
| 30 | +#define FL_CMD_H |
| 31 | + |
| 32 | +#ifdef __cplusplus |
| 33 | +extern "C" { |
| 34 | +#endif |
| 35 | + |
| 36 | +#include "fl.h" |
| 37 | +#include "fl_codec.h" |
| 38 | +#include "fl_error.h" |
| 39 | +#include "fl_log.h" |
| 40 | + |
| 41 | +/** |
| 42 | + * @brief Parsed command arguments (shared by all handlers) |
| 43 | + */ |
| 44 | +typedef struct { |
| 45 | + const char* cmd; |
| 46 | + const char* data; |
| 47 | + uintptr_t addr; |
| 48 | + uintptr_t orig; |
| 49 | + uintptr_t target; |
| 50 | + int crc; /* -1 = no CRC provided */ |
| 51 | + int len; |
| 52 | + int size; |
| 53 | + int comp; |
| 54 | + int all; |
| 55 | + int enable; /* -1 = not specified, 0 = disable, 1 = enable */ |
| 56 | + int force; |
| 57 | + const char* path; |
| 58 | + const char* newpath; |
| 59 | + const char* mode; |
| 60 | +} cmd_args_t; |
| 61 | + |
| 62 | +/** |
| 63 | + * @brief Command handler function pointer |
| 64 | + * @return FL_OK or negative fl_error_t |
| 65 | + */ |
| 66 | +typedef fl_error_t (*cmd_handler_t)(fl_context_t* ctx, const cmd_args_t* args); |
| 67 | + |
| 68 | +/** |
| 69 | + * @brief Verify args->crc against a pre-computed CRC value |
| 70 | + * @param crc args->crc (-1 = skip verification) |
| 71 | + * @param calc Expected CRC computed by caller |
| 72 | + * @return true if CRC matches or not provided; false on mismatch (error response sent) |
| 73 | + */ |
| 74 | +bool fl_verify_crc(int crc, uint16_t calc); |
| 75 | + |
| 76 | +/** |
| 77 | + * @brief Flush data cache for a memory region |
| 78 | + */ |
| 79 | +void fl_flush_dcache(fl_context_t* ctx, const void* addr, size_t len); |
| 80 | + |
| 81 | +/** |
| 82 | + * @brief Check if [addr, addr+len) is a safe memory range |
| 83 | + * @return true if the range is safe to access |
| 84 | + */ |
| 85 | +bool fl_check_addr_range(uintptr_t addr, size_t len); |
| 86 | + |
| 87 | +/* =========================== |
| 88 | + Command handler declarations |
| 89 | + =========================== */ |
| 90 | + |
| 91 | +/* Core commands (fl_cmd_core.c) */ |
| 92 | +fl_error_t fl_cmd_ping(fl_context_t* ctx, const cmd_args_t* args); |
| 93 | +fl_error_t fl_cmd_echo(fl_context_t* ctx, const cmd_args_t* args); |
| 94 | +fl_error_t fl_cmd_echoback(fl_context_t* ctx, const cmd_args_t* args); |
| 95 | +fl_error_t fl_cmd_info(fl_context_t* ctx, const cmd_args_t* args); |
| 96 | +fl_error_t fl_cmd_hello(fl_context_t* ctx, const cmd_args_t* args); |
| 97 | + |
| 98 | +/* Memory commands (fl_cmd_mem.c) */ |
| 99 | +fl_error_t fl_cmd_alloc(fl_context_t* ctx, const cmd_args_t* args); |
| 100 | +fl_error_t fl_cmd_upload(fl_context_t* ctx, const cmd_args_t* args); |
| 101 | +fl_error_t fl_cmd_read(fl_context_t* ctx, const cmd_args_t* args); |
| 102 | +fl_error_t fl_cmd_write(fl_context_t* ctx, const cmd_args_t* args); |
| 103 | + |
| 104 | +/* Patch commands (fl_cmd_patch.c) */ |
| 105 | +void fl_cmd_print_fpb_info(fl_context_t* ctx); |
| 106 | +fl_error_t fl_cmd_patch(fl_context_t* ctx, const cmd_args_t* args); |
| 107 | +fl_error_t fl_cmd_tpatch(fl_context_t* ctx, const cmd_args_t* args); |
| 108 | +fl_error_t fl_cmd_dpatch(fl_context_t* ctx, const cmd_args_t* args); |
| 109 | +fl_error_t fl_cmd_unpatch(fl_context_t* ctx, const cmd_args_t* args); |
| 110 | +fl_error_t fl_cmd_enable(fl_context_t* ctx, const cmd_args_t* args); |
| 111 | + |
| 112 | +/* File commands (fl_cmd_file.c) */ |
| 113 | +#if FL_USE_FILE |
| 114 | +fl_error_t fl_cmd_fopen(fl_context_t* ctx, const cmd_args_t* args); |
| 115 | +fl_error_t fl_cmd_fwrite(fl_context_t* ctx, const cmd_args_t* args); |
| 116 | +fl_error_t fl_cmd_fread(fl_context_t* ctx, const cmd_args_t* args); |
| 117 | +fl_error_t fl_cmd_fclose(fl_context_t* ctx, const cmd_args_t* args); |
| 118 | +fl_error_t fl_cmd_fcrc(fl_context_t* ctx, const cmd_args_t* args); |
| 119 | +fl_error_t fl_cmd_fseek(fl_context_t* ctx, const cmd_args_t* args); |
| 120 | +fl_error_t fl_cmd_fstat(fl_context_t* ctx, const cmd_args_t* args); |
| 121 | +fl_error_t fl_cmd_flist(fl_context_t* ctx, const cmd_args_t* args); |
| 122 | +fl_error_t fl_cmd_fremove(fl_context_t* ctx, const cmd_args_t* args); |
| 123 | +fl_error_t fl_cmd_fmkdir(fl_context_t* ctx, const cmd_args_t* args); |
| 124 | +fl_error_t fl_cmd_frename(fl_context_t* ctx, const cmd_args_t* args); |
| 125 | +#endif |
| 126 | + |
| 127 | +#ifdef __cplusplus |
| 128 | +} |
| 129 | +#endif |
| 130 | + |
| 131 | +#endif /* FL_CMD_H */ |
0 commit comments