From patchwork Thu Jun 1 15:25:40 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Thomas Huth X-Patchwork-Id: 769799 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.ozlabs.org (lists.ozlabs.org [103.22.144.68]) (using TLSv1.2 with cipher ADH-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 3wdrlW4tzSz9s89 for ; Fri, 2 Jun 2017 01:25:59 +1000 (AEST) Received: from lists.ozlabs.org (lists.ozlabs.org [IPv6:2401:3900:2:1::3]) by lists.ozlabs.org (Postfix) with ESMTP id 3wdrlW3w7RzDq5x for ; Fri, 2 Jun 2017 01:25:59 +1000 (AEST) X-Original-To: slof@lists.ozlabs.org Delivered-To: slof@lists.ozlabs.org Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by lists.ozlabs.org (Postfix) with ESMTPS id 3wdrlH4ClMzDqFT for ; Fri, 2 Jun 2017 01:25:47 +1000 (AEST) Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id C31392D9FE5; Thu, 1 Jun 2017 15:25:45 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com C31392D9FE5 Authentication-Results: ext-mx06.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx06.extmail.prod.ext.phx2.redhat.com; spf=pass smtp.mailfrom=thuth@redhat.com DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.redhat.com C31392D9FE5 Received: from thh440s.str.redhat.com (dhcp-192-189.str.redhat.com [10.33.192.189]) by smtp.corp.redhat.com (Postfix) with ESMTP id 4EFFD17CCB; Thu, 1 Jun 2017 15:25:45 +0000 (UTC) From: Thomas Huth To: slof@lists.ozlabs.org Date: Thu, 1 Jun 2017 17:25:40 +0200 Message-Id: <1496330742-18181-3-git-send-email-thuth@redhat.com> In-Reply-To: <1496330742-18181-1-git-send-email-thuth@redhat.com> References: <1496330742-18181-1-git-send-email-thuth@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.30]); Thu, 01 Jun 2017 15:25:45 +0000 (UTC) Subject: [SLOF] [PATCH 2/4] bootmenu: Gather devices and print the menu X-BeenThere: slof@lists.ozlabs.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: "Patches for https://github.com/aik/SLOF" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: slof-bounces+incoming=patchwork.ozlabs.org@lists.ozlabs.org Sender: "SLOF" Examine the aliases to get a list of possible boot devices and print a list with all these devices. Signed-off-by: Thomas Huth --- lib/libbootmenu/bootmenu.c | 86 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 86 insertions(+) diff --git a/lib/libbootmenu/bootmenu.c b/lib/libbootmenu/bootmenu.c index d8d00cb..649e518 100644 --- a/lib/libbootmenu/bootmenu.c +++ b/lib/libbootmenu/bootmenu.c @@ -14,8 +14,94 @@ #include #include +#include +#include #include "bootmenu.h" +#define MAX_DEVS 36 /* Enough for 10 digits + 26 letters */ +#define MAX_ALIAS_LEN 8 /* Maximum length of alias names */ + +struct bootdev { + char alias[MAX_ALIAS_LEN]; + char *path; +}; + +static int nr_devs; +static struct bootdev bootdevs[MAX_DEVS]; + +/** + * Look up an alias name. + * @return The NUL-terminated device tree path (should be released with free() + * when it's not required anymore), or NULL if it can't be found. + */ +static char *find_alias(char *alias) +{ + char *path; + long len; + + forth_push((unsigned long)alias); + forth_push(strlen(alias)); + forth_eval("find-alias"); + + len = forth_pop(); + if (!len) + return NULL; + + path = malloc(len + 1); + memcpy(path, (void *)forth_pop(), len); + path[len] = '\0'; + + return path; +} + +static void bootmenu_populate_devs(void) +{ + char *aliases[] = { "cdrom", "disk", "net", NULL }; + int ai, idx; + + for (ai = 0; aliases[ai] != NULL; ai++) { + for (idx = 0; idx <= 9; idx++) { + char *cur_alias = bootdevs[nr_devs].alias; + if (idx == 0) + strcpy(cur_alias, aliases[ai]); + else + sprintf(cur_alias, "%s%i", aliases[ai], idx); + bootdevs[nr_devs].path = find_alias(cur_alias); + if (!bootdevs[nr_devs].path) + break; + nr_devs += 1; + } + } +} + +static void bootmenu_free_devs(void) +{ + while (nr_devs > 0) { + nr_devs -= 1; + free(bootdevs[nr_devs].path); + bootdevs[nr_devs].path = NULL; + } +} + +static void bootmenu_show_devs(void) +{ + int i; + + for (i = 0; i < nr_devs; i++) { + printf("%c) %6s : %s\n", i < 9 ? '1' + i : 'a' + i - 9, + bootdevs[i].alias, bootdevs[i].path); + } +} + void bootmenu(void) { + bootmenu_populate_devs(); + if (!nr_devs) { + puts("No available boot devices!"); + return; + } + + bootmenu_show_devs(); + + bootmenu_free_devs(); }