📄 flashfiles.c
字号:
/*
* Copyright (c) 1995,2000 TriMedia Technologies Inc.
*
* +------------------------------------------------------------------+
* | This software is furnished under a license and may only be used |
* | and copied in accordance with the terms and conditions of such |
* | a license and with the inclusion of this copyright notice. This |
* | software or any other copies of this software may not be provided|
* | or otherwise made available to any other person. The ownership |
* | and title of this software is not transferred. |
* | |
* | The information in this software is subject to change without |
* | any prior notice and should not be construed as a commitment by |
* | TriMedia Technologies. |
* | |
* | this code and information is provided "as is" without any |
* | warranty of any kind, either expressed or implied, including but |
* | not limited to the implied warranties of merchantability and/or |
* | fitness for any particular purpose. |
* +------------------------------------------------------------------+
*
* Module name : flashfiles.c 1.4
*
* Last update : 10:25:45 - 97/12/02
*
* This is a demo of how a FLASH file system
* can be accessed using IO Drivers.
*
* This demo maintains a list of files in
* memory which can be accessed via the
* set of memory file driver routines which
* are defined in this module, and which are
* installed in the initialisation routine
* at the bottom. These memory file driver routines
* are enabled by opening files using the name
* pattern as defined in the macro NAME_MASK below.
*
* The simulated 'flash' files are loaded using
* from disk in the initialisation. Only one
* files is loaded for specific use by main.c
*
* lib_image : the image of a dynamic library lib.dll.
*/
#include "tmlib/IODrivers.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
/* --- . --- */
#define NAME_MASK "/dev/FLASH%d"
typedef struct FlashFile {
Address flash;
Int32 size;
} *FlashFile;
typedef struct FlashFD {
Address pos;
FlashFile file;
} *FlashFD;
/* --- . --- */
#define FLASH_CAPACITY 100
static struct FlashFile flashfiles[FLASH_CAPACITY];
static Int32 nr_flashfiles = 0;
static Bool
load_file_to_flash(String path)
{
if (nr_flashfiles == FLASH_CAPACITY) {
return False;
}
else {
FILE *f;
struct stat buf;
f = fopen(path, "rb");
if (f == Null) {
return False;
}
else {
stat(path, &buf);
flashfiles[nr_flashfiles].flash = (Address) malloc(buf.st_size);
flashfiles[nr_flashfiles].size = buf.st_size;
fread(flashfiles[nr_flashfiles].flash, 1, buf.st_size, f);
fclose(f);
nr_flashfiles++;
return True;
}
}
}
/* --- . --- */
static Bool
RecogFlash(String path)
{
Int32 index;
return sscanf(path, NAME_MASK, &index) == 1;
}
static FlashFD
OpenFlash(String path, Int32 oflag, Int32 mode)
{
FlashFD result = (Pointer) malloc(sizeof (*result));
if (result == Null) {
free(result);
return (FlashFD) - 1;
}
else {
Int32 index;
sscanf(path, NAME_MASK, &index);
if (index < 0 || index >= nr_flashfiles) {
free(result);
return (FlashFD) - 1;
}
else {
result->pos = flashfiles[index].flash;
result->file = &flashfiles[index];
return result;
}
}
}
static Int32
CloseFlash(FlashFD fd)
{
free(fd);
return 0;
}
static Int32
ReadFlash(FlashFD fd, Address buffer, Int32 size)
{
Int32 sizeleft = (fd->file->flash + fd->file->size) - fd->pos;
if (size > sizeleft)
size = sizeleft;
memcpy(buffer, fd->pos, size);
fd->pos += size;
return size;
}
static Int32
SeekFlash(FlashFD fd, long offset, int ptrname)
{
switch (ptrname) {
case 0:fd->pos = fd->file->flash + offset;
return 0;
case 1:
fd->pos = fd->pos + offset;
return 0;
case 2:
return -1;
default:
return 0;
}
}
static Int32
IsattyFlash(FlashFD fd)
{
return 0;
}
static Int32
StatFlash(FlashFD fd, struct stat * buf)
{
buf->st_mode = S_IFREG;
buf->st_blksize = 8000;
buf->st_size = fd->file->size;
return 0;
}
static FlashFD
OpenDLL(String path)
{
if (strcmp(path, "lib.dll") == 0) {
return OpenFlash("/dev/FLASH0", 0, 0); /* mode and flags not
* used in OpenFlash */
} else {
return (FlashFD)-1;
}
}
/* --- . --- */
FlashFiles_init()
{
load_file_to_flash("lib_image");
IOD_install_driver(
(IOD_RecogFunc) RecogFlash,
(IOD_InitFunc) Null,
(IOD_TermFunc) Null,
(IOD_OpenFunc) OpenFlash,
(IOD_OpenDllFunc) OpenDLL,
(IOD_CloseFunc) CloseFlash,
(IOD_ReadFunc) ReadFlash,
(IOD_WriteFunc) Null,
(IOD_SeekFunc) SeekFlash,
(IOD_IsattyFunc) IsattyFlash,
(IOD_FstatFunc) StatFlash,
(IOD_FcntlFunc) Null,
(IOD_StatFunc) Null
);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -