diff mbox series

[v2,2/2] discover: Reimplement native-parser as a Bison parser

Message ID 20181114042015.9863-2-sam@mendozajonas.com
State Accepted
Headers show
Series [v2,1/2] discover: Remove unused params.c | expand

Commit Message

Sam Mendoza-Jonas Nov. 14, 2018, 4:20 a.m. UTC
Occasionally you look at some code and realise that a) this never gets
built, and b) even if it did it would never compile. Today's example is
native-parser.c which we must have just assumed worked for quite a
while.

The native parser has bitrotted entirely and needs to be brought up to
date. While we're here, lets take the change to implement a proper
grammar for it. This helps us reason more effectively about the parser,
lets us extend it easily in the future, and.. I wanted to write a Bison
parser too.

This implements most of the old functionality, but drops off some
smaller details like settings icons which needs some separate attention
to bring up to date.

Signed-off-by: Samuel Mendoza-Jonas <sam@mendozajonas.com>
---
v2: Extend the parser to properly handle multi-word strings and more
easily distinguish between whitespace and arguments. 

 Makefile.am                       |   1 +
 discover/Makefile.am              |   1 +
 discover/native-parser.c          | 131 -------------------
 discover/native/Makefile.am       |  54 ++++++++
 discover/native/native-lexer.l    |  65 ++++++++++
 discover/native/native-parser.y   | 202 ++++++++++++++++++++++++++++++
 discover/native/native.c          |  55 ++++++++
 discover/native/native.h          |  27 ++++
 test/parser/Makefile.am           |  10 +-
 test/parser/test-native-globals.c |  45 +++++++
 test/parser/test-native-simple.c  |  31 +++++
 test/parser/test-native-strings.c |  35 ++++++
 12 files changed, 524 insertions(+), 133 deletions(-)
 delete mode 100644 discover/native-parser.c
 create mode 100644 discover/native/Makefile.am
 create mode 100644 discover/native/native-lexer.l
 create mode 100644 discover/native/native-parser.y
 create mode 100644 discover/native/native.c
 create mode 100644 discover/native/native.h
 create mode 100644 test/parser/test-native-globals.c
 create mode 100644 test/parser/test-native-simple.c
 create mode 100644 test/parser/test-native-strings.c

Comments

Sam Mendoza-Jonas Nov. 16, 2018, 3:22 a.m. UTC | #1
On Wed, 2018-11-14 at 15:20 +1100, Samuel Mendoza-Jonas wrote:
> Occasionally you look at some code and realise that a) this never gets
> built, and b) even if it did it would never compile. Today's example is
> native-parser.c which we must have just assumed worked for quite a
> while.
> 
> The native parser has bitrotted entirely and needs to be brought up to
> date. While we're here, lets take the change to implement a proper
> grammar for it. This helps us reason more effectively about the parser,
> lets us extend it easily in the future, and.. I wanted to write a Bison
> parser too.
> 
> This implements most of the old functionality, but drops off some
> smaller details like settings icons which needs some separate attention
> to bring up to date.
> 
> Signed-off-by: Samuel Mendoza-Jonas <sam@mendozajonas.com>

Merged (with a quick fix for certain newlines) as 646d77d8

