2011-12-11 03:34:10 +04:00
|
|
|
/* vim: tabstop=4 shiftwidth=4 noexpandtab
|
|
|
|
*
|
2013-12-02 07:37:13 +04:00
|
|
|
* Kernel Argument Manager
|
2011-04-15 05:36:51 +04:00
|
|
|
*
|
2013-12-02 07:37:13 +04:00
|
|
|
* Arguments to the kernel are provided from the bootloader and
|
|
|
|
* provide information such as what mode to pass to init, or what
|
|
|
|
* hard disk partition should be mounted as root.
|
2011-04-15 05:36:51 +04:00
|
|
|
*
|
2013-12-02 07:37:13 +04:00
|
|
|
* This module provides access
|
2011-03-26 21:30:08 +03:00
|
|
|
*/
|
|
|
|
#include <system.h>
|
2012-09-13 10:14:01 +04:00
|
|
|
#include <logging.h>
|
2013-12-02 07:37:13 +04:00
|
|
|
#include <args.h>
|
2013-12-02 09:19:23 +04:00
|
|
|
#include <hashmap.h>
|
2013-12-02 07:37:13 +04:00
|
|
|
#include <tokenize.h>
|
2011-03-26 21:30:08 +03:00
|
|
|
|
2013-03-18 11:52:12 +04:00
|
|
|
char * cmdline = NULL;
|
|
|
|
|
2013-12-02 09:19:23 +04:00
|
|
|
hashmap_t * kernel_args_map = NULL;
|
2013-12-02 07:37:13 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if an argument was provided to the kernel. If the argument is
|
|
|
|
* a simple switch, a response of 1 can be considered "on" for that
|
|
|
|
* argument; otherwise, this just notes that the argument was present,
|
|
|
|
* so the caller should check whether it is correctly set.
|
|
|
|
*/
|
|
|
|
int args_present(char * karg) {
|
2013-12-02 09:19:23 +04:00
|
|
|
if (!kernel_args_map) return 0; /* derp */
|
2013-12-02 07:37:13 +04:00
|
|
|
|
2013-12-02 09:19:23 +04:00
|
|
|
return hashmap_has(kernel_args_map, karg);
|
2013-12-02 07:37:13 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the value associated with an argument provided to the kernel.
|
|
|
|
*/
|
|
|
|
char * args_value(char * karg) {
|
2013-12-02 09:19:23 +04:00
|
|
|
if (!kernel_args_map) return 0; /* derp */
|
2013-12-02 07:37:13 +04:00
|
|
|
|
2013-12-02 09:19:23 +04:00
|
|
|
return hashmap_get(kernel_args_map, karg);
|
2013-12-02 07:37:13 +04:00
|
|
|
}
|
|
|
|
|
2011-04-15 05:36:51 +04:00
|
|
|
/**
|
|
|
|
* Parse the given arguments to the kernel.
|
|
|
|
*
|
|
|
|
* @param arg A string containing all arguments, separated by spaces.
|
|
|
|
*/
|
2013-12-02 07:37:13 +04:00
|
|
|
void args_parse(char * _arg) {
|
2011-04-15 05:36:51 +04:00
|
|
|
/* Sanity check... */
|
2013-03-18 11:52:12 +04:00
|
|
|
if (!_arg) { return; }
|
|
|
|
|
|
|
|
char * arg = strdup(_arg);
|
2013-12-02 07:37:13 +04:00
|
|
|
char * argv[1024];
|
|
|
|
int argc = tokenize(arg, " ", argv);
|
|
|
|
|
|
|
|
/* New let's parse the tokens into the arguments list so we can index by key */
|
|
|
|
|
2013-12-02 09:19:23 +04:00
|
|
|
if (!kernel_args_map) {
|
|
|
|
kernel_args_map = hashmap_create(10);
|
2011-03-26 21:30:08 +03:00
|
|
|
}
|
2013-12-02 07:37:13 +04:00
|
|
|
|
|
|
|
for (int i = 0; i < argc; ++i) {
|
|
|
|
char * c = strdup(argv[i]);
|
|
|
|
|
2013-12-02 09:19:23 +04:00
|
|
|
char * name;
|
|
|
|
char * value;
|
|
|
|
|
|
|
|
name = c;
|
|
|
|
value = NULL;
|
2013-12-02 07:37:13 +04:00
|
|
|
/* Find the first = and replace it with a null */
|
|
|
|
char * v = c;
|
|
|
|
while (*v) {
|
|
|
|
if (*v == '=') {
|
|
|
|
*v = '\0';
|
|
|
|
v++;
|
2013-12-02 09:19:23 +04:00
|
|
|
value = v;
|
2013-12-02 07:37:13 +04:00
|
|
|
goto _break;
|
2012-10-04 09:03:21 +04:00
|
|
|
}
|
2013-12-02 07:37:13 +04:00
|
|
|
v++;
|
2011-03-26 21:30:08 +03:00
|
|
|
}
|
2013-12-02 07:37:13 +04:00
|
|
|
|
|
|
|
_break:
|
2013-12-02 09:19:23 +04:00
|
|
|
hashmap_set(kernel_args_map, name, value);
|
2011-03-26 21:30:08 +03:00
|
|
|
}
|
2013-12-02 07:37:13 +04:00
|
|
|
|
|
|
|
free(arg);
|
|
|
|
|
2011-03-26 21:30:08 +03:00
|
|
|
}
|
2011-04-15 05:36:51 +04:00
|
|
|
|