Upgrade bluez5_37 :Merge the code from private
[platform/upstream/bluez.git] / peripheral / efivars.c
1 /*
2  *
3  *  BlueZ - Bluetooth protocol stack for Linux
4  *
5  *  Copyright (C) 2015  Intel Corporation. All rights reserved.
6  *
7  *
8  *  This library is free software; you can redistribute it and/or
9  *  modify it under the terms of the GNU Lesser General Public
10  *  License as published by the Free Software Foundation; either
11  *  version 2.1 of the License, or (at your option) any later version.
12  *
13  *  This library is distributed in the hope that it will be useful,
14  *  but 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
19  *  License along with this library; if not, write to the Free Software
20  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
21  *
22  */
23
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27
28 #include <stdio.h>
29 #include <errno.h>
30 #include <fcntl.h>
31 #include <unistd.h>
32 #include <string.h>
33 #include <stdlib.h>
34 #include <stdint.h>
35 #include <sys/stat.h>
36 #include <sys/types.h>
37 #include <sys/param.h>
38 #include <sys/uio.h>
39
40 #include "peripheral/efivars.h"
41
42 #define SYSFS_EFIVARS "/sys/firmware/efi/efivars"
43
44 typedef struct {
45     uint32_t data1;
46     uint16_t data2;
47     uint16_t data3;
48     uint8_t  data4[8];
49 } efi_guid_t;
50
51 #define VENDOR_GUID \
52         (efi_guid_t) { 0xd5f9d775, 0x1a09, 0x4e89, \
53                         { 0x96, 0xcf, 0x1d, 0x19, 0x55, 0x4d, 0xa6, 0x67 } }
54
55 static void efivars_pathname(const char *name, char *pathname, size_t size)
56 {
57         static efi_guid_t guid = VENDOR_GUID;
58
59         snprintf(pathname, size - 1,
60                 "%s/%s-%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
61                 SYSFS_EFIVARS, name, guid.data1, guid.data2, guid.data3,
62                 guid.data4[0], guid.data4[1], guid.data4[2], guid.data4[3],
63                 guid.data4[4], guid.data4[5], guid.data4[6], guid.data4[7]);
64 }
65
66 int efivars_read(const char *name, uint32_t *attributes,
67                                         void *data, size_t size)
68 {
69         char pathname[PATH_MAX];
70         struct iovec iov[2];
71         uint32_t attr;
72         ssize_t len;
73         int fd;
74
75         efivars_pathname(name, pathname, PATH_MAX);
76
77         fd = open(pathname, O_RDONLY | O_CLOEXEC);
78         if (fd < 0)
79                 return -EIO;
80
81         iov[0].iov_base = &attr;
82         iov[0].iov_len = sizeof(attr);
83         iov[1].iov_base = data;
84         iov[1].iov_len = size;
85
86         len = readv(fd, iov, 2);
87
88         close(fd);
89
90         if (len < 0)
91                 return -EIO;
92
93         if (attributes)
94                 *attributes = attr;
95
96         return 0;
97 }
98
99 int efivars_write(const char *name, uint32_t attributes,
100                                         const void *data, size_t size)
101 {
102         char pathname[PATH_MAX];
103         void *buf;
104         ssize_t written;
105         int fd;
106
107         efivars_pathname(name, pathname, PATH_MAX);
108
109         buf = malloc(size + sizeof(attributes));
110         if (!buf)
111                 return -ENOMEM;
112
113         fd = open(pathname, O_CREAT | O_WRONLY | O_TRUNC | O_CLOEXEC,
114                                 S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
115         if (fd < 0) {
116                 free(buf);
117                 return -EIO;
118         }
119
120         memcpy(buf, &attributes, sizeof(attributes));
121         memcpy(buf + sizeof(attributes), data, size);
122
123         written = write(fd, buf, size + sizeof(attributes));
124
125         close(fd);
126         free(buf);
127
128         if (written < 0)
129                 return -EIO;
130
131         return 0;
132 }