1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2013 Kay Sievers
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
30 #include <sys/timex.h>
33 #include "boot-loader.h"
38 #include "conf-files.h"
40 static int get_boot_entries(struct boot_info *info) {
45 d = opendir("/sys/firmware/efi/efivars");
49 for (dent = readdir(d); dent != NULL; dent = readdir(d)) {
51 struct boot_info_entry *e;
53 if (dent->d_name[0] == '.')
55 if (sscanf(dent->d_name, "Boot%04X-8be4df61-93ca-11d2-aa0d-00e098032b8c", &id) != 1)
58 e = realloc(info->fw_entries, (info->fw_entries_count+1) * sizeof(struct boot_info_entry));
65 e = &info->fw_entries[info->fw_entries_count];
66 memset(e, 0, sizeof(struct boot_info_entry));
69 err = efi_get_boot_option(id, NULL, &e->title, &e->part_uuid, &e->path, &e->data, &e->data_size);
74 info->fw_entries_count++;
81 static int find_active_entry(struct boot_info *info) {
88 err = efi_get_variable(EFI_VENDOR_GLOBAL, "BootCurrent", NULL, &buf, &l);
92 memcpy(&boot_cur, buf, sizeof(uint16_t));
93 for (i = 0; i < info->fw_entries_count; i++) {
94 if (info->fw_entries[i].id != boot_cur)
96 info->fw_entry_active = i;
104 static int get_boot_order(struct boot_info *info) {
108 err = efi_get_boot_order(&info->fw_entries_order, &info->fw_entries_order_count);
112 for (i = 0; i < info->fw_entries_order_count; i++) {
113 for (k = 0; k < info->fw_entries_count; k++) {
114 if (info->fw_entries[k].id != info->fw_entries_order[i])
116 info->fw_entries[k].order = i;
124 static int entry_cmp(const void *a, const void *b) {
125 const struct boot_info_entry *e1 = a;
126 const struct boot_info_entry *e2 = b;
128 /* boot order of active entries */
129 if (e1->order > 0 && e2->order > 0)
130 return e1->order - e2->order;
132 /* sort active entries before inactive ones */
138 /* order of inactive entries */
139 return e1->id - e2->id;
142 int boot_info_query(struct boot_info *info) {
147 info->loader = efi_get_variable_string(EFI_VENDOR_LOADER, "LoaderInfo");
149 get_boot_entries(info);
150 if (info->fw_entries_count > 0) {
151 get_boot_order(info);
152 qsort(info->fw_entries, info->fw_entries_count, sizeof(struct boot_info_entry), entry_cmp);
153 find_active_entry(info);
156 info->fw_type = efi_get_variable_string(EFI_VENDOR_LOADER, "LoaderFirmwareType");
157 info->fw_info = efi_get_variable_string(EFI_VENDOR_LOADER, "LoaderFirmwareInfo");
158 info->loader_image_path = efi_get_variable_string(EFI_VENDOR_LOADER, "LoaderImageIdentifier");
159 efi_get_loader_device_part_uuid(&info->loader_part_uuid);
161 boot_loader_read_entries(info);
162 loader_active = efi_get_variable_string(EFI_VENDOR_LOADER, "LoaderEntrySelected");
164 boot_loader_find_active_entry(info, loader_active);
168 snprintf(str, sizeof(str), "LoaderEntryOptions-%s", sd_id128_to_string(info->machine_id, buf));
169 info->loader_options_added = efi_get_variable_string(EFI_VENDOR_LOADER, str);