Kexec Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: Dave Young <dyoung@redhat.com>
To: kexec@lists.infradead.org
Cc: mjg59@srcf.ucam.org, linux-efi@vger.kernel.org,
	toshi.kani@hp.com, matt@console-pimps.org, greg@kroah.com,
	x86@kernel.org, James.Bottomley@HansenPartnership.com,
	horms@verge.net.au, bp@alien8.de, ebiederm@xmission.com,
	hpa@zytor.com, vgoyal@redhat.com
Subject: [PATCH v4 4/4] Passing efi related data via setup_data
Date: Fri, 20 Dec 2013 18:05:47 +0800	[thread overview]
Message-ID: <1387533947-18210-5-git-send-email-dyoung@redhat.com> (raw)
In-Reply-To: <1387533947-18210-1-git-send-email-dyoung@redhat.com>

For supporting efi runtime, several efi physical addresses
fw_vendor, runtime, config tables, smbios and the whole runtime
mapping info need to be used in kexec kernel. Thus introduce
setup_data struct for passing these data.

collect the varialbes from /sys/firmware/efi/systab and
/sys/firmware/efi/runtime-map

Tested on qemu+ovmf, dell laptop, lenovo laptop and HP workstation.

v1->v2:
HPA: use uint*_t instead of __uint*_t
Simon: indention fix; fix a memory leak
move offset change update to previous patch in setup header
only passing setup_data when the bzImage support efi boot
Vivek: export a value in bzImage probe so it can be used
      to check if we should pass acpi_rsdp.
coding style

v2->v3:
code cleanup
bail out if efi mm desc_version != 1
bhe: define macro for SETUP_EFI
     break loop if find matched string in systab.

v3->v4:
patch04:
  update memmap in efi_info to the saved runtime map
  So kernel side can iterate them as normal boot.

Signed-off-by: Dave Young <dyoung@redhat.com>
---
 kexec/arch/i386/x86-linux-setup.c | 201 +++++++++++++++++++++++++++++++++++++-
 1 file changed, 198 insertions(+), 3 deletions(-)

diff --git a/kexec/arch/i386/x86-linux-setup.c b/kexec/arch/i386/x86-linux-setup.c
index 38bd9dd..b8000b2 100644
--- a/kexec/arch/i386/x86-linux-setup.c
+++ b/kexec/arch/i386/x86-linux-setup.c
@@ -36,6 +36,8 @@
 #include "x86-linux-setup.h"
 #include "../../kexec/kexec-syscall.h"
 
+#define SETUP_EFI	4
+
 void init_linux_parameters(struct x86_linux_param_header *real_mode)
 {
 	/* Fill in the values that are usually provided by the kernel. */
@@ -480,11 +482,204 @@ void setup_subarch(struct x86_linux_param_header *real_mode)
 	get_bootparam(&real_mode->hardware_subarch, offset, sizeof(uint32_t));
 }
 
