Skip to content

Commit

Permalink
Merge pull request #429 from zhangxp1998/gbl
Browse files Browse the repository at this point in the history
Add block IO protocol to UEFI loader
  • Loading branch information
travisg authored Jan 10, 2025
2 parents 0fd355e + 5c066ae commit f9a5a16
Show file tree
Hide file tree
Showing 15 changed files with 614 additions and 44 deletions.
11 changes: 11 additions & 0 deletions lib/bio/bio.c
Original file line number Diff line number Diff line change
Expand Up @@ -534,6 +534,17 @@ void bio_unregister_device(bdev_t *dev) {
bdev_dec_ref(dev); // remove the ref the list used to have
}

void bio_iter_devices(bool (*callback)(void *, bdev_t *), void *cookie) {
bdev_t *entry = NULL;
mutex_acquire(&bdevs.lock);
list_for_every_entry(&bdevs.list, entry, bdev_t, node) {
if (!callback(cookie, entry)) {
break;
}
}
mutex_release(&bdevs.lock);
}

void bio_dump_devices(void) {
printf("block devices:\n");
bdev_t *entry;
Expand Down
21 changes: 20 additions & 1 deletion lib/bio/include/lib/bio.h
Original file line number Diff line number Diff line change
Expand Up @@ -130,4 +130,23 @@ enum bio_ioctl_num {
BIO_IOCTL_IS_MAPPED, /* if supported, returns whether or not the device is memory mapped. */
};

__END_CDECLS
// The callback will be called once for every block device, with the cookie and pointer
// to the bdev structure. Note callback would be called with internal mutex held, which
// prevents other process/threads from using APIs such as bio_open, so kindly ask callers
// not to do any long blocking operations in callback functions. If the callback function
// returns |false|, iteration stop immediately, and bio_iter_devices returns.
void bio_iter_devices(bool (*callback)(void *, bdev_t *), void *cookie);

__END_CDECLS

#ifdef __cplusplus
template <typename Callable>
bool iter_device_callback(void *cookie, bdev_t *dev) {
auto func = reinterpret_cast<Callable *>(cookie);
return (*func)(dev);
}

template <typename Callable> void bio_iter_devices(Callable &&func) {
bio_iter_devices(&iter_device_callback<Callable>, &func);
}
#endif
Loading

0 comments on commit f9a5a16

Please sign in to comment.