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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
|
/*
* Copyright (C) 2016 Google, Inc
* Written by Simon Glass <sjg@chromium.org>
*
* SPDX-License-Identifier: GPL-2.0+
*/
#include <common.h>
#include <linux/err.h>
struct blk_driver *blk_driver_lookup_type(int if_type)
{
struct blk_driver *drv = ll_entry_start(struct blk_driver, blk_driver);
const int n_ents = ll_entry_count(struct blk_driver, blk_driver);
struct blk_driver *entry;
for (entry = drv; entry != drv + n_ents; entry++) {
if (if_type == entry->if_type)
return entry;
}
/* Not found */
return NULL;
}
static struct blk_driver *blk_driver_lookup_typename(const char *if_typename)
{
struct blk_driver *drv = ll_entry_start(struct blk_driver, blk_driver);
const int n_ents = ll_entry_count(struct blk_driver, blk_driver);
struct blk_driver *entry;
for (entry = drv; entry != drv + n_ents; entry++) {
if (!strcmp(if_typename, entry->if_typename))
return entry;
}
/* Not found */
return NULL;
}
/**
* get_desc() - Get the block device descriptor for the given device number
*
* @drv: Legacy block driver
* @devnum: Device number (0 = first)
* @descp: Returns block device descriptor on success
* @return 0 on success, -ENODEV if there is no such device, -ENOSYS if the
* driver does not provide a way to find a device, or other -ve on other
* error.
*/
static int get_desc(struct blk_driver *drv, int devnum, struct blk_desc **descp)
{
if (drv->desc) {
if (devnum < 0 || devnum >= drv->max_devs)
return -ENODEV;
*descp = &drv->desc[devnum];
return 0;
}
if (!drv->get_dev)
return -ENOSYS;
return drv->get_dev(devnum, descp);
}
#ifdef HAVE_BLOCK_DEVICE
int blk_list_part(enum if_type if_type)
{
struct blk_driver *drv;
struct blk_desc *desc;
int devnum, ok;
bool first = true;
drv = blk_driver_lookup_type(if_type);
if (!drv)
return -ENOSYS;
for (ok = 0, devnum = 0; devnum < drv->max_devs; ++devnum) {
if (get_desc(drv, devnum, &desc))
continue;
if (desc->part_type != PART_TYPE_UNKNOWN) {
++ok;
if (!first)
putc('\n');
part_print(desc);
first = false;
}
}
if (!ok)
return -ENODEV;
return 0;
}
int blk_print_part_devnum(enum if_type if_type, int devnum)
{
struct blk_driver *drv = blk_driver_lookup_type(if_type);
struct blk_desc *desc;
int ret;
if (!drv)
return -ENOSYS;
ret = get_desc(drv, devnum, &desc);
if (ret)
return ret;
if (desc->type == DEV_TYPE_UNKNOWN)
return -ENOENT;
part_print(desc);
return 0;
}
void blk_list_devices(enum if_type if_type)
{
struct blk_driver *drv = blk_driver_lookup_type(if_type);
struct blk_desc *desc;
int i;
if (!drv)
return;
for (i = 0; i < drv->max_devs; ++i) {
if (get_desc(drv, i, &desc))
continue;
if (desc->type == DEV_TYPE_UNKNOWN)
continue; /* list only known devices */
printf("Device %d: ", i);
dev_print(desc);
}
}
int blk_print_device_num(enum if_type if_type, int devnum)
{
struct blk_driver *drv = blk_driver_lookup_type(if_type);
struct blk_desc *desc;
int ret;
if (!drv)
return -ENOSYS;
ret = get_desc(drv, devnum, &desc);
if (ret)
return ret;
printf("\n%s device %d: ", drv->if_typename, devnum);
dev_print(desc);
return 0;
}
int blk_show_device(enum if_type if_type, int devnum)
{
struct blk_driver *drv = blk_driver_lookup_type(if_type);
struct blk_desc *desc;
int ret;
if (!drv)
return -ENOSYS;
printf("\nDevice %d: ", devnum);
if (devnum >= drv->max_devs) {
puts("unknown device\n");
return -ENODEV;
}
ret = get_desc(drv, devnum, &desc);
if (ret)
return ret;
dev_print(desc);
if (desc->type == DEV_TYPE_UNKNOWN)
return -ENOENT;
return 0;
}
#endif /* HAVE_BLOCK_DEVICE */
struct blk_desc *blk_get_devnum_by_type(enum if_type if_type, int devnum)
{
struct blk_driver *drv = blk_driver_lookup_type(if_type);
struct blk_desc *desc;
if (!drv)
return NULL;
if (get_desc(drv, devnum, &desc))
return NULL;
return desc;
}
int blk_dselect_hwpart(struct blk_desc *desc, int hwpart)
{
struct blk_driver *drv = blk_driver_lookup_type(desc->if_type);
if (!drv)
return -ENOSYS;
if (drv->select_hwpart)
return drv->select_hwpart(desc, hwpart);
return 0;
}
struct blk_desc *blk_get_devnum_by_typename(const char *if_typename, int devnum)
{
struct blk_driver *drv = blk_driver_lookup_typename(if_typename);
struct blk_desc *desc;
if (!drv)
return NULL;
if (get_desc(drv, devnum, &desc))
return NULL;
return desc;
}
ulong blk_read_devnum(enum if_type if_type, int devnum, lbaint_t start,
lbaint_t blkcnt, void *buffer)
{
struct blk_driver *drv = blk_driver_lookup_type(if_type);
struct blk_desc *desc;
ulong n;
int ret;
if (!drv)
return -ENOSYS;
ret = get_desc(drv, devnum, &desc);
if (ret)
return ret;
n = desc->block_read(desc, start, blkcnt, buffer);
if (IS_ERR_VALUE(n))
return n;
/* flush cache after read */
flush_cache((ulong)buffer, blkcnt * desc->blksz);
return n;
}
ulong blk_write_devnum(enum if_type if_type, int devnum, lbaint_t start,
lbaint_t blkcnt, const void *buffer)
{
struct blk_driver *drv = blk_driver_lookup_type(if_type);
struct blk_desc *desc;
int ret;
if (!drv)
return -ENOSYS;
ret = get_desc(drv, devnum, &desc);
if (ret)
return ret;
return desc->block_write(desc, start, blkcnt, buffer);
}
int blk_select_hwpart_devnum(enum if_type if_type, int devnum, int hwpart)
{
struct blk_driver *drv = blk_driver_lookup_type(if_type);
struct blk_desc *desc;
int ret;
if (!drv)
return -ENOSYS;
ret = get_desc(drv, devnum, &desc);
if (ret)
return ret;
return drv->select_hwpart(desc, hwpart);
}
|