iPXE
Functions | Variables
linux_args.c File Reference
#include <hci/linux_args.h>
#include <getopt.h>
#include <string.h>
#include <stdio.h>
#include <ipxe/settings.h>
#include <ipxe/linux.h>
#include <ipxe/malloc.h>
#include <ipxe/init.h>

Go to the source code of this file.

Functions

 FILE_LICENCE (GPL2_OR_LATER)
__asmcall void save_args (int argc, char **argv)
 Save argc and argv for later access.
static int parse_kv (char *kv, struct list_head *list)
 Parse k1=v1[,k2=v2]* into linux_settings.
static int parse_net_args (char *args)
 Parse --net arguments.
static int parse_settings_args (char *args)
 Parse --settings arguments.
void linux_args_parse ()
 Parse passed command-line arguments.
void linux_args_cleanup (int flags __unused)
 Clean up requests and settings.
struct startup_fn
startup_linux_args 
__startup_fn (STARTUP_EARLY)

Variables

static int saved_argc = 0
 Saved argc.
static char ** saved_argv
 Saved argv.
static struct option options []
 Supported command-line options.

Function Documentation

FILE_LICENCE ( GPL2_OR_LATER  )
__asmcall void save_args ( int  argc,
char **  argv 
)

Save argc and argv for later access.

To be called by linuxprefix

Definition at line 40 of file linux_args.c.

References saved_argc, and saved_argv.

{
        saved_argc = argc;
        saved_argv = argv;
}
static int parse_kv ( char *  kv,
struct list_head list 
) [static]

Parse k1=v1[,k2=v2]* into linux_settings.

Definition at line 56 of file linux_args.c.

References linux_setting::applied, DBG, linux_setting::list, list_add, malloc(), linux_setting::name, name, NULL, strsep(), token, linux_setting::value, and value.

Referenced by parse_net_args(), and parse_settings_args().

{
        char *token;
        char *name;
        char *value;
        struct linux_setting *setting;

        while ((token = strsep(&kv, ",")) != NULL) {
                name = strsep(&token, "=");
                if (name == NULL)
                        continue;
                value = token;
                if (value == NULL) {
                        DBG("Bad parameter: '%s'\n", name);
                        continue;
                }

                setting = malloc(sizeof(*setting));

                if (! setting)
                        return -1;

                setting->name = name;
                setting->value = value;
                setting->applied = 0;
                list_add(&setting->list, list);
        }

        return 0;
}
static int parse_net_args ( char *  args) [static]

Parse --net arguments.

Format is --net driver_name[,name=value]*

Definition at line 92 of file linux_args.c.

References linux_device_request::driver, driver, INIT_LIST_HEAD, linux_device_requests, linux_device_request::list, list_add_tail, malloc(), parse_kv(), printf(), rc, linux_device_request::settings, strlen(), and strsep().

Referenced by linux_args_parse().

{
        char *driver;
        struct linux_device_request *dev_request;
        int rc;

        driver = strsep(&args, ",");

        if (strlen(driver) == 0) {
                printf("Missing driver name");
                return -1;
        }

        dev_request = malloc(sizeof(*dev_request));

        dev_request->driver = driver;
        INIT_LIST_HEAD(&dev_request->settings);
        list_add_tail(&dev_request->list, &linux_device_requests);

        /* Parse rest of the settings */
        rc = parse_kv(args, &dev_request->settings);

        if (rc)
                printf("Parsing net settings failed");

        return rc;
}
static int parse_settings_args ( char *  args) [static]

Parse --settings arguments.

Format is --settings name=value[,name=value]*

Definition at line 125 of file linux_args.c.

References linux_global_settings, and parse_kv().

Referenced by linux_args_parse().

{
        return parse_kv(args, &linux_global_settings);
}
void linux_args_parse ( )

Parse passed command-line arguments.

Definition at line 132 of file linux_args.c.

References getopt_long(), optarg, parse_net_args(), parse_settings_args(), rc, reset_getopt(), saved_argc, and saved_argv.

{
        int c;
        int rc;

        reset_getopt();
        while (1) {
                int option_index = 0;

                c = getopt_long(saved_argc, saved_argv, "", options, &option_index);
                if (c == -1)
                        break;

                switch (c) {
                case 'n':
                        if ((rc = parse_net_args(optarg)) != 0)
                                return;
                        break;
                case 's':
                        if ((rc = parse_settings_args(optarg)) != 0)
                                return;
                        break;
                default:
                        return;
                }
        }

        return;
}
void linux_args_cleanup ( int flags  __unused)

Clean up requests and settings.

Definition at line 163 of file linux_args.c.

References free, linux_device_requests, linux_global_settings, linux_device_request::list, linux_setting::list, list_del, list_for_each_entry_safe, request, and linux_device_request::settings.

{
        struct linux_device_request *request;
        struct linux_device_request *rtmp;
        struct linux_setting *setting;
        struct linux_setting *stmp;

        /* Clean up requests and their settings */
        list_for_each_entry_safe(request, rtmp, &linux_device_requests, list) {
                list_for_each_entry_safe(setting, stmp, &request->settings, list) {
                        list_del(&setting->list);
                        free(setting);
                }
                list_del(&request->list);
                free(request);
        }

        /* Clean up global settings */
        list_for_each_entry_safe(setting, stmp, &linux_global_settings, list) {
                list_del(&setting->list);
                free(setting);
        }
}
struct startup_fn startup_linux_args __startup_fn ( STARTUP_EARLY  ) [read]

Variable Documentation

int saved_argc = 0 [static]

Saved argc.

Definition at line 31 of file linux_args.c.

Referenced by linux_args_parse(), and save_args().

char** saved_argv [static]

Saved argv.

Definition at line 33 of file linux_args.c.

Referenced by linux_args_parse(), and save_args().

struct option options[] [static]
Initial value:
 {
        {"net", 1, NULL, 'n'},
        {"settings", 1, NULL, 's'},
        {NULL, 0, NULL, 0}
}

Supported command-line options.

Definition at line 47 of file linux_args.c.