xref: /aosp_15_r20/external/coreboot/payloads/libpayload/arch/x86/multiboot.c (revision b9411a12aaaa7e1e6a6fb7c5e057f44ee179a49c)
1 /*
2  *
3  * Copyright (C) 2008 Advanced Micro Devices, Inc.
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  * 3. The name of the author may not be used to endorse or promote products
14  *    derived from this software without specific prior written permission.
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 <libpayload-config.h>
30 #include <libpayload.h>
31 #include <multiboot_tables.h>
32 
33 unsigned long loader_eax;  /* The value of EAX passed from the loader */
34 unsigned long loader_ebx;  /* The value of EBX passed from the loader */
35 
mb_add_memrange(struct sysinfo_t * info,unsigned long long base,unsigned long long size,unsigned int type)36 static int mb_add_memrange(struct sysinfo_t *info, unsigned long long base,
37 			   unsigned long long size, unsigned int type)
38 {
39 	if (info->n_memranges >= SYSINFO_MAX_MEM_RANGES)
40 		return -1;
41 
42 #if CONFIG(LP_MEMMAP_RAM_ONLY)
43 	/* 1 == normal RAM.  Ignore everything else for now */
44 	if (type != 1)
45 		return 0;
46 #endif
47 
48 	info->memrange[info->n_memranges].base = base;
49 	info->memrange[info->n_memranges].size = size;
50 	info->memrange[info->n_memranges].type = type;
51 	info->n_memranges++;
52 
53 	return 0;
54 }
55 
mb_parse_mmap(struct multiboot_header * table,struct sysinfo_t * info)56 static void mb_parse_mmap(struct multiboot_header *table,
57 			  struct sysinfo_t *info)
58 {
59 	u8 *start = (u8 *) phys_to_virt(table->mmap_addr);
60 	u8 *ptr = start;
61 
62 	info->n_memranges = 0;
63 
64 	while(ptr < (start + table->mmap_length)) {
65 		struct multiboot_mmap *mmap = (struct multiboot_mmap *) ptr;
66 
67 		if (mb_add_memrange(info, mmap->addr, mmap->length, mmap->type))
68 			return;
69 
70 		ptr += (mmap->size + sizeof(mmap->size));
71 	}
72 }
73 
mb_parse_meminfo(struct multiboot_header * table,struct sysinfo_t * info)74 static void mb_parse_meminfo(struct multiboot_header *table,
75 			     struct sysinfo_t *info)
76 {
77 	unsigned long long mem_low = table->mem_lower;
78 	unsigned long long mem_high = table->mem_higher;
79 
80 	info->n_memranges = 0;
81 
82 	if (mem_low)
83 		mb_add_memrange(info, 0 * MiB, mem_low * KiB, 1);
84 
85 	if (mem_high)
86 		mb_add_memrange(info, 1 * MiB, mem_high * KiB, 1);
87 }
88 
mb_parse_cmdline(struct multiboot_header * table)89 static void mb_parse_cmdline(struct multiboot_header *table)
90 {
91 	extern int main_argc;
92 	extern char *main_argv[];
93 	char *c = phys_to_virt(table->cmdline);
94 
95 	while(*c != '\0' && main_argc < MAX_ARGC_COUNT) {
96 		main_argv[main_argc++] = c;
97 
98 		for( ; *c != '\0' && !isspace(*c); c++);
99 
100 		if (*c) {
101 			*c = 0;
102 			c++;
103 		}
104 	}
105 }
106 
get_multiboot_info(struct sysinfo_t * info)107 int get_multiboot_info(struct sysinfo_t *info)
108 {
109 	struct multiboot_header *table;
110 
111 	if (loader_eax != MULTIBOOT_MAGIC)
112 		return -1;
113 
114 	table = (struct multiboot_header *) phys_to_virt(loader_ebx);
115 
116 	info->mbtable = phys_to_virt(loader_ebx);
117 
118 	if (table->flags & MULTIBOOT_FLAGS_MMAP)
119 		mb_parse_mmap(table, info);
120 	else if (table->flags & MULTIBOOT_FLAGS_MEMINFO)
121 		mb_parse_meminfo(table, info);
122 
123 	if (table->flags & MULTIBOOT_FLAGS_CMDLINE)
124 		mb_parse_cmdline(table);
125 
126 	return 0;
127 }
128