mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Lucas Stach <l.stach@pengutronix.de>
To: barebox@lists.infradead.org
Subject: [PATCH 11/12] fs: efivars: implement write support
Date: Mon,  8 Dec 2014 14:42:38 +0100	[thread overview]
Message-ID: <1418046159-29843-11-git-send-email-l.stach@pengutronix.de> (raw)
In-Reply-To: <1418046159-29843-1-git-send-email-l.stach@pengutronix.de>

Implement the standard FS ops for writing/manipulating
efivars.

Signed-off-by: Lucas Stach <l.stach@pengutronix.de>
---
 fs/efivarfs.c | 105 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 include/efi.h |   3 +-
 2 files changed, 107 insertions(+), 1 deletion(-)

diff --git a/fs/efivarfs.c b/fs/efivarfs.c
index f54925133848..9a1503bd1387 100644
--- a/fs/efivarfs.c
+++ b/fs/efivarfs.c
@@ -126,6 +126,75 @@ static int efivarfs_parse_filename(const char *filename, efi_guid_t *vendor, s16
 	return 0;
 }
 
+static int efivars_create(struct device_d *dev, const char *pathname, mode_t mode)
+{
+	struct efivarfs_priv *priv = dev->priv;
+	struct efivarfs_inode *inode;
+	efi_guid_t vendor;
+	efi_status_t efiret;
+	u8 dummydata;
+	char *name8;
+	s16 *name;
+	int ret;
+
+	if (pathname[0] == '/')
+		pathname++;
+
+	/* deny creating files with other vendor GUID than our own */
+	ret = efivarfs_parse_filename(pathname, &vendor, &name);
+	if (ret)
+		return -ENOENT;
+
+	if (memcmp(&vendor, &EFI_BAREBOX_VENDOR_GUID, sizeof(efi_guid_t)))
+		return -EPERM;
+
+	inode = xzalloc(sizeof(*inode));
+	inode->name = name;
+	inode->vendor = vendor;
+
+
+	name8 = strdup_wchar_to_char(inode->name);
+	inode->full_name = asprintf("%s-%pUl", name8, &inode->vendor);
+	free(name8);
+
+	efiret = RT->set_variable(inode->name, &inode->vendor,
+				  EFI_VARIABLE_NON_VOLATILE |
+				  EFI_VARIABLE_BOOTSERVICE_ACCESS |
+				  EFI_VARIABLE_RUNTIME_ACCESS,
+				  1, &dummydata);
+	if (EFI_ERROR(efiret)) {
+		free(inode);
+		return -efi_errno(efiret);
+	}
+
+	list_add_tail(&inode->node, &priv->inodes);
+
+	return 0;
+}
+
+static int efivars_unlink(struct device_d *dev, const char *pathname)
+{
+	struct efivarfs_priv *priv = dev->priv;
+	struct efivarfs_inode *inode, *tmp;
+	efi_status_t efiret;
+
+	if (pathname[0] == '/')
+		pathname++;
+
+	list_for_each_entry_safe(inode, tmp, &priv->inodes, node) {
+		if (!strcmp(inode->full_name, pathname)) {
+			efiret = RT->set_variable(inode->name, &inode->vendor,
+						  0, 0, NULL);
+			if (EFI_ERROR(efiret))
+				return -efi_errno(efiret);
+			list_del(&inode->node);
+			free(inode);
+		}
+	}
+
+	return 0;
+}
+
 struct efivars_file {
 	void *buf;
 	unsigned long size;
@@ -197,6 +266,38 @@ static int efivarfs_read(struct device_d *_dev, FILE *f, void *buf, size_t insiz
 	return insize;
 }
 
+static int efivarfs_write(struct device_d *_dev, FILE *f, const void *buf, size_t insize)
+{
+	struct efivars_file *efile = f->inode;
+
+	if (efile->size < f->pos + insize) {
+		efile->buf = realloc(efile->buf, f->pos + insize);
+		efile->size = f->pos + insize;
+	}
+
+	memcpy(efile->buf + f->pos, buf, insize);
+
+	RT->set_variable(efile->name, &efile->vendor, efile->attributes,
+			 efile->size ? efile->size : 1, efile->buf);
+
+	return insize;
+}
+
+static int efivarfs_truncate(struct device_d *dev, FILE *f, ulong size)
+{
+	struct efivars_file *efile = f->inode;
+
+	efile->size = size;
+	efile->buf = realloc(efile->buf, efile->size + sizeof(uint32_t));
+
+	RT->set_variable(efile->name, &efile->vendor, efile->attributes,
+			 efile->size ? efile->size : 1, efile->buf);
+
+	f->size = efile->size;
+
+	return 0;
+}
+
 static loff_t efivarfs_lseek(struct device_d *dev, FILE *f, loff_t pos)
 {
 	f->pos = pos;
@@ -320,9 +421,13 @@ static void efivarfs_remove(struct device_d *dev)
 }
 
 static struct fs_driver_d efivarfs_driver = {
+	.create    = efivars_create,
+	.unlink    = efivars_unlink,
 	.open      = efivarfs_open,
 	.close     = efivarfs_close,
 	.read      = efivarfs_read,
+	.write     = efivarfs_write,
+	.truncate  = efivarfs_truncate,
 	.lseek     = efivarfs_lseek,
 	.opendir   = efivarfs_opendir,
 	.readdir   = efivarfs_readdir,
diff --git a/include/efi.h b/include/efi.h
index 4abb5d2e3683..9b4f16bd9f54 100644
--- a/include/efi.h
+++ b/include/efi.h
@@ -279,7 +279,8 @@ typedef struct {
 			u32 *Attributes, unsigned long *data_size, void *data);
 	efi_status_t (EFIAPI *get_next_variable)(unsigned long *variable_name_size,
 			s16 *variable_name, efi_guid_t *vendor);
-	void *set_variable;
+	efi_status_t (EFIAPI *set_variable)(s16 *variable_name, efi_guid_t *vendor,
+			u32 Attributes, unsigned long data_size, void *data);
 	void *get_next_high_mono_count;
 	void (EFIAPI *reset_system)(efi_reset_type_t reset_type, efi_status_t reset_status,
 			unsigned long data_size, void *reset_data);
-- 
2.1.3


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

  parent reply	other threads:[~2014-12-08 13:43 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-12-08 13:42 [PATCH 01/12] efi: add function to determine type of device path Lucas Stach
2014-12-08 13:42 ` [PATCH 02/12] efi: add proper reset hook Lucas Stach
2014-12-08 13:42 ` [PATCH 03/12] efi: move exit to EFI into own command Lucas Stach
2014-12-08 13:42 ` [PATCH 04/12] efi: add Barebox GUID Lucas Stach
2014-12-08 13:42 ` [PATCH 05/12] fs: efivars: cosmetic changes Lucas Stach
2014-12-08 13:42 ` [PATCH 06/12] fs: efivar: switch to standard list implementation Lucas Stach
2014-12-08 13:42 ` [PATCH 07/12] fs: efivar: move variable discovery into probe Lucas Stach
2014-12-08 13:42 ` [PATCH 08/12] lib: add wchar strdup Lucas Stach
2014-12-08 13:42 ` [PATCH 09/12] fs: efivar: preserve more info in inode Lucas Stach
2014-12-08 13:42 ` [PATCH 10/12] fs: efivars: don't store attributes in file Lucas Stach
2014-12-08 13:42 ` Lucas Stach [this message]
2014-12-08 13:42 ` [PATCH 12/12] efi: mount efivarfs by default if enabled Lucas Stach
2014-12-09  8:59 ` [PATCH 01/12] efi: add function to determine type of device path Sascha Hauer

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=1418046159-29843-11-git-send-email-l.stach@pengutronix.de \
    --to=l.stach@pengutronix.de \
    --cc=barebox@lists.infradead.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