|
|
e2e2c2 |
/***************************************************************/
|
|
|
e2e2c2 |
/* perk: PE Resource Kit */
|
|
|
e2e2c2 |
/* Copyright (C) 2015--2016 Z. Gilboa */
|
|
|
e2e2c2 |
/* Released under GPLv2 and GPLv3; see COPYING.PERK. */
|
|
|
e2e2c2 |
/***************************************************************/
|
|
|
e2e2c2 |
|
|
|
81e8b3 |
#include <stdint.h>
|
|
|
d7ed3e |
#include <stddef.h>
|
|
|
81e8b3 |
#include <stdlib.h>
|
|
|
81e8b3 |
#include <string.h>
|
|
|
81e8b3 |
#include <sys/mman.h>
|
|
|
d7ed3e |
|
|
|
81e8b3 |
#include <perk/perk.h>
|
|
|
eb4c04 |
#include "perk_driver_impl.h"
|
|
|
81e8b3 |
|
|
|
d7ed3e |
static int pe_free_unit_ctx_impl(struct pe_unit_ctx_impl * ctx, int status)
|
|
|
81e8b3 |
{
|
|
|
81e8b3 |
if (ctx) {
|
|
|
81e8b3 |
pe_free_image_meta(ctx->meta);
|
|
|
81e8b3 |
pe_unmap_raw_image(&ctx->map);
|
|
|
81e8b3 |
free(ctx);
|
|
|
81e8b3 |
}
|
|
|
81e8b3 |
|
|
|
81e8b3 |
return status;
|
|
|
81e8b3 |
}
|
|
|
81e8b3 |
|
|
|
81e8b3 |
int pe_get_unit_ctx(
|
|
|
a9788e |
const struct pe_driver_ctx * dctx,
|
|
|
a9788e |
const char * path,
|
|
|
a9788e |
struct pe_unit_ctx ** pctx)
|
|
|
81e8b3 |
{
|
|
|
d7ed3e |
struct pe_unit_ctx_impl * ctx;
|
|
|
720609 |
int prot;
|
|
|
81e8b3 |
|
|
|
ffcd00 |
if (!dctx || !(ctx = calloc(1,sizeof(*ctx))))
|
|
|
81e8b3 |
return -1;
|
|
|
81e8b3 |
|
|
|
c61328 |
prot = (dctx->cctx->actflags & PERK_ACTION_MAP_READWRITE)
|
|
|
81e8b3 |
? PROT_READ | PROT_WRITE
|
|
|
81e8b3 |
: PROT_READ;
|
|
|
81e8b3 |
|
|
|
76aabf |
if (pe_map_raw_image(dctx->cctx->ioctx->fdin,path,prot,&ctx->map))
|
|
|
81e8b3 |
return pe_free_unit_ctx_impl(ctx,-1);
|
|
|
81e8b3 |
|
|
|
81e8b3 |
if (pe_get_image_meta(&ctx->map,&ctx->meta))
|
|
|
81e8b3 |
return pe_free_unit_ctx_impl(ctx,-1);
|
|
|
81e8b3 |
|
|
|
c61328 |
memcpy(&ctx->cctx,dctx->cctx,
|
|
|
81e8b3 |
sizeof(ctx->cctx));
|
|
|
81e8b3 |
|
|
|
72cb99 |
ctx->path = path;
|
|
|
76aabf |
ctx->ioctx.prot = prot;
|
|
|
81e8b3 |
|
|
|
d7ed3e |
ctx->uctx.path = &ctx->path;
|
|
|
d7ed3e |
ctx->uctx.map = &ctx->map;
|
|
|
d7ed3e |
ctx->uctx.meta = ctx->meta;
|
|
|
d7ed3e |
ctx->uctx.cctx = &ctx->cctx;
|
|
|
d7ed3e |
|
|
|
d7ed3e |
*pctx = &ctx->uctx;
|
|
|
81e8b3 |
return 0;
|
|
|
81e8b3 |
}
|
|
|
81e8b3 |
|
|
|
81e8b3 |
void pe_free_unit_ctx(struct pe_unit_ctx * ctx)
|
|
|
81e8b3 |
{
|
|
|
d7ed3e |
struct pe_unit_ctx_impl * ictx;
|
|
|
d7ed3e |
uintptr_t addr;
|
|
|
d7ed3e |
|
|
|
d7ed3e |
if (ctx) {
|
|
|
d7ed3e |
addr = (uintptr_t)ctx - offsetof(struct pe_unit_ctx_impl,uctx);
|
|
|
d7ed3e |
ictx = (struct pe_unit_ctx_impl *)addr;
|
|
|
d7ed3e |
pe_free_unit_ctx_impl(ictx,0);
|
|
|
d7ed3e |
}
|
|
|
81e8b3 |
}
|