1 1.12 martin /* $NetBSD: loadfile_ecoff.c,v 1.12 2008/04/28 20:24:06 martin Exp $ */ 2 1.1 thorpej 3 1.1 thorpej /*- 4 1.1 thorpej * Copyright (c) 1997 The NetBSD Foundation, Inc. 5 1.1 thorpej * All rights reserved. 6 1.1 thorpej * 7 1.1 thorpej * This code is derived from software contributed to The NetBSD Foundation 8 1.1 thorpej * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility, 9 1.1 thorpej * NASA Ames Research Center and by Christos Zoulas. 10 1.1 thorpej * 11 1.1 thorpej * Redistribution and use in source and binary forms, with or without 12 1.1 thorpej * modification, are permitted provided that the following conditions 13 1.1 thorpej * are met: 14 1.1 thorpej * 1. Redistributions of source code must retain the above copyright 15 1.1 thorpej * notice, this list of conditions and the following disclaimer. 16 1.1 thorpej * 2. Redistributions in binary form must reproduce the above copyright 17 1.1 thorpej * notice, this list of conditions and the following disclaimer in the 18 1.1 thorpej * documentation and/or other materials provided with the distribution. 19 1.1 thorpej * 20 1.1 thorpej * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 21 1.1 thorpej * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 22 1.1 thorpej * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 23 1.1 thorpej * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 24 1.1 thorpej * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 25 1.1 thorpej * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 26 1.1 thorpej * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 27 1.1 thorpej * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 28 1.1 thorpej * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 29 1.1 thorpej * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 30 1.1 thorpej * POSSIBILITY OF SUCH DAMAGE. 31 1.1 thorpej */ 32 1.1 thorpej 33 1.2 thorpej #ifdef _STANDALONE 34 1.2 thorpej #include <lib/libsa/stand.h> 35 1.2 thorpej #include <lib/libkern/libkern.h> 36 1.5 perry #else 37 1.2 thorpej #include <stdio.h> 38 1.2 thorpej #include <string.h> 39 1.2 thorpej #include <errno.h> 40 1.2 thorpej #include <stdlib.h> 41 1.5 perry #include <unistd.h> 42 1.2 thorpej #include <fcntl.h> 43 1.2 thorpej #include <err.h> 44 1.5 perry #endif 45 1.2 thorpej 46 1.5 perry #include <sys/param.h> 47 1.2 thorpej #include <sys/exec.h> 48 1.2 thorpej 49 1.2 thorpej #include "loadfile.h" 50 1.2 thorpej 51 1.2 thorpej #ifdef BOOT_ECOFF 52 1.2 thorpej 53 1.2 thorpej int 54 1.11 tsutsui loadfile_coff(int fd, struct ecoff_exechdr *coff, u_long *marks, int flags) 55 1.1 thorpej { 56 1.1 thorpej paddr_t offset = marks[MARK_START]; 57 1.1 thorpej paddr_t minp = ~0, maxp = 0, pos; 58 1.9 isaki ssize_t nr; 59 1.4 reinoud 60 1.4 reinoud /* some ports dont use the offset */ 61 1.4 reinoud offset = offset; 62 1.1 thorpej 63 1.1 thorpej /* Read in text. */ 64 1.1 thorpej if (lseek(fd, ECOFF_TXTOFF(coff), SEEK_SET) == -1) { 65 1.1 thorpej WARN(("lseek text")); 66 1.1 thorpej return 1; 67 1.1 thorpej } 68 1.1 thorpej 69 1.1 thorpej if (coff->a.tsize != 0) { 70 1.1 thorpej if (flags & LOAD_TEXT) { 71 1.1 thorpej PROGRESS(("%lu", coff->a.tsize)); 72 1.9 isaki nr = READ(fd, coff->a.text_start, coff->a.tsize); 73 1.9 isaki if (nr == -1) { 74 1.9 isaki return 1; 75 1.9 isaki } 76 1.9 isaki if (nr != coff->a.tsize) { 77 1.10 isaki errno = EIO; 78 1.1 thorpej return 1; 79 1.1 thorpej } 80 1.1 thorpej } 81 1.1 thorpej else { 82 1.1 thorpej if (lseek(fd, coff->a.tsize, SEEK_CUR) == -1) { 83 1.1 thorpej WARN(("read text")); 84 1.1 thorpej return 1; 85 1.1 thorpej } 86 1.1 thorpej } 87 1.1 thorpej if (flags & (COUNT_TEXT|LOAD_TEXT)) { 88 1.1 thorpej pos = coff->a.text_start; 89 1.1 thorpej if (minp > pos) 90 1.1 thorpej minp = pos; 91 1.1 thorpej pos += coff->a.tsize; 92 1.1 thorpej if (maxp < pos) 93 1.1 thorpej maxp = pos; 94 1.1 thorpej } 95 1.1 thorpej } 96 1.1 thorpej 97 1.1 thorpej /* Read in data. */ 98 1.1 thorpej if (coff->a.dsize != 0) { 99 1.1 thorpej if (flags & LOAD_DATA) { 100 1.1 thorpej PROGRESS(("+%lu", coff->a.dsize)); 101 1.9 isaki nr = READ(fd, coff->a.data_start, coff->a.dsize); 102 1.9 isaki if (nr == -1) { 103 1.9 isaki WARN(("read data")); 104 1.9 isaki return 1; 105 1.9 isaki } 106 1.9 isaki if (nr != coff->a.dsize) { 107 1.10 isaki errno = EIO; 108 1.1 thorpej WARN(("read data")); 109 1.1 thorpej return 1; 110 1.1 thorpej } 111 1.1 thorpej } 112 1.1 thorpej if (flags & (COUNT_DATA|LOAD_DATA)) { 113 1.1 thorpej pos = coff->a.data_start; 114 1.1 thorpej if (minp > pos) 115 1.1 thorpej minp = pos; 116 1.1 thorpej pos += coff->a.dsize; 117 1.1 thorpej if (maxp < pos) 118 1.1 thorpej maxp = pos; 119 1.1 thorpej } 120 1.1 thorpej } 121 1.1 thorpej 122 1.1 thorpej /* Zero out bss. */ 123 1.1 thorpej if (coff->a.bsize != 0) { 124 1.1 thorpej if (flags & LOAD_BSS) { 125 1.1 thorpej PROGRESS(("+%lu", coff->a.bsize)); 126 1.1 thorpej BZERO(coff->a.bss_start, coff->a.bsize); 127 1.1 thorpej } 128 1.1 thorpej if (flags & (COUNT_BSS|LOAD_BSS)) { 129 1.1 thorpej pos = coff->a.bss_start; 130 1.1 thorpej if (minp > pos) 131 1.1 thorpej minp = pos; 132 1.1 thorpej pos = coff->a.bsize; 133 1.1 thorpej if (maxp < pos) 134 1.1 thorpej maxp = pos; 135 1.1 thorpej } 136 1.1 thorpej } 137 1.1 thorpej 138 1.1 thorpej marks[MARK_START] = LOADADDR(minp); 139 1.1 thorpej marks[MARK_ENTRY] = LOADADDR(coff->a.entry); 140 1.1 thorpej marks[MARK_NSYM] = 1; /* XXX: Kernel needs >= 0 */ 141 1.1 thorpej marks[MARK_SYM] = LOADADDR(maxp); 142 1.1 thorpej marks[MARK_END] = LOADADDR(maxp); 143 1.8 martin marks[MARK_DATA] = LOADADDR(coff->a.data_start); 144 1.1 thorpej return 0; 145 1.1 thorpej } 146 1.2 thorpej 147 1.2 thorpej #endif /* BOOT_ECOFF */ 148