From: Matt Fleming <matt.fleming at linux.intel.com> These patches are based on the elflink branch. This set of patches is my attempt at moving the command-line interface functionality out of the core and into an ELF module to reduce the size of the core. The most interesting patch is [PATCH 4/4] which moves the cli code out of core/elflink and into com32/elflink/modules. [PATCH 4/4] is an RFC as I'd really like to start a discussion on whether the approach I've taken to shrinking the size of the core is OK. All comments appreciated! Matt Fleming (4): gitignore: Add modules.dep elflink: Warn about unresolved symbols when creating modules.dep sys/times.h: Mark times() static inline elflink: Move cli lib code out of core .gitignore | 1 + com32/elflink/modules/Makefile | 12 +- com32/elflink/modules/cli.c | 437 +++++++++++++++++++++++++++++++++++++++ com32/elflink/modules/cli.h | 6 +- com32/elflink/modules/cmdline.c | 34 +++ com32/elflink/modules/ui.c | 56 +++++ com32/include/sys/times.h | 8 +- com32/lib/sys/times.c | 42 ---- core/elflink/abort_new.c | 10 + core/elflink/cli.c | 410 ------------------------------------ core/elflink/cli.h | 23 -- core/elflink/execute.c | 4 +- core/elflink/get_key.c | 175 ---------------- core/elflink/getkey.h | 80 ------- core/elflink/load_env32.c | 50 +----- elf_gen_dep.sh | 22 ++- 16 files changed, 584 insertions(+), 786 deletions(-) create mode 100644 com32/elflink/modules/cli.c create mode 100644 com32/elflink/modules/cmdline.c create mode 100644 com32/elflink/modules/ui.c delete mode 100644 com32/lib/sys/times.c delete mode 100644 core/elflink/cli.c delete mode 100644 core/elflink/cli.h delete mode 100644 core/elflink/get_key.c delete mode 100644 core/elflink/getkey.h -- 1.7.4
From: Matt Fleming <matt.fleming at linux.intel.com> This file is generated at build time and contains a list of modules and their dependencies. Signed-off-by: Matt Fleming <matt.fleming at linux.intel.com> --- .gitignore | 1 + 1 files changed, 1 insertions(+), 0 deletions(-) diff --git a/.gitignore b/.gitignore index aa9a70e..512a270 100644 --- a/.gitignore +++ b/.gitignore @@ -47,3 +47,4 @@ *GPATH *GRTAGS *GTAGS +modules.dep -- 1.7.4
Matt Fleming
2011-Feb-17 22:03 UTC
[syslinux] [PATCH 2/4] elflink: Warn about unresolved symbols when creating modules.dep
From: Matt Fleming <matt.fleming at linux.intel.com> If, after running elf_gen_dep.sh, there are still some symbols that are not provided by any of the modules that we built, issue a warning and list the unresolved symbols. Signed-off-by: Matt Fleming <matt.fleming at linux.intel.com> --- elf_gen_dep.sh | 22 +++++++++++++++++++--- 1 files changed, 19 insertions(+), 3 deletions(-) diff --git a/elf_gen_dep.sh b/elf_gen_dep.sh index 306034a..abdeb41 100755 --- a/elf_gen_dep.sh +++ b/elf_gen_dep.sh @@ -71,9 +71,20 @@ resolve_sym () while read symbol do - #echo $symbol - sed -n -e "s/^$symbol <\(.*\)>/\1/p" all.txt >> resolve.tmp - #grep $symbol all.txt + # If no one provides the symbol we're trying to + # resolve then add it to the list of unresolved + # symbols. + grep -q $symbol all.txt + if [ $? -ne 0 ]; then + # We only need to add the symbol once + if [[ ! "$unresolved_symbols" =~ "$symbol" ]]; then + unresolved_symbols="$unresolved_symbols $symbol" + fi + else + #echo $symbol + sed -n -e "s/^$symbol <\(.*\)>/\1/p" all.txt >> resolve.tmp + #grep $symbol all.txt + fi done rm_cr < resolve.tmp @@ -108,4 +119,9 @@ done rm *.txt rm *.ext rm *.int + +if [ "$unresolved_symbols" ]; then + echo "WARNING: These symbols could not be resolved:" $unresolved_symbols +fi + echo ELF modules dependency is bult up, pls check modules.dep! -- 1.7.4
Matt Fleming
2011-Feb-17 22:03 UTC
[syslinux] [PATCH 3/4] sys/times.h: Mark times() static inline
From: Matt Fleming <matt.fleming at linux.intel.com> As times() is just an accessor function for accessing __ms_timer it's worth marking it as static inline so that we don't incur any function call overhead just for accessing a symbol. Signed-off-by: Matt Fleming <matt.fleming at linux.intel.com> --- com32/include/sys/times.h | 8 +++++++- com32/lib/sys/times.c | 42 ------------------------------------------ 2 files changed, 7 insertions(+), 43 deletions(-) delete mode 100644 com32/lib/sys/times.c diff --git a/com32/include/sys/times.h b/com32/include/sys/times.h index 9047006..5eda295 100644 --- a/com32/include/sys/times.h +++ b/com32/include/sys/times.h @@ -16,6 +16,12 @@ struct tms { typedef uint32_t clock_t; -clock_t times(struct tms *); +extern volatile uint32_t __ms_timer; + +static inline clock_t times(struct tms *buf) +{ + (void)buf; + return __ms_timer; +} #endif /* _SYS_TIMES_H */ diff --git a/com32/lib/sys/times.c b/com32/lib/sys/times.c deleted file mode 100644 index 518ba82..0000000 --- a/com32/lib/sys/times.c +++ /dev/null @@ -1,42 +0,0 @@ -/* ----------------------------------------------------------------------- * - * - * Copyright 2004-2008 H. Peter Anvin - All Rights Reserved - * - * Permission is hereby granted, free of charge, to any person - * obtaining a copy of this software and associated documentation - * files (the "Software"), to deal in the Software without - * restriction, including without limitation the rights to use, - * copy, modify, merge, publish, distribute, sublicense, and/or - * sell copies of the Software, and to permit persons to whom - * the Software is furnished to do so, subject to the following - * conditions: - * - * The above copyright notice and this permission notice shall - * be included in all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, - * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES - * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND - * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT - * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, - * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING - * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR - * OTHER DEALINGS IN THE SOFTWARE. - * - * ----------------------------------------------------------------------- */ - -/* - * sys/times.c - * - * Returns something like a clock. - */ - -#include <sys/times.h> -#include <syslinux/pmapi.h> -#include <com32.h> - -clock_t times(struct tms * buf) -{ - (void)buf; - return __ms_timer; -} -- 1.7.4
Matt Fleming
2011-Feb-17 22:03 UTC
[syslinux] [RFC][PATCH 4/4] elflink: Move cli lib code out of core
From: Matt Fleming <matt.fleming at linux.intel.com> The cli code does not belong in the core, so move it out into an ELF module. Now, we can share the cli library code among different modules, namely the new cmdline.c32 and ui.c32 modules. This change also provides a good example of how the core can load modules on demand. For instance, as the core still needs to drop the user to a command prompt sometimes (like if we're aborting) we can execute the new cmdline.c32 module. Below are some measurements of the size of ldlinux.elf before and after this change. Before: text data bss dec hex filename 161328 10439 913468 1085235 108f33 core/ldlinux.elf After: text data bss dec hex filename 156279 10439 911412 1078130 107372 core/ldlinux.elf There are plenty of other pieces of code that could be moved into a module such as the config parser and kernel loader. So further reductions in core size are definitely possible. Signed-off-by: Matt Fleming <matt.fleming at linux.intel.com> --- com32/elflink/modules/Makefile | 12 +- com32/elflink/modules/cli.c | 437 +++++++++++++++++++++++++++++++++++++++ com32/elflink/modules/cli.h | 6 +- com32/elflink/modules/cmdline.c | 34 +++ com32/elflink/modules/ui.c | 56 +++++ core/elflink/abort_new.c | 10 + core/elflink/cli.c | 410 ------------------------------------ core/elflink/cli.h | 23 -- core/elflink/execute.c | 4 +- core/elflink/get_key.c | 175 ---------------- core/elflink/getkey.h | 80 ------- core/elflink/load_env32.c | 50 +----- 12 files changed, 557 insertions(+), 740 deletions(-) create mode 100644 com32/elflink/modules/cli.c create mode 100644 com32/elflink/modules/cmdline.c create mode 100644 com32/elflink/modules/ui.c delete mode 100644 core/elflink/cli.c delete mode 100644 core/elflink/cli.h delete mode 100644 core/elflink/get_key.c delete mode 100644 core/elflink/getkey.h diff --git a/com32/elflink/modules/Makefile b/com32/elflink/modules/Makefile index 9c9cb97..da3867c 100644 --- a/com32/elflink/modules/Makefile +++ b/com32/elflink/modules/Makefile @@ -15,7 +15,8 @@ include MCONFIG MODULES = hello.c32 sort.c32 mytest.c32 menumain.c32 printmsg.c32 background.c32 passwd.c32 sha1hash.c32 \ unbase64.c32 sha512crypt.c32 md5.c32 crypt-md5.c32 sha256crypt.c32 get_key.c32 ansiraw.c32 test.c32 \ - meminfo.c32 menu.c32 drain.c32 dir.c32 pcitest.c32 vesainfo.c32 cpuid.c32 cpuidtest.c32 + meminfo.c32 menu.c32 drain.c32 dir.c32 pcitest.c32 vesainfo.c32 cpuid.c32 cpuidtest.c32 cli.c32 \ + cmdline.c32 ui.c32 TESTFILES @@ -93,6 +94,15 @@ cpuid.c32 : cpuid.o cpuidtest.c32 : cpuidtest.o $(LD) $(LDFLAGS) -o $@ $^ +cli.c32 : cli.o + $(LD) $(LDFLAGS) -o $@ $^ + +cmdline.c32 : cmdline.o + $(LD) $(LDFLAGS) -o $@ $^ + +ui.c32 : ui.o + $(LD) $(LDFLAGS) -o $@ $^ + tidy dist: rm -f *.o *.lo *.a *.lst .*.d diff --git a/com32/elflink/modules/cli.c b/com32/elflink/modules/cli.c new file mode 100644 index 0000000..ccd60b3 --- /dev/null +++ b/com32/elflink/modules/cli.c @@ -0,0 +1,437 @@ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <console.h> +#include <com32.h> +#include <syslinux/adv.h> +#include <syslinux/config.h> +#include <setjmp.h> +#include <netinet/in.h> +#include <limits.h> +#include <minmax.h> +#include <linux/list.h> +#include <sys/exec.h> +#include <sys/module.h> + +#include "getkey.h" +#include "menu.h" +#include "cli.h" + +static jmp_buf timeout_jump; + +static struct list_head cli_history_head; + +void clear_screen(void) +{ + //mp("enter"); + fputs("\033e\033%@\033)0\033(B\1#0\033[?25l\033[2J", stdout); +} + +int mygetkey(clock_t timeout) +{ + clock_t t0, t; + clock_t tto, to; + int key; + + //mp("enter"); + if (!totaltimeout) + return get_key(stdin, timeout); + + for (;;) { + tto = min(totaltimeout, INT_MAX); + to = timeout ? min(tto, timeout) : tto; + + t0 = 0; + key = get_key(stdin, to); + t = 0 - t0; + + if (totaltimeout <= t) + longjmp(timeout_jump, 1); + + totaltimeout -= t; + + if (key != KEY_NONE) { + //mp("get key 0x%x", key); + return key; + } + + if (timeout) { + if (timeout <= t) { + //mp("timeout"); + return KEY_NONE; + } + + timeout -= t; + } + } +} + +const char *edit_cmdline(const char *input, int top /*, int width */ , + int (*pDraw_Menu) (int, int, int), + void (*show_fkey) (int)) +{ + static char cmdline[MAX_CMDLINE_LEN]; + char temp_cmdline[MAX_CMDLINE_LEN] = { }; + int key, len, prev_len, cursor; + int redraw = 1; /* We enter with the menu already drawn */ + int x, y; + bool done = false; + const char *ret; + int width = 0; + struct cli_command *comm_counter; + comm_counter + list_entry(cli_history_head.next->prev, typeof(*comm_counter), list); + + if (!width) { + int height; + if (getscreensize(1, &height, &width)) + width = 80; + } + + strncpy(cmdline, input, MAX_CMDLINE_LEN); + cmdline[MAX_CMDLINE_LEN - 1] = '\0'; + + len = cursor = strlen(cmdline); + prev_len = 0; + x = y = 0; + + while (!done) { + if (redraw > 1 && pDraw_Menu != NULL) { + /* Clear and redraw whole screen */ + /* Enable ASCII on G0 and DEC VT on G1; do it in this order + to avoid confusing the Linux console */ + /* clear_screen(); + draw_menu(-1, top, 1); */ + clear_screen(); + (*pDraw_Menu) (-1, top, 1); + prev_len = 0; + // printf("\033[0m\033[2J\033[H"); + } + + if (redraw > 0) { + int dy, at; + + prev_len = max(len, prev_len); + + /* Redraw the command line */ + printf("\033[?7l\033[?25l"); + if (y) + printf("\033[%dA", y); + printf("\033[1G\033[1;36m> \033[0m"); + + x = 2; + y = 0; + at = 0; + while (at < prev_len) { + putchar(at >= len ? ' ' : cmdline[at]); + at++; + x++; + if (x >= width) { + printf("\r\n"); + x = 0; + y++; + } + } + printf("\033[K\r"); + + dy = y - (cursor + 2) / width; + x = (cursor + 2) % width; + + if (dy) { + printf("\033[%dA", dy); + y -= dy; + } + if (x) + printf("\033[%dC", x); + printf("\033[?25h"); + prev_len = len; + redraw = 0; + } + + key = mygetkey(0); + + switch (key) { + case KEY_CTRL('L'): + redraw = 2; + break; + + case KEY_ENTER: + case KEY_CTRL('J'): + ret = cmdline; + done = true; + break; + + case KEY_ESC: + case KEY_CTRL('C'): + ret = NULL; + done = true; + break; + + case KEY_BACKSPACE: + case KEY_DEL: + if (cursor) { + memmove(cmdline + cursor - 1, cmdline + cursor, + len - cursor + 1); + len--; + cursor--; + redraw = 1; + } + break; + + case KEY_CTRL('D'): + case KEY_DELETE: + if (cursor < len) { + memmove(cmdline + cursor, cmdline + cursor + 1, len - cursor); + len--; + redraw = 1; + } + break; + + case KEY_CTRL('U'): + if (len) { + len = cursor = 0; + cmdline[len] = '\0'; + redraw = 1; + } + break; + + case KEY_CTRL('W'): + if (cursor) { + int prevcursor = cursor; + + while (cursor && my_isspace(cmdline[cursor - 1])) + cursor--; + + while (cursor && !my_isspace(cmdline[cursor - 1])) + cursor--; + +#if 0 + memmove(cmdline + cursor, cmdline + prevcursor, + len - prevcursor + 1); +#else + { + int i; + char *q = cmdline + cursor; + char *p = cmdline + prevcursor; + for (i = 0; i < len - prevcursor + 1; i++) + *q++ = *p++; + } +#endif + len -= (prevcursor - cursor); + redraw = 1; + } + break; + + case KEY_LEFT: + case KEY_CTRL('B'): + if (cursor) { + cursor--; + redraw = 1; + } + break; + + case KEY_RIGHT: + case KEY_CTRL('F'): + if (cursor < len) { + putchar(cmdline[cursor]); + cursor++; + x++; + if (x >= width) { + printf("\r\n"); + y++; + x = 0; + } + } + break; + + case KEY_CTRL('K'): + if (cursor < len) { + cmdline[len = cursor] = '\0'; + redraw = 1; + } + break; + + case KEY_HOME: + case KEY_CTRL('A'): + if (cursor) { + cursor = 0; + redraw = 1; + } + break; + + case KEY_END: + case KEY_CTRL('E'): + if (cursor != len) { + cursor = len; + redraw = 1; + } + break; + + case KEY_F1: + case KEY_F2: + case KEY_F3: + case KEY_F4: + case KEY_F5: + case KEY_F6: + case KEY_F7: + case KEY_F8: + case KEY_F9: + case KEY_F10: + case KEY_F11: + case KEY_F12: + if (show_fkey != NULL) { + (*show_fkey) (key); + redraw = 1; + } + break; + case KEY_UP: + { + if (!list_empty(&cli_history_head)) { + comm_counter + list_entry(comm_counter->list.next, + typeof(*comm_counter), list); + if (&comm_counter->list == &cli_history_head) { + strcpy(cmdline, temp_cmdline); + } else { + strcpy(cmdline, comm_counter->command); + } + cursor = len = strlen(cmdline); + redraw = 1; + } + } + break; + case KEY_DOWN: + { + if (!list_empty(&cli_history_head)) { + comm_counter + list_entry(comm_counter->list.prev, + typeof(*comm_counter), list); + if (&comm_counter->list == &cli_history_head) { + strcpy(cmdline, temp_cmdline); + } else { + strcpy(cmdline, comm_counter->command); + } + cursor = len = strlen(cmdline); + redraw = 1; + } + } + break; + default: + if (key >= ' ' && key <= 0xFF && len < MAX_CMDLINE_LEN - 1) { + if (cursor == len) { + temp_cmdline[len] = key; + cmdline[len++] = key; + temp_cmdline[len] = cmdline[len] = '\0'; + putchar(key); + cursor++; + x++; + if (x >= width) { + printf("\r\n\033[K"); + y++; + x = 0; + } + prev_len++; + } else { + memmove(cmdline + cursor + 1, cmdline + cursor, + len - cursor + 1); + memmove(temp_cmdline + cursor + 1, temp_cmdline + cursor, + len - cursor + 1); + temp_cmdline[cursor] = key; + cmdline[cursor++] = key; + len++; + redraw = 1; + } + } + break; + } + } + + printf("\033[?7h"); + return ret; +} + +void process_command(const char *cmd, bool history) +{ + char **argv = malloc((MAX_COMMAND_ARGS + 1) * sizeof(char *)); + char *temp_cmd = (char *)malloc(sizeof(char) * (strlen(cmd) + 1)); + int argc = 1, len_mn; + char *crt_arg, *module_name; + + /* return if user only press enter */ + if (cmd[0] == '\0') { + printf("\n"); + return; + } + printf("\n"); + + if (history) { + struct cli_command *comm; + + comm = (struct cli_command *)malloc(sizeof(struct cli_command *)); + comm->command + (char *)malloc(sizeof(char) * (strlen(cmd) + 1)); + strcpy(comm->command, cmd); + list_add(&(comm->list), &cli_history_head); + } + + // mp("raw cmd = %s", cmd); + strcpy(temp_cmd, cmd); + module_name = strtok(cmd, COMMAND_DELIM); + len_mn = strlen(module_name); + + if (!strcmp(module_name + len_mn - 4, ".c32")) { + if (module_find(module_name) != NULL) { + /* make module re-enterable */ + // mp("Module %s is already running"); + } + do { + argv[0] = module_name; + crt_arg = strtok(NULL, COMMAND_DELIM); + if (crt_arg != NULL && strlen(crt_arg) > 0) { + argv[argc] = crt_arg; + argc++; + } else + break; + } while (argc < MAX_COMMAND_ARGS); + argv[argc] = NULL; + module_load_dependencies(module_name, MODULES_DEP); + spawn_load(module_name, argv); + } else if (!strcmp(module_name + len_mn - 2, ".0")) { + execute(cmd, KT_PXE); + } else if (!strcmp(module_name + len_mn - 3, ".bs")) { + } else if (!strcmp(module_name + len_mn - 4, ".img")) { + execute(cmd, KT_FDIMAGE); + } else if (!strcmp(module_name + len_mn - 4, ".bin")) { + } else if (!strcmp(module_name + len_mn - 4, ".bss")) { + execute(cmd, KT_BSS); + } else if (!strcmp(module_name + len_mn - 4, ".com") + || !strcmp(module_name + len_mn - 4, ".cbt")) { + execute(cmd, KT_COMBOOT); + } else if (!strcmp(module_name + len_mn - 4, ".cfg") + || !strcmp(module_name + len_mn - 5, ".conf") + || !strcmp(module_name + len_mn - 7, ".config")) { + execute(module_name, KT_CONFIG); + } + /* use KT_KERNEL as default */ + else + execute(temp_cmd, KT_KERNEL); + +cleanup: + free(argv); + free(temp_cmd); +} + +static int cli_init(void) +{ + INIT_LIST_HEAD(&cli_history_head); + + return 0; +} + +static void cli_exit(void) +{ + /* Nothing to do */ +} + +MODULE_INIT(cli_init); +MODULE_EXIT(cli_exit); diff --git a/com32/elflink/modules/cli.h b/com32/elflink/modules/cli.h index c452643..4ae9caf 100644 --- a/com32/elflink/modules/cli.h +++ b/com32/elflink/modules/cli.h @@ -2,18 +2,20 @@ #define CLI_H #define MAX_CMD_HISTORY 64 +#define COMMAND_DELIM " \t\n" // Whitespace delimiters +#define MAX_COMMAND_ARGS 40 struct cli_command { struct list_head list; char *command; }; -struct list_head cli_history_head; - extern void clear_screen(void); extern int mygetkey(clock_t timeout); extern const char *edit_cmdline(const char *input, int top /*, int width */ , int (*pDraw_Menu) (int, int, int), void (*show_fkey) (int)); +extern void process_command(const char *cmd, bool history); +extern struct menu *root_menu, *start_menu, *hide_menu, *menu_list, *default_menu; #endif diff --git a/com32/elflink/modules/cmdline.c b/com32/elflink/modules/cmdline.c new file mode 100644 index 0000000..d8c7603 --- /dev/null +++ b/com32/elflink/modules/cmdline.c @@ -0,0 +1,34 @@ +#include <linux/list.h> +#include <sys/times.h> +#include <stdbool.h> +#include "cli.h" +#include "console.h" + +#include <sys/module.h> + +static void enter_cmdline(void) +{ + struct cli_command *aux; + char *cmdline; + + /* Enter endless command line prompt, should support "exit" */ + while (1) { + cmdline = edit_cmdline("", 1, NULL, NULL); + /* feng: give up the aux check here */ + //aux = list_entry(cli_history_head.next, typeof(*aux), list); + //if (strcmp(aux->command, cmdline)) { + process_command(cmdline, true); + //} + } +} + +static int cmdline_main(int argc, char **argv) +{ + openconsole(&dev_rawcon_r, &dev_ansiserial_w); + + /* Should never return */ + enter_cmdline(); + + return 0; +} +MODULE_MAIN(cmdline_main); diff --git a/com32/elflink/modules/ui.c b/com32/elflink/modules/ui.c new file mode 100644 index 0000000..9c8ec37 --- /dev/null +++ b/com32/elflink/modules/ui.c @@ -0,0 +1,56 @@ +#include <linux/list.h> +#include <sys/times.h> +#include <stdbool.h> +#include "cli.h" +#include "menu.h" +#include "console.h" + +#include <sys/module.h> + +static void enter_cmdline(void) +{ + struct cli_command *aux; + char *cmdline; + + /* Enter endless command line prompt, should support "exit" */ + while (1) { + cmdline = edit_cmdline("", 1, NULL, NULL); + /* feng: give up the aux check here */ + //aux = list_entry(cli_history_head.next, typeof(*aux), list); + //if (strcmp(aux->command, cmdline)) { + process_command(cmdline, true); + //} + } +} + +/* parameter is the config file name if any */ +static void start_ui(char *config_file) +{ + char *cmdline; + char *argv[2] = {config_file, NULL}; + + // mp("enter, config file = %s", config_file); + + parse_configs(argv); + + /* run the default menu if found */ + if (default_menu) { + cmdline = default_menu->menu_entries[default_menu->defentry]->cmdline; + if (*cmdline == '.') { + while (*cmdline++ != ' '); + } + process_command(cmdline, false); + } + + /* Should never return */ + enter_cmdline(); +} + +static int ui_main(int argc, char **argv) +{ + openconsole(&dev_rawcon_r, &dev_ansiserial_w); + start_ui(argv); + + return 0; +} +MODULE_MAIN(ui_main); diff --git a/core/elflink/abort_new.c b/core/elflink/abort_new.c index 39ba226..11df18b 100644 --- a/core/elflink/abort_new.c +++ b/core/elflink/abort_new.c @@ -4,6 +4,8 @@ #include <string.h> #include <console.h> +#include <sys/exec.h> + //#include <syslinux/loadfile.h> //#include <syslinux/linux.h> //#include <syslinux/pxe.h> @@ -12,6 +14,14 @@ #include "core-elf.h" #include "menu.h" +void enter_cmdline(void) +{ + char *params = NULL; + + module_load_dependencies("cmdline.c32", "modules.dep"); + spawn_load("cmdline.c32", ¶ms); +} + void abort_load_new(com32sys_t *reg) { char *str; diff --git a/core/elflink/cli.c b/core/elflink/cli.c deleted file mode 100644 index f029ae4..0000000 --- a/core/elflink/cli.c +++ /dev/null @@ -1,410 +0,0 @@ -#include <stdio.h> -#include <stdlib.h> -#include <string.h> -#include <console.h> -#include <com32.h> -#include <syslinux/adv.h> -#include <syslinux/config.h> -#include <setjmp.h> -#include <netinet/in.h> -#include <limits.h> -#include <minmax.h> -#include <linux/list.h> -#include <sys/exec.h> -#include <sys/module.h> -#include <core.h> - -#include "getkey.h" -#include "common.h" -#include "menu.h" -#include "cli.h" - -void clear_screen(void) -{ - mp("enter"); - fputs("\033e\033%@\033)0\033(B\1#0\033[?25l\033[2J", stdout); -} - -int mygetkey(clock_t timeout) -{ - clock_t t0, t; - clock_t tto, to; - int key; - - //mp("enter"); - if (!totaltimeout) - return get_key(stdin, timeout); - - for (;;) { - tto = min(totaltimeout, INT_MAX); - to = timeout ? min(tto, timeout) : tto; - - t0 = jiffies(); - key = get_key(stdin, to); - t = jiffies() - t0; - - if (totaltimeout <= t) - longjmp(timeout_jump, 1); - - totaltimeout -= t; - - if (key != KEY_NONE) { - //mp("get key 0x%x", key); - return key; - } - - if (timeout) { - if (timeout <= t) { - //mp("timeout"); - return KEY_NONE; - } - - timeout -= t; - } - } -} - -const char *edit_cmdline(const char *input, int top /*, int width */ , - int (*pDraw_Menu) (int, int, int), - void (*show_fkey) (int)) -{ - static char cmdline[MAX_CMDLINE_LEN]; - char temp_cmdline[MAX_CMDLINE_LEN] = { }; - int key, len, prev_len, cursor; - int redraw = 1; /* We enter with the menu already drawn */ - int x, y; - bool done = false; - const char *ret; - int width = 0; - struct cli_command *comm_counter; - comm_counter - list_entry(cli_history_head.next->prev, typeof(*comm_counter), list); - - if (!width) { - int height; - if (getscreensize(1, &height, &width)) - width = 80; - } - - strncpy(cmdline, input, MAX_CMDLINE_LEN); - cmdline[MAX_CMDLINE_LEN - 1] = '\0'; - - len = cursor = strlen(cmdline); - prev_len = 0; - x = y = 0; - - while (!done) { - if (redraw > 1 && pDraw_Menu != NULL) { - /* Clear and redraw whole screen */ - /* Enable ASCII on G0 and DEC VT on G1; do it in this order - to avoid confusing the Linux console */ - /* clear_screen(); - draw_menu(-1, top, 1); */ - clear_screen(); - (*pDraw_Menu) (-1, top, 1); - prev_len = 0; - // printf("\033[0m\033[2J\033[H"); - } - - if (redraw > 0) { - int dy, at; - - prev_len = max(len, prev_len); - - /* Redraw the command line */ - printf("\033[?7l\033[?25l"); - if (y) - printf("\033[%dA", y); - printf("\033[1G\033[1;36m> \033[0m"); - - x = 2; - y = 0; - at = 0; - while (at < prev_len) { - putchar(at >= len ? ' ' : cmdline[at]); - at++; - x++; - if (x >= width) { - printf("\r\n"); - x = 0; - y++; - } - } - printf("\033[K\r"); - - dy = y - (cursor + 2) / width; - x = (cursor + 2) % width; - - if (dy) { - printf("\033[%dA", dy); - y -= dy; - } - if (x) - printf("\033[%dC", x); - printf("\033[?25h"); - prev_len = len; - redraw = 0; - } - - key = mygetkey(0); - - switch (key) { - case KEY_CTRL('L'): - redraw = 2; - break; - - case KEY_ENTER: - case KEY_CTRL('J'): - ret = cmdline; - done = true; - break; - - case KEY_ESC: - case KEY_CTRL('C'): - ret = NULL; - done = true; - break; - - case KEY_BACKSPACE: - case KEY_DEL: - if (cursor) { - memmove(cmdline + cursor - 1, cmdline + cursor, - len - cursor + 1); - len--; - cursor--; - redraw = 1; - } - break; - - case KEY_CTRL('D'): - case KEY_DELETE: - if (cursor < len) { - memmove(cmdline + cursor, cmdline + cursor + 1, len - cursor); - len--; - redraw = 1; - } - break; - - case KEY_CTRL('U'): - if (len) { - len = cursor = 0; - cmdline[len] = '\0'; - redraw = 1; - } - break; - - case KEY_CTRL('W'): - if (cursor) { - int prevcursor = cursor; - - while (cursor && my_isspace(cmdline[cursor - 1])) - cursor--; - - while (cursor && !my_isspace(cmdline[cursor - 1])) - cursor--; - -#if 0 - memmove(cmdline + cursor, cmdline + prevcursor, - len - prevcursor + 1); -#else - { - int i; - char *q = cmdline + cursor; - char *p = cmdline + prevcursor; - for (i = 0; i < len - prevcursor + 1; i++) - *q++ = *p++; - } -#endif - len -= (prevcursor - cursor); - redraw = 1; - } - break; - - case KEY_LEFT: - case KEY_CTRL('B'): - if (cursor) { - cursor--; - redraw = 1; - } - break; - - case KEY_RIGHT: - case KEY_CTRL('F'): - if (cursor < len) { - putchar(cmdline[cursor]); - cursor++; - x++; - if (x >= width) { - printf("\r\n"); - y++; - x = 0; - } - } - break; - - case KEY_CTRL('K'): - if (cursor < len) { - cmdline[len = cursor] = '\0'; - redraw = 1; - } - break; - - case KEY_HOME: - case KEY_CTRL('A'): - if (cursor) { - cursor = 0; - redraw = 1; - } - break; - - case KEY_END: - case KEY_CTRL('E'): - if (cursor != len) { - cursor = len; - redraw = 1; - } - break; - - case KEY_F1: - case KEY_F2: - case KEY_F3: - case KEY_F4: - case KEY_F5: - case KEY_F6: - case KEY_F7: - case KEY_F8: - case KEY_F9: - case KEY_F10: - case KEY_F11: - case KEY_F12: - if (show_fkey != NULL) { - (*show_fkey) (key); - redraw = 1; - } - break; - case KEY_UP: - { - if (!list_empty(&cli_history_head)) { - comm_counter - list_entry(comm_counter->list.next, - typeof(*comm_counter), list); - if (&comm_counter->list == &cli_history_head) { - strcpy(cmdline, temp_cmdline); - } else { - strcpy(cmdline, comm_counter->command); - } - cursor = len = strlen(cmdline); - redraw = 1; - } - } - break; - case KEY_DOWN: - { - if (!list_empty(&cli_history_head)) { - comm_counter - list_entry(comm_counter->list.prev, - typeof(*comm_counter), list); - if (&comm_counter->list == &cli_history_head) { - strcpy(cmdline, temp_cmdline); - } else { - strcpy(cmdline, comm_counter->command); - } - cursor = len = strlen(cmdline); - redraw = 1; - } - } - break; - default: - if (key >= ' ' && key <= 0xFF && len < MAX_CMDLINE_LEN - 1) { - if (cursor == len) { - temp_cmdline[len] = key; - cmdline[len++] = key; - temp_cmdline[len] = cmdline[len] = '\0'; - putchar(key); - cursor++; - x++; - if (x >= width) { - printf("\r\n\033[K"); - y++; - x = 0; - } - prev_len++; - } else { - memmove(cmdline + cursor + 1, cmdline + cursor, - len - cursor + 1); - memmove(temp_cmdline + cursor + 1, temp_cmdline + cursor, - len - cursor + 1); - temp_cmdline[cursor] = key; - cmdline[cursor++] = key; - len++; - redraw = 1; - } - } - break; - } - } - - printf("\033[?7h"); - return ret; -} - -void process_command(const char *cmd) -{ - char **argv = malloc((MAX_COMMAND_ARGS + 1) * sizeof(char *)); - char *temp_cmd = (char *)malloc(sizeof(char) * (strlen(cmd) + 1)); - int argc = 1, len_mn; - char *crt_arg, *module_name; - - /* return if user only press enter */ - if (cmd[0] == '\0') { - printf("\n"); - return; - } - printf("\n"); - - mp("raw cmd = %s", cmd); - strcpy(temp_cmd, cmd); - module_name = strtok(cmd, COMMAND_DELIM); - len_mn = strlen(module_name); - - if (!strcmp(module_name + len_mn - 4, ".c32")) { - if (module_find(module_name) != NULL) { - /* make module re-enterable */ - mp("Module %s is already running"); - } - do { - argv[0] = module_name; - crt_arg = strtok(NULL, COMMAND_DELIM); - if (crt_arg != NULL && strlen(crt_arg) > 0) { - argv[argc] = crt_arg; - argc++; - } else - break; - } while (argc < MAX_COMMAND_ARGS); - argv[argc] = NULL; - module_load_dependencies(module_name, MODULES_DEP); - spawn_load(module_name, argv); - } else if (!strcmp(module_name + len_mn - 2, ".0")) { - execute(cmd, KT_PXE); - } else if (!strcmp(module_name + len_mn - 3, ".bs")) { - } else if (!strcmp(module_name + len_mn - 4, ".img")) { - execute(cmd, KT_FDIMAGE); - } else if (!strcmp(module_name + len_mn - 4, ".bin")) { - } else if (!strcmp(module_name + len_mn - 4, ".bss")) { - execute(cmd, KT_BSS); - } else if (!strcmp(module_name + len_mn - 4, ".com") - || !strcmp(module_name + len_mn - 4, ".cbt")) { - execute(cmd, KT_COMBOOT); - } else if (!strcmp(module_name + len_mn - 4, ".cfg") - || !strcmp(module_name + len_mn - 5, ".conf") - || !strcmp(module_name + len_mn - 7, ".config")) { - execute(module_name, KT_CONFIG); - } - /* use KT_KERNEL as default */ - else - execute(temp_cmd, KT_KERNEL); - -cleanup: - free(argv); - free(temp_cmd); -} diff --git a/core/elflink/cli.h b/core/elflink/cli.h deleted file mode 100644 index 9cd8c16..0000000 --- a/core/elflink/cli.h +++ /dev/null @@ -1,23 +0,0 @@ -#ifndef CLI_H -#define CLI_H - -#define MAX_CMD_HISTORY 64 -#define COMMAND_DELIM " \t\n" // Whitespace delimiters -#define MAX_COMMAND_ARGS 40 - -struct cli_command { - struct list_head list; - char *command; -}; - -struct list_head cli_history_head; - -extern void clear_screen(void); -extern int mygetkey(clock_t timeout); -extern const char *edit_cmdline(const char *input, int top /*, int width */ , - int (*pDraw_Menu) (int, int, int), - void (*show_fkey) (int)); -extern void process_command(const char *cmd); - -extern struct menu *root_menu, *start_menu, *hide_menu, *menu_list, *default_menu; -#endif diff --git a/core/elflink/execute.c b/core/elflink/execute.c index 29e9933..3c9c3a7 100644 --- a/core/elflink/execute.c +++ b/core/elflink/execute.c @@ -72,7 +72,9 @@ void execute(const char *cmdline, enum kernel_type type) new_linux_kernel(kernel, cmdline); } else if (type == KT_CONFIG) { /* kernel contains the config file name */ - start_ui(kernel); + spawn_load_param[0] = args; + module_load_dependencies("ui.c32", "modules.dep"); + spawn_load(kernel, spawn_load_param); } else { /* process the image need int 22 support */ if (type == KT_LOCALBOOT) { diff --git a/core/elflink/get_key.c b/core/elflink/get_key.c deleted file mode 100644 index b2f7092..0000000 --- a/core/elflink/get_key.c +++ /dev/null @@ -1,175 +0,0 @@ -/* ----------------------------------------------------------------------- * - * - * Copyright 2004-2008 H. Peter Anvin - All Rights Reserved - * - * Permission is hereby granted, free of charge, to any person - * obtaining a copy of this software and associated documentation - * files (the "Software"), to deal in the Software without - * restriction, including without limitation the rights to use, - * copy, modify, merge, publish, distribute, sublicense, and/or - * sell copies of the Software, and to permit persons to whom - * the Software is furnished to do so, subject to the following - * conditions: - * - * The above copyright notice and this permission notice shall - * be included in all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, - * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES - * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND - * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT - * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, - * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING - * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR - * OTHER DEALINGS IN THE SOFTWARE. - * - * ----------------------------------------------------------------------- */ - -/* - * get_key.c - * - * Get a single key, and try to pick apart function key codes. - * This doesn't decode anywhere close to all possiblities, but - * hopefully is enough to be useful. - */ - -#include <stdio.h> -#include <string.h> -#include <errno.h> -#include <unistd.h> -#include <time.h> -#include <core.h> - -#include <sys/times.h> -#include <sys/module.h> - -#include "getkey.h" - -struct keycode { - int code; - int seqlen; - const unsigned char *seq; -}; - -#define MAXLEN 8 -#define CODE(x,y) { x, (sizeof y)-1, y } - -static const struct keycode keycodes[] = { - /* First, the BIOS combined codes */ - CODE(KEY_F1, "\0\x3B"), - CODE(KEY_F2, "\0\x3C"), - CODE(KEY_F3, "\0\x3D"), - CODE(KEY_F4, "\0\x3E"), - CODE(KEY_F5, "\0\x3F"), - CODE(KEY_F6, "\0\x40"), - CODE(KEY_F7, "\0\x41"), - CODE(KEY_F8, "\0\x42"), - CODE(KEY_F9, "\0\x43"), - CODE(KEY_F10, "\0\x44"), - CODE(KEY_F11, "\0\x85"), - CODE(KEY_F12, "\0\x86"), - - CODE(KEY_UP, "\0\x48"), - CODE(KEY_DOWN, "\0\x50"), - CODE(KEY_LEFT, "\0\x4B"), - CODE(KEY_RIGHT, "\0\x4D"), - CODE(KEY_PGUP, "\0\x49"), - CODE(KEY_PGDN, "\0\x51"), - CODE(KEY_HOME, "\0\x47"), - CODE(KEY_END, "\0\x4F"), - CODE(KEY_INSERT, "\0\x52"), - CODE(KEY_DELETE, "\0\x53"), - - /* Now, VT/xterm/Linux codes */ - CODE(KEY_F1, "\033[[A"), - CODE(KEY_F1, "\033OP"), - CODE(KEY_F2, "\033[[B"), - CODE(KEY_F2, "\033OQ"), - CODE(KEY_F3, "\033[[C"), - CODE(KEY_F3, "\033OR"), - CODE(KEY_F4, "\033[[D"), - CODE(KEY_F4, "\033OS"), - CODE(KEY_F5, "\033[[E"), - CODE(KEY_F5, "\033[15~"), - CODE(KEY_F6, "\033[17~"), - CODE(KEY_F7, "\033[18~"), - CODE(KEY_F8, "\033[19~"), - CODE(KEY_F9, "\033[20~"), - CODE(KEY_F10, "\033[21~"), - CODE(KEY_F11, "\033[23~"), - CODE(KEY_F12, "\033[24~"), - - CODE(KEY_UP, "\033[A"), - CODE(KEY_DOWN, "\033[B"), - CODE(KEY_LEFT, "\033[D"), - CODE(KEY_RIGHT, "\033[C"), - CODE(KEY_PGUP, "\033[5~"), - CODE(KEY_PGUP, "\033[V"), - CODE(KEY_PGDN, "\033[6~"), - CODE(KEY_PGDN, "\033[U"), - CODE(KEY_HOME, "\033[1~"), - CODE(KEY_HOME, "\033[H"), - CODE(KEY_END, "\033[4~"), - CODE(KEY_END, "\033[F"), - CODE(KEY_END, "\033OF"), - CODE(KEY_INSERT, "\033[2~"), - CODE(KEY_INSERT, "\033[@"), - CODE(KEY_DELETE, "\033[3~"), -}; - -#define NCODES ((int)(sizeof keycodes/sizeof(struct keycode))) - -#define KEY_TIMEOUT ((CLK_TCK+9)/10) - -int get_key(FILE * f, clock_t timeout) -{ - unsigned char buffer[MAXLEN]; - int nc, i, rv; - const struct keycode *kc; - int another; - unsigned char ch; - clock_t start; - - /* We typically start in the middle of a clock tick */ - if (timeout) - timeout++; - - nc = 0; - start = jiffies(); - do { - rv = read(fileno(f), &ch, 1); - if (rv == 0 || (rv == -1 && errno == EAGAIN)) { - clock_t lateness = jiffies() - start; - if (nc && lateness > 1 + KEY_TIMEOUT) { - if (nc == 1) - return buffer[0]; /* timeout in sequence */ - else if (timeout && lateness > timeout) - return KEY_NONE; - } else if (!nc && timeout && lateness > timeout) - return KEY_NONE; /* timeout before sequence */ - - syslinux_idle(); - - another = 1; - continue; - } - - start = jiffies(); - - buffer[nc++] = ch; - - another = 0; - for (i = 0, kc = keycodes; i < NCODES; i++, kc++) { - if (nc == kc->seqlen && !memcmp(buffer, kc->seq, nc)) - return kc->code; - else if (nc < kc->seqlen && !memcmp(buffer, kc->seq, nc)) { - another = 1; - break; - } - } - } while (another); - - /* We got an unrecognized sequence; return the first character */ - /* We really should remember this and return subsequent characters later */ - return buffer[0]; -} diff --git a/core/elflink/getkey.h b/core/elflink/getkey.h deleted file mode 100644 index 52312a2..0000000 --- a/core/elflink/getkey.h +++ /dev/null @@ -1,80 +0,0 @@ -/* ----------------------------------------------------------------------- * - * - * Copyright 2004-2008 H. Peter Anvin - All Rights Reserved - * - * Permission is hereby granted, free of charge, to any person - * obtaining a copy of this software and associated documentation - * files (the "Software"), to deal in the Software without - * restriction, including without limitation the rights to use, - * copy, modify, merge, publish, distribute, sublicense, and/or - * sell copies of the Software, and to permit persons to whom - * the Software is furnished to do so, subject to the following - * conditions: - * - * The above copyright notice and this permission notice shall - * be included in all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, - * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES - * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND - * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT - * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, - * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING - * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR - * OTHER DEALINGS IN THE SOFTWARE. - * - * ----------------------------------------------------------------------- */ - -/* - * getkey.h - * - * Function to get a key symbol and parse it - */ - -#ifndef LIBUTIL_GETKEY_H -#define LIBUTIL_GETKEY_H - -#include <stdio.h> -#include <unistd.h> -#include <sys/times.h> - -#ifndef CLK_TCK -# define CLK_TCK sysconf(_SC_CLK_TCK) -#endif - -#define KEY_NONE (-1) - -#define KEY_CTRL(x) ((x) & 0x001f) -#define KEY_BACKSPACE 0x0008 -#define KEY_TAB 0x0009 -#define KEY_ENTER 0x000d -#define KEY_ESC 0x001b -#define KEY_DEL 0x007f - -#define KEY_F1 0x0100 -#define KEY_F2 0x0101 -#define KEY_F3 0x0102 -#define KEY_F4 0x0103 -#define KEY_F5 0x0104 -#define KEY_F6 0x0105 -#define KEY_F7 0x0106 -#define KEY_F8 0x0107 -#define KEY_F9 0x0108 -#define KEY_F10 0x0109 -#define KEY_F11 0x010A -#define KEY_F12 0x010B - -#define KEY_UP 0x0120 -#define KEY_DOWN 0x0121 -#define KEY_LEFT 0x0122 -#define KEY_RIGHT 0x0123 -#define KEY_PGUP 0x0124 -#define KEY_PGDN 0x0125 -#define KEY_HOME 0x0126 -#define KEY_END 0x0127 -#define KEY_INSERT 0x0128 -#define KEY_DELETE 0x0129 - -int get_key(FILE *, clock_t); - -#endif /* LIBUTIL_GETKEY_H */ diff --git a/core/elflink/load_env32.c b/core/elflink/load_env32.c index 8d0a557..8e43900 100644 --- a/core/elflink/load_env32.c +++ b/core/elflink/load_env32.c @@ -15,7 +15,6 @@ #include <sys/module.h> #include "common.h" #include "menu.h" -#include "cli.h" #include "core-elf.h" typedef void (*constructor_t) (void); @@ -65,59 +64,14 @@ static void call_constr(void) (*p) (); } -void enter_cmdline(void) -{ - struct cli_command *comm, *aux; - char *cmdline; - - /* Enter endless command line prompt, should support "exit" */ - while (1) { - cmdline = edit_cmdline("", 1, NULL, NULL); - /* feng: give up the aux check here */ - //aux = list_entry(cli_history_head.next, typeof(*aux), list); - //if (strcmp(aux->command, cmdline)) { - comm = (struct cli_command *)malloc(sizeof(struct cli_command *)); - comm->command - (char *)malloc(sizeof(char) * (strlen(cmdline) + 1)); - strcpy(comm->command, cmdline); - list_add(&(comm->list), &cli_history_head); - process_command(cmdline); - //} - } -} - -/* parameter is the config file name if any */ -void start_ui(char *config_file) -{ - char *cmdline; - char *argv[2] = {config_file, NULL}; - - mp("enter, config file = %s", config_file); - - parse_configs(argv); - - /* run the default menu if found */ - if (default_menu) { - cmdline = default_menu->menu_entries[default_menu->defentry]->cmdline; - if (*cmdline == '.') { - while (*cmdline++ != ' '); - } - process_command(cmdline); - } - - /* Should never return */ - enter_cmdline(); -} - /* note to self: do _*NOT*_ use static key word on this function */ void load_env32(com32sys_t * regs) { printf("Starting 32 bit elf module subsystem...\n"); call_constr(); - openconsole(&dev_rawcon_r, &dev_ansiserial_w); - INIT_LIST_HEAD(&cli_history_head); init_module_subsystem(&core_module); - start_ui(NULL); + /* Load the UI ELF module which will drop us to a menu */ + execute("ui.c32", KT_COM32); } -- 1.7.4
H. Peter Anvin
2011-Feb-24 20:41 UTC
[syslinux] [RFC][PATCH 4/4] elflink: Move cli lib code out of core
On 02/24/2011 12:28 PM, Jim Cromie wrote:> > I think its insufficient here; the new file has different line count than > the 1st, so it cant be a verbatim copy as is. This is why I was looking > for 2 patches > - 1st to rename w no diff to inspect, > - 2nd to make the mods which deleted the 27 extra lines. > Sorry I wasnt more clear on that particular. >git can sort that out, though. -hpa