> ---
> v2: Extend the parser to properly handle multi-word strings and more
> easily distinguish between whitespace and arguments. 
> 
>  Makefile.am                       |   1 +
>  discover/Makefile.am              |   1 +
>  discover/native-parser.c          | 131 -------------------
>  discover/native/Makefile.am       |  54 ++++++++
>  discover/native/native-lexer.l    |  65 ++++++++++
>  discover/native/native-parser.y   | 202 ++++++++++++++++++++++++++++++
>  discover/native/native.c          |  55 ++++++++
>  discover/native/native.h          |  27 ++++
>  test/parser/Makefile.am           |  10 +-
>  test/parser/test-native-globals.c |  45 +++++++
>  test/parser/test-native-simple.c  |  31 +++++
>  test/parser/test-native-strings.c |  35 ++++++
>  12 files changed, 524 insertions(+), 133 deletions(-)
>  delete mode 100644 discover/native-parser.c
>  create mode 100644 discover/native/Makefile.am
>  create mode 100644 discover/native/native-lexer.l
>  create mode 100644 discover/native/native-parser.y
>  create mode 100644 discover/native/native.c
>  create mode 100644 discover/native/native.h
>  create mode 100644 test/parser/test-native-globals.c
>  create mode 100644 test/parser/test-native-simple.c
>  create mode 100644 test/parser/test-native-strings.c
> 
> diff --git a/Makefile.am b/Makefile.am
> index c0ad8397..63456ca4 100644
> --- a/Makefile.am
> +++ b/Makefile.am
> @@ -50,6 +50,7 @@ CLEANFILES =
>  
>  include lib/Makefile.am
>  include discover/grub2/Makefile.am
> +include discover/native/Makefile.am
>  include discover/Makefile.am
>  include test/Makefile.am
>  include test/lib/Makefile.am
> diff --git a/discover/Makefile.am b/discover/Makefile.am
> index d98ebec6..bfe33fad 100644
> --- a/discover/Makefile.am
> +++ b/discover/Makefile.am
> @@ -55,6 +55,7 @@ discover_pb_discover_SOURCES = \
>  
>  discover_pb_discover_LDADD = \
>  	discover/grub2/grub2-parser.ro \
> +	discover/native/native-parser.ro \
>  	discover/platform.ro \
>  	$(core_lib) \
>  	$(UDEV_LIBS)
> diff --git a/discover/native-parser.c b/discover/native-parser.c
> deleted file mode 100644
> index 08309d1b..00000000
> --- a/discover/native-parser.c
> +++ /dev/null
> @@ -1,131 +0,0 @@
> -/*
> - *  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
> - */
> -
> -#include "parser.h"
> -#include "params.h"
> -#include "paths.h"
> -
> -#include <stdlib.h>
> -#include <stdio.h>
> -#include <string.h>
> -
> -static const char *conf_filename = "/boot/petitboot.conf";
> -
> -static struct boot_option *cur_opt;
> -static struct device *dev;
> -static const char *devpath;
> -static int device_added;
> -
> -static int check_and_add_device(struct device *dev)
> -{
> -	if (!dev->icon_file)
> -		dev->icon_file = strdup(generic_icon_file(guess_device_type()));
> -
> -	return !add_device(dev);
> -}
> -
> -static int section(char *section_name)
> -{
> -	if (!device_added++ && !check_and_add_device(dev))
> -		return 0;
> -
> -	if (cur_opt) {
> -		add_boot_option(cur_opt);
> -		free_boot_option(cur_opt);
> -	}
> -
> -	cur_opt = malloc(sizeof(*cur_opt));
> -	memset(cur_opt, 0, sizeof(*cur_opt));
> -	return 1;
> -}
> -
> -
> -static void set_boot_option_parameter(struct boot_option *opt,
> -		const char *name, const char *value)
> -{
> -	if (streq(name, "name"))
> -		opt->name = strdup(value);
> -
> -	else if (streq(name, "description"))
> -		opt->description = strdup(value);
> -
> -	else if (streq(name, "image"))
> -		opt->boot_image_file = resolve_path(value, devpath);
> -
> -	else if (streq(name, "icon"))
> -		opt->icon_file = resolve_path(value, devpath);
> -
> -	else if (streq(name, "initrd"))
> -		opt->initrd_file =resolve_path(value, devpath);
> -
> -	else if (streq(name, "args"))
> -		opt->boot_args = strdup(value);
> -
> -	else
> -		fprintf(stderr, "Unknown parameter %s\n", name);
> -}
> -
> -static void set_device_parameter(struct device *dev,
> -		const char *name, const char *value)
> -{
> -	if (streq(name, "name"))
> -		dev->name = strdup(value);
> -
> -	else if (streq(name, "description"))
> -		dev->description = strdup(value);
> -
> -	else if (streq(name, "icon"))
> -		dev->icon_file = resolve_path(value, devpath);
> -}
> -
> -static int parameter(char *param_name, char *param_value)
> -{
> -	if (cur_opt)
> -		set_boot_option_parameter(cur_opt, param_name, param_value);
> -	else
> -		set_device_parameter(dev, param_name, param_value);
> -	return 1;
> -}
> -
> -
> -static int native_parse(const char *device)
> -{
> -	char *filepath;
> -	int rc;
> -
> -	filepath = resolve_path(conf_filename, device);
> -
> -	cur_opt = NULL;
> -	dev = malloc(sizeof(*dev));
> -	memset(dev, 0, sizeof(*dev));
> -	dev->id = strdup(device);
> -
> -	rc = pm_process(filepath, section, parameter);
> -	if (!rc)
> -		return 0;
> -
> -	if (cur_opt) {
> -		add_boot_option(cur_opt);
> -		free_boot_option(cur_opt);
> -	}
> -
> -	cur_opt = NULL;
> -
> -	free(filepath);
> -
> -	return 1;
> -}
> -
> -define_parser(native, native_parse);
> diff --git a/discover/native/Makefile.am b/discover/native/Makefile.am
> new file mode 100644
> index 00000000..f120e704
> --- /dev/null
> +++ b/discover/native/Makefile.am
> @@ -0,0 +1,54 @@
> +#  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
> +#
> +
> +noinst_PROGRAMS += discover/native/native-parser.ro
> +
> +discover_native_native_parser_ro_SOURCES = \
> +	discover/native/native.h \
> +	discover/native/native.c \
> +	discover/native/native-lexer.l \
> +	discover/native/native-parser.y
> +
> +BUILT_SOURCES += \
> +	discover/native/native-parser.c \
> +	discover/native/native-parser.h \
> +	discover/native/native-lexer.h \
> +	discover/native/native-lexer.c
> +
> +CLEANFILES += \
> +	discover/native/native-parser.c \
> +	discover/native/native-parser.h \
> +	discover/native/native-lexer.c \
> +	discover/native/native-lexer.h
> +
> +discover_native_native_parser_ro_CPPFLAGS = \
> +	$(AM_CPPFLAGS) \
> +	-I$(top_srcdir)/discover/native \
> +	-I$(top_builddir)/discover/native
> +
> +discover_native_native_parser_ro_LINK = \
> +	$(LD) -r -o $@
> +
> +# ylwrap doesn't handle flex header files well; use our own rule here.
> +discover/native/native-lexer.h discover/native/native-lexer.c: \
> +		$(top_srcdir)/discover/native/native-lexer.l
> +	$(AM_V_LEX)$(LEXCOMPILE) --header-file=discover/native/native-lexer.h \
> +				       -o discover/native/native-lexer.c $^
> +
> +# We need to loosen our warnings for the generated lexer code.
> +discover/native/%native-lexer.o discover/native/native-lexer.o: \
> +		AM_CFLAGS += -Wno-unused-parameter -Wno-missing-prototypes \
> +			       -Wno-missing-declarations -Wno-sign-compare
> +
> +$(discover_native_native_parser_ro_OBJECTS): discover/native/native-parser.h
> diff --git a/discover/native/native-lexer.l b/discover/native/native-lexer.l
> new file mode 100644
> index 00000000..53fc7285
> --- /dev/null
> +++ b/discover/native/native-lexer.l
> @@ -0,0 +1,65 @@
> +%{
> +#include "native.h"
> +#include "native-parser.h"
> +#include <talloc/talloc.h>
> +
> +#define YYSTYPE NSTYPE
> +
> +void yyerror(struct native_parser *parser, const char *fmt, ...);
> +%}
> +
> +%option nounput noinput
> +%option batch never-interactive
> +%option warn
> +%option noyywrap
> +%option reentrant
> +%option bison-bridge
> +%option yylineno
> +%option noyyalloc noyyfree noyyrealloc
> +%option extra-type="struct native_parser *"
> +%option prefix="n"
> +
> +%x label
> +%x args
> +
> +DELIM	[ \t]+
> +NUMBER	0|[1-9][0-9]*
> +WORDS	[^\n]+
> +NEWLINE [\n]+
> +
> +%%
> +
> +name		{ BEGIN(label); return TOKEN_NAME; }
> +image		{ BEGIN(label); return TOKEN_IMAGE; }
> +initrd		{ BEGIN(label); return TOKEN_INITRD; }
> +args		{ BEGIN(label); return TOKEN_ARGS; }
> +dtb		{ BEGIN(label); return TOKEN_DTB; }
> +description	{ BEGIN(label); return TOKEN_DESCRIPTION; }
> +default		{ BEGIN(label); return TOKEN_DEFAULT; }
> +dev_description { BEGIN(label); return TOKEN_DEV_DESCRIPTION; }
> +{DELIM}		{ ; }
> +{NEWLINE}	{ ; }
> +<label>{DELIM}	{ BEGIN(args); return TOKEN_DELIM; }
> +<args>{WORDS}	{ yylval->word = strdup(yytext); return TOKEN_WORD; }
> +<args>{NEWLINE}	{ BEGIN(INITIAL); return TOKEN_NEWLINE; }
> +
> +%%
> +
> +struct native_parser;
> +
> +void *yyalloc(size_t bytes, void *yyscanner)
> +{
> +	struct native_parser *parser = yyget_extra(yyscanner);
> +	return talloc_size(parser, bytes);
> +}
> +
> +void *yyrealloc(void *ptr, size_t bytes, void *yyscanner)
> +{
> +	struct native_parser *parser = yyget_extra(yyscanner);
> +	return talloc_realloc_size(parser, ptr, bytes);
> +}
> +
> +void yyfree(void *ptr, void *yyscanner __attribute__((unused)))
> +{
> +	talloc_free(ptr);
> +}
> diff --git a/discover/native/native-parser.y b/discover/native/native-parser.y
> new file mode 100644
> index 00000000..49a1b4ae
> --- /dev/null
> +++ b/discover/native/native-parser.y
> @@ -0,0 +1,202 @@
> +
> +%pure-parser
> +%lex-param { nscan_t scanner }
> +%parse-param { struct native_parser *parser }
> +%parse-param { void *scanner }
> +%error-verbose
> +
> +%define api.prefix {n}
> +%{
> +#include <talloc/talloc.h>
> +#include <log/log.h>
> +#include "discover/resource.h"
> +#include "discover/parser-utils.h"
> +
> +#include "native.h"
> +
> +void yyerror(struct native_parser *parser, void *scanner, const char *fmt, ...);
> +%}
> +
> +%union {
> +	char	*word;
> +	int	num;
> +}
> +
> +%token	<word>	TOKEN_WORD
> +%token	<num>	TOKEN_NUMBER
> +%token	<num>	TOKEN_DELIM
> +
> +%token TOKEN_DEFAULT
> +%token TOKEN_DEV_DESCRIPTION
> +
> +%token TOKEN_NAME
> +%token TOKEN_IMAGE
> +%token TOKEN_INITRD
> +%token TOKEN_ARGS
> +%token TOKEN_DTB
> +%token TOKEN_DESCRIPTION
> +%token TOKEN_NEWLINE
> +
> +%{
> +#include "native-lexer.h"
> +%}
> +
> +%%
> +
> +native:
> +      globals boot_options { native_parser_finish(parser); }
> +      | boot_options { native_parser_finish(parser); }
> +      ;
> +
> +globals: globals global
> +       | global
> +       ;
> +
> +global: TOKEN_DEFAULT delims TOKEN_WORD TOKEN_NEWLINE {
> +		if (parser->default_name)
> +			pb_log_fn("Duplicate default option, ignoring\n");
> +		else
> +			parser->default_name = talloc_strdup(parser, $3);
> +	}
> +	| TOKEN_DEV_DESCRIPTION delims TOKEN_WORD TOKEN_NEWLINE {
> +		native_append_string(parser,
> +			&parser->ctx->device->device->description, $3);
> +	}
> +	;
> +
> +boot_options:
> +	    boot_options option
> +	    | option
> +	    ;
> +
> +option: name params
> +     ;
> +
> +name: TOKEN_NAME delims TOKEN_WORD TOKEN_NEWLINE {
> +		native_parser_create_option(parser, $3);
> +	}
> +	;
> +
> +params: params param
> +      | param
> +      ;
> +
> +param: statement TOKEN_NEWLINE
> +
> +statement: TOKEN_IMAGE delims TOKEN_WORD {
> +		native_set_resource(parser, &parser->opt->boot_image, $3);
> +	}
> +	| TOKEN_INITRD delims TOKEN_WORD {
> +		native_set_resource(parser, &parser->opt->initrd, $3);
> +	}
> +	| TOKEN_DTB delims TOKEN_WORD  {
> +		native_set_resource(parser, &parser->opt->dtb, $3);
> +	}
> +	| TOKEN_ARGS delims TOKEN_WORD {
> +		native_append_string(parser, &parser->opt->option->boot_args, $3);
> +	}
> +	| TOKEN_DESCRIPTION delims TOKEN_WORD  {
> +		native_append_string(parser, &parser->opt->option->description, $3);
> +	}
> +	;
> +
> +delims: delims TOKEN_DELIM
> +      | TOKEN_DELIM
> +      ;
> +
> +%%
> +
> +void yyerror(struct native_parser *parser, void *scanner, const char *fmt, ...)
> +{
> +	const char *str;
> +	va_list ap;
> +
> +	va_start(ap, fmt);
> +	str = talloc_vasprintf(parser, fmt, ap);
> +	va_end(ap);
> +
> +	pb_log("parse error: %d('%s'): %s\n", nget_lineno(scanner),
> +					nget_text(scanner), str);
> +}
> +
> +void native_parser_finish(struct native_parser *parser)
> +{
> +	if (parser->opt) {
> +		discover_context_add_boot_option(parser->ctx, parser->opt);
> +		parser->opt = NULL;
> +	}
> +}
> +
> +void native_set_resource(struct native_parser *parser,
> +		struct resource ** resource, const char *path)
> +{
> +	if (*resource) {
> +		pb_log_fn("Duplicate resource at line %d: %s\n",
> +			nget_lineno(parser->scanner), path);
> +		return;
> +	}
> +
> +	*resource = create_devpath_resource(parser->opt, parser->opt->device,
> +					path);
> +}
> +
> +void native_append_string(struct native_parser *parser,
> +		char **str, const char *append)
> +{
> +	if (*str)
> +		*str = talloc_asprintf_append(*str, "%s", append);
> +	else
> +		*str = talloc_strdup(parser->opt, append);
> +}
> +
> +void native_parser_create_option(struct native_parser *parser, const char *name)
> +{
> +	struct discover_boot_option *opt = parser->opt;
> +
> +	if (opt)
> +		native_parser_finish(parser);
> +
> +	opt = discover_boot_option_create(parser->ctx, parser->ctx->device);
> +	opt->option->name = talloc_strdup(opt, name);
> +	opt->option->id = talloc_asprintf(opt, "%s@%p",
> +			parser->ctx->device->device->id, opt);
> +	opt->option->type = DISCOVER_BOOT_OPTION;
> +	opt->option->is_default = parser->default_name &&
> +				streq(parser->default_name, name);
> +	parser->opt = opt;
> +	return;
> +}
> +
> +struct native_parser *native_parser_create(struct discover_context *ctx)
> +{
> +	struct native_parser *parser;
> +
> +	parser = talloc_zero(ctx, struct native_parser);
> +	parser->ctx = ctx;
> +	nlex_init_extra(parser, &parser->scanner);
> +
> +	return parser;
> +}
> +
> +void native_parser_parse(struct native_parser *parser, const char *filename,
> +	char *buf, int len)
> +{
> +	YY_BUFFER_STATE bufstate;
> +	int rc;
> +
> +	if (!len)
> +		return;
> +
> +	parser->filename = filename;
> +
> +	bufstate = n_scan_bytes(buf, len - 1, parser->scanner);
> +	nset_lineno(1, parser->scanner);
> +
> +	rc = nparse(parser, parser->scanner);
> +
> +	if (rc)
> +		pb_log("Failed to parse %s\n", filename);
> +
> +	n_delete_buffer(bufstate, parser->scanner);
> +}
> +
> diff --git a/discover/native/native.c b/discover/native/native.c
> new file mode 100644
> index 00000000..964ad1d4
> --- /dev/null
> +++ b/discover/native/native.c
> @@ -0,0 +1,55 @@
> +#include <assert.h>
> +#include <string.h>
> +#include <i18n/i18n.h>
> +
> +#include <talloc/talloc.h>
> +#include <url/url.h>
> +
> +#include <discover/resource.h>
> +#include <discover/parser.h>
> +#include <discover/parser-utils.h>
> +
> +#include "native.h"
> +
> +static const char *const native_conf_files[] = {
> +	"/boot/petitboot.conf",
> +	"/petitboot.conf",
> +	NULL
> +};
> +
> +static int native_parse(struct discover_context *dc)
> +{
> +	const char * const *filename;
> +	struct native_parser *parser;
> +	int len, rc;
> +	char *buf;
> +
> +	/* Support block device boot only at present */
> +	if (dc->event)
> +		return -1;
> +
> +	for (filename = native_conf_files; *filename; filename++) {
> +		rc = parser_request_file(dc, dc->device, *filename, &buf, &len);
> +		if (rc)
> +			continue;
> +
> +		parser = native_parser_create(dc);
> +		native_parser_parse(parser, *filename, buf, len);
> +		device_handler_status_dev_info(dc->handler, dc->device,
> +				_("Parsed native configuration from %s"),
> +				*filename);
> +		talloc_free(buf);
> +		talloc_free(parser);
> +		break;
> +	}
> +
> +	return 0;
> +}
> +
> +static struct parser native_parser = {
> +	.name			= "native",
> +	.parse			= native_parse,
> +	.resolve_resource	= resolve_devpath_resource,
> +};
> +
> +register_parser(native_parser);
> diff --git a/discover/native/native.h b/discover/native/native.h
> new file mode 100644
> index 00000000..1cf7d6aa
> --- /dev/null
> +++ b/discover/native/native.h
> @@ -0,0 +1,27 @@
> +#ifndef NATIVE_H
> +#define NATIVE_H
> +
> +#include <discover/device-handler.h>
> +
> +struct native_parser {
> +	struct discover_context		*ctx;
> +	struct discover_boot_option 	*opt;
> +	void				*scanner;
> +	const char			*filename;
> +	char				*default_name;
> +};
> +
> +void native_parser_finish(struct native_parser *parser);
> +void native_set_resource(struct native_parser *parser, struct resource **,
> +		const char *path);
> +void native_append_string(struct native_parser *parser,
> +		char **str, const char *append);
> +void native_parser_create_option(struct native_parser *parser,
> +		const char *name);
> +
> +/* external parser api */
> +struct native_parser *native_parser_create(struct discover_context *ctx);
> +void native_parser_parse(struct native_parser *parser, const char *filename,
> +		char *buf, int len);
> +#endif /* NATIVE_H */
> +
> diff --git a/test/parser/Makefile.am b/test/parser/Makefile.am
> index 363933e3..1a227c8a 100644
> --- a/test/parser/Makefile.am
> +++ b/test/parser/Makefile.am
> @@ -87,7 +87,10 @@ parser_TESTS = \
>  	test/parser/test-syslinux-single-yocto \
>  	test/parser/test-syslinux-global-append \
>  	test/parser/test-syslinux-explicit \
> -	test/parser/test-syslinux-nested-config
> +	test/parser/test-syslinux-nested-config \
> +	test/parser/test-native-simple \
> +	test/parser/test-native-globals \
> +	test/parser/test-native-strings
>  
>  
>  TESTS += $(parser_TESTS)
> @@ -133,13 +136,16 @@ test_parser_libtest_ro_SOURCES = \
>  	discover/parser-conf.c \
>  	discover/user-event.c \
>  	discover/event.c \
> -	$(discover_grub2_grub2_parser_ro_SOURCES)
> +	$(discover_grub2_grub2_parser_ro_SOURCES) \
> +	$(discover_native_native_parser_ro_SOURCES)
>  
>  test_parser_libtest_ro_CPPFLAGS = \
>  	$(AM_CPPFLAGS) \
>  	-I$(top_srcdir)/discover \
>  	-I$(top_srcdir)/discover/grub2 \
>  	-I$(top_builddir)/discover/grub2 \
> +	-I$(top_srcdir)/discover/native \
> +	-I$(top_builddir)/discover/native \
>  	-DPETITBOOT_TEST \
>  	-DLOCAL_STATE_DIR='"$(localstatedir)"' \
>  	-DTEST_CONF_BASE='"$(top_srcdir)/test/parser/data"'
> diff --git a/test/parser/test-native-globals.c b/test/parser/test-native-globals.c
> new file mode 100644
> index 00000000..0361e03b
> --- /dev/null
> +++ b/test/parser/test-native-globals.c
> @@ -0,0 +1,45 @@
> +#include "parser-test.h"
> +
> +#if 0 /* PARSER_EMBEDDED_CONFIG */
> +
> +default first
> +
> +name first
> +image /vmlinuz
> +args console=hvc0
> +initrd /initrd
> +
> +name second
> +image /boot/vmlinuz
> +args console=tty0
> +initrd /boot/initrd
> +
> +#endif
> +
> +void run_test(struct parser_test *test)
> +{
> +	struct discover_boot_option *opt;
> +	struct discover_context *ctx;
> +
> +	test_read_conf_embedded(test, "/boot/petitboot.conf");
> +
> +	test_run_parser(test, "native");
> +
> +	ctx = test->ctx;
> +
> +	check_boot_option_count(ctx, 2);
> +
> +	opt = get_boot_option(ctx, 0);
> +
> +	check_name(opt, "first");
> +	check_resolved_local_resource(opt->boot_image, ctx->device, "/vmlinuz");
> +	check_args(opt, "console=hvc0");
> +	check_resolved_local_resource(opt->initrd, ctx->device, "/initrd");
> +	check_is_default(opt);
> +
> +	opt = get_boot_option(ctx, 1);
> +	check_name(opt, "second");
> +	check_resolved_local_resource(opt->boot_image, ctx->device, "/boot/vmlinuz");
> +	check_args(opt, "console=tty0");
> +	check_resolved_local_resource(opt->initrd, ctx->device, "/boot/initrd");
> +}
> diff --git a/test/parser/test-native-simple.c b/test/parser/test-native-simple.c
> new file mode 100644
> index 00000000..f817228e
> --- /dev/null
> +++ b/test/parser/test-native-simple.c
> @@ -0,0 +1,31 @@
> +#include "parser-test.h"
> +
> +#if 0 /* PARSER_EMBEDDED_CONFIG */
> +
> +name native-option
> +image /vmlinuz
> +args console=hvc0
> +initrd /initrd
> +
> +#endif
> +
> +void run_test(struct parser_test *test)
> +{
> +	struct discover_boot_option *opt;
> +	struct discover_context *ctx;
> +
> +	test_read_conf_embedded(test, "/boot/petitboot.conf");
> +
> +	test_run_parser(test, "native");
> +
> +	ctx = test->ctx;
> +
> +	check_boot_option_count(ctx, 1);
> +
> +	opt = get_boot_option(ctx, 0);
> +
> +	check_name(opt, "native-option");
> +	check_resolved_local_resource(opt->boot_image, ctx->device, "/vmlinuz");
> +	check_args(opt, "console=hvc0");
> +	check_resolved_local_resource(opt->initrd, ctx->device, "/initrd");
> +}
> diff --git a/test/parser/test-native-strings.c b/test/parser/test-native-strings.c
> new file mode 100644
> index 00000000..33d242af
> --- /dev/null
> +++ b/test/parser/test-native-strings.c
> @@ -0,0 +1,35 @@
> +#include "parser-test.h"
> +
> +#if 0 /* PARSER_EMBEDDED_CONFIG */
> +
> +name A longer name
> +
> +image /some/kernel
> +initrd /some/other/initrd
> +args console=tty0 console=hvc0 debug 
> +dtb /a/dtb
> +description Contains a number of words
> +
> +#endif
> +
> +void run_test(struct parser_test *test)
> +{
> +	struct discover_boot_option *opt;
> +	struct discover_context *ctx;
> +
> +	test_read_conf_embedded(test, "/boot/petitboot.conf");
> +
> +	test_run_parser(test, "native");
> +
> +	ctx = test->ctx;
> +
> +	check_boot_option_count(ctx, 1);
> +
> +	opt = get_boot_option(ctx, 0);
> +
> +	check_name(opt, "A longer name");
> +	check_resolved_local_resource(opt->boot_image, ctx->device, "/some/kernel");
> +	check_args(opt, "console=tty0 console=hvc0 debug ");
> +	check_resolved_local_resource(opt->initrd, ctx->device, "/some/other/initrd");
> +	check_resolved_local_resource(opt->dtb, ctx->device, "/a/dtb");
> +}
diff mbox series

