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. More...
 
static int parse_kv (char *kv, struct list_head *list)
 Parse k1=v1[,k2=v2]* into linux_settings. More...
 
static int parse_net_args (char *args)
 Parse –net arguments. More...
 
static int parse_settings_args (char *args)
 Parse –settings arguments. More...
 
void linux_args_parse ()
 Parse passed command-line arguments. More...
 
void linux_args_cleanup (int flags __unused)
 Clean up requests and settings. More...
 
struct startup_fn startup_linux_args __startup_fn (STARTUP_EARLY)
 

Variables

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

Function Documentation

◆ FILE_LICENCE()

FILE_LICENCE ( GPL2_OR_LATER  )

◆ save_args()

__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.

41 {
42  saved_argc = argc;
43  saved_argv = argv;
44 }
static int saved_argc
Saved argc.
Definition: linux_args.c:31
static char ** saved_argv
Saved argv.
Definition: linux_args.c:33

References saved_argc, and saved_argv.

◆ parse_kv()

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.

57 {
58  char *token;
59  char *name;
60  char *value;
61  struct linux_setting *setting;
62 
63  while ((token = strsep(&kv, ",")) != NULL) {
64  name = strsep(&token, "=");
65  if (name == NULL)
66  continue;
67  value = token;
68  if (value == NULL) {
69  DBG("Bad parameter: '%s'\n", name);
70  continue;
71  }
72 
73  setting = malloc(sizeof(*setting));
74 
75  if (! setting)
76  return -1;
77 
78  setting->name = name;
79  setting->value = value;
80  setting->applied = 0;
81  list_add(&setting->list, list);
82  }
83 
84  return 0;
85 }
const char * name
Definition: ath9k_hw.c:1984
struct list_head list
List node.
Definition: linux.h:116
#define list_add(new, head)
Add a new entry to the head of a list.
Definition: list.h:69
const char * name
Name.
Definition: settings.h:28
A device request setting.
Definition: linux.h:108
pseudo_bit_t value[0x00020]
Definition: arbel.h:13
void * malloc(size_t size)
Allocate memory.
Definition: malloc.c:583
u8 token
Definition: CIB_PRM.h:42
A setting.
Definition: settings.h:23
char * strsep(char **s, const char *ct)
strsep - Split a string into tokens @s: The string to be searched @ct: The characters to search for
Definition: stringextra.c:73
#define DBG(...)
Print a debugging message.
Definition: compiler.h:498
#define NULL
NULL pointer (VOID *)
Definition: Base.h:362

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

Referenced by parse_net_args(), and parse_settings_args().

◆ parse_net_args()

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.

93 {
94  char *driver;
95  struct linux_device_request *dev_request;
96  int rc;
97 
98  driver = strsep(&args, ",");
99 
100  if (strlen(driver) == 0) {
101  printf("Missing driver name");
102  return -1;
103  }
104 
105  dev_request = malloc(sizeof(*dev_request));
106 
107  dev_request->driver = driver;
108  INIT_LIST_HEAD(&dev_request->settings);
109  list_add_tail(&dev_request->list, &linux_device_requests);
110 
111  /* Parse rest of the settings */
112  rc = parse_kv(args, &dev_request->settings);
113 
114  if (rc)
115  printf("Parsing net settings failed");
116 
117  return rc;
118 }
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:464
A device request.
Definition: linux.h:98
#define list_add_tail(new, head)
Add a new entry to the tail of a list.
Definition: list.h:93
static int parse_kv(char *kv, struct list_head *list)
Parse k1=v1[,k2=v2]* into linux_settings.
Definition: linux_args.c:56
size_t strlen(const char *src)
Get length of string.
Definition: string.c:213
char * driver
Driver name.
Definition: linux.h:100
void * malloc(size_t size)
Allocate memory.
Definition: malloc.c:583
char * strsep(char **s, const char *ct)
strsep - Split a string into tokens @s: The string to be searched @ct: The characters to search for
Definition: stringextra.c:73
#define INIT_LIST_HEAD(list)
Initialise a list head.
Definition: list.h:45
struct list_head list
List node.
Definition: linux.h:102
struct list_head settings
List of settings.
Definition: linux.h:104
struct list_head linux_device_requests
List of requested devices.

