[Linux-ia64] Elilo-3.3: eliloalt broken

From: Andreas Schwab <schwab_at_suse.de>
Date: 2002-09-13 20:00:16
Apparently there is no single EFI variable that can be relied upon.  This
fixes eliloalt so that it just uses the first one it finds when it wants
to create the EliloAlt variable.

--- elilo-3.3/tools/eliloalt.c	2002/09/13 09:29:00	1.1
+++ elilo-3.3/tools/eliloalt.c	2002/09/13 09:39:42
@@ -41,6 +41,7 @@
 #include <errno.h>
 #include <stdint.h>
 #include <string.h>
+#include <dirent.h>
 
 
 #define ELILOALT_VERSION	"0.01"
@@ -49,12 +50,6 @@
 #define EFIVAR_DIR	"/proc/efi/vars"
 #define ELILO_ALTVAR	EFIVAR_DIR"/"ELILO_ALT_NAME"-00000000-0000-0000-0000-000000000000"
 
-/*
- * to write/create a variable we need to open for writing ANY existing variable.
- * So here we chose one that does seem to always exist
- */
-#define ELILOALT_WRITE_VICTIM EFIVAR_DIR"/Efi-47c7b226-c42a-11d2-8e57-00a0c969723b";
-
 #define EFI_VARIABLE_NON_VOLATILE 0x0000000000000001
 #define EFI_VARIABLE_BOOTSERVICE_ACCESS 0x0000000000000002
 #define EFI_VARIABLE_RUNTIME_ACCESS 0x0000000000000004
@@ -124,18 +119,35 @@ usage(char **argv)
 	);
 }
 
-static void
-check_proc_efi(void)
+static char *
+check_proc_efi(int find_entry)
 {
-	int fd;
+	DIR *efi_vars;
+	struct dirent *entry;
+	static char name[1024];
 
 	if (getuid() != 0) {
 		fatal_error("This program must be run as root\n");
 	}
-	fd = open(EFIVAR_DIR, O_RDONLY);
-	if (fd == -1) {
+	efi_vars = opendir(EFIVAR_DIR);
+	if (efi_vars == NULL) {
 		fatal_error("Cannot access %s\n", EFIVAR_DIR);
 	}
+	if (!find_entry) {
+		closedir(efi_vars);
+		return NULL;
+	}
+	/* Find one entry we can open */
+	while ((entry = readdir(efi_vars)) != NULL) {
+		if (strcmp(entry->d_name, ".") && strcmp(entry->d_name, ".."))
+			break;
+	}
+	if (entry == NULL) {
+		fatal_error("Cannot find entry in %s\n", EFIVAR_DIR);
+	}
+	sprintf(name, "%s/%s", EFIVAR_DIR, entry->d_name);
+	closedir(efi_vars);
+	return name;
 }
 
 static void
@@ -144,7 +156,7 @@ delete_var(void)
 	efi_variable_t var;
 	int fd, r, i;
 
-	check_proc_efi();
+	check_proc_efi(0);
 
 	fd = open(ELILO_ALTVAR, O_WRONLY);
 	if (fd == -1) {
@@ -177,7 +189,7 @@ print_var(void)
 	int fd, r, i;
 
 
-	check_proc_efi();
+	check_proc_efi(0);
 
 	fd = open(ELILO_ALTVAR, O_RDONLY);
 	if (fd == -1) {
@@ -203,9 +215,9 @@ set_var(char *cmdline)
 {
 	efi_variable_t var;
 	int fd, r, i, j, l;
-	char *name = ELILOALT_WRITE_VICTIM;
+	char *name;
 
-	check_proc_efi();
+	name = check_proc_efi(1);
 
 	if (cmdline == NULL) {
 		fatal_error("invalid cmdline argument\n");

Andreas.

-- 
Andreas Schwab, SuSE Labs, schwab@suse.de
SuSE Linux AG, Deutschherrnstr. 15-19, D-90429 Nürnberg
Key fingerprint = 58CA 54C7 6D53 942B 1756  01D3 44D5 214B 8276 4ED5
"And now for something completely different."
Received on Fri Sep 13 03:00:35 2002

This archive was generated by hypermail 2.1.8 : 2005-08-02 09:20:10 EST