main.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /* -*- linux-c -*- ------------------------------------------------------- *
  2. *
  3. * Copyright (C) 1991, 1992 Linus Torvalds
  4. * Copyright 2007 rPath, Inc. - All Rights Reserved
  5. *
  6. * This file is part of the Linux kernel, and is made available under
  7. * the terms of the GNU General Public License version 2.
  8. *
  9. * ----------------------------------------------------------------------- */
  10. /*
  11. * Main module for the real-mode kernel code
  12. */
  13. #include "boot.h"
  14. struct boot_params boot_params __attribute__((aligned(16)));
  15. char *HEAP = _end;
  16. char *heap_end = _end; /* Default end of heap = no heap */
  17. /*
  18. * Copy the header into the boot parameter block. Since this
  19. * screws up the old-style command line protocol, adjust by
  20. * filling in the new-style command line pointer instead.
  21. */
  22. static void copy_boot_params(void)
  23. {
  24. struct old_cmdline {
  25. u16 cl_magic;
  26. u16 cl_offset;
  27. };
  28. const struct old_cmdline * const oldcmd =
  29. (const struct old_cmdline *)OLD_CL_ADDRESS;
  30. BUILD_BUG_ON(sizeof boot_params != 4096);
  31. memcpy(&boot_params.hdr, &hdr, sizeof hdr);
  32. if (!boot_params.hdr.cmd_line_ptr &&
  33. oldcmd->cl_magic == OLD_CL_MAGIC) {
  34. /* Old-style command line protocol. */
  35. u16 cmdline_seg;
  36. /* Figure out if the command line falls in the region
  37. of memory that an old kernel would have copied up
  38. to 0x90000... */
  39. if (oldcmd->cl_offset < boot_params.hdr.setup_move_size)
  40. cmdline_seg = ds();
  41. else
  42. cmdline_seg = 0x9000;
  43. boot_params.hdr.cmd_line_ptr =
  44. (cmdline_seg << 4) + oldcmd->cl_offset;
  45. }
  46. }
  47. /*
  48. * Set the keyboard repeat rate to maximum. Unclear why this
  49. * is done here; this might be possible to kill off as stale code.
  50. */
  51. static void keyboard_set_repeat(void)
  52. {
  53. u16 ax = 0x0305;
  54. u16 bx = 0;
  55. asm volatile("int $0x16"
  56. : "+a" (ax), "+b" (bx)
  57. : : "ecx", "edx", "esi", "edi");
  58. }
  59. /*
  60. * Get Intel SpeedStep (IST) information.
  61. */
  62. static void query_ist(void)
  63. {
  64. /* Some 486 BIOSes apparently crash on this call */
  65. if (cpu.level < 6)
  66. return;
  67. asm("int $0x15"
  68. : "=a" (boot_params.ist_info.signature),
  69. "=b" (boot_params.ist_info.command),
  70. "=c" (boot_params.ist_info.event),
  71. "=d" (boot_params.ist_info.perf_level)
  72. : "a" (0x0000e980), /* IST Support */
  73. "d" (0x47534943)); /* Request value */
  74. }
  75. /*
  76. * Tell the BIOS what CPU mode we intend to run in.
  77. */
  78. static void set_bios_mode(void)
  79. {
  80. #ifdef CONFIG_X86_64
  81. u32 eax, ebx;
  82. eax = 0xec00;
  83. ebx = 2;
  84. asm volatile("int $0x15"
  85. : "+a" (eax), "+b" (ebx)
  86. : : "ecx", "edx", "esi", "edi");
  87. #endif
  88. }
  89. static void init_heap(void)
  90. {
  91. char *stack_end;
  92. if (boot_params.hdr.loadflags & CAN_USE_HEAP) {
  93. asm("leal %P1(%%esp),%0"
  94. : "=r" (stack_end) : "i" (-STACK_SIZE));
  95. heap_end = (char *)
  96. ((size_t)boot_params.hdr.heap_end_ptr + 0x200);
  97. if (heap_end > stack_end)
  98. heap_end = stack_end;
  99. } else {
  100. /* Boot protocol 2.00 only, no heap available */
  101. puts("WARNING: Ancient bootloader, some functionality "
  102. "may be limited!\n");
  103. }
  104. }
  105. void main(void)
  106. {
  107. /* First, copy the boot header into the "zeropage" */
  108. copy_boot_params();
  109. /* End of heap check */
  110. init_heap();
  111. /* Make sure we have all the proper CPU support */
  112. if (validate_cpu()) {
  113. puts("Unable to boot - please use a kernel appropriate "
  114. "for your CPU.\n");
  115. die();
  116. }
  117. /* Tell the BIOS what CPU mode we intend to run in. */
  118. set_bios_mode();
  119. /* Detect memory layout */
  120. detect_memory();
  121. /* Set keyboard repeat rate (why?) */
  122. keyboard_set_repeat();
  123. /* Query MCA information */
  124. query_mca();
  125. /* Voyager */
  126. #ifdef CONFIG_X86_VOYAGER
  127. query_voyager();
  128. #endif
  129. /* Query Intel SpeedStep (IST) information */
  130. query_ist();
  131. /* Query APM information */
  132. #if defined(CONFIG_APM) || defined(CONFIG_APM_MODULE)
  133. query_apm_bios();
  134. #endif
  135. /* Query EDD information */
  136. #if defined(CONFIG_EDD) || defined(CONFIG_EDD_MODULE)
  137. query_edd();
  138. #endif
  139. /* Set the video mode */
  140. set_video();
  141. /* Parse command line for 'quiet' and pass it to decompressor. */
  142. if (cmdline_find_option_bool("quiet"))
  143. boot_params.hdr.loadflags |= QUIET_FLAG;
  144. /* Do the last things and invoke protected mode */
  145. go_to_protected_mode();
  146. }