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