1 /* $NetBSD: load_coff.cpp,v 1.3 2002/02/11 17:05:45 uch Exp $ */ 2 3 /*- 4 * Copyright (c) 2001 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by UCHIYAMA Yasushi. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. All advertising materials mentioning features or use of this software 19 * must display the following acknowledgement: 20 * This product includes software developed by the NetBSD 21 * Foundation, Inc. and its contributors. 22 * 4. Neither the name of The NetBSD Foundation nor the names of its 23 * contributors may be used to endorse or promote products derived 24 * from this software without specific prior written permission. 25 * 26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 36 * POSSIBILITY OF SUCH DAMAGE. 37 */ 38 39 #include <load.h> 40 #include <load_coff.h> 41 #undef DPRINTF // trash coff_machdep.h 's define. 42 #include <console.h> 43 #include <memory.h> 44 #include <file.h> 45 46 CoffLoader::CoffLoader(Console *&cons, MemoryManager *&mem) 47 : Loader(cons, mem) 48 { 49 memset(&_eh, 0, sizeof(struct coff_exechdr)); 50 _fh = &_eh.f; 51 _ah = &_eh.a; 52 53 DPRINTF((TEXT("Loader: COFF\n"))); 54 } 55 56 CoffLoader::~CoffLoader(void) 57 { 58 } 59 60 BOOL 61 CoffLoader::setFile(File *&file) 62 { 63 Loader::setFile(file); 64 65 /* read COFF header and check it */ 66 return read_header(); 67 } 68 69 size_t 70 CoffLoader::memorySize() 71 { 72 size_t sz = _ah->a_tsize + _ah->a_dsize; 73 74 DPRINTF((TEXT("file size: text 0x%x + data 0x%x = 0x%x byte\n"), 75 _ah->a_tsize, _ah->a_dsize, sz)); 76 return sz; 77 } 78 79 kaddr_t 80 CoffLoader::jumpAddr() 81 { 82 83 DPRINTF((TEXT("kernel entry address: 0x%08x\n"), _ah->a_entry)); 84 85 return _ah->a_entry; 86 } 87 88 BOOL 89 CoffLoader::load() 90 { 91 size_t filesz; 92 size_t memsz; 93 vaddr_t kv; 94 off_t fileofs; 95 96 /* start tag chain */ 97 _load_segment_start(); 98 99 /* text */ 100 filesz = memsz = _ah->a_tsize; 101 kv = _ah->a_tstart; 102 fileofs = COFF_ROUND(N_TXTOFF(_fh, _ah), 16); 103 DPRINTF((TEXT("[text]"))); 104 _load_segment(kv, memsz, fileofs, filesz); 105 /* data */ 106 fileofs += filesz; 107 filesz = memsz = _ah->a_dsize; 108 kv = _ah->a_dstart; 109 DPRINTF((TEXT("[data]"))); 110 _load_segment(kv, memsz, fileofs, filesz); 111 /* bss */ 112 filesz = 0; 113 memsz = _ah->a_bsize; 114 kv = _ah->a_dstart + _ah->a_dsize; 115 fileofs = 0; 116 DPRINTF((TEXT("[bss ]"))); 117 _load_segment(kv, memsz, fileofs, filesz); 118 119 /* tag chain still opening */ 120 return _load_success(); 121 } 122 123 BOOL 124 CoffLoader::read_header(void) 125 { 126 #ifndef COFF_BADMAG 127 DPRINTF((TEXT("coff loader not implemented.\n"))); 128 return FALSE; 129 #else 130 // read COFF header 131 _file->read(&_eh, sizeof(struct coff_exechdr), 0); 132 133 // check COFF Magic. 134 if (COFF_BADMAG(_fh)) { 135 DPRINTF((TEXT("not a COFF file.\n"))); 136 return FALSE; 137 } 138 139 return TRUE; 140 #endif 141 } 142