KMSAgent_direct.h revision 4f14b0f29aa144cc03efdde5508ae126ae197acf
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
*/
/**
* \file direct.h
*
* This file is included in KMSAgentStorage.cpp to provide file handling
* hooks. It also contains our EEPROM file handling functions.
*
* HISTORY:
* 8/6/07 BJB Added code for 9840D encryption sectors
* 4/7/08 BJB Changed to remove 'file is junk' message - EVT
* didn't like it.
* 4/24/08 BJB Added function call back to T10000/9840D code
* to lookup eeprom address -
* so it isn't a hardcoded value.
* 05/09/08 BJB Added being more conservative about EEPROM so as to change
* frequency of 32CA problems
*
*/
#ifndef KMSAGENT_DIRECT_H
#define KMSAGENT_DIRECT_H
#include <stdio.h>
#include <string.h>
#ifdef WIN32
#include <direct.h>
#endif
#ifndef METAWARE
#else // METAWARE
#include "SYSCommon.h"
#include "debug.h"
#include <snprintf.h>
extern char st[256];
#ifndef I_KNOW_REAL_NAMES
#define fopen my_eeprom_fopen
#define fclose my_eeprom_fclose
#define fputs my_eeprom_fputs
#define fwrite my_eeprom_fwrite
#define fread my_eeprom_fread
#define fgets my_eeprom_fgets
#define unlink my_eeprom_unlink
#define rmdir my_eeprom_rmdir
#define my_eeprom_rmdir(a) (1)
#endif
#include "KMSClientProfile.h"
extern char KMSAgent_Version[KMS_MAX_VERSION_LENGTH];
unsigned char volatile* eeprom_addr_lookup_FILESYSTEM_START_ADDR(void);
unsigned int EEPROM_Sector_size(void);
// was ((unsigned char volatile*)0xf20000) on 9840
// was ((unsigned char volatile*)0x41dc0000) on T10000
#define FILESYSTEM_SECTOR_SIZE (EEPROM_Sector_size())
// was hardcoded 0x20000
#define EEPROM_ERASED_BYTE 0xFF
/* size of a file allowed (not counting the name) */
#define EEPROM_FILE_SECT_SIZE 0x1800
/* size of a file name allowed (+ the null) */
#define EEPROM_FNAME_SIZE 0x80
// 1000h = 4096d
/* number of 'files' available */
#define FTABLE_SIZE 0x5
#define UCHAR8 unsigned char
#define UINT32 unsigned long
extern "C" unsigned long
EEPROM_Sector_Erase( unsigned char volatile *sector_address );
extern "C" unsigned long
extern "C" unsigned long
/* a eeprom file */
struct eepromBlockStruct
{
unsigned long fsize;
char fname[EEPROM_FNAME_SIZE];
unsigned char fdata[EEPROM_BLOCK_SIZE];
};
struct fileSystem
{
/* what level of toolkit wrote this - for
future compatibility */
};
#define MODE_WRITE 0x01
#define MODE_READ 0x02
#define MYFILE_CLOSED 0x0
#define MYFILE_OPEN_READ 0x1
#define MYFILE_OPEN_WRITE 0x2
/* an open file */
typedef struct fileStruct
{
unsigned char *readptr;
unsigned long readsize;
unsigned char *writeptr;
struct eepromBlockStruct *memptr;
unsigned long mode;
unsigned long file_number;
} myFILE;
extern "C" int my_eeprom_fsys_erase(void);
#ifndef FALSE
#define FALSE 0
#endif
#ifndef TRUE
#define TRUE 1
#endif
#ifdef DEFINE_STORAGE
/* ram copy of the file system */
struct fileSystem ramFileSystem;
#else
/* ram copy of the file system */
extern struct fileSystem ramFileSystem;
extern int file_read;
extern unsigned long fstate[FTABLE_SIZE];
#endif
#ifdef DEFINE_STORAGE
extern "C" unsigned long
/* UNIT TEST */
int TESTV = 0;
/* UNIT TEST */
#define MY_FLASH_READ(a,b,c) \
Flash_Read(a,b,c); \
if (TESTV++ < 5) { \
}
/**
* my_eeprom_fopen()
* my filesystem fileopen
*/
{
int i;
int open_for_write = TRUE;
int retries = 100;
{
// write
}
{
// read binary
}
else
{
FATAL_APPLICATION_STATE("my_eeprom_fopen : Illegal mode");
}
/* read the file table from eeprom? */
if (!file_read)
{
MY_FLASH_READ((unsigned char *)&ramFileSystem,
(unsigned char *)FILESYSTEM_SECTOR,
sizeof(ramFileSystem));
/* Audit the filesystem - if it looks junked, reclaim bad files */
for (i=0; i < FTABLE_SIZE; i++)
{
{
/* if this looks bad, don't accept this outcome until
100 retries */
if (retries-- > 0)
{
"KMSAgent_direct::fopen file %d is junk - (name %x, "
"size %x, data %x) RETRYING \n",
i,
goto RETRY;
}
else
{
"KMSAgent_direct::fopen file %d is junk - (name %x, "
"size %x, data %x) formatting it\n",
i,
/* set data to reasonable starting values */
}
}
} /* for */
} /* !file read */
/* read the file table */
for (i=0; i < FTABLE_SIZE; i++)
{
/* compare filename to ram copy filename */
{
if (fstate[i] != MYFILE_CLOSED)
{
log_printf("KMSAgent_direct::fopen FN=%s file matches, "
"FILE %d WAS ALREADY OPEN for %s with mode %s"
"file size is %d \n",
i,
mode,
}
/* create a new ramfile handle */
/* since file is being rewritten, it is now size 0 */
if (open_for_write)
/* remember the file is open */
/* if filename matches, set file pointers to it */
file->file_number = i;
log_printf("KMSAgent_direct::fopen FN=%s file matches, "
"opening existing file number %d with mode %s"
"file size is %d \n",
i,
mode,
break;
}
}
/* if no existing entry was found, assign a new entry to this filename */
if (i == FTABLE_SIZE)
{
/* couldn't find an old file and opened for read so return NULL */
if (open_for_write == FALSE)
{
log_printf("KMSAgent_direct::fopen FN=%s no file match, "
"file opened for read so return NULL %d\n",
i);
return NULL;
}
/* find a free file pointer */
for (i=0; i < FTABLE_SIZE ; i++)
{
/* empty data will be 0xffffffff since it was copied
straight from EEPROM */
{
log_printf("KMSAgent_direct::fopen FN=%s no file match, "
"opening new file %d\n",
i);
/* give it a filename - marks this as in use */
/* set filesize to zero */
/* remember the file is open */
/* create a new ramfile handle */
/* if filename matches, set file pointers to it */
file->file_number = i;
break;
}
}
if (i == FTABLE_SIZE)
{
log_error_printf("KMSAgent_direct::fopen FN=%s no free files \n",
filename);
/* no free entries - bail */
return NULL;
}
}
/* return pointer to the myFILE struct created */
return file;
}
/**
* my_eeprom_fclose()
* my filesystem fileclose
*/
{
FATAL_ASSERT(f);
#ifdef DEBUG
log_printf("KMSAgent_direct::fclose FN=%s, "
"FILE %d "
"file size is %d \n",
f->file_number,
#endif
{
log_error_printf("KMSAgent_direct::fclose FN=%s, "
"FILE %d WAS ALREADY CLOSED "
"file size is %d \n",
f->file_number,
}
/* remember the file is closed */
/* don't rewrite eeprom unless file was opened for writing */
if (f->mode == MODE_WRITE)
{
/* Erase the sector we are rewriting */
EEPROM_Sector_Erase( (unsigned char*)FILESYSTEM_SECTOR ) ;
/* remember the version of toolkit that wrote this filesystem
(for any future compatibility problems). */
sizeof(KMSAgent_Version));
/* flush the memory indicated by myFILE * out to EEPROM */
/* see boot_eeprom.c for details on Flash_Write */
Flash_Write ((unsigned char*)FILESYSTEM_SECTOR,
(unsigned char*)&ramFileSystem,
}
/* delete the placeholder file */
free(f);
return 0;
}
/**
* my_eeprom_fputs()
* my filesystem put string
*/
{
/* put the string to the memory indicated by myFILE */
int i;
#ifdef DEBUG
"\nKMSAgent_direct::my_eeprom_fputs "
"f->writeptr =%x "
"f->readptr=%x "
"f->readsize=%x "
"f->memptr=%x"
"f->memptr->fsize=%x\n",
f->writeptr,
f->readptr,
f->readsize,
f->memptr,
#endif
FATAL_ASSERT(s);
FATAL_ASSERT(f);
for (i=0;;i++)
{
/* end of input string? quit */
if (*s == '\0')
break;
/* end of max file memory, quit */
if ((unsigned char*)s >=
(unsigned char*)(f->memptr + sizeof(struct eepromBlockStruct)))
break;
/* copy */
*(f->writeptr) = *s++;
f->writeptr++;
/* increment the filesize */
}
#ifdef DEBUG
"\nKMSAgent_direct::fputs wrote %d chars, "
"filesize is %d\n",
i,
#endif
/* return how many chars were written */
return i;
}
/**
* my_eeprom_fwrite()
* my filesystem put string
*/
extern "C" int my_eeprom_fwrite(const char *s,
int size,
int nobj,
myFILE *f)
{
/* put the string to the memory indicated by myFILE */
int i,j;
"\nKMSAgent_direct::my_eeprom_fputs "
"f->writeptr =%x "
"f->readptr=%x "
"f->readsize=%x "
"f->memptr=%x"
"f->memptr->fsize=%x\n",
f->writeptr,
f->readptr,
f->readsize,
f->memptr,
FATAL_ASSERT(s);
FATAL_ASSERT(f);
for (i=0; i<nobj; i++)
{
for (j=0; j<size; j++)
{
/* end of max file memory, quit */
if ((unsigned char*)s >=
(unsigned char*)(f->memptr + sizeof(struct eepromBlockStruct)))
goto DONE;
/* copy */
*(f->writeptr) = *s++;
f->writeptr++;
/* increment the filesize */
}
}
DONE:
#ifdef DEBUG
"\nKMSAgent_direct::fwrite wrote %d chars, "
"filesize is %d\n",
i,
#endif
/* return how many objects were written */
return i;
}
/**
* my_eeprom_fgets()
* my filesystem getstring
*/
{
/* put the string to the memory indicated by myFILE */
int i;
char *tmp = s;
FATAL_ASSERT(s);
FATAL_ASSERT(n > 0);
FATAL_ASSERT(f);
#ifdef DEBUG
"\nKMSAgent_direct::my_eeprom_fgets from file %s "
"fileptr %x "
"readptr = %x "
"readsize = %d "
"filesize = %d \n",
f,
f->readptr,
f->readsize,
#endif
/* get up to n-1 (and a \0) chars */
for (i=0; i < n; i++)
{
/* end of read line? quit */
if (*f->readptr == '\n')
{
/* increment the number of bytes read */
f->readptr++;
f->readsize++;
break;
}
/* trying to read beyond what was written? quit */
if (f->readsize >
{
#ifdef DEBUG
"\nKMSAgent_direct::fgets eof after %d chars, "
"readsize is %d "
"filesize is %d\n",
i,
f->readsize,
#endif
return NULL;
}
/* trying to read beyond max file memory, quit */
if ((unsigned char*)f->readptr >=
(unsigned char*)(f->memptr + sizeof(struct eepromBlockStruct)))
{
#ifdef DEBUG
"\nKMSAgent_direct::fgets end of max file after %d chars "
"readsize is %d "
"filesize is %d\n",
i,
f->readsize,
#endif
/* EOF or error is signalled by NULL return */
return NULL;
}
/* copy a char */
/* increment the number of bytes read */
f->readptr++;
f->readsize++;
} /* for */
/* terminate the returned string with a 0, but not a \n */
*tmp='\0';
#ifdef DEBUG
/* terminate returned string after a non-absurd number of chars
*/
"\nKMSAgent_direct::fgets string [%s] "
"end of line after %d chars "
"readsize is %d "
"filesize is %d\n",
s,
i,
f->readsize,
#endif
/* on success, return the original pointer */
return s;
}
/**
* my_eeprom_fread()
* my filesystem read a file
*/
{
/* put the string to the memory indicated by myFILE */
int i,j;
FATAL_ASSERT(s);
FATAL_ASSERT(f);
for (i=0; i<nobj; i++)
{
for (j=0; j<size; j++)
{
/* end of written file memory, quit */
if ( (unsigned char*) s >=
goto DONE;
/* copy */
*s++ = *(f->readptr);
f->readptr++;
}
}
DONE:
#ifdef DEBUG
"\nKMSAgent_direct::fread read %d chars, "
"filesize is %d\n",
i,
#endif
/* return how many objects were written */
return i;
}
/**
* my_eeprom_unlink() - delete the file named
*/
{
int i;
/* assume the file is closed */
/* read the file table */
for (i=0; i < FTABLE_SIZE; i++)
{
if (fstate[i] != MYFILE_CLOSED)
{
log_error_printf("KMSAgent_direct::unlink FN=%s file matches, "
"FILE %d WAS STILL OPEN for %s"
"file size is %d \n",
i,
}
/* compare input filename to ram copy filename */
{
/* if filename matches, zero it out to delete the file */
/* remember the file is closed */
fstate[i]= MYFILE_CLOSED;
/* remember the version of toolkit that wrote this filesystem
(for any future compatibility problems). */
sizeof(KMSAgent_Version));
/* flush the memory indicated by myFILE * out to EEPROM */
Flash_Write ((unsigned char*)FILESYSTEM_SECTOR,
(unsigned char*)&ramFileSystem,
break;
}
}
#ifdef DEBUG
log_printf ("my_eeprom_unlink : returning");
#endif
if (i == FTABLE_SIZE)
return -1;
/* success */
return 0;
}
/**
* my_eeprom_fsys_erase() - delete all files
*/
extern "C" int my_eeprom_fsys_erase(void)
{
int i;
/* read the file table */
for (i=0; i < FTABLE_SIZE; i++)
{
if (fstate[i] != MYFILE_CLOSED)
{
log_error_printf("KMSAgent_direct::fsys_erase FN=%s file "
"FILE %d WAS STILL OPEN for %s "
"file size is %d \n",
i,
}
/* zero filename out to delete the file */
/* remember the file is closed */
fstate[i]= MYFILE_CLOSED;
/* remember the version of toolkit that wrote this filesystem
(for any future compatibility problems). */
sizeof(KMSAgent_Version));
/* flush the memory indicated by myFILE * out to EEPROM */
Flash_Write ((unsigned char*)FILESYSTEM_SECTOR,
(unsigned char*)&ramFileSystem,
}
#ifdef DEBUG
log_printf ("\nmy_eeprom_fsys_erase : returning");
#endif
if (i == FTABLE_SIZE)
return -1;
/* success */
return 0;
}
#endif // DEFINE_STORAGE
#endif // METAWARE
#endif