efivars.c revision 34e5a31ec5897de8ba1436dad53df99637569d0a
/*-*- 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 <fcntl.h>
#include "util.h"
#include "utf8.h"
#include "efivars.h"
bool is_efiboot(void) {
}
int efi_get_variable(sd_id128_t vendor, const char *name, uint32_t *attribute, void **value, size_t *size) {
_cleanup_free_ char *p = NULL;
uint32_t a;
ssize_t n;
void *r;
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 (int) n;
if (n != sizeof(a))
return -EIO;
if (!r)
return -ENOMEM;
if (n < 0) {
free(r);
return (int) -n;
}
free(r);
return -EIO;
}
/* Always NUL terminate (2 bytes, to protect UTF-16) */
*value = r;
if (attribute)
*attribute = a;
return 0;
}
static int read_bogomips(unsigned long *u) {
if (!f)
return -errno;
while (!feof(f)) {
char *x;
unsigned long a, b;
return -EIO;
continue;
x = line + 8;
x += strspn(x, WHITESPACE);
if (*x != ':')
continue;
x++;
x += strspn(x, WHITESPACE);
continue;
*u = a * 1000000L + b * 10000L;
return 0;
}
return -EIO;
}
_cleanup_free_ void *i = NULL;
_cleanup_free_ char *j = NULL;
int r;
uint64_t x;
assert(u);
if (r < 0)
return r;
j = utf16_to_utf8(i, is);
if (!j)
return -ENOMEM;
r = safe_atou64(j, &x);
if (r < 0)
return r;
*u = USEC_PER_SEC * x / speed;
return 0;
}
uint64_t x, y;
int r;
unsigned long bogomips;
/* Returns the usec after the CPU was turned on. The two
* timestamps are: the firmware finished, and the boot loader
* finished. */
/* We assume that the kernel's bogomips value is calibrated to
* twice the CPU frequency, and use this to convert the TSC
* ticks into usec. Of course, bogomips are only vaguely
* defined. If this breaks one day we can come up with
* something better. However, for now this saves us from doing
* a local calibration loop. */
r = read_bogomips(&bogomips);
if (r < 0)
return 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_get_boot_timestamps(const dual_timestamp *n, dual_timestamp *firmware, dual_timestamp *loader) {
usec_t x, y, a;
int r;
if (!n) {
n = &_n;
}
r = get_boot_usec(&x, &y);
if (r < 0)
return r;
/* Let's convert this to timestamps where the firmware
* since usec_t is unsigned and the kernel's monotonic clock
* begins at kernel initialization we'll actually initialize
* the monotonic timestamps here as negative of the actual
* value. */
return 0;
}