]> git.ipfire.org Git - thirdparty/ipxe.git/commitdiff
[menu] Add menu commands
authorMichael Brown <mcb30@ipxe.org>
Wed, 28 Mar 2012 23:05:22 +0000 (00:05 +0100)
committerMichael Brown <mcb30@ipxe.org>
Thu, 29 Mar 2012 13:02:02 +0000 (14:02 +0100)
Allow iPXE scripts to create menus.  For example:

    #!ipxe

    menu iSCSI boot demonstration
    item install         Install Fedora to ${root-path}
    item --default boot  Boot from ${root-path}
    item shell           Enter iPXE shell
    item exit            Exit to BIOS
    choose label && goto ${label}

    :boot
    sanboot ${root-path}

    :install
    sanhook ${root-path}
    chain http://${next-server}/fedora.ipxe

    :shell
    shell

    :exit

Inspired-by: Robin Smidsrød <robin@smidsrod.no>
Tested-by: Robin Smidsrød <robin@smidsrod.no>
Signed-off-by: Michael Brown <mcb30@ipxe.org>
src/config/config.c
src/config/general.h
src/core/parseopt.c
src/hci/commands/menu_cmd.c [new file with mode: 0644]
src/include/ipxe/errfile.h
src/include/ipxe/features.h
src/include/ipxe/parseopt.h

index 4b0bc3710014c8c0aa0b0d0e572f36ba0f3bac87..bc3f7ed5d760b7c1e8653ba5e9b066ea7e291a4b 100644 (file)
@@ -223,6 +223,9 @@ REQUIRE_OBJECT ( dhcp_cmd );
 #ifdef SANBOOT_CMD
 REQUIRE_OBJECT ( sanboot_cmd );
 #endif
+#ifdef MENU_CMD
+REQUIRE_OBJECT ( menu_cmd );
+#endif
 #ifdef LOGIN_CMD
 REQUIRE_OBJECT ( login_cmd );
 #endif
index a10696ee79d5b3010140a36fc32bd74b3d0837c0..70742165c56e9b182f817c73f9b2df92520e193b 100644 (file)
@@ -119,6 +119,7 @@ FILE_LICENCE ( GPL2_OR_LATER );
 #define IMAGE_CMD              /* Image management commands */
 #define DHCP_CMD               /* DHCP management commands */
 #define SANBOOT_CMD            /* SAN boot commands */
+#define MENU_CMD               /* Menu commands */
 #define LOGIN_CMD              /* Login command */
 //#define TIME_CMD             /* Time commands */
 //#define DIGEST_CMD           /* Image crypto digest commands */
index 57140690be5d308edebae5bc784bcdac76c33a6f..432e856d08c95e5af3a1cd2740dbec90f20fc335 100644 (file)
@@ -26,6 +26,7 @@ FILE_LICENCE ( GPL2_OR_LATER );
 #include <errno.h>
 #include <getopt.h>
 #include <ipxe/netdevice.h>
