Home | History | Annotate | Line # | Download | only in libefi
bootinfo.c revision 1.4.18.1
      1 /*	$NetBSD: bootinfo.c,v 1.4.18.1 2016/08/06 00:19:05 pgoyette Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1998 Michael Smith <msmith (at) freebsd.org>
      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 AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26  * SUCH DAMAGE.
     27  */
     28 
     29 #include <sys/cdefs.h>
     30 /* __FBSDID("$FreeBSD: src/sys/boot/efi/libefi/bootinfo.c,v 1.10 2004/01/04 23:28:16 obrien Exp $"); */
     31 
     32 #include <lib/libsa/stand.h>
     33 #include <lib/libsa/loadfile.h>
     34 #include <lib/libkern/libkern.h>
     35 
     36 #include <sys/param.h>
     37 #include <sys/reboot.h>
     38 #include <sys/boot_flag.h>
     39 #include <sys/exec_elf.h>
     40 #include <sys/lock.h>
     41 
     42 #include <machine/vmparam.h>
     43 #include <machine/elf_machdep.h>
     44 #include <machine/bootinfo.h>
     45 
     46 
     47 #include <efi.h>
     48 #include <efilib.h>
     49 #include <efiboot.h>
     50 
     51 #include <machine/efilib.h>
     52 
     53 #include "bootstrap.h"
     54 
     55 static EFI_GUID hcdp = HCDP_TABLE_GUID;
     56 
     57 /*
     58  * Return a 'boothowto' value corresponding to the kernel arguments in
     59  * (kargs) and any relevant environment variables.
     60  */
     61 static struct
     62 {
     63     const char	*ev;
     64     int		mask;
     65 } howto_names[] = {
     66     {"boot_askname",	RB_ASKNAME},
     67     {"boot_single",	RB_SINGLE},
     68     {"boot_nosync",     RB_NOSYNC},
     69     {"boot_halt",       RB_HALT},
     70     {"boot_kdb",	RB_KDB},
     71     {"boot_rdonly",     RB_RDONLY},
     72     {"boot_dump",       RB_DUMP},
     73     {"boot_miniroot",   RB_MINIROOT},
     74     {"boot_userconf",   RB_USERCONF},
     75     {NULL,	0}
     76 };
     77 
     78 extern char *efi_fmtdev(void *vdev);
     79 
     80 int
     81 bi_getboothowto(char *kargs)
     82 {
     83     char	*cp;
     84     int		howto;
     85     int		active;
     86     int		i;
     87 
     88     /* Parse kargs */
     89     howto = 0;
     90     if (kargs  != NULL) {
     91 	cp = kargs;
     92 	active = 0;
     93 	while (*cp != 0) {
     94 	    if (!active && (*cp == '-')) {
     95 		active = 1;
     96 	    } else if (active)
     97 
     98 	      BOOT_FLAG(*cp, howto);
     99 
    100 	    cp++;
    101 	}
    102     }
    103     /* get equivalents from the environment */
    104     for (i = 0; howto_names[i].ev != NULL; i++)
    105 	if (getenv(howto_names[i].ev) != NULL)
    106 	    howto |= howto_names[i].mask;
    107     return(howto);
    108 }
    109 
    110 /*
    111  * Copy the environment into the load area starting at (addr).
    112  * Each variable is formatted as <name>=<value>, with a single nul
    113  * separating each variable, and a double nul terminating the environment.
    114  */
    115 vaddr_t
    116 bi_copyenv(vaddr_t addr)
    117 {
    118     struct env_var	*ep;
    119 
    120     /* traverse the environment */
    121     for (ep = environ; ep != NULL; ep = ep->ev_next) {
    122 	efi_copyin(ep->ev_name, addr, strlen(ep->ev_name));
    123 	addr += strlen(ep->ev_name);
    124 	efi_copyin("=", addr, 1);
    125 	addr++;
    126 	if (ep->ev_value != NULL) {
    127 	    efi_copyin(ep->ev_value, addr, strlen(ep->ev_value));
    128 	    addr += strlen(ep->ev_value);
    129 	}
    130 	efi_copyin("", addr, 1);
    131 	addr++;
    132     }
    133     efi_copyin("", addr, 1);
    134     addr++;
    135     return(addr);
    136 }
    137 
    138 /*
    139  * Copy module-related data into the load area, where it can be
    140  * used as a directory for loaded modules.
    141  *
    142  * Module data is presented in a self-describing format.  Each datum
    143  * is preceded by a 32-bit identifier and a 32-bit size field.
    144  *
    145  * Currently, the following data are saved:
    146  *
    147  * MOD_NAME	(variable)		module name (string)
    148  * MOD_TYPE	(variable)		module type (string)
    149  * MOD_ARGS	(variable)		module parameters (string)
    150  * MOD_ADDR	sizeof(vm_offset_t)	module load address
    151  * MOD_SIZE	sizeof(size_t)		module size
    152  * MOD_METADATA	(variable)		type-specific metadata
    153  */
    154 #define COPY32(v, a) {				\
    155     u_int32_t	x = (v);			\
    156     efi_copyin(&x, a, sizeof(x));		\
    157     a += sizeof(x);				\
    158 }
    159 
    160 #define MOD_STR(t, a, s) {			\
    161     COPY32(t, a);				\
    162     COPY32(strlen(s) + 1, a);			\
    163     efi_copyin(s, a, strlen(s) + 1);		\
    164     a += roundup(strlen(s) + 1, sizeof(u_int64_t));\
    165 }
    166 
    167 #define MOD_NAME(a, s)	MOD_STR(MODINFO_NAME, a, s)
    168 #define MOD_TYPE(a, s)	MOD_STR(MODINFO_TYPE, a, s)
    169 #define MOD_ARGS(a, s)	MOD_STR(MODINFO_ARGS, a, s)
    170 
    171 #define MOD_VAR(t, a, s) {			\
    172     COPY32(t, a);				\
    173     COPY32(sizeof(s), a);			\
    174     efi_copyin(&s, a, sizeof(s));		\
    175     a += roundup(sizeof(s), sizeof(u_int64_t));	\
    176 }
    177 
    178 #define MOD_ADDR(a, s)	MOD_VAR(MODINFO_ADDR, a, s)
    179 #define MOD_SIZE(a, s)	MOD_VAR(MODINFO_SIZE, a, s)
    180 
    181 #define MOD_METADATA(a, mm) {			\
    182     COPY32(MODINFO_METADATA | mm->md_type, a);	\
    183     COPY32(mm->md_size, a);			\
    184     efi_copyin(mm->md_data, a, mm->md_size);	\
    185     a += roundup(mm->md_size, sizeof(u_int64_t));\
    186 }
    187 
    188 #define MOD_END(a) {				\
    189     COPY32(MODINFO_END, a);			\
    190     COPY32(0, a);				\
    191 }
    192 
    193 /*
    194  * Load the information expected by the kernel.
    195  *
    196  * - The kernel environment is copied into kernel space.
    197  * - Module metadata are formatted and placed in kernel space.
    198  */
    199 int
    200 bi_load(struct bootinfo *bi, struct preloaded_file *fp, UINTN *mapkey,
    201     UINTN pages)
    202 {
    203     char			*rootdevname;
    204     struct efi_devdesc		*rootdev;
    205     struct preloaded_file	*xp;
    206     vaddr_t			addr, bootinfo_addr;
    207     vaddr_t			ssym, esym;
    208     struct file_metadata	*md;
    209     EFI_STATUS			status;
    210     UINTN			bisz, key;
    211 
    212     /*
    213      * Version 1 bootinfo.
    214      */
    215     bi->bi_magic = BOOTINFO_MAGIC;
    216     bi->bi_version = 1;
    217 
    218     /*
    219      * Calculate boothowto.
    220      */
    221     bi->bi_boothowto = bi_getboothowto(fp->f_args);
    222 
    223     /*
    224      * Stash EFI System Table.
    225      */
    226     bi->bi_systab = (u_int64_t) ST;
    227 
    228     /*
    229      * Allow the environment variable 'rootdev' to override the supplied
    230      * device. This should perhaps go to MI code and/or have $rootdev
    231      * tested/set by MI code before launching the kernel.
    232      */
    233     rootdevname = getenv("rootdev");
    234     efi_getdev((void **)(&rootdev), rootdevname, NULL);
    235     if (rootdev == NULL) {		/* bad $rootdev/$currdev */
    236 	printf("can't determine root device\n");
    237 	return(EINVAL);
    238     }
    239 
    240     /* Try reading the /etc/fstab file to select the root device */
    241     getrootmount(efi_fmtdev((void *)rootdev));
    242     free(rootdev);
    243 
    244     ssym = esym = 0;
    245 
    246     ssym = fp->marks[MARK_SYM];
    247     esym = fp->marks[MARK_END];
    248 
    249     if (ssym == 0 || esym == 0)
    250 	ssym = esym = 0;		/* sanity */
    251 
    252     bi->bi_symtab = ssym;
    253     bi->bi_esymtab = esym;
    254 
    255     bi->bi_hcdp = (uint64_t)efi_get_table(&hcdp); /* DIG64 HCDP table addr. */
    256     fpswa_init(&bi->bi_fpswa);		/* find FPSWA interface */
    257 
    258     /* find the last module in the chain */
    259     addr = 0;
    260     for (xp = file_findfile(NULL, NULL); xp != NULL; xp = xp->f_next) {
    261 	if (addr < (xp->f_addr + xp->f_size))
    262 	    addr = xp->f_addr + xp->f_size;
    263     }
    264 
    265     /* pad to a page boundary */
    266     addr = (addr + PAGE_MASK) & ~PAGE_MASK;
    267 
    268     /* copy our environment */
    269     bi->bi_envp = addr;
    270     addr = bi_copyenv(addr);
    271 
    272     /* pad to a page boundary */
    273     addr = (addr + PAGE_MASK) & ~PAGE_MASK;
    274 
    275     /* all done copying stuff in, save end of loaded object space */
    276     bi->bi_kernend = addr;
    277 
    278     /*
    279      * Read the memory map and stash it after bootinfo. Align the memory map
    280      * on a 16-byte boundary (the bootinfo block is page aligned).
    281      */
    282     bisz = (sizeof(struct bootinfo) + 0x0f) & ~0x0f;
    283     bi->bi_memmap = ((u_int64_t)bi) + bisz;
    284     bi->bi_memmap_size = EFI_PAGE_SIZE * pages - bisz;
    285     status = BS->GetMemoryMap(&bi->bi_memmap_size,
    286 		(EFI_MEMORY_DESCRIPTOR *)bi->bi_memmap, &key,
    287 		&bi->bi_memdesc_size, &bi->bi_memdesc_version);
    288     if (EFI_ERROR(status)) {
    289 	printf("bi_load: Can't read memory map\n");
    290 	return EINVAL;
    291     }
    292     *mapkey = key;
    293 
    294     return(0);
    295 }
    296