Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

setup: move cmdline functionality into a dedicated file #246

Merged
merged 3 commits into from
Jan 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
108 changes: 108 additions & 0 deletions common/cmdline.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
/*
* Copyright (c) 2021 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.
*/

#include <cmdline.h>
#include <ktf.h>
#include <mm/regions.h>
#include <string.h>

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

bool opt_keyboard = true;
bool_cmd("keyboard", opt_keyboard);

bool opt_pit = false;
bool_cmd("pit", opt_pit);

bool opt_apic_timer = false;
bool_cmd("apic_timer", opt_apic_timer);

bool opt_hpet = false;
bool_cmd("hpet", opt_hpet);

bool opt_fpu = false;
bool_cmd("fpu", opt_fpu);

const char *kernel_cmdline;

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:
strncpy(param->var, optval, param->varlen);
if (strlen(optval) >= param->varlen) {
((char *) param->var)[param->varlen - 1] = '\0';
printk("WARNING: The commandline parameter value for %s does not fit "
"into the preallocated buffer (size %lu >= %u)\n",
param->name, strlen(optval), param->varlen);
}
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;
}
}
}
}
106 changes: 8 additions & 98 deletions common/setup.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,107 +60,17 @@
#include <perfmon/pfmlib.h>
#endif

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

bool opt_keyboard = true;
bool_cmd("keyboard", opt_keyboard);

bool opt_pit = false;
bool_cmd("pit", opt_pit);

bool opt_apic_timer = false;
bool_cmd("apic_timer", opt_apic_timer);

bool opt_hpet = false;
bool_cmd("hpet", opt_hpet);

bool opt_fpu = false;
bool_cmd("fpu", opt_fpu);

io_port_t __data_rmode com_ports[2] = {COM1_PORT, COM2_PORT};

boot_flags_t boot_flags;

static unsigned bsp_cpu_id = 0;

const char *kernel_cmdline;
char cpu_identifier[49];

unsigned get_bsp_cpu_id(void) { return bsp_cpu_id; }
void set_bsp_cpu_id(unsigned cpu_id) { bsp_cpu_id = cpu_id; }

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:
strncpy(param->var, optval, param->varlen);
if (strlen(optval) >= param->varlen) {
((char *) param->var)[param->varlen - 1] = '\0';
printk("WARNING: The commandline parameter value for %s does not fit "
"into the preallocated buffer (size %lu >= %u)\n",
param->name, strlen(optval), param->varlen);
}
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 __text_init init_console(void) {
get_com_ports();

Expand Down Expand Up @@ -216,6 +126,14 @@ void __noreturn __text_init kernel_start(uint32_t multiboot_magic,
/* Zero-out BSS sections */
zero_bss();

if (multiboot_magic == MULTIBOOT_BOOTLOADER_MAGIC) {
/* Indentity mapping is still on, so fill in multiboot structures */
init_multiboot(mbi, &kernel_cmdline);
}

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

/* Initialize console early */
init_console();

Expand All @@ -237,14 +155,6 @@ void __noreturn __text_init kernel_start(uint32_t multiboot_magic,
/* PIC is initialized - enable local interrupts */
sti();

if (multiboot_magic == MULTIBOOT_BOOTLOADER_MAGIC) {
/* Indentity mapping is still on, so fill in multiboot structures */
init_multiboot(mbi, &kernel_cmdline);
}

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

/* Initialize Physical Memory Manager */
init_regions();
init_pmm();
Expand Down
28 changes: 28 additions & 0 deletions include/cmdline.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@

#ifndef __ASSEMBLY__
#include <compiler.h>
#include <ktf.h>
#include <string.h>

#define PARAM_MAX_LENGTH 32

Expand All @@ -53,4 +55,30 @@ struct __packed ktf_param {

#endif /* __ASSEMBLY__ */

/* External declarations */

extern bool opt_debug;
extern bool opt_keyboard;
extern bool opt_pit;
extern bool opt_apic_timer;
extern bool opt_hpet;
extern bool opt_fpu;
extern const char *kernel_cmdline;

extern void cmdline_parse(const char *cmdline);

/* Static declarations */

static inline 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;
}

#endif /* KTF_CMDLINE_H */
1 change: 0 additions & 1 deletion include/setup.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ typedef struct boot_flags boot_flags_t;

extern io_port_t com_ports[2];

extern const char *kernel_cmdline;
extern char cpu_identifier[49];
extern boot_flags_t boot_flags;

Expand Down
1 change: 0 additions & 1 deletion tests/unittests.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
#include <symbols.h>
#include <test.h>

extern char *kernel_cmdline;
#include <cmdline.h>

static char opt_string[4];
Expand Down