]> git.ozlabs.org Git - petitboot/blob - test/lib/test-efivar.c
1be95fd39450c8fdf95286ffa9b642da3afb4bfa
[petitboot] / test / lib / test-efivar.c
1 /*
2  *  This program is free software; you can redistribute it and/or modify
3  *  it under the terms of the GNU General Public License as published by
4  *  the Free Software Foundation; version 2 of the License.
5  *
6  *  This program is distributed in the hope that it will be useful,
7  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
8  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
9  *  GNU General Public License for more details.
10  *
11  *  You should have received a copy of the GNU General Public License
12  *  along with this program; if not, write to the Free Software
13  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
14  *
15  *  Copyright (C) 2018 Huaxintong Semiconductor Technology Co.,Ltd. All rights
16  *  reserved.
17  *  Author: Ge Song <ge.song@hxt-semitech.com>
18  */
19
20 #include <assert.h>
21 #include <errno.h>
22 #include <stdbool.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26
27 #include <sys/stat.h>
28 #include <sys/statfs.h>
29 #include <sys/types.h>
30
31 #include "efi/efivar.h"
32 #include "log/log.h"
33 #include "talloc/talloc.h"
34
35 static const struct efi_mount efi_mount = {
36         .path = "/tmp/pb-test-efivar",
37         .guid = "c9c07add-256e-4452-b911-f8d0d35a1ac7",
38 };
39 static const char v_name[] = "petitboot-test-one";
40 static const char v_data[] = "petitboot-efi-tester";
41
42 void finish(int code)
43 {
44         efi_del_variable(&efi_mount, v_name);
45         rmdir(efi_mount.path);
46         exit(code);
47 }
48
49 int main(void)
50 {
51         struct efi_data *efi_data;
52         int rc;
53
54         __pb_log_init(stderr, true);
55
56         rc = mkdir(efi_mount.path, 0755);
57         if (rc) {
58                 if (errno == EEXIST)
59                         pb_log("mkdir exists\n");
60                 else {
61                         pb_log("mkdir failed: (%d) %s\n", errno,
62                                strerror(errno));
63                         finish(__LINE__);
64                 }
65         }
66
67         if (!efi_check_mount_magic(&efi_mount, false))
68                 finish(__LINE__);
69
70         efi_data = talloc_zero(NULL, struct efi_data);
71         efi_data->attributes = EFI_DEFALT_ATTRIBUTES;
72         efi_data->data = talloc_strdup(efi_data, v_data);
73         efi_data->data_size = sizeof(v_data);
74
75         if (efi_set_variable(&efi_mount, v_name, efi_data))
76                 finish(__LINE__);
77
78         talloc_free(efi_data);
79
80         if (efi_get_variable(NULL, &efi_mount, v_name, &efi_data))
81                 finish(__LINE__);
82
83         if (!efi_data->data) {
84                 pb_log("No efi_data->data\n");
85                 finish(__LINE__);
86         }
87
88         if (strcmp((char *)efi_data->data, v_data)) {
89                 pb_log("Bad efi_data->data: '%s' != '%s'\n",
90                        (char *)efi_data->data, v_data);
91                 finish(__LINE__);
92         }
93
94         if (efi_del_variable(&efi_mount, v_name))
95                 finish(__LINE__);
96
97         /* Get after delete should fail. */
98         if (!efi_get_variable(NULL, &efi_mount, v_name, &efi_data))
99                 finish(__LINE__);
100
101         finish(EXIT_SUCCESS);
102 }