fs: efivars: implement write support
Implement the standard FS ops for writing/manipulating efivars. Signed-off-by: Lucas Stach <l.stach@pengutronix.de> Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
This commit is contained in:
parent
c296bef5ed
commit
46b5a6d652
105
fs/efivarfs.c
105
fs/efivarfs.c
|
@ -126,6 +126,75 @@ static int efivarfs_parse_filename(const char *filename, efi_guid_t *vendor, s16
|
||||||
return 0;
|
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 {
|
struct efivars_file {
|
||||||
void *buf;
|
void *buf;
|
||||||
unsigned long size;
|
unsigned long size;
|
||||||
|
@ -197,6 +266,38 @@ static int efivarfs_read(struct device_d *_dev, FILE *f, void *buf, size_t insiz
|
||||||
return insize;
|
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)
|
static loff_t efivarfs_lseek(struct device_d *dev, FILE *f, loff_t pos)
|
||||||
{
|
{
|
||||||
f->pos = pos;
|
f->pos = pos;
|
||||||
|
@ -320,9 +421,13 @@ static void efivarfs_remove(struct device_d *dev)
|
||||||
}
|
}
|
||||||
|
|
||||||
static struct fs_driver_d efivarfs_driver = {
|
static struct fs_driver_d efivarfs_driver = {
|
||||||
|
.create = efivars_create,
|
||||||
|
.unlink = efivars_unlink,
|
||||||
.open = efivarfs_open,
|
.open = efivarfs_open,
|
||||||
.close = efivarfs_close,
|
.close = efivarfs_close,
|
||||||
.read = efivarfs_read,
|
.read = efivarfs_read,
|
||||||
|
.write = efivarfs_write,
|
||||||
|
.truncate = efivarfs_truncate,
|
||||||
.lseek = efivarfs_lseek,
|
.lseek = efivarfs_lseek,
|
||||||
.opendir = efivarfs_opendir,
|
.opendir = efivarfs_opendir,
|
||||||
.readdir = efivarfs_readdir,
|
.readdir = efivarfs_readdir,
|
||||||
|
|
|
@ -279,7 +279,8 @@ typedef struct {
|
||||||
u32 *Attributes, unsigned long *data_size, void *data);
|
u32 *Attributes, unsigned long *data_size, void *data);
|
||||||
efi_status_t (EFIAPI *get_next_variable)(unsigned long *variable_name_size,
|
efi_status_t (EFIAPI *get_next_variable)(unsigned long *variable_name_size,
|
||||||
s16 *variable_name, efi_guid_t *vendor);
|
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 *get_next_high_mono_count;
|
||||||
void (EFIAPI *reset_system)(efi_reset_type_t reset_type, efi_status_t reset_status,
|
void (EFIAPI *reset_system)(efi_reset_type_t reset_type, efi_status_t reset_status,
|
||||||
unsigned long data_size, void *reset_data);
|
unsigned long data_size, void *reset_data);
|
||||||
|
|
Loading…
Reference in New Issue