video.c 7.1 KB

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