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