Ignore:
Timestamp:
Jan 21, 2018, 12:10:09 AM (6 years ago)
Author:
coas-nagasima
Message:

prototoolに関連するプロジェクトをnewlibからmuslを使うよう変更・更新
ntshellをnewlibの下位の実装から、muslのsyscallの実装に変更・更新
以下のOSSをアップデート
・mruby-1.3.0
・musl-1.1.18
・onigmo-6.1.3
・tcc-0.9.27
以下のOSSを追加
・openssl-1.1.0e
・curl-7.57.0
・zlib-1.2.11
以下のmrbgemsを追加
・iij/mruby-digest
・iij/mruby-env
・iij/mruby-errno
・iij/mruby-iijson
・iij/mruby-ipaddr
・iij/mruby-mock
・iij/mruby-require
・iij/mruby-tls-openssl

File:
1 edited

Legend:

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

    r321 r331  
    3535 *  @(#) $Id$
    3636 */
     37#include "shellif.h"
    3738#include <stdint.h>
    38 #include <stdio.h>
    39 #include <sys/unistd.h>
    40 #include <limits.h>
    41 #include <fcntl.h>
    4239#include "ff.h"
    4340#include <kernel.h>
     
    4542#include <t_stdlib.h>
    4643#include <sil.h>
    47 #include <stdlib.h>
    4844#include <string.h>
    49 #include <stdio.h>
    5045#include <setjmp.h>
    5146#include "syssvc/syslog.h"
     
    6156#include <net/if_var.h>
    6257#include <netinet/udp_var.h>
    63 #include <ethernet_api.h>
    64 #include "ff.h"
     58//#include <ethernet_api.h>
    6559#include "socket_stub.h"
    66 #include "../../../musl-1.1.12/include/_dirent.h"
    67 #include "../../../musl-1.1.12/include/_termios.h"
    68 #include "ntstdio.h"
    69 
    70 int shell_open(const char * path, int flags)
    71 {
    72         FRESULT res;
    73 
    74         struct _IO_FILE *fp = new_file_fd(0);
    75         if (fp == NULL)
    76                 return -1;
     60//#include <sys/stat.h>
     61#include "util/ntstdio.h"
     62#include "usrcmd.h"
     63#include "core/ntlibc.h"
     64
     65int fresult2errno(FRESULT res)
     66{
     67        switch (res) {
     68        case FR_INVALID_OBJECT:
     69                return -EINVAL;
     70        case FR_TOO_MANY_OPEN_FILES:
     71                return -ENOMEM;
     72        case FR_NO_FILE:
     73        case FR_NO_PATH:
     74        case FR_INVALID_DRIVE:
     75        case FR_INVALID_NAME:
     76                return -ENOENT;
     77        case FR_DISK_ERR:
     78        case FR_NO_FILESYSTEM:
     79        case FR_NOT_ENABLED:
     80                return -ENODEV;
     81        case FR_WRITE_PROTECTED:
     82        case FR_DENIED:
     83                return -EACCES;
     84        case FR_EXIST:
     85                return -EEXIST;
     86        case FR_INT_ERR:
     87        default:
     88                return -EIO;
     89        }
     90}
     91
     92int shell_open(const char * path, int flags, void *arg)
     93{
     94        FRESULT res;
     95        struct _IO_FILE *fp;
     96
     97        if (flags & O_DIRECTORY) {
     98                fp = new_dir_fd(0);
     99                if (fp == NULL)
     100                        return -ENOMEM;
     101
     102                DIR *dir = &fp->dir;
     103                FRESULT res;
     104                if ((res = f_opendir(dir, path)) != FR_OK) {
     105                        return fresult2errno(res);
     106                }
     107                return 0;
     108        }
     109
     110        fp = new_file_fd(0);
     111        if (fp == NULL)
     112                return -ENOMEM;
    77113
    78114        BYTE fmd = 0;
     
    115151        }
    116152
    117         return -1;
     153        return fresult2errno(res);
    118154}
    119155
     
    126162        }
    127163
    128         return -1;
     164        return -EINVAL;
    129165}
    130166
     
    135171
    136172        if ((res = f_read(&fp->file, data, len, &ret)) != FR_OK)
    137                 return -1;
     173                return -EIO;
    138174
    139175        return ret;
     
    146182
    147183        if ((res = f_write(&fp->file, data, len, &ret)) != FR_OK)
    148                 return -1;
     184                return -EIO;
    149185
    150186        return ret;
    151187}
    152188
     189off_t file_seek(struct _IO_FILE *fp, off_t ptr, int dir)
     190{
     191        switch (dir) {
     192        case SEEK_SET:
     193                dir = F_SEEK_SET;
     194                break;
     195        case SEEK_CUR:
     196                dir = F_SEEK_CUR;
     197                break;
     198        case SEEK_END:
     199                dir = F_SEEK_END;
     200                break;
     201        default:
     202                return -EINVAL;
     203        }
     204
     205        FRESULT res;
     206        if ((res = f_seek(&fp->file, ptr, dir)) != FR_OK)
     207                return -EIO;
     208
     209        return fp->file.fptr;
     210}
     211
     212int file_ioctl(struct _IO_FILE *fp, int req, void *arg)
     213{
     214        DRESULT res;
     215
     216        if ((res = disk_ioctl(fp->file.fs->drv, req, arg) != RES_OK))
     217                return -EINVAL;
     218
     219        return 0;
     220}
     221
    153222int shell_close(int fd)
    154223{
    155224        struct _IO_FILE *fp = fd_to_fp(fd);
    156225        if (fp == NULL)
    157                 return -1;
     226                return -EBADF;
    158227
    159228        return fp->close(fp);
    160229}
    161230
    162 int shell_read(int fd, char *data, int len)
    163 {
    164         struct _IO_FILE *fp = fd_to_fp(fd);
    165         if (fp == NULL)
    166                 return -1;
     231ssize_t shell_read(int fd, void *data, size_t len)
     232{
     233        struct _IO_FILE *fp = fd_to_fp(fd);
     234        if (fp == NULL)
     235                return -EBADF;
    167236
    168237        return fp->read(fp, (unsigned char *)data, len);
    169238}
    170239
    171 int shell_write(int fd, char *data, int len)
    172 {
    173         struct _IO_FILE *fp = fd_to_fp(fd);
    174         if (fp == NULL)
    175                 return -1;
     240int shell_readv(int fd, const struct iovec *iov, int iovcnt)
     241{
     242        int result = 0;
     243        struct _IO_FILE *fp = fd_to_fp(fd);
     244        if (fp == NULL)
     245                return -EBADF;
     246
     247        const struct iovec *end = &iov[iovcnt];
     248        for (; iov < end; iov++) {
     249                result += fp->read(fp, (unsigned char *)iov->iov_base, iov->iov_len);
     250        }
     251
     252        return result;
     253}
     254
     255ssize_t shell_write(int fd, const void *data, size_t len)
     256{
     257        struct _IO_FILE *fp = fd_to_fp(fd);
     258        if (fp == NULL)
     259                return -EBADF;
    176260
    177261        return fp->write(fp, (unsigned char *)data, len);
    178262}
    179263
    180 int shell_lseek(int fd, int ptr, int dir)
    181 {
    182         struct _IO_FILE *fp = fd_to_fp(fd);
    183         if (fp == NULL)
    184                 return -1;
    185 
    186         FRESULT res;
    187         if ((res = f_seek(&fp->file, ptr, dir)) != FR_OK)
    188                 return -1;
    189 
    190         return fp->file.fptr;
     264int shell_writev(int fd, const struct iovec *iov, int iovcnt)
     265{
     266        int result = 0;
     267        struct _IO_FILE *fp = fd_to_fp(fd);
     268        if (fp == NULL)
     269                return -EBADF;
     270
     271        const struct iovec *end = &iov[iovcnt];
     272        for (; iov < end; iov++) {
     273                result += fp->write(fp, (unsigned char *)iov->iov_base, iov->iov_len);
     274        }
     275
     276        return result;
     277}
     278
     279int shell_llseek(int fd, off_t ptr, off_t *result, int dir)
     280{
     281        struct _IO_FILE *fp = fd_to_fp(fd);
     282        if (fp == NULL)
     283                return -EBADF;
     284
     285        off_t ret = fp->seek(fp, ptr, dir);
     286        if (ret < 0)
     287                return ret;
     288
     289        *result = ret;
     290        return 0;
    191291}
    192292
     
    195295        struct _IO_FILE *fp = fd_to_fp(fd);
    196296        if (fp == NULL)
    197                 return -1;
     297                return -EBADF;
    198298
    199299        memset(st, 0, sizeof(*st));
     
    203303}
    204304
    205 int fsync(int fd)
    206 {
    207         struct _IO_FILE *fp = fd_to_fp(fd);
    208         if (fp == NULL)
    209                 return -1;
    210         return -1;
    211 }
    212 
    213 int ftruncate(int fd, off_t length)
    214 {
    215         struct _IO_FILE *fp = fd_to_fp(fd);
    216         if (fp == NULL)
    217                 return -1;
     305int shell_fsync(int fd)
     306{
     307        struct _IO_FILE *fp = fd_to_fp(fd);
     308        if (fp == NULL)
     309                return -EBADF;
     310        return -EIO;
     311}
     312
     313int shell_ftruncate(int fd, off_t length)
     314{
     315        struct _IO_FILE *fp = fd_to_fp(fd);
     316        if (fp == NULL)
     317                return -EBADF;
    218318
    219319        FRESULT res;
    220320        if ((res = f_truncate(&fp->file)) != FR_OK)
    221                 return -1;
    222 
    223         return 0;
    224 }
    225 
    226 int ioctl(int fd, int request, va_list ap)
    227 {
    228         struct _IO_FILE *fp = fd_to_fp(fd);
    229         if (fp == NULL)
    230                 return -1;
    231         return -1;
    232 }
    233 
    234 int tcgetattr(int fd, struct termios *termios)
     321                return fresult2errno(res);
     322
     323        return 0;
     324}
     325
     326int shell_fcntl(int fd, int cmd, void *arg)
     327{
     328        return shell_ioctl(fd, cmd, arg);
     329}
     330
     331int sio_tcgetattr(int fd, struct termios *termios)
    235332{
    236333        extern ntstdio_t ntstdio;
     
    265362                return 0;
    266363        }
    267         abort();
    268         return 0;
    269 }
    270 
    271 int tcsetattr(int fd, int optional_actions, const struct termios *termios)
     364        shell_abort();
     365        return 0;
     366}
     367
     368int sio_tcsetattr(int fd, int optional_actions, const struct termios *termios)
    272369{
    273370        extern ntstdio_t ntstdio;
     
    300397                return 0;
    301398        }
    302         abort();
    303         return 0;
    304 }
    305 
    306 int shell_stat(const char *path, struct stat *st)
     399        shell_abort();
     400        return 0;
     401}
     402
     403int shell_stat(const char *__restrict path, struct stat *__restrict st)
    307404{
    308405        FILINFO fi;
     
    313410        fi.lfsize = sizeof lfn;
    314411#endif
    315         if (strcmp(path, ".") == 0) {
     412        if (ntlibc_strcmp(path, ".") == 0) {
    316413                char cwd[_MAX_LFN];
    317414                if ((ret = f_getcwd(cwd, sizeof(cwd))) != FR_OK) {
    318                 return -1;
    319                 }
    320                 int l = strlen(cwd);
     415                        return fresult2errno(ret);
     416                }
     417                int l = ntlibc_strlen(cwd);
    321418                // ルートディレクトリの場合
    322419                if (cwd[l - 2] == ':' && cwd[l - 1] == '/') {
    323420                        st->st_size = 0;
    324                         st->st_mtime = 0;
     421                        st->st_mtim.tv_nsec = 0;
     422                        st->st_mtim.tv_sec = 0;
    325423                        st->st_mode = S_IFDIR;
    326424                        return 0;
    327425                }
    328426                if ((ret = f_stat(cwd, &fi)) != FR_OK) {
    329                         return -1;
     427                        return fresult2errno(ret);
    330428                }
    331429        }
    332430        else if ((ret = f_stat(path, &fi)) != FR_OK) {
    333                 return -1;
     431                return fresult2errno(ret);
    334432        }
    335433
    336434        st->st_size = fi.fsize;
    337         st->st_mtime = fi.fdate + fi.ftime;
     435        st->st_mtim.tv_nsec = 0;
     436        st->st_mtim.tv_sec = fi.fdate + fi.ftime;
    338437        st->st_mode  = (S_IRUSR | S_IRGRP | S_IROTH);
    339438        st->st_mode |= (fi.fattrib & AM_RDO) ? 0 : (S_IWUSR | S_IWGRP | S_IWOTH);
     
    343442}
    344443
    345 int shell_link(void)
    346 {
    347         return -1;
     444int shell_lstat(const char *__restrict path, struct stat *__restrict st)
     445{
     446        return shell_stat(path, st);
     447}
     448
     449int shell_link(const char *a, const char *b)
     450{
     451        return -EPERM;
    348452}
    349453
     
    353457
    354458        if ((res = f_unlink(path)) != FR_OK)
    355                 return -1;
    356 
    357         return 0;
    358 }
    359 
    360 int rmdir(const char *path)
     459                return -EIO;
     460
     461        return 0;
     462}
     463
     464int shell_rmdir(const char *path)
    361465{
    362466        FRESULT res;
    363467
    364468        if ((res = f_unlink(path)) != FR_OK)
    365                 return -1;
     469                return -EIO;
    366470
    367471        return 0;
     
    373477
    374478        if ((res = f_rename(oldpath, newpath)) != FR_OK)
    375                 return -1;
    376         return 0;
    377 }
    378 
    379 int mkdir(const char *path, mode_t mode)
     479                return fresult2errno(res);
     480        return 0;
     481}
     482
     483#define S_IREAD S_IRUSR
     484#define S_IWRITE S_IWUSR
     485
     486int shell_mkdir(const char *path, mode_t mode)
    380487{
    381488        FRESULT res;
    382489
    383490        if ((res = f_mkdir(path)) != FR_OK)
    384                 return -1;
     491                return fresult2errno(res);
    385492
    386493        BYTE attr = 0;
     
    397504
    398505        if((res = f_chmod(path, attr, mask)) != FR_OK) {
    399                 return -1;
    400         }
    401 
    402         return 0;
    403 }
    404 
    405 int chmod(const char *path, mode_t mode)
    406 {
    407         FRESULT ret;
     506                return fresult2errno(res);
     507        }
     508
     509        return 0;
     510}
     511
     512int shell_chmod(const char *path, mode_t mode)
     513{
     514        FRESULT res;
    408515        BYTE attr = 0;
    409516        BYTE mask = AM_RDO | AM_SYS; // AM_ARC, AM_HID
     
    418525        }
    419526
    420         if((ret = f_chmod(path, attr, mask)) != FR_OK) {
    421                 return -1;
    422         }
    423 
    424         return 0;
    425 }
    426 
    427 char *getcwd(char *buf, size_t size)
     527        if((res = f_chmod(path, attr, mask)) != FR_OK) {
     528                return fresult2errno(res);
     529        }
     530
     531        return 0;
     532}
     533
     534char *shell_getcwd(char *buf, size_t size)
    428535{
    429536        FRESULT ret;
     
    435542}
    436543
    437 int chdir(const char *path)
    438 {
    439         FRESULT ret;
    440         if ((ret = f_chdir(path)) != FR_OK) {
    441                 return -1;
    442         }
    443 
    444         return 0;
    445 }
    446 
    447 int chroot(const char *path)
    448 {
    449         abort();
    450         return -1;
    451 }
    452 
    453 DIR *opendir(const char *path)
    454 {
    455         DIR *dir = malloc(sizeof(DIR) + sizeof(struct dirent));
    456         FRESULT ret;
    457         if ((ret = f_opendir(dir, path)) != FR_OK) {
    458                 free(dir);
    459                 return NULL;
    460         }
    461 
    462         dir->dirent = &dir[1];
    463         return dir;
    464 }
    465 
    466 int closedir(DIR *dir)
    467 {
    468         FRESULT ret;
    469         if ((ret = f_closedir(dir)) != FR_OK) {
    470                 free(dir);
    471                 return -1;
    472         }
    473 
    474         free(dir);
    475         return 0;
    476 }
    477 
    478 struct dirent *readdir(DIR *dir)
    479 {
    480         struct dirent *de = dir->dirent;
     544int shell_chdir(const char *path)
     545{
     546        FRESULT res;
     547        if ((res = f_chdir(path)) != FR_OK) {
     548                return fresult2errno(res);
     549        }
     550
     551        return 0;
     552}
     553
     554int shell_chroot(const char *path)
     555{
     556        shell_abort();
     557        return -EPERM;
     558}
     559
     560int dir_close(struct _IO_FILE *fp)
     561{
     562        FRESULT res;
     563        if ((res = f_closedir(&fp->dir)) != FR_OK) {
     564                return fresult2errno(res);
     565        }
     566
     567        return 0;
     568}
     569
     570int shell_getdents(int fd, struct dirent *de, size_t len)
     571{
     572        if (len < sizeof(struct dirent))
     573                return -EINVAL;
     574
     575        struct _IO_FILE *fp = fd_to_fp(fd);
     576        if (fp == NULL)
     577                return -EBADF;
     578
    481579        FILINFO fno;
    482580#if _USE_LFN
     
    485583        fno.lfsize = sizeof lfn;
    486584#endif
    487         FRESULT ret;
    488         if ((ret = f_readdir(dir, &fno)) != FR_OK || fno.fname[0] == '\0') {
    489                 return NULL;
     585        FRESULT res;
     586        if ((res = f_readdir(&fp->dir, &fno)) != FR_OK || fno.fname[0] == '\0') {
     587                return fresult2errno(res);
    490588        }
    491589
    492590        memset(de, 0, sizeof(*de));
    493591#if _USE_LFN
    494         strlcpy(de->d_name, *fno.lfname ? fno.lfname : fno.fname, sizeof(de->d_name));
     592        ntlibc_strlcpy(de->d_name, *fno.lfname ? fno.lfname : fno.fname, sizeof(de->d_name));
    495593#else
    496         strlcpy(de->d_name, fno.fname, sizeof(de->d_name));
     594        ntlibc_strlcpy(de->d_name, fno.fname, sizeof(de->d_name));
    497595#endif
    498596
    499         return de;
    500 }
    501 
    502 void rewinddir(DIR *dir)
    503 {
    504         FRESULT ret;
    505         if ((ret = f_rewinddir(dir)) != FR_OK) {
    506                 return;
    507         }
    508 }
    509 
    510 void seekdir(DIR *dir, long pos)
    511 {
    512         abort();
    513 }
    514 
    515 long telldir(DIR *dir)
    516 {
    517         abort();
    518         return 0;
    519 }
    520 
    521 int shell_getpid(int n)
     597        return 0;
     598}
     599
     600size_t dir_read(struct _IO_FILE *fp, unsigned char *data, size_t len)
     601{
     602        return -EPERM;
     603}
     604
     605size_t dir_write(struct _IO_FILE *fp, const unsigned char *data, size_t len)
     606{
     607        return -EPERM;
     608}
     609
     610off_t dir_seek(struct _IO_FILE *fp, off_t ptr, int dir)
     611{
     612        FRESULT res;
     613
     614        if (dir != SEEK_SET)
     615                return -EINVAL;
     616
     617        if (ptr == 0) {
     618                if ((res = f_rewinddir(&fp->dir)) != FR_OK) {
     619                        return fresult2errno(res);
     620                }
     621        }
     622        else {
     623                FILINFO fno;
     624#if _USE_LFN
     625                static char lfn[_MAX_LFN + 1];   /* Buffer to store the LFN */
     626                fno.lfname = lfn;
     627                fno.lfsize = sizeof lfn;
     628#endif
     629                if ((res = f_rewinddir(&fp->dir)) != FR_OK) {
     630                        return fresult2errno(res);
     631                }
     632
     633                for (int i = 0; i < ptr; i++) {
     634                        if ((res = f_readdir(&fp->dir, &fno)) != FR_OK || fno.fname[0] == '\0') {
     635                                return fresult2errno(res);
     636                        }
     637                }
     638        }
     639
     640        return ptr;
     641}
     642
     643int dir_ioctl(struct _IO_FILE *fp, int req, void *arg)
     644{
     645        return -EINVAL;
     646}
     647
     648pid_t shell_getpid(void)
    522649{
    523650        return 1;
    524651}
     652
     653int shell_access(const char *path, int mode)
     654{
     655        struct stat st;
     656        int ret;
     657
     658        ret = shell_stat(path, &st);
     659        if (ret != 0)
     660                return ret;
     661
     662        return 0;
     663}
     664
     665//#include "../musl-1.1.18/include/bits/limits.h"
     666#define PAGE_SIZE 4096
     667
     668uint32_t  __CmdBase;
     669uint32_t  __CmdLimit;
     670
     671void *shell_brk(void *addr)
     672{
     673        if (addr == 0) {
     674                return (void *)((intptr_t)&__CmdBase + 0x20000);
     675        }
     676        if ((addr >= (intptr_t)&__CmdBase + 0x20000) && (addr < &__CmdLimit)) {
     677                return addr;
     678        }
     679        return (void *)-1;
     680}
     681
     682void *shell_mmap2(void *start, size_t length, int prot, int flags, int fd, off_t pgoffset)
     683{
     684        if (fd != -1)
     685                return -EINVAL;
     686
     687        if ((length >= 0) && (length <= (intptr_t)&__CmdLimit - (intptr_t)&__CmdBase - 0x20000)) {
     688                return &__CmdBase + 0x20000;
     689        }
     690        return (void *)-1;
     691}
     692
     693int shell_mprotect(void *addr, size_t len, int prot)
     694{
     695        //if ((addr >= (intptr_t)&__CmdBase + 0x20000) && ((intptr_t)addr + len < &__CmdLimit)) {
     696                return 0;
     697        //}
     698        //return -1;
     699}
Note: See TracChangeset for help on using the changeset viewer.