References linux_device_request::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().

◆ parse_settings_args()

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.

126 {
127  return parse_kv(args, &linux_global_settings);
128 }
static int parse_kv(char *kv, struct list_head *list)
Parse k1=v1[,k2=v2]* into linux_settings.
Definition: linux_args.c:56
struct list_head linux_global_settings
List of global settings to apply.

References linux_global_settings, and parse_kv().

Referenced by linux_args_parse().

◆ linux_args_parse()

void linux_args_parse ( )

Parse passed command-line arguments.

Definition at line 132 of file linux_args.c.

133 {
134  int c;
135  int rc;
136 
137  reset_getopt();
138  while (1) {
139  int option_index = 0;
140 
141  c = getopt_long(saved_argc, saved_argv, "", options, &option_index);
142  if (c == -1)
143  break;
144 
145  switch (c) {
146  case 'n':
147  if ((rc = parse_net_args(optarg)) != 0)
148  return;
149  break;
150  case 's':
151  if ((rc = parse_settings_args(optarg)) != 0)
152  return;
153  break;
154  default:
155  return;
156  }
157  }
158 
159  return;
160 }
uint32_t c
Definition: md4.c:30
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
static int parse_settings_args(char *args)
Parse –settings arguments.
Definition: linux_args.c:125
static int saved_argc
Saved argc.
Definition: linux_args.c:31
static char ** saved_argv
Saved argv.
Definition: linux_args.c:33
static void reset_getopt(void)
Reset getopt() internal state.
Definition: getopt.h:89
int getopt_long(int argc, char *const argv[], const char *optstring, const struct option *longopts, int *longindex)
Parse command-line options.
Definition: getopt.c:229
static struct option options[]
Supported command-line options.
Definition: linux_args.c:47
static int parse_net_args(char *args)
Parse –net arguments.
Definition: linux_args.c:92
char * optarg
Option argument.
Definition: getopt.c:43

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

◆ linux_args_cleanup()

void linux_args_cleanup ( int flags  __unused)

Clean up requests and settings.

Definition at line 163 of file linux_args.c.

164 {
166  struct linux_device_request *rtmp;
167  struct linux_setting *setting;
168  struct linux_setting *stmp;
169 
170  /* Clean up requests and their settings */
172  list_for_each_entry_safe(setting, stmp, &request->settings, list) {
173  list_del(&setting->list);
174  free(setting);
175  }
176  list_del(&request->list);
177  free(request);
178  }
179 
180  /* Clean up global settings */
182  list_del(&setting->list);
183  free(setting);
184  }
185 }
struct list_head list
List node.
Definition: linux.h:116
A device request.
Definition: linux.h:98
#define list_del(list)
Delete an entry from a list.
Definition: list.h:119
A device request setting.
Definition: linux.h:108
#define list_for_each_entry_safe(pos, tmp, head, member)
Iterate over entries in a list, safe against deletion of the current entry.
Definition: list.h:447
static void(* free)(struct refcnt *refcnt))
Definition: refcnt.h:54
A setting.
Definition: settings.h:23
struct list_head linux_global_settings
List of global settings to apply.
u8 request[0]
List of IEs requested.
Definition: ieee80211.h:16
struct list_head linux_device_requests
List of requested devices.

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

◆ __startup_fn()

struct startup_fn startup_linux_args __startup_fn ( STARTUP_EARLY  )

Variable Documentation

◆ saved_argc

int saved_argc = 0
static

Saved argc.

Definition at line 31 of file linux_args.c.

Referenced by linux_args_parse(), and save_args().

◆ saved_argv

char** saved_argv
static

Saved argv.

Definition at line 33 of file linux_args.c.

Referenced by linux_args_parse(), and save_args().

◆ options

struct option options[]
static
Initial value:
= {
{"net", 1, NULL, 'n'},
{"settings", 1, NULL, 's'},
{NULL, 0, NULL, 0}
}
#define NULL
NULL pointer (VOID *)
Definition: Base.h:362

Supported command-line options.

Definition at line 47 of file linux_args.c.

Referenced by linux_args_parse().