summaryrefslogtreecommitdiff
path: root/src/boot/boot-efi.c
blob: 426652595e4d45ede41707713753c317bca902ee (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/

/***
  This file is part of systemd.

  Copyright 2013 Kay Sievers

  systemd is free software; you can redistribute it and/or modify it
  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 <stdlib.h>
#include <stdbool.h>
#include <unistd.h>
#include <getopt.h>
#include <locale.h>
#include <string.h>
#include <fnmatch.h>
#include <fcntl.h>
#include <sys/timex.h>

#include "boot.h"
#include "boot-loader.h"
#include "build.h"
#include "util.h"
#include "strv.h"
#include "efivars.h"
#include "conf-files.h"

static char *tilt_slashes(char *s) {
        char *p;

        if (!s)
                return NULL;

        for (p = s; *p; p++)
                if (*p == '\\')
                        *p = '/';
        return s;
}

static int get_boot_entries(struct boot_info *info) {
        uint16_t *list;
        int i, n;
        int err = 0;

        n = efi_get_boot_options(&list);
        if (n < 0)
                return n;

        for (i = 0; i < n; i++) {
                struct boot_info_entry *e;

                e = realloc(info->fw_entries, (info->fw_entries_count+1) * sizeof(struct boot_info_entry));
                if (!e) {
                        err = -ENOMEM;
                                break;
                }
                info->fw_entries = e;

                e = &info->fw_entries[info->fw_entries_count];
                memzero(e, sizeof(struct boot_info_entry));
                e->order = -1;

                err = efi_get_boot_option(list[i], &e->title, &e->part_uuid, &e->path);
                if (err < 0)
                        continue;

                if (isempty(e->title)) {
                        free(e->title);
                        e->title = NULL;
                }
                tilt_slashes(e->path);

                e->id = list[i];
                info->fw_entries_count++;
        }

        free(list);
        return err;
}

static int find_active_entry(struct boot_info *info) {
        uint16_t boot_cur;
        void *buf;
        size_t l;
        size_t i;
        int err;

        err = efi_get_variable(EFI_VENDOR_GLOBAL, "BootCurrent", NULL, &buf, &l);
        if (err < 0)
                return err;

        memcpy(&boot_cur, buf, sizeof(uint16_t));
        for (i = 0; i < info->fw_entries_count; i++) {
                if (info->fw_entries[i].id != boot_cur)
                        continue;
                info->fw_entry_active = i;
                err = 0;
                break;
        }
        free(buf);
        return err;
}

static int get_boot_order(struct boot_info *info) {
        size_t i, k;
        int r;

        r = efi_get_boot_order(&info->fw_entries_order);
        if (r < 0)
                return r;

        info->fw_entries_order_count = r;

        for (i = 0; i < info->fw_entries_order_count; i++) {
                for (k = 0; k < info->fw_entries_count; k++) {
                        if (info->fw_entries[k].id != info->fw_entries_order[i])
                                continue;
                        info->fw_entries[k].order = i;
                        break;
                }
        }

        return 0;
}

static int entry_cmp(const void *a, const void *b) {
        const struct boot_info_entry *e1 = a;
        const struct boot_info_entry *e2 = b;

        /* boot order of active entries */
        if (e1->order > 0 && e2->order > 0)
                return e1->order - e2->order;

        /* sort active entries before inactive ones */
        if (e1->order > 0)
                return 1;
        if (e2->order > 0)
                return -1;

        /* order of inactive entries */
        return e1->id - e2->id;
}

int boot_info_query(struct boot_info *info) {
        char str[64];
        char buf[64];
        char *loader_active = NULL;

        info->fw_secure_boot = is_efi_secure_boot();
        info->fw_secure_boot_setup_mode = is_efi_secure_boot_setup_mode();

        efi_get_variable_string(EFI_VENDOR_LOADER, "LoaderInfo", &info->loader);

        get_boot_entries(info);
        if (info->fw_entries_count > 0) {
                get_boot_order(info);
                qsort(info->fw_entries, info->fw_entries_count, sizeof(struct boot_info_entry), entry_cmp);
                find_active_entry(info);
        }

        efi_get_variable_string(EFI_VENDOR_LOADER, "LoaderFirmwareType", &info->fw_type);
        efi_get_variable_string(EFI_VENDOR_LOADER, "LoaderFirmwareInfo", &info->fw_info);
        efi_get_variable_string(EFI_VENDOR_LOADER, "LoaderImageIdentifier", &info->loader_image_path);
        tilt_slashes(info->loader_image_path);
        efi_loader_get_device_part_uuid(&info->loader_part_uuid);

        boot_loader_read_entries(info);
        efi_get_variable_string(EFI_VENDOR_LOADER, "LoaderEntrySelected", &loader_active);
        if (loader_active) {
                boot_loader_find_active_entry(info, loader_active);
                free(loader_active);
        }

        snprintf(str, sizeof(str), "LoaderEntryOptions-%s", sd_id128_to_string(info->machine_id, buf));
        efi_get_variable_string(EFI_VENDOR_LOADER, str, &info->loader_options_added);

        return 0;
}