xref: /kvm-unit-tests/lib/x86/setup.c (revision 6ca8c283190847bd2ff648232f98f70e4e6d3d0d)
1 /*
2  * Initialize machine setup information
3  *
4  * Copyright (C) 2017, Red Hat Inc, Andrew Jones <drjones@redhat.com>
5  *
6  * This work is licensed under the terms of the GNU LGPL, version 2.
7  */
8 #include "libcflat.h"
9 #include "fwcfg.h"
10 #include "alloc_phys.h"
11 
12 extern char edata;
13 
14 struct mbi_bootinfo {
15 	u32 flags;
16 	u32 mem_lower;
17 	u32 mem_upper;
18 	u32 boot_device;
19 	u32 cmdline;
20 	u32 mods_count;
21 	u32 mods_addr;
22 	u32 reserved[5];   /* 28-47 */
23 	u32 mmap_addr;
24 	u32 reserved0[3];  /* 52-63 */
25 	u32 bootloader;
26 	u32 reserved1[5];  /* 68-87 */
27 	u32 size;
28 };
29 
30 struct mbi_module {
31 	u32 start, end;
32 	u32 cmdline;
33 	u32 unused;
34 };
35 
36 #define ENV_SIZE 16384
37 
38 void setup_env(char *env, int size);
39 void setup_multiboot(struct mbi_bootinfo *bootinfo);
40 void setup_libcflat(void);
41 
42 char *initrd;
43 u32 initrd_size;
44 
45 static char env[ENV_SIZE];
46 
47 void setup_multiboot(struct mbi_bootinfo *bootinfo)
48 {
49 	struct mbi_module *mods;
50 
51 	/* TODO: use e820 */
52 	u64 end_of_memory = bootinfo->mem_upper * 1024ull;
53 	phys_alloc_init((uintptr_t) &edata, end_of_memory - (uintptr_t) &edata);
54 
55 	if (bootinfo->mods_count != 1)
56 		return;
57 
58 	mods = (struct mbi_module *)(uintptr_t) bootinfo->mods_addr;
59 
60 	initrd = (char *)(uintptr_t) mods->start;
61 	initrd_size = mods->end - mods->start;
62 }
63 
64 void setup_libcflat(void)
65 {
66 	if (initrd) {
67 		/* environ is currently the only file in the initrd */
68 		u32 size = MIN(initrd_size, ENV_SIZE);
69 		memcpy(env, initrd, size);
70 		setup_env(env, size);
71 	}
72 }
73