c51d84dc09476d9c06b8aac726220bf3c7d62e8dKay Sievers/***
c51d84dc09476d9c06b8aac726220bf3c7d62e8dKay Sievers This file is part of systemd.
c51d84dc09476d9c06b8aac726220bf3c7d62e8dKay Sievers
c51d84dc09476d9c06b8aac726220bf3c7d62e8dKay Sievers Copyright 2012 Lennart Poettering
c51d84dc09476d9c06b8aac726220bf3c7d62e8dKay Sievers Copyright 2013 Kay Sievers
c51d84dc09476d9c06b8aac726220bf3c7d62e8dKay Sievers
c51d84dc09476d9c06b8aac726220bf3c7d62e8dKay Sievers systemd is free software; you can redistribute it and/or modify it
c51d84dc09476d9c06b8aac726220bf3c7d62e8dKay Sievers under the terms of the GNU Lesser General Public License as published by
c51d84dc09476d9c06b8aac726220bf3c7d62e8dKay Sievers the Free Software Foundation; either version 2.1 of the License, or
c51d84dc09476d9c06b8aac726220bf3c7d62e8dKay Sievers (at your option) any later version.
c51d84dc09476d9c06b8aac726220bf3c7d62e8dKay Sievers
c51d84dc09476d9c06b8aac726220bf3c7d62e8dKay Sievers systemd is distributed in the hope that it will be useful, but
c51d84dc09476d9c06b8aac726220bf3c7d62e8dKay Sievers WITHOUT ANY WARRANTY; without even the implied warranty of
c51d84dc09476d9c06b8aac726220bf3c7d62e8dKay Sievers MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
c51d84dc09476d9c06b8aac726220bf3c7d62e8dKay Sievers Lesser General Public License for more details.
c51d84dc09476d9c06b8aac726220bf3c7d62e8dKay Sievers
c51d84dc09476d9c06b8aac726220bf3c7d62e8dKay Sievers You should have received a copy of the GNU Lesser General Public License
c51d84dc09476d9c06b8aac726220bf3c7d62e8dKay Sievers along with systemd; If not, see <http://www.gnu.org/licenses/>.
c51d84dc09476d9c06b8aac726220bf3c7d62e8dKay Sievers***/
c51d84dc09476d9c06b8aac726220bf3c7d62e8dKay Sievers
c51d84dc09476d9c06b8aac726220bf3c7d62e8dKay Sievers#include "acpi-fpdt.h"
cf0fbc49e67b55f8d346fc94de28c90113505297Thomas Hindoe Paaboel Andersen#include "boot-timestamps.h"
c51d84dc09476d9c06b8aac726220bf3c7d62e8dKay Sievers#include "efivars.h"
a8fbdf5424be099ba1b2b1ec261c02b8759d6b0cThomas Hindoe Paaboel Andersen#include "macro.h"
a8fbdf5424be099ba1b2b1ec261c02b8759d6b0cThomas Hindoe Paaboel Andersen#include "time-util.h"
c51d84dc09476d9c06b8aac726220bf3c7d62e8dKay Sievers
c51d84dc09476d9c06b8aac726220bf3c7d62e8dKay Sieversint boot_timestamps(const dual_timestamp *n, dual_timestamp *firmware, dual_timestamp *loader) {
39883f622f392d8579f4428fc5a789a102efbb10Lennart Poettering usec_t x = 0, y = 0, a;
c51d84dc09476d9c06b8aac726220bf3c7d62e8dKay Sievers int r;
c51d84dc09476d9c06b8aac726220bf3c7d62e8dKay Sievers dual_timestamp _n;
c51d84dc09476d9c06b8aac726220bf3c7d62e8dKay Sievers
c51d84dc09476d9c06b8aac726220bf3c7d62e8dKay Sievers assert(firmware);
c51d84dc09476d9c06b8aac726220bf3c7d62e8dKay Sievers assert(loader);
c51d84dc09476d9c06b8aac726220bf3c7d62e8dKay Sievers
c51d84dc09476d9c06b8aac726220bf3c7d62e8dKay Sievers if (!n) {
c51d84dc09476d9c06b8aac726220bf3c7d62e8dKay Sievers dual_timestamp_get(&_n);
c51d84dc09476d9c06b8aac726220bf3c7d62e8dKay Sievers n = &_n;
c51d84dc09476d9c06b8aac726220bf3c7d62e8dKay Sievers }
c51d84dc09476d9c06b8aac726220bf3c7d62e8dKay Sievers
c51d84dc09476d9c06b8aac726220bf3c7d62e8dKay Sievers r = acpi_get_boot_usec(&x, &y);
c51d84dc09476d9c06b8aac726220bf3c7d62e8dKay Sievers if (r < 0) {
c51d84dc09476d9c06b8aac726220bf3c7d62e8dKay Sievers r = efi_loader_get_boot_usec(&x, &y);
c51d84dc09476d9c06b8aac726220bf3c7d62e8dKay Sievers if (r < 0)
c51d84dc09476d9c06b8aac726220bf3c7d62e8dKay Sievers return r;
c51d84dc09476d9c06b8aac726220bf3c7d62e8dKay Sievers }
c51d84dc09476d9c06b8aac726220bf3c7d62e8dKay Sievers
c51d84dc09476d9c06b8aac726220bf3c7d62e8dKay Sievers /* Let's convert this to timestamps where the firmware
c51d84dc09476d9c06b8aac726220bf3c7d62e8dKay Sievers * began/loader began working. To make this more confusing:
c51d84dc09476d9c06b8aac726220bf3c7d62e8dKay Sievers * since usec_t is unsigned and the kernel's monotonic clock
c51d84dc09476d9c06b8aac726220bf3c7d62e8dKay Sievers * begins at kernel initialization we'll actually initialize
c51d84dc09476d9c06b8aac726220bf3c7d62e8dKay Sievers * the monotonic timestamps here as negative of the actual
c51d84dc09476d9c06b8aac726220bf3c7d62e8dKay Sievers * value. */
c51d84dc09476d9c06b8aac726220bf3c7d62e8dKay Sievers
c51d84dc09476d9c06b8aac726220bf3c7d62e8dKay Sievers firmware->monotonic = y;
c51d84dc09476d9c06b8aac726220bf3c7d62e8dKay Sievers loader->monotonic = y - x;
c51d84dc09476d9c06b8aac726220bf3c7d62e8dKay Sievers
c51d84dc09476d9c06b8aac726220bf3c7d62e8dKay Sievers a = n->monotonic + firmware->monotonic;
c51d84dc09476d9c06b8aac726220bf3c7d62e8dKay Sievers firmware->realtime = n->realtime > a ? n->realtime - a : 0;
c51d84dc09476d9c06b8aac726220bf3c7d62e8dKay Sievers
c51d84dc09476d9c06b8aac726220bf3c7d62e8dKay Sievers a = n->monotonic + loader->monotonic;
c51d84dc09476d9c06b8aac726220bf3c7d62e8dKay Sievers loader->realtime = n->realtime > a ? n->realtime - a : 0;
c51d84dc09476d9c06b8aac726220bf3c7d62e8dKay Sievers
c51d84dc09476d9c06b8aac726220bf3c7d62e8dKay Sievers return 0;
c51d84dc09476d9c06b8aac726220bf3c7d62e8dKay Sievers}