From patchwork Thu May 24 04:47:40 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sam Mendoza-Jonas X-Patchwork-Id: 919570 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.ozlabs.org (lists.ozlabs.org [203.11.71.2]) (using TLSv1.2 with cipher ADH-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 40rxjy5mjjz9s0q for ; Thu, 24 May 2018 14:49:10 +1000 (AEST) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=mendozajonas.com Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=mendozajonas.com header.i=@mendozajonas.com header.b="Pd1/RnOm"; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=messagingengine.com header.i=@messagingengine.com header.b="XFXzN2po"; dkim-atps=neutral Received: from lists.ozlabs.org (lists.ozlabs.org [IPv6:2401:3900:2:1::3]) by lists.ozlabs.org (Postfix) with ESMTP id 40rxjy3Mf7zDrBx for ; Thu, 24 May 2018 14:49:10 +1000 (AEST) Authentication-Results: lists.ozlabs.org; dmarc=none (p=none dis=none) header.from=mendozajonas.com Authentication-Results: lists.ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=mendozajonas.com header.i=@mendozajonas.com header.b="Pd1/RnOm"; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=messagingengine.com header.i=@messagingengine.com header.b="XFXzN2po"; dkim-atps=neutral X-Original-To: petitboot@lists.ozlabs.org Delivered-To: petitboot@lists.ozlabs.org Authentication-Results: lists.ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=mendozajonas.com (client-ip=66.111.4.28; helo=out4-smtp.messagingengine.com; envelope-from=sam@mendozajonas.com; receiver=) Authentication-Results: lists.ozlabs.org; dmarc=none (p=none dis=none) header.from=mendozajonas.com Authentication-Results: lists.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=mendozajonas.com header.i=@mendozajonas.com header.b="Pd1/RnOm"; dkim=pass (2048-bit key; unprotected) header.d=messagingengine.com header.i=@messagingengine.com header.b="XFXzN2po"; dkim-atps=neutral Received: from out4-smtp.messagingengine.com (out4-smtp.messagingengine.com [66.111.4.28]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by lists.ozlabs.org (Postfix) with ESMTPS id 40rxhh5Mx6zF1Hp for ; Thu, 24 May 2018 14:48:04 +1000 (AEST) Received: from compute2.internal (compute2.nyi.internal [10.202.2.42]) by mailout.nyi.internal (Postfix) with ESMTP id 7C37921C60; Thu, 24 May 2018 00:48:02 -0400 (EDT) Received: from mailfrontend2 ([10.202.2.163]) by compute2.internal (MEProxy); Thu, 24 May 2018 00:48:02 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= mendozajonas.com; h=cc:date:from:in-reply-to:message-id :references:subject:to:x-me-sender:x-me-sender:x-sasl-enc; s= fm3; bh=60AJp+U+aUDrJFK7y/Fvz94yuLels9lVcxYI5pix5nI=; b=Pd1/RnOm 5mUc8HFmYQmwQRVIk0PD61irz39RFYoc9KiNzh97TaLpALNVT9P6QqOUcc6Q6/xg Twoy6lGpEwYpU4Lh+HAG727fHxBBFf9pwr8lKdj693dAV+uav2xkpV3NxU4VPxIY UQmwlMwNkDHrT6UB0y5cDvyp2XcOMMZsbVLj99QVofmrLq34HVQKnEMFXXoCATK7 tsS9MGJ8eT43G9EcACMROHLdVSgTbckVK9kbj3CqlqwXTVXCxEJEYbocVaYLEvBe b+rD5ElZbsCxI0QLZnr4X49N4Tf+WZ/KxvyAD1A4pm8xP5fzKkn0R8F/i3FfeJCA t45fqo1hOzqEzA== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:date:from:in-reply-to:message-id :references:subject:to:x-me-sender:x-me-sender:x-sasl-enc; s= fm2; bh=60AJp+U+aUDrJFK7y/Fvz94yuLels9lVcxYI5pix5nI=; b=XFXzN2po oUBdbaB4ejQTnMeCZXmB7+Zj3aMGl5vkG4xVqcMTJ9FXsVH7DvM+/WTW/VyBU0B7 D0ereavG7DMhiKBL/GVYz9smGefBoP5eRfMgGsPRgzhdDqfTAnwf5e83/icjPlUA 5HmExj+YWcv6LhcwKPhnPUCWQb3ZZUjeb/K3RQnziSrDlvg3WynTgDA7nMOAM+zs Wkf+7qt7B79RWOEsJW2Y649JPUESErHUR02QUax2e5Q22cHdRp1sYWmXAWM3SbvN MzvJ8UjMfxdIiquVldaq66GCoqGtvI+HrndpXDajHBoi5IuA33NKQZgAI49tLfNz 0rovKpr6KPr5sw== X-ME-Proxy: X-ME-Proxy: X-ME-Proxy: X-ME-Proxy: X-ME-Proxy: X-ME-Proxy: X-ME-Sender: Received: from v4.ozlabs.ibm.com (unknown [122.99.82.10]) by mail.messagingengine.com (Postfix) with ESMTPA id 1D0021025E; Thu, 24 May 2018 00:48:00 -0400 (EDT) From: Samuel Mendoza-Jonas To: petitboot@lists.ozlabs.org Subject: [PATCH v2 7/9] discover/pxe-parser: Parse simple iPXE scripts Date: Thu, 24 May 2018 14:47:40 +1000 Message-Id: <20180524044742.25889-8-sam@mendozajonas.com> X-Mailer: git-send-email 2.17.0 In-Reply-To: <20180524044742.25889-1-sam@mendozajonas.com> References: <20180524044742.25889-1-sam@mendozajonas.com> X-BeenThere: petitboot@lists.ozlabs.org X-Mailman-Version: 2.1.26 Precedence: list List-Id: Petitboot bootloader development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Samuel Mendoza-Jonas MIME-Version: 1.0 Errors-To: petitboot-bounces+incoming=patchwork.ozlabs.org@lists.ozlabs.org Sender: "Petitboot" Signed-off-by: Samuel Mendoza-Jonas --- discover/pxe-parser.c | 96 ++++++++++++++++++++++++++++- test/parser/Makefile.am | 3 + test/parser/test-pxe-ipxe-default.c | 40 ++++++++++++ test/parser/test-pxe-ipxe-named.c | 39 ++++++++++++ test/parser/test-pxe-ipxe.c | 40 ++++++++++++ 5 files changed, 216 insertions(+), 2 deletions(-) create mode 100644 test/parser/test-pxe-ipxe-default.c create mode 100644 test/parser/test-pxe-ipxe-named.c create mode 100644 test/parser/test-pxe-ipxe.c diff --git a/discover/pxe-parser.c b/discover/pxe-parser.c index fd8b8e8..da9a49f 100644 --- a/discover/pxe-parser.c +++ b/discover/pxe-parser.c @@ -258,6 +258,97 @@ static void pxe_load_next_filename(struct conf_context *conf) return; } +/* + * Parse a limited set of iPXE commands. This is handled separately from + * conf_parse_buf() since not all commands will have a value. + */ +static bool ipxe_simple_parser(struct conf_context *ctx, char *buf, int len) +{ + struct discover_boot_option *opt; + char *p, *name, *value; + struct pb_url *url; + + p = strstr(buf, "#!ipxe"); + + /* Only try to parse this if the ipxe header is right at the start */ + if (!p || p != buf) + return false; + p = strchr(buf, '\n') + 1; + + opt = discover_boot_option_create(ctx->dc, ctx->dc->device); + + opt->option->id = talloc_asprintf(opt, "%s@%p", + ctx->dc->device->device->id, opt); + opt->option->type = DISCOVER_BOOT_OPTION; + + while (p && p <= buf + len) { + + p = conf_get_pair(ctx, p, &name, &value, ' ', '\n'); + + /* The 'boot' command appears by itself without options */ + if (!name && value && streq(value, "boot")) { + opt->option->is_default = true; + continue; + } + + /* All other parameters require a value */ + if (!value) { + pb_debug("%s: '%s' missing value\n", __func__, name); + continue; + } + + if (streq(name, "kernel")) { + char *args, *name = NULL, *saveptr = NULL, *tmp; + + /* + * value is of the form: + * boot_image [--name option_name ] [option args ...] + */ + tmp = strtok_r(value, " ", &saveptr); + if (!tmp) { + pb_log("%s: malformed kernel statement\n", __func__); + break; + } + + url = pxe_url_join(ctx->dc, ctx->dc->conf_url, tmp); + opt->boot_image = create_url_resource(opt, url); + + tmp = strtok_r(NULL, " ", &saveptr); + if (!tmp) + continue; + if (streq(tmp, "--name")) { + tmp = strtok_r(NULL, " ", &saveptr); + name = talloc_asprintf(opt, "%s", + tmp ?: "malformed ipxe option"); + args = strtok_r(NULL, " ", &saveptr); + } else + args = tmp; + + while (args) { + pxe_append_string(opt, args); + args = strtok_r(NULL, " ", &saveptr); + } + + opt->option->name = name ?: talloc_asprintf(opt, + "ipxe option (%s)", url->full); + continue; + } + + if (streq(name, "initrd")) { + url = pxe_url_join(ctx->dc, ctx->dc->conf_url, value); + opt->initrd = create_url_resource(opt, url); + continue; + } + } + + if (opt->boot_image) + discover_context_add_boot_option(ctx->dc, opt); + else + pb_debug("ipxe file did not contain a valid option\n"); + + return true; +} + /* * Callback for asynchronous loads from pxe_parse() * @param result Result of load_url_async() @@ -308,9 +399,10 @@ static void pxe_conf_parse_cb(struct load_url_result *result, void *data) * Parse the first successfully downloaded file. We only want to parse * the first because otherwise we could parse options from both a * machine-specific config and a 'fallback' default config + * We also check if the file is in the limited ipxe format. */ - - conf_parse_buf(conf, buf, len); + if (!ipxe_simple_parser(conf, buf, len)) + conf_parse_buf(conf, buf, len); /* We may be called well after the original caller of iterate_parsers(), * commit any new boot options ourselves */ diff --git a/test/parser/Makefile.am b/test/parser/Makefile.am index 6ff3972..1732158 100644 --- a/test/parser/Makefile.am +++ b/test/parser/Makefile.am @@ -64,6 +64,9 @@ parser_TESTS = \ test/parser/test-pxe-initrd-in-append \ test/parser/test-pxe-mac-without-conf \ test/parser/test-pxe-ip-without-conf \ + test/parser/test-pxe-ipxe \ + test/parser/test-pxe-ipxe-named \ + test/parser/test-pxe-ipxe-default \ test/parser/test-pxe-non-url-conf \ test/parser/test-pxe-local \ test/parser/test-pxe-ipappend \ diff --git a/test/parser/test-pxe-ipxe-default.c b/test/parser/test-pxe-ipxe-default.c new file mode 100644 index 0000000..d80cc41 --- /dev/null +++ b/test/parser/test-pxe-ipxe-default.c @@ -0,0 +1,40 @@ +#include "parser-test.h" + +#if 0 /* PARSER_EMBEDDED_CONFIG */ +#!ipxe +kernel vmlinux --name test-option +initrd initrd +boot +#endif + +/** + * Test that we recognise an ipxe-formatted script obtained from bootfile_url + * (DHCPv6 option 59) that some vendors use, and that we set it as default. + */ + +void run_test(struct parser_test *test) +{ + struct discover_boot_option *opt; + struct discover_context *ctx; + + test_read_conf_embedded_url(test, "tftp://host/dir1/conf"); + + test_set_event_source(test); + test_set_event_param(test->ctx->event, "bootfile_url", "tftp://host/dir1/conf"); + + test_run_parser(test, "pxe"); + + ctx = test->ctx; + + check_boot_option_count(ctx, 1); + opt = get_boot_option(ctx, 0); + + check_name(opt, "test-option"); + + check_resolved_url_resource(opt->boot_image, + "tftp://host/dir1/vmlinux"); + check_resolved_url_resource(opt->initrd, + "tftp://host/dir1/initrd"); + check_args(opt, NULL); + check_is_default(opt); +} diff --git a/test/parser/test-pxe-ipxe-named.c b/test/parser/test-pxe-ipxe-named.c new file mode 100644 index 0000000..dae2fc6 --- /dev/null +++ b/test/parser/test-pxe-ipxe-named.c @@ -0,0 +1,39 @@ +#include "parser-test.h" + +#if 0 /* PARSER_EMBEDDED_CONFIG */ +#!ipxe +kernel vmlinux --name test-option append kernel args +initrd initrd +#endif + +/** + * Test that we recognise an ipxe-formatted script obtained from bootfile_url + * (DHCPv6 option 59) that some vendors use, and that we correctly parse the + * --name parameter from the kernel arguments. + */ + +void run_test(struct parser_test *test) +{ + struct discover_boot_option *opt; + struct discover_context *ctx; + + test_read_conf_embedded_url(test, "tftp://host/dir1/conf"); + + test_set_event_source(test); + test_set_event_param(test->ctx->event, "bootfile_url", "tftp://host/dir1/conf"); + + test_run_parser(test, "pxe"); + + ctx = test->ctx; + + check_boot_option_count(ctx, 1); + opt = get_boot_option(ctx, 0); + + check_name(opt, "test-option"); + + check_resolved_url_resource(opt->boot_image, + "tftp://host/dir1/vmlinux"); + check_resolved_url_resource(opt->initrd, + "tftp://host/dir1/initrd"); + check_args(opt, "append kernel args"); +} diff --git a/test/parser/test-pxe-ipxe.c b/test/parser/test-pxe-ipxe.c new file mode 100644 index 0000000..e3bc202 --- /dev/null +++ b/test/parser/test-pxe-ipxe.c @@ -0,0 +1,40 @@ + + +#include "parser-test.h" + +#if 0 /* PARSER_EMBEDDED_CONFIG */ +#!ipxe +kernel vmlinux append kernel args +initrd initrd +#endif + +/** + * Test that we recognise an ipxe-formatted script obtained from bootfile_url + * (DHCPv6 option 59) that some vendors use. + */ + +void run_test(struct parser_test *test) +{ + struct discover_boot_option *opt; + struct discover_context *ctx; + + test_read_conf_embedded_url(test, "tftp://host/dir1/conf"); + + test_set_event_source(test); + test_set_event_param(test->ctx->event, "bootfile_url", "tftp://host/dir1/conf"); + + test_run_parser(test, "pxe"); + + ctx = test->ctx; + + check_boot_option_count(ctx, 1); + opt = get_boot_option(ctx, 0); + + check_name(opt, "ipxe option (tftp://host/dir1/vmlinux)"); + + check_resolved_url_resource(opt->boot_image, + "tftp://host/dir1/vmlinux"); + check_resolved_url_resource(opt->initrd, + "tftp://host/dir1/initrd"); + check_args(opt, "append kernel args"); +}