video.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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. * arch/i386/boot/video.c
  12. *
  13. * Select video mode
  14. */
  15. #include "boot.h"
  16. #include "video.h"
  17. #include "vesa.h"
  18. static void store_cursor_position(void)
  19. {
  20. u16 curpos;
  21. u16 ax, bx;
  22. ax = 0x0300;
  23. bx = 0;
  24. asm(INT10
  25. : "=d" (curpos), "+a" (ax), "+b" (bx)
  26. : : "ecx", "esi", "edi");
  27. boot_params.screen_info.orig_x = curpos;
  28. boot_params.screen_info.orig_y = curpos >> 8;
  29. }
  30. static void store_video_mode(void)
  31. {
  32. u16 ax, page;
  33. /* N.B.: the saving of the video page here is a bit silly,
  34. since we pretty much assume page 0 everywhere. */
  35. ax = 0x0f00;
  36. asm(INT10
  37. : "+a" (ax), "=b" (page)
  38. : : "ecx", "edx", "esi", "edi");
  39. /* Not all BIOSes are clean with respect to the top bit */
  40. boot_params.screen_info.orig_video_mode = ax & 0x7f;
  41. boot_params.screen_info.orig_video_page = page >> 8;
  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. #ifdef CONFIG_VIDEO_RETAIN
  190. /* Save screen content to the heap */
  191. 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. u16 ax, bx, dx;
  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. ax = 0x0200; /* Set cursor position */
  250. bx = 0; /* Page number (<< 8) */
  251. dx = (saved.cury << 8)+saved.curx;
  252. asm volatile(INT10
  253. : "+a" (ax), "+b" (bx), "+d" (dx)
  254. : : "ecx", "esi", "edi");
  255. }
  256. #else
  257. #define save_screen() ((void)0)
  258. #define restore_screen() ((void)0)
  259. #endif
  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. }