You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

console_cmd.c 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. /*
  2. * Copyright (C) 2013 Michael Brown <mbrown@fensystems.co.uk>.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as
  6. * published by the Free Software Foundation; either version 2 of the
  7. * License, or any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  17. * 02110-1301, USA.
  18. */
  19. FILE_LICENCE ( GPL2_OR_LATER );
  20. /** @file
  21. *
  22. * Console management commands
  23. *
  24. */
  25. #include <string.h>
  26. #include <getopt.h>
  27. #include <ipxe/command.h>
  28. #include <ipxe/parseopt.h>
  29. #include <ipxe/console.h>
  30. #include <ipxe/image.h>
  31. #include <ipxe/pixbuf.h>
  32. #include <ipxe/ansicol.h>
  33. #include <usr/imgmgmt.h>
  34. /** "console" options */
  35. struct console_options {
  36. /** Console configuration */
  37. struct console_configuration config;
  38. /** Picture URI */
  39. char *picture;
  40. /** Keep picture after configuration */
  41. int keep;
  42. };
  43. /** "console" option list */
  44. static struct option_descriptor console_opts[] = {
  45. OPTION_DESC ( "x", 'x', required_argument,
  46. struct console_options, config.width, parse_integer ),
  47. OPTION_DESC ( "y", 'y', required_argument,
  48. struct console_options, config.height, parse_integer ),
  49. OPTION_DESC ( "bpp", 'b', required_argument,
  50. struct console_options, config.bpp, parse_integer ),
  51. OPTION_DESC ( "picture", 'p', required_argument,
  52. struct console_options, picture, parse_string ),
  53. OPTION_DESC ( "keep", 'k', no_argument,
  54. struct console_options, keep, parse_flag ),
  55. };
  56. /** "console" command descriptor */
  57. static struct command_descriptor console_cmd =
  58. COMMAND_DESC ( struct console_options, console_opts, 0, 0, NULL );
  59. /**
  60. * "console" command
  61. *
  62. * @v argc Argument count
  63. * @v argv Argument list
  64. * @ret rc Return status code
  65. */
  66. static int console_exec ( int argc, char **argv ) {
  67. struct console_options opts;
  68. struct image *image = NULL;
  69. int rc;
  70. /* Parse options */
  71. if ( ( rc = parse_options ( argc, argv, &console_cmd, &opts ) ) != 0 )
  72. goto err_parse;
  73. /* Handle background picture, if applicable */
  74. if ( opts.picture ) {
  75. /* Acquire image */
  76. if ( ( rc = imgacquire ( opts.picture, &image ) ) != 0 )
  77. goto err_acquire;
  78. /* Convert to pixel buffer */
  79. if ( ( rc = image_pixbuf ( image, &opts.config.pixbuf ) ) != 0){
  80. printf ( "Could not use picture: %s\n",
  81. strerror ( rc ) );
  82. goto err_pixbuf;
  83. }
  84. /* Apply image's width and height if none specified */
  85. if ( ! opts.config.width )
  86. opts.config.width = opts.config.pixbuf->width;
  87. if ( ! opts.config.height )
  88. opts.config.height = opts.config.pixbuf->height;
  89. }
  90. /* Configure console */
  91. if ( ( rc = console_configure ( &opts.config ) ) != 0 ) {
  92. printf ( "Could not configure console: %s\n", strerror ( rc ) );
  93. goto err_configure;
  94. }
  95. err_configure:
  96. pixbuf_put ( opts.config.pixbuf );
  97. err_pixbuf:
  98. /* Discard image unless --keep was specified */
  99. if ( image && ( ! opts.keep ) )
  100. unregister_image ( image );
  101. err_acquire:
  102. err_parse:
  103. return rc;
  104. }
  105. /** "colour" options */
  106. struct colour_options {
  107. /** Basic colour */
  108. unsigned int basic;
  109. /** 24-bit RGB value */
  110. unsigned int rgb;
  111. };
  112. /** "colour" option list */
  113. static struct option_descriptor colour_opts[] = {
  114. OPTION_DESC ( "basic", 'b', required_argument,
  115. struct colour_options, basic, parse_integer ),
  116. OPTION_DESC ( "rgb", 'r', required_argument,
  117. struct colour_options, rgb, parse_integer ),
  118. };
  119. /** "colour" command descriptor */
  120. static struct command_descriptor colour_cmd =
  121. COMMAND_DESC ( struct colour_options, colour_opts, 1, 1, "<colour>" );
  122. /**
  123. * "colour" command
  124. *
  125. * @v argc Argument count
  126. * @v argv Argument list
  127. * @ret rc Return status code
  128. */
  129. static int colour_exec ( int argc, char **argv ) {
  130. struct colour_options opts;
  131. unsigned int colour;
  132. int rc;
  133. /* Initialise options */
  134. memset ( &opts, 0, sizeof ( opts ) );
  135. opts.basic = COLOUR_DEFAULT;
  136. opts.rgb = ANSICOL_NO_RGB;
  137. /* Parse options */
  138. if ( ( rc = reparse_options ( argc, argv, &colour_cmd, &opts ) ) != 0 )
  139. return rc;
  140. /* Parse colour index */
  141. if ( ( rc = parse_integer ( argv[optind], &colour ) ) != 0 )
  142. return rc;
  143. /* Define colour */
  144. if ( ( rc = ansicol_define ( colour, opts.basic, opts.rgb ) ) != 0 ) {
  145. printf ( "Could not define colour: %s\n", strerror ( rc ) );
  146. return rc;
  147. }
  148. /* Reapply default colour pair, in case definition has changed */
  149. ansicol_set_pair ( CPAIR_DEFAULT );
  150. return 0;
  151. }
  152. /** "cpair" options */
  153. struct cpair_options {
  154. /** Foreground colour */
  155. unsigned int foreground;
  156. /** Background colour */
  157. unsigned int background;
  158. };
  159. /** "cpair" option list */
  160. static struct option_descriptor cpair_opts[] = {
  161. OPTION_DESC ( "foreground", 'f', required_argument,
  162. struct cpair_options, foreground, parse_integer ),
  163. OPTION_DESC ( "background", 'b', required_argument,
  164. struct cpair_options, background, parse_integer ),
  165. };
  166. /** "cpair" command descriptor */
  167. static struct command_descriptor cpair_cmd =
  168. COMMAND_DESC ( struct cpair_options, cpair_opts, 1, 1, "<cpair>" );
  169. /**
  170. * "cpair" command
  171. *
  172. * @v argc Argument count
  173. * @v argv Argument list
  174. * @ret rc Return status code
  175. */
  176. static int cpair_exec ( int argc, char **argv ) {
  177. struct cpair_options opts;
  178. unsigned int cpair;
  179. int rc;
  180. /* Initialise options */
  181. memset ( &opts, 0, sizeof ( opts ) );
  182. opts.foreground = COLOUR_DEFAULT;
  183. opts.background = COLOUR_DEFAULT;
  184. /* Parse options */
  185. if ( ( rc = reparse_options ( argc, argv, &cpair_cmd, &opts ) ) != 0 )
  186. return rc;
  187. /* Parse colour pair index */
  188. if ( ( rc = parse_integer ( argv[optind], &cpair ) ) != 0 )
  189. return rc;
  190. /* Define colour pair */
  191. if ( ( rc = ansicol_define_pair ( cpair, opts.foreground,
  192. opts.background ) ) != 0 ) {
  193. printf ( "Could not define colour pair: %s\n",
  194. strerror ( rc ) );
  195. return rc;
  196. }
  197. /* Reapply default colour pair, in case definition has changed */
  198. ansicol_set_pair ( CPAIR_DEFAULT );
  199. return 0;
  200. }
  201. /** Console management commands */
  202. struct command console_commands[] __command = {
  203. {
  204. .name = "console",
  205. .exec = console_exec,
  206. },
  207. {
  208. .name = "colour",
  209. .exec = colour_exec,
  210. },
  211. {
  212. .name = "cpair",
  213. .exec = cpair_exec,
  214. },
  215. };