-static void setup_efi_info(struct x86_linux_param_header *real_mode)
+struct efi_mem_descriptor {
+	uint32_t type;
+	uint32_t pad;
+	uint64_t phys_addr;
+	uint64_t virt_addr;
+	uint64_t num_pages;
+	uint64_t attribute;
+};
+
+struct efi_setup_data {
+	uint64_t fw_vendor;
+	uint64_t runtime;
+	uint64_t tables;
+	uint64_t smbios;
+	uint64_t reserved[8];
+	struct efi_mem_descriptor map[0];
+};
+
+struct setup_data {
+	uint64_t next;
+	uint32_t type;
+	uint32_t len;
+	uint8_t data[0];
+} __attribute__((packed));
+
+static int __get_efi_value(char *line, const char *pattern, uint64_t *val)
+{
+	char *s, *end;
+	s = strstr(line, pattern);
+	if (s)
+		*val = strtoull(s + strlen(pattern), &end, 16);
+
+	if (!s || *val == ULONG_MAX)
+		return 1;
+	return 0;
+}
+
+static void _get_efi_value(const char *filename,
+			const char *pattern, uint64_t *val)
 {
+	FILE *fp;
+	char line[1024];
+	int ret;
+
+	fp = fopen(filename, "r");
+	if (!fp)
+		return;
+
+	while (fgets(line, sizeof(line), fp) != 0) {
+		ret = __get_efi_value(line, pattern, val);
+		if (!ret)
+			break;
+	}
+
+	fclose(fp);
+}
+
+static void get_efi_values(struct efi_setup_data *esd)
+{
+	_get_efi_value("/sys/firmware/efi/systab", "SMBIOS=0x",
+			&esd->smbios);
+	_get_efi_value("/sys/firmware/efi/fw_vendor", "0x",
+			&esd->fw_vendor);
+	_get_efi_value("/sys/firmware/efi/runtime", "0x",
+			&esd->runtime);
+	_get_efi_value("/sys/firmware/efi/config_table", "0x",
+			&esd->tables);
+}
+
+static int get_efi_runtime_map(struct efi_setup_data **esd)
+{
+	DIR *dirp;
+	struct dirent *entry;
+	char filename[1024];
+	struct efi_mem_descriptor md;
+	int nr_maps = 0;
+
+	dirp = opendir("/sys/firmware/efi/runtime-map");
+	if (!dirp)
+		return 0;
+	while ((entry = readdir(dirp)) != NULL) {
+		sprintf(filename,
+			"/sys/firmware/efi/runtime-map/%s",
+			(char *)entry->d_name);
+		if (*entry->d_name == '.')
+			continue;
+		file_scanf(filename, "type", "0x%x", (unsigned int *)&md.type);
+		file_scanf(filename, "phys_addr", "0x%llx",
+			   (unsigned long long *)&md.phys_addr);
+		file_scanf(filename, "virt_addr", "0x%llx",
+			   (unsigned long long *)&md.virt_addr);
+		file_scanf(filename, "num_pages", "0x%llx",
+			   (unsigned long long *)&md.num_pages);
+		file_scanf(filename, "attribute", "0x%llx",
+			   (unsigned long long *)&md.attribute);
+		*esd = realloc(*esd, sizeof(struct efi_setup_data) +
+			   (nr_maps + 1) * sizeof(struct efi_mem_descriptor));
+		*((*esd)->map + nr_maps) = md;
+		nr_maps++;
+	}
+
+	closedir(dirp);
+	return nr_maps;
+}
+
+struct efi_info {
+	uint32_t efi_loader_signature;
+	uint32_t efi_systab;
+	uint32_t efi_memdesc_size;
+	uint32_t efi_memdesc_version;
+	uint32_t efi_memmap;
+	uint32_t efi_memmap_size;
+	uint32_t efi_systab_hi;
+	uint32_t efi_memmap_hi;
+};
+
+
+static int setup_efi_setup_data(struct kexec_info *info,
+			struct x86_linux_param_header *real_mode)
+{
+	int nr_maps;
+	int64_t setup_data_paddr;
+	struct setup_data *sd;
+	struct efi_setup_data *esd;
+	int size, sdsize;
+	int has_efi = 0;
+	struct efi_info *ei = (struct efi_info *)real_mode->efi_info;
+
+	has_efi = access("/sys/firmware/efi/systab", F_OK);
+	if (has_efi < 0)
+		return 1;
+
+	esd = malloc(sizeof(struct efi_setup_data));
+	if (!esd)
+		return 1;
+	memset(esd, 0, sizeof(struct efi_setup_data));
+	get_efi_values(esd);
+	nr_maps = get_efi_runtime_map(&esd);
+	if (!nr_maps) {
+		free(esd);
+		return 1;
+	}
+	size = nr_maps * sizeof(struct efi_mem_descriptor) +
+		sizeof(struct efi_setup_data);
+	sd = malloc(sizeof(struct setup_data) + size);
+	if (!sd) {
+		free(esd);
+		return 1;
+	}
+
+	memset(sd, 0, sizeof(struct setup_data) + size);
+	sd->next = 0;
+	sd->type = SETUP_EFI;
+	sd->len = size;
+	memcpy(sd->data, esd, size);
+	free(esd);
+	sdsize = sd->len + sizeof(struct setup_data);
+	setup_data_paddr = add_buffer(info, sd, sdsize, sdsize, getpagesize(),
+					0x100000, ULONG_MAX, INT_MAX);
+
+	real_mode->setup_data = setup_data_paddr;
+
+	ei->efi_memmap = real_mode->setup_data + sizeof(struct setup_data) +
+			 sizeof(struct efi_setup_data);
+	ei->efi_memmap_size = nr_maps * sizeof(struct efi_mem_descriptor);
+	ei->efi_memdesc_size = sizeof(struct efi_mem_descriptor);
+
+	return 0;
+}
+
+static int
+get_efi_mem_desc_version(struct x86_linux_param_header *real_mode)
+{
+	struct efi_info *ei = (struct efi_info *)real_mode->efi_info;
+
+	return ei->efi_memdesc_version;
+}
+
+static void setup_efi_info(struct kexec_info *info,
+			   struct x86_linux_param_header *real_mode)
+{
+	int ret, desc_version;
 	off_t offset = offsetof(typeof(*real_mode), efi_info);
 
-	get_bootparam(&real_mode->efi_info, offset, 32);
+	ret = get_bootparam(&real_mode->efi_info, offset, 32);
+	if (ret)
+		return;
+	desc_version = get_efi_mem_desc_version(real_mode);
+	if (desc_version != 1) {
+		fprintf(stderr,
+			"efi memory descriptor version %d is not supported!\n",
+			desc_version);
+		memset(&real_mode->efi_info, 0, 32);
+		return;
+	}
+	ret = setup_efi_setup_data(info, real_mode);
+	if (ret)
+		memset(&real_mode->efi_info, 0, 32);
 }
 
 void setup_linux_system_parameters(struct kexec_info *info,
@@ -497,7 +692,7 @@ void setup_linux_system_parameters(struct kexec_info *info,
 	/* get subarch from running kernel */
 	setup_subarch(real_mode);
 	if (bzImage_support_efi_boot)
-		setup_efi_info(real_mode);
+		setup_efi_info(info, real_mode);
 	
 	/* Default screen size */
 	real_mode->orig_x = 0;
-- 
1.8.3.1


_______________________________________________
kexec mailing list
kexec@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/kexec

  parent reply	other threads:[~2013-12-20 10:07 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-12-20 10:05 [PATCH v4 0/4] kexec-tools: efi runtime support on kexec kernel Dave Young
2013-12-20 10:05 ` [PATCH v4 1/4] build fix: include x86-linux.h in x86-linux-setup.h Dave Young
2013-12-20 10:05 ` [PATCH v4 2/4] Add function get_bootparam Dave Young
2013-12-20 10:05 ` [PATCH v4 3/4] Add efi_info in x86 setup header Dave Young
2013-12-20 10:05 ` Dave Young [this message]
2013-12-30  7:16   ` [PATCH v4 4/4 update] Passing efi related data via setup_data Dave Young
2014-01-06 21:58     ` Toshi Kani
2013-12-20 17:58 ` [PATCH v4 0/4] kexec-tools: efi runtime support on kexec kernel Toshi Kani
2014-01-21  2:55 ` Dave Young
2014-01-21  5:07   ` Simon Horman
2014-01-21  6:13     ` Dave Young
2014-01-21  6:31       ` Simon Horman

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1387533947-18210-5-git-send-email-dyoung@redhat.com \
    --to=dyoung@redhat.com \
    --cc=James.Bottomley@HansenPartnership.com \
    --cc=bp@alien8.de \
    --cc=ebiederm@xmission.com \
    --cc=greg@kroah.com \
    --cc=horms@verge.net.au \
    --cc=hpa@zytor.com \
    --cc=kexec@lists.infradead.org \
    --cc=linux-efi@vger.kernel.org \
    --cc=matt@console-pimps.org \
    --cc=mjg59@srcf.ucam.org \
    --cc=toshi.kani@hp.com \
    --cc=vgoyal@redhat.com \
    --cc=x86@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox