video.c 7.1 KB

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