From patchwork Mon Aug 22 20:57:38 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Leif Lindholm X-Patchwork-Id: 74464 Delivered-To: patch@linaro.org Received: by 10.140.29.52 with SMTP id a49csp1761383qga; Mon, 22 Aug 2016 13:58:51 -0700 (PDT) X-Received: by 10.200.49.129 with SMTP id h1mr26184753qte.103.1471899530945; Mon, 22 Aug 2016 13:58:50 -0700 (PDT) Return-Path: Received: from lists.gnu.org (lists.gnu.org. [2001:4830:134:3::11]) by mx.google.com with ESMTPS id c67si15129912qkf.219.2016.08.22.13.58.50 for (version=TLS1 cipher=AES128-SHA bits=128/128); Mon, 22 Aug 2016 13:58:50 -0700 (PDT) Received-SPF: pass (google.com: domain of grub-devel-bounces+patch=linaro.org@gnu.org designates 2001:4830:134:3::11 as permitted sender) client-ip=2001:4830:134:3::11; Authentication-Results: mx.google.com; dkim=neutral (body hash did not verify) header.i=@linaro.org; spf=pass (google.com: domain of grub-devel-bounces+patch=linaro.org@gnu.org designates 2001:4830:134:3::11 as permitted sender) smtp.mailfrom=grub-devel-bounces+patch=linaro.org@gnu.org; dmarc=fail (p=NONE dis=NONE) header.from=linaro.org Received: from localhost ([::1]:43157 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bbwJ0-0000XZ-EX for patch@linaro.org; Mon, 22 Aug 2016 16:58:50 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:56751) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bbwI1-0000Vd-JA for grub-devel@gnu.org; Mon, 22 Aug 2016 16:57:51 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1bbwHw-00084R-Dp for grub-devel@gnu.org; Mon, 22 Aug 2016 16:57:49 -0400 Received: from mail-wm0-x22c.google.com ([2a00:1450:400c:c09::22c]:35041) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bbwHw-00083u-2Z for grub-devel@gnu.org; Mon, 22 Aug 2016 16:57:44 -0400 Received: by mail-wm0-x22c.google.com with SMTP id f65so136984390wmi.0 for ; Mon, 22 Aug 2016 13:57:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=0M33u0ytkShWSj3pqYT8YQaD+bmyr4qZ6JAN06SnGEg=; b=S3fQdKU9eUcdmxlvHoaUPHmV3W5GmZMERkswTQWEzzjXOynUSZtT3DyNwLQrM5+qvo 4SwMljkK4lFHrMTJ5GlxtqJs9QCMJvHDFZiLiY7+hu7JDVNshHO26ip90CVg0M4xBK6Z xZHzMw14Imb/RgPqlOeK8FBlPRV1Qte3/jOiE= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=0M33u0ytkShWSj3pqYT8YQaD+bmyr4qZ6JAN06SnGEg=; b=HvG4ODNostz3g9Mr5ix2NjkALeg6nOhkrVp1yoNrWvgTr+Tr9GKAs6PwO/ZolKtfi7 zB9uRzAsH9lFdSGvMqgci59Tqp6zH4hMR+MnumTuw00dcSa3z8XxPg4Ir9XLOdR+MV6E Ghok1mQyuC7JHAjA7iXhXeOKSRxZT692FkmEubEq79gKnU+rpiGUQi92oUH+2iA4ZsdU m2kMVt2KycfzrDUnPf/p/yjrqSQdzFvyHIYAE7JosdUz5M0FPKwxdcK1wpm4+F2k86XB QzS3nuS9LYPONQxjAkgx6EE2zd5l9tZzCTgFtWD7gwDQLDzDUDCV92FD2eD06Rdn2xHU HBMA== X-Gm-Message-State: AEkooutI5RI16uoKLT3Se4tojH0rpAIYBXN1xGL8ityuaSIkIyYaWi8yTvN8jmk+9qdgSENa X-Received: by 10.194.15.130 with SMTP id x2mr19140190wjc.122.1471899463038; Mon, 22 Aug 2016 13:57:43 -0700 (PDT) Received: from mohikan.mushroom.smurfnet.nu (cpc92308-cmbg19-2-0-cust814.5-4.cable.virginm.net. [82.24.251.47]) by smtp.gmail.com with ESMTPSA id p71sm23578958wmf.9.2016.08.22.13.57.42 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Mon, 22 Aug 2016 13:57:42 -0700 (PDT) From: Leif Lindholm To: grub-devel@gnu.org Subject: [PATCH 1/3] move efi fdt helper library Date: Mon, 22 Aug 2016 21:57:38 +0100 Message-Id: <1471899460-13596-2-git-send-email-leif.lindholm@linaro.org> X-Mailer: git-send-email 2.1.4 In-Reply-To: <1471899460-13596-1-git-send-email-leif.lindholm@linaro.org> References: <1471899460-13596-1-git-send-email-leif.lindholm@linaro.org> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 2a00:1450:400c:c09::22c X-BeenThere: grub-devel@gnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: The development of GNU GRUB List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: The development of GNU GRUB Cc: agraf@suse.de MIME-Version: 1.0 Errors-To: grub-devel-bounces+patch=linaro.org@gnu.org Sender: "Grub-devel" There is nothing ARM64 (or even ARM) specific about the efi fdt helper library, which is used for locating or overriding a firmware-provided devicetree in a UEFI system - so move it to loader/efi for reuse. Move the fdtload.h include file to grub/efi and move the (at least theoretically) machine dependent page size definitions to grub/machine/memory.h. --- grub-core/Makefile.core.def | 2 +- grub-core/loader/arm64/fdt.c | 185 ------------------------------------- grub-core/loader/arm64/linux.c | 3 +- grub-core/loader/arm64/xen_boot.c | 3 +- grub-core/loader/efi/fdt.c | 186 ++++++++++++++++++++++++++++++++++++++ include/grub/arm64/efi/memory.h | 3 + include/grub/arm64/fdtload.h | 35 ------- include/grub/efi/fdtload.h | 32 +++++++ 8 files changed, 226 insertions(+), 223 deletions(-) delete mode 100644 grub-core/loader/arm64/fdt.c create mode 100644 grub-core/loader/efi/fdt.c delete mode 100644 include/grub/arm64/fdtload.h create mode 100644 include/grub/efi/fdtload.h -- 2.1.4 _______________________________________________ Grub-devel mailing list Grub-devel@gnu.org https://lists.gnu.org/mailman/listinfo/grub-devel diff --git a/grub-core/Makefile.core.def b/grub-core/Makefile.core.def index 58b4208..89e338d 100644 --- a/grub-core/Makefile.core.def +++ b/grub-core/Makefile.core.def @@ -1674,7 +1674,7 @@ module = { module = { name = fdt; - arm64 = loader/arm64/fdt.c; + arm64 = loader/efi/fdt.c; common = lib/fdt.c; enable = fdt; }; diff --git a/grub-core/loader/arm64/fdt.c b/grub-core/loader/arm64/fdt.c deleted file mode 100644 index 5202c14..0000000 --- a/grub-core/loader/arm64/fdt.c +++ /dev/null @@ -1,185 +0,0 @@ -/* - * GRUB -- GRand Unified Bootloader - * Copyright (C) 2013-2015 Free Software Foundation, Inc. - * - * GRUB 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, either version 3 of the License, or - * (at your option) any later version. - * - * GRUB 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 GRUB. If not, see . - */ - -#include -#include -#include -#include -#include -#include -#include -#include - -static void *loaded_fdt; -static void *fdt; - -static void * -get_firmware_fdt (void) -{ - grub_efi_configuration_table_t *tables; - grub_efi_guid_t fdt_guid = GRUB_EFI_DEVICE_TREE_GUID; - void *firmware_fdt = NULL; - unsigned int i; - - /* Look for FDT in UEFI config tables. */ - tables = grub_efi_system_table->configuration_table; - - for (i = 0; i < grub_efi_system_table->num_table_entries; i++) - if (grub_memcmp (&tables[i].vendor_guid, &fdt_guid, sizeof (fdt_guid)) == 0) - { - firmware_fdt = tables[i].vendor_table; - grub_dprintf ("linux", "found registered FDT @ %p\n", firmware_fdt); - break; - } - - return firmware_fdt; -} - -void * -grub_fdt_load (grub_size_t additional_size) -{ - void *raw_fdt; - grub_size_t size; - - if (fdt) - { - size = GRUB_EFI_BYTES_TO_PAGES (grub_fdt_get_totalsize (fdt)); - grub_efi_free_pages ((grub_efi_physical_address_t) fdt, size); - } - - if (loaded_fdt) - raw_fdt = loaded_fdt; - else - raw_fdt = get_firmware_fdt(); - - size = - raw_fdt ? grub_fdt_get_totalsize (raw_fdt) : GRUB_FDT_EMPTY_TREE_SZ; - size += additional_size; - - grub_dprintf ("linux", "allocating %ld bytes for fdt\n", size); - fdt = grub_efi_allocate_pages (0, GRUB_EFI_BYTES_TO_PAGES (size)); - if (!fdt) - return NULL; - - if (raw_fdt) - { - grub_memmove (fdt, raw_fdt, size); - grub_fdt_set_totalsize (fdt, size); - } - else - { - grub_fdt_create_empty_tree (fdt, size); - } - return fdt; -} - -grub_err_t -grub_fdt_install (void) -{ - grub_efi_boot_services_t *b; - grub_efi_guid_t fdt_guid = GRUB_EFI_DEVICE_TREE_GUID; - grub_efi_status_t status; - - b = grub_efi_system_table->boot_services; - status = b->install_configuration_table (&fdt_guid, fdt); - if (status != GRUB_EFI_SUCCESS) - return grub_error (GRUB_ERR_IO, "failed to install FDT"); - - grub_dprintf ("fdt", "Installed/updated FDT configuration table @ %p\n", - fdt); - return GRUB_ERR_NONE; -} - -void -grub_fdt_unload (void) { - if (!fdt) { - return; - } - grub_efi_free_pages ((grub_efi_physical_address_t) fdt, - GRUB_EFI_BYTES_TO_PAGES (grub_fdt_get_totalsize (fdt))); - fdt = NULL; -} - -static grub_err_t -grub_cmd_devicetree (grub_command_t cmd __attribute__ ((unused)), - int argc, char *argv[]) -{ - grub_file_t dtb; - void *blob = NULL; - int size; - - if (loaded_fdt) - grub_free (loaded_fdt); - loaded_fdt = NULL; - - /* No arguments means "use firmware FDT". */ - if (argc == 0) - { - return GRUB_ERR_NONE; - } - - dtb = grub_file_open (argv[0]); - if (!dtb) - goto out; - - size = grub_file_size (dtb); - blob = grub_malloc (size); - if (!blob) - goto out; - - if (grub_file_read (dtb, blob, size) < size) - { - if (!grub_errno) - grub_error (GRUB_ERR_BAD_OS, N_("premature end of file %s"), argv[0]); - goto out; - } - - if (grub_fdt_check_header (blob, size) != 0) - { - grub_error (GRUB_ERR_BAD_OS, N_("invalid device tree")); - goto out; - } - -out: - if (dtb) - grub_file_close (dtb); - - if (blob) - { - if (grub_errno == GRUB_ERR_NONE) - loaded_fdt = blob; - else - grub_free (blob); - } - - return grub_errno; -} - -static grub_command_t cmd_devicetree; - -GRUB_MOD_INIT (fdt) -{ - cmd_devicetree = - grub_register_command ("devicetree", grub_cmd_devicetree, 0, - N_("Load DTB file.")); -} - -GRUB_MOD_FINI (fdt) -{ - grub_unregister_command (cmd_devicetree); -} diff --git a/grub-core/loader/arm64/linux.c b/grub-core/loader/arm64/linux.c index 9519d2e..02e4054 100644 --- a/grub-core/loader/arm64/linux.c +++ b/grub-core/loader/arm64/linux.c @@ -26,11 +26,12 @@ #include #include #include -#include #include +#include #include #include #include +#include GRUB_MOD_LICENSE ("GPLv3+"); diff --git a/grub-core/loader/arm64/xen_boot.c b/grub-core/loader/arm64/xen_boot.c index a914eb8..341805c 100644 --- a/grub-core/loader/arm64/xen_boot.c +++ b/grub-core/loader/arm64/xen_boot.c @@ -27,12 +27,13 @@ #include #include #include -#include #include #include +#include #include /* required by struct xen_hypervisor_header */ #include #include +#include GRUB_MOD_LICENSE ("GPLv3+"); diff --git a/grub-core/loader/efi/fdt.c b/grub-core/loader/efi/fdt.c new file mode 100644 index 0000000..a1e19b7 --- /dev/null +++ b/grub-core/loader/efi/fdt.c @@ -0,0 +1,186 @@ +/* + * GRUB -- GRand Unified Bootloader + * Copyright (C) 2013-2015 Free Software Foundation, Inc. + * + * GRUB 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, either version 3 of the License, or + * (at your option) any later version. + * + * GRUB 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 GRUB. If not, see . + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +static void *loaded_fdt; +static void *fdt; + +static void * +get_firmware_fdt (void) +{ + grub_efi_configuration_table_t *tables; + grub_efi_guid_t fdt_guid = GRUB_EFI_DEVICE_TREE_GUID; + void *firmware_fdt = NULL; + unsigned int i; + + /* Look for FDT in UEFI config tables. */ + tables = grub_efi_system_table->configuration_table; + + for (i = 0; i < grub_efi_system_table->num_table_entries; i++) + if (grub_memcmp (&tables[i].vendor_guid, &fdt_guid, sizeof (fdt_guid)) == 0) + { + firmware_fdt = tables[i].vendor_table; + grub_dprintf ("linux", "found registered FDT @ %p\n", firmware_fdt); + break; + } + + return firmware_fdt; +} + +void * +grub_fdt_load (grub_size_t additional_size) +{ + void *raw_fdt; + unsigned int size; + + if (fdt) + { + size = GRUB_EFI_BYTES_TO_PAGES (grub_fdt_get_totalsize (fdt)); + grub_efi_free_pages ((grub_addr_t) fdt, size); + } + + if (loaded_fdt) + raw_fdt = loaded_fdt; + else + raw_fdt = get_firmware_fdt(); + + size = + raw_fdt ? grub_fdt_get_totalsize (raw_fdt) : GRUB_FDT_EMPTY_TREE_SZ; + size += additional_size; + + grub_dprintf ("linux", "allocating %d bytes for fdt\n", size); + fdt = grub_efi_allocate_pages (0, GRUB_EFI_BYTES_TO_PAGES (size)); + if (!fdt) + return NULL; + + if (raw_fdt) + { + grub_memmove (fdt, raw_fdt, size); + grub_fdt_set_totalsize (fdt, size); + } + else + { + grub_fdt_create_empty_tree (fdt, size); + } + return fdt; +} + +grub_err_t +grub_fdt_install (void) +{ + grub_efi_boot_services_t *b; + grub_efi_guid_t fdt_guid = GRUB_EFI_DEVICE_TREE_GUID; + grub_efi_status_t status; + + b = grub_efi_system_table->boot_services; + status = b->install_configuration_table (&fdt_guid, fdt); + if (status != GRUB_EFI_SUCCESS) + return grub_error (GRUB_ERR_IO, "failed to install FDT"); + + grub_dprintf ("fdt", "Installed/updated FDT configuration table @ %p\n", + fdt); + return GRUB_ERR_NONE; +} + +void +grub_fdt_unload (void) { + if (!fdt) { + return; + } + grub_efi_free_pages ((grub_addr_t) fdt, + GRUB_EFI_BYTES_TO_PAGES (grub_fdt_get_totalsize (fdt))); + fdt = NULL; +} + +static grub_err_t +grub_cmd_devicetree (grub_command_t cmd __attribute__ ((unused)), + int argc, char *argv[]) +{ + grub_file_t dtb; + void *blob = NULL; + int size; + + if (loaded_fdt) + grub_free (loaded_fdt); + loaded_fdt = NULL; + + /* No arguments means "use firmware FDT". */ + if (argc == 0) + { + return GRUB_ERR_NONE; + } + + dtb = grub_file_open (argv[0]); + if (!dtb) + goto out; + + size = grub_file_size (dtb); + blob = grub_malloc (size); + if (!blob) + goto out; + + if (grub_file_read (dtb, blob, size) < size) + { + if (!grub_errno) + grub_error (GRUB_ERR_BAD_OS, N_("premature end of file %s"), argv[0]); + goto out; + } + + if (grub_fdt_check_header (blob, size) != 0) + { + grub_error (GRUB_ERR_BAD_OS, N_("invalid device tree")); + goto out; + } + +out: + if (dtb) + grub_file_close (dtb); + + if (blob) + { + if (grub_errno == GRUB_ERR_NONE) + loaded_fdt = blob; + else + grub_free (blob); + } + + return grub_errno; +} + +static grub_command_t cmd_devicetree; + +GRUB_MOD_INIT (fdt) +{ + cmd_devicetree = + grub_register_command ("devicetree", grub_cmd_devicetree, 0, + N_("Load DTB file.")); +} + +GRUB_MOD_FINI (fdt) +{ + grub_unregister_command (cmd_devicetree); +} diff --git a/include/grub/arm64/efi/memory.h b/include/grub/arm64/efi/memory.h index c6cb324..9414b69 100644 --- a/include/grub/arm64/efi/memory.h +++ b/include/grub/arm64/efi/memory.h @@ -3,4 +3,7 @@ #define GRUB_EFI_MAX_USABLE_ADDRESS 0xffffffffffffULL +#define GRUB_EFI_PAGE_SHIFT 12 +#define GRUB_EFI_BYTES_TO_PAGES(bytes) (((bytes) + 0xfff) >> GRUB_EFI_PAGE_SHIFT) + #endif /* ! GRUB_MEMORY_CPU_HEADER */ diff --git a/include/grub/arm64/fdtload.h b/include/grub/arm64/fdtload.h deleted file mode 100644 index 7b9ddba..0000000 --- a/include/grub/arm64/fdtload.h +++ /dev/null @@ -1,35 +0,0 @@ -/* - * GRUB -- GRand Unified Bootloader - * Copyright (C) 2013-2015 Free Software Foundation, Inc. - * - * GRUB 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, either version 3 of the License, or - * (at your option) any later version. - * - * GRUB 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 GRUB. If not, see . - */ - -#ifndef GRUB_FDTLOAD_CPU_HEADER -#define GRUB_FDTLOAD_CPU_HEADER 1 - -#include -#include - -void * -grub_fdt_load (grub_size_t additional_size); -void -grub_fdt_unload (void); -grub_err_t -grub_fdt_install (void); - -#define GRUB_EFI_PAGE_SHIFT 12 -#define GRUB_EFI_BYTES_TO_PAGES(bytes) (((bytes) + 0xfff) >> GRUB_EFI_PAGE_SHIFT) - -#endif diff --git a/include/grub/efi/fdtload.h b/include/grub/efi/fdtload.h new file mode 100644 index 0000000..713c942 --- /dev/null +++ b/include/grub/efi/fdtload.h @@ -0,0 +1,32 @@ +/* + * GRUB -- GRand Unified Bootloader + * Copyright (C) 2013-2015 Free Software Foundation, Inc. + * + * GRUB 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, either version 3 of the License, or + * (at your option) any later version. + * + * GRUB 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 GRUB. If not, see . + */ + +#ifndef GRUB_FDTLOAD_CPU_HEADER +#define GRUB_FDTLOAD_CPU_HEADER 1 + +#include +#include + +void * +grub_fdt_load (grub_size_t additional_size); +void +grub_fdt_unload (void); +grub_err_t +grub_fdt_install (void); + +#endif