From 2fbf57533b7037c7b21c6c00a766fee91dd1b8c7 Mon Sep 17 00:00:00 2001 From: Florian Lohoff Date: Sun, 5 Oct 2008 11:42:41 +0000 Subject: Move arg handling to prom code --- arclib/prom.c | 106 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 103 insertions(+), 3 deletions(-) (limited to 'arclib/prom.c') diff --git a/arclib/prom.c b/arclib/prom.c index 87a2bf3..5c12ff4 100644 --- a/arclib/prom.c +++ b/arclib/prom.c @@ -1,11 +1,20 @@ +#include +#include #include +#include + #include "arc.h" #include "prom.h" -#include -#include -#include +#define MAX_ARG 64 + +static char *iargv[MAX_ARG]; +static int iargc=0; + +static char *OSLoadPartition=NULL; +static char *OSLoadFilename=NULL; +static char *OSLoadOptions=NULL; void prom_flush_cache_all(void ) { ArcFlushAllCaches(); @@ -139,3 +148,94 @@ int prom_read(FILE stream, char *string, unsigned long len, unsigned long *rlen) return ArcRead(stream, string, len, rlen); } +/* we filter these out of the command line */ +static char* env_vars[] = { "ConsoleIn=", + "ConsoleOut=", + "OSLoader=", + "OSLoadPartition=", + "OSLoadFilename=", + "OSLoadOptions=", + }; +#define NENTS(foo) ((sizeof((foo)) / (sizeof((foo[0]))))) + + +static int isEnvVar(const char* arg) +{ + unsigned int i; + + for (i = 0; i < NENTS(env_vars); i++) { + if(strncmp( env_vars[i], arg, strlen(env_vars[i]) ) == 0) + return 1; + } + return 0; +} + +int prom_parse_args(int argc, char **argv) +{ + unsigned long arg; + char *equals; + size_t len; + + /* save some things we need later */ + for (arg = 1; arg < argc; arg++) { + equals = strchr(argv[arg], '='); + if (equals != NULL) { + len = equals - argv[arg]; + if (strncmp(argv[arg], "OSLoadPartition", len) == 0) + OSLoadPartition = equals + 1; + if (strncmp(argv[arg], "OSLoadFilename", len) == 0) + OSLoadFilename = equals + 1; + if (strncmp(argv[arg], "OSLoadOptions", len) == 0) { + /* Copy options to local memory to avoid overwrite later */ + OSLoadOptions = strdup(equals + 1); + if (OSLoadOptions == NULL) + prom_fatal("Cannot allocate memory for options string\n\r"); + } + } + } + /* + * in case the user typed "boot a b c" the argv looks like: + * scsi(0)..(8)/arcboot a b c OSLoadPartition=.. SystemPartition=.. + * argv: `0 `-1`-2`-3`-4 `-5 + * we're interested in a,b,c so scan the command line and check for + * each argument if it is an environment variable. We're using a fixed + * list instead of ArcGetEnvironmentVariable since e.g. the prom sets + * "OSLoadOptions=auto" on reboot but + * EnvironmentVariable("OSLoadOptions") == NULL + */ + for( arg = 1; arg < argc; arg++ ) { + if(!isEnvVar(argv[arg])) { + iargv[iargc++]=strdup(argv[arg]); + } else { + if (OSLoadOptions) { + prom_add_arg(OSLoadOptions); + } + return (iargc); + } + } + return 0; +} + +void prom_add_arg(char *arg) { + iargv[iargc++]=strdup(arg); +} + +char *prom_get_label(void ) { + if (iargc > 1 && iargv[1]) { + return iargv[1]; + } + return "Linux"; +} + +char *prom_get_partition(void ) { + return OSLoadPartition; +} + +char *prom_get_options(void ) { + return OSLoadOptions; +} + +void prom_get_karg(int *argc, char ***argv) { + *argc=iargc; + *argv=iargv; +} -- cgit v1.2.3