/* $Id: kLdrRdr.c 2861 2006-11-10 03:04:42Z bird $ */ /** @file * * kLdr - The Dynamic Loader, file abstraction. * * Copyright (c) 2006 knut st. osmundsen * * * This file is part of kLdr. * * kLdr is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * kLdr is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with kLdr; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * */ /******************************************************************************* * Header Files * *******************************************************************************/ #include #include "kLdrInternal.h" /******************************************************************************* * Defined Constants And Macros * *******************************************************************************/ /** @def KLDRRDR_STRICT * Define KLDRRDR_STRICT to enabled strict checks in KLDRMOD. */ #define KLDRRDR_STRICT 1 /** @def KLDRRDR_ASSERT * Assert that an expression is true when KLDR_STRICT is defined. */ #ifdef KLDRRDR_STRICT # define KLDRRDR_ASSERT(expr) kldrHlpAssert(expr) #else # define KLDRRDR_ASSERT(expr) do {} while (0) #endif /** Return / crash validation of a reader argument. */ #define KLDRRDR_VALIDATE_EX(pRdr, rc) \ do { \ if ( (pRdr)->u32Magic != KLDRRDR_MAGIC \ || (pRdr)->pOps == NULL \ )\ { \ return (rc); \ } \ } while (0) /** Return / crash validation of a reader argument. */ #define KLDRRDR_VALIDATE(pRdr) \ KLDRRDR_VALIDATE_EX(pRdr, KLDR_ERR_INVALID_PARAMETER) /** Return / crash validation of a reader argument. */ #define KLDRRDR_VALIDATE_VOID(pRdr) \ do { \ if ( (pRdr)->u32Magic != KLDRRDR_MAGIC \ || (pRdr)->pOps == NULL \ )\ { \ return; \ } \ } while (0) /******************************************************************************* * Global Variables * *******************************************************************************/ /** The list of file providers. */ static PCKLDRRDROPS g_pRdrHead = &g_kLdrRdrFileOps; /** * Adds a new file provider. * * @param pAdd The new file provider. */ void kLdrRdrAddProvider(PKLDRRDROPS pAdd) { pAdd->pNext = g_pRdrHead; g_pRdrHead = pAdd; } /** * Tries to opens a file. * * @returns 0 on success, OS status code on failure. * @param ppRdr Where to store the file provider instance. * @param pszFilename The filename. */ int kLdrRdrOpen(PPKLDRRDR ppRdr, const char *pszFilename) { int rc; PCKLDRRDROPS pCur; for (pCur = g_pRdrHead; pCur; pCur = pCur->pNext) { rc = pCur->pfnCreate(ppRdr, pszFilename); if (!rc) return 0; } return rc; } /** * Closes the file. * * @returns 0 on success, OS specific error code on failure. * On failure, the file provider instance will be in an indeterminate state - don't touch it! * @param pRdr The file provider instance. */ int kLdrRdrClose(PKLDRRDR pRdr) { KLDRRDR_VALIDATE(pRdr); return pRdr->pOps->pfnDestroy(pRdr); } /** Read bits from the file. * * @returns 0 on success, OS specific error code on failure. * @param pRdr The file provider instance. * @param pvBuf Where to put the bits. * @param cb The number of bytes to read. * @param off Where to start reading. */ int kLdrRdrRead(PKLDRRDR pRdr, void *pvBuf, size_t cb, off_t off) { KLDRRDR_VALIDATE(pRdr); return pRdr->pOps->pfnRead(pRdr, pvBuf, cb, off); } /** Map all the file bits into memory (read only). * * @returns 0 on success, OS specific error code on failure. * @param pRdr The file provider instance. * @param ppvBits Where to store the address of the mapping. * The size can be obtained using pfnSize. */ int kLdrRdrAllMap(PKLDRRDR pRdr, const void **ppvBits) { KLDRRDR_VALIDATE(pRdr); return pRdr->pOps->pfnAllMap(pRdr, ppvBits); } /** Unmap a file bits mapping obtained by KLDRRDROPS::pfnAllMap. * * @returns 0 on success, OS specific error code on failure. * @param pRdr The file provider instance. * @param pvBits The mapping address. */ int kLdrRdrAllUnmap(PKLDRRDR pRdr, const void *pvBits) { KLDRRDR_VALIDATE(pRdr); return pRdr->pOps->pfnAllUnmap(pRdr, pvBits); } /** Get the file size. * * @returns The file size. Returns -1 on failure. * @param pRdr The file provider instance. */ off_t kLdrRdrSize(PKLDRRDR pRdr) { KLDRRDR_VALIDATE(pRdr); return pRdr->pOps->pfnSize(pRdr); } /** Get the file pointer offset. * * @returns The file pointer offset. Returns -1 on failure. * @param pRdr The file provider instance. */ off_t kLdrRdrTell(PKLDRRDR pRdr) { KLDRRDR_VALIDATE(pRdr); return pRdr->pOps->pfnTell(pRdr); } /** Get the file name. * * @returns The file name. Returns NULL on failure. * @param pRdr The file provider instance. */ const char *kLdrRdrName(PKLDRRDR pRdr) { KLDRRDR_VALIDATE_EX(pRdr, NULL); return pRdr->pOps->pfnName(pRdr); } /** * Gets the page size used when mapping sections of the file. * * @returns The page size. * @param pRdr The file provider instance. */ size_t kLdrRdrPageSize(PKLDRRDR pRdr) { KLDRRDR_VALIDATE_EX(pRdr, 0x10000); return pRdr->pOps->pfnPageSize(pRdr); } /** * Maps the segments of a image into memory. * * The file reader will be using the RVA member of each segment to figure out where * it goes relative to the image base address. * * @returns 0 on success, OS specific error code on failure. * @param pRdr The file provider instance. * @param ppvBase On input when fFixed is set, this contains the base address of the mapping. * On output this contains the base of the image mapping. * @param cSegments The number of segments in the array pointed to by paSegments. * @param paSegments The segments thats going to be mapped. * @param fFixed If set, the address at *ppvBase should be the base address of the mapping. */ int kLdrRdrMap(PKLDRRDR pRdr, void **ppvBase, uint32_t cSegments, PCKLDRSEG paSegments, unsigned fFixed) { KLDRRDR_VALIDATE(pRdr); return pRdr->pOps->pfnMap(pRdr, ppvBase, cSegments, paSegments, fFixed); } /** * Reloads dirty pages in mapped image. * * @returns 0 on success, OS specific error code on failure. * @param pRdr The file provider instance. * @param pvBase The base address of the image mapping. * @param cSegments The number of segments in the array pointed to by paSegments. * @param paSegments The segments thats going to be mapped. */ int kLdrRdrRefresh(PKLDRRDR pRdr, void *pvBase, uint32_t cSegments, PCKLDRSEG paSegments) { KLDRRDR_VALIDATE(pRdr); return pRdr->pOps->pfnRefresh(pRdr, pvBase, cSegments, paSegments); } /** * Protects or unprotects an image mapping. * * This is typically used for getting write access to read or execute only * pages while applying fixups. * * @returns 0 on success, OS specific error code on failure. * @param pRdr The file provider instance. * @param pvBase The base address of the image mapping. * @param cSegments The number of segments in the array pointed to by paSegments. * @param paSegments The segments thats going to be mapped. * @param fUnprotectOrProtect When set the all mapped segments are made writable. * When clean the segment protection is restored. */ int kLdrRdrProtect(PKLDRRDR pRdr, void *pvBase, uint32_t cSegments, PCKLDRSEG paSegments, unsigned fUnprotectOrProtect) { KLDRRDR_VALIDATE(pRdr); return pRdr->pOps->pfnProtect(pRdr, pvBase, cSegments, paSegments, fUnprotectOrProtect); } /** * Unmaps a image mapping. * * @returns 0 on success, OS specific error code on failure. * @param pRdr The file provider instance. * @param pvBase The base address of the image mapping. * @param cSegments The number of segments in the array pointed to by paSegments. * @param paSegments The segments thats going to be mapped. */ int kLdrRdrUnmap(PKLDRRDR pRdr, void *pvBase, uint32_t cSegments, PCKLDRSEG paSegments) { KLDRRDR_VALIDATE(pRdr); return pRdr->pOps->pfnUnmap(pRdr, pvBase, cSegments, paSegments); } /** * We're done reading from the file but would like to keep file mappings. * * If the OS support closing the file handle while the file is mapped, * the reader should do so. * * @param pRdr The file provider instance. */ void kLdrRdrDone(PKLDRRDR pRdr) { KLDRRDR_VALIDATE_VOID(pRdr); pRdr->pOps->pfnDone(pRdr); }