bootxx.c revision 1.3 1 /* $NetBSD: bootxx.c,v 1.3 2007/02/21 22:59:41 thorpej Exp $ */
2
3 /*-
4 * Copyright (c) 2004, 2005 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 #ifdef _STANDALONE
40 #include <lib/libsa/stand.h>
41 #include <lib/libkern/libkern.h>
42 #endif
43
44 #include <sys/types.h>
45 #include <sys/param.h>
46 #include "bootxx.h"
47
48 #include <sys/exec_ecoff.h>
49 #include <sys/exec_elf.h>
50
51 #include <machine/sector.h>
52 #include <machine/sbd.h>
53
54 #include "common.h"
55
56 #define FILHSZ (sizeof(struct ecoff_filehdr))
57 #define SCNHSZ (sizeof(struct ecoff_scnhdr))
58 #define N_TXTOFF(f, a) \
59 ((a)->vstamp < 23 ? \
60 ((FILHSZ +(f)->f_opthdr +(f)->f_nscns * SCNHSZ + 7) & ~7) : \
61 ((FILHSZ +(f)->f_opthdr +(f)->f_nscns * SCNHSZ + 15) & ~15))
62
63 int main(void);
64 int loader(const char *, uint32_t *);
65 int load_elf(uint8_t *, uint32_t *);
66 int load_coff(uint8_t *, uint32_t *);
67 int dk_read(int, int, void *);
68
69 const char *errmsg[] = {
70 [BERR_PDINFO] = "No PDINFO",
71 [BERR_RDVTOC] = "VTOC read error",
72 [BERR_VTOC] = "VTOC bad magic",
73 [BERR_NOBFS] = "No BFS partition",
74 [BERR_RDINODE] = "I-node read error",
75 [BERR_NOROOT] = "No root",
76 [BERR_RDDIRENT] = "Dirent read error",
77 [BERR_NOFILE] = "No such a file",
78 [BERR_RDFILE] = "File read error",
79 [BERR_FILEMAGIC] = "Bad file magic",
80 [BERR_AOUTHDR] = "Not a OMAGIC",
81 [BERR_ELFHDR] = "Not a ELF",
82 [BERR_TARHDR] = "Read tar file",
83 [BERR_TARMAGIC] = "Bad tar magic",
84 };
85
86 const char *boottab[] = {
87 "boot", /* NetBSD (bfs,ustarfs) */
88 "iopboot", /* EWS-UX (bfs) */
89 0 /* terminate */
90 };
91
92 int __dk_unit, __dk_type;
93 bool (*fd_position)(uint32_t, uint32_t *, int *);
94
95 int
96 main(void)
97 {
98 char msg[] = "[+++] NetBSD/ews4800mips >> ";
99 const char *p;
100 const char **boot;
101 uint32_t entry;
102 int err;
103
104 #ifdef _STANDALONE
105 int i, fd_format;
106
107 boot_device(&__dk_type, &__dk_unit, &fd_format);
108 msg[1] = __dk_type + '0';
109 msg[2] = __dk_unit + '0';
110 msg[3] = FS_SIGNATURE;
111 for (i = 0; msg[i] != 0; i++)
112 ROM_PUTC(32 + i * 12, 536, msg[i]);
113
114 if (__dk_type == NVSRAM_BOOTDEV_FLOPPYDISK) {
115 fd_position = blk_to_2d_position;
116 if (fd_format == FD_FORMAT_2HD) {
117 fd_position = blk_to_2hd_position;
118 __dk_unit |= 0x1000000; /* | 2HD flag */
119 }
120 }
121 #else
122 printf("%s\n", msg);
123 sector_init("/dev/rsd1p", DEV_BSIZE);
124 sector_read(SDBOOT_PDINFOADDR, PDINFO_SECTOR);
125 #endif
126
127 for (boot = boottab; *boot; boot++) {
128 if ((err = loader(*boot, &entry)) == 0) {
129 #ifdef _STANDALONE
130 ROM_PUTC(0, 0, '\n');
131 ROM_PUTC(0, 0, '\r');
132 return entry;
133 #else
134 printf("entry=%p\n", (void *)entry);
135 sector_fini();
136 return 0;
137 #endif
138 }
139 p = errmsg[err];
140 #ifdef _STANDALONE
141 for (i = 0; p[i] != 0; i++)
142 ROM_PUTC(600 + i * 12, 536, p[i]);
143 #else
144 DPRINTF("***ERROR *** %s\n", p);
145 #endif
146 }
147
148 #ifdef _STANDALONE
149 while (/*CONSTCOND*/1)
150 ;
151 /* NOTREACHED */
152 #else
153 sector_fini();
154 #endif
155 return 0;
156 }
157
158 int
159 loader(const char *fname, uint32_t *entry)
160 {
161 int err;
162 uint16_t mag;
163 size_t sz;
164 #ifdef _STANDALONE
165 int i;
166
167 for (i = 0; fname[i] != 0; i++)
168 ROM_PUTC(380 + i * 12, 536, fname[i]);
169 #else
170 printf("%s\n", fname);
171 #endif
172
173 if ((err = fileread(fname, &sz)) != 0)
174 return err;
175
176 DPRINTF("%s found. %d bytes.\n", fname, sz);
177
178 mag = *(uint16_t *)SDBOOT_SCRATCHADDR;
179 if (mag == 0x7f45)
180 return load_elf((uint8_t *)SDBOOT_SCRATCHADDR, entry);
181 else if (mag == ECOFF_MAGIC_MIPSEB)
182 return load_coff((uint8_t *)SDBOOT_SCRATCHADDR, entry);
183
184 return BERR_FILEMAGIC;
185 }
186
187 int
188 load_elf(uint8_t *buf, uint32_t *entry)
189 {
190 Elf32_Ehdr *e = (void *)buf;
191 Elf32_Phdr *p;
192
193 if (e->e_ident[EI_MAG2] != 'L' || e->e_ident[EI_MAG3] != 'F' ||
194 e->e_ident[EI_CLASS] != ELFCLASS32 ||
195 e->e_ident[EI_DATA] != ELFDATA2MSB ||
196 e->e_type != ET_EXEC ||
197 e->e_machine != EM_MIPS)
198 return BERR_ELFHDR;
199
200 BASSERT(e->e_phentsize == sizeof(Elf32_Phdr));
201 p = (void *)(buf + e->e_phoff);
202 #ifdef _STANDALONE
203 memcpy((void *)p->p_vaddr, buf + p->p_offset, p->p_filesz);
204 p++;
205 memcpy((void *)p->p_vaddr, buf + p->p_offset, p->p_filesz);
206 #else
207 DPRINTF("ELF entry point 0x%08x\n", e->e_entry);
208 DPRINTF("[text] 0x%08x 0x%x %dbyte.\n", p->p_vaddr, p->p_offset,
209 p->p_filesz);
210 p++;
211 DPRINTF("[data] 0x%08x 0x%x %dbyte.\n", p->p_vaddr, p->p_offset,
212 p->p_filesz);
213 #endif
214 *entry = e->e_entry;
215
216 return 0;
217 }
218
219 int
220 load_coff(uint8_t *p, uint32_t *entry)
221 {
222 struct ecoff_exechdr *eh = (void *)p;
223 struct ecoff_aouthdr *a = &eh->a;
224 struct ecoff_filehdr *f = &eh->f;
225
226 if (a->magic != ECOFF_OMAGIC)
227 return BERR_AOUTHDR;
228
229 p += N_TXTOFF(f, a);
230 DPRINTF("file offset = 0x%x\n", N_TXTOFF(f, a));
231 #ifdef _STANDALONE
232 memcpy((void *)a->text_start, p, a->tsize);
233 memcpy((void *)a->data_start, p + a->tsize, a->dsize);
234 #else
235 DPRINTF("COFF entry point 0x%08lx\n", a->entry);
236 DPRINTF("[text] 0x%08lx %ld byte.\n", a->text_start, a->tsize);
237 DPRINTF("[data] 0x%08lx %ld byte.\n", a->data_start, a->dsize);
238 #endif
239
240 *entry = a->entry;
241
242 return 0;
243 }
244
245 int
246 dk_read(int sector, int count, void *buf)
247 {
248 #ifdef _STANDALONE
249 int i, cnt;
250 uint32_t pos;
251 uint8_t *p = buf;
252
253 if (__dk_type == NVSRAM_BOOTDEV_HARDDISK) { /* DISK */
254 if ((ROM_DK_READ(__dk_unit, sector, count, p) & 0x7f) != 0)
255 return 1;
256 } else { /* FD */
257 for (i = 0; i < count; i++, p += 512) {
258 fd_position(sector + i, &pos, &cnt);
259 if ((ROM_FD_READ(__dk_unit, pos, cnt, p) & 0x7f) != 0)
260 return 1;
261 }
262 }
263 #else
264 sector_read_n(buf, sector, count);
265 #endif
266 return 0;
267 }
268