main.c 4.3 KB

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