From f485102d2619cdc4757a3ea74cc63a4206c51dfe Mon Sep 17 00:00:00 2001 From: logicog Date: Sun, 16 Nov 2025 20:12:41 +0100 Subject: [PATCH] Add fetching of configuration file and current cmd history --- httpd/httpd.c | 53 ++++++++++++++++++++++++++++++++++++----------- httpd/page_impl.c | 52 ++++++++++++++++++++++++++++++++++++++++++++++ httpd/page_impl.h | 2 ++ 3 files changed, 95 insertions(+), 12 deletions(-) diff --git a/httpd/httpd.c b/httpd/httpd.c index d8deeb6..346e59d 100644 --- a/httpd/httpd.c +++ b/httpd/httpd.c @@ -42,7 +42,8 @@ __xdata uint8_t *content_type = 0; // Global variables holding POST state __xdata uint16_t bindex; // Current index into the boundary - +__xdata uint8_t verify_crc; +__xdata uint32_t max_upload; __xdata uint16_t short_parsed; #define TSTATE_NONE 0 @@ -232,14 +233,21 @@ uint8_t stream_upload(uint16_t bptr) uptr += write_len; write_len = 0; // TODO: This is a bit premature, what about a nice web-page saying the device will reset??? - print_string("CRC16: "); print_short(crc_final); write_char('\n'); - if (crc_final == 0xb001) { - print_string("Checksum OK."); - } else { - print_string("Checksum incorrect!"); + if (verify_crc) { + print_string("CRC16: "); print_short(crc_final); write_char('\n'); + if (crc_final == 0xb001) { + print_string("Checksum OK."); + } else { + print_string("Checksum incorrect!"); + } + print_string("Upload to flash done, will reset!\n"); + reset_chip(); } - print_string("Upload to flash done, will reset!\n"); - reset_chip(); + // Make sure there is a 0 at the end of the uploaded data + flash_buf[0] = 0; + flash_region.addr = uptr; + flash_region.len = 1; + flash_write_bytes(flash_buf); if (bptr >= uip_len) return 0; return 1; @@ -309,8 +317,8 @@ void handle_post(void) cmd_buffer[i] = '\0'; if (i) cmd_available = 1; - } else if (is_word(request_path, "upload")) { - print_string("POST upload request\n"); + } else if (is_word(request_path, "upload") || is_word(request_path, "config")) { + print_string("POST upload/config request\n"); if (!boundary[0]) { print_string("Bad request, no boundary!\n"); send_bad_request(); @@ -330,7 +338,18 @@ void handle_post(void) print_string("Have content octets\n"); p += 4; // Skip \r\n\r\n sequence at end of preamble of part - uptr = FIRMWARE_UPLOAD_START; + if (is_word(request_path, "upload")) { + uptr = FIRMWARE_UPLOAD_START; + verify_crc = 1; + max_upload = 1024576; + } else { + print_string("Configuration upload, erasing config mem!\n"); + uptr = CONFIG_START; + verify_crc = 0; + max_upload = 2048; + flash_region.addr = CONFIG_START; + flash_sector_erase(); + } crc_value = 0; bindex = 0; write_len = 0; @@ -407,7 +426,13 @@ void httpd_appcall(void) s->tstate = TSTATE_TX; } } else if (uip_newdata() && s->tstate == TSTATE_POST) { - stream_upload(0); + // Check here maxupload by subtracting uip_len and close socekt if fails! + if (max_upload - uip_len > 0) { + stream_upload(0); + } else { + send_bad_request(); + goto do_send; + } } else if (uip_newdata() && s->tstate != TSTATE_TX) { cont_len = 0; write_char('<'); print_short(uip_len); write_char('\n'); @@ -456,6 +481,10 @@ void httpd_appcall(void) send_eee(); } else if (is_word(q, "/mirror.json")) { send_mirror(); + } else if (is_word(q, "/config")) { + send_config(); + } else if (is_word(q, "/cmd_log")) { + send_cmd_log(); } else { send_not_found(); } diff --git a/httpd/page_impl.c b/httpd/page_impl.c index b787d4a..efedfb7 100644 --- a/httpd/page_impl.c +++ b/httpd/page_impl.c @@ -4,6 +4,7 @@ #include "../rtl837x_common.h" #include "../rtl837x_regs.h" #include "../rtl837x_port.h" +#include "../rtl837x_flash.h" #include "uip.h" #include "../html_data.h" #include @@ -15,6 +16,8 @@ extern __xdata uint8_t outbuf[TCP_OUTBUF_SIZE]; extern __xdata uint16_t slen; +extern __xdata uint16_t cont_len; +extern __xdata uint32_t cont_addr; extern __code uint8_t * __code hex; extern __xdata uip_ipaddr_t uip_hostaddr, uip_draddr, uip_netmask; extern __code struct uip_eth_addr uip_ethaddr; @@ -30,7 +33,13 @@ extern __xdata uint8_t isRTL8373; extern __xdata uint8_t sfp_pins_last; extern __xdata uint8_t vlan_names[VLAN_NAMES_SIZE]; +extern __xdata uint8_t cmd_history[CMD_HISTORY_SIZE]; +extern __xdata uint16_t cmd_history_ptr; + +extern __xdata struct flash_region_t flash_region; + __code uint8_t * __code HTTP_RESPONCE_JSON = "HTTP/1.1 200 OK\r\nContent-Type: application/json\r\n\r\n"; +__code uint8_t * __code HTTP_RESPONCE_TXT = "HTTP/1.1 200 OK\r\nContent-Type: text/plain\r\n\r\n"; /* Convert only the lower nibble to ascii HEX char. For convenience the upper nibble is masked out. @@ -371,3 +380,46 @@ void send_status(void) char_to_html(']'); } } + + +void send_config(void) +{ + print_string("send_config called\n"); + __xdata uint32_t pos = CONFIG_START; // 70000 , 6c000 / 0xc000 = 9 + + extern __xdata uint16_t len_left = CONFIG_LEN; + slen = strtox(outbuf, HTTP_RESPONCE_TXT); + while (read_flash((CONFIG_START-CODE0_SIZE) / CODE_BANK_SIZE + 1, + (__code uint8_t *) (((CONFIG_START + len_left - CODE0_SIZE) % CODE_BANK_SIZE) + CODE0_SIZE + len_left)) == 0xff) { + print_short(len_left); + len_left--; + } + len_left++; + + if (len_left > (TCP_OUTBUF_SIZE - slen)) { + cont_len = len_left - (TCP_OUTBUF_SIZE - slen); + len_left = TCP_OUTBUF_SIZE - slen; + cont_addr = len_left; + } + flash_region.addr = CONFIG_START; + flash_region.len = len_left; + flash_read_bulk(outbuf + slen); + slen += len_left; +} + +void send_cmd_log(void) +{ + print_string("send_cmd_log called\n"); + slen = strtox(outbuf, HTTP_RESPONCE_TXT); + __xdata uint16_t p = (cmd_history_ptr + 1) & CMD_HISTORY_MASK; + __xdata uint8_t found_begin = 0; + print_string("History ptr: "); + print_short(cmd_history_ptr); write_char('\n'); + while (p != cmd_history_ptr) { + if (!cmd_history[p] || cmd_history[p] == '\n') + found_begin = 1; + if (found_begin && cmd_history[p]) + outbuf[slen++] = cmd_history[p]; + p = (p + 1) & CMD_HISTORY_MASK; + } +} diff --git a/httpd/page_impl.h b/httpd/page_impl.h index 9692989..455b276 100644 --- a/httpd/page_impl.h +++ b/httpd/page_impl.h @@ -7,5 +7,7 @@ void send_vlan(register uint16_t vlan); void send_basic_info(void); void send_eee(void); void send_mirror(void); +void send_config(void); +void send_cmd_log(void); #endif