main.c 4.3 KB

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