main.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. #include "string.h"
  16. struct boot_params boot_params __attribute__((aligned(16)));
  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. (const struct old_cmdline *)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. /* First, copy the boot header into the "zeropage" */
  115. copy_boot_params();
  116. /* Initialize the early-boot console */
  117. console_init();
  118. if (cmdline_find_option_bool("debug"))
  119. puts("early console in setup code\n");
  120. /* End of heap check */
  121. init_heap();
  122. /* Make sure we have all the proper CPU support */
  123. if (validate_cpu()) {
  124. puts("Unable to boot - please use a kernel appropriate "
  125. "for your CPU.\n");
  126. die();
  127. }
  128. /* Tell the BIOS what CPU mode we intend to run in. */
  129. set_bios_mode();
  130. /* Detect memory layout */
  131. detect_memory();
  132. /* Set keyboard repeat rate (why?) and query the lock flags */
  133. keyboard_init();
  134. /* Query Intel SpeedStep (IST) information */
  135. query_ist();
  136. /* Query APM information */
  137. #if defined(CONFIG_APM) || defined(CONFIG_APM_MODULE)
  138. query_apm_bios();
  139. #endif
  140. /* Query EDD information */
  141. #if defined(CONFIG_EDD) || defined(CONFIG_EDD_MODULE)
  142. query_edd();
  143. #endif
  144. /* Set the video mode */
  145. set_video();
  146. /* Do the last things and invoke protected mode */
  147. go_to_protected_mode();
  148. }