diff options
author | Suren A. Chilingaryan <csa@suren.me> | 2015-08-05 15:30:41 +0200 |
---|---|---|
committer | Suren A. Chilingaryan <csa@suren.me> | 2015-08-05 15:30:41 +0200 |
commit | f2e47ba725cc7ba477d3a788addc974f2b18d36f (patch) | |
tree | 060ec738882e450205dce5d7f7c69b89fc024c65 /pcilib | |
parent | 90f6e9b0856fc1c9fdf1195c0c5c43d3b58426c1 (diff) | |
download | pcitool-f2e47ba725cc7ba477d3a788addc974f2b18d36f.tar.gz pcitool-f2e47ba725cc7ba477d3a788addc974f2b18d36f.tar.bz2 pcitool-f2e47ba725cc7ba477d3a788addc974f2b18d36f.tar.xz pcitool-f2e47ba725cc7ba477d3a788addc974f2b18d36f.zip |
Protect mmaping BARs as well
Diffstat (limited to 'pcilib')
-rw-r--r-- | pcilib/bar.c | 17 |
1 files changed, 13 insertions, 4 deletions
diff --git a/pcilib/bar.c b/pcilib/bar.c index ce04f6d..17828a8 100644 --- a/pcilib/bar.c +++ b/pcilib/bar.c @@ -73,21 +73,29 @@ int pcilib_detect_address(pcilib_t *ctx, pcilib_bar_t *bar, uintptr_t *addr, siz void *pcilib_map_bar(pcilib_t *ctx, pcilib_bar_t bar) { void *res; - int ret; + int err, ret; const pcilib_board_info_t *board_info = pcilib_get_board_info(ctx); if (!board_info) return NULL; - + if (ctx->bar_space[bar]) return ctx->bar_space[bar]; - + + err = pcilib_lock(ctx->locks.mmap); + if (err) { + pcilib_error("Error (%i) acquiring mmap lock", err); + return NULL; + } + ret = ioctl( ctx->handle, PCIDRIVER_IOC_MMAP_MODE, PCIDRIVER_MMAP_PCI ); if (ret) { + pcilib_unlock(ctx->locks.mmap); pcilib_error("PCIDRIVER_IOC_MMAP_MODE ioctl have failed", bar); return NULL; } ret = ioctl( ctx->handle, PCIDRIVER_IOC_MMAP_AREA, PCIDRIVER_BAR0 + bar ); if (ret) { + pcilib_unlock(ctx->locks.mmap); pcilib_error("PCIDRIVER_IOC_MMAP_AREA ioctl have failed for bank %i", bar); return NULL; } @@ -98,12 +106,13 @@ void *pcilib_map_bar(pcilib_t *ctx, pcilib_bar_t bar) { #else res = mmap( 0, board_info->bar_length[bar], PROT_WRITE | PROT_READ, MAP_SHARED, ctx->handle, 0 ); #endif + pcilib_unlock(ctx->locks.mmap); + if ((!res)||(res == MAP_FAILED)) { pcilib_error("Failed to mmap data bank %i", bar); return NULL; } - return res; } |