Changeset 441 for EcnlProtoTool/trunk/ntshell/src/io_stub.c
- Timestamp:
- Jul 10, 2020, 9:09:25 PM (4 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
EcnlProtoTool/trunk/ntshell/src/io_stub.c
r434 r441 1 1 /* 2 2 * TOPPERS PROJECT Home Network Working Group Software 3 * 3 * 4 4 * Copyright (C) 2017-2019 Cores Co., Ltd. Japan 5 * 5 * 6 6 * 上記著作権者は,以下の(1)~(4)の条件を満たす場合に限り,本ソフトウェ 7 7 * ア(本ソフトウェアを改変したものを含む.以下同じ)を使用・複製・改 … … 26 26 * 由に基づく請求からも,上記著作権者およびTOPPERSプロジェクトを 27 27 * 免責すること. 28 * 28 * 29 29 * 本ソフトウェアは,無保証で提供されているものである.上記著作権者お 30 30 * よびTOPPERSプロジェクトは,本ソフトウェアに関して,特定の使用目的 … … 32 32 * アの利用により直接的または間接的に生じたいかなる損害に関しても,そ 33 33 * の責任を負わない. 34 * 34 * 35 35 * @(#) $Id$ 36 36 */ … … 42 42 #include <t_stdlib.h> 43 43 #include <sil.h> 44 //#include <string.h>45 #include <setjmp.h>44 #include <string.h> 45 #include "syssvc/serial.h" 46 46 #include "syssvc/syslog.h" 47 #include <tinet_config.h>48 #include <netinet/in.h>49 #include <netinet/in_itron.h>50 #include <tinet_nic_defs.h>51 #include <tinet_cfg.h>52 #include <netinet/in_var.h>53 #include <net/ethernet.h>54 #include <net/if6_var.h>55 #include <net/net.h>56 #include <net/if_var.h>57 #include <netinet/udp_var.h>58 //#include <ethernet_api.h>59 47 #include "fdtable.h" 60 //#include <sys/stat.h>61 48 #include "util/ntstdio.h" 62 49 #include "usrcmd.h" 63 50 #include "core/ntlibc.h" 51 #include "kernel_cfg.h" 52 #include "target_syssvc.h" 53 54 struct SHELL_DIR { 55 FATFS_DIR dir; 56 struct dirent dirent; 57 }; 64 58 65 59 int fresult2errno(FRESULT res) … … 90 84 } 91 85 92 int shell_open(const char * path, int flags, void *arg) 93 { 94 FRESULT res; 95 struct _IO_FILE *fp; 86 static int file_close(struct SHELL_FILE *fp); 87 static size_t file_read(struct SHELL_FILE *fp, unsigned char *data, size_t len); 88 static size_t file_write(struct SHELL_FILE *fp, const unsigned char *data, size_t len); 89 static off_t file_seek(struct SHELL_FILE *fp, off_t ofs, int org); 90 static int file_ioctl(struct SHELL_FILE *fp, int req, void *arg); 91 static bool_t file_readable(struct SHELL_FILE *fp); 92 static bool_t file_writable(struct SHELL_FILE *fp); 93 static void file_delete(struct SHELL_FILE *fp); 94 95 static int dir_close(struct SHELL_FILE *fp); 96 static size_t dir_read(struct SHELL_FILE *fp, unsigned char *data, size_t len); 97 static size_t dir_write(struct SHELL_FILE *fp, const unsigned char *data, size_t len); 98 static off_t dir_seek(struct SHELL_FILE *fp, off_t ofs, int org); 99 static int dir_ioctl(struct SHELL_FILE *fp, int req, void *arg); 100 static bool_t dir_readable(struct SHELL_FILE *fp); 101 static bool_t dir_writable(struct SHELL_FILE *fp); 102 static void dir_delete(struct SHELL_FILE *fp); 103 104 IO_TYPE IO_TYPE_FILE = { file_close, file_read, file_write, file_seek, file_ioctl, file_readable, file_writable, file_delete }; 105 IO_TYPE IO_TYPE_DIR = { dir_close, dir_read, dir_write, dir_seek, dir_ioctl, dir_readable, dir_writable, dir_delete }; 106 107 int shell_open(const char *path, int flags, void *arg) 108 { 109 FRESULT res; 110 struct SHELL_FILE *fp; 96 111 97 112 if (flags & O_DIRECTORY) { 98 fp = new_ dir_fd(0);113 fp = new_fp(&IO_TYPE_DIR, 0, 0); 99 114 if (fp == NULL) 100 115 return -ENOMEM; 101 116 102 FATFS_DIR *dir = &fp->dir; 103 FRESULT res; 117 fp->exinf = malloc(sizeof(struct SHELL_DIR)); 118 memset(fp->exinf, 0, sizeof(struct SHELL_DIR)); 119 120 FATFS_DIR *dir = &((struct SHELL_DIR *)fp->exinf)->dir; 104 121 if ((res = f_opendir(dir, path)) != FR_OK) { 122 delete_fp(fp); 105 123 return fresult2errno(res); 106 124 } … … 108 126 } 109 127 110 fp = new_f ile_fd(0);128 fp = new_fp(&IO_TYPE_FILE, 0, 1); 111 129 if (fp == NULL) 112 130 return -ENOMEM; 131 132 fp->exinf = malloc(sizeof(FIL)); 133 memset(fp->exinf, 0, sizeof(FIL)); 113 134 114 135 BYTE fmd = 0; … … 146 167 } 147 168 148 if ((res = f_open( &fp->file, path, fmd)) == FR_OK) {169 if ((res = f_open((FIL *)fp->exinf, path, fmd)) == FR_OK) { 149 170 fp->handle = fp->fd; 150 171 return fp->fd; 151 172 } 152 173 174 delete_fp(fp); 153 175 return fresult2errno(res); 154 176 } 155 177 156 int file_close(struct _IO_FILE *fp)157 { 158 FRESULT res; 159 160 if ((res = f_close( &fp->file)) == FR_OK) {178 int file_close(struct SHELL_FILE *fp) 179 { 180 FRESULT res; 181 182 if ((res = f_close((FIL *)fp->exinf)) == FR_OK) { 161 183 return 0; 162 184 } … … 165 187 } 166 188 167 size_t file_read(struct _IO_FILE *fp, unsigned char *data, size_t len)189 size_t file_read(struct SHELL_FILE *fp, unsigned char *data, size_t len) 168 190 { 169 191 unsigned int ret = 0; 170 192 FRESULT res; 171 193 172 if ((res = f_read( &fp->file, data, len, &ret)) != FR_OK)194 if ((res = f_read((FIL *)fp->exinf, data, len, &ret)) != FR_OK) 173 195 return -EIO; 174 196 … … 176 198 } 177 199 178 size_t file_write(struct _IO_FILE *fp, const unsigned char *data, size_t len)200 size_t file_write(struct SHELL_FILE *fp, const unsigned char *data, size_t len) 179 201 { 180 202 unsigned int ret = 0; 181 203 FRESULT res; 182 204 183 if ((res = f_write( &fp->file, data, len, &ret)) != FR_OK)205 if ((res = f_write((FIL *)fp->exinf, data, len, &ret)) != FR_OK) 184 206 return -EIO; 185 207 … … 187 209 } 188 210 189 off_t file_seek(struct _IO_FILE *fp, off_t ptr, int dir)211 off_t file_seek(struct SHELL_FILE *fp, off_t ptr, int dir) 190 212 { 191 213 switch (dir) { … … 204 226 205 227 FRESULT res; 206 if ((res = f_seek( &fp->file, ptr, dir)) != FR_OK)228 if ((res = f_seek((FIL *)fp->exinf, ptr, dir)) != FR_OK) 207 229 return -EIO; 208 230 209 return fp->file.fptr;210 } 211 212 int file_ioctl(struct _IO_FILE *fp, int req, void *arg)231 return ((FIL *)fp->exinf)->fptr; 232 } 233 234 int file_ioctl(struct SHELL_FILE *fp, int req, void *arg) 213 235 { 214 236 DRESULT res; 215 237 216 if ((res = disk_ioctl( fp->file.fs->drv, req, arg) != RES_OK))238 if ((res = disk_ioctl(((FIL *)fp->exinf)->fs->drv, req, arg) != RES_OK)) 217 239 return -EINVAL; 218 240 … … 220 242 } 221 243 222 int shell_close(int fd) 223 { 224 struct _IO_FILE *fp = fd_to_fp(fd); 225 if (fp == NULL) 226 return -EBADF; 227 228 return fp->close(fp); 229 } 230 231 ssize_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; 236 237 return fp->read(fp, (unsigned char *)data, len); 238 } 239 240 int 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 255 ssize_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; 260 261 return fp->write(fp, (unsigned char *)data, len); 262 } 263 264 int 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 279 int 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; 244 bool_t file_readable(struct SHELL_FILE *fp) 245 { 246 return fp->readevt_w != fp->readevt_r; 247 } 248 249 bool_t file_writable(struct SHELL_FILE *fp) 250 { 251 return fp->writable && (fp->writeevt_w == fp->writeevt_r); 252 } 253 254 void file_delete(struct SHELL_FILE *fp) 255 { 256 free((FIL *)fp->exinf); 257 fp->exinf = NULL; 291 258 } 292 259 293 260 int shell_fstat(int fd, struct stat * st) 294 261 { 295 struct _IO_FILE *fp = fd_to_fp(fd);262 struct SHELL_FILE *fp = fd_to_fp(fd); 296 263 if (fp == NULL) 297 264 return -EBADF; … … 305 272 int shell_fsync(int fd) 306 273 { 307 struct _IO_FILE *fp = fd_to_fp(fd);274 struct SHELL_FILE *fp = fd_to_fp(fd); 308 275 if (fp == NULL) 309 276 return -EBADF; … … 313 280 int shell_ftruncate(int fd, off_t length) 314 281 { 315 struct _IO_FILE *fp = fd_to_fp(fd);282 struct SHELL_FILE *fp = fd_to_fp(fd); 316 283 if (fp == NULL) 317 284 return -EBADF; 318 285 319 286 FRESULT res; 320 if ((res = f_truncate( &fp->file)) != FR_OK)287 if ((res = f_truncate((FIL *)fp->exinf)) != FR_OK) 321 288 return fresult2errno(res); 322 289 … … 329 296 } 330 297 331 int sio_tcgetattr(int fd, struct termios *termios) 332 { 333 extern ntstdio_t ntstdio; 334 335 if (fd == STDIN_FILENO) { 336 memset(termios, 0, sizeof(*termios)); 337 338 if (ntstdio.option & NTSTDIO_OPTION_LINE_ECHO) { 339 termios->c_lflag |= ECHO; 340 } 341 else { 342 termios->c_lflag &= ~ECHO; 343 } 344 if (ntstdio.option & NTSTDIO_OPTION_CANON) { 345 termios->c_lflag |= ICANON; 346 } 347 else { 348 termios->c_lflag &= ~ICANON; 349 } 350 if (ntstdio.option & NTSTDIO_OPTION_LF_CR) { 351 termios->c_iflag |= INLCR; 352 } 353 else { 354 termios->c_iflag &= ~INLCR; 355 } 356 if (ntstdio.option & NTSTDIO_OPTION_LF_CRLF) { 357 termios->c_oflag |= ONLCR; 358 } 359 else { 360 termios->c_oflag &= ~ONLCR; 361 } 362 return 0; 363 } 364 shell_abort(); 365 return 0; 366 } 367 368 int sio_tcsetattr(int fd, int optional_actions, const struct termios *termios) 369 { 370 extern ntstdio_t ntstdio; 371 372 if ((fd == STDIN_FILENO) && (optional_actions == TCSANOW)) { 373 if (termios->c_lflag & ECHO) { 374 ntstdio.option |= NTSTDIO_OPTION_LINE_ECHO; 375 } 376 else { 377 ntstdio.option &= ~NTSTDIO_OPTION_LINE_ECHO; 378 } 379 if (termios->c_lflag & ICANON) { 380 ntstdio.option |= NTSTDIO_OPTION_CANON; 381 } 382 else { 383 ntstdio.option &= ~NTSTDIO_OPTION_CANON; 384 } 385 if (termios->c_iflag & INLCR) { 386 ntstdio.option |= NTSTDIO_OPTION_LF_CR; 387 } 388 else{ 389 ntstdio.option &= ~NTSTDIO_OPTION_LF_CR; 390 } 391 if (termios->c_oflag & ONLCR) { 392 ntstdio.option |= NTSTDIO_OPTION_LF_CRLF; 393 } 394 else { 395 ntstdio.option &= ~NTSTDIO_OPTION_LF_CRLF; 396 } 397 return 0; 398 } 399 shell_abort(); 400 return 0; 401 } 402 403 int shell_stat(const char *__restrict path, struct stat *__restrict st) 298 int shell_stat(const char *restrict path, struct stat *restrict st) 404 299 { 405 300 FILINFO fi; … … 434 329 st->st_size = fi.fsize; 435 330 st->st_mtim.tv_nsec = 0; 436 st->st_mtim.tv_sec = fi.fdate +fi.ftime;437 st->st_mode 331 st->st_mtim.tv_sec = (time_t)fi.fdate + (time_t)fi.ftime; 332 st->st_mode = (S_IRUSR | S_IRGRP | S_IROTH); 438 333 st->st_mode |= (fi.fattrib & AM_RDO) ? 0 : (S_IWUSR | S_IWGRP | S_IWOTH); 439 334 st->st_mode |= (fi.fattrib & (AM_DIR | AM_VOL)) ? S_IFDIR : S_IFREG; … … 442 337 } 443 338 444 int shell_lstat(const char * __restrict path, struct stat *__restrict st)339 int shell_lstat(const char *restrict path, struct stat *restrict st) 445 340 { 446 341 return shell_stat(path, st); … … 494 389 BYTE mask = AM_RDO | AM_SYS; // AM_ARC, AM_HID 495 390 496 if (mode & S_IREAD) {497 if ((mode & S_IWRITE) == 0) {391 if (mode & S_IREAD) { 392 if ((mode & S_IWRITE) == 0) { 498 393 attr |= AM_RDO; 499 394 } … … 503 398 } 504 399 505 if ((res = f_chmod(path, attr, mask)) != FR_OK) {400 if ((res = f_chmod(path, attr, mask)) != FR_OK) { 506 401 return fresult2errno(res); 507 402 } … … 516 411 BYTE mask = AM_RDO | AM_SYS; // AM_ARC, AM_HID 517 412 518 if (mode & S_IREAD) {519 if ((mode & S_IWRITE) == 0) {413 if (mode & S_IREAD) { 414 if ((mode & S_IWRITE) == 0) { 520 415 attr |= AM_RDO; 521 416 } … … 525 420 } 526 421 527 if ((res = f_chmod(path, attr, mask)) != FR_OK) {422 if ((res = f_chmod(path, attr, mask)) != FR_OK) { 528 423 return fresult2errno(res); 529 424 } … … 535 430 { 536 431 FRESULT ret; 537 if ((ret = f_getcwd(buf, size)) != FR_OK) {432 if ((ret = f_getcwd(buf, size)) != FR_OK) { 538 433 return NULL; 539 434 } … … 558 453 } 559 454 560 int dir_close(struct _IO_FILE *fp)561 { 562 FRESULT res; 563 if ((res = f_closedir(& fp->dir)) != FR_OK) {455 int dir_close(struct SHELL_FILE *fp) 456 { 457 FRESULT res; 458 if ((res = f_closedir(&((struct SHELL_DIR *)fp->exinf)->dir)) != FR_OK) { 564 459 return fresult2errno(res); 565 460 } … … 573 468 return -EINVAL; 574 469 575 struct _IO_FILE *fp = fd_to_fp(fd);470 struct SHELL_FILE *fp = fd_to_fp(fd); 576 471 if (fp == NULL) 577 472 return -EBADF; … … 584 479 #endif 585 480 FRESULT res; 586 if ((res = f_readdir(& fp->dir, &fno)) != FR_OK || fno.fname[0] == '\0') {481 if ((res = f_readdir(&((struct SHELL_DIR *)fp->exinf)->dir, &fno)) != FR_OK || fno.fname[0] == '\0') { 587 482 return fresult2errno(res); 588 483 } … … 598 493 } 599 494 600 size_t dir_read(struct _IO_FILE *fp, unsigned char *data, size_t len)495 size_t dir_read(struct SHELL_FILE *fp, unsigned char *data, size_t len) 601 496 { 602 497 return -EPERM; 603 498 } 604 499 605 size_t dir_write(struct _IO_FILE *fp, const unsigned char *data, size_t len)500 size_t dir_write(struct SHELL_FILE *fp, const unsigned char *data, size_t len) 606 501 { 607 502 return -EPERM; 608 503 } 609 504 610 off_t dir_seek(struct _IO_FILE *fp, off_t ptr, int dir)505 off_t dir_seek(struct SHELL_FILE *fp, off_t ptr, int dir) 611 506 { 612 507 FRESULT res; … … 616 511 617 512 if (ptr == 0) { 618 if ((res = f_rewinddir(& fp->dir)) != FR_OK) {513 if ((res = f_rewinddir(&((struct SHELL_DIR *)fp->exinf)->dir)) != FR_OK) { 619 514 return fresult2errno(res); 620 515 } … … 627 522 fno.lfsize = sizeof lfn; 628 523 #endif 629 if ((res = f_rewinddir(& fp->dir)) != FR_OK) {524 if ((res = f_rewinddir(&((struct SHELL_DIR *)fp->exinf)->dir)) != FR_OK) { 630 525 return fresult2errno(res); 631 526 } 632 527 633 528 for (int i = 0; i < ptr; i++) { 634 if ((res = f_readdir(& fp->dir, &fno)) != FR_OK || fno.fname[0] == '\0') {529 if ((res = f_readdir(&((struct SHELL_DIR *)fp->exinf)->dir, &fno)) != FR_OK || fno.fname[0] == '\0') { 635 530 return fresult2errno(res); 636 531 } … … 641 536 } 642 537 643 int dir_ioctl(struct _IO_FILE *fp, int req, void *arg)538 int dir_ioctl(struct SHELL_FILE *fp, int req, void *arg) 644 539 { 645 540 return -EINVAL; 541 } 542 543 bool_t dir_readable(struct SHELL_FILE *fp) 544 { 545 return fp->readevt_w != fp->readevt_r; 546 } 547 548 bool_t dir_writable(struct SHELL_FILE *fp) 549 { 550 return false; 551 } 552 553 void dir_delete(struct SHELL_FILE *fp) 554 { 555 free((struct SHELL_DIR *)fp->exinf); 556 fp->exinf = NULL; 646 557 } 647 558 … … 662 573 return 0; 663 574 } 664 665 //#include "../musl-1.1.18/include/bits/limits.h"666 #define PAGE_SIZE 4096667 668 uint32_t __CmdBase;669 uint32_t __CmdLimit;670 671 void *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 682 void *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 693 int 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.