iPXE
linux_args.c
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2010 Piotr JaroszyƄski <p.jaroszynski@gmail.com>
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 St, Fifth Floor, Boston, MA 02110-1301 USA.
00017  */
00018 
00019 FILE_LICENCE(GPL2_OR_LATER);
00020 
00021 #include <hci/linux_args.h>
00022 #include <getopt.h>
00023 #include <string.h>
00024 #include <stdio.h>
00025 #include <ipxe/settings.h>
00026 #include <ipxe/linux.h>
00027 #include <ipxe/malloc.h>
00028 #include <ipxe/init.h>
00029 
00030 /** Saved argc */
00031 static int saved_argc = 0;
00032 /** Saved argv */
00033 static char ** saved_argv;
00034 
00035 /**
00036  * Save argc and argv for later access.
00037  *
00038  * To be called by linuxprefix
00039  */
00040 __asmcall void save_args(int argc, char **argv)
00041 {
00042         saved_argc = argc;
00043         saved_argv = argv;
00044 }
00045 
00046 /** Supported command-line options */
00047 static struct option options[] = {
00048         {"net", 1, NULL, 'n'},
00049         {"settings", 1, NULL, 's'},
00050         {NULL, 0, NULL, 0}
00051 };
00052 
00053 /**
00054  * Parse k1=v1[,k2=v2]* into linux_settings
00055  */
00056 static int parse_kv(char *kv, struct list_head *list)
00057 {
00058         char *token;
00059         char *name;
00060         char *value;
00061         struct linux_setting *setting;
00062 
00063         while ((token = strsep(&kv, ",")) != NULL) {
00064                 name = strsep(&token, "=");
00065                 if (name == NULL)
00066                         continue;
00067                 value = token;
00068                 if (value == NULL) {
00069                         DBG("Bad parameter: '%s'\n", name);
00070                         continue;
00071                 }
00072 
00073                 setting = malloc(sizeof(*setting));
00074 
00075                 if (! setting)
00076                         return -1;
00077 
00078                 setting->name = name;
00079                 setting->value = value;
00080                 setting->applied = 0;
00081                 list_add(&setting->list, list);
00082         }
00083 
00084         return 0;
00085 }
00086 
00087 /**
00088  * Parse --net arguments
00089  *
00090  * Format is --net driver_name[,name=value]*
00091  */
00092 static int parse_net_args(char *args)
00093 {
00094         char *driver;
00095         struct linux_device_request *dev_request;
00096         int rc;
00097 
00098         driver = strsep(&args, ",");
00099 
00100         if (strlen(driver) == 0) {
00101                 printf("Missing driver name");
00102                 return -1;
00103         }
00104 
00105         dev_request = malloc(sizeof(*dev_request));
00106 
00107         dev_request->driver = driver;
00108         INIT_LIST_HEAD(&dev_request->settings);
00109         list_add_tail(&dev_request->list, &linux_device_requests);
00110 
00111         /* Parse rest of the settings */
00112         rc = parse_kv(args, &dev_request->settings);
00113 
00114         if (rc)
00115                 printf("Parsing net settings failed");
00116 
00117         return rc;
00118 }
00119 
00120 /**
00121  * Parse --settings arguments
00122  *
00123  * Format is --settings name=value[,name=value]*
00124  */
00125 static int parse_settings_args(char *args)
00126 {
00127         return parse_kv(args, &linux_global_settings);
00128 }
00129 
00130 
00131 /** Parse passed command-line arguments */
00132 void linux_args_parse()
00133 {
00134         int c;
00135         int rc;
00136 
00137         reset_getopt();
00138         while (1) {
00139                 int option_index = 0;
00140 
00141                 c = getopt_long(saved_argc, saved_argv, "", options, &option_index);
00142                 if (c == -1)
00143                         break;
00144 
00145                 switch (c) {
00146                 case 'n':
00147                         if ((rc = parse_net_args(optarg)) != 0)
00148                                 return;
00149                         break;
00150                 case 's':
00151                         if ((rc = parse_settings_args(optarg)) != 0)
00152                                 return;
00153                         break;
00154                 default:
00155                         return;
00156                 }
00157         }
00158 
00159         return;
00160 }
00161 
00162 /** Clean up requests and settings */
00163 void linux_args_cleanup(int flags __unused)
00164 {
00165         struct linux_device_request *request;
00166         struct linux_device_request *rtmp;
00167         struct linux_setting *setting;
00168         struct linux_setting *stmp;
00169 
00170         /* Clean up requests and their settings */
00171         list_for_each_entry_safe(request, rtmp, &linux_device_requests, list) {
00172                 list_for_each_entry_safe(setting, stmp, &request->settings, list) {
00173                         list_del(&setting->list);
00174                         free(setting);
00175                 }
00176                 list_del(&request->list);
00177                 free(request);
00178         }
00179 
00180         /* Clean up global settings */
00181         list_for_each_entry_safe(setting, stmp, &linux_global_settings, list) {
00182                 list_del(&setting->list);
00183                 free(setting);
00184         }
00185 }
00186 
00187 struct startup_fn startup_linux_args __startup_fn(STARTUP_EARLY) = {
00188         .startup = linux_args_parse,
00189         .shutdown = linux_args_cleanup,
00190 };