Ignore:
Timestamp:
Jul 21, 2020, 8:38:44 AM (4 years ago)
Author:
coas-nagasima
Message:

プログラムテーブルのセクションを追加しntshellで読みだしてHELPを表示するよう変更

File:
1 edited

Legend:

Unmodified
Added
Removed
  • EcnlProtoTool/trunk/ntshell/src/main.c

    r443 r446  
    125125static const Elf32_Ehdr *load_elf(const uint8_t *file);
    126126static void execute_elf(const Elf32_Ehdr *, long *args);
     127static void read_cmdlist_from_elf(const Elf32_Ehdr *Ehdr);
    127128static int usrcmd_ntopt_callback(long *args, void *extobj);
    128129#ifndef NTSHELL_NO_SOCKET
     
    527528        {"info", "This is a description text string for info command.", usrcmd_info},
    528529        {"exit", "Exit Natural Tiny Shell", usrcmd_exit},
     530        {"help", "This is a description text string for help command.", usrcmd_help},
    529531};
    530 cmd_table_info_t cmd_table_info = { cmdlist, sizeof(cmdlist) / sizeof(cmdlist[0]) };
     532const cmd_table_info_t cmd_table_info = { cmdlist, sizeof(cmdlist) / sizeof(cmdlist[0]) };
     533cmd_table_info_t elf_cmd_table_info;
    531534
    532535static int usrcmd_ntopt_callback(long *args, void *extobj)
     
    540543
    541544        if (strcmp((const char *)args[1], "help") == 0) {
     545                found = 1;
    542546                result = usrcmd_help(args[0], (char **)&args[1]);
    543547        }
     
    567571int usrcmd_help(int argc, char **argv)
    568572{
    569         const cmd_table_t *p = cmd_table_info.table;
    570         for (int i = 0; i < cmd_table_info.count; i++) {
    571                 printf(p->cmd);
    572                 printf("\t:");
    573                 puts(p->desc);
    574                 p++;
     573        const cmd_table_t *p, *q;
     574        const Elf32_Ehdr *elf = load_elf((const uint8_t *)0x18200000);
     575        if (elf != NULL) {
     576                read_cmdlist_from_elf(elf);
     577        }
     578        else {
     579                elf_cmd_table_info.count = 0;
     580        }
     581        p = cmd_table_info.table;
     582        for (int i = 0; i < cmd_table_info.count - 3; i++, p++) {
     583                printf("%s\t:%s\n", p->cmd, p->desc);
     584        }
     585        p = elf_cmd_table_info.table;
     586        for (int i = 0; i < elf_cmd_table_info.count; i++, p++) {
     587                printf("%s\t:%s\n", p->cmd, p->desc);
     588        }
     589        p = &cmd_table_info.table[cmd_table_info.count - 3];
     590        for (int i = cmd_table_info.count - 3; i < cmd_table_info.count; i++, p++) {
     591                printf("%s\t:%s\n", p->cmd, p->desc);
    575592        }
    576593        return 0;
     
    700717                        Shdr.sh_size -= Shdr.sh_addr;
    701718
    702                         char *name;
    703                         if (Shdr.sh_name > 0) {
    704                                 name = (char *)&file[StringShdr.sh_offset + Shdr.sh_name];
    705                         }
    706                         else {
    707                                 name = NULL;
    708                         }
    709 
    710719                        if ((Shdr.sh_flags & SHF_ALLOC) == 0) {
    711720                                // skip
     
    730739        ((elf_entry_t)Ehdr->e_entry)(args);
    731740}
     741
     742void read_cmdlist_from_elf(const Elf32_Ehdr *Ehdr)
     743{
     744        const uint8_t *file = (const uint8_t *)Ehdr;
     745        Elf32_Shdr StringShdr;
     746        memcpy(&StringShdr, &file[Ehdr->e_shoff + (Ehdr->e_shstrndx * Ehdr->e_shentsize)], sizeof(StringShdr));
     747
     748        if (Ehdr->e_shoff > 0) {
     749                Elf32_Shdr Shdr;
     750                for (int i = 0; i < Ehdr->e_shnum; i++) {
     751                        memcpy(&Shdr, (Elf32_Shdr *)&file[Ehdr->e_shoff + (i * Ehdr->e_shentsize)], sizeof(Elf32_Shdr));
     752                        Shdr.sh_size -= Shdr.sh_addr;
     753
     754                        char *name;
     755                        if (Shdr.sh_name > 0) {
     756                                name = (char *)&file[StringShdr.sh_offset + Shdr.sh_name];
     757                        }
     758                        else {
     759                                name = NULL;
     760                        }
     761
     762                        if ((name != NULL) && (strcmp(name, ".cmdlist") == 0)) {
     763                                elf_cmd_table_info.table = (const cmd_table_t *)Shdr.sh_addr;
     764                                elf_cmd_table_info.count = Shdr.sh_size / sizeof(cmd_table_t);
     765                        }
     766                }
     767        }
     768}
Note: See TracChangeset for help on using the changeset viewer.