summaryrefslogtreecommitdiffstats
path: root/discover/grub2
diff options
context:
space:
mode:
authorJeremy Kerr <jk@ozlabs.org>2013-09-16 15:12:13 +0800
committerJeremy Kerr <jk@ozlabs.org>2013-09-24 13:14:59 +0800
commit2ea5eb23b027519372dd20fbe8f958c06ac2aa6c (patch)
tree850a805d771be946d1cc28fbecabea0be6acf84c /discover/grub2
parent5c6263a12dd7f859daa0feba8b58a0558f0ff21c (diff)
downloadtalos-petitboot-2ea5eb23b027519372dd20fbe8f958c06ac2aa6c.tar.gz
talos-petitboot-2ea5eb23b027519372dd20fbe8f958c06ac2aa6c.zip
discover/grub2: Hook up flex/bison parser to discover server
Signed-off-by: Jeremy Kerr <jk@ozlabs.org>
Diffstat (limited to 'discover/grub2')
-rw-r--r--discover/grub2/Makefile.am48
-rw-r--r--discover/grub2/grub2.c103
-rw-r--r--discover/grub2/grub2.h18
-rw-r--r--discover/grub2/lexer.l1
-rw-r--r--discover/grub2/parser.y25
-rw-r--r--discover/grub2/script.c2
6 files changed, 196 insertions, 1 deletions
diff --git a/discover/grub2/Makefile.am b/discover/grub2/Makefile.am
new file mode 100644
index 0000000..71f3282
--- /dev/null
+++ b/discover/grub2/Makefile.am
@@ -0,0 +1,48 @@
+# 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; version 2 of the License.
+#
+# 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
+#
+
+AM_CPPFLAGS = -I$(top_srcdir)/lib -I$(top_srcdir) $(DEFAULT_CPPFLAGS)
+
+AM_CFLAGS = $(DEFAULT_CFLAGS) \
+ -DPREFIX='"$(prefix)"' \
+ -DPKG_SHARE_DIR='"$(pkgdatadir)"' \
+ -DPKG_SYSCONF_DIR='"$(pkgsysconfdir)"' \
+ -DLOCAL_STATE_DIR='"$(localstatedir)"'
+
+AM_YFLAGS = -d
+
+noinst_PROGRAMS = grub2-parser.ro
+
+grub2-parser.ro$(EXEEXT): $(grub2_parser_ro_OBJECTS)
+ $(LD) -r -o $@ $^
+
+
+grub2_parser_ro_SOURCES = \
+ builtins.c \
+ grub2.h \
+ grub2.c \
+ lexer.l \
+ parser.y \
+ script.c \
+ parser.c
+
+BUILT_SOURCES = parser.h lexer.h
+
+# ylwrap doesn't handle flex header files well; use our own rule here.
+lexer.h lexer.c: lexer.l
+ $(LEX) $(LFLAGS) --header-file=lexer.h -o lexer.c $^
+
+lexer.o: CFLAGS+=-Wno-unused-parameter -Wno-missing-prototypes \
+ -Wno-missing-declarations
+
diff --git a/discover/grub2/grub2.c b/discover/grub2/grub2.c
new file mode 100644
index 0000000..ebc6ac7
--- /dev/null
+++ b/discover/grub2/grub2.c
@@ -0,0 +1,103 @@
+
+#include <assert.h>
+
+#include <talloc/talloc.h>
+
+#include <discover/resource.h>
+#include <discover/parser.h>
+#include <discover/parser-utils.h>
+
+#include "grub2.h"
+#include "parser.h"
+#include "lexer.h"
+
+static const char *const grub2_conf_files[] = {
+ "/grub.cfg",
+ "/menu.lst",
+ "/grub/grub.cfg",
+ "/grub2/grub.cfg",
+ "/grub/menu.lst",
+ "/boot/grub/grub.cfg",
+ "/boot/grub2/grub.cfg",
+ "/boot/grub/menu.lst",
+ "/GRUB.CFG",
+ "/MENU.LST",
+ "/GRUB/GRUB.CFG",
+ "/GRUB2/GRUB.CFG",
+ "/GRUB/MENU.LST",
+ "/BOOT/GRUB/GRUB.CFG",
+ "/BOOT/GRUB/MENU.LST",
+ NULL
+};
+
+struct grub2_resource_info {
+ struct grub2_root *root;
+ char *path;
+};
+
+/* we use slightly different resources for grub2 */
+struct resource *create_grub2_resource(void *ctx,
+ struct discover_device *orig_device,
+ struct grub2_root *root, const char *path)
+{
+ struct grub2_resource_info *info;
+ struct resource *res;
+
+ res = talloc(ctx, struct resource);
+
+ if (root) {
+ info = talloc(res, struct grub2_resource_info);
+ info->root = root;
+ talloc_reference(info, root);
+ info->path = talloc_strdup(info, path);
+
+ res->resolved = false;
+ res->info = info;
+
+ } else
+ resolve_resource_against_device(res, orig_device, path);
+
+ return res;
+}
+
+bool resolve_grub2_resource(struct device_handler *handler,
+ struct resource *res)
+{
+ struct grub2_resource_info *info = res->info;
+ struct discover_device *dev;
+
+ assert(!res->resolved);
+
+ dev = device_lookup_by_uuid(handler, info->root->uuid);
+
+ if (!dev)
+ return false;
+
+ resolve_resource_against_device(res, dev, info->path);
+ talloc_free(info);
+
+ return true;
+}
+
+static int grub2_parse(struct discover_context *dc, char *buf, int len)
+{
+ struct grub2_parser *parser;
+
+ parser = grub2_parser_create(dc);
+
+ grub2_parser_parse(parser, buf, len);
+
+ talloc_free(parser);
+
+ return 1;
+}
+
+static struct parser grub2_parser = {
+ .name = "grub2",
+ .method = CONF_METHOD_LOCAL_FILE,
+ .parse = grub2_parse,
+ .filenames = grub2_conf_files,
+ .resolve_resource = resolve_grub2_resource,
+};
+
+register_parser(grub2_parser);
diff --git a/discover/grub2/grub2.h b/discover/grub2/grub2.h
index 9da5cee..149e1e1 100644
--- a/discover/grub2/grub2.h
+++ b/discover/grub2/grub2.h
@@ -1,6 +1,8 @@
#ifndef GRUB2_H
#define GRUB2_H
+#include <discover/device-handler.h>
+
#include <stdbool.h>
#include <list/list.h>
@@ -88,6 +90,10 @@ struct grub2_parser {
struct grub2_script *script;
};
+struct grub2_root {
+ char *uuid;
+};
+
struct grub2_statements *create_statements(struct grub2_parser *parser);
struct grub2_statement *create_statement_simple(struct grub2_parser *parser,
@@ -143,5 +149,17 @@ struct grub2_command *script_lookup_command(struct grub2_script *script,
const char *name);
void register_builtins(struct grub2_script *script);
+
+/* resources */
+struct resource *create_grub2_resource(void *ctx,
+ struct discover_device *orig_device,
+ struct grub2_root *root, const char *path);
+
+bool resolve_grub2_resource(struct device_handler *handler,
+ struct resource *res);
+
+/* external parser api */
+struct grub2_parser *grub2_parser_create(void *ctx);
+void grub2_parser_parse(struct grub2_parser *parser, char *buf, int len);
#endif /* GRUB2_H */
diff --git a/discover/grub2/lexer.l b/discover/grub2/lexer.l
index ab26f4b..5a4447b 100644
--- a/discover/grub2/lexer.l
+++ b/discover/grub2/lexer.l
@@ -6,6 +6,7 @@
%}
%option nounput noinput
+%option batch never-interactive
%option warn
%option noyywrap
%option stack noyy_top_state
diff --git a/discover/grub2/parser.y b/discover/grub2/parser.y
index 0d02bc1..f49cecd 100644
--- a/discover/grub2/parser.y
+++ b/discover/grub2/parser.y
@@ -220,3 +220,28 @@ void word_append(struct grub2_word *w1, struct grub2_word *w2)
w1->last->next = w2;
w1->last = w2;
}
+
+struct grub2_parser *grub2_parser_create(void *ctx)
+{
+ struct grub2_parser *parser;
+
+ parser = talloc(ctx, struct grub2_parser);
+ yylex_init_extra(parser, &parser->scanner);
+ parser->script = create_script(parser);
+
+ return parser;
+}
+
+void grub2_parser_parse(struct grub2_parser *parser, char *buf, int len)
+{
+ YY_BUFFER_STATE bufstate;
+
+ bufstate = yy_scan_bytes(buf, len - 1, parser->scanner);
+
+ yyparse(parser);
+
+ yy_delete_buffer(bufstate, parser->scanner);
+
+ script_execute(parser->script);
+}
+
diff --git a/discover/grub2/script.c b/discover/grub2/script.c
index fe65e2d..9cc0cfb 100644
--- a/discover/grub2/script.c
+++ b/discover/grub2/script.c
@@ -190,7 +190,7 @@ static void process_expansions(struct grub2_script *script,
argv->argv[i++] = word->text;
}
-int statements_execute(struct grub2_script *script,
+static int statements_execute(struct grub2_script *script,
struct grub2_statements *stmts)
{
struct grub2_statement *stmt;
OpenPOWER on IntegriCloud