// SPDX-License-Identifier: GPL-2.0-only1/* -*- linux-c -*- ------------------------------------------------------- *2*3* Copyright (C) 1991, 1992 Linus Torvalds4* Copyright 2007 rPath, Inc. - All Rights Reserved5* Copyright 2009 Intel Corporation; author H. Peter Anvin6*7* ----------------------------------------------------------------------- */89/*10* Main module for the real-mode kernel code11*/12#include <linux/build_bug.h>1314#include "boot.h"15#include "string.h"1617struct boot_params boot_params __attribute__((aligned(16)));1819struct port_io_ops pio_ops;2021char *HEAP = _end;22char *heap_end = _end; /* Default end of heap = no heap */2324/*25* Copy the header into the boot parameter block. Since this26* screws up the old-style command line protocol, adjust by27* filling in the new-style command line pointer instead.28*/29static void copy_boot_params(void)30{31struct old_cmdline {32u16 cl_magic;33u16 cl_offset;34};35const struct old_cmdline * const oldcmd = absolute_pointer(OLD_CL_ADDRESS);3637BUILD_BUG_ON(sizeof(boot_params) != 4096);38memcpy(&boot_params.hdr, &hdr, sizeof(hdr));3940if (!boot_params.hdr.cmd_line_ptr && oldcmd->cl_magic == OLD_CL_MAGIC) {41/* Old-style command line protocol */42u16 cmdline_seg;4344/*45* Figure out if the command line falls in the region46* of memory that an old kernel would have copied up47* to 0x90000...48*/49if (oldcmd->cl_offset < boot_params.hdr.setup_move_size)50cmdline_seg = ds();51else52cmdline_seg = 0x9000;5354boot_params.hdr.cmd_line_ptr = (cmdline_seg << 4) + oldcmd->cl_offset;55}56}5758/*59* Query the keyboard lock status as given by the BIOS, and60* set the keyboard repeat rate to maximum. Unclear why the latter61* is done here; this might be possible to kill off as stale code.62*/63static void keyboard_init(void)64{65struct biosregs ireg, oreg;6667initregs(&ireg);6869ireg.ah = 0x02; /* Get keyboard status */70intcall(0x16, &ireg, &oreg);71boot_params.kbd_status = oreg.al;7273ireg.ax = 0x0305; /* Set keyboard repeat rate */74intcall(0x16, &ireg, NULL);75}7677/*78* Get Intel SpeedStep (IST) information.79*/80static void query_ist(void)81{82struct biosregs ireg, oreg;8384/*85* Some older BIOSes apparently crash on this call, so filter86* it from machines too old to have SpeedStep at all.87*/88if (cpu.level < 6)89return;9091initregs(&ireg);92ireg.ax = 0xe980; /* IST Support */93ireg.edx = 0x47534943; /* Request value */94intcall(0x15, &ireg, &oreg);9596boot_params.ist_info.signature = oreg.eax;97boot_params.ist_info.command = oreg.ebx;98boot_params.ist_info.event = oreg.ecx;99boot_params.ist_info.perf_level = oreg.edx;100}101102/*103* Tell the BIOS what CPU mode we intend to run in.104*/105static void set_bios_mode(void)106{107#ifdef CONFIG_X86_64108struct biosregs ireg;109110initregs(&ireg);111ireg.ax = 0xec00;112ireg.bx = 2;113intcall(0x15, &ireg, NULL);114#endif115}116117static void init_heap(void)118{119char *stack_end;120121if (boot_params.hdr.loadflags & CAN_USE_HEAP) {122stack_end = (char *) (current_stack_pointer - STACK_SIZE);123heap_end = (char *) ((size_t)boot_params.hdr.heap_end_ptr + 0x200);124if (heap_end > stack_end)125heap_end = stack_end;126} else {127/* Boot protocol 2.00 only, no heap available */128puts("WARNING: Ancient bootloader, some functionality may be limited!\n");129}130}131132void main(void)133{134init_default_io_ops();135136/* First, copy the boot header into the "zeropage" */137copy_boot_params();138139/* Initialize the early-boot console */140console_init();141if (cmdline_find_option_bool("debug"))142puts("early console in setup code\n");143144/* End of heap check */145init_heap();146147/* Make sure we have all the proper CPU support */148if (validate_cpu()) {149puts("Unable to boot - please use a kernel appropriate for your CPU.\n");150die();151}152153/* Tell the BIOS what CPU mode we intend to run in */154set_bios_mode();155156/* Detect memory layout */157detect_memory();158159/* Set keyboard repeat rate (why?) and query the lock flags */160keyboard_init();161162/* Query Intel SpeedStep (IST) information */163query_ist();164165/* Query APM information */166#if defined(CONFIG_APM) || defined(CONFIG_APM_MODULE)167query_apm_bios();168#endif169170/* Query EDD information */171#if defined(CONFIG_EDD) || defined(CONFIG_EDD_MODULE)172query_edd();173#endif174175/* Set the video mode */176set_video();177178/* Do the last things and invoke protected mode */179go_to_protected_mode();180}181182183