]>
git.ipfire.org Git - people/ms/u-boot.git/blob - drivers/block/blk_legacy.c
2 * Copyright (C) 2016 Google, Inc
3 * Written by Simon Glass <sjg@chromium.org>
5 * SPDX-License-Identifier: GPL-2.0+
11 struct blk_driver
*blk_driver_lookup_type(int if_type
)
13 struct blk_driver
*drv
= ll_entry_start(struct blk_driver
, blk_driver
);
14 const int n_ents
= ll_entry_count(struct blk_driver
, blk_driver
);
15 struct blk_driver
*entry
;
17 for (entry
= drv
; entry
!= drv
+ n_ents
; entry
++) {
18 if (if_type
== entry
->if_type
)
26 static struct blk_driver
*blk_driver_lookup_typename(const char *if_typename
)
28 struct blk_driver
*drv
= ll_entry_start(struct blk_driver
, blk_driver
);
29 const int n_ents
= ll_entry_count(struct blk_driver
, blk_driver
);
30 struct blk_driver
*entry
;
32 for (entry
= drv
; entry
!= drv
+ n_ents
; entry
++) {
33 if (!strcmp(if_typename
, entry
->if_typename
))
42 * get_desc() - Get the block device descriptor for the given device number
44 * @drv: Legacy block driver
45 * @devnum: Device number (0 = first)
46 * @descp: Returns block device descriptor on success
47 * @return 0 on success, -ENODEV if there is no such device, -ENOSYS if the
48 * driver does not provide a way to find a device, or other -ve on other
51 static int get_desc(struct blk_driver
*drv
, int devnum
, struct blk_desc
**descp
)
54 if (devnum
< 0 || devnum
>= drv
->max_devs
)
56 *descp
= &drv
->desc
[devnum
];
62 return drv
->get_dev(devnum
, descp
);
65 #ifdef HAVE_BLOCK_DEVICE
66 int blk_list_part(enum if_type if_type
)
68 struct blk_driver
*drv
;
69 struct blk_desc
*desc
;
73 drv
= blk_driver_lookup_type(if_type
);
76 for (ok
= 0, devnum
= 0; devnum
< drv
->max_devs
; ++devnum
) {
77 if (get_desc(drv
, devnum
, &desc
))
79 if (desc
->part_type
!= PART_TYPE_UNKNOWN
) {
93 int blk_print_part_devnum(enum if_type if_type
, int devnum
)
95 struct blk_driver
*drv
= blk_driver_lookup_type(if_type
);
96 struct blk_desc
*desc
;
101 ret
= get_desc(drv
, devnum
, &desc
);
104 if (desc
->type
== DEV_TYPE_UNKNOWN
)
111 void blk_list_devices(enum if_type if_type
)
113 struct blk_driver
*drv
= blk_driver_lookup_type(if_type
);
114 struct blk_desc
*desc
;
119 for (i
= 0; i
< drv
->max_devs
; ++i
) {
120 if (get_desc(drv
, i
, &desc
))
122 if (desc
->type
== DEV_TYPE_UNKNOWN
)
123 continue; /* list only known devices */
124 printf("Device %d: ", i
);
129 int blk_print_device_num(enum if_type if_type
, int devnum
)
131 struct blk_driver
*drv
= blk_driver_lookup_type(if_type
);
132 struct blk_desc
*desc
;
137 ret
= get_desc(drv
, devnum
, &desc
);
140 printf("\n%s device %d: ", drv
->if_typename
, devnum
);
146 int blk_show_device(enum if_type if_type
, int devnum
)
148 struct blk_driver
*drv
= blk_driver_lookup_type(if_type
);
149 struct blk_desc
*desc
;
154 printf("\nDevice %d: ", devnum
);
155 if (devnum
>= drv
->max_devs
) {
156 puts("unknown device\n");
159 ret
= get_desc(drv
, devnum
, &desc
);
164 if (desc
->type
== DEV_TYPE_UNKNOWN
)
169 #endif /* HAVE_BLOCK_DEVICE */
171 struct blk_desc
*blk_get_devnum_by_type(enum if_type if_type
, int devnum
)
173 struct blk_driver
*drv
= blk_driver_lookup_type(if_type
);
174 struct blk_desc
*desc
;
179 if (get_desc(drv
, devnum
, &desc
))
185 int blk_dselect_hwpart(struct blk_desc
*desc
, int hwpart
)
187 struct blk_driver
*drv
= blk_driver_lookup_type(desc
->if_type
);
191 if (drv
->select_hwpart
)
192 return drv
->select_hwpart(desc
, hwpart
);
197 struct blk_desc
*blk_get_devnum_by_typename(const char *if_typename
, int devnum
)
199 struct blk_driver
*drv
= blk_driver_lookup_typename(if_typename
);
200 struct blk_desc
*desc
;
205 if (get_desc(drv
, devnum
, &desc
))
211 ulong
blk_read_devnum(enum if_type if_type
, int devnum
, lbaint_t start
,
212 lbaint_t blkcnt
, void *buffer
)
214 struct blk_driver
*drv
= blk_driver_lookup_type(if_type
);
215 struct blk_desc
*desc
;
221 ret
= get_desc(drv
, devnum
, &desc
);
224 n
= desc
->block_read(desc
, start
, blkcnt
, buffer
);
228 /* flush cache after read */
229 flush_cache((ulong
)buffer
, blkcnt
* desc
->blksz
);
234 ulong
blk_write_devnum(enum if_type if_type
, int devnum
, lbaint_t start
,
235 lbaint_t blkcnt
, const void *buffer
)
237 struct blk_driver
*drv
= blk_driver_lookup_type(if_type
);
238 struct blk_desc
*desc
;
243 ret
= get_desc(drv
, devnum
, &desc
);
246 return desc
->block_write(desc
, start
, blkcnt
, buffer
);
249 int blk_select_hwpart_devnum(enum if_type if_type
, int devnum
, int hwpart
)
251 struct blk_driver
*drv
= blk_driver_lookup_type(if_type
);
252 struct blk_desc
*desc
;
257 ret
= get_desc(drv
, devnum
, &desc
);
260 return drv
->select_hwpart(desc
, hwpart
);