elf64_freebsd.c revision 39833
1/*-
2 * Copyright (c) 1998 Michael Smith <msmith@freebsd.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 *
26 *	$Id: aout_freebsd.c,v 1.6 1998/09/29 09:11:49 peter Exp $
27 */
28
29#include <sys/param.h>
30#include <sys/exec.h>
31#include <sys/reboot.h>
32#include <sys/linker.h>
33#include <string.h>
34#include <machine/bootinfo.h>
35#include <machine/elf.h>
36#include <stand.h>
37
38#include "bootstrap.h"
39#include "libi386.h"
40#include "btxv86.h"
41
42static int	elf_exec(struct loaded_module *amp);
43
44struct module_format i386_elf = { elf_loadmodule, elf_exec };
45
46static struct bootinfo	bi;
47
48/*
49 * There is an a.out kernel and one or more a.out modules loaded.
50 * We wish to start executing the kernel image, so make such
51 * preparations as are required, and do so.
52 */
53static int
54elf_exec(struct loaded_module *mp)
55{
56    struct loaded_module	*xp;
57    struct i386_devdesc		*currdev;
58    struct module_metadata	*md;
59    Elf_Ehdr 			*ehdr;
60    u_int32_t			argv[6];	/* kernel arguments */
61    int				major, bootdevnr;
62    vm_offset_t			addr, entry;
63    u_int			pad;
64
65    if ((md = mod_findmetadata(mp, MODINFOMD_ELFHDR)) == NULL)
66	return(EFTYPE);			/* XXX actually EFUCKUP */
67    ehdr = (Elf_Ehdr *)&(md->md_data);
68
69    /* Boot from whatever the current device is */
70    i386_getdev((void **)(&currdev), NULL, NULL);
71    switch(currdev->d_type) {
72    case DEVT_DISK:
73	major = 0;			/* XXX work out the best possible major here */
74	bootdevnr = MAKEBOOTDEV(major,
75				currdev->d_kind.biosdisk.slice >> 4,
76				currdev->d_kind.biosdisk.slice & 0xf,
77				currdev->d_kind.biosdisk.unit,
78				currdev->d_kind.biosdisk.partition);
79	break;
80    default:
81	printf("elf_exec: WARNING - don't know how to boot from device type %d\n", currdev->d_type);
82    }
83    free(currdev);
84
85    /* legacy bootinfo structure */
86    bi.bi_version = BOOTINFO_VERSION;
87    bi.bi_kernelname = 0;		/* XXX char * -> kernel name */
88    bi.bi_nfs_diskless = 0;		/* struct nfs_diskless * */
89    bi.bi_n_bios_used = 0;		/* XXX would have to hook biosdisk driver for these */
90    /* bi.bi_bios_geom[] */
91    bi.bi_size = sizeof(bi);
92    bi.bi_memsizes_valid = 1;
93    bi.bi_vesa = 0;			/* XXX correct value? */
94    bi.bi_basemem = getbasemem();
95    bi.bi_extmem = getextmem();
96    bi.bi_symtab = 0;			/* Let kld use _DYNAMIC */
97    bi.bi_esymtab = 0;
98
99    /* Device data is kept in the kernel argv array */
100    argv[0] = bi_getboothowto(mp->m_args);	/* boothowto */
101    argv[1] = bootdevnr;			/* bootdev */
102    argv[2] = 0;				/* old cyloffset */
103    argv[3] = 0;				/* old esym */
104    argv[4] = 0;				/* "new" bootinfo magic */
105    argv[5] = 0;				/* physical addr of bootinfo */
106
107    /* find the last module in the chain */
108    for (xp = mp; xp->m_next != NULL; xp = xp->m_next)
109	;
110    addr = xp->m_addr + xp->m_size;
111    /* pad to a page boundary */
112    pad = (u_int)addr & PAGE_MASK;
113    if (pad != 0) {
114	pad = PAGE_SIZE - pad;
115	addr += pad;
116    }
117    /* copy our environment */
118    bi.bi_envp = addr;
119    addr = bi_copyenv(addr);
120
121    /*
122     * Note, we could move the following onto a seperate page for reclaiming,
123     * but the environment is so small and so is this.
124     */
125
126    /* pad to a 4-byte boundary */
127    addr = (addr + 0x3) & ~0x3;
128
129    /* leave space for bootinfo */
130    argv[5] = (u_int32_t)addr;
131    addr += sizeof(struct bootinfo);
132
133    /* pad to a 4-byte boundary */
134    addr = (addr + 0x3) & ~0x3;
135
136    /* save in kernel name */
137    bi.bi_kernelname = addr;
138    i386_copyin(mp->m_name, addr, strlen(mp->m_name) + 1);
139    addr += strlen(mp->m_name) + 1;
140
141    /* pad to a page boundary */
142    pad = (u_int)addr & PAGE_MASK;
143    if (pad != 0) {
144	pad = PAGE_SIZE - pad;
145	addr += pad;
146    }
147    /* copy module list and metadata */
148    bi.bi_modulep = addr;
149    addr = bi_copymodules(addr);
150
151    /* all done copying stuff in, save end of loaded object space */
152    bi.bi_kernend = addr;
153
154    /* and insert bootinfo struct into reserved spot */
155    i386_copyin(&bi, (vm_offset_t)argv[5], sizeof(bi));
156    argv[0] |= RB_BOOTINFO;		/* it's there now */
157
158    entry = ehdr->e_entry & 0xffffff;
159
160#ifdef DEBUG
161    {
162	int i;
163	for (i = 0; i < 6; i++)
164	    printf("argv[%d]=%lx\n", i, argv[i]);
165    }
166
167    printf("Start @ 0x%lx ...\n", entry);
168#endif
169
170    __exec((void *)entry, argv[0], argv[1], argv[2], argv[3], argv[4], argv[5]);
171
172    panic("exec returned");
173}
174