zbsdmod.c revision 1.1.6.2 1 1.1.6.2 ad /* $NetBSD: zbsdmod.c,v 1.1.6.2 2007/01/12 01:01:03 ad Exp $ */
2 1.1.6.2 ad /* $OpenBSD: zbsdmod.c,v 1.7 2005/05/02 02:45:29 uwe Exp $ */
3 1.1.6.2 ad
4 1.1.6.2 ad /*
5 1.1.6.2 ad * Copyright (c) 2005 Uwe Stuehler <uwe (at) bsdx.de>
6 1.1.6.2 ad *
7 1.1.6.2 ad * Permission to use, copy, modify, and distribute this software for any
8 1.1.6.2 ad * purpose with or without fee is hereby granted, provided that the above
9 1.1.6.2 ad * copyright notice and this permission notice appear in all copies.
10 1.1.6.2 ad *
11 1.1.6.2 ad * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 1.1.6.2 ad * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 1.1.6.2 ad * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 1.1.6.2 ad * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 1.1.6.2 ad * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 1.1.6.2 ad * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 1.1.6.2 ad * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 1.1.6.2 ad */
19 1.1.6.2 ad
20 1.1.6.2 ad /*
21 1.1.6.2 ad * Zaurus NetBSD bootstrap loader.
22 1.1.6.2 ad */
23 1.1.6.2 ad
24 1.1.6.2 ad #include "compat_linux.h"
25 1.1.6.2 ad
26 1.1.6.2 ad #include <machine/bootinfo.h>
27 1.1.6.2 ad
28 1.1.6.2 ad #define BOOTARGS_BUFSIZ 256
29 1.1.6.2 ad
30 1.1.6.2 ad #define ZBOOTDEV_MAJOR 99
31 1.1.6.2 ad #define ZBOOTDEV_MODE 0222
32 1.1.6.2 ad #define ZBOOTDEV_NAME "zboot"
33 1.1.6.2 ad #define ZBOOTMOD_NAME "zbsdmod"
34 1.1.6.2 ad
35 1.1.6.2 ad /* Prototypes */
36 1.1.6.2 ad int init_module(void);
37 1.1.6.2 ad void cleanup_module(void);
38 1.1.6.2 ad
39 1.1.6.2 ad static ssize_t zbsdmod_write(struct file *, const char *, size_t, loff_t *);
40 1.1.6.2 ad static int zbsdmod_open(struct inode *, struct file *);
41 1.1.6.2 ad static int zbsdmod_close(struct inode *, struct file *);
42 1.1.6.2 ad
43 1.1.6.2 ad static void elf32bsdboot(void);
44 1.1.6.2 ad
45 1.1.6.2 ad static struct file_operations fops = {
46 1.1.6.2 ad 0, /* struct module *owner */
47 1.1.6.2 ad 0, /* lseek */
48 1.1.6.2 ad 0, /* read */
49 1.1.6.2 ad zbsdmod_write, /* write */
50 1.1.6.2 ad 0, /* readdir */
51 1.1.6.2 ad 0, /* poll */
52 1.1.6.2 ad 0, /* ioctl */
53 1.1.6.2 ad 0, /* mmap */
54 1.1.6.2 ad zbsdmod_open, /* open */
55 1.1.6.2 ad 0, /* flush */
56 1.1.6.2 ad zbsdmod_close, /* release */
57 1.1.6.2 ad 0, /* sync */
58 1.1.6.2 ad 0, /* async */
59 1.1.6.2 ad 0, /* check media change */
60 1.1.6.2 ad 0, /* revalidate */
61 1.1.6.2 ad 0, /* lock */
62 1.1.6.2 ad };
63 1.1.6.2 ad
64 1.1.6.2 ad static int isopen;
65 1.1.6.2 ad static loff_t position;
66 1.1.6.2 ad
67 1.1.6.2 ad /* Outcast local variables to avoid stack usage in elf32bsdboot(). */
68 1.1.6.2 ad static int cpsr;
69 1.1.6.2 ad static unsigned int sz;
70 1.1.6.2 ad static int i;
71 1.1.6.2 ad static vaddr_t minv, maxv, posv;
72 1.1.6.2 ad static vaddr_t elfv, shpv;
73 1.1.6.2 ad static int *addr;
74 1.1.6.2 ad static vaddr_t *esymp;
75 1.1.6.2 ad static Elf_Shdr *shp;
76 1.1.6.2 ad static Elf_Off off;
77 1.1.6.2 ad static int havesyms;
78 1.1.6.2 ad
79 1.1.6.2 ad /* The maximum size of a kernel image is restricted to 5MB. */
80 1.1.6.2 ad static u_int bsdimage[1310720]; /* XXX use kmalloc() */
81 1.1.6.2 ad static char bootargs[BOOTARGS_BUFSIZ];
82 1.1.6.2 ad
83 1.1.6.2 ad /*
84 1.1.6.2 ad * Boot the loaded BSD kernel image, or return if an error is found.
85 1.1.6.2 ad * Part of this routine is borrowed from sys/lib/libsa/loadfile.c.
86 1.1.6.2 ad */
87 1.1.6.2 ad static void
88 1.1.6.2 ad elf32bsdboot(void)
89 1.1.6.2 ad {
90 1.1.6.2 ad
91 1.1.6.2 ad #define elf ((Elf32_Ehdr *)bsdimage)
92 1.1.6.2 ad #define phdr ((Elf32_Phdr *)((char *)elf + elf->e_phoff))
93 1.1.6.2 ad
94 1.1.6.2 ad if (memcmp(elf->e_ident, ELFMAG, SELFMAG) != 0 ||
95 1.1.6.2 ad elf->e_ident[EI_CLASS] != ELFCLASS32)
96 1.1.6.2 ad return;
97 1.1.6.2 ad
98 1.1.6.2 ad minv = (vaddr_t)~0;
99 1.1.6.2 ad maxv = (vaddr_t)0;
100 1.1.6.2 ad posv = (vaddr_t)0;
101 1.1.6.2 ad esymp = 0;
102 1.1.6.2 ad
103 1.1.6.2 ad /*
104 1.1.6.2 ad * Get min and max addresses used by the loaded kernel.
105 1.1.6.2 ad */
106 1.1.6.2 ad for (i = 0; i < elf->e_phnum; i++) {
107 1.1.6.2 ad
108 1.1.6.2 ad if (phdr[i].p_type != PT_LOAD ||
109 1.1.6.2 ad (phdr[i].p_flags & (PF_W|PF_R|PF_X)) == 0)
110 1.1.6.2 ad continue;
111 1.1.6.2 ad
112 1.1.6.2 ad #define IS_TEXT(p) (p.p_flags & PF_X)
113 1.1.6.2 ad #define IS_DATA(p) ((p.p_flags & PF_X) == 0)
114 1.1.6.2 ad #define IS_BSS(p) (p.p_filesz < p.p_memsz)
115 1.1.6.2 ad /*
116 1.1.6.2 ad * XXX: Assume first address is lowest
117 1.1.6.2 ad */
118 1.1.6.2 ad if (IS_TEXT(phdr[i]) || IS_DATA(phdr[i])) {
119 1.1.6.2 ad posv = phdr[i].p_vaddr;
120 1.1.6.2 ad if (minv > posv)
121 1.1.6.2 ad minv = posv;
122 1.1.6.2 ad posv += phdr[i].p_filesz;
123 1.1.6.2 ad if (maxv < posv)
124 1.1.6.2 ad maxv = posv;
125 1.1.6.2 ad }
126 1.1.6.2 ad if (IS_DATA(phdr[i]) && IS_BSS(phdr[i])) {
127 1.1.6.2 ad posv += phdr[i].p_memsz;
128 1.1.6.2 ad if (maxv < posv)
129 1.1.6.2 ad maxv = posv;
130 1.1.6.2 ad }
131 1.1.6.2 ad /*
132 1.1.6.2 ad * 'esym' is the first word in the .data section,
133 1.1.6.2 ad * and marks the end of the symbol table.
134 1.1.6.2 ad */
135 1.1.6.2 ad if (IS_DATA(phdr[i]) && !IS_BSS(phdr[i]))
136 1.1.6.2 ad esymp = (vaddr_t *)phdr[i].p_vaddr;
137 1.1.6.2 ad }
138 1.1.6.2 ad
139 1.1.6.2 ad __asm volatile ("mrs %0, cpsr_all" : "=r" (cpsr));
140 1.1.6.2 ad cpsr |= 0xc0; /* set FI */
141 1.1.6.2 ad __asm volatile ("msr cpsr_all, %0" :: "r" (cpsr));
142 1.1.6.2 ad
143 1.1.6.2 ad /*
144 1.1.6.2 ad * Copy the boot arguments.
145 1.1.6.2 ad */
146 1.1.6.2 ad sz = BOOTARGS_BUFSIZ;
147 1.1.6.2 ad while (sz > 0) {
148 1.1.6.2 ad sz--;
149 1.1.6.2 ad ((char *)minv - BOOTARGS_BUFSIZ)[sz] = bootargs[sz];
150 1.1.6.2 ad }
151 1.1.6.2 ad
152 1.1.6.2 ad /*
153 1.1.6.2 ad * Set up pointers to copied ELF and section headers.
154 1.1.6.2 ad */
155 1.1.6.2 ad #define roundup(x, y) ((((x)+((y)-1))/(y))*(y))
156 1.1.6.2 ad elfv = maxv = roundup(maxv, sizeof(long));
157 1.1.6.2 ad maxv += sizeof(Elf_Ehdr);
158 1.1.6.2 ad
159 1.1.6.2 ad sz = elf->e_shnum * sizeof(Elf_Shdr);
160 1.1.6.2 ad shp = (Elf_Shdr *)((vaddr_t)elf + elf->e_shoff);
161 1.1.6.2 ad shpv = maxv;
162 1.1.6.2 ad maxv += roundup(sz, sizeof(long));
163 1.1.6.2 ad
164 1.1.6.2 ad /*
165 1.1.6.2 ad * Now load the symbol sections themselves. Make sure the
166 1.1.6.2 ad * sections are aligned, and offsets are relative to the
167 1.1.6.2 ad * copied ELF header. Don't bother with string tables if
168 1.1.6.2 ad * there are no symbol sections.
169 1.1.6.2 ad */
170 1.1.6.2 ad off = roundup((sizeof(Elf_Ehdr) + sz), sizeof(long));
171 1.1.6.2 ad for (havesyms = i = 0; i < elf->e_shnum; i++)
172 1.1.6.2 ad if (shp[i].sh_type == SHT_SYMTAB)
173 1.1.6.2 ad havesyms = 1;
174 1.1.6.2 ad for (i = 0; i < elf->e_shnum; i++) {
175 1.1.6.2 ad if (shp[i].sh_type == SHT_SYMTAB ||
176 1.1.6.2 ad shp[i].sh_type == SHT_STRTAB) {
177 1.1.6.2 ad if (havesyms) {
178 1.1.6.2 ad sz = shp[i].sh_size;
179 1.1.6.2 ad while (sz > 0) {
180 1.1.6.2 ad sz--;
181 1.1.6.2 ad ((char *)maxv)[sz] =
182 1.1.6.2 ad ((char *)elf +
183 1.1.6.2 ad shp[i].sh_offset)[sz];
184 1.1.6.2 ad }
185 1.1.6.2 ad }
186 1.1.6.2 ad maxv += roundup(shp[i].sh_size, sizeof(long));
187 1.1.6.2 ad shp[i].sh_offset = off;
188 1.1.6.2 ad off += roundup(shp[i].sh_size, sizeof(long));
189 1.1.6.2 ad }
190 1.1.6.2 ad }
191 1.1.6.2 ad
192 1.1.6.2 ad /*
193 1.1.6.2 ad * Copy the ELF and section headers.
194 1.1.6.2 ad */
195 1.1.6.2 ad sz = sizeof(Elf_Ehdr);
196 1.1.6.2 ad while (sz > 0) {
197 1.1.6.2 ad sz--;
198 1.1.6.2 ad ((char *)elfv)[sz] = ((char *)elf)[sz];
199 1.1.6.2 ad }
200 1.1.6.2 ad sz = elf->e_shnum * sizeof(Elf_Shdr);
201 1.1.6.2 ad while (sz > 0) {
202 1.1.6.2 ad sz--;
203 1.1.6.2 ad ((char *)shpv)[sz] = ((char *)shp)[sz];
204 1.1.6.2 ad }
205 1.1.6.2 ad
206 1.1.6.2 ad /*
207 1.1.6.2 ad * Frob the copied ELF header to give information relative
208 1.1.6.2 ad * to elfv.
209 1.1.6.2 ad */
210 1.1.6.2 ad ((Elf_Ehdr *)elfv)->e_phoff = 0;
211 1.1.6.2 ad ((Elf_Ehdr *)elfv)->e_shoff = sizeof(Elf_Ehdr);
212 1.1.6.2 ad ((Elf_Ehdr *)elfv)->e_phentsize = 0;
213 1.1.6.2 ad ((Elf_Ehdr *)elfv)->e_phnum = 0;
214 1.1.6.2 ad
215 1.1.6.2 ad /*
216 1.1.6.2 ad * Tell locore.S where the symbol table ends, and arrange
217 1.1.6.2 ad * to skip esym when loading the data section.
218 1.1.6.2 ad */
219 1.1.6.2 ad if (esymp != 0)
220 1.1.6.2 ad *esymp = (vaddr_t)maxv;
221 1.1.6.2 ad for (i = 0; esymp != 0 && i < elf->e_phnum; i++) {
222 1.1.6.2 ad if (phdr[i].p_type != PT_LOAD ||
223 1.1.6.2 ad (phdr[i].p_flags & (PF_W|PF_R|PF_X)) == 0)
224 1.1.6.2 ad continue;
225 1.1.6.2 ad if (phdr[i].p_vaddr == (vaddr_t)esymp) {
226 1.1.6.2 ad phdr[i].p_vaddr = (vaddr_t)((char *)phdr[i].p_vaddr + sizeof(long));
227 1.1.6.2 ad phdr[i].p_offset = (vaddr_t)((char *)phdr[i].p_offset + sizeof(long));
228 1.1.6.2 ad phdr[i].p_filesz -= sizeof(long);
229 1.1.6.2 ad break;
230 1.1.6.2 ad }
231 1.1.6.2 ad }
232 1.1.6.2 ad
233 1.1.6.2 ad /*
234 1.1.6.2 ad * Load text and data.
235 1.1.6.2 ad */
236 1.1.6.2 ad for (i = 0; i < elf->e_phnum; i++) {
237 1.1.6.2 ad if (phdr[i].p_type != PT_LOAD ||
238 1.1.6.2 ad (phdr[i].p_flags & (PF_W|PF_R|PF_X)) == 0)
239 1.1.6.2 ad continue;
240 1.1.6.2 ad
241 1.1.6.2 ad if (IS_TEXT(phdr[i]) || IS_DATA(phdr[i])) {
242 1.1.6.2 ad sz = phdr[i].p_filesz;
243 1.1.6.2 ad while (sz > 0) {
244 1.1.6.2 ad sz--;
245 1.1.6.2 ad ((char *)phdr[i].p_vaddr)[sz] =
246 1.1.6.2 ad (((char *)elf) + phdr[i].p_offset)[sz];
247 1.1.6.2 ad }
248 1.1.6.2 ad }
249 1.1.6.2 ad }
250 1.1.6.2 ad
251 1.1.6.2 ad addr = (int *)(elf->e_entry);
252 1.1.6.2 ad __asm volatile (
253 1.1.6.2 ad "mov r0, %0;"
254 1.1.6.2 ad "mov r2, #0;"
255 1.1.6.2 ad "mcr p15, 0, r2, c7, c7, 0;"
256 1.1.6.2 ad "mov r2, r2;"
257 1.1.6.2 ad "sub pc, pc, #4;"
258 1.1.6.2 ad "mov r1, #(0x00000010 | 0x00000020);"
259 1.1.6.2 ad "mcr p15, 0, r1, c1, c0, 0;"
260 1.1.6.2 ad "mcr p15, 0, r2, c8, c7, 0;"
261 1.1.6.2 ad "mov r2, r2;"
262 1.1.6.2 ad "sub pc, pc, #4;"
263 1.1.6.2 ad "mov pc, r0" :: "r"(addr) : "r0","r1","r2");
264 1.1.6.2 ad }
265 1.1.6.2 ad
266 1.1.6.2 ad /*
267 1.1.6.2 ad * Initialize the LKM.
268 1.1.6.2 ad */
269 1.1.6.2 ad int
270 1.1.6.2 ad init_module(void)
271 1.1.6.2 ad {
272 1.1.6.2 ad struct proc_dir_entry *entry;
273 1.1.6.2 ad int rc;
274 1.1.6.2 ad
275 1.1.6.2 ad rc = register_chrdev(ZBOOTDEV_MAJOR, ZBOOTDEV_NAME, &fops);
276 1.1.6.2 ad if (rc != 0) {
277 1.1.6.2 ad printk("%s: register_chrdev(%d, ...): error %d\n",
278 1.1.6.2 ad ZBOOTMOD_NAME, -rc);
279 1.1.6.2 ad return 1;
280 1.1.6.2 ad }
281 1.1.6.2 ad
282 1.1.6.2 ad entry = proc_mknod(ZBOOTDEV_NAME, ZBOOTDEV_MODE | S_IFCHR,
283 1.1.6.2 ad &proc_root, MKDEV(ZBOOTDEV_MAJOR, 0));
284 1.1.6.2 ad if (entry == (struct proc_dir_entry *)0) {
285 1.1.6.2 ad (void)unregister_chrdev(ZBOOTDEV_MAJOR, ZBOOTDEV_NAME);
286 1.1.6.2 ad return 1;
287 1.1.6.2 ad }
288 1.1.6.2 ad
289 1.1.6.2 ad printk("%s: NetBSD/" MACHINE " bootstrap device is %d,0\n",
290 1.1.6.2 ad ZBOOTMOD_NAME, ZBOOTDEV_MAJOR);
291 1.1.6.2 ad
292 1.1.6.2 ad return 0;
293 1.1.6.2 ad }
294 1.1.6.2 ad
295 1.1.6.2 ad /*
296 1.1.6.2 ad * Cleanup - undo whatever init_module did.
297 1.1.6.2 ad */
298 1.1.6.2 ad void
299 1.1.6.2 ad cleanup_module(void)
300 1.1.6.2 ad {
301 1.1.6.2 ad
302 1.1.6.2 ad (void)unregister_chrdev(ZBOOTDEV_MAJOR, ZBOOTDEV_NAME);
303 1.1.6.2 ad remove_proc_entry(ZBOOTDEV_NAME, &proc_root);
304 1.1.6.2 ad
305 1.1.6.2 ad printk("%s: NetBSD/" MACHINE " bootstrap device unloaded\n",
306 1.1.6.2 ad ZBOOTMOD_NAME);
307 1.1.6.2 ad }
308 1.1.6.2 ad
309 1.1.6.2 ad static ssize_t
310 1.1.6.2 ad zbsdmod_write(struct file *f, const char *buf, size_t len, loff_t *offp)
311 1.1.6.2 ad {
312 1.1.6.2 ad
313 1.1.6.2 ad if (len < 1)
314 1.1.6.2 ad return 0;
315 1.1.6.2 ad
316 1.1.6.2 ad if (*offp + len >= sizeof(bsdimage))
317 1.1.6.2 ad return EFBIG;
318 1.1.6.2 ad
319 1.1.6.2 ad memcpy(((char *)bsdimage) + *offp, buf, len);
320 1.1.6.2 ad
321 1.1.6.2 ad *offp += len;
322 1.1.6.2 ad if (*offp > position)
323 1.1.6.2 ad position = *offp;
324 1.1.6.2 ad
325 1.1.6.2 ad return len;
326 1.1.6.2 ad }
327 1.1.6.2 ad
328 1.1.6.2 ad static int
329 1.1.6.2 ad zbsdmod_open(struct inode *ino, struct file *f)
330 1.1.6.2 ad {
331 1.1.6.2 ad
332 1.1.6.2 ad /* XXX superuser check */
333 1.1.6.2 ad
334 1.1.6.2 ad if (isopen)
335 1.1.6.2 ad return -EBUSY;
336 1.1.6.2 ad
337 1.1.6.2 ad isopen = 1;
338 1.1.6.2 ad position = 0;
339 1.1.6.2 ad
340 1.1.6.2 ad return 0;
341 1.1.6.2 ad }
342 1.1.6.2 ad
343 1.1.6.2 ad static int
344 1.1.6.2 ad zbsdmod_close(struct inode *ino, struct file *f)
345 1.1.6.2 ad {
346 1.1.6.2 ad
347 1.1.6.2 ad if (!isopen)
348 1.1.6.2 ad return -EBUSY;
349 1.1.6.2 ad
350 1.1.6.2 ad if (position > 0) {
351 1.1.6.2 ad printk("%s: loaded %d bytes\n", ZBOOTDEV_NAME,
352 1.1.6.2 ad position);
353 1.1.6.2 ad
354 1.1.6.2 ad if (position < BOOTARGS_BUFSIZ) {
355 1.1.6.2 ad *(u_int *)bootargs = BOOTARGS_MAGIC;
356 1.1.6.2 ad bootargs[position + sizeof(u_int)] = '\0';
357 1.1.6.2 ad memcpy(bootargs + sizeof(u_int), bsdimage,
358 1.1.6.2 ad position);
359 1.1.6.2 ad } else {
360 1.1.6.2 ad elf32bsdboot();
361 1.1.6.2 ad printk("%s: boot failed\n", ZBOOTDEV_NAME);
362 1.1.6.2 ad }
363 1.1.6.2 ad }
364 1.1.6.2 ad isopen = 0;
365 1.1.6.2 ad
366 1.1.6.2 ad return 0;
367 1.1.6.2 ad }
368