/*************************************************************************** * Copyright (c) 2003-2010, Broadcom Corporation * All Rights Reserved * Confidential Property of Broadcom Corporation * * THIS SOFTWARE MAY ONLY BE USED SUBJECT TO AN EXECUTED SOFTWARE LICENSE * AGREEMENT BETWEEN THE USER AND BROADCOM. YOU HAVE NO RIGHT TO USE OR * EXPLOIT THIS MATERIAL EXCEPT SUBJECT TO THE TERMS OF SUCH AN AGREEMENT. * * $brcm_Workfile: $ * $brcm_Revision: $ * $brcm_Date: $ * * Module Description: flash simulation module in memory * * Revision History: * * $brcm_Log: $ * * ***************************************************************************/ #include "bstd.h" #include "bdbg.h" #include "bkni.h" #include "bfds.h" #define BFDS_SECTOR_SIZE 0x1000 #define BFDS_PAGE_SIZE 0x100 #define BFDS_SECTOR_MASK (~(BFDS_SECTOR_SIZE - 1)) struct bfds_state { void * storage; unsigned size; }; static struct bfds_state mems; int bfds_open(bfds_handle * handle) { int res; BKNI_Memset(&mems, 0, sizeof(struct bfds_state)); //mems.size = BFDS_SECTOR_SIZE * 4 * 16; /* 16 sectors of flash */ mems.size = 0x400000 * 2; /* 16 sectors of flash */ mems.storage = BKNI_Malloc(mems.size); if(NULL == mems.storage){ res = BFDS_ERROR; goto ExitFunc; } /* fill memory with 0xff to simulate flash */ BKNI_Memset(mems.storage, 0xff, mems.size); *handle = &mems; res = BFDS_OK; ExitFunc: return res; } int bfds_close( bfds_handle handle /* [in] device handle */ ) { if (mems.storage) { BKNI_Free(mems.storage); mems.storage = NULL; } return BFDS_OK; } int bfds_read(bfds_handle handle, unsigned offset, void * data, unsigned size) { void * pdata; BDBG_ASSERT(NULL != handle); pdata = (void*)((unsigned)(handle->storage) + offset); BKNI_Memcpy(data, pdata, size); return BFDS_OK; } int bfds_sector_erase(bfds_handle handle, unsigned offset) { void * erase_addr; BDBG_ASSERT(NULL != handle); erase_addr = (void *)((unsigned)(handle->storage) + (offset & BFDS_SECTOR_MASK)); BKNI_Memset(erase_addr, 0xff, BFDS_SECTOR_SIZE); return BFDS_OK; } int bfds_page_program(bfds_handle handle, unsigned offset, void * data, unsigned size) { void * page_addr; unsigned remainder; BDBG_ASSERT(NULL != handle); page_addr = (void *)((unsigned)(handle->storage) + offset); remainder = BFDS_PAGE_SIZE - (offset & (BFDS_PAGE_SIZE - 1)); if(size > remainder){ /* If write is not page aligned we need to chop size, in real flash data will wrap to the beggining of the page*/ BKNI_Fail(); } BKNI_Memcpy(page_addr, data, size); return BFDS_OK; } int bfds_get_properties ( bfds_handle handle, /* [in] device handle */ bfds_properties * properties /* [out] details of flash device */ ) { int res; if(NULL == properties){ res = BFDS_ERROR; goto ExitFunc; } properties->flash_sector_size = BFDS_SECTOR_SIZE; properties->flash_page_size = BFDS_PAGE_SIZE; res = BFDS_OK; ExitFunc: return res; }