/* $Id: kLdrRdr.c 2856 2006-11-04 22:19:33Z 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); } /** * Prepares a memory region to map file sections into. * * @returns 0 on success, OS specific error code on failure. * @param pRdr The file provider instance. * @param ppv If fFixed is set, *ppv contains the memory location which * the region should be based at. If fFixed is clear the OS * is free to choose the location. * On successful return *ppv contains address of the prepared * memory region. * @param cb The size of the memory region to prepare. * @param fFixed When set *ppv will contain the desired region address. * */ int kLdrRdrPrepare(PKLDRRDR pRdr, void **ppv, size_t cb, unsigned fFixed) { KLDRRDR_VALIDATE(pRdr); return pRdr->pOps->pfnPrepare(pRdr, ppv, cb, fFixed); } /** * Maps a section of the file into the memory region reserved by pfnPrepare. * * @returns 0 on success, OS specific error code on failure. * @param pRdr The file provider instance. * @param pv The address in the prepared region. * @param cb The size of the memory mapping. * @param enmProt The desired memory protection. * @param offFile The start of the raw file bytes. * @param cbFile The number of raw file bytes. This must be less or equal to cb. */ int kLdrRdrMap(PKLDRRDR pRdr, void *pv, size_t cb, KLDRPROT enmProt, off_t offFile, size_t cbFile) { KLDRRDR_VALIDATE(pRdr); return pRdr->pOps->pfnMap(pRdr, pv, cb, enmProt, offFile, cbFile); } /** * Reloads dirty pages in mapped section. * * @returns 0 on success, OS specific error code on failure. * @param pRdr The file provider instance. * @param pv The address in the prepared region. * @param cb The size of the memory mapping. * @param enmProt The desired memory protection. * @param offFile The start of the raw file bytes. * @param cbFile The number of raw file bytes. This must be less or equal to cb. */ int kLdrRdrRefreshMap(PKLDRRDR pRdr, void *pv, size_t cb, KLDRPROT enmProt, off_t offFile, size_t cbFile) { KLDRRDR_VALIDATE(pRdr); return pRdr->pOps->pfnRefreshMap(pRdr, pv, cb, enmProt, offFile, cbFile); } /** * Changes the page protection of a section mapped using pfnMap. * * This is typically used for applying fixups and similar. * * @returns 0 on success, OS specific error code on failure. * @param pRdr The file provider instance. * @param pv The address passed to pfnMap. * @param cb The size passed to pfnMap. * @param enmProt The desired memory protection. */ int kLdrRdrProtect(PKLDRRDR pRdr, void *pv, size_t cb, KLDRPROT enmProt) { KLDRRDR_VALIDATE(pRdr); return pRdr->pOps->pfnProtect(pRdr, pv, cb, enmProt); } /** * Unmaps a section of the file previously mapped using pfnMap. * * @returns 0 on success, OS specific error code on failure. * @param pRdr The file provider instance. * @param pv The address passed to pfnMap. * @param cb The size passed to pfnMap. */ int kLdrRdrUnmap(PKLDRRDR pRdr, void *pv, size_t cb) { KLDRRDR_VALIDATE(pRdr); return pRdr->pOps->pfnUnmap(pRdr, pv, cb); } /** * Releases the memory region prepared by pfnPrepare(). * * Before calling this function, all sections mapped by pfnMap must first be unmapped by calling pfnUnmap. * * @returns 0 on success, OS specific error code on failure. * @param pRdr The file provider instance. * @param pv The address of the prepared region. * @param cb The size of the prepared region. */ int kLdrRdrUnprepare(PKLDRRDR pRdr, void *pv, size_t cb) { KLDRRDR_VALIDATE(pRdr); return pRdr->pOps->pfnUnprepare(pRdr, pv, cb); } /** * 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); }