Blame peripheral/efivars.c

Packit 34410b
/*
Packit 34410b
 *
Packit 34410b
 *  BlueZ - Bluetooth protocol stack for Linux
Packit 34410b
 *
Packit 34410b
 *  Copyright (C) 2015  Intel Corporation. All rights reserved.
Packit 34410b
 *
Packit 34410b
 *
Packit 34410b
 *  This library is free software; you can redistribute it and/or
Packit 34410b
 *  modify it under the terms of the GNU Lesser General Public
Packit 34410b
 *  License as published by the Free Software Foundation; either
Packit 34410b
 *  version 2.1 of the License, or (at your option) any later version.
Packit 34410b
 *
Packit 34410b
 *  This library is distributed in the hope that it will be useful,
Packit 34410b
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 34410b
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 34410b
 *  Lesser General Public License for more details.
Packit 34410b
 *
Packit 34410b
 *  You should have received a copy of the GNU Lesser General Public
Packit 34410b
 *  License along with this library; if not, write to the Free Software
Packit 34410b
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
Packit 34410b
 *
Packit 34410b
 */
Packit 34410b
Packit 34410b
#ifdef HAVE_CONFIG_H
Packit 34410b
#include <config.h>
Packit 34410b
#endif
Packit 34410b
Packit 34410b
#define _GNU_SOURCE
Packit 34410b
#include <stdio.h>
Packit 34410b
#include <errno.h>
Packit 34410b
#include <fcntl.h>
Packit 34410b
#include <unistd.h>
Packit 34410b
#include <string.h>
Packit 34410b
#include <stdlib.h>
Packit 34410b
#include <stdint.h>
Packit 34410b
#include <sys/stat.h>
Packit 34410b
#include <sys/types.h>
Packit 34410b
#include <sys/param.h>
Packit 34410b
#include <sys/uio.h>
Packit 34410b
Packit 34410b
#include "peripheral/efivars.h"
Packit 34410b
Packit 34410b
#define SYSFS_EFIVARS "/sys/firmware/efi/efivars"
Packit 34410b
Packit 34410b
typedef struct {
Packit 34410b
    uint32_t data1;
Packit 34410b
    uint16_t data2;
Packit 34410b
    uint16_t data3;
Packit 34410b
    uint8_t  data4[8];
Packit 34410b
} efi_guid_t;
Packit 34410b
Packit 34410b
#define VENDOR_GUID \
Packit 34410b
	(efi_guid_t) { 0xd5f9d775, 0x1a09, 0x4e89, \
Packit 34410b
			{ 0x96, 0xcf, 0x1d, 0x19, 0x55, 0x4d, 0xa6, 0x67 } }
Packit 34410b
Packit 34410b
static void efivars_pathname(const char *name, char *pathname, size_t size)
Packit 34410b
{
Packit 34410b
	static efi_guid_t guid = VENDOR_GUID;
Packit 34410b
Packit 34410b
	snprintf(pathname, size - 1,
Packit 34410b
		"%s/%s-%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
Packit 34410b
		SYSFS_EFIVARS, name, guid.data1, guid.data2, guid.data3,
Packit 34410b
		guid.data4[0], guid.data4[1], guid.data4[2], guid.data4[3],
Packit 34410b
		guid.data4[4], guid.data4[5], guid.data4[6], guid.data4[7]);
Packit 34410b
}
Packit 34410b
Packit 34410b
int efivars_read(const char *name, uint32_t *attributes,
Packit 34410b
					void *data, size_t size)
Packit 34410b
{
Packit 34410b
	char pathname[PATH_MAX];
Packit 34410b
	struct iovec iov[2];
Packit 34410b
	uint32_t attr;
Packit 34410b
	ssize_t len;
Packit 34410b
	int fd;
Packit 34410b
Packit 34410b
	efivars_pathname(name, pathname, PATH_MAX);
Packit 34410b
Packit 34410b
	fd = open(pathname, O_RDONLY | O_CLOEXEC);
Packit 34410b
	if (fd < 0)
Packit 34410b
		return -EIO;
Packit 34410b
Packit 34410b
	iov[0].iov_base = &att;;
Packit 34410b
	iov[0].iov_len = sizeof(attr);
Packit 34410b
	iov[1].iov_base = data;
Packit 34410b
	iov[1].iov_len = size;
Packit 34410b
Packit 34410b
	len = readv(fd, iov, 2);
Packit 34410b
Packit 34410b
	close(fd);
Packit 34410b
Packit 34410b
	if (len < 0)
Packit 34410b
		return -EIO;
Packit 34410b
Packit 34410b
	if (attributes)
Packit 34410b
		*attributes = attr;
Packit 34410b
Packit 34410b
	return 0;
Packit 34410b
}
Packit 34410b
Packit 34410b
int efivars_write(const char *name, uint32_t attributes,
Packit 34410b
					const void *data, size_t size)
Packit 34410b
{
Packit 34410b
	char pathname[PATH_MAX];
Packit 34410b
	void *buf;
Packit 34410b
	ssize_t written;
Packit 34410b
	int fd;
Packit 34410b
Packit 34410b
	efivars_pathname(name, pathname, PATH_MAX);
Packit 34410b
Packit 34410b
	buf = malloc(size + sizeof(attributes));
Packit 34410b
	if (!buf)
Packit 34410b
		return -ENOMEM;
Packit 34410b
Packit 34410b
	fd = open(pathname, O_CREAT | O_WRONLY | O_TRUNC | O_CLOEXEC,
Packit 34410b
				S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
Packit 34410b
	if (fd < 0) {
Packit 34410b
		free(buf);
Packit 34410b
		return -EIO;
Packit 34410b
	}
Packit 34410b
Packit 34410b
	memcpy(buf, &attributes, sizeof(attributes));
Packit 34410b
	memcpy(buf + sizeof(attributes), data, size);
Packit 34410b
Packit 34410b
	written = write(fd, buf, size + sizeof(attributes));
Packit 34410b
Packit 34410b
	close(fd);
Packit 34410b
	free(buf);
Packit 34410b
Packit 34410b
	if (written < 0)
Packit 34410b
		return -EIO;
Packit 34410b
Packit 34410b
	return 0;
Packit 34410b
}