X-Git-Url: http://git.ozlabs.org/?p=petitboot;a=blobdiff_plain;f=discover%2Fparser.c;h=c04a0afca610b31bfb07468824770445cfc94d92;hp=c106dec0ae20c0a6210d317060cbe125f9656450;hb=5e7c90eddd7ac2e4a3b05a7a5f6e29166edfa161;hpb=7c5e552c210b38a06ed9fbb99418b62e20978666 diff --git a/discover/parser.c b/discover/parser.c index c106dec..c04a0af 100644 --- a/discover/parser.c +++ b/discover/parser.c @@ -1,38 +1,127 @@ +#include #include +#include +#include -#include "pb-protocol/pb-protocol.h" +#include "types/types.h" #include +#include #include "device-handler.h" #include "parser.h" #include "parser-utils.h" +#include "paths.h" -struct parser __native_parser; -struct parser __yaboot_parser; -struct parser __kboot_parser; -struct parser __grub2_parser; +static int n_parsers; +static struct parser **parsers; -static const struct parser *const parsers[] = { -// &__native_parser, - &__yaboot_parser, - &__kboot_parser, - NULL -}; +static const int max_file_size = 1024 * 1024; -void iterate_parsers(struct discover_context *ctx) +static int read_file(struct discover_context *ctx, + const char *filename, char **bufp, int *lenp) +{ + struct stat statbuf; + int rc, fd, i, len; + char *buf; + + fd = open(filename, O_RDONLY); + if (fd < 0) + return -1; + + rc = fstat(fd, &statbuf); + if (rc < 0) + goto err_close; + + len = statbuf.st_size; + if (len > max_file_size) + goto err_close; + + buf = talloc_array(ctx, char, len); + if (!buf) + goto err_close; + + for (i = 0; i < len; i += rc) { + rc = read(fd, buf + i, len - i); + + /* unexpected EOF: trim and return */ + if (rc == 0) { + len = i; + break; + } + + if (rc < 0) + goto err_free; + + } + + close(fd); + *bufp = buf; + *lenp = len; + return 0; + +err_free: + talloc_free(buf); +err_close: + close(fd); + return -1; +} + +static char *local_path(struct discover_context *ctx, + const char *filename) +{ + return join_paths(ctx, ctx->device->mount_path, filename); +} + +static void iterate_parser_files(struct discover_context *ctx, + const struct parser *parser) +{ + const char * const *filename; + const char *path; + + if (!parser->filenames) + return; + + for (filename = parser->filenames; *filename; filename++) { + int rc, len; + char *buf; + + path = local_path(ctx, *filename); + if (!path) + continue; + + rc = read_file(ctx, path, &buf, &len); + if (!rc) { + parser->parse(ctx, buf, len); + talloc_free(buf); + } + } +} + +void iterate_parsers(struct discover_context *ctx, enum conf_method method) { int i; - unsigned int count = 0; - pb_log("trying parsers for %s\n", ctx->device_path); + pb_log("trying parsers for %s\n", ctx->device->device->id); + + if (method == CONF_METHOD_LOCAL_FILE) { + for (i = 0; i < n_parsers; i++) { + if (parsers[i]->method != CONF_METHOD_LOCAL_FILE) + continue; - for (i = 0; parsers[i]; i++) { - pb_log("\ttrying parser '%s'\n", parsers[i]->name); - count += parsers[i]->parse(ctx); + pb_log("\ttrying parser '%s'\n", parsers[i]->name); + ctx->parser = parsers[i]; + iterate_parser_files(ctx, ctx->parser); + } + ctx->parser = NULL; } - if (!count) - pb_log("\tno boot_options found\n"); +} + +void __register_parser(struct parser *parser) +{ + parsers = talloc_realloc(NULL, parsers, struct parser *, n_parsers + 1); + parsers[n_parsers] = parser; + n_parsers++; } void parser_init(void)