Ignore:
Timestamp:
Jul 13, 2020, 8:07:55 PM (4 years ago)
Author:
coas-nagasima
Message:

ntshellアプリはnewlibを使うよう変更し、syscallの実装部分と区別がつくよう更新。

File:
1 edited

Legend:

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

    r441 r442  
    4040 */
    4141
    42 #include "shellif.h"
    4342#include <kernel.h>
    4443#include <t_stdlib.h>
     
    5150#include "kernel_cfg.h"
    5251#include "main.h"
     52#include "mbed_retarget.h"
    5353#include "fdtable.h"
    5454#ifndef NTSHELL_NO_SOCKET
     
    6969#include "core/ntshell.h"
    7070#include "core/ntlibc.h"
    71 #include "util/ntstdio.h"
    7271#include "usrcmd.h"
    7372#include "util/ntopt.h"
     
    7978ID ws_api_mailboxid = MAIN_DATAQUEUE;
    8079ID ws_mempoolid = MPF_NET_BUF_256;
    81 extern ntstdio_t *ntstdio;
    8280
    8381char command[NTOPT_TEXT_MAXLEN];
    8482
    8583const uint8_t mac_addr[6] = {0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC};
    86 const struct utsname host_name = {
    87         "TOPPERS/ASP3",
    88         TARGET_NAME,
    89         "3.5.0",
    90         "3.5.0",
    91         TARGET_NAME,
    92         "toppers.jp"
    93 };
    94 
    95 int shell_uname(struct utsname *uts)
    96 {
    97         memcpy(uts, &host_name, sizeof(host_name));
    98         return 0;
    99 }
    10084
    10185enum main_state_t {
     
    220204{
    221205        if (text != NULL) {
    222                 ntlibc_strlcpy(command, text, sizeof(command));
     206                strlcpy(command, text, sizeof(command));
    223207        }
    224208        ER ret;
     
    552536                return result;
    553537
    554         if (ntlibc_strcmp((const char *)args[1], "help") == 0) {
    555                 found = 1;
     538        if (strcmp((const char *)args[1], "help") == 0) {
    556539                result = usrcmd_help(args[0], (char **)&args[1]);
    557540        }
    558541        else for (int i = 0; i < cmd_table_info.count; i++) {
    559                 if (ntlibc_strcmp((const char *)args[1], p->cmd) == 0) {
     542                if (strcmp((const char *)args[1], p->cmd) == 0) {
    560543                        found = 1;
    561544                        result = p->func(args[0], (char **)&args[1]);
     
    571554
    572555        if ((found == 0) && (((const char *)args[1])[0] != '\0'))
    573                 ntstdio_printf(ntstdio, "Unknown command found. %s \n", (const char *)args[1]);
     556                printf("Unknown command found. %s \n", (const char *)args[1]);
    574557
    575558        return result;
     
    580563        const cmd_table_t *p = cmd_table_info.table;
    581564        for (int i = 0; i < cmd_table_info.count; i++) {
    582                 ntstdio_puts(ntstdio, p->cmd);
    583                 ntstdio_puts(ntstdio, "\t:");
    584                 ntstdio_puts(ntstdio, p->desc);
    585                 ntstdio_puts(ntstdio, "\n");
     565                printf(p->cmd);
     566                printf("\t:");
     567                puts(p->desc);
    586568                p++;
    587569        }
     
    599581        /* shellcmdタスクの優先度に戻す */
    600582        chg_pri(SHELLCMD_PRIORITY, SHELLCMD_PRIORITY);
    601 }
    602 
    603 void shell_abort()
    604 {
    605         shellcmd_exit_code = -1;
    606         longjmp(shellcmd_exit, 1);
    607 }
    608 
    609 void shell_exit(int exitcd)
    610 {
    611         shellcmd_exit_code = exitcd;
    612         longjmp(shellcmd_exit, 1);
    613 }
    614 
    615 void shell_exit_group(int exitcd)
    616 {
    617         shellcmd_exit_code = exitcd;
    618         longjmp(shellcmd_exit, 1);
    619583}
    620584
     
    679643}
    680644
    681 int shell_clock_getres(clockid_t clk_id, struct timespec *res)
    682 {
    683         if ((clk_id != CLOCK_REALTIME) && (clk_id != CLOCK_MONOTONIC))
    684                 return -EINVAL;
    685 
    686         memset(&res->tv_sec, 0xFF, sizeof(res->tv_sec));
    687         res->tv_nsec = 0;
    688 
    689         return 0;
    690 }
    691 
    692 int shell_clock_gettime(clockid_t clk_id, struct timespec *tp)
    693 {
    694         SYSTIM now = 0;
    695 
    696         if ((clk_id != CLOCK_REALTIME) && (clk_id != CLOCK_MONOTONIC))
    697                 return -EINVAL;
    698 
    699         get_tim(&now);
    700         tp->tv_sec = now / 1000000;
    701         tp->tv_nsec = (now % 1000000) * 1000;
    702 
    703         return 0;
    704 }
    705 
    706 int shell_clock_settime(clockid_t clk_id, const struct timespec *tp)
    707 {
    708         if ((clk_id != CLOCK_REALTIME) && (clk_id != CLOCK_MONOTONIC))
    709                 return -EINVAL;
    710 
    711         SYSTIM time;
    712         ER ret;
    713 
    714         time = (tp->tv_sec * 1000000ll) + (tp->tv_nsec / 1000ll);
    715 
    716         ret = set_tim(time);
    717         if (ret != E_OK) {
    718                 return -EPERM;
    719         }
    720 
    721         return 0;
    722 }
    723 
    724 sigset_t g_sigmask;
    725 
    726 int shell_sigprocmask(int how, const sigset_t *restrict set, sigset_t *restrict old)
    727 {
    728         if (old != NULL)
    729                 memcpy(old, &g_sigmask, sizeof(sigset_t));
    730 
    731         switch (how) {
    732         case SIG_BLOCK:
    733                 for (int i = 0; i < sizeof(g_sigmask.__bits) / sizeof(g_sigmask.__bits[0]); i++) {
    734                         g_sigmask.__bits[i] |= set->__bits[i];
    735                 }
    736                 break;
    737         case SIG_UNBLOCK:
    738                 for (int i = 0; i < sizeof(g_sigmask.__bits) / sizeof(g_sigmask.__bits[0]); i++) {
    739                         g_sigmask.__bits[i] &= ~set->__bits[i];
    740                 }
    741                 break;
    742         case SIG_SETMASK:
    743                 memcpy(&g_sigmask, set, sizeof(sigset_t));
    744                 break;
    745         default:
    746                 return -EINVAL;
    747         }
    748 
    749         return 0;
    750 }
    751 
    752 // musl-1.1.18\src\internal\ksigaction.h
    753 struct k_sigaction {
    754         void(*handler)(int);
    755         unsigned long flags;
    756         void(*restorer)(void);
    757         unsigned mask[2];
    758 };
    759 
    760 struct k_sigaction sigtable[7];
    761 
    762 int shell_sigaction(int sig, const struct k_sigaction *restrict sa,
    763         struct k_sigaction *restrict old, size_t size)
    764 {
    765         struct k_sigaction *sat;
    766 
    767         switch(sig){
    768         case SIGALRM:
    769                 sat = &sigtable[0];
    770                 break;
    771         case SIGFPE:
    772                 sat = &sigtable[1];
    773                 break;
    774         case SIGILL:
    775                 sat = &sigtable[2];
    776                 break;
    777         case SIGSEGV:
    778                 sat = &sigtable[3];
    779                 break;
    780         case SIGBUS:
    781                 sat = &sigtable[4];
    782                 break;
    783         case SIGABRT:
    784                 sat = &sigtable[5];
    785                 break;
    786         case SIGPIPE:
    787                 sat = &sigtable[6];
    788                 break;
    789         default:
    790                 return -EINVAL;
    791         }
    792 
    793         if (old != NULL)
    794                 memcpy(old, sat, offsetof(struct k_sigaction, mask) + size);
    795 
    796         memcpy(sat, sa, offsetof(struct k_sigaction, mask) + size);
    797 
    798         return 0;
    799 }
    800 
    801 int shell_madvise(void *a, size_t b, int c)
    802 {
    803         return 0;
    804 }
    805 
    806 int shell_gettid()
    807 {
    808         ID tskid;
    809         ER ret;
    810 
    811         ret = get_tid(&tskid);
    812         if (ret != E_OK)
    813                 return -1;
    814 
    815         return tskid;
    816 }
    817 
    818 int shell_tkill(int tid, int sig)
    819 {
    820         if ((tid == SHELLCMD_TASK) && (sig == SIGABRT)) {
    821                 shell_abort();
    822         }
    823 
    824         no_implement("tkill");
    825         return -1;
    826 }
    827 
    828 int shell_kill(int pid, int sig)
    829 {
    830         DebugBreak();
    831         return -1;
    832 }
    833 
    834 int shell_gettimeofday(struct timeval *tv, void *tzvp)
    835 {
    836         SYSTIM time;
    837         if (!tv) return 0;
    838         get_tim(&time);
    839         tv->tv_sec = time / 1000000;
    840         tv->tv_usec = time - (tv->tv_sec * 1000000);
    841         return 0;
    842 }
    843 
    844 int shell_nanosleep(const struct timespec *req, struct timespec *rem)
    845 {
    846         ER ret;
    847         TMO tmo;
    848         SYSTIM prev, now, diff;
    849 
    850         if ((req == NULL) || (req->tv_nsec < 0) || (req->tv_nsec >= 1000000000))
    851                 return -EINVAL;
    852 
    853         get_tim(&prev);
    854 
    855         tmo = req->tv_sec * 1000000 + req->tv_nsec / 1000;
    856         ret = tslp_tsk(tmo);
    857         if (ret == E_OK) {
    858                 if (rem != NULL) {
    859                         get_tim(&now);
    860                         diff = now - prev;
    861                         rem->tv_sec = diff / 1000000ll;
    862                         rem->tv_nsec = (diff - (rem->tv_sec * 1000000ll)) * 1000ll;
    863                 }
    864                 return 0;
    865         }
    866         else if (ret == E_TMOUT) {
    867                 if (rem != NULL) {
    868                         rem->tv_sec = 0;
    869                         rem->tv_nsec = 0;
    870                 }
    871                 return 0;
    872         }
    873 
    874         return -EFAULT;
    875 }
    876 
    877 ssize_t shell_getrandom(void *buf, size_t buflen, unsigned int flags)
    878 {
    879         SYSTIM now;
    880         int32_t i;
    881         int *output = (int *)buf;
    882         size_t sz = buflen / 4;
    883 
    884         get_tim(&now);
    885         srand(now);
    886 
    887         for (i = 0; i < sz; i++)
    888                 output[i] = rand();
    889 
    890         for (i = 4 * sz; i < buflen; i++)
    891                 ((char *)buf)[i] = rand();
    892 
    893         return buflen;
    894 }
    895 
    896 extern uint32_t  __CmdBase;
    897 extern uint32_t  __CmdLimit;
    898 
    899 void *shell_brk(void *addr)
    900 {
    901         if (addr == 0) {
    902                 return (void *)((intptr_t)&__CmdBase + 0x40000);
    903         }
    904         if ((addr >= (intptr_t)&__CmdBase + 0x40000) && (addr < &__CmdLimit)) {
    905                 return addr;
    906         }
    907         return (void *)-1;
    908 }
    909 
    910 void *shell_mmap2(void *start, size_t length, int prot, int flags, int fd, off_t pgoffset)
    911 {
    912         if (fd != -1)
    913                 return -EINVAL;
    914 
    915         if ((length >= 0) && (length <= (intptr_t)&__CmdLimit - ((intptr_t)&__CmdBase + 0x40000))) {
    916                 return &__CmdBase + 0x40000;
    917         }
    918         return (void *)-1;
    919 }
    920 
    921 int shell_mprotect(void *addr, size_t len, int prot)
    922 {
    923         //if ((addr >= (intptr_t)&__CmdBase + 0x40000) && ((intptr_t)addr + len < &__CmdLimit)) {
    924                 return 0;
    925         //}
    926         //return -1;
    927 }
Note: See TracChangeset for help on using the changeset viewer.