Patch

diff --git a/Makefile.am b/Makefile.am
index c0ad8397..63456ca4 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -50,6 +50,7 @@  CLEANFILES =
 
 include lib/Makefile.am
 include discover/grub2/Makefile.am
+include discover/native/Makefile.am
 include discover/Makefile.am
 include test/Makefile.am
 include test/lib/Makefile.am
diff --git a/discover/Makefile.am b/discover/Makefile.am
index d98ebec6..bfe33fad 100644
--- a/discover/Makefile.am
+++ b/discover/Makefile.am
@@ -55,6 +55,7 @@  discover_pb_discover_SOURCES = \
 
 discover_pb_discover_LDADD = \
 	discover/grub2/grub2-parser.ro \
+	discover/native/native-parser.ro \
 	discover/platform.ro \
 	$(core_lib) \
 	$(UDEV_LIBS)
diff --git a/discover/native-parser.c b/discover/native-parser.c
deleted file mode 100644
index 08309d1b..00000000
--- a/discover/native-parser.c
+++ /dev/null
@@ -1,131 +0,0 @@ 
-/*
- *  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
- */
-
-#include "parser.h"
-#include "params.h"
-#include "paths.h"
-
-#include <stdlib.h>
-#include <stdio.h>
-#include <string.h>
-
-static const char *conf_filename = "/boot/petitboot.conf";
-
-static struct boot_option *cur_opt;
-static struct device *dev;
-static const char *devpath;
-static int device_added;
-
-static int check_and_add_device(struct device *dev)
-{
-	if (!dev->icon_file)
-		dev->icon_file = strdup(generic_icon_file(guess_device_type()));
-
-	return !add_device(dev);
-}
-
-static int section(char *section_name)
-{
-	if (!device_added++ && !check_and_add_device(dev))
-		return 0;
-
-	if (cur_opt) {
-		add_boot_option(cur_opt);
-		free_boot_option(cur_opt);
-	}
-
-	cur_opt = malloc(sizeof(*cur_opt));
-	memset(cur_opt, 0, sizeof(*cur_opt));
-	return 1;
-}
-
-
-static void set_boot_option_parameter(struct boot_option *opt,
-		const char *name, const char *value)
-{
-	if (streq(name, "name"))
-		opt->name = strdup(value);
-
-	else if (streq(name, "description"))
-		opt->description = strdup(value);
-
-	else if (streq(name, "image"))
-		opt->boot_image_file = resolve_path(value, devpath);
-
-	else if (streq(name, "icon"))
-		opt->icon_file = resolve_path(value, devpath);
-
-	else if (streq(name, "initrd"))
-		opt->initrd_file =resolve_path(value, devpath);
-
-	else if (streq(name, "args"))
-		opt->boot_args = strdup(value);
-
-	else
-		fprintf(stderr, "Unknown parameter %s\n", name);
-}
-
-static void set_device_parameter(struct device *dev,
-		const char *name, const char *value)
-{
-	if (streq(name, "name"))
-		dev->name = strdup(value);
-
-	else if (streq(name, "description"))
-		dev->description = strdup(value);
-
-	else if (streq(name, "icon"))
-		dev->icon_file = resolve_path(value, devpath);
-}
-
-static int parameter(char *param_name, char *param_value)
-{
-	if (cur_opt)
-		set_boot_option_parameter(cur_opt, param_name, param_value);
-	else
-		set_device_parameter(dev, param_name, param_value);
-	return 1;
-}
-
-
-static int native_parse(const char *device)
-{
-	char *filepath;
-	int rc;
-
-	filepath = resolve_path(conf_filename, device);
-
-	cur_opt = NULL;
-	dev = malloc(sizeof(*dev));
-	memset(dev, 0, sizeof(*dev));
-	dev->id = strdup(device);
-
-	rc = pm_process(filepath, section, parameter);
-	if (!rc)
-		return 0;
-
-	if (cur_opt) {
-		add_boot_option(cur_opt);
-		free_boot_option(cur_opt);
-	}
-
-	cur_opt = NULL;
-
-	free(filepath);
-
-	return 1;
-}
-
-define_parser(native, native_parse);
diff --git a/discover/native/Makefile.am b/discover/native/Makefile.am
new file mode 100644
index 00000000..f120e704
--- /dev/null
+++ b/discover/native/Makefile.am
@@ -0,0 +1,54 @@ 
+#  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
+#
+
+noinst_PROGRAMS += discover/native/native-parser.ro
+
+discover_native_native_parser_ro_SOURCES = \
+	discover/native/native.h \
+	discover/native/native.c \
+	discover/native/native-lexer.l \
+	discover/native/native-parser.y
+
+BUILT_SOURCES += \
+	discover/native/native-parser.c \
+	discover/native/native-parser.h \
+	discover/native/native-lexer.h \
+	discover/native/native-lexer.c
+
+CLEANFILES += \
+	discover/native/native-parser.c \
+	discover/native/native-parser.h \
+	discover/native/native-lexer.c \
+	discover/native/native-lexer.h
+
+discover_native_native_parser_ro_CPPFLAGS = \
+	$(AM_CPPFLAGS) \
+	-I$(top_srcdir)/discover/native \
+	-I$(top_builddir)/discover/native
+
+discover_native_native_parser_ro_LINK = \
+	$(LD) -r -o $@
+
+# ylwrap doesn't handle flex header files well; use our own rule here.
+discover/native/native-lexer.h discover/native/native-lexer.c: \
+		$(top_srcdir)/discover/native/native-lexer.l
+	$(AM_V_LEX)$(LEXCOMPILE) --header-file=discover/native/native-lexer.h \
+				       -o discover/native/native-lexer.c $^
+
+# We need to loosen our warnings for the generated lexer code.
+discover/native/%native-lexer.o discover/native/native-lexer.o: \
+		AM_CFLAGS += -Wno-unused-parameter -Wno-missing-prototypes \
+			       -Wno-missing-declarations -Wno-sign-compare
+
+$(discover_native_native_parser_ro_OBJECTS): discover/native/native-parser.h
diff --git a/discover/native/native-lexer.l b/discover/native/native-lexer.l
new file mode 100644
index 00000000..53fc7285
--- /dev/null
+++ b/discover/native/native-lexer.l
@@ -0,0 +1,65 @@ 
+%{
+#include "native.h"
+#include "native-parser.h"
+#include <talloc/talloc.h>
+
+#define YYSTYPE NSTYPE
+
+void yyerror(struct native_parser *parser, const char *fmt, ...);
+%}
+
+%option nounput noinput
+%option batch never-interactive
+%option warn
+%option noyywrap
+%option reentrant
+%option bison-bridge
+%option yylineno
+%option noyyalloc noyyfree noyyrealloc
+%option extra-type="struct native_parser *"
+%option prefix="n"
+
+%x label
+%x args
+
+DELIM	[ \t]+
+NUMBER	0|[1-9][0-9]*
+WORDS	[^\n]+
+NEWLINE [\n]+
+
+%%
+
+name		{ BEGIN(label); return TOKEN_NAME; }
+image		{ BEGIN(label); return TOKEN_IMAGE; }
+initrd		{ BEGIN(label); return TOKEN_INITRD; }
+args		{ BEGIN(label); return TOKEN_ARGS; }
+dtb		{ BEGIN(label); return TOKEN_DTB; }
+description	{ BEGIN(label); return TOKEN_DESCRIPTION; }
+default		{ BEGIN(label); return TOKEN_DEFAULT; }
+dev_description { BEGIN(label); return TOKEN_DEV_DESCRIPTION; }
+{DELIM}		{ ; }
+{NEWLINE}	{ ; }
+<label>{DELIM}	{ BEGIN(args); return TOKEN_DELIM; }
+<args>{WORDS}	{ yylval->word = strdup(yytext); return TOKEN_WORD; }
+<args>{NEWLINE}	{ BEGIN(INITIAL); return TOKEN_NEWLINE; }
+
+%%
+
+struct native_parser;
+
+void *yyalloc(size_t bytes, void *yyscanner)
+{
+	struct native_parser *parser = yyget_extra(yyscanner);
+	return talloc_size(parser, bytes);
+}
+
+void *yyrealloc(void *ptr, size_t bytes, void *yyscanner)
+{
+	struct native_parser *parser = yyget_extra(yyscanner);
+	return talloc_realloc_size(parser, ptr, bytes);
+}
+
+void yyfree(void *ptr, void *yyscanner __attribute__((unused)))
+{
+	talloc_free(ptr);
+}
diff --git a/discover/native/native-parser.y b/discover/native/native-parser.y
new file mode 100644
index 00000000..49a1b4ae
--- /dev/null
+++ b/discover/native/native-parser.y
@@ -0,0 +1,202 @@ 
+
+%pure-parser
+%lex-param { nscan_t scanner }
+%parse-param { struct native_parser *parser }
+%parse-param { void *scanner }
+%error-verbose
+
+%define api.prefix {n}
+%{
+#include <talloc/talloc.h>
+#include <log/log.h>
+#include "discover/resource.h"
+#include "discover/parser-utils.h"
+
+#include "native.h"
+
+void yyerror(struct native_parser *parser, void *scanner, const char *fmt, ...);
+%}
+
+%union {
+	char	*word;
+	int	num;
+}
+
+%token	<word>	TOKEN_WORD
+%token	<num>	TOKEN_NUMBER
+%token	<num>	TOKEN_DELIM
+
+%token TOKEN_DEFAULT
+%token TOKEN_DEV_DESCRIPTION
+
+%token TOKEN_NAME
+%token TOKEN_IMAGE
+%token TOKEN_INITRD
+%token TOKEN_ARGS
+%token TOKEN_DTB
+%token TOKEN_DESCRIPTION
+%token TOKEN_NEWLINE
+
+%{
+#include "native-lexer.h"
+%}
+
+%%
+
+native:
+      globals boot_options { native_parser_finish(parser); }
+      | boot_options { native_parser_finish(parser); }
+      ;
+
+globals: globals global
+       | global
+       ;
+
+global: TOKEN_DEFAULT delims TOKEN_WORD TOKEN_NEWLINE {
+		if (parser->default_name)
+			pb_log_fn("Duplicate default option, ignoring\n");
+		else
+			parser->default_name = talloc_strdup(parser, $3);
+	}
+	| TOKEN_DEV_DESCRIPTION delims TOKEN_WORD TOKEN_NEWLINE {
+		native_append_string(parser,
+			&parser->ctx->device->device->description, $3);
+	}
+	;
+
+boot_options:
+	    boot_options option
+	    | option
+	    ;
+
+option: name params
+     ;
+
+name: TOKEN_NAME delims TOKEN_WORD TOKEN_NEWLINE {
+		native_parser_create_option(parser, $3);
+	}
+	;
+
+params: params param
+      | param
+      ;
+
+param: statement TOKEN_NEWLINE
+
+statement: TOKEN_IMAGE delims TOKEN_WORD {
+		native_set_resource(parser, &parser->opt->boot_image, $3);
+	}
+	| TOKEN_INITRD delims TOKEN_WORD {
+		native_set_resource(parser, &parser->opt->initrd, $3);
+	}
+	| TOKEN_DTB delims TOKEN_WORD  {
+		native_set_resource(parser, &parser->opt->dtb, $3);
+	}
+	| TOKEN_ARGS delims TOKEN_WORD {
+		native_append_string(parser, &parser->opt->option->boot_args, $3);
+	}
+	| TOKEN_DESCRIPTION delims TOKEN_WORD  {
+		native_append_string(parser, &parser->opt->option->description, $3);
+	}
+	;
+
+delims: delims TOKEN_DELIM
+      | TOKEN_DELIM
+      ;
+
+%%
+
+void yyerror(struct native_parser *parser, void *scanner, const char *fmt, ...)
+{
+	const char *str;
+	va_list ap;
+
+	va_start(ap, fmt);
+	str = talloc_vasprintf(parser, fmt, ap);
+	va_end(ap);
+
+	pb_log("parse error: %d('%s'): %s\n", nget_lineno(scanner),
+					nget_text(scanner), str);
+}
+
+void native_parser_finish(struct native_parser *parser)
+{
+	if (parser->opt) {
+		discover_context_add_boot_option(parser->ctx, parser->opt);
+		parser->opt = NULL;
+	}
+}
+
+void native_set_resource(struct native_parser *parser,
+		struct resource ** resource, const char *path)
+{
+	if (*resource) {
+		pb_log_fn("Duplicate resource at line %d: %s\n",
+			nget_lineno(parser->scanner), path);
+		return;
+	}
+
+	*resource = create_devpath_resource(parser->opt, parser->opt->device,
+					path);
+}
+
+void native_append_string(struct native_parser *parser,
+		char **str, const char *append)
+{
+	if (*str)
+		*str = talloc_asprintf_append(*str, "%s", append);
+	else
+		*str = talloc_strdup(parser->opt, append);
+}
+
+void native_parser_create_option(struct native_parser *parser, const char *name)
+{
+	struct discover_boot_option *opt = parser->opt;
+
+	if (opt)
+		native_parser_finish(parser);
+
+	opt = discover_boot_option_create(parser->ctx, parser->ctx->device);
+	opt->option->name = talloc_strdup(opt, name);
+	opt->option->id = talloc_asprintf(opt, "%s@%p",
+			parser->ctx->device->device->id, opt);
+	opt->option->type = DISCOVER_BOOT_OPTION;
+	opt->option->is_default = parser->default_name &&
+				streq(parser->default_name, name);
+	parser->opt = opt;
+	return;
+}
+
+struct native_parser *native_parser_create(struct discover_context *ctx)
+{
+	struct native_parser *parser;
+
+	parser = talloc_zero(ctx, struct native_parser);
+	parser->ctx = ctx;
+	nlex_init_extra(parser, &parser->scanner);
+
+	return parser;
+}
+
+void native_parser_parse(struct native_parser *parser, const char *filename,
+	char *buf, int len)
+{
+	YY_BUFFER_STATE bufstate;
+	int rc;
+
+	if (!len)
+		return;
+
+	parser->filename = filename;
+
+	bufstate = n_scan_bytes(buf, len - 1, parser->scanner);
+	nset_lineno(1, parser->scanner);
+
+	rc = nparse(parser, parser->scanner);
+
+	if (rc)
+		pb_log("Failed to parse %s\n", filename);
+
+	n_delete_buffer(bufstate, parser->scanner);
+}
+
diff --git a/discover/native/native.c b/discover/native/native.c
new file mode 100644
index 00000000..964ad1d4
--- /dev/null
+++ b/discover/native/native.c
@@ -0,0 +1,55 @@ 
+#include <assert.h>
+#include <string.h>
+#include <i18n/i18n.h>
+
+#include <talloc/talloc.h>
+#include <url/url.h>
+
+#include <discover/resource.h>
+#include <discover/parser.h>
+#include <discover/parser-utils.h>
+
+#include "native.h"
+
+static const char *const native_conf_files[] = {
+	"/boot/petitboot.conf",
+	"/petitboot.conf",
+	NULL
+};
+
+static int native_parse(struct discover_context *dc)
+{
+	const char * const *filename;
+	struct native_parser *parser;
+	int len, rc;
+	char *buf;
+
+	/* Support block device boot only at present */
+	if (dc->event)
+		return -1;
+
+	for (filename = native_conf_files; *filename; filename++) {
+		rc = parser_request_file(dc, dc->device, *filename, &buf, &len);
+		if (rc)
+			continue;
+
+		parser = native_parser_create(dc);
+		native_parser_parse(parser, *filename, buf, len);
+		device_handler_status_dev_info(dc->handler, dc->device,
+				_("Parsed native configuration from %s"),
+				*filename);
+		talloc_free(buf);
+		talloc_free(parser);
+		break;
+	}
+
+	return 0;
+}
+
+static struct parser native_parser = {
+	.name			= "native",
+	.parse			= native_parse,
+	.resolve_resource	= resolve_devpath_resource,
+};
+
+register_parser(native_parser);
diff --git a/discover/native/native.h b/discover/native/native.h
new file mode 100644
index 00000000..1cf7d6aa
--- /dev/null
+++ b/discover/native/native.h
@@ -0,0 +1,27 @@ 
+#ifndef NATIVE_H
+#define NATIVE_H
+
+#include <discover/device-handler.h>
+
+struct native_parser {
+	struct discover_context		*ctx;
+	struct discover_boot_option 	*opt;
+	void				*scanner;
+	const char			*filename;
+	char				*default_name;
+};
+
+void native_parser_finish(struct native_parser *parser);
+void native_set_resource(struct native_parser *parser, struct resource **,
+		const char *path);
+void native_append_string(struct native_parser *parser,
+		char **str, const char *append);
+void native_parser_create_option(struct native_parser *parser,
+		const char *name);
+
+/* external parser api */
+struct native_parser *native_parser_create(struct discover_context *ctx);
+void native_parser_parse(struct native_parser *parser, const char *filename,
+		char *buf, int len);
+#endif /* NATIVE_H */
+
diff --git a/test/parser/Makefile.am b/test/parser/Makefile.am
index 363933e3..1a227c8a 100644
--- a/test/parser/Makefile.am
+++ b/test/parser/Makefile.am
@@ -87,7 +87,10 @@  parser_TESTS = \
 	test/parser/test-syslinux-single-yocto \
 	test/parser/test-syslinux-global-append \
 	test/parser/test-syslinux-explicit \
