iPXE
console_cmd.c
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2013 Michael Brown <mbrown@fensystems.co.uk>.
00003  *
00004  * This program is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU General Public License as
00006  * published by the Free Software Foundation; either version 2 of the
00007  * License, or any later version.
00008  *
00009  * This program is distributed in the hope that it will be useful, but
00010  * WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  * General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software
00016  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
00017  * 02110-1301, USA.
00018  *
00019  * You can also choose to distribute this program under the terms of
00020  * the Unmodified Binary Distribution Licence (as given in the file
00021  * COPYING.UBDL), provided that you have satisfied its requirements.
00022  */
00023 
00024 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
00025 
00026 /** @file
00027  *
00028  * Console management commands
00029  *
00030  */
00031 
00032 #include <string.h>
00033 #include <getopt.h>
00034 #include <ipxe/command.h>
00035 #include <ipxe/parseopt.h>
00036 #include <ipxe/console.h>
00037 #include <ipxe/image.h>
00038 #include <ipxe/pixbuf.h>
00039 #include <ipxe/ansiesc.h>
00040 #include <ipxe/ansicol.h>
00041 #include <usr/imgmgmt.h>
00042 
00043 /** "console" options */
00044 struct console_options {
00045         /** Console configuration */
00046         struct console_configuration config;
00047         /** Picture URI */
00048         char *picture;
00049         /** Keep picture after configuration */
00050         int keep;
00051 };
00052 
00053 /** "console" option list */
00054 static struct option_descriptor console_opts[] = {
00055         OPTION_DESC ( "x", 'x', required_argument,
00056                       struct console_options, config.width, parse_integer ),
00057         OPTION_DESC ( "y", 'y', required_argument,
00058                       struct console_options, config.height, parse_integer ),
00059         OPTION_DESC ( "left", 'l', required_argument,
00060                       struct console_options, config.left, parse_integer ),
00061         OPTION_DESC ( "right", 'r', required_argument,
00062                       struct console_options, config.right, parse_integer ),
00063         OPTION_DESC ( "top", 't', required_argument,
00064                       struct console_options, config.top, parse_integer ),
00065         OPTION_DESC ( "bottom", 'b', required_argument,
00066                       struct console_options, config.bottom, parse_integer ),
00067         OPTION_DESC ( "depth", 'd', required_argument,
00068                       struct console_options, config.depth, parse_integer ),
00069         OPTION_DESC ( "picture", 'p', required_argument,
00070                       struct console_options, picture, parse_string ),
00071         OPTION_DESC ( "keep", 'k', no_argument,
00072                       struct console_options, keep, parse_flag ),
00073 };
00074 
00075 /** "console" command descriptor */
00076 static struct command_descriptor console_cmd =
00077         COMMAND_DESC ( struct console_options, console_opts, 0, 0, NULL );
00078 
00079 /**
00080  * "console" command
00081  *
00082  * @v argc              Argument count
00083  * @v argv              Argument list
00084  * @ret rc              Return status code
00085  */
00086 static int console_exec ( int argc, char **argv ) {
00087         struct console_options opts;
00088         struct image *image = NULL;
00089         int rc;
00090 
00091         /* Parse options */
00092         if ( ( rc = parse_options ( argc, argv, &console_cmd, &opts ) ) != 0 )
00093                 goto err_parse;
00094 
00095         /* Handle background picture, if applicable */
00096         if ( opts.picture ) {
00097 
00098                 /* Acquire image */
00099                 if ( ( rc = imgacquire ( opts.picture, 0, &image ) ) != 0 )
00100                         goto err_acquire;
00101 
00102                 /* Convert to pixel buffer */
00103                 if ( ( rc = image_pixbuf ( image, &opts.config.pixbuf ) ) != 0){
00104                         printf ( "Could not use picture: %s\n",
00105                                  strerror ( rc ) );
00106                         goto err_pixbuf;
00107                 }
00108 
00109                 /* Apply image's width and height if none specified */
00110                 if ( ! opts.config.width )
00111                         opts.config.width = opts.config.pixbuf->width;
00112                 if ( ! opts.config.height )
00113                         opts.config.height = opts.config.pixbuf->height;
00114         }
00115 
00116         /* Configure console */
00117         if ( ( rc = console_configure ( &opts.config ) ) != 0 ) {
00118                 printf ( "Could not configure console: %s\n", strerror ( rc ) );
00119                 goto err_configure;
00120         }
00121 
00122         /* Reapply default colour pair and clear screen */
00123         ansicol_set_pair ( CPAIR_DEFAULT );
00124         printf ( CSI "2J" CSI "H" );
00125 
00126  err_configure:
00127         pixbuf_put ( opts.config.pixbuf );
00128  err_pixbuf:
00129         /* Discard image unless --keep was specified */
00130         if ( image && ( ! opts.keep ) )
00131                 unregister_image ( image );
00132  err_acquire:
00133  err_parse:
00134         return rc;
00135 }
00136 
00137 /** "colour" options */
00138 struct colour_options {
00139         /** Basic colour */
00140         unsigned int basic;
00141         /** 24-bit RGB value */
00142         unsigned int rgb;
00143 };
00144 
00145 /** "colour" option list */
00146 static struct option_descriptor colour_opts[] = {
00147         OPTION_DESC ( "basic", 'b', required_argument,
00148                       struct colour_options, basic, parse_integer ),
00149         OPTION_DESC ( "rgb", 'r', required_argument,
00150                       struct colour_options, rgb, parse_integer ),
00151 };
00152 
00153 /** "colour" command descriptor */
00154 static struct command_descriptor colour_cmd =
00155         COMMAND_DESC ( struct colour_options, colour_opts, 1, 1, "<colour>" );
00156 
00157 /**
00158  * "colour" command
00159  *
00160  * @v argc              Argument count
00161  * @v argv              Argument list
00162  * @ret rc              Return status code
00163  */
00164 static int colour_exec ( int argc, char **argv ) {
00165         struct colour_options opts;
00166         unsigned int colour;
00167         int rc;
00168 
00169         /* Initialise options */
00170         memset ( &opts, 0, sizeof ( opts ) );
00171         opts.basic = COLOUR_DEFAULT;
00172         opts.rgb = ANSICOL_NO_RGB;
00173 
00174         /* Parse options */
00175         if ( ( rc = reparse_options ( argc, argv, &colour_cmd, &opts ) ) != 0 )
00176                 return rc;
00177 
00178         /* Parse colour index */
00179         if ( ( rc = parse_integer ( argv[optind], &colour ) ) != 0 )
00180                 return rc;
00181 
00182         /* Define colour */
00183         if ( ( rc = ansicol_define ( colour, opts.basic, opts.rgb ) ) != 0 ) {
00184                 printf ( "Could not define colour: %s\n", strerror ( rc ) );
00185                 return rc;
00186         }
00187 
00188         /* Reapply default colour pair, in case definition has changed */
00189         ansicol_set_pair ( CPAIR_DEFAULT );
00190 
00191         return 0;
00192 }
00193 
00194 /** "cpair" options */
00195 struct cpair_options {
00196         /** Foreground colour */
00197         unsigned int foreground;
00198         /** Background colour */
00199         unsigned int background;
00200 };
00201 
00202 /** "cpair" option list */
00203 static struct option_descriptor cpair_opts[] = {
00204         OPTION_DESC ( "foreground", 'f', required_argument,
00205                       struct cpair_options, foreground, parse_integer ),
00206         OPTION_DESC ( "background", 'b', required_argument,
00207                       struct cpair_options, background, parse_integer ),
00208 };
00209 
00210 /** "cpair" command descriptor */
00211 static struct command_descriptor cpair_cmd =
00212         COMMAND_DESC ( struct cpair_options, cpair_opts, 1, 1, "<cpair>" );
00213 
00214 /**
00215  * "cpair" command
00216  *
00217  * @v argc              Argument count
00218  * @v argv              Argument list
00219  * @ret rc              Return status code
00220  */
00221 static int cpair_exec ( int argc, char **argv ) {
00222         struct cpair_options opts;
00223         unsigned int cpair;
00224         int rc;
00225 
00226         /* Initialise options */
00227         memset ( &opts, 0, sizeof ( opts ) );
00228         opts.foreground = COLOUR_DEFAULT;
00229         opts.background = COLOUR_DEFAULT;
00230 
00231         /* Parse options */
00232         if ( ( rc = reparse_options ( argc, argv, &cpair_cmd, &opts ) ) != 0 )
00233                 return rc;
00234 
00235         /* Parse colour pair index */
00236         if ( ( rc = parse_integer ( argv[optind], &cpair ) ) != 0 )
00237                 return rc;
00238 
00239         /* Define colour pair */
00240         if ( ( rc = ansicol_define_pair ( cpair, opts.foreground,
00241                                           opts.background ) ) != 0 ) {
00242                 printf ( "Could not define colour pair: %s\n",
00243                          strerror ( rc ) );
00244                 return rc;
00245         }
00246 
00247         /* Reapply default colour pair, in case definition has changed */
00248         ansicol_set_pair ( CPAIR_DEFAULT );
00249 
00250         return 0;
00251 }
00252 
00253 /** Console management commands */
00254 struct command console_commands[] __command = {
00255         {
00256                 .name = "console",
00257                 .exec = console_exec,
00258         },
00259         {
00260                 .name = "colour",
00261                 .exec = colour_exec,
00262         },
00263         {
00264                 .name = "cpair",
00265                 .exec = cpair_exec,
00266         },
00267 };