Changeset 331 for EcnlProtoTool/trunk/ntshell/src/io_stub.c
- Timestamp:
- Jan 21, 2018, 12:10:09 AM (6 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
EcnlProtoTool/trunk/ntshell/src/io_stub.c
r321 r331 35 35 * @(#) $Id$ 36 36 */ 37 #include "shellif.h" 37 38 #include <stdint.h> 38 #include <stdio.h>39 #include <sys/unistd.h>40 #include <limits.h>41 #include <fcntl.h>42 39 #include "ff.h" 43 40 #include <kernel.h> … … 45 42 #include <t_stdlib.h> 46 43 #include <sil.h> 47 #include <stdlib.h>48 44 #include <string.h> 49 #include <stdio.h>50 45 #include <setjmp.h> 51 46 #include "syssvc/syslog.h" … … 61 56 #include <net/if_var.h> 62 57 #include <netinet/udp_var.h> 63 #include <ethernet_api.h> 64 #include "ff.h" 58 //#include <ethernet_api.h> 65 59 #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 65 int 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 92 int 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; 77 113 78 114 BYTE fmd = 0; … … 115 151 } 116 152 117 return -1;153 return fresult2errno(res); 118 154 } 119 155 … … 126 162 } 127 163 128 return - 1;164 return -EINVAL; 129 165 } 130 166 … … 135 171 136 172 if ((res = f_read(&fp->file, data, len, &ret)) != FR_OK) 137 return - 1;173 return -EIO; 138 174 139 175 return ret; … … 146 182 147 183 if ((res = f_write(&fp->file, data, len, &ret)) != FR_OK) 148 return - 1;184 return -EIO; 149 185 150 186 return ret; 151 187 } 152 188 189 off_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 212 int 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 153 222 int shell_close(int fd) 154 223 { 155 224 struct _IO_FILE *fp = fd_to_fp(fd); 156 225 if (fp == NULL) 157 return - 1;226 return -EBADF; 158 227 159 228 return fp->close(fp); 160 229 } 161 230 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;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; 167 236 168 237 return fp->read(fp, (unsigned char *)data, len); 169 238 } 170 239 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; 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; 176 260 177 261 return fp->write(fp, (unsigned char *)data, len); 178 262 } 179 263 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; 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; 191 291 } 192 292 … … 195 295 struct _IO_FILE *fp = fd_to_fp(fd); 196 296 if (fp == NULL) 197 return - 1;297 return -EBADF; 198 298 199 299 memset(st, 0, sizeof(*st)); … … 203 303 } 204 304 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;305 int 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 313 int shell_ftruncate(int fd, off_t length) 314 { 315 struct _IO_FILE *fp = fd_to_fp(fd); 316 if (fp == NULL) 317 return -EBADF; 218 318 219 319 FRESULT res; 220 320 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 326 int shell_fcntl(int fd, int cmd, void *arg) 327 { 328 return shell_ioctl(fd, cmd, arg); 329 } 330 331 int sio_tcgetattr(int fd, struct termios *termios) 235 332 { 236 333 extern ntstdio_t ntstdio; … … 265 362 return 0; 266 363 } 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 368 int sio_tcsetattr(int fd, int optional_actions, const struct termios *termios) 272 369 { 273 370 extern ntstdio_t ntstdio; … … 300 397 return 0; 301 398 } 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 403 int shell_stat(const char *__restrict path, struct stat *__restrict st) 307 404 { 308 405 FILINFO fi; … … 313 410 fi.lfsize = sizeof lfn; 314 411 #endif 315 if ( strcmp(path, ".") == 0) {412 if (ntlibc_strcmp(path, ".") == 0) { 316 413 char cwd[_MAX_LFN]; 317 414 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); 321 418 // ルートディレクトリの場合 322 419 if (cwd[l - 2] == ':' && cwd[l - 1] == '/') { 323 420 st->st_size = 0; 324 st->st_mtime = 0; 421 st->st_mtim.tv_nsec = 0; 422 st->st_mtim.tv_sec = 0; 325 423 st->st_mode = S_IFDIR; 326 424 return 0; 327 425 } 328 426 if ((ret = f_stat(cwd, &fi)) != FR_OK) { 329 return -1;427 return fresult2errno(ret); 330 428 } 331 429 } 332 430 else if ((ret = f_stat(path, &fi)) != FR_OK) { 333 return -1;431 return fresult2errno(ret); 334 432 } 335 433 336 434 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; 338 437 st->st_mode = (S_IRUSR | S_IRGRP | S_IROTH); 339 438 st->st_mode |= (fi.fattrib & AM_RDO) ? 0 : (S_IWUSR | S_IWGRP | S_IWOTH); … … 343 442 } 344 443 345 int shell_link(void) 346 { 347 return -1; 444 int shell_lstat(const char *__restrict path, struct stat *__restrict st) 445 { 446 return shell_stat(path, st); 447 } 448 449 int shell_link(const char *a, const char *b) 450 { 451 return -EPERM; 348 452 } 349 453 … … 353 457 354 458 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 464 int shell_rmdir(const char *path) 361 465 { 362 466 FRESULT res; 363 467 364 468 if ((res = f_unlink(path)) != FR_OK) 365 return - 1;469 return -EIO; 366 470 367 471 return 0; … … 373 477 374 478 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 486 int shell_mkdir(const char *path, mode_t mode) 380 487 { 381 488 FRESULT res; 382 489 383 490 if ((res = f_mkdir(path)) != FR_OK) 384 return -1;491 return fresult2errno(res); 385 492 386 493 BYTE attr = 0; … … 397 504 398 505 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 re t;506 return fresult2errno(res); 507 } 508 509 return 0; 510 } 511 512 int shell_chmod(const char *path, mode_t mode) 513 { 514 FRESULT res; 408 515 BYTE attr = 0; 409 516 BYTE mask = AM_RDO | AM_SYS; // AM_ARC, AM_HID … … 418 525 } 419 526 420 if((re t= 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 534 char *shell_getcwd(char *buf, size_t size) 428 535 { 429 536 FRESULT ret; … … 435 542 } 436 543 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; 544 int 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 554 int shell_chroot(const char *path) 555 { 556 shell_abort(); 557 return -EPERM; 558 } 559 560 int 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 570 int 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 481 579 FILINFO fno; 482 580 #if _USE_LFN … … 485 583 fno.lfsize = sizeof lfn; 486 584 #endif 487 FRESULT re t;488 if ((re t = 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); 490 588 } 491 589 492 590 memset(de, 0, sizeof(*de)); 493 591 #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)); 495 593 #else 496 strlcpy(de->d_name, fno.fname, sizeof(de->d_name));594 ntlibc_strlcpy(de->d_name, fno.fname, sizeof(de->d_name)); 497 595 #endif 498 596 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 600 size_t dir_read(struct _IO_FILE *fp, unsigned char *data, size_t len) 601 { 602 return -EPERM; 603 } 604 605 size_t dir_write(struct _IO_FILE *fp, const unsigned char *data, size_t len) 606 { 607 return -EPERM; 608 } 609 610 off_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 643 int dir_ioctl(struct _IO_FILE *fp, int req, void *arg) 644 { 645 return -EINVAL; 646 } 647 648 pid_t shell_getpid(void) 522 649 { 523 650 return 1; 524 651 } 652 653 int 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 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.