video.c 7.2 KB

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