From: <bob...@us...> - 2007-07-16 11:51:05
|
Revision: 1160 http://svn.sourceforge.net/hackndev/?rev=1160&view=rev Author: bobofdoom Date: 2007-07-16 04:51:01 -0700 (Mon, 16 Jul 2007) Log Message: ----------- Cocoboot: Added ability to read a config file from memory card. Name the file "cocoboot.conf" and place it in the root directory. See the file cocboot.conf.example to see the syntax and what options are available. Modified Paths: -------------- cocoboot/trunk/Changelog cocoboot/trunk/include/imgloader.h cocoboot/trunk/m68k/Makefile cocoboot/trunk/m68k/cocoboot.c cocoboot/trunk/m68k/mainform.c Added Paths: ----------- cocoboot/trunk/cocoboot.conf.example cocoboot/trunk/include/options.h cocoboot/trunk/m68k/options.c Modified: cocoboot/trunk/Changelog =================================================================== --- cocoboot/trunk/Changelog 2007-07-16 00:05:34 UTC (rev 1159) +++ cocoboot/trunk/Changelog 2007-07-16 11:51:01 UTC (rev 1160) @@ -1,4 +1,5 @@ - - Added compile option for skipping prompts. + - Added reading of a config file. + - Added option for skipping prompts. version 0.3: - Added overflow-buffer as last resort to allocation problem (slapin's idea) Added: cocoboot/trunk/cocoboot.conf.example =================================================================== --- cocoboot/trunk/cocoboot.conf.example (rev 0) +++ cocoboot/trunk/cocoboot.conf.example 2007-07-16 11:51:01 UTC (rev 1160) @@ -0,0 +1,32 @@ +# cocoboot.conf.exaple - example configuration file for Cocboot + +## cmdline +# The value of cmdline is the default kernel command-line. The user can +# edit this via the GUI unless noprompt is enabled. +# +# Default: [blank] + +cmdline = root=/dev/mmcblk0p1 + +## kernel +# Filename of the kernel zImage. Cocoboot will look for this file on all +# attached VFS disks. +# +# Defaut: /zImage + +kernel = /zImage-2.6.21 + +## initrd +# Filename of the initrd or initramfs ramdisk image. +# +# Default: /initrd.gz + +initrd = /initramfs_cpio.gz + +## noprompt +# When this option is 1, Cocoboot will not prompt for the kernel +# command-line and will instead boot Linux immediately when it is started. +# +# Default: 0 + +noprompt = 0 Modified: cocoboot/trunk/include/imgloader.h =================================================================== --- cocoboot/trunk/include/imgloader.h 2007-07-16 00:05:34 UTC (rev 1159) +++ cocoboot/trunk/include/imgloader.h 2007-07-16 11:51:01 UTC (rev 1160) @@ -1,3 +1,3 @@ Int32 search_image(char *name, char *loc, UInt16 loc_len, UInt32 *size); Int32 load_image(char *name, UInt32 size, UInt16 vol_ref, void *buffer); - +int search_file(char *name, UInt16 *vol_ref, UInt32 *size); Added: cocoboot/trunk/include/options.h =================================================================== --- cocoboot/trunk/include/options.h (rev 0) +++ cocoboot/trunk/include/options.h 2007-07-16 11:51:01 UTC (rev 1160) @@ -0,0 +1,9 @@ +#ifndef _OPTIONS_H_ +#define _OPTIONS_H_ + +#define CONFIG_FILE "/cocoboot.conf" + +char *get_option(char *key); +int set_option(char *key, char *value); +int read_config(void); +#endif Modified: cocoboot/trunk/m68k/Makefile =================================================================== --- cocoboot/trunk/m68k/Makefile 2007-07-16 00:05:34 UTC (rev 1159) +++ cocoboot/trunk/m68k/Makefile 2007-07-16 11:51:01 UTC (rev 1160) @@ -2,7 +2,7 @@ CFLAGS = -Wall -O -s -I../include ${DEFINES} LDFLAGS = -static LIBS=-L/software/apps/prc-tools/sdks/sdk-5r3/lib/m68k-palmos-coff/ -lPalmOSGlue -OBJS = cocoboot.o mainform.o mem.o cpu.o imgloader.o mach.o +OBJS = cocoboot.o mainform.o mem.o cpu.o imgloader.o mach.o options.o cocoboot.m68k: $(OBJS) Modified: cocoboot/trunk/m68k/cocoboot.c =================================================================== --- cocoboot/trunk/m68k/cocoboot.c 2007-07-16 00:05:34 UTC (rev 1159) +++ cocoboot/trunk/m68k/cocoboot.c 2007-07-16 11:51:01 UTC (rev 1160) @@ -26,6 +26,7 @@ #include <VFSMgr.h> #include "cocoboot.h" +#include "options.h" #include "cocoboot_r.h" #include "mainform.h" @@ -398,7 +399,7 @@ UInt16 start_app() { - + read_config(); arm_stack[0] = 0; FrmGotoForm(MainForm); Modified: cocoboot/trunk/m68k/mainform.c =================================================================== --- cocoboot/trunk/m68k/mainform.c 2007-07-16 00:05:34 UTC (rev 1159) +++ cocoboot/trunk/m68k/mainform.c 2007-07-16 11:51:01 UTC (rev 1160) @@ -6,6 +6,7 @@ #include "mem.h" #include "regs.h" #include "imgloader.h" +#include "options.h" #include <stdio.h> #include <stdlib.h> #include <string.h> @@ -270,10 +271,10 @@ char *cmdline; log_clear(); - kernel_size = load_parts(0, "/zImage", &kernel); + kernel_size = load_parts(0, get_option("kernel"), &kernel); if(kernel_size) { if(use_initrd) { - initrd_size = load_parts(1, "/initrd.gz", &initrd); + initrd_size = load_parts(1, get_option("initrd"), &initrd); } if(!use_initrd || initrd_size) { @@ -372,7 +373,7 @@ cmdline_p = FrmGetObjectPtr(form, FrmGetObjectIndex(form, CommandLine)); cmdline_th = MemHandleNew(size); cmdline_tp = MemHandleLock(cmdline_th); - StrCopy(cmdline_tp, " "); /* default value */ + StrCopy(cmdline_tp, get_option("cmdline")); /* default value */ //PrefGetAppPreferences ('CcBt', 1, cmdline_tp, &size, true); MemHandleUnlock(cmdline_th); FldSetTextHandle(cmdline_p, cmdline_th); @@ -381,13 +382,13 @@ FrmDrawForm(form); handled = true; - kernel_ok = check_image("/zImage"); - use_initrd = check_image("/initrd.gz"); + kernel_ok = check_image(get_option("kernel")); + use_initrd = check_image(get_option("initrd")); + + /* boot immediately if in noprompt mode */ + if (atoi(get_option("noprompt")) != 0 && kernel_ok) + boot_linux(); -#ifdef NOPROMPT_MODE - if (kernel_ok) boot_linux(); -#endif - } else if (event->eType == menuEvent) { return mainform_menu_event(event->data.menu.itemID); } Added: cocoboot/trunk/m68k/options.c =================================================================== --- cocoboot/trunk/m68k/options.c (rev 0) +++ cocoboot/trunk/m68k/options.c 2007-07-16 11:51:01 UTC (rev 1160) @@ -0,0 +1,202 @@ +/* + * This program is free software ; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + */ + +#include <PalmOS.h> +#include <VFSMgr.h> +#include <ctype.h> +#include <stdio.h> +#include <string.h> +#include "cocoboot.h" +#include "options.h" +#include "imgloader.h" +#include "cocoboot_r.h" + +struct option { + char key[1024]; + char value[1024]; +} options[] = { + { + .key = "cmdline", + .value = " ", + }, + { + .key = "kernel", + .value = "/zImage", + }, + { + .key = "initrd", + .value = "/initrd.gz", + }, + { + .key = "noprompt", + .value = "0", + }, + { + .key = "", /* sentinal */ + }, +}; + +//int search_file(char *name, UInt16 *vol_ref, UInt32 *size); + +/** + * Read a single line from file f into the given buffer, stripping linefeeds. + */ +Err read_line(FileRef f, char *buf, int buflen) +{ + Err err; + char c; + int i; + + for (i=0; i<buflen; i++) { + err = VFSFileRead(f, 1, &c, NULL); + if (err != errNone) { + return err; + } + + if (c == '\n') { + buf[i] = 0; + break; + } + + if (c == '\r') { + continue; + } + + buf[i] = c; + } + return errNone; +} + +char *parse_config_line(char *line, char **out_key, char **out_value) +{ + char *p = line; + char *key, *value; + + *out_key = *out_value = NULL; + + /* skip whitespace at the start of the key */ + key = line; + while (*key && isspace(*key)) + key++; + if (!*key || *key == '#') return NULL; /* error: empty line or comment */ + + + /* divide key and value into seperate strings */ + while (*p && *p != '=') p++; + if (!*p) return "no equals sign found"; + *p = 0; + value = p + 1; + + /* work backwards to find the end of the key, again skipping spaces */ + p--; + while (p >= line && *p && isspace(*p)) + p--; + if (p < line) return "empty key"; + p++; + *p = 0; + + /* strip whitespace at the start of the value */ + while (*value && isspace(*value)) + value++; + if (!*value) return "empty value"; + p = value; + + /* find end of value */ + while (*p) p++; + p--; + + /* work backwards to find end of value, again skipping spaces */ + while (p >= value && *p && isspace(*p)) + p--; + p++; + *p = 0; + + *out_key = key; + *out_value = value; + return NULL; +} + +void show_parse_error(char *errmsg, int line_no) +{ + char s_line_no[128]; + sprintf(s_line_no, "\nLine %d of " CONFIG_FILE, line_no); + FrmCustomAlert(ErrorAlert, "Parse Error: ", errmsg, s_line_no); +} + +int set_option(char *key, char *value) +{ + struct option *opt = &options[0]; + + while (opt->key[0]) { + if (!strcmp(key, opt->key)) { + strncpy(opt->value, value, sizeof(opt->value)-1); + return 0; + } + opt++; + } + return -1; +} + +char *get_option(char *key) +{ + struct option *opt = &options[0]; + + while (opt->key[0]) { + if (!strcmp(key, opt->key)) { + return opt->value; + } + opt++; + } + return NULL; +} + +int read_config(void) +{ + Err err; + UInt16 vol; + UInt32 size; + FileRef f; + char buf[2048]; + int line_no; + char *errmsg, *key, *value; + + if (!search_file(CONFIG_FILE, &vol, &size)) { + return -1; + } + + err = VFSFileOpen(vol, CONFIG_FILE, vfsModeRead, &f); + if (err != errNone) { + return err; + } + + for (line_no = 1; 1; line_no++) { + err = read_line(f, buf, sizeof(buf)-1); + if (err != errNone) break; + + + if ((errmsg=parse_config_line(buf, &key, &value))) { + show_parse_error(errmsg, line_no); + continue; + } + + if (set_option(key, value) < 0) { + show_parse_error("unrecognised option", line_no); + continue; + } + } + return 0; +} This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |