Exemplo n.º 1
0
int MPFS_Mount ( uint8_t diskNo )
{
    uint32_t index;

    mpfsObject.numFiles = 1;
    mpfsObject.isMPFSLocked = false;

    mpfsObject.diskNumber = diskNo;

    /* Take MPFSStubs[0] for MPFS internal usage */
    MPFSStubs[0].basePointer = SYS_FS_MEDIA_MANAGER_AddressGet(diskNo);
    for(index = 1; index <= SYS_FS_MAX_FILES ; index++)
    {
        MPFSStubs[index].addr = MPFS_INVALID;
        MPFSStubs[index].basePointer = SYS_FS_MEDIA_MANAGER_AddressGet(diskNo);
    }
    MPFSStubs[0].addr = 0;
    MPFSStubs[0].bytesRem = 8;


    // Now, using the file object, read the MPFS file and compare content (string compare)
    MPFSGetArray(0, (uint8_t*)&fatCache, 6);
    if(!memcmp((void*)&fatCache, (const void*)"MPFS\x02\x01", 6))
    {
        MPFSGetArray(0, (uint8_t*)&mpfsObject.numFiles, 2);
    }
    else
    {
        mpfsObject.numFiles = 0;
    }

    mpfsObject.fatCacheID = MPFS_INVALID_FAT;

    return MPFS_OK;
}
Exemplo n.º 2
0
size_t FileReadUInt16(WORD *ptr, FILE_HANDLE stream)
{
	BYTE databuff[2];
	*ptr=0x0000;

	#if defined STACK_USE_MPFS2
        WORD retVal;

		retVal=MPFSGetArray(stream, (BYTE*)ptr, 2);

		if(retVal == 2)//Number of bytes read
		{
			((BYTE*)ptr)[1]= databuff[1];
			((BYTE*)ptr)[0]= databuff[0];
			return 2;//Number of bytes read
		}
		else
			return 0;
		
    #elif defined STACK_USE_MDD 
		size_t retVal; 
      
		retVal= FSfread(databuff, 2, 1, stream);

		if(retVal == 1)//Number of Uints of 4 bytes each Read
		{
			((BYTE*)ptr)[1]= databuff[1];
			((BYTE*)ptr)[0]= databuff[0];
			return 2;//Number of bytes read
		}
		else
			return 0;
        
    #endif
}
Exemplo n.º 3
0
/*********************************************************************
 * Function:        void MPFSGetFilename(MPFS_HANDLE hMPFS, BYTE *name, WORD len)
 *
 * PreCondition:    hMPFS is a valid open file handle
 *
 * Input:           hMPFS: the file to locate the start address of
 *					name: buffer to place the filename in
 *					len: size of buffer
 *
 * Output:          name: the filename of the current file
 * 
 * Side Effects:    None
 *
 * Overview:        Finds the filename of the currently opened file
 *
 * Note:            None
 ********************************************************************/
BOOL MPFSGetFilename(MPFS_HANDLE hMPFS, BYTE *name, WORD len)
{
	DWORD addr;
	
	// Make sure a valid file is open
	if(hMPFS > MAX_MPFS_HANDLES)
		return FALSE;
	if(MPFSStubs[hMPFS].addr == MPFS_INVALID)
		return FALSE;
	
	// Move to the point for reading
	LoadFATRecord(hMPFS);
	MPFSStubs[0].addr += 4;
	MPFSGetArray(0, (BYTE*)&(addr), 4);
	MPFSStubs[0].addr = addr;

	MPFSStubs[0].bytesRem = 255;
	
	// Read the value and return
	MPFSGet(0, name);
	len--;
	for(; len > 0 && *name != '\0'; len--)
	{
		name++;
		MPFSGet(0, name);
	}
	
	return TRUE;
}
Exemplo n.º 4
0
int MPFS_Read ( uintptr_t handle, void* buff, uint32_t btr, uint32_t * br )
{
    MPFS_HANDLE address = ((MPFS_HANDLE )handle);
    *br = MPFSGetArray ( address, buff, btr );


    return MPFS_OK;
}
Exemplo n.º 5
0
/*********************************************************************
 * Function:        MPFS_PTR MPFSGetEndAddr(MPFS_HANDLE hMPFS)
 *
 * PreCondition:    hMPFS is a valid open file handle
 *
 * Input:           hMPFS: the file to locate the start address of
 *
 * Output:          the start address
 * 
 * Side Effects:    None
 *
 * Overview:        Reads an ending file address from the MPFS Fat
 *
 * Note:            None
 ********************************************************************/
MPFS_PTR MPFSGetEndAddr(MPFS_HANDLE hMPFS)
{
	MPFS_PTR start, len;
	
	// Make sure a valid file is open
	if(hMPFS > MAX_MPFS_HANDLES)
		return MPFS_INVALID;
	if(MPFSStubs[hMPFS].addr == MPFS_INVALID)
		return MPFS_INVALID;
	
	// Move to the point for reading
	LoadFATRecord(hMPFS);
	MPFSStubs[0].addr += 8;
	
	// Read the value and return
	MPFSGetArray(0, (BYTE*)&start, 4);
	MPFSGetArray(0, (BYTE*)&len, 4);
	return start + len;
}
Exemplo n.º 6
0
/*****************************************************************************
  Function:
	static void _LoadFATRecord(WORD fatID)

  Description:
	Loads the FAT record for a specified handle.
	
  Precondition:
	None

  Parameters:
	fatID - the ID of the file whose FAT is to be loaded

  Returns:
	None

  Remarks:
	The FAT record will be stored in fatCache.
  ***************************************************************************/
static void _LoadFATRecord(WORD fatID)
{
	if(fatID == fatCacheID || fatID >= numFiles)
		return;
	
	// Read the FAT record to the cache
	MPFSStubs[0].bytesRem = 22;
	MPFSStubs[0].addr = 8 + numFiles*2 + fatID*22;
	MPFSGetArray(0, (BYTE*)&fatCache, 22);
	fatCacheID = fatID;
}
Exemplo n.º 7
0
/*****************************************************************************
  Function:
	void _Validate(void)

  Summary:
	Validates the MPFS Image

  Description:
	Verifies that the MPFS image is valid, and reads the number of 
	available files from the image header.  This function is called on
	boot, and again after any image is written.

  Precondition:
	None

  Parameters:
	None

  Returns:
	None
  ***************************************************************************/
static void _Validate(void)
{
	// If this function causes an Address Error Exception on 16-bit
	// platforms with code stored in internal Flash, make sure your
	// compiler memory model settings are correct.
	//
	// In MPLAB, choose Project Menu > Build Options > Project.
	// Select the MPLAB C30 tab and change Cagetory to Memory Model.
	// Ensure that Large Code Model is selected, and that the remaining
	//   options are set to Default.
	
	// Validate the image and update numFiles
	MPFSStubs[0].addr = 0;
	MPFSStubs[0].bytesRem = 8;
	MPFSGetArray(0, (BYTE*)&fatCache, 6);
	if(!memcmppgm2ram((void*)&fatCache, (ROM void*)"MPFS\x02\x01", 6))
		MPFSGetArray(0, (BYTE*)&numFiles, 2);
	else
		numFiles = 0;
	fatCacheID = MPFS_INVALID_FAT;
}	
Exemplo n.º 8
0
/*********************************************************************
 * Function:        DWORD HTTPIncFile(TCP_SOCKET skt, 
 *						DWORD callbackPos, ROM BYTE* file)
 *
 * PreCondition:    curHTTP is loaded
 *
 * Input:           None
 *
 * Output:          Updates curHTTP.callbackPos
 *
 * Side Effects:    None
 *
 * Overview:        Writes an MPFS file to the socket and returns
 *
 * Note:            Provides rudimentary include support for dynamic
 *					files which allows them to use header, footer, 
 *					and/or menu inclusion files rather than
 *					duplicating code across all files.
 ********************************************************************/
void HTTPIncFile(ROM BYTE* file)
{
	WORD count, len;
	BYTE data[64];
	MPFS_HANDLE fp;
	
	// Check if this is a first round call
	if(curHTTP.callbackPos == 0x00)
	{// On initial call, open the file and save its ID
		fp = MPFSOpenROM(file);
		if(fp == MPFS_INVALID_HANDLE)
		{// File not found, so abort
			return;
		}
		((DWORD_VAL*)&curHTTP.callbackPos)->w[0] = MPFSGetID(fp);
	}
	else
	{// The file was already opened, so load up it's ID and seek
		fp = MPFSOpenID(((DWORD_VAL*)&curHTTP.callbackPos)->w[0]);
		if(fp == MPFS_INVALID_HANDLE)
		{// File not found, so abort
			curHTTP.callbackPos = 0x00;
			return;
		}
		MPFSSeek(fp, ((DWORD_VAL*)&curHTTP.callbackPos)->w[1], MPFS_SEEK_FORWARD);
	}
	
	// Get/put as many bytes as possible
	count = TCPIsPutReady(sktHTTP);
	while(count > 0)
	{
		len = MPFSGetArray(fp, data, mMIN(count, 64));
		if(len == 0)
		{// If no bytes were read, an EOF was reached
			MPFSClose(fp);
			curHTTP.callbackPos = 0x00;
			return;
		}
		else
		{// Write the bytes to the socket
			TCPPutArray(sktHTTP, data, len);
			count -= len;
		}
	}
	
	// Save the new address and close the file
	((DWORD_VAL*)&curHTTP.callbackPos)->w[1] = MPFSTell(fp);
	MPFSClose(fp);
	
	return;
}
Exemplo n.º 9
0
/*********************************************************************
 * Function:        static BOOL HTTPSendFile(void)
 *
 * PreCondition:    curHTTP.file and curHTTP.offsets have both been
 *					opened for reading.
 *
 * Input:           None
 *
 * Output:          TRUE if EOF was reached and reading is done
 *					FALSE if more data remains
 *
 * Side Effects:    None
 *
 * Overview:        This function serves the next chunk of curHTTP's
 *					file, up to a) available TX FIFO or b) up to
 *					the next recorded callback index, whichever comes
 *					first.
 *
 * Note:            None
 ********************************************************************/
static BOOL HTTPSendFile(void)
{
	WORD numBytes, len;
	BYTE c, data[64];
	
	// Determine how many bytes we can read right now
	numBytes = mMIN(TCPIsPutReady(sktHTTP), curHTTP.nextCallback - curHTTP.byteCount);
	
	// Get/put as many bytes as possible
	curHTTP.byteCount += numBytes;
	while(numBytes > 0)
	{
		len = MPFSGetArray(curHTTP.file, data, mMIN(numBytes, 64));
		if(len == 0)
			return TRUE;
		else
			TCPPutArray(sktHTTP, data, len);
		numBytes -= len;
	}
	
	// Check if a callback index was reached
	if(curHTTP.byteCount == curHTTP.nextCallback)
	{
		// Update the state machine
		smHTTP = SM_HTTP_SEND_FROM_CALLBACK;
		curHTTP.callbackPos = 0;

		// Read past the variable name and close the MPFS
		MPFSGet(curHTTP.file, NULL);
		do
		{
			if(!MPFSGet(curHTTP.file, &c))
				break;
			curHTTP.byteCount++;
		} while(c != '~');
		curHTTP.byteCount++;
		
		// Read in the callback address and next offset
		MPFSGetLong(curHTTP.offsets, &(curHTTP.callbackID));
		if(!MPFSGetLong(curHTTP.offsets, &(curHTTP.nextCallback)))
		{
			curHTTP.nextCallback = 0xffffffff;
			MPFSClose(curHTTP.offsets);
			curHTTP.offsets = MPFS_INVALID_HANDLE;
		}
	}

    // We are not done sending a file yet...
    return FALSE;
}
Exemplo n.º 10
0
/*********************************************************************
 * Function:        MPFS_HANDLE MPFSOpenID(WORD fatID)
 *
 * PreCondition:    None
 *
 * Input:           fatID: the FAT ID of a previously opened file
 *
 * Output:          An MPFS_HANDLE to the reopened file
 *                  MPFS_INVALID_HANDLE if file not found or no free handles
 *
 * Side Effects:    None
 *
 * Overview:        None
 *
 * Note:            Use this function along with MPFSGetID() to 
 *					quickly re-open a file without tying up a 
 *					permanent MPFSStub
 ********************************************************************/
