chiark / gitweb /
ac5f9c12a76e76755f1488db8b3e9233c9a0edfa
[elogind.git] / src / boot / boot-efi.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /***
4   This file is part of systemd.
5
6   Copyright 2013 Kay Sievers
7
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.
12
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.
17
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/>.
20 ***/
21
22 #include <stdlib.h>
23 #include <stdbool.h>
24 #include <unistd.h>
25 #include <getopt.h>
26 #include <locale.h>
27 #include <string.h>
28 #include <fnmatch.h>
29 #include <fcntl.h>
30 #include <sys/timex.h>
31
32 #include "boot.h"
33 #include "boot-loader.h"
34 #include "build.h"
35 #include "util.h"
36 #include "strv.h"
37 #include "efivars.h"
38 #include "conf-files.h"
39
40 static int get_boot_entries(struct boot_info *info) {
41         DIR *d = NULL;
42         struct dirent *dent;
43         int err = 0;
44
45         d = opendir("/sys/firmware/efi/efivars");
46         if (!d)
47                 return -errno;
48
49         for (dent = readdir(d); dent != NULL; dent = readdir(d)) {
50                 unsigned int id;
51                 struct boot_info_entry *e;
52
53                 if (dent->d_name[0] == '.')
54                         continue;
55                 if (sscanf(dent->d_name, "Boot%04X-8be4df61-93ca-11d2-aa0d-00e098032b8c", &id) != 1)
56                         continue;
57
58                 e = realloc(info->fw_entries, (info->fw_entries_count+1) * sizeof(struct boot_info_entry));
59                 if (!e) {
60                         err = -ENOMEM;
61                         break;
62                 }
63                 info->fw_entries = e;
64
65                 e = &info->fw_entries[info->fw_entries_count];
66                 memset(e, 0, sizeof(struct boot_info_entry));
67                 e->order = -1;
68
69                 err = efi_get_boot_option(id, &e->title, &e->part_uuid, &e->path);
70                 if (err < 0)
71                         break;
72                 e->id = id;
73
74                 info->fw_entries_count++;
75         }
76         closedir(d);
77
78         return err;
79 }
80
81 static int find_active_entry(struct boot_info *info) {
82         uint16_t boot_cur;
83         void *buf;
84         size_t l;
85         size_t i;
86         int err = -ENOENT;
87
88         err = efi_get_variable(EFI_VENDOR_GLOBAL, "BootCurrent", NULL, &buf, &l);
89         if (err < 0)
90                 return err;
91
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)
95                         continue;
96                 info->fw_entry_active = i;
97                 err = 0;
98                 break;
99         }
100         free(buf);
101         return err;
102 }
103
104 static int get_boot_order(struct boot_info *info) {
105         size_t i, k;
106         int r;
107
108         r = efi_get_boot_order(&info->fw_entries_order);
109         if (r < 0)
110                 return r;
111
112         info->fw_entries_order_count = r;
113
114         for (i = 0; i < info->fw_entries_order_count; i++) {
115                 for (k = 0; k < info->fw_entries_count; k++) {
116                         if (info->fw_entries[k].id != info->fw_entries_order[i])
117                                 continue;
118                         info->fw_entries[k].order = i;
119                         break;
120                 }
121         }
122
123         return 0;
124 }
125
126 static int entry_cmp(const void *a, const void *b) {
127         const struct boot_info_entry *e1 = a;
128         const struct boot_info_entry *e2 = b;
129
130         /* boot order of active entries */
131         if (e1->order > 0 && e2->order > 0)
132                 return e1->order - e2->order;
133
134         /* sort active entries before inactive ones */
135         if (e1->order > 0)
136                 return 1;
137         if (e2->order > 0)
138                 return -1;
139
140         /* order of inactive entries */
141         return e1->id - e2->id;
142 }
143
144 int boot_info_query(struct boot_info *info) {
145         char str[64];
146         char buf[64];
147         char *loader_active = NULL;
148
149         efi_get_variable_string(EFI_VENDOR_LOADER, "LoaderInfo", &info->loader);
150
151         get_boot_entries(info);
152         if (info->fw_entries_count > 0) {
153                 get_boot_order(info);
154                 qsort(info->fw_entries, info->fw_entries_count, sizeof(struct boot_info_entry), entry_cmp);
155                 find_active_entry(info);
156         }
157
158         efi_get_variable_string(EFI_VENDOR_LOADER, "LoaderFirmwareType", &info->fw_type);
159         efi_get_variable_string(EFI_VENDOR_LOADER, "LoaderFirmwareInfo", &info->fw_info);
160         efi_get_variable_string(EFI_VENDOR_LOADER, "LoaderImageIdentifier", &info->loader_image_path);
161         efi_get_loader_device_part_uuid(&info->loader_part_uuid);
162
163         boot_loader_read_entries(info);
164         efi_get_variable_string(EFI_VENDOR_LOADER, "LoaderEntrySelected", &loader_active);
165         if (loader_active) {
166                 boot_loader_find_active_entry(info, loader_active);
167                 free(loader_active);
168         }
169
170         snprintf(str, sizeof(str), "LoaderEntryOptions-%s", sd_id128_to_string(info->machine_id, buf));
171         efi_get_variable_string(EFI_VENDOR_LOADER, str, &info->loader_options_added);
172
173         return 0;
174 }