iPXE
console_cmd.c
Go to the documentation of this file.
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  * You can also choose to distribute this program under the terms of
20  * the Unmodified Binary Distribution Licence (as given in the file
21  * COPYING.UBDL), provided that you have satisfied its requirements.
22  */
23 
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
25 FILE_SECBOOT ( PERMITTED );
26 
27 /** @file
28  *
29  * Console management commands
30  *
31  */
32 
33 #include <string.h>
34 #include <getopt.h>
35 #include <ipxe/command.h>
36 #include <ipxe/parseopt.h>
37 #include <ipxe/console.h>
38 #include <ipxe/image.h>
39 #include <ipxe/pixbuf.h>
40 #include <ipxe/ansiesc.h>
41 #include <ipxe/ansicol.h>
42 #include <usr/imgmgmt.h>
43 
44 /** "console" options */
46  /** Console configuration */
48  /** Picture URI */
49  char *picture;
50  /** Keep picture after configuration */
51  int keep;
52 };
53 
54 /** "console" option list */
55 static struct option_descriptor console_opts[] = {
56  OPTION_DESC ( "x", 'x', required_argument,
57  struct console_options, config.width, parse_integer ),
58  OPTION_DESC ( "y", 'y', required_argument,
59  struct console_options, config.height, parse_integer ),
60  OPTION_DESC ( "left", 'l', required_argument,
61  struct console_options, config.left, parse_integer ),
62  OPTION_DESC ( "right", 'r', required_argument,
63  struct console_options, config.right, parse_integer ),
64  OPTION_DESC ( "top", 't', required_argument,
65  struct console_options, config.top, parse_integer ),
66  OPTION_DESC ( "bottom", 'b', required_argument,
67  struct console_options, config.bottom, parse_integer ),
68  OPTION_DESC ( "depth", 'd', required_argument,
69  struct console_options, config.depth, parse_integer ),
70  OPTION_DESC ( "picture", 'p', required_argument,
71  struct console_options, picture, parse_string ),
72  OPTION_DESC ( "keep", 'k', no_argument,
73  struct console_options, keep, parse_flag ),
74 };
75 
76 /** "console" command descriptor */
78  COMMAND_DESC ( struct console_options, console_opts, 0, 0, NULL );
79 
80 /**
81  * "console" command
82  *
83  * @v argc Argument count
84  * @v argv Argument list
85  * @ret rc Return status code
86  */
87 static int console_exec ( int argc, char **argv ) {
88  struct console_options opts;
89  struct image *image = NULL;
90  int rc;
91 
92  /* Parse options */
93  if ( ( rc = parse_options ( argc, argv, &console_cmd, &opts ) ) != 0 )
94  goto err_parse;
95 
96  /* Handle background picture, if applicable */
97  if ( opts.picture ) {
98 
99  /* Acquire image */
100  if ( ( rc = imgacquire ( opts.picture, 0, &image ) ) != 0 )
101  goto err_acquire;
102 
103  /* Convert to pixel buffer */
104  if ( ( rc = image_pixbuf ( image, &opts.config.pixbuf ) ) != 0){
105  printf ( "Could not use picture: %s\n",
106  strerror ( rc ) );
107  goto err_pixbuf;
108  }
109 
110  /* Apply image's width and height if none specified */
111  if ( ! opts.config.width )
112  opts.config.width = opts.config.pixbuf->width;
113  if ( ! opts.config.height )
114  opts.config.height = opts.config.pixbuf->height;
115  }
116 
117  /* Configure console */
118  if ( ( rc = console_configure ( &opts.config ) ) != 0 ) {
119  printf ( "Could not configure console: %s\n", strerror ( rc ) );
120  goto err_configure;
121  }
122 
123  /* Reapply default colour pair and clear screen */
125  printf ( CSI "2J" CSI "H" );
126 
127  err_configure:
128  pixbuf_put ( opts.config.pixbuf );
129  err_pixbuf:
130  /* Discard image unless --keep was specified */
131  if ( image && ( ! opts.keep ) )
133  err_acquire:
134  err_parse:
135  return rc;
136 }
137 
138 /** "colour" options */
140  /** Basic colour */
141  unsigned int basic;
142  /** 24-bit RGB value */
143  unsigned int rgb;
144 };
145 
146 /** "colour" option list */
147 static struct option_descriptor colour_opts[] = {
148  OPTION_DESC ( "basic", 'b', required_argument,
149  struct colour_options, basic, parse_integer ),
150  OPTION_DESC ( "rgb", 'r', required_argument,
151  struct colour_options, rgb, parse_integer ),
152 };
153 
154 /** "colour" command descriptor */
156  COMMAND_DESC ( struct colour_options, colour_opts, 1, 1, "<colour>" );
157 
158 /**
159  * "colour" command
160  *
161  * @v argc Argument count
162  * @v argv Argument list
163  * @ret rc Return status code
164  */
165 static int colour_exec ( int argc, char **argv ) {
166  struct colour_options opts;
167  unsigned int colour;
168  int rc;
169 
170  /* Initialise options */
171  memset ( &opts, 0, sizeof ( opts ) );
172  opts.basic = COLOUR_DEFAULT;
173  opts.rgb = ANSICOL_NO_RGB;
174 
175  /* Parse options */
176  if ( ( rc = reparse_options ( argc, argv, &colour_cmd, &opts ) ) != 0 )
177  return rc;
178 
179  /* Parse colour index */
180  if ( ( rc = parse_integer ( argv[optind], &colour ) ) != 0 )
181  return rc;
182 
183  /* Define colour */
184  if ( ( rc = ansicol_define ( colour, opts.basic, opts.rgb ) ) != 0 ) {
185  printf ( "Could not define colour: %s\n", strerror ( rc ) );
186  return rc;
187  }
188 
189  /* Reapply default colour pair, in case definition has changed */
191 
192  return 0;
193 }
194 
195 /** "cpair" options */
197  /** Foreground colour */
198  unsigned int foreground;
199  /** Background colour */
200  unsigned int background;
201 };
202 
203 /** "cpair" option list */
204 static struct option_descriptor cpair_opts[] = {
205  OPTION_DESC ( "foreground", 'f', required_argument,
206  struct cpair_options, foreground, parse_integer ),
207  OPTION_DESC ( "background", 'b', required_argument,
208  struct cpair_options, background, parse_integer ),
209 };
210 
211 /** "cpair" command descriptor */
213  COMMAND_DESC ( struct cpair_options, cpair_opts, 1, 1, "<cpair>" );
214 
215 /**
216  * "cpair" command
217  *
218  * @v argc Argument count
219  * @v argv Argument list
220  * @ret rc Return status code
221  */
222 static int cpair_exec ( int argc, char **argv ) {
223  struct cpair_options opts;
224  unsigned int cpair;
225  int rc;
226 
227  /* Initialise options */
228  memset ( &opts, 0, sizeof ( opts ) );
229  opts.foreground = COLOUR_DEFAULT;
230  opts.background = COLOUR_DEFAULT;
231 
232  /* Parse options */
233  if ( ( rc = reparse_options ( argc, argv, &cpair_cmd, &opts ) ) != 0 )
234  return rc;
235 
236  /* Parse colour pair index */
237  if ( ( rc = parse_integer ( argv[optind], &cpair ) ) != 0 )
238  return rc;
239 
240  /* Define colour pair */
241  if ( ( rc = ansicol_define_pair ( cpair, opts.foreground,
242  opts.background ) ) != 0 ) {
243  printf ( "Could not define colour pair: %s\n",
244  strerror ( rc ) );
245  return rc;
246  }
247 
248  /* Reapply default colour pair, in case definition has changed */
250 
251  return 0;
252 }
253 
254 /** Console management commands */
255 COMMAND ( console, console_exec );
257 COMMAND ( cpair, cpair_exec );
int image_pixbuf(struct image *image, struct pixel_buffer **pixbuf)
Create pixel buffer from image.
Definition: pixbuf.c:100
int parse_integer(char *text, unsigned int *value)
Parse integer value.
Definition: parseopt.c:92
unsigned int rgb
24-bit RGB value
Definition: console_cmd.c:143
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
int printf(const char *fmt,...)
Write a formatted string to the console.
Definition: vsprintf.c:465
int optind
Current option index.
Definition: getopt.c:52
unsigned int basic
Basic colour.
Definition: console_cmd.c:141
unsigned int background
Background colour.
Definition: console_cmd.c:200
char * picture
Picture URI.
Definition: console_cmd.c:49
int keep
Keep picture after configuration.
Definition: console_cmd.c:51
static int console_exec(int argc, char **argv)
"console" command
Definition: console_cmd.c:87
COMMAND(console, console_exec)
Console management commands.
int parse_options(int argc, char **argv, struct command_descriptor *cmd, void *opts)
Parse command-line options.
Definition: parseopt.c:485
An executable image.
Definition: image.h:24
Pixel buffer.
A command descriptor.
Definition: parseopt.h:78
static int cpair_exec(int argc, char **argv)
"cpair" command
Definition: console_cmd.c:222
A console configuration.
Definition: console.h:25
static struct option_descriptor console_opts[]
"console" option list
Definition: console_cmd.c:55
void ansicol_set_pair(unsigned int cpair)
Set ANSI foreground and background colour.
Definition: ansicol.c:90
static struct command_descriptor colour_cmd
"colour" command descriptor
Definition: console_cmd.c:155
#define CSI
Control Sequence Introducer.
Definition: ansiesc.h:96
"console" options
Definition: console_cmd.c:45
Parse command-line options.
#define colour
Colour for debug messages.
Definition: acpi.c:42
int parse_string(char *text, char **value)
Parse string value.
Definition: parseopt.c:74
Executable images.
struct console_configuration config
Console configuration.
Definition: console_cmd.c:47
int ansicol_define(unsigned int colour, unsigned int basic, uint32_t rgb)
Define ANSI colour.
Definition: ansicoldef.c:126
int ansicol_define_pair(unsigned int cpair, unsigned int foreground, unsigned int background)
Define ANSI colour pair.
Definition: ansicol.c:111
static struct command_descriptor console_cmd
"console" command descriptor
Definition: console_cmd.c:77
ANSI escape sequences.
static struct option_descriptor cpair_opts[]
"cpair" option list
Definition: console_cmd.c:204
FILE_SECBOOT(PERMITTED)
int reparse_options(int argc, char **argv, struct command_descriptor *cmd, void *opts)
Reparse command-line options.
Definition: parseopt.c:402
User interaction.
#define COLOUR_DEFAULT
Default colour (usually white foreground, black background)
Definition: ansicol.h:17
int parse_flag(char *text __unused, int *flag)
Parse flag.
Definition: parseopt.c:227
char * strerror(int errno)
Retrieve string representation of error number.
Definition: strerror.c:79
FILE_LICENCE(GPL2_OR_LATER_OR_UBDL)
Command line option parsing.
Option does not take an argument.
Definition: getopt.h:17
static struct option_descriptor colour_opts[]
"colour" option list
Definition: console_cmd.c:147
int console_configure(struct console_configuration *config)
Configure console.
Definition: console.c:147
#define ANSICOL_NO_RGB
RGB value for "not defined".
Definition: ansicol.h:30
unsigned int foreground
Foreground colour.
Definition: console_cmd.c:198
#define CPAIR_DEFAULT
Default colour pair.
Definition: ansicol.h:38
void unregister_image(struct image *image)
Unregister executable image.
Definition: image.c:358
Image management.
#define OPTION_DESC(_longopt, _shortopt, _has_arg, _struct, _field, _parse)
Construct option descriptor.
Definition: parseopt.h:68
Option requires an argument.
Definition: getopt.h:19
A command-line option descriptor.
Definition: parseopt.h:24
static int colour_exec(int argc, char **argv)
"colour" command
Definition: console_cmd.c:165
static union @447 opts
"cert<xxx>" option list
#define COMMAND_DESC(_struct, _options, _min_args, _max_args, _usage)
Construct command descriptor.
Definition: parseopt.h:109
static struct command_descriptor cpair_cmd
"cpair" command descriptor
Definition: console_cmd.c:212
"colour" options
Definition: console_cmd.c:139
"cpair" options
Definition: console_cmd.c:196
#define NULL
NULL pointer (VOID *)
Definition: Base.h:322
String functions.
ANSI colours.
int imgacquire(const char *name_uri, unsigned long timeout, struct image **image)
Acquire an image.
Definition: imgmgmt.c:143
void * memset(void *dest, int character, size_t len) __nonnull