1 /*
2  * Copyright (C) 2021 Intel Corporation.
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  */
6 
7 #include <types.h>
8 #include <errno.h>
9 #include <asm/pgtable.h>
10 #include <boot.h>
11 #include <rtl.h>
12 #include <logmsg.h>
13 
14 static struct acrn_boot_info acrn_bi = { 0 };
15 
16 /**
17  * @pre (p_start != NULL) && (p_end != NULL)
18  */
get_boot_mods_range(uint64_t * p_start,uint64_t * p_end)19 void get_boot_mods_range(uint64_t *p_start, uint64_t *p_end)
20 {
21 	uint32_t i;
22 	uint64_t start = ~0UL, end = 0UL;
23 	struct acrn_boot_info *abi = get_acrn_boot_info();
24 
25 	for (i = 0; i < abi->mods_count; i++) {
26 		if (hva2hpa(abi->mods[i].start) < start) {
27 			start = hva2hpa(abi->mods[i].start);
28 		}
29 		if (hva2hpa(abi->mods[i].start + abi->mods[i].size) > end) {
30 			end = hva2hpa(abi->mods[i].start + abi->mods[i].size);
31 		}
32 	}
33 	*p_start = start;
34 	*p_end = end;
35 }
36 
init_acrn_boot_info(uint32_t * registers)37 void init_acrn_boot_info(uint32_t *registers)
38 {
39 	(void)init_multiboot_info(registers);
40 	/* TODO: add more boot protocol support here */
41 }
42 
sanitize_acrn_boot_info(struct acrn_boot_info * abi)43 int32_t sanitize_acrn_boot_info(struct acrn_boot_info *abi)
44 {
45 	int32_t abi_status = 0;
46 
47 	if (abi->mods_count == 0U) {
48 		pr_err("no boot module info found");
49 		abi_status = -EINVAL;
50 	}
51 
52 	if (abi->mmap_entries == 0U) {
53 		pr_err("no boot mmap info found");
54 		abi_status = -EINVAL;
55 	}
56 
57 	printf("%s environment detected.\n", boot_from_uefi(abi) ? "UEFI" : "Non-UEFI");
58 	if (boot_from_uefi(abi) && ((abi->uefi_info.memmap == 0U) || (abi->uefi_info.memmap_hi != 0U))) {
59 		pr_err("no efi memmap found below 4GB space!");
60 		abi_status = -EINVAL;
61 	}
62 
63 	if (abi->loader_name[0] == '\0') {
64 		pr_err("no bootloader name found!");
65 		abi_status = -EINVAL;
66 	} else {
67 		printf("%s Bootloader: %s\n", abi->protocol_name, abi->loader_name);
68 	}
69 
70 	return abi_status;
71 }
72 
73 /*
74  * @post retval != NULL
75  */
get_acrn_boot_info(void)76 struct acrn_boot_info *get_acrn_boot_info(void)
77 {
78 	return &acrn_bi;
79 }
80