videoinfo.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. /* videoinfo.c - command to list video modes. */
  2. /*
  3. * GRUB -- GRand Unified Bootloader
  4. * Copyright (C) 2005,2007,2008,2009,2010 Free Software Foundation, Inc.
  5. *
  6. * GRUB is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * GRUB is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include <grub/video.h>
  20. #include <grub/dl.h>
  21. #include <grub/env.h>
  22. #include <grub/misc.h>
  23. #include <grub/mm.h>
  24. #include <grub/command.h>
  25. #include <grub/i18n.h>
  26. GRUB_MOD_LICENSE ("GPLv3+");
  27. struct hook_ctx
  28. {
  29. unsigned height, width, depth;
  30. struct grub_video_mode_info *current_mode;
  31. };
  32. static int
  33. hook (const struct grub_video_mode_info *info, void *hook_arg)
  34. {
  35. struct hook_ctx *ctx = hook_arg;
  36. if (ctx->height && ctx->width && (info->width != ctx->width || info->height != ctx->height))
  37. return 0;
  38. if (ctx->depth && info->bpp != ctx->depth)
  39. return 0;
  40. if (info->mode_number == GRUB_VIDEO_MODE_NUMBER_INVALID)
  41. grub_printf (" ");
  42. else
  43. {
  44. if (ctx->current_mode && info->mode_number == ctx->current_mode->mode_number)
  45. grub_printf ("*");
  46. else
  47. grub_printf (" ");
  48. grub_printf (" 0x%03x ", info->mode_number);
  49. }
  50. grub_printf ("%4d x %4d x %2d (%4d) ", info->width, info->height, info->bpp,
  51. info->pitch);
  52. if (info->mode_type & GRUB_VIDEO_MODE_TYPE_PURE_TEXT)
  53. grub_xputs (_("Text-only "));
  54. /* Show mask and position details for direct color modes. */
  55. if (info->mode_type & GRUB_VIDEO_MODE_TYPE_RGB)
  56. /* TRANSLATORS: "Direct color" is a mode when the color components
  57. are written dirrectly into memory. */
  58. grub_printf_ (N_("Direct color, mask: %d/%d/%d/%d pos: %d/%d/%d/%d"),
  59. info->red_mask_size,
  60. info->green_mask_size,
  61. info->blue_mask_size,
  62. info->reserved_mask_size,
  63. info->red_field_pos,
  64. info->green_field_pos,
  65. info->blue_field_pos,
  66. info->reserved_field_pos);
  67. if (info->mode_type & GRUB_VIDEO_MODE_TYPE_INDEX_COLOR)
  68. /* TRANSLATORS: In "paletted color" mode you write the index of the color
  69. in the palette. Synonyms include "packed pixel". */
  70. grub_xputs (_("Paletted "));
  71. if (info->mode_type & GRUB_VIDEO_MODE_TYPE_YUV)
  72. grub_xputs (_("YUV "));
  73. if (info->mode_type & GRUB_VIDEO_MODE_TYPE_PLANAR)
  74. /* TRANSLATORS: "Planar" is the video memory where you have to write
  75. in several different banks "plans" to control the different color
  76. components of the same pixel. */
  77. grub_xputs (_("Planar "));
  78. if (info->mode_type & GRUB_VIDEO_MODE_TYPE_HERCULES)
  79. grub_xputs (_("Hercules "));
  80. if (info->mode_type & GRUB_VIDEO_MODE_TYPE_CGA)
  81. grub_xputs (_("CGA "));
  82. if (info->mode_type & GRUB_VIDEO_MODE_TYPE_NONCHAIN4)
  83. /* TRANSLATORS: Non-chain 4 is a 256-color planar
  84. (unchained) video memory mode. */
  85. grub_xputs (_("Non-chain 4 "));
  86. if (info->mode_type & GRUB_VIDEO_MODE_TYPE_1BIT_BITMAP)
  87. grub_xputs (_("Monochrome "));
  88. if (info->mode_type & GRUB_VIDEO_MODE_TYPE_UNKNOWN)
  89. grub_xputs (_("Unknown video mode "));
  90. grub_xputs ("\n");
  91. return 0;
  92. }
  93. static void
  94. print_edid (struct grub_video_edid_info *edid_info)
  95. {
  96. unsigned int edid_width, edid_height;
  97. if (grub_video_edid_checksum (edid_info))
  98. {
  99. grub_puts_ (N_(" EDID checksum invalid"));
  100. grub_errno = GRUB_ERR_NONE;
  101. return;
  102. }
  103. grub_printf_ (N_(" EDID version: %u.%u\n"),
  104. edid_info->version, edid_info->revision);
  105. if (grub_video_edid_preferred_mode (edid_info, &edid_width, &edid_height)
  106. == GRUB_ERR_NONE)
  107. grub_printf_ (N_(" Preferred mode: %ux%u\n"), edid_width, edid_height);
  108. else
  109. {
  110. grub_printf_ (N_(" No preferred mode available\n"));
  111. grub_errno = GRUB_ERR_NONE;
  112. }
  113. }
  114. static grub_err_t
  115. grub_cmd_videoinfo (grub_command_t cmd __attribute__ ((unused)),
  116. int argc, char **args)
  117. {
  118. grub_video_adapter_t adapter;
  119. grub_video_driver_id_t id;
  120. struct hook_ctx ctx;
  121. ctx.height = ctx.width = ctx.depth = 0;
  122. if (argc)
  123. {
  124. char *ptr;
  125. ptr = args[0];
  126. ctx.width = grub_strtoul (ptr, &ptr, 0);
  127. if (grub_errno)
  128. return grub_errno;
  129. if (*ptr != 'x')
  130. return grub_error (GRUB_ERR_BAD_ARGUMENT,
  131. N_("invalid video mode specification `%s'"),
  132. args[0]);
  133. ptr++;
  134. ctx.height = grub_strtoul (ptr, &ptr, 0);
  135. if (grub_errno)
  136. return grub_errno;
  137. if (*ptr == 'x')
  138. {
  139. ptr++;
  140. ctx.depth = grub_strtoul (ptr, &ptr, 0);
  141. if (grub_errno)
  142. return grub_errno;
  143. }
  144. }
  145. #ifdef GRUB_MACHINE_PCBIOS
  146. if (grub_strcmp (cmd->name, "vbeinfo") == 0)
  147. grub_dl_load ("vbe");
  148. #endif
  149. id = grub_video_get_driver_id ();
  150. grub_puts_ (N_("List of supported video modes:"));
  151. grub_puts_ (N_("Legend: mask/position=red/green/blue/reserved"));
  152. FOR_VIDEO_ADAPTERS (adapter)
  153. {
  154. struct grub_video_mode_info info;
  155. struct grub_video_edid_info edid_info;
  156. grub_printf_ (N_("Adapter `%s':\n"), adapter->name);
  157. if (!adapter->iterate)
  158. {
  159. grub_puts_ (N_(" No info available"));
  160. continue;
  161. }
  162. ctx.current_mode = NULL;
  163. if (adapter->id == id)
  164. {
  165. if (grub_video_get_info (&info) == GRUB_ERR_NONE)
  166. ctx.current_mode = &info;
  167. else
  168. /* Don't worry about errors. */
  169. grub_errno = GRUB_ERR_NONE;
  170. }
  171. else
  172. {
  173. if (adapter->init ())
  174. {
  175. grub_puts_ (N_(" Failed to initialize video adapter"));
  176. grub_errno = GRUB_ERR_NONE;
  177. continue;
  178. }
  179. }
  180. if (adapter->print_adapter_specific_info)
  181. adapter->print_adapter_specific_info ();
  182. adapter->iterate (hook, &ctx);
  183. if (adapter->get_edid && adapter->get_edid (&edid_info) == GRUB_ERR_NONE)
  184. print_edid (&edid_info);
  185. else
  186. grub_errno = GRUB_ERR_NONE;
  187. ctx.current_mode = NULL;
  188. if (adapter->id != id)
  189. {
  190. if (adapter->fini ())
  191. {
  192. grub_errno = GRUB_ERR_NONE;
  193. continue;
  194. }
  195. }
  196. }
  197. return GRUB_ERR_NONE;
  198. }
  199. static grub_command_t cmd;
  200. #ifdef GRUB_MACHINE_PCBIOS
  201. static grub_command_t cmd_vbe;
  202. #endif
  203. GRUB_MOD_INIT(videoinfo)
  204. {
  205. cmd = grub_register_command ("videoinfo", grub_cmd_videoinfo,
  206. /* TRANSLATORS: "x" has to be entered in,
  207. like an identifier, so please don't
  208. use better Unicode codepoints. */
  209. N_("[WxH[xD]]"),
  210. N_("List available video modes. If "
  211. "resolution is given show only modes"
  212. " matching it."));
  213. #ifdef GRUB_MACHINE_PCBIOS
  214. cmd_vbe = grub_register_command ("vbeinfo", grub_cmd_videoinfo,
  215. /* TRANSLATORS: "x" has to be entered in,
  216. like an identifier, so please don't
  217. use better Unicode codepoints. */
  218. N_("[WxH[xD]]"),
  219. N_("List available video modes. If "
  220. "resolution is given show only modes"
  221. " matching it."));
  222. #endif
  223. }
  224. GRUB_MOD_FINI(videoinfo)
  225. {
  226. grub_unregister_command (cmd);
  227. #ifdef GRUB_MACHINE_PCBIOS
  228. grub_unregister_command (cmd_vbe);
  229. #endif
  230. }