main.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /* -*- linux-c -*- ------------------------------------------------------- *
  2. *
  3. * Copyright (C) 1991, 1992 Linus Torvalds
  4. * Copyright 2007 rPath, Inc. - All Rights Reserved
  5. * Copyright 2009 Intel Corporation; author H. Peter Anvin
  6. *
  7. * This file is part of the Linux kernel, and is made available under
  8. * the terms of the GNU General Public License version 2.
  9. *
  10. * ----------------------------------------------------------------------- */
  11. /*
  12. * Main module for the real-mode kernel code
  13. */
  14. #include "boot.h"
  15. struct boot_params boot_params __attribute__((aligned(16)));
  16. char *HEAP = _end;
  17. char *heap_end = _end; /* Default end of heap = no heap */
  18. /*
  19. * Copy the header into the boot parameter block. Since this
  20. * screws up the old-style command line protocol, adjust by
  21. * filling in the new-style command line pointer instead.
  22. */
  23. static void copy_boot_params(void)
  24. {
  25. struct old_cmdline {
  26. u16 cl_magic;
  27. u16 cl_offset;
  28. };
  29. const struct old_cmdline * const oldcmd =
  30. (const struct old_cmdline *)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 &&
  34. oldcmd->cl_magic == OLD_CL_MAGIC) {
  35. /* Old-style command line protocol. */
  36. u16 cmdline_seg;
  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. if (oldcmd->cl_offset < boot_params.hdr.setup_move_size)
  41. cmdline_seg = ds();
  42. else
  43. cmdline_seg = 0x9000;
  44. boot_params.hdr.cmd_line_ptr =
  45. (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. /* Some older BIOSes apparently crash on this call, so filter
  70. it from machines too old to have SpeedStep at all. */
  71. if (cpu.level < 6)
  72. return;
  73. initregs(&ireg);
  74. ireg.ax = 0xe980; /* IST Support */
  75. ireg.edx = 0x47534943; /* Request value */
  76. intcall(0x15, &ireg, &oreg);
  77. boot_params.ist_info.signature = oreg.eax;
  78. boot_params.ist_info.command = oreg.ebx;
  79. boot_params.ist_info.event = oreg.ecx;
  80. boot_params.ist_info.perf_level = oreg.edx;
  81. }
  82. /*
  83. * Tell the BIOS what CPU mode we intend to run in.
  84. */
  85. static void set_bios_mode(void)
  86. {
  87. #ifdef CONFIG_X86_64
  88. struct biosregs ireg;
  89. initregs(&ireg);
  90. ireg.ax = 0xec00;
  91. ireg.bx = 2;
  92. intcall(0x15, &ireg, NULL);
  93. #endif
  94. }
  95. static void init_heap(void)
  96. {
  97. char *stack_end;
  98. if (boot_params.hdr.loadflags & CAN_USE_HEAP) {
  99. asm("leal %P1(%%esp),%0"
  100. : "=r" (stack_end) : "i" (-STACK_SIZE));
  101. heap_end = (char *)
  102. ((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 "
  108. "may be limited!\n");
  109. }
  110. }
  111. void main(void)
  112. {
  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 "
  124. "for your CPU.\n");
  125. die();
  126. }
  127. /* Tell the BIOS what CPU mode we intend to run in. */
  128. set_bios_mode();
  129. /* Detect memory layout */
  130. detect_memory();
  131. /* Set keyboard repeat rate (why?) and query the lock flags */
  132. keyboard_init();
  133. /* Query MCA information */
  134. query_mca();
  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. }