video.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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. * Select video mode
  12. */
  13. #include "boot.h"
  14. #include "video.h"
  15. #include "vesa.h"
  16. static void store_cursor_position(void)
  17. {
  18. u16 curpos;
  19. u16 ax, bx;
  20. ax = 0x0300;
  21. bx = 0;
  22. asm(INT10
  23. : "=d" (curpos), "+a" (ax), "+b" (bx)
  24. : : "ecx", "esi", "edi");
  25. boot_params.screen_info.orig_x = curpos;
  26. boot_params.screen_info.orig_y = curpos >> 8;
  27. }
  28. static void store_video_mode(void)
  29. {
  30. u16 ax, page;
  31. /* N.B.: the saving of the video page here is a bit silly,
  32. since we pretty much assume page 0 everywhere. */
  33. ax = 0x0f00;
  34. asm(INT10
  35. : "+a" (ax), "=b" (page)
  36. : : "ecx", "edx", "esi", "edi");
  37. /* Not all BIOSes are clean with respect to the top bit */
  38. boot_params.screen_info.orig_video_mode = ax & 0x7f;
  39. boot_params.screen_info.orig_video_page = page >> 8;
  40. }
  41. /*
  42. * Store the video mode parameters for later usage by the kernel.
  43. * This is done by asking the BIOS except for the rows/columns
  44. * parameters in the default 80x25 mode -- these are set directly,
  45. * because some very obscure BIOSes supply insane values.
  46. */
  47. static void store_mode_params(void)
  48. {
  49. u16 font_size;
  50. int x, y;
  51. /* For graphics mode, it is up to the mode-setting driver
  52. (currently only video-vesa.c) to store the parameters */
  53. if (graphic_mode)
  54. return;
  55. store_cursor_position();
  56. store_video_mode();
  57. if (boot_params.screen_info.orig_video_mode == 0x07) {
  58. /* MDA, HGC, or VGA in monochrome mode */
  59. video_segment = 0xb000;
  60. } else {
  61. /* CGA, EGA, VGA and so forth */
  62. video_segment = 0xb800;
  63. }
  64. set_fs(0);
  65. font_size = rdfs16(0x485); /* Font size, BIOS area */
  66. boot_params.screen_info.orig_video_points = font_size;
  67. x = rdfs16(0x44a);
  68. y = (adapter == ADAPTER_CGA) ? 25 : rdfs8(0x484)+1;
  69. if (force_x)
  70. x = force_x;
  71. if (force_y)
  72. y = force_y;
  73. boot_params.screen_info.orig_video_cols = x;
  74. boot_params.screen_info.orig_video_lines = y;
  75. }
  76. static unsigned int get_entry(void)
  77. {
  78. char entry_buf[4];
  79. int i, len = 0;
  80. int key;
  81. unsigned int v;
  82. do {
  83. key = getchar();
  84. if (key == '\b') {
  85. if (len > 0) {
  86. puts("\b \b");
  87. len--;
  88. }
  89. } else if ((key >= '0' && key <= '9') ||
  90. (key >= 'A' && key <= 'Z') ||
  91. (key >= 'a' && key <= 'z')) {
  92. if (len < sizeof entry_buf) {
  93. entry_buf[len++] = key;
  94. putchar(key);
  95. }
  96. }
  97. } while (key != '\r');
  98. putchar('\n');
  99. if (len == 0)
  100. return VIDEO_CURRENT_MODE; /* Default */
  101. v = 0;
  102. for (i = 0; i < len; i++) {
  103. v <<= 4;
  104. key = entry_buf[i] | 0x20;
  105. v += (key > '9') ? key-'a'+10 : key-'0';
  106. }
  107. return v;
  108. }
  109. static void display_menu(void)
  110. {
  111. struct card_info *card;
  112. struct mode_info *mi;
  113. char ch;
  114. int i;
  115. int nmodes;
  116. int modes_per_line;
  117. int col;
  118. nmodes = 0;
  119. for (card = video_cards; card < video_cards_end; card++)
  120. nmodes += card->nmodes;
  121. modes_per_line = 1;
  122. if (nmodes >= 20)
  123. modes_per_line = 3;
  124. for (col = 0; col < modes_per_line; col++)
  125. puts("Mode: Resolution: Type: ");
  126. putchar('\n');
  127. col = 0;
  128. ch = '0';
  129. for (card = video_cards; card < video_cards_end; card++) {
  130. mi = card->modes;
  131. for (i = 0; i < card->nmodes; i++, mi++) {
  132. char resbuf[32];
  133. int visible = mi->x && mi->y;
  134. u16 mode_id = mi->mode ? mi->mode :
  135. (mi->y << 8)+mi->x;
  136. if (!visible)
  137. continue; /* Hidden mode */
  138. if (mi->depth)
  139. sprintf(resbuf, "%dx%d", mi->y, mi->depth);
  140. else
  141. sprintf(resbuf, "%d", mi->y);
  142. printf("%c %03X %4dx%-7s %-6s",
  143. ch, mode_id, mi->x, resbuf, card->card_name);
  144. col++;
  145. if (col >= modes_per_line) {
  146. putchar('\n');
  147. col = 0;
  148. }
  149. if (ch == '9')
  150. ch = 'a';
  151. else if (ch == 'z' || ch == ' ')
  152. ch = ' '; /* Out of keys... */
  153. else
  154. ch++;
  155. }
  156. }
  157. if (col)
  158. putchar('\n');
  159. }
  160. #define H(x) ((x)-'a'+10)
  161. #define SCAN ((H('s')<<12)+(H('c')<<8)+(H('a')<<4)+H('n'))
  162. static unsigned int mode_menu(void)
  163. {
  164. int key;
  165. unsigned int sel;
  166. puts("Press <ENTER> to see video modes available, "
  167. "<SPACE> to continue, or wait 30 sec\n");
  168. kbd_flush();
  169. while (1) {
  170. key = getchar_timeout();
  171. if (key == ' ' || key == 0)
  172. return VIDEO_CURRENT_MODE; /* Default */
  173. if (key == '\r')
  174. break;
  175. putchar('\a'); /* Beep! */
  176. }
  177. for (;;) {
  178. display_menu();
  179. puts("Enter a video mode or \"scan\" to scan for "
  180. "additional modes: ");
  181. sel = get_entry();
  182. if (sel != SCAN)
  183. return sel;
  184. probe_cards(1);
  185. }
  186. }
  187. #ifdef CONFIG_VIDEO_RETAIN
  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. u16 ax, bx, dx;
  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. ax = 0x0200; /* Set cursor position */
  248. bx = 0; /* Page number (<< 8) */
  249. dx = (saved.cury << 8)+saved.curx;
  250. asm volatile(INT10
  251. : "+a" (ax), "+b" (bx), "+d" (dx)
  252. : : "ecx", "esi", "edi");
  253. }
  254. #else
  255. #define save_screen() ((void)0)
  256. #define restore_screen() ((void)0)
  257. #endif
  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. }