efivars.c revision 7432b24b8357d913943580b442ffe7040e610f9e
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
/***
This file is part of systemd.
Copyright 2013 Lennart Poettering
under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
systemd is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include "util.h"
#include "utf8.h"
#include "efivars.h"
#ifdef ENABLE_EFI
#define LOAD_OPTION_ACTIVE 0x00000001
#define MEDIA_DEVICE_PATH 0x04
#define MEDIA_HARDDRIVE_DP 0x01
#define MEDIA_FILEPATH_DP 0x04
#define SIGNATURE_TYPE_GUID 0x02
#define MBR_TYPE_EFI_PARTITION_TABLE_HEADER 0x02
#define END_DEVICE_PATH_TYPE 0x7f
#define END_ENTIRE_DEVICE_PATH_SUBTYPE 0xff
struct boot_option {
} _packed_;
struct drive_path {
char signature[16];
} _packed_;
struct device_path {
union {
struct drive_path drive;
};
} _packed_;
bool is_efi_boot(void) {
}
int r;
_cleanup_free_ void *v = NULL;
size_t s;
uint8_t b;
if (r < 0)
return r;
if (s != 1)
return -EINVAL;
b = *(uint8_t *)v;
r = b > 0;
return r;
}
int is_efi_secure_boot(void) {
return read_flag("SecureBoot");
}
int is_efi_secure_boot_setup_mode(void) {
return read_flag("SetupMode");
}
int efi_get_variable(
const char *name,
void **value,
_cleanup_free_ char *p = NULL;
uint32_t a;
ssize_t n;
_cleanup_free_ void *buf;
if (asprintf(&p,
"/sys/firmware/efi/efivars/%s-%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
return -ENOMEM;
if (fd < 0)
return -errno;
return -errno;
return -EIO;
return -E2BIG;
if (n < 0)
return -errno;
if (n != sizeof(a))
return -EIO;
if (!buf)
return -ENOMEM;
if (n < 0)
return -errno;
return -EIO;
/* Always NUL terminate (2 bytes, to protect UTF-16) */
if (attribute)
*attribute = a;
return 0;
}
int efi_set_variable(
const char *name,
const void *value,
struct var {
char buf[];
_cleanup_free_ char *p = NULL;
if (asprintf(&p,
"/sys/firmware/efi/efivars/%s-%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
return -ENOMEM;
if (size == 0) {
if (unlink(p) < 0)
return -errno;
return 0;
}
if (fd < 0)
return -errno;
if (!buf)
return -ENOMEM;
}
_cleanup_free_ void *s = NULL;
int r;
char *x;
if (r < 0)
return r;
x = utf16_to_utf8(s, ss);
if (!x)
return -ENOMEM;
*p = x;
return 0;
}
size_t l = 0;
while (s[l] > 0)
l++;
return (l+1) * sizeof(uint16_t);
}
struct uuid {
} _packed_;
}
int efi_get_boot_option(
char **title,
char **path,
bool *active) {
char boot_id[9];
size_t l;
struct boot_option *header;
int r;
if (r < 0)
return r;
if (l < sizeof(struct boot_option))
return -ENOENT;
return -EINVAL;
if (title) {
if (!s)
return -ENOMEM;
}
dnext = 0;
struct device_path *dpath;
break;
/* Type 0x7F – End of Hardware Device Path, Sub-Type 0xFF – End Entire Device Path */
break;
/* Type 0x04 – Media Device Path */
continue;
/* Sub-Type 1 – Hard Drive */
/* 0x02 – GUID Partition Table */
continue;
/* 0x02 – GUID signature */
continue;
if (part_uuid)
continue;
}
/* Sub-Type 4 – File Path */
continue;
}
}
}
if (title) {
*title = s;
s = NULL;
}
if (part_uuid)
if (path) {
*path = p;
p = NULL;
}
if (active)
return 0;
}
int i;
for (i = 0; src[i] != '\0'; i++)
dest[i] = '\0';
}
struct guid {
} _packed_;
}
uint16_t *p;
for (p = s; *p; p++)
if (*p == '/')
*p = '\\';
return s;
}
char *efi_tilt_backslashes(char *s) {
char *p;
for (p = s; *p; p++)
if (*p == '\\')
*p = '/';
return s;
}
char boot_id[9];
struct boot_option *option;
struct device_path *devicep;
sizeof(struct drive_path) +
if (!buf)
return -ENOMEM;
/* header */
/* partition info */
/* path to loader */
/* end of path */
}
char boot_id[9];
}
size_t l;
int r;
if (r < 0)
return r;
if (l <= 0)
return -ENOENT;
if (l % sizeof(uint16_t) > 0 ||
return -EINVAL;
return (int) (l / sizeof(uint16_t));
}
}
static int boot_id_hex(const char s[4]) {
int i;
int id = 0;
for (i = 0; i < 4; i++)
if (s[i] >= '0' && s[i] <= '9')
else if (s[i] >= 'A' && s[i] <= 'F')
else
return -EINVAL;
return id;
}
return (int)*a - (int)*b;
}
int count = 0;
if (!dir)
return -errno;
int id;
continue;
continue;
continue;
if (id < 0)
continue;
return -ENOMEM;
}
return count;
}
_cleanup_free_ char *j = NULL;
int r;
uint64_t x = 0;
assert(u);
if (r < 0)
return r;
r = safe_atou64(j, &x);
if (r < 0)
return r;
*u = x;
return 0;
}
uint64_t x, y;
int r;
if (r < 0)
return r;
if (r < 0)
return r;
if (y == 0 || y < x)
return -EIO;
if (y > USEC_PER_HOUR)
return -EIO;
*firmware = x;
*loader = y;
return 0;
}
int efi_loader_get_device_part_uuid(sd_id128_t *u) {
_cleanup_free_ char *p = NULL;
int r, parsed[16];
if (r < 0)
return r;
if (sscanf(p, "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
return -EIO;
if (u) {
unsigned i;
for (i = 0; i < ELEMENTSOF(parsed); i++)
}
return 0;
}
#endif