xref: /netbsd/sys/dev/kloader.c (revision 08686a64)
1 /*	$NetBSD: kloader.c,v 1.26 2014/01/25 10:14:29 skrll Exp $	*/
2 
3 /*-
4  * Copyright (c) 2001, 2002, 2004 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  * POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 __KERNEL_RCSID(0, "$NetBSD: kloader.c,v 1.26 2014/01/25 10:14:29 skrll Exp $");
31 
32 #include "debug_kloader.h"
33 
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/malloc.h>
37 #include <sys/proc.h>
38 #include <sys/vnode.h>
39 #include <sys/namei.h>
40 #include <sys/fcntl.h>
41 #define	ELFSIZE	32
42 #include <sys/exec_elf.h>
43 
44 #include <uvm/uvm.h>
45 
46 #include <machine/kloader.h>
47 
48 #define	PRINTF(fmt, args...)	printf("kloader: " fmt, ##args)
49 
50 #ifdef KLOADER_DEBUG
51 int	kloader_debug = 1;
52 #define	DPRINTF(fmt, args...)						\
53 	if (kloader_debug)						\
54 		printf("%s: " fmt, __func__ , ##args)
55 #define	_DPRINTF(fmt, args...)						\
56 	if (kloader_debug)						\
57 		printf(fmt, ##args)
58 #define	DPRINTFN(n, fmt, args...)					\
59 	if (kloader_debug > (n))					\
60 		printf("%s: " fmt, __func__ , ##args)
61 #define	_DPRINTFN(n, fmt, args...)					\
62 	if (kloader_debug > (n))					\
63 		printf(fmt, ##args)
64 #define	STATIC
65 #else
66 #define	DPRINTF(fmt, args...)		((void)0)
67 #define	_DPRINTF(fmt, args...)		((void)0)
68 #define	DPRINTFN(n, fmt, args...)	((void)0)
69 #define	_DPRINTFN(n, fmt, args...)	((void)0)
70 #define	STATIC	static
71 #endif
72 
73 struct kloader {
74 	struct pglist pg_head;
75 	struct vm_page *cur_pg;		/* XXX use bus_dma(9) */
76 	struct kloader_page_tag *cur_tag;
77 	struct vnode *vp;
78 	struct kloader_page_tag *tagstart;
79 	struct kloader_bootinfo *bootinfo;
80 	struct kloader_bootinfo *rebootinfo;
81 	vaddr_t loader_sp;
82 	kloader_bootfunc_t *loader;
83 	int setuped;
84 	int called;
85 	struct kloader_ops *ops;
86 };
87 
88 #define	BUCKET_SIZE	(PAGE_SIZE - sizeof(struct kloader_page_tag))
89 #define	KLOADER_LWP	(&lwp0)
90 STATIC struct kloader kloader;
91 
92 #define	ROUND4(x)	(((x) + 3) & ~3)
93 
94 STATIC int kloader_load(void);
95 
96 STATIC int kloader_alloc_memory(size_t);
97 STATIC struct kloader_page_tag *kloader_get_tag(vaddr_t);
98 STATIC void kloader_from_file(vaddr_t, off_t, size_t);
99 STATIC void kloader_copy(vaddr_t, const void *, size_t);
100 STATIC void kloader_zero(vaddr_t, size_t);
101 
102 STATIC void kloader_load_segment(Elf_Phdr *);
103 
104 STATIC struct vnode *kloader_open(const char *);
105 STATIC void kloader_close(void);
106 STATIC int kloader_read(size_t, size_t, void *);
107 
108 #ifdef KLOADER_DEBUG
109 STATIC void kloader_pagetag_dump(void);
110 #endif
111 
112 void
113 __kloader_reboot_setup(struct kloader_ops *ops, const char *filename)
114 {
115 
116 	if (kloader.bootinfo == NULL) {
117 		PRINTF("No bootinfo.\n");
118 		return;
119 	}
120 
121 	if (ops == NULL || ops->jump == NULL || ops->boot == NULL) {
122 		PRINTF("No boot operations.\n");
123 		return;
124 	}
125 	kloader.ops = ops;
126 
127 	if (kloader.called++ == 0) {
128 		PRINTF("kernel file name: %s\n", filename);
129 		kloader.vp = kloader_open(filename);
130 		if (kloader.vp == NULL)
131 			return;
132 
133 		if (kloader_load() == 0) {
134 			kloader.setuped = TRUE;
135 #ifdef KLOADER_DEBUG
136 			kloader_pagetag_dump();
137 #endif
138 		}
139 		kloader_close();
140 	} else {
141 		/* Fatal case. reboot from DDB etc. */
142 		kloader_reboot();
143 	}
144 }
145 
146 
147 void
148 kloader_reboot(void)
149 {
150 
151 	if (kloader.setuped) {
152 		PRINTF("Rebooting...\n");
153 		(*kloader.ops->jump)(kloader.loader, kloader.loader_sp,
154 		    kloader.rebootinfo, kloader.tagstart);
155 	}
156 
157 	if (kloader.ops->reset != NULL) {
158 		PRINTF("Resetting...\n");
159 		(*kloader.ops->reset)();
160 	}
161 	while (/*CONSTCOND*/1)
162 		continue;
163 	/* NOTREACHED */
164 }
165 
166 
167 int
168 kloader_load(void)
169 {
170 	Elf_Ehdr eh;
171 	Elf_Phdr *ph, *p;
172 	Elf_Shdr *sh;
173 	Elf_Addr entry;
174 	vaddr_t kv;
175 	size_t sz;
176 	size_t shstrsz;
177 	char *shstrtab;
178 	int symndx, strndx;
179 	size_t ksymsz;
180 	struct kloader_bootinfo nbi; /* new boot info */
181 	char *oldbuf, *newbuf;
182 	char **ap;
183 	int i;
184 
185 	ph = NULL;
186 	sh = NULL;
187 	shstrtab = NULL;
188 
189 	/* read kernel's ELF header */
190 	kloader_read(0, sizeof(Elf_Ehdr), &eh);
191 
192 	if (eh.e_ident[EI_MAG0] != ELFMAG0 ||
193 	    eh.e_ident[EI_MAG1] != ELFMAG1 ||
194 	    eh.e_ident[EI_MAG2] != ELFMAG2 ||
195 	    eh.e_ident[EI_MAG3] != ELFMAG3) {
196 		PRINTF("not an ELF file\n");
197 		goto err;
198 	}
199 
200 	/* read program headers */
201 	sz = eh.e_phentsize * eh.e_phnum;
202 	if ((ph = malloc(sz, M_TEMP, M_NOWAIT)) == NULL) {
203 		PRINTF("can't allocate program header table.\n");
204 		goto err;
205 	}
206 	if (kloader_read(eh.e_phoff, sz, ph) != 0) {
207 		PRINTF("program header read error.\n");
208 		goto err;
209 	}
210 
211 	/* read section headers */
212 	sz = eh.e_shentsize * eh.e_shnum;
213 	if ((sh = malloc(sz, M_TEMP, M_NOWAIT)) == NULL) {
214 		PRINTF("can't allocate section header table.\n");
215 		goto err;
216 	}
217 	if (kloader_read(eh.e_shoff, eh.e_shentsize * eh.e_shnum, sh) != 0) {
218 		PRINTF("section header read error.\n");
219 		goto err;
220 	}
221 
222 	/* read section names */
223 	shstrsz = ROUND4(sh[eh.e_shstrndx].sh_size);
224 	shstrtab = malloc(shstrsz, M_TEMP, M_NOWAIT);
225 	if (shstrtab == NULL) {
226 		PRINTF("unable to allocate memory for .shstrtab\n");
227 		goto err;
228 	}
229 	DPRINTF("reading 0x%x bytes of .shstrtab at 0x%x\n",
230 	    sh[eh.e_shstrndx].sh_size, sh[eh.e_shstrndx].sh_offset);
231 	kloader_read(sh[eh.e_shstrndx].sh_offset, sh[eh.e_shstrndx].sh_size,
232 	    shstrtab);
233 
234 	/* save entry point, code to construct symbol table overwrites it */
235 	entry = eh.e_entry;
236 
237 	/*
238 	 * Calculate memory size
239 	 */
240 	sz = 0;
241 
242 	/* loadable segments */
243 	for (i = 0; i < eh.e_phnum; i++) {
244 		if (ph[i].p_type == PT_LOAD) {
245 			DPRINTF("segment %d size = file 0x%x memory 0x%x\n",
246 			    i, ph[i].p_filesz, ph[i].p_memsz);
247 #ifdef KLOADER_ZERO_BSS
248 			sz += round_page(ph[i].p_memsz);
249 #else
250 			sz += round_page(ph[i].p_filesz);
251 #endif
252 			sz += PAGE_SIZE; /* compensate for partial last tag */
253 		}
254 	}
255 
256 	if (sz == 0)		/* nothing to load? */
257 		goto err;
258 
259 	/* symbols/strings sections */
260 	symndx = strndx = -1;
261 	for (i = 0; i < eh.e_shnum; i++) {
262 		if (strcmp(shstrtab + sh[i].sh_name, ".symtab") == 0)
263 			symndx = i;
264 		else if (strcmp(shstrtab + sh[i].sh_name, ".strtab") == 0)
265 			strndx = i;
266 		else if (i != eh.e_shstrndx)
267 			/* while here, mark all other sections as unused */
268 			sh[i].sh_type = SHT_NULL;
269 	}
270 
271 	if (symndx < 0 || strndx < 0) {
272 		if (symndx < 0)
273 			PRINTF("no .symtab section\n");
274 		if (strndx < 0)
275 			PRINTF("no .strtab section\n");
276 		ksymsz = SELFMAG; /* just a bad magic */
277 	} else {
278 		ksymsz = sizeof(Elf_Ehdr)
279 		    + eh.e_shentsize * eh.e_shnum
280 		    + shstrsz		/* rounded to 4 bytes */
281 		    + sh[symndx].sh_size
282 		    + sh[strndx].sh_size;
283 		DPRINTF("ksyms size = 0x%zx\n", ksymsz);
284 	}
285 	sz += ROUND4(ksymsz);
286 
287 	/* boot info for the new kernel */
288 	sz += sizeof(struct kloader_bootinfo);
289 
290 	/* get memory for new kernel */
291 	if (kloader_alloc_memory(sz) != 0)
292 		goto err;
293 
294 	/*
295 	 * Copy new kernel in.
296 	 */
297 	kv = 0;			/* XXX: -Wuninitialized */
298 	for (i = 0, p = ph; i < eh.e_phnum; i++, p++) {
299 		if (p->p_type == PT_LOAD) {
300 			kloader_load_segment(p);
301 			kv = p->p_vaddr + ROUND4(p->p_memsz);
302 		}
303 	}
304 
305 	/*
306 	 * Construct symbol table for ksyms.
307 	 */
308 	if (symndx < 0 || strndx < 0) {
309 		kloader_zero(kv, SELFMAG);
310 		kv += SELFMAG;
311 	} else {
312 		Elf_Off eoff;
313 		off_t symoff, stroff;
314 
315 		/* save offsets of .symtab and .strtab before we change them */
316 		symoff = sh[symndx].sh_offset;
317 		stroff = sh[strndx].sh_offset;
318 
319 		/* no loadable segments */
320 		eh.e_entry = 0;
321 		eh.e_phnum = 0;
322 		eh.e_phoff = 0;
323 
324 		/* change offsets to reflect new layout */
325 		eoff = sizeof(Elf_Ehdr);
326 		eh.e_shoff = eoff;
327 
328 		eoff += eh.e_shentsize * eh.e_shnum;
329 		sh[eh.e_shstrndx].sh_offset = eoff;
330 
331 		eoff += shstrsz;
332 		sh[symndx].sh_offset = eoff;
333 
334 		eoff += sh[symndx].sh_size;
335 		sh[strndx].sh_offset = eoff;
336 
337 		/* local copies massaged, can serve them now */
338 		DPRINTF("ksyms ELF header\n");
339 		kloader_copy(kv, &eh, sizeof(Elf_Ehdr));
340 		kv += sizeof(Elf_Ehdr);
341 
342 		DPRINTF("ksyms section headers\n");
343 		kloader_copy(kv, sh, eh.e_shentsize * eh.e_shnum);
344 		kv += eh.e_shentsize * eh.e_shnum;
345 
346 		DPRINTF("ksyms .shstrtab\n");
347 		kloader_copy(kv, shstrtab, shstrsz);
348 		kv += shstrsz;
349 
350 		DPRINTF("ksyms .symtab\n");
351 		kloader_from_file(kv, symoff, sh[symndx].sh_size);
352 		kv += sh[symndx].sh_size;
353 
354 		DPRINTF("ksyms .strtab\n");
355 		kloader_from_file(kv, stroff, ROUND4(sh[strndx].sh_size));
356 		kv += ROUND4(sh[strndx].sh_size);
357 	}
358 
359 	/*
360 	 * Create boot info to pass to the new kernel.
361 	 * All pointers in it are *not* valid until the new kernel runs!
362 	 */
363 
364 	/* get a private copy of current bootinfo to vivisect */
365 	memcpy(&nbi, kloader.bootinfo, sizeof(struct kloader_bootinfo));
366 
367 	/* new kernel entry point */
368 	nbi.entry = entry;
369 
370 	/* where args currently are, see kloader_bootinfo_set() */
371 	oldbuf = &kloader.bootinfo->_argbuf[0];
372 
373 	/* where args *will* be after boot code copied them */
374 	newbuf = (char *)(void *)kv
375 	    + offsetof(struct kloader_bootinfo, _argbuf);
376 
377 	DPRINTF("argv: old %p -> new %p\n", oldbuf, newbuf);
378 
379 	/* not a valid pointer in this kernel! */
380 	nbi.argv = (void *)newbuf;
381 
382 	/* local copy that we populate with new (not yet valid) pointers */
383 	ap = (char **)(void *)nbi._argbuf;
384 
385 	for (i = 0; i < kloader.bootinfo->argc; ++i) {
386 		DPRINTFN(1, " [%d]: %p -> ", i, kloader.bootinfo->argv[i]);
387 		ap[i] = newbuf +
388 		    (kloader.bootinfo->argv[i] - oldbuf);
389 		_DPRINTFN(1, "%p\n", ap[i]);
390 	}
391 
392 	/* arrange for the new bootinfo to get copied */
393 	DPRINTF("bootinfo\n");
394 	kloader_copy(kv, &nbi, sizeof(struct kloader_bootinfo));
395 
396 	/* will be valid by the time the new kernel starts */
397 	kloader.rebootinfo = (void *)kv;
398 	/* kv += sizeof(struct kloader_bootinfo); */
399 
400 	/*
401 	 * Copy loader code
402 	 */
403 	KDASSERT(kloader.cur_pg);
404 	kloader.loader = (void *)PG_VADDR(kloader.cur_pg);
405 	memcpy(kloader.loader, kloader.ops->boot, PAGE_SIZE);
406 
407 	/* loader stack starts at the bottom of that page */
408 	kloader.loader_sp = (vaddr_t)kloader.loader + PAGE_SIZE;
409 
410 	DPRINTF("[loader] addr=%p sp=%p [kernel] entry=%p\n",
411 	    kloader.loader, (void *)kloader.loader_sp, (void *)nbi.entry);
412 
413 	return (0);
414  err:
415 	if (ph != NULL)
416 		free(ph, M_TEMP);
417 	if (sh != NULL)
418 		free(sh, M_TEMP);
419 	if (shstrtab != NULL)
420 		free(shstrtab, M_TEMP);
421 
422 	return 1;
423 }
424 
425 
426 int
427 kloader_alloc_memory(size_t sz)
428 {
429 	extern paddr_t avail_start, avail_end;
430 	int n, error;
431 
432 	n = (sz + BUCKET_SIZE - 1) / BUCKET_SIZE	/* kernel &co */
433 	    + 1;					/* 2nd loader */
434 
435 	error = uvm_pglistalloc(n * PAGE_SIZE, avail_start, avail_end,
436 	    PAGE_SIZE, 0, &kloader.pg_head, n, 0);
437 	if (error) {
438 		PRINTF("can't allocate memory.\n");
439 		return (1);
440 	}
441 	DPRINTF("allocated %d pages.\n", n);
442 
443 	kloader.cur_pg = TAILQ_FIRST(&kloader.pg_head);
444 	kloader.tagstart = (void *)PG_VADDR(kloader.cur_pg);
445 	kloader.cur_tag = NULL;
446 
447 	return (0);
448 }
449 
450 
451 struct kloader_page_tag *
452 kloader_get_tag(vaddr_t dst)
453 {
454 	struct vm_page *pg;
455 	vaddr_t addr;
456 	struct kloader_page_tag *tag;
457 
458 	tag = kloader.cur_tag;
459 	if (tag != NULL		/* has tag */
460 	    && tag->sz < BUCKET_SIZE /* that has free space */
461 	    && tag->dst + tag->sz == dst) /* and new data are contiguous */
462 	{
463 		DPRINTFN(1, "current tag %x/%x ok\n", tag->dst, tag->sz);
464 		return (tag);
465 	}
466 
467 	pg = kloader.cur_pg;
468 	KDASSERT(pg != NULL);
469 	kloader.cur_pg = TAILQ_NEXT(pg, pageq.queue);
470 
471 	addr = PG_VADDR(pg);
472 	tag = (void *)addr;
473 
474 	/*
475 	 * 2nd loader uses simple word-by-word copy, so destination
476 	 * address of a tag must be properly aligned.
477 	 */
478 	KASSERT(ALIGNED_POINTER(dst, register_t));
479 
480 	tag->src = addr + sizeof(struct kloader_page_tag);
481 	tag->dst = dst;
482 	tag->sz = 0;
483 	tag->next = 0;	/* Terminate. this member may overwrite after. */
484 	if (kloader.cur_tag)
485 		kloader.cur_tag->next = addr;
486 	kloader.cur_tag = tag;
487 
488 	return (tag);
489 }
490 
491 
492 /*
493  * Operations to populate kloader_page_tag's with data.
494  */
495 
496 void
497 kloader_from_file(vaddr_t dst, off_t ofs, size_t sz)
498 {
499 	struct kloader_page_tag *tag;
500 	size_t freesz;
501 
502 	while (sz > 0) {
503 		tag = kloader_get_tag(dst);
504 		KDASSERT(tag != NULL);
505 		freesz = BUCKET_SIZE - tag->sz;
506 		if (freesz > sz)
507 			freesz = sz;
508 
509 		DPRINTFN(1, "0x%08"PRIxVADDR" + 0x%zx <- 0x%lx\n", dst, freesz,
510 		    (unsigned long)ofs);
511 		kloader_read(ofs, freesz, (void *)(tag->src + tag->sz));
512 
513 		tag->sz += freesz;
514 		sz -= freesz;
515 		ofs += freesz;
516 		dst += freesz;
517 	}
518 }
519 
520 
521 void
522 kloader_copy(vaddr_t dst, const void *src, size_t sz)
523 {
524 	struct kloader_page_tag *tag;
525 	size_t freesz;
526 
527 	while (sz > 0) {
528 		tag = kloader_get_tag(dst);
529 		KDASSERT(tag != NULL);
530 		freesz = BUCKET_SIZE - tag->sz;
531 		if (freesz > sz)
532 			freesz = sz;
533 
534 		DPRINTFN(1, "0x%08"PRIxVADDR" + 0x%zx <- %p\n", dst, freesz, src);
535 		memcpy((void *)(tag->src + tag->sz), src, freesz);
536 
537 		tag->sz += freesz;
538 		sz -= freesz;
539 		src = (const char *)src + freesz;
540 		dst += freesz;
541 	}
542 }
543 
544 
545 void
546 kloader_zero(vaddr_t dst, size_t sz)
547 {
548 	struct kloader_page_tag *tag;
549 	size_t freesz;
550 
551 	while (sz > 0) {
552 		tag = kloader_get_tag(dst);
553 		KDASSERT(tag != NULL);
554 		freesz = BUCKET_SIZE - tag->sz;
555 		if (freesz > sz)
556 			freesz = sz;
557 
558 		DPRINTFN(1, "0x%08"PRIxVADDR" + 0x%zx\n", dst, freesz);
559 		memset((void *)(tag->src + tag->sz), 0, freesz);
560 
561 		tag->sz += freesz;
562 		sz -= freesz;
563 		dst += freesz;
564 	}
565 }
566 
567 
568 void
569 kloader_load_segment(Elf_Phdr *p)
570 {
571 
572 	DPRINTF("memory 0x%08x 0x%x <- file 0x%x 0x%x\n",
573 	    p->p_vaddr, p->p_memsz, p->p_offset, p->p_filesz);
574 
575 	kloader_from_file(p->p_vaddr, p->p_offset, p->p_filesz);
576 #ifdef KLOADER_ZERO_BSS
577 	kloader_zero(p->p_vaddr + p->p_filesz, p->p_memsz - p->p_filesz);
578 #endif
579 }
580 
581 
582 /*
583  * file access
584  */
585 struct vnode *
586 kloader_open(const char *filename)
587 {
588 	struct pathbuf *pb;
589 	struct nameidata nid;
590 	int error;
591 
592 	pb = pathbuf_create(filename);
593 	if (pb == NULL) {
594 		PRINTF("%s: pathbuf_create failed\n", filename);
595 		return (NULL);
596 	}
597 
598 	NDINIT(&nid, LOOKUP, FOLLOW, pb);
599 
600 	error = namei(&nid);
601 	if (error != 0) {
602 		PRINTF("%s: namei failed, errno=%d\n", filename, error);
603 		pathbuf_destroy(pb);
604 		return (NULL);
605 	}
606 
607 	error = vn_open(&nid, FREAD, 0);
608 	if (error != 0) {
609 		PRINTF("%s: open failed, errno=%d\n", filename, error);
610 		pathbuf_destroy(pb);
611 		return (NULL);
612 	}
613 
614 	pathbuf_destroy(pb);
615 	return (nid.ni_vp);
616 }
617 
618 void
619 kloader_close(void)
620 {
621 	struct lwp *l = KLOADER_LWP;
622 	struct vnode *vp = kloader.vp;
623 
624 	VOP_UNLOCK(vp);
625 	vn_close(vp, FREAD, l->l_cred);
626 }
627 
628 int
629 kloader_read(size_t ofs, size_t size, void *buf)
630 {
631 	struct lwp *l = KLOADER_LWP;
632 	struct vnode *vp = kloader.vp;
633 	size_t resid;
634 	int error;
635 
636 	error = vn_rdwr(UIO_READ, vp, buf, size, ofs, UIO_SYSSPACE,
637 	    IO_NODELOCKED | IO_SYNC, l->l_cred, &resid, NULL);
638 
639 	if (error)
640 		PRINTF("read error.\n");
641 
642 	return (error);
643 }
644 
645 
646 /*
647  * bootinfo
648  */
649 void
650 kloader_bootinfo_set(struct kloader_bootinfo *kbi, int argc, char *argv[],
651     struct bootinfo *bi, int printok)
652 {
653 	char *p, *pend, *buf;
654 	int i;
655 
656 	kloader.bootinfo = kbi;
657 	buf = kbi->_argbuf;
658 	if (bi != NULL)
659 		memcpy(&kbi->bootinfo, bi, sizeof(struct bootinfo));
660 	kbi->argc = argc;
661 	kbi->argv = (char **)buf;
662 
663 	p = &buf[argc * sizeof(char **)];
664 	pend = &buf[KLOADER_KERNELARGS_MAX - 1];
665 
666 	for (i = 0; i < argc; i++) {
667 		char *q = argv[i];
668 		int len = strlen(q) + 1;
669 		if ((p + len) > pend) {
670 			kloader.bootinfo = NULL;
671 			if (printok)
672 				PRINTF("buffer insufficient.\n");
673 			return;
674 		}
675 		kbi->argv[i] = p;
676 		memcpy(p, q, len);
677 		p += len;
678 	}
679 }
680 
681 
682 #ifdef KLOADER_DEBUG
683 void
684 kloader_pagetag_dump(void)
685 {
686 	struct kloader_page_tag *tag = kloader.tagstart;
687 	struct kloader_page_tag *p, *op;
688 	bool print;
689 	int i, n;
690 
691 	p = tag;
692 	op = NULL;
693 	i = 0, n = 15;
694 
695 	PRINTF("[page tag chain]\n");
696 	do  {
697 		print = FALSE;
698 		if (i < n)
699 			print = TRUE;
700 		if ((uint32_t)p & 3) {
701 			printf("tag alignment error\n");
702 			break;
703 		}
704 		if ((p->src & 3) || (p->dst & 3)) {
705 			printf("data alignment error.\n");
706 			print = TRUE;
707 		}
708 
709 		if (print) {
710 			printf("[%2d] next 0x%08x src 0x%08x dst 0x%08x"
711 			    " sz 0x%x\n", i, p->next, p->src, p->dst, p->sz);
712 		} else if (i == n) {
713 			printf("[...]\n");
714 		}
715 		op = p;
716 		i++;
717 	} while ((p = (struct kloader_page_tag *)(p->next)) != 0);
718 
719 	if (op != NULL)
720 		printf("[%d(last)] next 0x%08x src 0x%08x dst 0x%08x sz 0x%x\n",
721 		    i - 1, op->next, op->src, op->dst, op->sz);
722 }
723 
724 #endif /* KLOADER_DEBUG */
725