+#include <ipxe/menu.h>
 #include <ipxe/parseopt.h>
 
 /** @file
@@ -113,6 +114,29 @@ int parse_netdev ( const char *text, struct net_device **netdev ) {
        return 0;
 }
 
+/**
+ * Parse menu name
+ *
+ * @v text             Text
+ * @ret menu           Menu
+ * @ret rc             Return status code
+ */
+int parse_menu ( const char *text, struct menu **menu ) {
+
+       /* Find menu */
+       *menu = find_menu ( text );
+       if ( ! *menu ) {
+               if ( text ) {
+                       printf ( "\"%s\": no such menu\n", text );
+               } else {
+                       printf ( "No default menu\n" );
+               }
+               return -ENOENT;
+       }
+
+       return 0;
+}
+
 /**
  * Parse flag
  *
diff --git a/src/hci/commands/menu_cmd.c b/src/hci/commands/menu_cmd.c
new file mode 100644 (file)
index 0000000..8dac3cb
--- /dev/null
@@ -0,0 +1,280 @@
+/*
+ * Copyright (C) 2012 Michael Brown <mbrown@fensystems.co.uk>.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2 of the
+ * License, or any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ */
+
+FILE_LICENCE ( GPL2_OR_LATER );
+
+/** @file
+ *
+ * Menu commands
+ *
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <errno.h>
+#include <getopt.h>
+#include <ipxe/menu.h>
+#include <ipxe/command.h>
+#include <ipxe/parseopt.h>
+#include <ipxe/settings.h>
+#include <ipxe/features.h>
+
+FEATURE ( FEATURE_MISC, "Menu", DHCP_EB_FEATURE_MENU, 1 );
+
+/** "menu" options */
+struct menu_options {
+       /** Name */
+       const char *name;
+       /** Delete */
+       int delete;
+};
+
+/** "menu" option list */
+static struct option_descriptor menu_opts[] = {
+       OPTION_DESC ( "name", 'n', required_argument,
+                     struct menu_options, name, parse_string ),
+       OPTION_DESC ( "delete", 'd', no_argument,
+                     struct menu_options, delete, parse_flag ),
+};
+
+/** "menu" command descriptor */
+static struct command_descriptor menu_cmd =
+       COMMAND_DESC ( struct menu_options, menu_opts, 0, MAX_ARGUMENTS,
+                      "[--name <name>] [--delete] [<title>]" );
+
+/**
+ * The "menu" command
+ *
+ * @v argc             Argument count
+ * @v argv             Argument list
+ * @ret rc             Return status code
+ */
+static int menu_exec ( int argc, char **argv ) {
+       struct menu_options opts;
+       struct menu *menu;
+       char *title;
+       int rc;
+
+       /* Parse options */
+       if ( ( rc = parse_options ( argc, argv, &menu_cmd, &opts ) ) != 0 )
+               goto err_parse_options;
+
+       /* Parse title */
+       title = concat_args ( &argv[optind] );
+       if ( ! title ) {
+               rc = -ENOMEM;
+               goto err_parse_title;
+       }
+
+       /* Create menu */
+       menu = create_menu ( opts.name, title );
+       if ( ! menu ) {
+               rc = -ENOMEM;
+               goto err_create_menu;
+       }
+
+       /* Destroy menu, if applicable */
+       if ( opts.delete )
+               destroy_menu ( menu );
+
+       /* Success */
+       rc = 0;
+
+ err_create_menu:
+       free ( title );
+ err_parse_title:
+ err_parse_options:
+       return rc;
+}
+
+/** "item" options */
+struct item_options {
+       /** Menu name */
+       const char *menu;
+       /** Shortcut key */
+       unsigned int key;
+       /** Use as default */
+       int is_default;
+       /** Use as a separator */
+       int is_gap;
+};
+
+/** "item" option list */
+static struct option_descriptor item_opts[] = {
+       OPTION_DESC ( "menu", 'm', required_argument,
+                     struct item_options, menu, parse_string ),
+       OPTION_DESC ( "key", 'k', required_argument,
+                     struct item_options, key, parse_key ),
+       OPTION_DESC ( "default", 'd', no_argument,
+                     struct item_options, is_default, parse_flag ),
+       OPTION_DESC ( "gap", 'g', no_argument,
+                     struct item_options, is_gap, parse_flag ),
+};
+
+/** "item" command descriptor */
+static struct command_descriptor item_cmd =
+       COMMAND_DESC ( struct item_options, item_opts, 0, MAX_ARGUMENTS,
+                      "[--menu <menu>] [--key <key>] [--default] "
+                      "[<label>|--gap [<text>]]" );
+
+/**
+ * The "item" command
+ *
+ * @v argc             Argument count
+ * @v argv             Argument list
+ * @ret rc             Return status code
+ */
+static int item_exec ( int argc, char **argv ) {
+       struct item_options opts;
+       struct menu *menu;
+       struct menu_item *item;
+       char *label = NULL;
+       char *text = NULL;
+       int rc;
+
+       /* Parse options */
+       if ( ( rc = parse_options ( argc, argv, &item_cmd, &opts ) ) != 0 )
+               goto err_parse_options;
+
+       /* Parse label, if present */
+       if ( ! opts.is_gap )
+               label = argv[optind++]; /* May be NULL */
+
+       /* Parse text, if present */
+       if ( optind < argc ) {
+               text = concat_args ( &argv[optind] );
+               if ( ! text ) {
+                       rc = -ENOMEM;
+                       goto err_parse_text;
+               }
+       }
+
+       /* Identify menu */
+       if ( ( rc = parse_menu ( opts.menu, &menu ) ) != 0 )
+               goto err_parse_menu;
+
+       /* Add menu item */
+       item = add_menu_item ( menu, label, ( text ? text : "" ),
+                              opts.key, opts.is_default );
+       if ( ! item ) {
+               rc = -ENOMEM;
+               goto err_add_menu_item;
+       }
+
+       /* Success */
+       rc = 0;
+
+ err_add_menu_item:
+ err_parse_menu:
+       free ( text );
+ err_parse_text:
+ err_parse_options:
+       return rc;
+}
+
+/** "choose" options */
+struct choose_options {
+       /** Menu name */
+       const char *menu;
+       /** Timeout */
+       unsigned int timeout;
+       /** Keep menu */
+       int keep;
+};
+
+/** "choose" option list */
+static struct option_descriptor choose_opts[] = {
+       OPTION_DESC ( "menu", 'm', required_argument,
+                     struct choose_options, menu, parse_string ),
+       OPTION_DESC ( "timeout", 't', required_argument,
+                     struct choose_options, timeout, parse_integer ),
+       OPTION_DESC ( "keep", 'k', no_argument,
+                     struct choose_options, keep, parse_flag ),
+};
+
+/** "choose" command descriptor */
+static struct command_descriptor choose_cmd =
+       COMMAND_DESC ( struct choose_options, choose_opts, 1, 1,
+                      "[--menu <menu>] [--timeout <timeout>] [--keep] "
+                      "<setting>" );
+
+/**
+ * The "choose" command
+ *
+ * @v argc             Argument count
+ * @v argv             Argument list
+ * @ret rc             Return status code
+ */
+static int choose_exec ( int argc, char **argv ) {
+       struct choose_options opts;
+       struct menu *menu;
+       struct menu_item *item;
+       const char *setting;
+       int rc;
+
+       /* Parse options */
+       if ( ( rc = parse_options ( argc, argv, &choose_cmd, &opts ) ) != 0 )
+               goto err_parse_options;
+
+       /* Parse setting name */
+       setting = argv[optind];
+
+       /* Identify menu */
+       if ( ( rc = parse_menu ( opts.menu, &menu ) ) != 0 )
+               goto err_parse_menu;
+
+       /* Show menu */
+       if ( ( rc = show_menu ( menu, opts.timeout, &item ) ) != 0 )
+               goto err_show_menu;
+
+       /* Store setting */
+       if ( ( rc = storef_named_setting ( setting, item->label ) ) != 0 ) {
+               printf ( "Could not store \"%s\": %s\n",
+                        setting, strerror ( rc ) );
+               goto err_store;
+       }
+
+       /* Success */
+       rc = 0;
+
+ err_store:
+ err_show_menu:
+       /* Destroy menu, if applicable */
+       if ( ! opts.keep )
+               destroy_menu ( menu );
+ err_parse_menu:
+ err_parse_options:
+       return rc;
+}
+
+/** Menu commands */
+struct command menu_commands[] __command = {
+       {
+               .name = "menu",
+               .exec = menu_exec,
+       },
+       {
+               .name = "item",
+               .exec = item_exec,
+       },
+       {
+               .name = "choose",
+               .exec = choose_exec,
+       },
+};
index c72aeb7b87542a944fc619cc5a1689baa4cf1726..77c2f8095371ec2a514d61c42fd5326a83356722 100644 (file)
@@ -252,6 +252,7 @@ FILE_LICENCE ( GPL2_OR_LATER );
 #define ERRFILE_cms                  ( ERRFILE_OTHER | 0x002a0000 )
 #define ERRFILE_imgtrust             ( ERRFILE_OTHER | 0x002b0000 )
 #define ERRFILE_menu_ui                      ( ERRFILE_OTHER | 0x002c0000 )
