xref: /freebsd/stand/efi/loader/arch/riscv/exec.c (revision 7c43148a974877188a930e4078a164f83da8e652)
12192efc0SMitchell Horne /*-
22192efc0SMitchell Horne  * Copyright (c) 2001 Benno Rice <benno@FreeBSD.org>
32192efc0SMitchell Horne  * Copyright (c) 2007 Semihalf, Rafal Jaworowski <raj@semihalf.com>
42192efc0SMitchell Horne  * All rights reserved.
52192efc0SMitchell Horne  *
62192efc0SMitchell Horne  * Redistribution and use in source and binary forms, with or without
72192efc0SMitchell Horne  * modification, are permitted provided that the following conditions
82192efc0SMitchell Horne  * are met:
92192efc0SMitchell Horne  * 1. Redistributions of source code must retain the above copyright
102192efc0SMitchell Horne  *    notice, this list of conditions and the following disclaimer.
112192efc0SMitchell Horne  * 2. Redistributions in binary form must reproduce the above copyright
122192efc0SMitchell Horne  *    notice, this list of conditions and the following disclaimer in the
132192efc0SMitchell Horne  *    documentation and/or other materials provided with the distribution.
142192efc0SMitchell Horne  *
152192efc0SMitchell Horne  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
162192efc0SMitchell Horne  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
172192efc0SMitchell Horne  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
182192efc0SMitchell Horne  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
192192efc0SMitchell Horne  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
202192efc0SMitchell Horne  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
212192efc0SMitchell Horne  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
222192efc0SMitchell Horne  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
232192efc0SMitchell Horne  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
242192efc0SMitchell Horne  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
252192efc0SMitchell Horne  * SUCH DAMAGE.
262192efc0SMitchell Horne  */
272192efc0SMitchell Horne 
282192efc0SMitchell Horne #include <sys/param.h>
292192efc0SMitchell Horne #include <sys/linker.h>
302192efc0SMitchell Horne 
312192efc0SMitchell Horne #include <machine/md_var.h>
322192efc0SMitchell Horne #include <machine/metadata.h>
332192efc0SMitchell Horne #include <machine/elf.h>
342192efc0SMitchell Horne 
352192efc0SMitchell Horne #include <stand.h>
362192efc0SMitchell Horne 
372192efc0SMitchell Horne #include <efi.h>
382192efc0SMitchell Horne #include <efilib.h>
392192efc0SMitchell Horne 
402192efc0SMitchell Horne #include "bootstrap.h"
412192efc0SMitchell Horne #include "loader_efi.h"
422192efc0SMitchell Horne 
43*ed87efbeSRoger Pau Monné extern int bi_load(char *, vm_offset_t *, vm_offset_t *, bool);
442192efc0SMitchell Horne 
452192efc0SMitchell Horne static int
__elfN(exec)462192efc0SMitchell Horne __elfN(exec)(struct preloaded_file *fp)
472192efc0SMitchell Horne {
482192efc0SMitchell Horne 	struct file_metadata *fmp;
492192efc0SMitchell Horne 	vm_offset_t modulep, kernend;
502192efc0SMitchell Horne 	Elf_Ehdr *e;
512192efc0SMitchell Horne 	int error;
522192efc0SMitchell Horne 	void (*entry)(void *);
532192efc0SMitchell Horne 
542192efc0SMitchell Horne 	if ((fmp = file_findmetadata(fp, MODINFOMD_ELFHDR)) == NULL)
552192efc0SMitchell Horne 		return (EFTYPE);
562192efc0SMitchell Horne 
572192efc0SMitchell Horne 	e = (Elf_Ehdr *)&fmp->md_data;
582192efc0SMitchell Horne 
592192efc0SMitchell Horne 	efi_time_fini();
602192efc0SMitchell Horne 
612192efc0SMitchell Horne 	entry = efi_translate(e->e_entry);
622192efc0SMitchell Horne 
634f8212c8SJessica Clarke 	printf("Kernel entry at %p...\n", entry);
642192efc0SMitchell Horne 	printf("Kernel args: %s\n", fp->f_args);
652192efc0SMitchell Horne 
66*ed87efbeSRoger Pau Monné 	if ((error = bi_load(fp->f_args, &modulep, &kernend, true)) != 0) {
672192efc0SMitchell Horne 		efi_time_init();
682192efc0SMitchell Horne 		return (error);
692192efc0SMitchell Horne 	}
702192efc0SMitchell Horne 
712192efc0SMitchell Horne 	/*
722192efc0SMitchell Horne 	 * At this point we've called ExitBootServices, so we can't call
732192efc0SMitchell Horne 	 * printf or any other function that uses Boot Services
742192efc0SMitchell Horne 	 */
752192efc0SMitchell Horne 	dev_cleanup();
762192efc0SMitchell Horne 
772192efc0SMitchell Horne 	(*entry)((void *)modulep);
782192efc0SMitchell Horne 	panic("exec returned");
792192efc0SMitchell Horne }
802192efc0SMitchell Horne 
812192efc0SMitchell Horne static struct file_format riscv_elf = {
822192efc0SMitchell Horne 	__elfN(loadfile),
832192efc0SMitchell Horne 	__elfN(exec)
842192efc0SMitchell Horne };
852192efc0SMitchell Horne 
862192efc0SMitchell Horne struct file_format *file_formats[] = {
872192efc0SMitchell Horne 	&riscv_elf,
882192efc0SMitchell Horne 	NULL
892192efc0SMitchell Horne };
90