video.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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. * Select video mode
  13. */
  14. #include "boot.h"
  15. #include "video.h"
  16. #include "vesa.h"
  17. static void store_cursor_position(void)
  18. {
  19. struct biosregs ireg, oreg;
  20. initregs(&ireg);
  21. ireg.ah = 0x03;
  22. intcall(0x10, &ireg, &oreg);
  23. boot_params.screen_info.orig_x = oreg.dl;
  24. boot_params.screen_info.orig_y = oreg.dh;
  25. }
  26. static void store_video_mode(void)
  27. {
  28. struct biosregs ireg, oreg;
  29. /* N.B.: the saving of the video page here is a bit silly,
  30. since we pretty much assume page 0 everywhere. */
  31. initregs(&ireg);
  32. ireg.ah = 0x0f;
  33. intcall(0x10, &ireg, &oreg);
  34. /* Not all BIOSes are clean with respect to the top bit */
  35. boot_params.screen_info.orig_video_mode = oreg.al & 0x7f;
  36. boot_params.screen_info.orig_video_page = oreg.bh;
  37. }
  38. /*
  39. * Store the video mode parameters for later usage by the kernel.
  40. * This is done by asking the BIOS except for the rows/columns
  41. * parameters in the default 80x25 mode -- these are set directly,
  42. * because some very obscure BIOSes supply insane values.
  43. */
  44. static void store_mode_params(void)
  45. {
  46. u16 font_size;
  47. int x, y;
  48. /* For graphics mode, it is up to the mode-setting driver
  49. (currently only video-vesa.c) to store the parameters */
  50. if (graphic_mode)
  51. return;
  52. store_cursor_position();
  53. store_video_mode();
  54. if (boot_params.screen_info.orig_video_mode == 0x07) {
  55. /* MDA, HGC, or VGA in monochrome mode */
  56. video_segment = 0xb000;
  57. } else {
  58. /* CGA, EGA, VGA and so forth */
  59. video_segment = 0xb800;
  60. }
  61. set_fs(0);
  62. font_size = rdfs16(0x485); /* Font size, BIOS area */
  63. boot_params.screen_info.orig_video_points = font_size;
  64. x = rdfs16(0x44a);
  65. y = (adapter == ADAPTER_CGA) ? 25 : rdfs8(0x484)+1;
  66. if (force_x)
  67. x = force_x;
  68. if (force_y)
  69. y = force_y;
  70. boot_params.screen_info.orig_video_cols = x;
  71. boot_params.screen_info.orig_video_lines = y;
  72. }
  73. static unsigned int get_entry(void)
  74. {
  75. char entry_buf[4];
  76. int i, len = 0;
  77. int key;
  78. unsigned int v;
  79. do {
  80. key = getchar();
  81. if (key == '\b') {
  82. if (len > 0) {
  83. puts("\b \b");
  84. len--;
  85. }
  86. } else if ((key >= '0' && key <= '9') ||
  87. (key >= 'A' && key <= 'Z') ||
  88. (key >= 'a' && key <= 'z')) {
  89. if (len < sizeof entry_buf) {
  90. entry_buf[len++] = key;
  91. putchar(key);
  92. }
  93. }
  94. } while (key != '\r');
  95. putchar('\n');
  96. if (len == 0)
  97. return VIDEO_CURRENT_MODE; /* Default */
  98. v = 0;
  99. for (i = 0; i < len; i++) {
  100. v <<= 4;
  101. key = entry_buf[i] | 0x20;
  102. v += (key > '9') ? key-'a'+10 : key-'0';
  103. }
  104. return v;
  105. }
  106. static void display_menu(void)
  107. {
  108. struct card_info *card;
  109. struct mode_info *mi;
  110. char ch;
  111. int i;
  112. int nmodes;
  113. int modes_per_line;
  114. int col;
  115. nmodes = 0;
  116. for (card = video_cards; card < video_cards_end; card++)
  117. nmodes += card->nmodes;
  118. modes_per_line = 1;
  119. if (nmodes >= 20)
  120. modes_per_line = 3;
  121. for (col = 0; col < modes_per_line; col++)
  122. puts("Mode: Resolution: Type: ");
  123. putchar('\n');
  124. col = 0;
  125. ch = '0';
  126. for (card = video_cards; card < video_cards_end; card++) {
  127. mi = card->modes;
  128. for (i = 0; i < card->nmodes; i++, mi++) {
  129. char resbuf[32];
  130. int visible = mi->x && mi->y;
  131. u16 mode_id = mi->mode ? mi->mode :
  132. (mi->y << 8)+mi->x;
  133. if (!visible)
  134. continue; /* Hidden mode */
  135. if (mi->depth)
  136. sprintf(resbuf, "%dx%d", mi->y, mi->depth);
  137. else
  138. sprintf(resbuf, "%d", mi->y);
  139. printf("%c %03X %4dx%-7s %-6s",
  140. ch, mode_id, mi->x, resbuf, card->card_name);
  141. col++;
  142. if (col >= modes_per_line) {
  143. putchar('\n');
  144. col = 0;
  145. }
  146. if (ch == '9')
  147. ch = 'a';
  148. else if (ch == 'z' || ch == ' ')
  149. ch = ' '; /* Out of keys... */
  150. else
  151. ch++;
  152. }
  153. }
  154. if (col)
  155. putchar('\n');
  156. }
  157. #define H(x) ((x)-'a'+10)
  158. #define SCAN ((H('s')<<12)+(H('c')<<8)+(H('a')<<4)+H('n'))
  159. static unsigned int mode_menu(void)
  160. {
  161. int key;
  162. unsigned int sel;
  163. puts("Press <ENTER> to see video modes available, "
  164. "<SPACE> to continue, or wait 30 sec\n");
  165. kbd_flush();
  166. while (1) {
  167. key = getchar_timeout();
  168. if (key == ' ' || key == 0)
  169. return VIDEO_CURRENT_MODE; /* Default */
  170. if (key == '\r')
  171. break;
  172. putchar('\a'); /* Beep! */
  173. }
  174. for (;;) {
  175. display_menu();
  176. puts("Enter a video mode or \"scan\" to scan for "
  177. "additional modes: ");
  178. sel = get_entry();
  179. if (sel != SCAN)
  180. return sel;
  181. probe_cards(1);
  182. }
  183. }
  184. /* Save screen content to the heap */
  185. static struct saved_screen {
  186. int x, y;
  187. int curx, cury;
  188. u16 *data;
  189. } saved;
  190. static void save_screen(void)
  191. {
  192. /* Should be called after store_mode_params() */
  193. saved.x = boot_params.screen_info.orig_video_cols;
  194. saved.y = boot_params.screen_info.orig_video_lines;
  195. saved.curx = boot_params.screen_info.orig_x;
  196. saved.cury = boot_params.screen_info.orig_y;
  197. if (!heap_free(saved.x*saved.y*sizeof(u16)+512))
  198. return; /* Not enough heap to save the screen */
  199. saved.data = GET_HEAP(u16, saved.x*saved.y);
  200. set_fs(video_segment);
  201. copy_from_fs(saved.data, 0, saved.x*saved.y*sizeof(u16));
  202. }
  203. static void restore_screen(void)
  204. {
  205. /* Should be called after store_mode_params() */
  206. int xs = boot_params.screen_info.orig_video_cols;
  207. int ys = boot_params.screen_info.orig_video_lines;
  208. int y;
  209. addr_t dst = 0;
  210. u16 *src = saved.data;
  211. struct biosregs ireg;
  212. if (graphic_mode)
  213. return; /* Can't restore onto a graphic mode */
  214. if (!src)
  215. return; /* No saved screen contents */
  216. /* Restore screen contents */
  217. set_fs(video_segment);
  218. for (y = 0; y < ys; y++) {
  219. int npad;
  220. if (y < saved.y) {
  221. int copy = (xs < saved.x) ? xs : saved.x;
  222. copy_to_fs(dst, src, copy*sizeof(u16));
  223. dst += copy*sizeof(u16);
  224. src += saved.x;
  225. npad = (xs < saved.x) ? 0 : xs-saved.x;
  226. } else {
  227. npad = xs;
  228. }
  229. /* Writes "npad" blank characters to
  230. video_segment:dst and advances dst */
  231. asm volatile("pushw %%es ; "
  232. "movw %2,%%es ; "
  233. "shrw %%cx ; "
  234. "jnc 1f ; "
  235. "stosw \n\t"
  236. "1: rep;stosl ; "
  237. "popw %%es"
  238. : "+D" (dst), "+c" (npad)
  239. : "bdS" (video_segment),
  240. "a" (0x07200720));
  241. }
  242. /* Restore cursor position */
  243. initregs(&ireg);
  244. ireg.ah = 0x02; /* Set cursor position */
  245. ireg.dh = saved.cury;
  246. ireg.dl = saved.curx;
  247. intcall(0x10, &ireg, NULL);
  248. }
  249. void set_video(void)
  250. {
  251. u16 mode = boot_params.hdr.vid_mode;
  252. RESET_HEAP();
  253. store_mode_params();
  254. save_screen();
  255. probe_cards(0);
  256. for (;;) {
  257. if (mode == ASK_VGA)
  258. mode = mode_menu();
  259. if (!set_mode(mode))
  260. break;
  261. printf("Undefined video mode number: %x\n", mode);
  262. mode = ASK_VGA;
  263. }
  264. boot_params.hdr.vid_mode = mode;
  265. vesa_store_edid();
  266. store_mode_params();
  267. if (do_restore)
  268. restore_screen();
  269. }