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