+#define ERRFILE_menu_cmd             ( ERRFILE_OTHER | 0x002d0000 )
 
 /** @} */
 
index 660015cd2a97780092b3c8622e54a33c408e450b..498ec944121b02f1689d335cd57f12ca8c622d14 100644 (file)
@@ -52,6 +52,7 @@ FILE_LICENCE ( GPL2_OR_LATER );
 #define DHCP_EB_FEATURE_EFI            0x24 /**< EFI format */
 #define DHCP_EB_FEATURE_FCOE           0x25 /**< FCoE protocol */
 #define DHCP_EB_FEATURE_VLAN           0x26 /**< VLAN support */
+#define DHCP_EB_FEATURE_MENU           0x27 /**< Menu support */
 
 /** @} */
 
index b5fd2203b2b43644be38cda792de67e99600dda9..b492a51e555fc447e2d527685442eddbd014e2e0 100644 (file)
@@ -13,6 +13,7 @@ FILE_LICENCE ( GPL2_OR_LATER );
 #include <stddef.h>
 
 struct net_device;
+struct menu;
 
 /** A command-line option descriptor */
 struct option_descriptor {
@@ -116,6 +117,7 @@ struct command_descriptor {
 extern int parse_string ( const char *text, const char **value );
 extern int parse_integer ( const char *text, unsigned int *value );
 extern int parse_netdev ( const char *text, struct net_device **netdev );
+extern int parse_menu ( const char *text, struct menu **menu );
 extern int parse_flag ( const char *text __unused, int *flag );
 extern int parse_key ( const char *text, unsigned int *key );
 extern void print_usage ( struct command_descriptor *cmd, char **argv );