Initial commit.
[virt-efivars.git] / editor.c
1 /* virt-efivars editor.
2  * Copyright (C) 2015 Red Hat Inc.
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17  */
18
19 #include <config.h>
20
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <stdint.h>
24 #include <unistd.h>
25
26 #include <efivar.h>
27
28 #define GUID_FORMAT "%08x-%04x-%04x-%04x-%02x%02x%02x%02x%02x%02x"
29
30 static void
31 list_variables (void)
32 {
33   efi_guid_t *guid = NULL;
34   char *name = NULL;
35   int r;
36
37   while ((r = efi_get_next_variable_name (&guid, &name)) > 0)
38     printf (GUID_FORMAT "-%s\n",
39             guid->a, guid->b, guid->c, bswap_16 (guid->d),
40             guid->e[0], guid->e[1], guid->e[2], guid->e[3],
41             guid->e[4], guid->e[5], name);
42
43   if (r < 0) {
44     perror ("efi_get_next_variable_name");
45     exit (EXIT_FAILURE);
46   }
47 }
48
49 static void
50 add_variable (void)
51 {
52   uint8_t data[] = "rwmjtestdata";
53
54   if (efi_set_variable (efi_guid_redhat, "RWMJTest",
55                         data, sizeof data,
56                         EFI_VARIABLE_BOOTSERVICE_ACCESS |
57                         EFI_VARIABLE_RUNTIME_ACCESS |
58                         EFI_VARIABLE_NON_VOLATILE) < 0) {
59     perror ("efi_set_variable");
60     exit (EXIT_FAILURE);
61   }
62 }
63
64 int
65 main (int argc, char *argv[])
66 {
67   printf ("*** editor running ***\n");
68
69   if (!efi_variables_supported ()) {
70     fprintf (stderr, "UEFI variables are not supported!\n");
71     exit (EXIT_FAILURE);
72   }
73
74   list_variables ();
75
76   add_variable ();
77
78   printf ("*** editor finished ***\n");
79   exit (EXIT_SUCCESS);
80 }