MPFS_HANDLE MPFSOpenID(WORD fatID)
{
	MPFS_HANDLE hMPFS;
	WORD i;
	
	// Make sure MPFS is unlocked and we got a filename
	if(isMPFSLocked == TRUE)
		return MPFS_INVALID_HANDLE;

	// Find a free file handle to use
	for(hMPFS = 1; hMPFS <= MAX_MPFS_HANDLES; hMPFS++)
		if(MPFSStubs[hMPFS].addr == MPFS_INVALID)
			break;
	if(hMPFS == MAX_MPFS_HANDLES)
		return MPFS_INVALID_HANDLE;
		
	// Initialize the FAT pointer
	MPFSStubs[0].addr = 0;

	// Read in the number of records
	MPFSStubs[0].bytesRem = 8;
	MPFSGetArray(0, NULL, 6);
	MPFSGetArray(0, (BYTE*)&i, 2);

	// Make sure ID isn't past the last record
	if(fatID >= i)
		return MPFS_INVALID_HANDLE;
		
	// Set up the file handle
	MPFSStubs[hMPFS].fatID = fatID;
	MPFSStubs[0].addr = 8 + 24*fatID + 8;
	MPFSStubs[0].bytesRem = 8;
	MPFSGetArray(0, (BYTE*)&MPFSStubs[hMPFS].addr, 4);
	MPFSGetArray(0, (BYTE*)&MPFSStubs[hMPFS].bytesRem, 4);
	
	return hMPFS;
}
Exemplo n.º 11
0
size_t FileRead(void *ptr, size_t size, size_t n, FILE_HANDLE stream)
{
    #if defined STACK_USE_MPFS2
        WORD length;
        length = size * n;
        return MPFSGetArray(stream, (BYTE*)ptr, length);
    #elif defined STACK_USE_MDD 
        if(ptr == NULL)
        {
            return 0;
        }
        else
        {
            return FSfread(ptr, size, n, stream);
        }
    #endif
}
Exemplo n.º 12
0
/*********************************************************************
 * Function:        DWORD MPFSGetSize(MPFS_HANDLE hMPFS)
 *
 * PreCondition:    hMPFS is a valid open file handle
 *
 * Input:           hMPFS: the file to find the size of
 *
 * Output:          the file size
 * 
 * Side Effects:    None
 *
 * Overview:        Reads a Length parameter from the MPFS Fat
 *
 * Note:            None
 ********************************************************************/
DWORD MPFSGetSize(MPFS_HANDLE hMPFS)
{
	DWORD val;
	
	// Make sure a valid file is open
	if(hMPFS > MAX_MPFS_HANDLES)
		return 0x00000000;
	if(MPFSStubs[hMPFS].addr == MPFS_INVALID)
		return 0x00000000;
	
	// Move to the point for reading
	LoadFATRecord(hMPFS);
	MPFSStubs[0].addr += 12;
	
	// Read the value and return
	MPFSGetArray(0, (BYTE*)&val, 4);
	return val;	
}
Exemplo n.º 13
0
/*****************************************************************************
  Function:
	BOOL MPFSGetFilename(MPFS_HANDLE hMPFS, BYTE* cName, WORD wLen)

  Description:
	Reads the file name of a file that is already open.
	
  Precondition:
	The file handle referenced by hMPFS is already open.

  Parameters:
	hMPFS - the file handle from which to determine the file name
	cName - where to store the name of the file
	wLen - the maximum length of data to store in cName

  Return Values:
	TRUE - the file name was successfully located
	FALSE - the file handle provided is not currently open
  ***************************************************************************/
BOOL MPFSGetFilename(MPFS_HANDLE hMPFS, BYTE* cName, WORD wLen)
{
	DWORD addr;
	
	// Make sure a valid file is open
	if(hMPFS > MAX_MPFS_HANDLES)
		return FALSE;
	if(MPFSStubs[hMPFS].addr == MPFS_INVALID)
		return FALSE;
	
	// Move to the point for reading
	_LoadFATRecord(MPFSStubs[hMPFS].fatID);
	addr = fatCache.string;
	MPFSStubs[0].addr = addr;
	MPFSStubs[0].bytesRem = 255;
	
	// Read the value and return
	MPFSGetArray(0, cName, wLen);
	return TRUE;
}
Exemplo n.º 14
0
/*****************************************************************************
  Function:
	BOOL MPFSGetLong(MPFS_HANDLE hMPFS, DWORD* ul)

  Description:
	Reads a DWORD or Long value from the MPFS.
	
  Precondition:
	The file handle referenced by hMPFS is already open.

  Parameters:
	hMPFS - the file handle from which to read
	ul - where to store the DWORD or long value that was read

  Returns:
	TRUE - The byte was successfully read
	FALSE - No byte was read because either the handle was invalid or
	        the end of the file has been reached.
  ***************************************************************************/