-	test/parser/test-syslinux-nested-config
+	test/parser/test-syslinux-nested-config \
+	test/parser/test-native-simple \
+	test/parser/test-native-globals \
+	test/parser/test-native-strings
 
 
 TESTS += $(parser_TESTS)
@@ -133,13 +136,16 @@  test_parser_libtest_ro_SOURCES = \
 	discover/parser-conf.c \
 	discover/user-event.c \
 	discover/event.c \
-	$(discover_grub2_grub2_parser_ro_SOURCES)
+	$(discover_grub2_grub2_parser_ro_SOURCES) \
+	$(discover_native_native_parser_ro_SOURCES)
 
 test_parser_libtest_ro_CPPFLAGS = \
 	$(AM_CPPFLAGS) \
 	-I$(top_srcdir)/discover \
 	-I$(top_srcdir)/discover/grub2 \
 	-I$(top_builddir)/discover/grub2 \
+	-I$(top_srcdir)/discover/native \
+	-I$(top_builddir)/discover/native \
 	-DPETITBOOT_TEST \
 	-DLOCAL_STATE_DIR='"$(localstatedir)"' \
 	-DTEST_CONF_BASE='"$(top_srcdir)/test/parser/data"'
diff --git a/test/parser/test-native-globals.c b/test/parser/test-native-globals.c
new file mode 100644
index 00000000..0361e03b
--- /dev/null
+++ b/test/parser/test-native-globals.c
@@ -0,0 +1,45 @@ 
+#include "parser-test.h"
+
+#if 0 /* PARSER_EMBEDDED_CONFIG */
+
+default first
+
+name first
+image /vmlinuz
+args console=hvc0
+initrd /initrd
+
+name second
+image /boot/vmlinuz
+args console=tty0
+initrd /boot/initrd
+
+#endif
+
+void run_test(struct parser_test *test)
+{
+	struct discover_boot_option *opt;
+	struct discover_context *ctx;
+
+	test_read_conf_embedded(test, "/boot/petitboot.conf");
+
+	test_run_parser(test, "native");
+
+	ctx = test->ctx;
+
+	check_boot_option_count(ctx, 2);
+
+	opt = get_boot_option(ctx, 0);
+
+	check_name(opt, "first");
+	check_resolved_local_resource(opt->boot_image, ctx->device, "/vmlinuz");
+	check_args(opt, "console=hvc0");
+	check_resolved_local_resource(opt->initrd, ctx->device, "/initrd");
+	check_is_default(opt);
+
+	opt = get_boot_option(ctx, 1);
+	check_name(opt, "second");
+	check_resolved_local_resource(opt->boot_image, ctx->device, "/boot/vmlinuz");
+	check_args(opt, "console=tty0");
+	check_resolved_local_resource(opt->initrd, ctx->device, "/boot/initrd");
+}
diff --git a/test/parser/test-native-simple.c b/test/parser/test-native-simple.c
new file mode 100644
index 00000000..f817228e
--- /dev/null
+++ b/test/parser/test-native-simple.c
@@ -0,0 +1,31 @@ 
+#include "parser-test.h"
+
+#if 0 /* PARSER_EMBEDDED_CONFIG */
+
+name native-option
+image /vmlinuz
+args console=hvc0
+initrd /initrd
+
+#endif
+
+void run_test(struct parser_test *test)
+{
+	struct discover_boot_option *opt;
+	struct discover_context *ctx;
+
+	test_read_conf_embedded(test, "/boot/petitboot.conf");
+
+	test_run_parser(test, "native");
+
+	ctx = test->ctx;
+
+	check_boot_option_count(ctx, 1);
+
+	opt = get_boot_option(ctx, 0);
+
+	check_name(opt, "native-option");
+	check_resolved_local_resource(opt->boot_image, ctx->device, "/vmlinuz");
+	check_args(opt, "console=hvc0");
+	check_resolved_local_resource(opt->initrd, ctx->device, "/initrd");
+}
diff --git a/test/parser/test-native-strings.c b/test/parser/test-native-strings.c
new file mode 100644
index 00000000..33d242af
--- /dev/null
+++ b/test/parser/test-native-strings.c
@@ -0,0 +1,35 @@ 
+#include "parser-test.h"
+
+#if 0 /* PARSER_EMBEDDED_CONFIG */
+
+name A longer name
+
+image /some/kernel
+initrd /some/other/initrd
+args console=tty0 console=hvc0 debug 
+dtb /a/dtb
+description Contains a number of words
+
+#endif
+
+void run_test(struct parser_test *test)
+{
+	struct discover_boot_option *opt;
+	struct discover_context *ctx;
+
+	test_read_conf_embedded(test, "/boot/petitboot.conf");
+
+	test_run_parser(test, "native");
+
+	ctx = test->ctx;
+
+	check_boot_option_count(ctx, 1);
+
+	opt = get_boot_option(ctx, 0);
+
+	check_name(opt, "A longer name");
+	check_resolved_local_resource(opt->boot_image, ctx->device, "/some/kernel");
+	check_args(opt, "console=tty0 console=hvc0 debug ");
+	check_resolved_local_resource(opt->initrd, ctx->device, "/some/other/initrd");
+	check_resolved_local_resource(opt->dtb, ctx->device, "/a/dtb");
+}