xref: /onnv-gate/usr/src/lib/libkmsagent/common/KMSAgent_direct.h (revision 12720:3db6e0082404)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
24  */
25 
26 /**
27  * \file direct.h
28  *
29  * This file is included in KMSAgentStorage.cpp to provide file handling
30  * hooks. It also contains our EEPROM file handling functions.
31  *
32  * HISTORY:
33  * 8/6/07   BJB  Added code for 9840D encryption sectors
34  * 4/7/08   BJB  Changed to remove 'file is junk' message - EVT
35  *                 didn't like it.
36  * 4/24/08  BJB  Added function call back to T10000/9840D code
37  *                 to lookup eeprom address -
38  *                 so it isn't a hardcoded value.
39  * 05/09/08 BJB  Added being more conservative about EEPROM so as to change
40  *                 frequency of 32CA problems
41  *
42  */
43 
44 
45 #ifndef KMSAGENT_DIRECT_H
46 #define KMSAGENT_DIRECT_H
47 
48 #include <stdio.h>
49 #include <string.h>
50 
51 #ifdef WIN32
52 #include <direct.h>
53 #endif
54 
55 #define my_unlink(a) unlink(a)
56 #define my_rmdir(a)  rmdir(a)
57 
58 #ifndef METAWARE
59 
60 #define myFILE FILE
61 
62 #else // METAWARE
63 
64 #include "SYSCommon.h"
65 #include "debug.h"
66 #include <snprintf.h>
67 
68 extern "C" void ecpt_trace_msg(ECPT_TRACE_ENTRY*,const char*,...);
69 
70 extern char st[256];
71 
72 
73 #ifndef I_KNOW_REAL_NAMES
74 #undef fopen
75 #undef fclose
76 #undef fputs
77 #undef fgets
78 #undef fwrite
79 #undef fread
80 #undef unlink
81 #undef rmdir
82 
83 #define fopen  my_eeprom_fopen
84 #define fclose my_eeprom_fclose
85 #define fputs  my_eeprom_fputs
86 #define fwrite my_eeprom_fwrite
87 #define fread  my_eeprom_fread
88 #define fgets  my_eeprom_fgets
89 #define unlink my_eeprom_unlink
90 #define rmdir  my_eeprom_rmdir
91 
92 #define my_eeprom_rmdir(a)  (1)
93 
94 #endif
95 
96 
97 #include "KMSClientProfile.h"
98 extern char KMSAgent_Version[KMS_MAX_VERSION_LENGTH];
99 
100 unsigned char volatile* eeprom_addr_lookup_FILESYSTEM_START_ADDR(void);
101 unsigned int  EEPROM_Sector_size(void);
102 
103 #define FILESYSTEM_SECTOR     (eeprom_addr_lookup_FILESYSTEM_START_ADDR())
104 // was ((unsigned char volatile*)0xf20000)   on 9840
105 // was ((unsigned char volatile*)0x41dc0000) on T10000
106 
107 #define FILESYSTEM_SECTOR_SIZE (EEPROM_Sector_size())
108 // was hardcoded 0x20000
109 
110 #define EEPROM_ERASED_BYTE      0xFF
111 
112 /* size of a file allowed (not counting the name) */
113 #define EEPROM_FILE_SECT_SIZE   0x1800
114 
115 /* size of a file name allowed (+ the null) */
116 #define EEPROM_FNAME_SIZE       0x80
117 
118 // 1000h = 4096d
119 #define EEPROM_BLOCK_SIZE       (EEPROM_FILE_SECT_SIZE - EEPROM_FNAME_SIZE - 4)
120 
121 /*  number of 'files' available */
122 #define FTABLE_SIZE             0x5
123 
124 
125 #define UCHAR8 unsigned char
126 #define UINT32 unsigned long
127 
128 
129 extern "C" unsigned long
130 EEPROM_Sector_Erase( unsigned char volatile *sector_address );
131 
132 extern "C" unsigned long
133 Flash_Write( UCHAR8 *destinationP,
134              UCHAR8 *sourceP,
135              UINT32  byteCount);
136 
137 extern "C" unsigned long
138 Flash_Read( UCHAR8 *dstP,
139             UCHAR8 *srcP,
140             UINT32  byteCount);
141 
142 /* a eeprom file */
143 struct eepromBlockStruct
144 {
145    unsigned long fsize;
146    char          fname[EEPROM_FNAME_SIZE];
147    unsigned char fdata[EEPROM_BLOCK_SIZE];
148 };
149 
150 
151 struct fileSystem
152 {
153    struct eepromBlockStruct ftable[FTABLE_SIZE];
154    /* what level of toolkit wrote this - for
155       future compatibility */
156    char writingversion[KMS_MAX_VERSION_LENGTH+1];
157 };
158 
159 
160 #define MODE_WRITE 0x01
161 #define MODE_READ  0x02
162 
163 #define MYFILE_CLOSED      0x0
164 #define MYFILE_OPEN_READ   0x1
165 #define MYFILE_OPEN_WRITE  0x2
166 
167 
168 /* an open file */
169 typedef struct fileStruct
170 {
171    unsigned char *readptr;
172    unsigned long readsize;
173    unsigned char *writeptr;
174    struct eepromBlockStruct *memptr;
175    unsigned long mode;
176    unsigned long file_number;
177 } myFILE;
178 
179 
180 extern "C" myFILE *my_eeprom_fopen  (const char *filename, const char *mode);
181 extern "C" int     my_eeprom_fclose (myFILE *stream);
182 extern "C" int     my_eeprom_fputs  (const char *s, myFILE *stream);
183 extern "C" int     my_eeprom_fwrite (const char *s, int size, int nobj, myFILE *f);
184 extern "C" int     my_eeprom_fread  (char *s, int size, int nobj, myFILE *f);
185 extern "C" char   *my_eeprom_fgets  (char *s, int n, myFILE *stream);
186 extern "C" int     my_eeprom_unlink (const char *filename);
187 extern "C" int     my_eeprom_fsys_erase(void);
188 
189 #ifndef FALSE
190 #define FALSE 0
191 #endif
192 #ifndef TRUE
193 #define TRUE 1
194 #endif
195 
196 #ifdef DEFINE_STORAGE
197 /* ram copy of the file system */
198 struct fileSystem ramFileSystem;
199 int file_read = FALSE;
200 
201 /* file state - open/closed/etc */
202 unsigned long fstate[FTABLE_SIZE] = {MYFILE_CLOSED,
203                                      MYFILE_CLOSED,
204                                      MYFILE_CLOSED,
205                                      MYFILE_CLOSED,
206                                      MYFILE_CLOSED};
207 #else
208 /* ram copy of the file system */
209 extern struct fileSystem ramFileSystem;
210 extern int file_read;
211 
212 /* file state - open/closed/etc */
213 extern unsigned long fstate[FTABLE_SIZE];
214 
215 #endif
216 
217 
218 
219 #ifdef DEFINE_STORAGE
220 
221 
222 extern "C" unsigned long
223 
224 
225 /* UNIT TEST */
226 int TESTV = 0;
227 
228 /* UNIT TEST */
229 #define MY_FLASH_READ(a,b,c) \
230   Flash_Read(a,b,c); \
231   if (TESTV++ < 5) { \
232      ramFileSystem.ftable[0].fname[0] = EEPROM_ERASED_BYTE; \
233      ramFileSystem.ftable[1].fsize = EEPROM_BLOCK_SIZE+1;   \
234   }
235 
236 
237 
238 /**
239  * my_eeprom_fopen()
240  * my filesystem fileopen
241  */
my_eeprom_fopen(const char * filename,const char * mode)242 extern "C" myFILE *my_eeprom_fopen(const char *filename, const char *mode)
243 {
244    int i;
245    myFILE *file;
246    int open_for_write = TRUE;
247    static int Firsttime = TRUE;
248    int retries = 100;
249 
250    ECPT_TRACE_ENTRY   *trace = NULL;
251    ECPT_TRACE( trace, my_eeprom_fopen ) ;
252    ecpt_trace_msg( trace, "file %s", (char*)filename);
253 
254    FATAL_ASSERT(filename);
255    FATAL_ASSERT(mode);
256 
257    if ( (strcmp(mode,"wb") == 0) ||
258         (strcmp(mode,"w") == 0))
259    {
260       // write
261       open_for_write = TRUE;
262    }
263    else if ( (strcmp(mode,"r") == 0) ||
264              (strcmp(mode,"rb") == 0) )
265    {
266       // read binary
267       open_for_write = FALSE;
268    }
269    else
270    {
271       FATAL_APPLICATION_STATE("my_eeprom_fopen : Illegal mode");
272    }
273 
274    /* read the file table from eeprom? */
275    if (!file_read)
276    {
277      RETRY:
278 
279       MY_FLASH_READ((unsigned char *)&ramFileSystem,
280                     (unsigned char *)FILESYSTEM_SECTOR,
281                     sizeof(ramFileSystem));
282 
283       /* Audit the filesystem - if it looks junked, reclaim bad files */
284 
285       for (i=0; i < FTABLE_SIZE; i++)
286       {
287          if ( (ramFileSystem.ftable[i].fname[0] == EEPROM_ERASED_BYTE) ||
288               (ramFileSystem.ftable[i].fsize >     EEPROM_BLOCK_SIZE)  )
289          {
290             /* if this looks bad, don't accept this outcome until
291                100 retries */
292             if (retries-- > 0)
293             {
294                ecpt_trace_msg( trace, "file was junk - retrying %s", (char*)filename);
295                log_error_printf(
296                   "KMSAgent_direct::fopen file %d is junk - (name %x, "
297                   "size %x, data %x) RETRYING \n",
298                   i,
299                   ramFileSystem.ftable[i].fname[0],
300                   ramFileSystem.ftable[i].fsize ,
301                   ramFileSystem.ftable[i].fdata[0]);
302 
303                goto RETRY;
304             }
305             else
306             {
307                ecpt_trace_msg( trace, "file was junk - formatting %s", (char*)filename);
308                log_error_printf(
309                   "KMSAgent_direct::fopen file %d is junk - (name %x, "
310                   "size %x, data %x) formatting it\n",
311                   i,
312                   ramFileSystem.ftable[i].fname[0],
313                   ramFileSystem.ftable[i].fsize ,
314                   ramFileSystem.ftable[i].fdata[0]);
315 
316                /* set data to reasonable starting values */
317                ramFileSystem.ftable[i].fname[0] = 0;
318                ramFileSystem.ftable[i].fsize    = 0;
319                ramFileSystem.ftable[i].fdata[0] = 0;
320             }
321 
322          }
323 
324       } /* for */
325 
326       file_read = TRUE;
327 
328    } /* !file read */
329 
330    /* read the file table */
331    for (i=0; i < FTABLE_SIZE; i++)
332    {
333       /* compare filename to ram copy filename */
334       if (strcmp(filename, (const char *)ramFileSystem.ftable[i].fname) == 0)
335       {
336 
337          if (fstate[i] != MYFILE_CLOSED)
338          {
339             log_printf("KMSAgent_direct::fopen FN=%s file matches, "
340                        "FILE %d WAS ALREADY OPEN for %s with mode %s"
341                        "file size is %d \n",
342                        filename,
343                        i,
344                        (fstate[i] == MYFILE_OPEN_WRITE ? "WRITE": "READ"),
345                        mode,
346                        ramFileSystem.ftable[i].fsize);
347          }
348 
349          /* create a new ramfile handle */
350          file = malloc(sizeof(myFILE));
351 
352          /* since file is being rewritten, it is now size 0 */
353          if (open_for_write)
354             ramFileSystem.ftable[i].fsize = 0;
355 
356          /* remember the file is open */
357          fstate[i]= open_for_write ? MYFILE_OPEN_WRITE : MYFILE_OPEN_READ;
358 
359 
360          /* if filename matches, set file pointers to it */
361          file->writeptr = file->readptr = ramFileSystem.ftable[i].fdata;
362          file->memptr   = &ramFileSystem.ftable[i];
363          file->readsize = 0;
364          file->mode = open_for_write ? MODE_WRITE : MODE_READ;
365          file->file_number = i;
366 
367 
368          log_printf("KMSAgent_direct::fopen FN=%s file matches, "
369                     "opening existing file number %d with mode %s"
370                     "file size is %d \n",
371                     filename,
372                     i,
373                     mode,
374                     ramFileSystem.ftable[i].fsize);
375 
376          break;
377       }
378 
379    }
380 
381    /* if no existing entry was found, assign a new entry to this filename */
382    if (i == FTABLE_SIZE)
383    {
384       /* couldn't find an old file and opened for read so return NULL */
385       if (open_for_write == FALSE)
386       {
387 
388 
389          log_printf("KMSAgent_direct::fopen FN=%s no file match, "
390                     "file opened for read so return NULL %d\n",
391                     filename,
392                     i);
393 
394 
395          return NULL;
396       }
397 
398       /* find a free file pointer */
399       for (i=0; i < FTABLE_SIZE ; i++)
400       {
401          /* empty data will be 0xffffffff since it was copied
402             straight from EEPROM */
403          if (ramFileSystem.ftable[i].fname[0] == '\0')
404          {
405             log_printf("KMSAgent_direct::fopen FN=%s no file match, "
406                        "opening new file %d\n",
407                        filename,
408                        i);
409 
410             /* give it a filename - marks this as in use */
411             strcpy ((char *)ramFileSystem.ftable[i].fname, filename);
412 
413             /* set filesize to zero */
414             ramFileSystem.ftable[i].fsize = 0;
415 
416             /* remember the file is open */
417             fstate[i]= open_for_write ? MYFILE_OPEN_WRITE : MYFILE_OPEN_READ;
418 
419 
420             /* create a new ramfile handle */
421             file = malloc(sizeof(myFILE));
422 
423             /* if filename matches, set file pointers to it */
424             file->writeptr = file->readptr = ramFileSystem.ftable[i].fdata;
425             file->memptr =  &ramFileSystem.ftable[i];
426             file->readsize = 0;
427             file->mode = open_for_write ? MODE_WRITE : MODE_READ;
428             file->file_number = i;
429 
430             break;
431          }
432 
433       }
434 
435       if (i == FTABLE_SIZE)
436       {
437          log_error_printf("KMSAgent_direct::fopen FN=%s no free files \n",
438                           filename);
439          /* no free entries - bail */
440          return NULL;
441       }
442 
443    }
444 
445    /* return pointer to the myFILE struct created */
446    return file;
447 
448 }
449 
450 /**
451  * my_eeprom_fclose()
452  * my filesystem fileclose
453  */
my_eeprom_fclose(myFILE * f)454 extern "C" int  my_eeprom_fclose(myFILE *f)
455 {
456    ECPT_TRACE_ENTRY   *trace = NULL;
457    ECPT_TRACE( trace, my_eeprom_fclose ) ;
458    ecpt_trace_msg( trace, "entered");
459 
460    FATAL_ASSERT(f);
461 
462 #ifdef DEBUG
463    log_printf("KMSAgent_direct::fclose FN=%s, "
464               "FILE %d "
465               "file size is %d \n",
466               ramFileSystem.ftable[f->file_number].fname,
467               f->file_number,
468               ramFileSystem.ftable[f->file_number].fsize);
469 #endif
470 
471    if (fstate[f->file_number] == MYFILE_CLOSED)
472    {
473       log_error_printf("KMSAgent_direct::fclose FN=%s, "
474                        "FILE %d WAS ALREADY CLOSED "
475                        "file size is %d \n",
476                        ramFileSystem.ftable[f->file_number].fname,
477                        f->file_number,
478                        ramFileSystem.ftable[f->file_number].fsize);
479    }
480 
481    /* remember the file is closed */
482    fstate[f->file_number]= MYFILE_CLOSED;
483 
484    /* don't rewrite eeprom unless file was opened for writing */
485    if (f->mode == MODE_WRITE)
486    {
487       /* Erase the sector we are rewriting */
488       EEPROM_Sector_Erase( (unsigned char*)FILESYSTEM_SECTOR ) ;
489 
490       /* remember the version of toolkit that wrote this filesystem
491          (for any future compatibility problems). */
492       strncpy(ramFileSystem.writingversion,
493               KMSAgent_Version,
494               sizeof(KMSAgent_Version));
495 
496       /* flush the memory indicated by myFILE * out to EEPROM */
497       /* see boot_eeprom.c for details on Flash_Write */
498       Flash_Write ((unsigned char*)FILESYSTEM_SECTOR,
499                    (unsigned char*)&ramFileSystem,
500                    FILESYSTEM_SECTOR_SIZE);
501    }
502 
503    /* delete the placeholder file */
504    free(f);
505 
506    return 0;
507 }
508 
509 /**
510  * my_eeprom_fputs()
511  * my filesystem put string
512  */
my_eeprom_fputs(const char * s,myFILE * f)513 extern "C" int  my_eeprom_fputs(const char *s, myFILE *f)
514 {
515    /* put the string to the memory indicated by myFILE */
516    int i;
517    ECPT_TRACE_ENTRY   *trace = NULL;
518    ECPT_TRACE( trace, my_eeprom_fputs ) ;
519 
520    ecpt_trace_msg (trace, "\nmy_eeprom_fputs : Entered");
521 
522 #ifdef DEBUG
523    log_printf(
524       "\nKMSAgent_direct::my_eeprom_fputs "
525       "f->writeptr =%x "
526       "f->readptr=%x "
527       "f->readsize=%x "
528       "f->memptr=%x"
529       "f->memptr->fsize=%x\n",
530       f->writeptr,
531       f->readptr,
532       f->readsize,
533       f->memptr,
534       f->memptr->fsize);
535 
536 #endif
537 
538    FATAL_ASSERT(s);
539    FATAL_ASSERT(f);
540 
541    for (i=0;;i++)
542    {
543       /* end of input string? quit */
544       if (*s == '\0')
545          break;
546 
547       /* end of max file memory, quit */
548       if ((unsigned char*)s >=
549           (unsigned char*)(f->memptr + sizeof(struct eepromBlockStruct)))
550          break;
551 
552       /* copy */
553       *(f->writeptr) = *s++;
554       f->writeptr++;
555 
556       /* increment the filesize */
557       f->memptr->fsize += 1;
558 
559    }
560 
561 #ifdef DEBUG
562    log_printf(
563            "\nKMSAgent_direct::fputs wrote %d chars, "
564            "filesize is %d\n",
565            i,
566            f->memptr->fsize );
567 #endif
568 
569    /* return how many chars were written */
570    return i;
571 }
572 
573 
574 /**
575  * my_eeprom_fwrite()
576  * my filesystem put string
577  */
my_eeprom_fwrite(const char * s,int size,int nobj,myFILE * f)578 extern "C" int  my_eeprom_fwrite(const char *s,
579                                  int size,
580                                  int nobj,
581                                  myFILE *f)
582 {
583    /* put the string to the memory indicated by myFILE */
584    int i,j;
585 
586    ECPT_TRACE_ENTRY   *trace = NULL;
587    ECPT_TRACE( trace, my_eeprom_fwrite ) ;
588    ecpt_trace_msg ( trace, "Entered");
589 
590    log_printf(
591            "\nKMSAgent_direct::my_eeprom_fputs "
592            "f->writeptr =%x "
593            "f->readptr=%x "
594            "f->readsize=%x "
595            "f->memptr=%x"
596            "f->memptr->fsize=%x\n",
597            f->writeptr,
598            f->readptr,
599            f->readsize,
600            f->memptr,
601            f->memptr->fsize);
602 
603    FATAL_ASSERT(s);
604    FATAL_ASSERT(f);
605    FATAL_ASSERT((size*nobj) > 0);
606 
607    for (i=0; i<nobj; i++)
608    {
609       for (j=0; j<size; j++)
610       {
611          /* end of max file memory, quit */
612          if ((unsigned char*)s >=
613              (unsigned char*)(f->memptr + sizeof(struct eepromBlockStruct)))
614             goto DONE;
615 
616          /* copy */
617          *(f->writeptr) = *s++;
618          f->writeptr++;
619 
620          /* increment the filesize */
621          f->memptr->fsize += 1;
622       }
623 
624    }
625 
626   DONE:
627 
628 
629 #ifdef DEBUG
630    log_printf(
631            "\nKMSAgent_direct::fwrite wrote %d chars, "
632            "filesize is %d\n",
633            i,
634            f->memptr->fsize );
635 #endif
636 
637    /* return how many objects were written */
638    return i;
639 }
640 
641 
642 
643 /**
644  * my_eeprom_fgets()
645  * my filesystem getstring
646  */
my_eeprom_fgets(char * s,int n,myFILE * f)647 extern "C" char *my_eeprom_fgets(char *s, int n, myFILE *f)
648 {
649    /* put the string to the memory indicated by myFILE */
650    int i;
651    char *tmp = s;
652 
653    ECPT_TRACE_ENTRY   *trace = NULL;
654    ECPT_TRACE( trace, my_eeprom_fgets ) ;
655    ecpt_trace_msg (trace, "entered");
656 
657    FATAL_ASSERT(s);
658    FATAL_ASSERT(n > 0);
659    FATAL_ASSERT(f);
660 
661 #ifdef DEBUG
662    log_printf(
663            "\nKMSAgent_direct::my_eeprom_fgets from file %s "
664            "fileptr %x "
665            "readptr = %x "
666            "readsize = %d "
667            "filesize = %d \n",
668            f->memptr->fname,
669            f,
670            f->readptr,
671            f->readsize,
672            f->memptr->fsize);
673 #endif
674 
675    /* get up to n-1 (and a \0) chars */
676    for (i=0; i < n; i++)
677    {
678       /* end of read line? quit */
679       if (*f->readptr == '\n')
680       {
681          /* increment the number of bytes read */
682          f->readptr++;
683          f->readsize++;
684          break;
685       }
686 
687       /* trying to read beyond what was written? quit */
688       if (f->readsize >
689           f->memptr->fsize)
690       {
691 #ifdef DEBUG
692          log_printf(
693                   "\nKMSAgent_direct::fgets eof after %d chars, "
694                   "readsize is %d "
695                   "filesize is %d\n",
696                   i,
697                   f->readsize,
698                   f->memptr->fsize);
699 #endif
700          return NULL;
701       }
702 
703       /* trying to read beyond max file memory, quit */
704       if ((unsigned char*)f->readptr >=
705           (unsigned char*)(f->memptr + sizeof(struct eepromBlockStruct)))
706       {
707 #ifdef DEBUG
708          log_printf(
709                  "\nKMSAgent_direct::fgets end of max file after %d chars "
710                  "readsize is %d "
711                  "filesize is %d\n",
712                  i,
713                  f->readsize,
714                  f->memptr->fsize);
715 #endif
716          /* EOF or error is signalled by NULL return */
717          return NULL;
718       }
719 
720       /* copy a char */
721       *tmp++ = *f->readptr;
722 
723       /* increment the number of bytes read */
724       f->readptr++;
725       f->readsize++;
726 
727    } /* for */
728 
729    /* terminate the returned string with a 0, but not a \n */
730    *tmp='\0';
731 
732 #ifdef DEBUG
733    /* terminate returned string after a non-absurd number of chars
734     */
735    log_printf(
736            "\nKMSAgent_direct::fgets string [%s] "
737            "end of line after %d chars "
738            "readsize is %d "
739            "filesize is %d\n",
740            s,
741            i,
742            f->readsize,
743            f->memptr->fsize);
744 #endif
745 
746    /* on success, return the original pointer */
747    return s;
748 }
749 
750 
751 
752 
753 /**
754  * my_eeprom_fread()
755  * my filesystem read a file
756  */
my_eeprom_fread(char * s,int size,int nobj,myFILE * f)757 extern "C" int my_eeprom_fread(char *s, int size, int nobj, myFILE *f)
758 {
759    /* put the string to the memory indicated by myFILE */
760    int i,j;
761 
762    ECPT_TRACE_ENTRY   *trace = NULL;
763    ECPT_TRACE( trace, my_eeprom_fread ) ;
764    ecpt_trace_msg (trace, "entered");
765 
766    FATAL_ASSERT(s);
767    FATAL_ASSERT(f);
768    FATAL_ASSERT((size*nobj) > 0);
769 
770    for (i=0; i<nobj; i++)
771    {
772       for (j=0; j<size; j++)
773       {
774          /* end of written file memory, quit */
775          if ( (unsigned char*) s >=
776               (unsigned char*) (f->memptr + f->memptr->fsize) )
777             goto DONE;
778 
779          /* copy */
780          *s++ = *(f->readptr);
781          f->readptr++;
782       }
783 
784    }
785 
786   DONE:
787 
788 #ifdef DEBUG
789    log_printf(
790            "\nKMSAgent_direct::fread read %d chars, "
791            "filesize is %d\n",
792            i,
793            f->memptr->fsize );
794 #endif
795 
796    /* return how many objects were written */
797    return i;
798 
799 }
800 
801 
802 
803 
804 
805 
806 /**
807  * my_eeprom_unlink() - delete the file named
808  */
my_eeprom_unlink(const char * filename)809 extern "C" int my_eeprom_unlink(const char *filename)
810 {
811    int i;
812 
813    ECPT_TRACE_ENTRY   *trace = NULL;
814    ECPT_TRACE( trace, my_eeprom_fgets ) ;
815    ecpt_trace_msg (trace, "Entered fn=%s", (char*)filename);
816 
817    /* assume the file is closed */
818    FATAL_ASSERT(filename);
819 
820    /* read the file table */
821    for (i=0; i < FTABLE_SIZE; i++)
822    {
823 
824       if (fstate[i] != MYFILE_CLOSED)
825       {
826          log_error_printf("KMSAgent_direct::unlink FN=%s file matches, "
827                           "FILE %d WAS STILL OPEN for %s"
828                           "file size is %d \n",
829                           filename,
830                           i,
831                           (fstate[i] == MYFILE_OPEN_WRITE ? "WRITE": "READ"),
832                           ramFileSystem.ftable[i].fsize);
833       }
834 
835       /* compare input filename to ram copy filename */
836       if (strcmp(filename, (const char*)ramFileSystem.ftable[i].fname) == 0)
837       {
838          /* if filename matches, zero it out to delete the file */
839          ramFileSystem.ftable[i].fname[0] = '\0';
840          ramFileSystem.ftable[i].fsize    = 0;
841          ramFileSystem.ftable[i].fdata[0] = 1;
842 
843          /* remember the file is closed */
844          fstate[i]= MYFILE_CLOSED;
845 
846          EEPROM_Sector_Erase( FILESYSTEM_SECTOR ) ;
847 
848          /* remember the version of toolkit that wrote this filesystem
849             (for any future compatibility problems). */
850          strncpy(ramFileSystem.writingversion,
851                  KMSAgent_Version,
852                  sizeof(KMSAgent_Version));
853 
854          /* flush the memory indicated by myFILE * out to EEPROM */
855          Flash_Write ((unsigned char*)FILESYSTEM_SECTOR,
856                       (unsigned char*)&ramFileSystem,
857                       FILESYSTEM_SECTOR_SIZE);
858 
859          break;
860       }
861 
862    }
863 
864 #ifdef DEBUG
865    log_printf ("my_eeprom_unlink : returning");
866 #endif
867 
868    if (i == FTABLE_SIZE)
869       return -1;
870 
871    /* success */
872    return 0;
873 }
874 
875 
876 /**
877  * my_eeprom_fsys_erase() - delete all files
878  */
my_eeprom_fsys_erase(void)879 extern "C" int my_eeprom_fsys_erase(void)
880 {
881    int i;
882 
883    ECPT_TRACE_ENTRY   *trace = NULL;
884    ECPT_TRACE( trace, my_eeprom_fread ) ;
885    ecpt_trace_msg (trace, "my_eeprom_fsys_erase : entering ");
886 
887    /* read the file table */
888    for (i=0; i < FTABLE_SIZE; i++)
889    {
890       if (fstate[i] != MYFILE_CLOSED)
891       {
892          log_error_printf("KMSAgent_direct::fsys_erase FN=%s file "
893                           "FILE %d WAS STILL OPEN for %s "
894                           "file size is %d \n",
895                           ramFileSystem.ftable[i].fname,
896                           i,
897                           (fstate[i] == MYFILE_OPEN_WRITE ? "WRITE": "READ"),
898                           ramFileSystem.ftable[i].fsize);
899       }
900 
901       /* zero filename out to delete the file */
902       ramFileSystem.ftable[i].fname[0] = '\0';
903       ramFileSystem.ftable[i].fsize    = 0;
904       ramFileSystem.ftable[i].fdata[0] = 2;
905 
906       /* remember the file is closed */
907       fstate[i]= MYFILE_CLOSED;
908 
909       EEPROM_Sector_Erase( FILESYSTEM_SECTOR ) ;
910 
911       /* remember the version of toolkit that wrote this filesystem
912          (for any future compatibility problems). */
913       strncpy(ramFileSystem.writingversion,
914               KMSAgent_Version,
915               sizeof(KMSAgent_Version));
916 
917       /* flush the memory indicated by myFILE * out to EEPROM */
918       Flash_Write ((unsigned char*)FILESYSTEM_SECTOR,
919                    (unsigned char*)&ramFileSystem,
920                    FILESYSTEM_SECTOR_SIZE);
921 
922 
923    }
924 
925 #ifdef DEBUG
926    log_printf ("\nmy_eeprom_fsys_erase : returning");
927 #endif
928 
929    if (i == FTABLE_SIZE)
930       return -1;
931 
932    /* success */
933    return 0;
934 }
935 
936 
937 
938 
939 #endif  // DEFINE_STORAGE
940 #endif  // METAWARE
941 #endif
942 
943