1 /* $NetBSD: load_coff.cpp,v 1.2 2001/05/08 18:51:23 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 DPRINTF((TEXT("kernel entry address: 0x%08x\n"), _ah->a_entry)); 83 return _ah->a_entry; 84 } 85 86 BOOL 87 CoffLoader::load() 88 { 89 size_t filesz; 90 size_t memsz; 91 vaddr_t kv; 92 off_t fileofs; 93 94 /* start tag chain */ 95 _load_segment_start(); 96 97 /* text */ 98 filesz = memsz = _ah->a_tsize; 99 kv = _ah->a_tstart; 100 fileofs = COFF_ROUND(N_TXTOFF(_fh, _ah), 16); 101 DPRINTF((TEXT("[text]"))); 102 _load_segment(kv, memsz, fileofs, filesz); 103 /* data */ 104 fileofs += filesz; 105 filesz = memsz = _ah->a_dsize; 106 kv = _ah->a_dstart; 107 DPRINTF((TEXT("[data]"))); 108 _load_segment(kv, memsz, fileofs, filesz); 109 /* bss */ 110 filesz = 0; 111 memsz = _ah->a_bsize; 112 kv = _ah->a_dstart + _ah->a_dsize; 113 fileofs = 0; 114 DPRINTF((TEXT("[bss ]"))); 115 _load_segment(kv, memsz, fileofs, filesz); 116 117 /* tag chain still opening */ 118 119 return TRUE; 120 } 121 122 BOOL 123 CoffLoader::read_header(void) 124 { 125 #ifndef COFF_BADMAG 126 DPRINTF((TEXT("coff loader not implemented.\n"))); 127 return FALSE; 128 #else 129 // read COFF header 130 _file->read(&_eh, sizeof(struct coff_exechdr), 0); 131 132 // check COFF Magic. 133 if (COFF_BADMAG(_fh)) { 134 DPRINTF((TEXT("not a COFF file.\n"))); 135 return FALSE; 136 } 137 138 return TRUE; 139 #endif 140 } 141