OSDN Git Service

Move i2o device parsing to linux-i2o.c
[android-x86/external-efivar.git] / src / linux.h
1 /*
2  * libefiboot - library for the manipulation of EFI boot variables
3  * Copyright 2012-2015 Red Hat, Inc.
4  * Copyright (C) 2001 Dell Computer Corporation <Matt_Domsch@dell.com>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public License as
8  * published by the Free Software Foundation; either version 2.1 of the
9  * License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, see
18  * <http://www.gnu.org/licenses/>.
19  *
20  */
21 #ifndef _EFIBOOT_LINUX_H
22 #define _EFIBOOT_LINUX_H
23
24 struct pci_root_info {
25         uint16_t pci_root_domain;
26         uint8_t pci_root_bus;
27         uint32_t pci_root_acpi_hid;
28         uint64_t pci_root_acpi_uid;
29         char *pci_root_acpi_uid_str;
30 };
31
32 struct pci_dev_info {
33         uint16_t pci_domain;
34         uint8_t pci_bus;
35         uint8_t pci_device;
36         uint8_t pci_function;
37 };
38
39 struct scsi_info {
40         uint32_t scsi_bus;
41         uint32_t scsi_device;
42         uint32_t scsi_target;
43         uint64_t scsi_lun;
44 };
45
46 struct sas_info {
47         uint32_t scsi_bus;
48         uint32_t scsi_device;
49         uint32_t scsi_target;
50         uint64_t scsi_lun;
51
52         uint64_t sas_address;
53 };
54
55 struct sata_info {
56         uint32_t scsi_bus;
57         uint32_t scsi_device;
58         uint32_t scsi_target;
59         uint64_t scsi_lun;
60
61         uint32_t ata_devno;
62         uint32_t ata_port;
63         uint32_t ata_pmp;
64
65         uint32_t ata_print_id;
66 };
67
68 struct nvme_info {
69         int32_t ctrl_id;
70         int32_t ns_id;
71         int has_eui;
72         uint8_t eui[8];
73 };
74
75 struct disk_info {
76         int interface_type;
77         unsigned int controllernum;
78         unsigned int disknum;
79         unsigned char part;
80         uint64_t major;
81         uint32_t minor;
82         uint32_t edd10_devicenum;
83
84         struct pci_root_info pci_root;
85         struct pci_dev_info pci_dev;
86
87         union {
88                 struct scsi_info scsi_info;
89                 struct sas_info sas_info;
90                 struct sata_info sata_info;
91                 struct nvme_info nvme_info;
92                 efi_guid_t nvdimm_label;
93         };
94
95         char *disk_name;
96         char *part_name;
97 };
98
99 enum interface_type {
100         unknown = 0,
101         bus_type_unknown = 0, /* XXX remove later */
102         interface_type_unknown = 0, /* XXX remove later */
103         isa, pci, network,
104         ata, atapi, scsi, sata, sas,
105         usb, i1394, fibre, i2o,
106         md, virtblk,
107         nvme, nd_pmem,
108 };
109
110 struct dev_probe;
111
112 struct device {
113         enum interface_type interface_type;
114         char *link;
115         char *device;
116         char *driver;
117
118         struct dev_probe **probes;
119         unsigned int n_probes;
120
121         union {
122                 struct {
123                         struct stat stat;
124
125                         unsigned int controllernum;
126                         unsigned int disknum;
127                         int part;
128                         uint64_t major;
129                         uint32_t minor;
130                         uint32_t edd10_devicenum;
131
132                         char *disk_name;
133                         char *part_name;
134
135                         struct pci_root_info pci_root;
136                         unsigned int n_pci_devs;
137                         struct pci_dev_info *pci_dev;
138
139                         union {
140                                 struct scsi_info scsi_info;
141                                 struct sas_info sas_info;
142                                 struct sata_info sata_info;
143                                 struct nvme_info nvme_info;
144                                 efi_guid_t nvdimm_label;
145                         };
146                 };
147                 char *ifname;
148         };
149 };
150
151 extern int eb_disk_info_from_fd(int fd, struct disk_info *info);
152 extern int HIDDEN set_disk_and_part_name(struct disk_info *info);
153 extern int make_blockdev_path(uint8_t *buf, ssize_t size,
154                               struct disk_info *info);
155
156 extern int eb_nvme_ns_id(int fd, uint32_t *ns_id);
157
158 extern int HIDDEN get_partition_number(const char *devpath);
159
160 int UNUSED get_sector_size(int filedes);
161
162 extern int HIDDEN find_parent_devpath(const char * const child,
163                                       char **parent);
164
165 extern ssize_t HIDDEN make_mac_path(uint8_t *buf, ssize_t size,
166                                     const char * const ifname);
167
168 #define read_sysfs_file(buf, fmt, args...)                              \
169         ({                                                              \
170                 uint8_t *buf_ = NULL;                                   \
171                 ssize_t bufsize_ = -1;                                  \
172                 int error_;                                             \
173                                                                         \
174                 bufsize_ = get_file(&buf_, "/sys/" fmt, ## args);       \
175                 if (bufsize_ > 0) {                                     \
176                         uint8_t *buf2_ = alloca(bufsize_);              \
177                         error_ = errno;                                 \
178                         if (buf2_)                                      \
179                                 memcpy(buf2_, buf_, bufsize_);          \
180                         free(buf_);                                     \
181                         *(buf) = (__typeof__(*(buf)))buf2_;             \
182                         errno = error_;                                 \
183                 }                                                       \
184                 bufsize_;                                               \
185         })
186
187 #define sysfs_readlink(linkbuf, fmt, args...)                           \
188         ({                                                              \
189                 char *_lb = alloca(PATH_MAX+1);                         \
190                 char *_pn;                                              \
191                 int _rc;                                                \
192                                                                         \
193                 *(linkbuf) = NULL;                                      \
194                 _rc = asprintfa(&_pn, "/sys/" fmt, ## args);            \
195                 if (_rc >= 0) {                                         \
196                         ssize_t _linksz;                                \
197                         _rc = _linksz = readlink(_pn, _lb, PATH_MAX);   \
198                         if (_linksz >= 0)                               \
199                                 _lb[_linksz] = '\0';                    \
200                         else                                            \
201                                 efi_error("readlink of %s failed", _pn);\
202                         *(linkbuf) = _lb;                               \
203                 } else {                                                \
204                         efi_error("could not allocate memory");         \
205                 }                                                       \
206                 _rc;                                                    \
207         })
208
209 #define sysfs_stat(statbuf, fmt, args...)                               \
210         ({                                                              \
211                 int rc_;                                                \
212                 char *pn_;                                              \
213                                                                         \
214                 rc_ = asprintfa(&pn_, "/sys/" fmt, ## args);            \
215                 if (rc_ >= 0) {                                         \
216                         rc_ = stat(pn_, statbuf);                       \
217                         if (rc_ < 0)                                    \
218                                 efi_error("could not stat %s", pn_);    \
219                 } else {                                                \
220                         efi_error("could not allocate memory");         \
221                 }                                                       \
222                 rc_;                                                    \
223         })
224
225 #define sysfs_opendir(fmt, args...)                                     \
226         ({                                                              \
227                 int rc_;                                                \
228                 char *pn_;                                              \
229                 DIR *dir_ = NULL;                                       \
230                                                                         \
231                 rc_ = asprintfa(&pn_, "/sys/" fmt, ## args);            \
232                 if (rc_ >= 0) {                                         \
233                         dir_ = opendir(pn_);                            \
234                         if (dir_ == NULL)                               \
235                                 efi_error("could not open %s", pn_);    \
236                 } else {                                                \
237                         efi_error("could not allocate memory");         \
238                 }                                                       \
239                 dir_;                                                   \
240         })
241
242 #define DEV_PROVIDES_ROOT       1
243 #define DEV_PROVIDES_HD         2
244
245 struct dev_probe {
246         char *name;
247         enum interface_type *iftypes;
248         uint32_t flags;
249         ssize_t (*parse)(struct device *dev, const char * const current);
250         ssize_t (*create)(struct device *dev,
251                           uint8_t *buf, ssize_t size, ssize_t off);
252         char *(*make_part_name)(struct device *dev);
253 };
254
255 extern ssize_t parse_scsi_link(const char *current, uint32_t *host,
256                                       uint32_t *bus, uint32_t *device,
257                                       uint32_t *target, uint64_t *lun);
258
259 #define set_part(x, y) /* XXX remove later */
260
261 /* device support implementations */
262 extern struct dev_probe i2o_parser;
263 extern struct dev_probe scsi_parser;
264 extern struct dev_probe ata_parser;
265
266
267 #endif /* _EFIBOOT_LINUX_H */