Skip to content

Commit

Permalink
commandline: Add commandline parsing abilities
Browse files Browse the repository at this point in the history
This commit adds commandline parsing abilities to KTF. Parameters are passed via
multiboot and then parsed and compared against the compile-time available
parameters.

Signed-off-by: Martin Mazein <[email protected]>
  • Loading branch information
MegaMaddin committed Aug 26, 2020
1 parent 7edd7e9 commit d0186dc
Show file tree
Hide file tree
Showing 5 changed files with 136 additions and 1 deletion.
72 changes: 71 additions & 1 deletion common/setup.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
*/
#include <acpi.h>
#include <apic.h>
#include <cmdline.h>
#include <console.h>
#include <ktf.h>
#include <lib.h>
Expand All @@ -44,12 +45,78 @@
#include <drivers/serial.h>
#include <slab.h>

bool opt_debug;
bool opt_debug = false;
bool_cmd("debug", opt_debug);

io_port_t com_ports[2];

const char *kernel_cmdline;

static __text_init int parse_bool(const char *s) {
if (!strcmp("no", s) || !strcmp("off", s) || !strcmp("false", s) ||
!strcmp("disable", s) || !strcmp("0", s))
return 0;

if (!strcmp("yes", s) || !strcmp("on", s) || !strcmp("true", s) ||
!strcmp("enable", s) || !strcmp("1", s))
return 1;

return -1;
}

void __text_init cmdline_parse(const char *cmdline) {
static __bss_init char opt[PAGE_SIZE];
char *optval, *optkey, *q;
const char *p = cmdline;
struct ktf_param *param;

if (cmdline == NULL)
return;

for (;;) {
p = string_trim_whitspace(p);

if (iseostr(*p))
break;

q = optkey = opt;
while ((!isspace(*p)) && (!iseostr(*p))) {
ASSERT(_ul(q - opt) < sizeof(opt) - 1);
*q++ = *p++;
}
*q = '\0';

/* split on '=' */
optval = strchr(opt, '=');
if (optval != NULL)
*optval++ = '\0';
else
/* assume a bool later */
optval = opt;

for (param = __start_cmdline; param < __end_cmdline; param++) {
if (strcmp(param->name, optkey))
continue;

switch (param->type) {
case STRING:
strcpy(param->var, optval);
break;
case ULONG:
*(unsigned long *) param->var = strtoul(optval, NULL, 0);
break;
case BOOL:
*(bool *) param->var =
!!parse_bool(!strcmp(optval, optkey) ? "1" : optval);
break;
default:
panic("Unkown cmdline type detected...");
break;
}
}
}
}

static void init_console(void) {
get_com_ports();

Expand Down Expand Up @@ -95,6 +162,9 @@ void __noreturn __text_init kernel_start(uint32_t multiboot_magic,
init_multiboot(mbi, &kernel_cmdline);
}

/* Parse commandline parameters */
cmdline_parse(kernel_cmdline);

/* Initialize Physical Memory Manager */
init_pmm();

Expand Down
54 changes: 54 additions & 0 deletions include/cmdline.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Copyright © 2020 Amazon.com, Inc. or its affiliates.
* All Rights Reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef KTF_CMDLINE_H
#define KTF_CMDLINE_H

#ifndef __ASSEMBLY__

#define PARAM_MAX_LENGTH 32

struct __packed ktf_param {
char name[PARAM_MAX_LENGTH];
enum { STRING, ULONG, BOOL } type;
void *var;
};

#define __ktfparam static __cmdline __used __aligned(1) struct ktf_param

/* compile time check for param name size */
#define __param_size_check(_name, _sizename) \
char __unused_##_name[(sizeof(_sizename) >= PARAM_MAX_LENGTH) ? -1 : 0]

#define cmd_param(_name, _var, _type) \
__param_size_check(_var, _name); \
__ktfparam __cmd_##_var = {_name, _type, &_var};

#define bool_cmd(_cmdname, _varname) cmd_param(_cmdname, _varname, BOOL)
#define ulong_cmd(_cmdname, _varname) cmd_param(_cmdname, _varname, ULONG)
#define string_cmd(_cmdname, _varname) cmd_param(_cmdname, _varname, STRING)

#endif /* __ASSEMBLY__ */

#endif /* KTF_CMDLINE_H */
3 changes: 3 additions & 0 deletions include/mm/pmm.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#define BIOS_ROM_ADDR_START 0xF0000

#ifndef __ASSEMBLY__
#include <cmdline.h>
#include <list.h>
#include <page.h>

Expand All @@ -52,6 +53,8 @@ extern unsigned long __start_bss_init[], __end_bss_init[];

extern unsigned long __start_rmode[], __end_rmode[];

extern struct ktf_param __start_cmdline[], __end_cmdline[];

struct addr_range {
const char *name;
unsigned long base;
Expand Down
1 change: 1 addition & 0 deletions include/setup.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#define KTF_SETUP_H

#ifndef __ASSEMBLY__
#include <cmdline.h>
#include <page.h>
#include <string.h>

Expand Down
7 changes: 7 additions & 0 deletions include/string.h
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,13 @@ static inline int strncmp(const char *s1, const char *s2, size_t n) {
return res;
}

static inline const char *string_trim_whitspace(const char *s) {
while (isspace(*s))
s++;

return s;
}

/* External declarations */

extern unsigned long strtoul(const char *nptr, char **endptr, int base);
Expand Down

0 comments on commit d0186dc

Please sign in to comment.