BOOL MPFSGetLong(MPFS_HANDLE hMPFS, DWORD* ul)
{
	return ( MPFSGetArray(hMPFS, (BYTE*)ul, 4) == 4u );
}
Exemplo n.º 15
0
MPFS_HANDLE MPFSOpenROM(ROM BYTE* cFile) 
{
	MPFS_HANDLE hMPFS;
	WORD nameHash, i;
	WORD hashCache[8];
	ROM BYTE *ptr;
	BYTE c;
	
	// Make sure MPFS is unlocked and we got a filename
	if(*cFile == '\0' || isMPFSLocked == TRUE)
		return MPFS_INVALID_HANDLE;

	// Calculate the name hash to speed up searching
	for(nameHash = 0, ptr = cFile; *ptr != '\0'; ptr++)
	{
		nameHash += *ptr;
		nameHash <<= 1;
	}
	
	// Find a free file handle to use
	for(hMPFS = 1; hMPFS <= MAX_MPFS_HANDLES; hMPFS++)
		if(MPFSStubs[hMPFS].addr == MPFS_INVALID)
			break;
	if(hMPFS == MAX_MPFS_HANDLES)
		return MPFS_INVALID_HANDLE;
		
	// Read in hashes, and check remainder on a match.  Store 8 in cache for performance
	for(i = 0; i < numFiles; i++)
	{
		// For new block of 8, read in data
		if((i & 0x07) == 0u)
		{
			MPFSStubs[0].addr = 8 + i*2;
			MPFSStubs[0].bytesRem = 16;
			MPFSGetArray(0, (BYTE*)hashCache, 16);
		}
		
		// If the hash matches, compare the full filename
		if(hashCache[i&0x07] == nameHash)
		{
			_LoadFATRecord(i);
			MPFSStubs[0].addr = fatCache.string;
			MPFSStubs[0].bytesRem = 255;
			
			// Loop over filename to perform comparison
			for(ptr = cFile; *ptr != '\0'; ptr++)
			{
				MPFSGet(0, &c);
				if(*ptr != c)
					break;
			}
			
			MPFSGet(0, &c);

			if(c == '\0' && *ptr == '\0')
			{// Filename matches, so return true
				MPFSStubs[hMPFS].addr = fatCache.data;
				MPFSStubs[hMPFS].bytesRem = fatCache.len;
				MPFSStubs[hMPFS].fatID = i;
				return hMPFS;
			}
		}
	}
	
	// No file name matched, so return nothing
	return MPFS_INVALID_HANDLE;
}
Exemplo n.º 16
0
int MPFS_Open ( uintptr_t handle, const char* filewithDisk, uint8_t mode )
{
    uint16_t nameHash = 0, ix = 0;
    uint16_t hashCache[8] = {};
    volatile const char *ptr = (volatile const char *)NULL;
    uint8_t c = 0;
    uint32_t hMPFS = 0;
    volatile const char* cFile = filewithDisk + 3;    // Take the file name without the disk number (ignore "0:/", so increment 3)


    if ( mpfsObject.mpfsMediaHandle == DRV_HANDLE_INVALID )
    {
        /* no opened media/uninitialized */
        return MPFS_INVALID_HANDLE;
    }

    /* Make sure MPFS is unlocked and we got a filename */
    if ( *cFile == '\0' )
    {
        return MPFS_INVALID_HANDLE;
    }

    if ( mpfsObject.isMPFSLocked == true  )
    {
        return MPFS_INVALID_HANDLE;
    }
    /* Calculate the name hash to speed up searching */
    for ( nameHash = 0, ptr = cFile; *ptr != '\0'; ptr++ )
    {
        nameHash += *ptr;
        nameHash <<= 1;
    }

    // Take a free file object as we want to open the MPFS file
    for(hMPFS = 1; hMPFS <= SYS_FS_MAX_FILES ; hMPFS++)
    {
        if( MPFSStubs[hMPFS].addr == MPFS_INVALID ) // not in use, so take it
        {
            break;
        }

    }

    /* If there is already more files opened than the allowed limit */
    if( hMPFS >= SYS_FS_MAX_FILES + 1 )
    {
        return MPFS_INVALID_HANDLE;
    }

    /* Read in hashes, and check remainder on a match.  Store 8 in cache
    for performance. */
    for ( ix = 0; ix < mpfsObject.numFiles; ix++ )
    {
        /* For new block of 8, read in data */
        if ( ( ix & 0x07 ) == 0u )
        {
            MPFSStubs[0].addr = 8 + ix * 2;
            MPFSStubs[0].bytesRem = 16;
            MPFSGetArray ( 0, ( uint8_t* )hashCache, 16 );
        }

        /* If the hash matches, compare the full filename */
        if ( hashCache[ix&0x07] == nameHash )
        {
            _LoadFATRecord(ix);
            MPFSStubs[0].addr = fatCache.string;
            MPFSStubs[0].bytesRem = 255;

            /* Loop over filename to perform comparison */
            for ( ptr = cFile; *ptr != '\0'; ptr++ )
            {
                MPFSGet ( 0, &c );
                if( *ptr != c )
                {
                        break;
                }
            }

            MPFSGet ( 0, &c );

            if ( ( c == '\0' ) && ( *ptr == '\0' ) )
            {
                /* Filename matches, so return true */
                MPFSStubs[hMPFS].addr = fatCache.data;
                MPFSStubs[hMPFS].bytesRem = fatCache.len;
                MPFSStubs[hMPFS].fatID = ix;

                /* Send the reference to the higher layer */
                MPFSStubs[hMPFS].index = hMPFS;
                *(uintptr_t *)handle =  MPFSStubs[hMPFS].index;

                /* Return the status */
                return MPFS_OK;
            }
        }
    }

    /* No file name matched, so return nothing  */
    return MPFS_NO_FILE;
}
Exemplo n.º 17
0
MPFS_HANDLE MPFSOpenROM(ROM BYTE* file) 
{
	MPFS_HANDLE hMPFS;
	WORD nameHash, i;
	ROM BYTE* ptr;
	BYTE c;
	
	// Make sure MPFS is unlocked and we got a filename
	if(*file == '\0' || isMPFSLocked == TRUE)
		return MPFS_INVALID_HANDLE;

	// Calculate the name hash for faster searching
	for(nameHash = 0, ptr = file; *ptr != '\0'; ptr++)
		nameHash += *ptr;
	
	// Find a free file handle to use
	for(hMPFS = 1; hMPFS <= MAX_MPFS_HANDLES; hMPFS++)
		if(MPFSStubs[hMPFS].addr == MPFS_INVALID)
			break;
	if(hMPFS == MAX_MPFS_HANDLES)
		return MPFS_INVALID_HANDLE;
		
	// Initialize the FAT pointer
	MPFSStubs[0].addr = 0;

	// Read in the number of records
	MPFSStubs[0].bytesRem = 8;
	MPFSGetArray(0, NULL, 6);
	MPFSGetArray(0, (BYTE*)&i, 2);
	MPFSStubs[0].bytesRem = i * (24);
	
	// Read in FAT records and compare the hash
	for(i = 0; MPFSGetArray(0, (BYTE*)&(MPFSStubs[hMPFS].fatID), 2) == 2; i++)
	{
		if(MPFSStubs[hMPFS].fatID == nameHash)
		{// If the hash matches, compare the full filename
			MPFSGetArray(0, NULL, 2);
			MPFSGetArray(0, (BYTE*)&(MPFSStubs[hMPFS].addr), 4);
			MPFSStubs[hMPFS].bytesRem = 255;

			// Loop over the filename
			for(ptr = file; *ptr != '\0'; ptr++)
			{
				MPFSGet(hMPFS, &c);
				if(*ptr != c)
					break;
			}

			MPFSGet(hMPFS, &c);

			if(c == '\0' && *ptr == '\0')
			{// Filename matches, so return true
				MPFSGetArray(0, (BYTE*)&(MPFSStubs[hMPFS].addr), 4);
				MPFSGetArray(0, (BYTE*)&(MPFSStubs[hMPFS].bytesRem), 4);
				MPFSStubs[hMPFS].fatID = i;
				return hMPFS;
			}
			else
			{// No match, so skip to next
				MPFSGetArray(0, NULL, 16);
			}
		}
		else
		{// No match, so skip to next
			MPFSGetArray(0, NULL, 22);
		}
	}
	
	// No file name matched, so return nothing
	MPFSStubs[hMPFS].addr = MPFS_INVALID;
	return MPFS_INVALID_HANDLE;
}
Exemplo n.º 18
0
/*****************************************************************************
  Function:
    int MPFS_Stat ( const char* filewithDisk, uintptr_t stat_str )

  Description:
    Returns the status (property) of the file

  Precondition:
    None

  Parameters:
    filewithDisk    -   string containing the Disk number appended to file name
    stat_str        -   pointer to structure which will return the file status

  Returns:
    Success     - MPFS_OK
    Failure      - MPFS_NO_FILE
*/
int MPFS_Stat ( const char* filewithDisk, uintptr_t stat_str )
{
    uint16_t nameHash = 0, ix = 0;
    uint16_t hashCache[8] = {};
    const char *ptr = (const char *)NULL, *ptr1 = (const char *)NULL;
    uint8_t c = 0;
    const char* cFile = filewithDisk + 3;    // Take the file name without the disk number (ignore "0:/", so increment 2)
    unsigned char i = 0;

    MPFS_STATUS *stat = (MPFS_STATUS *) stat_str;


    if ( mpfsObject.mpfsMediaHandle == DRV_HANDLE_INVALID )
    {
        /* no opened media/uninitialized */
        return 1;
    }

    /* Make sure MPFS is unlocked and we got a filename */
    if ( *cFile == '\0' )
    {
        return 1;
    }

    if ( mpfsObject.isMPFSLocked == true  )
    {
        return 1;
    }
    /* Calculate the name hash to speed up searching */
    for ( nameHash = 0, ptr = cFile; *ptr != '\0'; ptr++ )
    {
        nameHash += *ptr;
        nameHash <<= 1;
    }

    /* Read in hashes, and check remainder on a match.  Store 8 in cache
    for performance. */
    for ( ix = 0; ix < mpfsObject.numFiles; ix++ )
    {
        /* For new block of 8, read in data */
        if ( ( ix & 0x07 ) == 0u )
        {
            MPFSStubs[0].addr = 8 + ix * 2;
            MPFSStubs[0].bytesRem = 16;
            MPFSGetArray ( 0, ( uint8_t* )hashCache, 16 );
        }

        /* If the hash matches, compare the full filename */
        if ( hashCache[ix&0x07] == nameHash )
        {
            _LoadFATRecord(ix);
            MPFSStubs[0].addr = fatCache.string;
            MPFSStubs[0].bytesRem = 255;

            /* Loop over filename to perform comparison */
            for ( ptr = cFile; *ptr != '\0'; ptr++ )
            {
                MPFSGet ( 0, &c );
                if( *ptr != c )
                {
                        break;
                }
            }

            MPFSGet ( 0, &c );

            if ( ( c == '\0' ) && ( *ptr == '\0' ) )
            {
                stat->fattrib = fatCache.flags;
                stat->fdate = (unsigned short)(fatCache.timestamp >> 16);
                stat->ftime = (unsigned short)(fatCache.timestamp);
                stat->fsize = fatCache.len;
                for ( ptr1 = cFile, i = 0; *ptr1 != '\0'; ptr1++, i++ )
                {
                    stat->fname[i] = *ptr1;
                }

                /* Return the status */
                return 0;
            }
        }
Exemplo n.º 19
0
/*****************************************************************************
  Function:
	bool MPFSGetLong(MPFS_HANDLE hMPFS, uint32_t* ul)

  Description:
	Reads a uint32_t or Long value from the MPFS.
	
  Precondition:
	The file handle referenced by hMPFS is already open.

  Parameters:
	hMPFS - the file handle from which to read
	ul - where to store the uint32_t or long value that was read

  Returns:
	true - The byte was successfully read
	false - No byte was read because either the handle was invalid or
	        the end of the file has been reached.
  ***************************************************************************/
bool MPFSGetLong(MPFS_HANDLE hMPFS, uint32_t* ul)
{
	return ( MPFSGetArray(hMPFS, (uint8_t*)ul, 4) == 4u );
}