t2ex/t2ex_source/t2ex/load/src/src_file.c | bare source | permlink (0.01 seconds) |
1: /* 2: *---------------------------------------------------------------------- 3: * T2EX Software Package 4: * 5: * Copyright 2012 by Ken Sakamura. 6: * This software is distributed under the latest version of T-License 2.x. 7: *---------------------------------------------------------------------- 8: * 9: * Released by T-Engine Forum(http://www.t-engine.org/) at 2012/12/12. 10: * Modified by TRON Forum(http://www.tron.org/) at 2015/06/04. 11: * 12: *---------------------------------------------------------------------- 13: */ 14: /* 15: * This software package is available for use, modification, 16: * and redistribution in accordance with the terms of the attached 17: * T-License 2.x. 18: * If you want to redistribute the source code, you need to attach 19: * the T-License 2.x document. 20: * There's no obligation to publish the content, and no obligation 21: * to disclose it to the TRON Forum if you have modified the 22: * software package. 23: * You can also distribute the modified source code. In this case, 24: * please register the modification to T-Kernel traceability service. 25: * People can know the history of modifications by the service, 26: * and can be sure that the version you have inherited some 27: * modification of a particular version or not. 28: * 29: * http://trace.tron.org/tk/?lang=en 30: * http://trace.tron.org/tk/?lang=ja 31: * 32: * As per the provisions of the T-License 2.x, TRON Forum ensures that 33: * the portion of the software that is copyrighted by Ken Sakamura or 34: * the TRON Forum does not infringe the copyrights of a third party. 35: * However, it does not make any warranty other than this. 36: * DISCLAIMER: TRON Forum and Ken Sakamura shall not be held 37: * responsible for any consequences or damages caused directly or 38: * indirectly by the use of this software package. 39: * 40: * The source codes in bsd_source.tar.gz in this software package are 41: * derived from NetBSD or OpenBSD and not covered under T-License 2.x. 42: * They need to be changed or redistributed according to the 43: * representation of each source header. 44: */ 45: 46: /* 47: * src_file.c 48: * 49: * T2EX: program load functions 50: * file program source 51: */ 52: 53: #include <basic.h> 54: #include <t2ex/fs.h> 55: #include "source.h" 56: 57: /* 58: * Read program 59: */ 60: LOCAL ER readFileSource(struct LoadSource* ldr, long ofs, void* adr, size_t sz) 61: { 62: size_t pos; 63: ER er = 0; 64: 65: /* Seek if current offset != ofs */ 66: if (ofs != ldr->li.file.cur) { 67: er = fs_lseek(ldr->li.file.fd, ofs, SEEK_SET); 68: if ( er < E_OK ) { 69: return er; 70: } 71: ldr->li.file.cur = ofs; 72: } 73: 74: /* Read from file */ 75: for ( pos = 0; pos < sz; ) { 76: er = fs_read(ldr->li.file.fd, (B*)adr + pos, sz - pos); 77: if ( er <= E_OK ) { 78: break; 79: } 80: pos += er; 81: ldr->li.file.cur += er; 82: } 83: 84: return (pos > 0)? pos: er; 85: } 86: 87: /* 88: * Quit loading 89: */ 90: LOCAL ER closeFileSource(struct LoadSource* ldr) 91: { 92: return fs_close(ldr->li.file.fd); 93: } 94: 95: /* 96: * Start loading 97: */ 98: EXPORT ER openFileSource(struct LoadSource* ldr, const struct pm* prog) 99: { 100: int fd = fs_open((char*)prog->pmhdr, O_RDONLY); 101: if ( fd < E_OK ) { 102: return fd; 103: } 104: 105: ldr->type = prog->pmtype; 106: ldr->read = readFileSource; 107: ldr->close = closeFileSource; 108: ldr->li.file.fd = fd; 109: ldr->li.file.cur = 0; 110: 111: return E_OK; 112: }