video.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764
  1. /*
  2. * GRUB -- GRand Unified Bootloader
  3. * Copyright (C) 2006,2007,2008,2009 Free Software Foundation, Inc.
  4. *
  5. * GRUB is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * GRUB is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include <grub/video.h>
  19. #include <grub/types.h>
  20. #include <grub/dl.h>
  21. #include <grub/misc.h>
  22. #include <grub/mm.h>
  23. #include <grub/i18n.h>
  24. GRUB_MOD_LICENSE ("GPLv3+");
  25. /* The list of video adapters registered to system. */
  26. grub_video_adapter_t grub_video_adapter_list = NULL;
  27. /* Active video adapter. */
  28. grub_video_adapter_t grub_video_adapter_active;
  29. /* Restore back to initial mode (where applicable). */
  30. grub_err_t
  31. grub_video_restore (void)
  32. {
  33. if (grub_video_adapter_active)
  34. {
  35. grub_video_adapter_active->fini ();
  36. if (grub_errno != GRUB_ERR_NONE)
  37. return grub_errno;
  38. grub_video_adapter_active = 0;
  39. }
  40. return GRUB_ERR_NONE;
  41. }
  42. /* Get information about active video mode. */
  43. grub_err_t
  44. grub_video_get_info (struct grub_video_mode_info *mode_info)
  45. {
  46. if (! grub_video_adapter_active)
  47. return grub_error (GRUB_ERR_BAD_DEVICE, "no video mode activated");
  48. /* If mode_info is NULL just report that video adapter is active. */
  49. if (! mode_info)
  50. {
  51. grub_errno = GRUB_ERR_NONE;
  52. return grub_errno;
  53. }
  54. return grub_video_adapter_active->get_info (mode_info);
  55. }
  56. grub_video_driver_id_t
  57. grub_video_get_driver_id (void)
  58. {
  59. if (! grub_video_adapter_active)
  60. return GRUB_VIDEO_DRIVER_NONE;
  61. return grub_video_adapter_active->id;
  62. }
  63. /* Get information about active video mode. */
  64. grub_err_t
  65. grub_video_get_info_and_fini (struct grub_video_mode_info *mode_info,
  66. void **framebuffer)
  67. {
  68. grub_err_t err;
  69. if (! grub_video_adapter_active)
  70. return grub_error (GRUB_ERR_BAD_DEVICE, "no video mode activated");
  71. err = grub_video_adapter_active->get_info_and_fini (mode_info, framebuffer);
  72. if (err)
  73. return err;
  74. grub_video_adapter_active = 0;
  75. return GRUB_ERR_NONE;
  76. }
  77. /* Determine optimized blitting formation for specified video mode info. */
  78. enum grub_video_blit_format
  79. grub_video_get_blit_format (struct grub_video_mode_info *mode_info)
  80. {
  81. /* Check if we have any known 32 bit modes. */
  82. if (mode_info->bpp == 32)
  83. {
  84. if ((mode_info->red_mask_size == 8)
  85. && (mode_info->red_field_pos == 16)
  86. && (mode_info->green_mask_size == 8)
  87. && (mode_info->green_field_pos == 8)
  88. && (mode_info->blue_mask_size == 8)
  89. && (mode_info->blue_field_pos == 0))
  90. {
  91. return GRUB_VIDEO_BLIT_FORMAT_BGRA_8888;
  92. }
  93. else if ((mode_info->red_mask_size == 8)
  94. && (mode_info->red_field_pos == 0)
  95. && (mode_info->green_mask_size == 8)
  96. && (mode_info->green_field_pos == 8)
  97. && (mode_info->blue_mask_size == 8)
  98. && (mode_info->blue_field_pos == 16))
  99. {
  100. return GRUB_VIDEO_BLIT_FORMAT_RGBA_8888;
  101. }
  102. }
  103. /* Check if we have any known 24 bit modes. */
  104. else if (mode_info->bpp == 24)
  105. {
  106. if ((mode_info->red_mask_size == 8)
  107. && (mode_info->red_field_pos == 16)
  108. && (mode_info->green_mask_size == 8)
  109. && (mode_info->green_field_pos == 8)
  110. && (mode_info->blue_mask_size == 8)
  111. && (mode_info->blue_field_pos == 0))
  112. {
  113. return GRUB_VIDEO_BLIT_FORMAT_BGR_888;
  114. }
  115. else if ((mode_info->red_mask_size == 8)
  116. && (mode_info->red_field_pos == 0)
  117. && (mode_info->green_mask_size == 8)
  118. && (mode_info->green_field_pos == 8)
  119. && (mode_info->blue_mask_size == 8)
  120. && (mode_info->blue_field_pos == 16))
  121. {
  122. return GRUB_VIDEO_BLIT_FORMAT_RGB_888;
  123. }
  124. }
  125. /* Check if we have any known 16 bit modes. */
  126. else if (mode_info->bpp == 16)
  127. {
  128. if ((mode_info->red_mask_size == 5)
  129. && (mode_info->red_field_pos == 11)
  130. && (mode_info->green_mask_size == 6)
  131. && (mode_info->green_field_pos == 5)
  132. && (mode_info->blue_mask_size == 5)
  133. && (mode_info->blue_field_pos == 0))
  134. {
  135. return GRUB_VIDEO_BLIT_FORMAT_BGR_565;
  136. }
  137. else if ((mode_info->red_mask_size == 5)
  138. && (mode_info->red_field_pos == 0)
  139. && (mode_info->green_mask_size == 6)
  140. && (mode_info->green_field_pos == 5)
  141. && (mode_info->blue_mask_size == 5)
  142. && (mode_info->blue_field_pos == 11))
  143. {
  144. return GRUB_VIDEO_BLIT_FORMAT_RGB_565;
  145. }
  146. }
  147. else if (mode_info->bpp == 1)
  148. return GRUB_VIDEO_BLIT_FORMAT_1BIT_PACKED;
  149. /* Backup route. Unknown format. */
  150. /* If there are more than 8 bits per color, assume RGB(A) mode. */
  151. if (mode_info->bpp > 8)
  152. {
  153. if (mode_info->reserved_mask_size > 0)
  154. {
  155. return GRUB_VIDEO_BLIT_FORMAT_RGBA;
  156. }
  157. else
  158. {
  159. return GRUB_VIDEO_BLIT_FORMAT_RGB;
  160. }
  161. }
  162. /* Assume as indexcolor mode. */
  163. return GRUB_VIDEO_BLIT_FORMAT_INDEXCOLOR;
  164. }
  165. /* Set new indexed color palette entries. */
  166. grub_err_t
  167. grub_video_set_palette (unsigned int start, unsigned int count,
  168. struct grub_video_palette_data *palette_data)
  169. {
  170. if (! grub_video_adapter_active)
  171. return grub_error (GRUB_ERR_BAD_DEVICE, "no video mode activated");
  172. return grub_video_adapter_active->set_palette (start, count, palette_data);
  173. }
  174. /* Get indexed color palette entries. */
  175. grub_err_t
  176. grub_video_get_palette (unsigned int start, unsigned int count,
  177. struct grub_video_palette_data *palette_data)
  178. {
  179. if (! grub_video_adapter_active)
  180. return grub_error (GRUB_ERR_BAD_DEVICE, "no video mode activated");
  181. return grub_video_adapter_active->get_palette (start, count, palette_data);
  182. }
  183. /* Set viewport dimensions. */
  184. grub_err_t
  185. grub_video_set_viewport (unsigned int x, unsigned int y,
  186. unsigned int width, unsigned int height)
  187. {
  188. if (! grub_video_adapter_active)
  189. return grub_error (GRUB_ERR_BAD_DEVICE, "no video mode activated");
  190. return grub_video_adapter_active->set_viewport (x, y, width, height);
  191. }
  192. /* Get viewport dimensions. */
  193. grub_err_t
  194. grub_video_get_viewport (unsigned int *x, unsigned int *y,
  195. unsigned int *width, unsigned int *height)
  196. {
  197. if (! grub_video_adapter_active)
  198. return grub_error (GRUB_ERR_BAD_DEVICE, "no video mode activated");
  199. return grub_video_adapter_active->get_viewport (x, y, width, height);
  200. }
  201. /* Set region dimensions. */
  202. grub_err_t
  203. grub_video_set_region (unsigned int x, unsigned int y,
  204. unsigned int width, unsigned int height)
  205. {
  206. if (! grub_video_adapter_active)
  207. return grub_error (GRUB_ERR_BAD_DEVICE, "no video mode activated");
  208. return grub_video_adapter_active->set_region (x, y, width, height);
  209. }
  210. /* Get region dimensions. */
  211. grub_err_t
  212. grub_video_get_region (unsigned int *x, unsigned int *y,
  213. unsigned int *width, unsigned int *height)
  214. {
  215. if (! grub_video_adapter_active)
  216. return grub_error (GRUB_ERR_BAD_DEVICE, "no video mode activated");
  217. return grub_video_adapter_active->get_region (x, y, width, height);
  218. }
  219. /* Set status of the intersection of the viewport and the region. */
  220. grub_err_t
  221. grub_video_set_area_status (grub_video_area_status_t area_status)
  222. {
  223. if (! grub_video_adapter_active)
  224. return grub_error (GRUB_ERR_BAD_DEVICE, "no video mode activated");
  225. return grub_video_adapter_active->set_area_status (area_status);
  226. }
  227. /* Get status of the intersection of the viewport and the region. */
  228. grub_err_t
  229. grub_video_get_area_status (grub_video_area_status_t *area_status)
  230. {
  231. if (! grub_video_adapter_active)
  232. return grub_error (GRUB_ERR_BAD_DEVICE, "no video mode activated");
  233. return grub_video_adapter_active->get_area_status (area_status);
  234. }
  235. /* Map color name to adapter specific color. */
  236. grub_video_color_t
  237. grub_video_map_color (grub_uint32_t color_name)
  238. {
  239. if (! grub_video_adapter_active)
  240. return 0;
  241. return grub_video_adapter_active->map_color (color_name);
  242. }
  243. /* Map RGB value to adapter specific color. */
  244. grub_video_color_t
  245. grub_video_map_rgb (grub_uint8_t red, grub_uint8_t green, grub_uint8_t blue)
  246. {
  247. if (! grub_video_adapter_active)
  248. return 0;
  249. return grub_video_adapter_active->map_rgb (red, green, blue);
  250. }
  251. /* Map RGBA value to adapter specific color. */
  252. grub_video_color_t
  253. grub_video_map_rgba (grub_uint8_t red, grub_uint8_t green, grub_uint8_t blue,
  254. grub_uint8_t alpha)
  255. {
  256. if (! grub_video_adapter_active)
  257. return 0;
  258. return grub_video_adapter_active->map_rgba (red, green, blue, alpha);
  259. }
  260. /* Unmap video color back to RGBA components. */
  261. grub_err_t
  262. grub_video_unmap_color (grub_video_color_t color, grub_uint8_t *red,
  263. grub_uint8_t *green, grub_uint8_t *blue,
  264. grub_uint8_t *alpha)
  265. {
  266. if (! grub_video_adapter_active)
  267. return grub_error (GRUB_ERR_BAD_DEVICE, "no video mode activated");
  268. return grub_video_adapter_active->unmap_color (color,
  269. red,
  270. green,
  271. blue,
  272. alpha);
  273. }
  274. /* Fill rectangle using specified color. */
  275. grub_err_t
  276. grub_video_fill_rect (grub_video_color_t color, int x, int y,
  277. unsigned int width, unsigned int height)
  278. {
  279. if (! grub_video_adapter_active)
  280. return grub_error (GRUB_ERR_BAD_DEVICE, "no video mode activated");
  281. return grub_video_adapter_active->fill_rect (color, x, y, width, height);
  282. }
  283. /* Blit bitmap to screen. */
  284. grub_err_t
  285. grub_video_blit_bitmap (struct grub_video_bitmap *bitmap,
  286. enum grub_video_blit_operators oper,
  287. int x, int y, int offset_x, int offset_y,
  288. unsigned int width, unsigned int height)
  289. {
  290. if (! grub_video_adapter_active)
  291. return grub_error (GRUB_ERR_BAD_DEVICE, "no video mode activated");
  292. return grub_video_adapter_active->blit_bitmap (bitmap, oper, x, y,
  293. offset_x, offset_y,
  294. width, height);
  295. }
  296. /* Blit render target to active render target. */
  297. grub_err_t
  298. grub_video_blit_render_target (struct grub_video_render_target *target,
  299. enum grub_video_blit_operators oper,
  300. int x, int y, int offset_x, int offset_y,
  301. unsigned int width, unsigned int height)
  302. {
  303. if (! grub_video_adapter_active)
  304. return grub_error (GRUB_ERR_BAD_DEVICE, "no video mode activated");
  305. return grub_video_adapter_active->blit_render_target (target, oper, x, y,
  306. offset_x, offset_y,
  307. width, height);
  308. }
  309. /* Scroll viewport and fill new areas with specified color. */
  310. grub_err_t
  311. grub_video_scroll (grub_video_color_t color, int dx, int dy)
  312. {
  313. if (! grub_video_adapter_active)
  314. return grub_error (GRUB_ERR_BAD_DEVICE, "no video mode activated");
  315. return grub_video_adapter_active->scroll (color, dx, dy);
  316. }
  317. /* Swap buffers (swap active render target). */
  318. grub_err_t
  319. grub_video_swap_buffers (void)
  320. {
  321. if (! grub_video_adapter_active)
  322. return grub_error (GRUB_ERR_BAD_DEVICE, "no video mode activated");
  323. return grub_video_adapter_active->swap_buffers ();
  324. }
  325. /* Create new render target. */
  326. grub_err_t
  327. grub_video_create_render_target (struct grub_video_render_target **result,
  328. unsigned int width, unsigned int height,
  329. unsigned int mode_type)
  330. {
  331. *result = 0;
  332. if (! grub_video_adapter_active)
  333. return grub_error (GRUB_ERR_BAD_DEVICE, "no video mode activated");
  334. return grub_video_adapter_active->create_render_target (result,
  335. width, height,
  336. mode_type);
  337. }
  338. /* Delete render target. */
  339. grub_err_t
  340. grub_video_delete_render_target (struct grub_video_render_target *target)
  341. {
  342. if (!target)
  343. return GRUB_ERR_NONE;
  344. if (! grub_video_adapter_active)
  345. return grub_error (GRUB_ERR_BAD_DEVICE, "no video mode activated");
  346. return grub_video_adapter_active->delete_render_target (target);
  347. }
  348. /* Set active render target. */
  349. grub_err_t
  350. grub_video_set_active_render_target (struct grub_video_render_target *target)
  351. {
  352. if (! grub_video_adapter_active)
  353. return grub_error (GRUB_ERR_BAD_DEVICE, "no video mode activated");
  354. return grub_video_adapter_active->set_active_render_target (target);
  355. }
  356. /* Get active render target. */
  357. grub_err_t
  358. grub_video_get_active_render_target (struct grub_video_render_target **target)
  359. {
  360. if (! grub_video_adapter_active)
  361. return grub_error (GRUB_ERR_BAD_DEVICE, "no video mode activated");
  362. return grub_video_adapter_active->get_active_render_target (target);
  363. }
  364. grub_err_t
  365. grub_video_edid_checksum (struct grub_video_edid_info *edid_info)
  366. {
  367. const char *edid_bytes = (const char *) edid_info;
  368. int i;
  369. char checksum = 0;
  370. /* Check EDID checksum. */
  371. for (i = 0; i < 128; ++i)
  372. checksum += edid_bytes[i];
  373. if (checksum != 0)
  374. return grub_error (GRUB_ERR_BAD_DEVICE,
  375. "invalid EDID checksum %d", checksum);
  376. grub_errno = GRUB_ERR_NONE;
  377. return grub_errno;
  378. }
  379. grub_err_t
  380. grub_video_edid_preferred_mode (struct grub_video_edid_info *edid_info,
  381. unsigned int *width, unsigned int *height)
  382. {
  383. /* Bit 1 in the Feature Support field indicates that the first
  384. Detailed Timing Description is the preferred timing mode. */
  385. if (edid_info->version == 1 /* we don't understand later versions */
  386. && (edid_info->feature_support
  387. & GRUB_VIDEO_EDID_FEATURE_PREFERRED_TIMING_MODE)
  388. && edid_info->detailed_timings[0].pixel_clock)
  389. {
  390. *width = edid_info->detailed_timings[0].horizontal_active_lo
  391. | (((unsigned int)
  392. (edid_info->detailed_timings[0].horizontal_hi & 0xf0))
  393. << 4);
  394. *height = edid_info->detailed_timings[0].vertical_active_lo
  395. | (((unsigned int)
  396. (edid_info->detailed_timings[0].vertical_hi & 0xf0))
  397. << 4);
  398. if (*width && *height)
  399. return GRUB_ERR_NONE;
  400. }
  401. return grub_error (GRUB_ERR_BAD_DEVICE, "no preferred mode available");
  402. }
  403. /* Parse <width>x<height>[x<depth>]*/
  404. static grub_err_t
  405. parse_modespec (const char *current_mode, int *width, int *height, int *depth)
  406. {
  407. const char *value;
  408. const char *param = current_mode;
  409. *width = *height = *depth = -1;
  410. if (grub_strcmp (param, "auto") == 0)
  411. {
  412. *width = *height = 0;
  413. return GRUB_ERR_NONE;
  414. }
  415. /* Find width value. */
  416. value = param;
  417. param = grub_strchr(param, 'x');
  418. if (param == NULL)
  419. return grub_error (GRUB_ERR_BAD_ARGUMENT,
  420. N_("invalid video mode specification `%s'"),
  421. current_mode);
  422. param++;
  423. *width = grub_strtoul (value, 0, 0);
  424. if (grub_errno != GRUB_ERR_NONE)
  425. return grub_error (GRUB_ERR_BAD_ARGUMENT,
  426. N_("invalid video mode specification `%s'"),
  427. current_mode);
  428. /* Find height value. */
  429. value = param;
  430. param = grub_strchr(param, 'x');
  431. if (param == NULL)
  432. {
  433. *height = grub_strtoul (value, 0, 0);
  434. if (grub_errno != GRUB_ERR_NONE)
  435. return grub_error (GRUB_ERR_BAD_ARGUMENT,
  436. N_("invalid video mode specification `%s'"),
  437. current_mode);
  438. }
  439. else
  440. {
  441. /* We have optional color depth value. */
  442. param++;
  443. *height = grub_strtoul (value, 0, 0);
  444. if (grub_errno != GRUB_ERR_NONE)
  445. return grub_error (GRUB_ERR_BAD_ARGUMENT,
  446. N_("invalid video mode specification `%s'"),
  447. current_mode);
  448. /* Convert color depth value. */
  449. value = param;
  450. *depth = grub_strtoul (value, 0, 0);
  451. if (grub_errno != GRUB_ERR_NONE)
  452. return grub_error (GRUB_ERR_BAD_ARGUMENT,
  453. N_("invalid video mode specification `%s'"),
  454. current_mode);
  455. }
  456. return GRUB_ERR_NONE;
  457. }
  458. grub_err_t
  459. grub_video_set_mode (const char *modestring,
  460. unsigned int modemask,
  461. unsigned int modevalue)
  462. {
  463. char *tmp;
  464. char *next_mode;
  465. char *current_mode;
  466. char *modevar;
  467. if (grub_video_adapter_active && grub_video_adapter_active->id == GRUB_VIDEO_ADAPTER_CAPTURE)
  468. return GRUB_ERR_NONE;
  469. modevalue &= modemask;
  470. /* Take copy of env.var. as we don't want to modify that. */
  471. modevar = grub_strdup (modestring);
  472. /* Initialize next mode. */
  473. next_mode = modevar;
  474. if (! modevar)
  475. return grub_errno;
  476. if (grub_memcmp (next_mode, "keep", sizeof ("keep")) == 0
  477. || grub_memcmp (next_mode, "keep,", sizeof ("keep,") - 1) == 0
  478. || grub_memcmp (next_mode, "keep;", sizeof ("keep;") - 1) == 0)
  479. {
  480. int suitable = 1;
  481. grub_err_t err;
  482. if (grub_video_adapter_active)
  483. {
  484. struct grub_video_mode_info mode_info;
  485. grub_memset (&mode_info, 0, sizeof (mode_info));
  486. err = grub_video_get_info (&mode_info);
  487. if (err)
  488. {
  489. suitable = 0;
  490. grub_errno = GRUB_ERR_NONE;
  491. }
  492. if ((mode_info.mode_type & modemask) != modevalue)
  493. suitable = 0;
  494. }
  495. else if (((GRUB_VIDEO_MODE_TYPE_PURE_TEXT & modemask) != 0)
  496. && ((GRUB_VIDEO_MODE_TYPE_PURE_TEXT & modevalue) == 0))
  497. suitable = 0;
  498. if (suitable)
  499. {
  500. grub_free (modevar);
  501. return GRUB_ERR_NONE;
  502. }
  503. next_mode += sizeof ("keep") - 1;
  504. if (! *next_mode)
  505. {
  506. grub_free (modevar);
  507. /* TRANSLATORS: This doesn't imply that there is no available video
  508. mode at all. All modes may have been filtered out by some criteria.
  509. */
  510. return grub_error (GRUB_ERR_BAD_ARGUMENT,
  511. N_("no suitable video mode found"));
  512. }
  513. /* Skip separator. */
  514. next_mode++;
  515. }
  516. /* De-activate last set video adapter. */
  517. if (grub_video_adapter_active)
  518. {
  519. /* Finalize adapter. */
  520. grub_video_adapter_active->fini ();
  521. if (grub_errno != GRUB_ERR_NONE)
  522. grub_errno = GRUB_ERR_NONE;
  523. /* Mark active adapter as not set. */
  524. grub_video_adapter_active = 0;
  525. }
  526. /* Loop until all modes has been tested out. */
  527. while (next_mode != NULL)
  528. {
  529. int width = -1;
  530. int height = -1;
  531. int depth = -1;
  532. grub_err_t err;
  533. unsigned int flags = modevalue;
  534. unsigned int flagmask = modemask;
  535. /* Use last next_mode as current mode. */
  536. tmp = next_mode;
  537. /* Save position of next mode and separate modes. */
  538. for (; *next_mode; next_mode++)
  539. if (*next_mode == ',' || *next_mode == ';')
  540. break;
  541. if (*next_mode)
  542. {
  543. *next_mode = 0;
  544. next_mode++;
  545. }
  546. else
  547. next_mode = 0;
  548. /* Skip whitespace. */
  549. while (grub_isspace (*tmp))
  550. tmp++;
  551. /* Initialize token holders. */
  552. current_mode = tmp;
  553. /* XXX: we assume that we're in pure text mode if
  554. no video mode is initialized. Is it always true? */
  555. if (grub_strcmp (current_mode, "text") == 0)
  556. {
  557. struct grub_video_mode_info mode_info;
  558. grub_memset (&mode_info, 0, sizeof (mode_info));
  559. if (((GRUB_VIDEO_MODE_TYPE_PURE_TEXT & modemask) == 0)
  560. || ((GRUB_VIDEO_MODE_TYPE_PURE_TEXT & modevalue) != 0))
  561. {
  562. /* Valid mode found from adapter, and it has been activated.
  563. Specify it as active adapter. */
  564. grub_video_adapter_active = NULL;
  565. /* Free memory. */
  566. grub_free (modevar);
  567. return GRUB_ERR_NONE;
  568. }
  569. else
  570. continue;
  571. }
  572. err = parse_modespec (current_mode, &width, &height, &depth);
  573. if (err)
  574. {
  575. /* Free memory before returning. */
  576. grub_free (modevar);
  577. return err;
  578. }
  579. /* Try out video mode. */
  580. /* If user requested specific depth check if this depth is supported. */
  581. if (depth != -1 && (flagmask & GRUB_VIDEO_MODE_TYPE_DEPTH_MASK)
  582. &&
  583. (((flags & GRUB_VIDEO_MODE_TYPE_DEPTH_MASK)
  584. != ((depth << GRUB_VIDEO_MODE_TYPE_DEPTH_POS)
  585. & GRUB_VIDEO_MODE_TYPE_DEPTH_MASK))))
  586. continue;
  587. if (depth != -1)
  588. {
  589. flags |= (depth << GRUB_VIDEO_MODE_TYPE_DEPTH_POS)
  590. & GRUB_VIDEO_MODE_TYPE_DEPTH_MASK;
  591. flagmask |= GRUB_VIDEO_MODE_TYPE_DEPTH_MASK;
  592. }
  593. /* Try to initialize requested mode. Ignore any errors. */
  594. grub_video_adapter_t p;
  595. /* Loop thru all possible video adapter trying to find requested mode. */
  596. for (p = grub_video_adapter_list; p; p = p->next)
  597. {
  598. struct grub_video_mode_info mode_info;
  599. grub_memset (&mode_info, 0, sizeof (mode_info));
  600. /* Try to initialize adapter, if it fails, skip to next adapter. */
  601. err = p->init ();
  602. if (err != GRUB_ERR_NONE)
  603. {
  604. grub_errno = GRUB_ERR_NONE;
  605. continue;
  606. }
  607. /* Try to initialize video mode. */
  608. err = p->setup (width, height, flags, flagmask);
  609. if (err != GRUB_ERR_NONE)
  610. {
  611. p->fini ();
  612. grub_errno = GRUB_ERR_NONE;
  613. continue;
  614. }
  615. err = p->get_info (&mode_info);
  616. if (err != GRUB_ERR_NONE)
  617. {
  618. p->fini ();
  619. grub_errno = GRUB_ERR_NONE;
  620. continue;
  621. }
  622. flags = mode_info.mode_type & ~GRUB_VIDEO_MODE_TYPE_DEPTH_MASK;
  623. flags |= (mode_info.bpp << GRUB_VIDEO_MODE_TYPE_DEPTH_POS)
  624. & GRUB_VIDEO_MODE_TYPE_DEPTH_MASK;
  625. /* Check that mode is suitable for upper layer. */
  626. if ((flags & GRUB_VIDEO_MODE_TYPE_PURE_TEXT)
  627. ? (((GRUB_VIDEO_MODE_TYPE_PURE_TEXT & modemask) != 0)
  628. && ((GRUB_VIDEO_MODE_TYPE_PURE_TEXT & modevalue) == 0))
  629. : ((flags & modemask) != modevalue))
  630. {
  631. p->fini ();
  632. grub_errno = GRUB_ERR_NONE;
  633. continue;
  634. }
  635. /* Valid mode found from adapter, and it has been activated.
  636. Specify it as active adapter. */
  637. grub_video_adapter_active = p;
  638. /* Free memory. */
  639. grub_free (modevar);
  640. return GRUB_ERR_NONE;
  641. }
  642. }
  643. /* Free memory. */
  644. grub_free (modevar);
  645. return grub_error (GRUB_ERR_BAD_ARGUMENT,
  646. N_("no suitable video mode found"));
  647. }