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 char *tilt_slashes(char *s) {
52 static int get_boot_entries(struct boot_info *info) {
57 n = efi_get_boot_options(&list);
61 for (i = 0; i < n; i++) {
62 struct boot_info_entry *e;
64 e = realloc(info->fw_entries, (info->fw_entries_count+1) * sizeof(struct boot_info_entry));
71 e = &info->fw_entries[info->fw_entries_count];
72 memset(e, 0, sizeof(struct boot_info_entry));
75 err = efi_get_boot_option(list[i], &e->title, &e->part_uuid, &e->path);
79 if (isempty(e->title)) {
83 tilt_slashes(e->path);
86 info->fw_entries_count++;
93 static int find_active_entry(struct boot_info *info) {
100 err = efi_get_variable(EFI_VENDOR_GLOBAL, "BootCurrent", NULL, &buf, &l);
104 memcpy(&boot_cur, buf, sizeof(uint16_t));
105 for (i = 0; i < info->fw_entries_count; i++) {
106 if (info->fw_entries[i].id != boot_cur)
108 info->fw_entry_active = i;
116 static int get_boot_order(struct boot_info *info) {
120 r = efi_get_boot_order(&info->fw_entries_order);
124 info->fw_entries_order_count = r;
126 for (i = 0; i < info->fw_entries_order_count; i++) {
127 for (k = 0; k < info->fw_entries_count; k++) {
128 if (info->fw_entries[k].id != info->fw_entries_order[i])
130 info->fw_entries[k].order = i;
138 static int entry_cmp(const void *a, const void *b) {
139 const struct boot_info_entry *e1 = a;
140 const struct boot_info_entry *e2 = b;
142 /* boot order of active entries */
143 if (e1->order > 0 && e2->order > 0)
144 return e1->order - e2->order;
146 /* sort active entries before inactive ones */
152 /* order of inactive entries */
153 return e1->id - e2->id;
156 int boot_info_query(struct boot_info *info) {
159 char *loader_active = NULL;
161 efi_get_variable_string(EFI_VENDOR_LOADER, "LoaderInfo", &info->loader);
163 get_boot_entries(info);
164 if (info->fw_entries_count > 0) {
165 get_boot_order(info);
166 qsort(info->fw_entries, info->fw_entries_count, sizeof(struct boot_info_entry), entry_cmp);
167 find_active_entry(info);
170 efi_get_variable_string(EFI_VENDOR_LOADER, "LoaderFirmwareType", &info->fw_type);
171 efi_get_variable_string(EFI_VENDOR_LOADER, "LoaderFirmwareInfo", &info->fw_info);
172 efi_get_variable_string(EFI_VENDOR_LOADER, "LoaderImageIdentifier", &info->loader_image_path);
173 tilt_slashes(info->loader_image_path);
174 efi_get_loader_device_part_uuid(&info->loader_part_uuid);
176 boot_loader_read_entries(info);
177 efi_get_variable_string(EFI_VENDOR_LOADER, "LoaderEntrySelected", &loader_active);
179 boot_loader_find_active_entry(info, loader_active);
183 snprintf(str, sizeof(str), "LoaderEntryOptions-%s", sd_id128_to_string(info->machine_id, buf));
184 efi_get_variable_string(EFI_VENDOR_LOADER, str, &info->loader_options_added);