xref: /openbmc/linux/arch/x86/boot/main.c (revision f2f4bf5a)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /* -*- linux-c -*- ------------------------------------------------------- *
3  *
4  *   Copyright (C) 1991, 1992 Linus Torvalds
5  *   Copyright 2007 rPath, Inc. - All Rights Reserved
6  *   Copyright 2009 Intel Corporation; author H. Peter Anvin
7  *
8  * ----------------------------------------------------------------------- */
9 
10 /*
11  * Main module for the real-mode kernel code
12  */
13 
14 #include "boot.h"
15 #include "string.h"
16 
17 struct boot_params boot_params __attribute__((aligned(16)));
18 
19 char *HEAP = _end;
20 char *heap_end = _end;		/* Default end of heap = no heap */
21 
22 /*
23  * Copy the header into the boot parameter block.  Since this
24  * screws up the old-style command line protocol, adjust by
25  * filling in the new-style command line pointer instead.
26  */
27 
28 static void copy_boot_params(void)
29 {
30 	struct old_cmdline {
31 		u16 cl_magic;
32 		u16 cl_offset;
33 	};
34 	const struct old_cmdline * const oldcmd =
35 		(const struct old_cmdline *)OLD_CL_ADDRESS;
36 
37 	BUILD_BUG_ON(sizeof(boot_params) != 4096);
38 	memcpy(&boot_params.hdr, &hdr, sizeof(hdr));
39 
40 	if (!boot_params.hdr.cmd_line_ptr &&
41 	    oldcmd->cl_magic == OLD_CL_MAGIC) {
42 		/* Old-style command line protocol. */
43 		u16 cmdline_seg;
44 
45 		/* Figure out if the command line falls in the region
46 		   of memory that an old kernel would have copied up
47 		   to 0x90000... */
48 		if (oldcmd->cl_offset < boot_params.hdr.setup_move_size)
49 			cmdline_seg = ds();
50 		else
51 			cmdline_seg = 0x9000;
52 
53 		boot_params.hdr.cmd_line_ptr =
54 			(cmdline_seg << 4) + oldcmd->cl_offset;
55 	}
56 }
57 
58 /*
59  * Query the keyboard lock status as given by the BIOS, and
60  * set the keyboard repeat rate to maximum.  Unclear why the latter
61  * is done here; this might be possible to kill off as stale code.
62  */
63 static void keyboard_init(void)
64 {
65 	struct biosregs ireg, oreg;
66 	initregs(&ireg);
67 
68 	ireg.ah = 0x02;		/* Get keyboard status */
69 	intcall(0x16, &ireg, &oreg);
70 	boot_params.kbd_status = oreg.al;
71 
72 	ireg.ax = 0x0305;	/* Set keyboard repeat rate */
73 	intcall(0x16, &ireg, NULL);
74 }
75 
76 /*
77  * Get Intel SpeedStep (IST) information.
78  */
79 static void query_ist(void)
80 {
81 	struct biosregs ireg, oreg;
82 
83 	/* Some older BIOSes apparently crash on this call, so filter
84 	   it from machines too old to have SpeedStep at all. */
85 	if (cpu.level < 6)
86 		return;
87 
88 	initregs(&ireg);
89 	ireg.ax  = 0xe980;	 /* IST Support */
90 	ireg.edx = 0x47534943;	 /* Request value */
91 	intcall(0x15, &ireg, &oreg);
92 
93 	boot_params.ist_info.signature  = oreg.eax;
94 	boot_params.ist_info.command    = oreg.ebx;
95 	boot_params.ist_info.event      = oreg.ecx;
96 	boot_params.ist_info.perf_level = oreg.edx;
97 }
98 
99 /*
100  * Tell the BIOS what CPU mode we intend to run in.
101  */
102 static void set_bios_mode(void)
103 {
104 #ifdef CONFIG_X86_64
105 	struct biosregs ireg;
106 
107 	initregs(&ireg);
108 	ireg.ax = 0xec00;
109 	ireg.bx = 2;
110 	intcall(0x15, &ireg, NULL);
111 #endif
112 }
113 
114 static void init_heap(void)
115 {
116 	char *stack_end;
117 
118 	if (boot_params.hdr.loadflags & CAN_USE_HEAP) {
119 		asm("leal %P1(%%esp),%0"
120 		    : "=r" (stack_end) : "i" (-STACK_SIZE));
121 
122 		heap_end = (char *)
123 			((size_t)boot_params.hdr.heap_end_ptr + 0x200);
124 		if (heap_end > stack_end)
125 			heap_end = stack_end;
126 	} else {
127 		/* Boot protocol 2.00 only, no heap available */
128 		puts("WARNING: Ancient bootloader, some functionality "
129 		     "may be limited!\n");
130 	}
131 }
132 
133 void main(void)
134 {
135 	/* First, copy the boot header into the "zeropage" */
136 	copy_boot_params();
137 
138 	/* Initialize the early-boot console */
139 	console_init();
140 	if (cmdline_find_option_bool("debug"))
141 		puts("early console in setup code\n");
142 
143 	/* End of heap check */
144 	init_heap();
145 
146 	/* Make sure we have all the proper CPU support */
147 	if (validate_cpu()) {
148 		puts("Unable to boot - please use a kernel appropriate "
149 		     "for your CPU.\n");
150 		die();
151 	}
152 
153 	/* Tell the BIOS what CPU mode we intend to run in. */
154 	set_bios_mode();
155 
156 	/* Detect memory layout */
157 	detect_memory();
158 
159 	/* Set keyboard repeat rate (why?) and query the lock flags */
160 	keyboard_init();
161 
162 	/* Query Intel SpeedStep (IST) information */
163 	query_ist();
164 
165 	/* Query APM information */
166 #if defined(CONFIG_APM) || defined(CONFIG_APM_MODULE)
167 	query_apm_bios();
168 #endif
169 
170 	/* Query EDD information */
171 #if defined(CONFIG_EDD) || defined(CONFIG_EDD_MODULE)
172 	query_edd();
173 #endif
174 
175 	/* Set the video mode */
176 	set_video();
177 
178 	/* Do the last things and invoke protected mode */
179 	go_to_protected_mode();
180 }
181