From patchwork Sat May 26 06:06:05 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Thomas Huth X-Patchwork-Id: 920931 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 40tCLX4fxgz9s1R for ; Sat, 26 May 2018 16:06:44 +1000 (AEST) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=redhat.com Received: from lists.ozlabs.org (lists.ozlabs.org [IPv6:2401:3900:2:1::3]) by lists.ozlabs.org (Postfix) with ESMTP id 40tCLX2f8NzDr0r for ; Sat, 26 May 2018 16:06:44 +1000 (AEST) Authentication-Results: lists.ozlabs.org; dmarc=pass (p=none dis=none) header.from=redhat.com X-Original-To: slof@lists.ozlabs.org Delivered-To: slof@lists.ozlabs.org Authentication-Results: lists.ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=redhat.com (client-ip=66.187.233.73; helo=mx1.redhat.com; envelope-from=thuth@redhat.com; receiver=) Authentication-Results: lists.ozlabs.org; dmarc=pass (p=none dis=none) header.from=redhat.com Received: from mx1.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by lists.ozlabs.org (Postfix) with ESMTPS id 40tCL407MlzDqrM for ; Sat, 26 May 2018 16:06:20 +1000 (AEST) Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id BD485818BAF4; Sat, 26 May 2018 06:06:17 +0000 (UTC) Received: from thh440s.redhat.com (ovpn-116-22.ams2.redhat.com [10.36.116.22]) by smtp.corp.redhat.com (Postfix) with ESMTP id B1A76210C6C0; Sat, 26 May 2018 06:06:16 +0000 (UTC) From: Thomas Huth To: slof@lists.ozlabs.org Date: Sat, 26 May 2018 08:06:05 +0200 Message-Id: <1527314768-4797-6-git-send-email-thuth@redhat.com> In-Reply-To: <1527314768-4797-1-git-send-email-thuth@redhat.com> References: <1527314768-4797-1-git-send-email-thuth@redhat.com> X-Scanned-By: MIMEDefang 2.78 on 10.11.54.4 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.8]); Sat, 26 May 2018 06:06:17 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.8]); Sat, 26 May 2018 06:06:17 +0000 (UTC) for IP:'10.11.54.4' DOMAIN:'int-mx04.intmail.prod.int.rdu2.redhat.com' HELO:'smtp.corp.redhat.com' FROM:'thuth@redhat.com' RCPT:'' Subject: [SLOF] [PATCH v3 5/8] libnet: Wire up pxelinux.cfg network booting X-BeenThere: slof@lists.ozlabs.org X-Mailman-Version: 2.1.26 Precedence: list List-Id: "Patches for https://github.com/aik/SLOF" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Greg Kurz MIME-Version: 1.0 Errors-To: slof-bounces+incoming=patchwork.ozlabs.org@lists.ozlabs.org Sender: "SLOF" In case the normal network loading failed, try to load a pxelinux.cfg config file. If that succeeds, load the kernel and initrd with the information that could be found in this file. Signed-off-by: Thomas Huth --- include/helpers.h | 2 ++ lib/libnet/netload.c | 84 ++++++++++++++++++++++++++++++++++++++++++++++++++-- slof/helpers.c | 15 ++++++++-- 3 files changed, 96 insertions(+), 5 deletions(-) diff --git a/include/helpers.h b/include/helpers.h index 04ee771..9dfe3ae 100644 --- a/include/helpers.h +++ b/include/helpers.h @@ -36,6 +36,8 @@ extern void SLOF_pci_config_write16(long offset, long value); extern void SLOF_pci_config_write8(long offset, long value); extern void *SLOF_translate_my_address(void *addr); extern int write_mm_log(char *data, unsigned int len, unsigned short type); +extern void SLOF_set_chosen_int(const char *s, long val); +extern void SLOF_set_chosen_bytes(const char *s, const char *addr, size_t size); extern void SLOF_encode_bootp_response(void *addr, size_t size); extern void SLOF_encode_dhcp_response(void *addr, size_t size); diff --git a/lib/libnet/netload.c b/lib/libnet/netload.c index f06c2fd..8a38771 100644 --- a/lib/libnet/netload.c +++ b/lib/libnet/netload.c @@ -26,6 +26,7 @@ #include #include "args.h" #include "netapps.h" +#include "pxelinux.h" #define IP_INIT_DEFAULT 5 #define IP_INIT_NONE 0 @@ -426,6 +427,75 @@ static int tftp_load(filename_ip_t *fnip, void *buffer, int len, return rc; } +#define CFG_BUF_SIZE 2048 +#define MAX_PL_CFG_ENTRIES 16 +static int net_pxelinux_load(filename_ip_t *fnip, char *loadbase, + int maxloadlen, uint8_t *mac, int retries) +{ + struct pl_cfg_entry entries[MAX_PL_CFG_ENTRIES]; + int def, rc, ilen; + static char *cfgbuf; + + cfgbuf = malloc(CFG_BUF_SIZE); + if (!cfgbuf) { + puts("Not enough memory for pxelinux config file buffer!"); + return -1; + } + + rc = pxelinux_load_parse_cfg(fnip, mac, retries, cfgbuf, CFG_BUF_SIZE, + entries, MAX_PL_CFG_ENTRIES, &def); + if (rc < 0) + goto out_free; + if (rc == 0) { + puts("No valid entries in pxelinux config file."); + rc = -1; + goto out_free; + } + + /* Load kernel */ + strncpy(fnip->filename, entries[def].kernel, + sizeof(fnip->filename) - 1); + fnip->filename[sizeof(fnip->filename) - 1] = 0; + rc = tftp_load(fnip, loadbase, maxloadlen, retries); + if (rc <= 0) + goto out_free; + + /* Load ramdisk */ + if (entries[def].initrd) { + loadbase += rc; + maxloadlen -= rc; + if (maxloadlen <= 0) { + puts(" Not enough space for loading the initrd!"); + rc = -1; + goto out_free; + } + strncpy(fnip->filename, entries[def].initrd, + sizeof(fnip->filename) - 1); + ilen = tftp_load(fnip, loadbase, maxloadlen, retries); + if (ilen < 0) { + rc = ilen; + goto out_free; + } + /* The ELF loader will move the kernel to some spot in low mem + * later, thus move the initrd to the end of the RAM instead */ + memmove(loadbase + maxloadlen - ilen, loadbase, ilen); + /* Encode the initrd information in the device tree */ + SLOF_set_chosen_int("linux,initrd-start", + (long)loadbase + maxloadlen - ilen); + SLOF_set_chosen_int("linux,initrd-end", + (long)loadbase + maxloadlen); + } + + if (entries[def].append) { + SLOF_set_chosen_bytes("bootargs", entries[def].append, + strlen(entries[def].append) + 1); + } + +out_free: + free(cfgbuf); + return rc; +} + static void encode_response(char *pkt_buffer, size_t size, int ip_init) { switch(ip_init) { @@ -444,7 +514,7 @@ static void encode_response(char *pkt_buffer, size_t size, int ip_init) int netload(char *buffer, int len, char *args_fs, int alen) { - int rc; + int rc, filename_len; filename_ip_t fn_ip; int fd_device; obp_tftp_args_t obp_tftp_args; @@ -687,7 +757,17 @@ int netload(char *buffer, int len, char *args_fs, int alen) } /* Do the TFTP load and print error message if necessary */ - rc = tftp_load(&fn_ip, buffer, len, obp_tftp_args.tftp_retries); + rc = 0; + filename_len = strlen(fn_ip.filename); + if (filename_len > 0 && fn_ip.filename[filename_len - 1] != '/') { + rc = tftp_load(&fn_ip, buffer, len, obp_tftp_args.tftp_retries); + } + + if (rc <= 0 && !obp_tftp_args.filename[0] && + (!filename_len || fn_ip.filename[filename_len - 1] == '/')) { + rc = net_pxelinux_load(&fn_ip, buffer, len, own_mac, + obp_tftp_args.tftp_retries); + } if (obp_tftp_args.ip_init == IP_INIT_DHCP) dhcp_send_release(fn_ip.fd); diff --git a/slof/helpers.c b/slof/helpers.c index a8d575c..bd0742e 100644 --- a/slof/helpers.c +++ b/slof/helpers.c @@ -181,7 +181,16 @@ int write_mm_log(char *data, unsigned int len, unsigned short type) return forth_eval_pop("write-mm-log"); } -static void SLOF_encode_response(void *addr, size_t size,char *s) +void SLOF_set_chosen_int(const char *s, long val) +{ + forth_push(val); + forth_eval("encode-int"); + forth_push((unsigned long)s); + forth_push(strlen(s)); + forth_eval("set-chosen"); +} + +void SLOF_set_chosen_bytes(const char *s, const char *addr, size_t size) { forth_push((unsigned long)addr); forth_push(size); @@ -193,10 +202,10 @@ static void SLOF_encode_response(void *addr, size_t size,char *s) void SLOF_encode_bootp_response(void *addr, size_t size) { - SLOF_encode_response(addr, size, "bootp-response"); + SLOF_set_chosen_bytes("bootp-response", addr, size); } void SLOF_encode_dhcp_response(void *addr, size_t size) { - SLOF_encode_response(addr, size, "dhcp-response"); + SLOF_set_chosen_bytes("dhcp-response", addr, size); }