summaryrefslogtreecommitdiff
path: root/ecos/packages/fs/rom/current/src/romfs.c
blob: b748200b9605a2df12a97fc01b097a1bf8fa977f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
//==========================================================================
//
//      romfs.c
//
//      ROM file system
//
//==========================================================================
// ####ECOSGPLCOPYRIGHTBEGIN####                                            
// -------------------------------------------                              
// This file is part of eCos, the Embedded Configurable Operating System.   
// Copyright (C) 1998, 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
//
// eCos is free software; you can redistribute it and/or modify it under    
// the terms of the GNU General Public License as published by the Free     
// Software Foundation; either version 2 or (at your option) any later      
// version.                                                                 
//
// eCos is distributed in the hope that it will be useful, but WITHOUT      
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or    
// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License    
// for more details.                                                        
//
// You should have received a copy of the GNU General Public License        
// along with eCos; if not, write to the Free Software Foundation, Inc.,    
// 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.            
//
// As a special exception, if other files instantiate templates or use      
// macros or inline functions from this file, or you compile this file      
// and link it with other works to produce a work based on this file,       
// this file does not by itself cause the resulting work to be covered by   
// the GNU General Public License. However the source code for this file    
// must still be made available in accordance with section (3) of the GNU   
// General Public License v2.                                               
//
// This exception does not invalidate any other reasons why a work based    
// on this file might be covered by the GNU General Public License.         
// -------------------------------------------                              
// ####ECOSGPLCOPYRIGHTEND####                                              
//==========================================================================
//#####DESCRIPTIONBEGIN####
//
// Author(s):           nickg
// Contributors:        nickg, richard.panton@3glab.com
// Date:                2000-07-25
// Purpose:             ROM file system
// Description:         This is a ROM filesystem for eCos. It attempts to
//                      provide full POSIX-compatible filesystem behaviour
//                      while at the same time being efficient in terms of
//                      time and space used.
//                      
//
//####DESCRIPTIONEND####
//
//==========================================================================
// 
// General Description
// ===================
// 
// This is an implementation of a ROM filesystem for eCos. Its goal is
// to provide a working example of a filesystem that provides most of
// the required POSIX functionality. And obviously it may also be
// useful in its own right.
//
//
// Header
// ------
//
// There is a single header that describes the overall format of the ROMFS
// disk. The address of this header is used as the base for all offsets used
// in the node and directory structures. It contains the following fields:
//
// label  - A human readable label for various purposes
// fssize - The size in bytes of the entire ROMFS disk
// nodes  - A count of the nodes in the disk
//
// Immediately following thisin memory is the node table, consisting of
// 'nodes' repetitions of the node object.
//
// Nodes
// -----
//
// All files and directories are represented by node objects. Each
// romfs_node structure contains the following fields:
//
// mode   - Node type, file or directory.
// nlink  - Number of links to this node. Each directory entry that references
//          this node is a link. 
// size   - Size of the data in this node in bytes.
// ctime  - Creation time of the file (NOT the ROMFS)
// data   - Offset of the first data byte for this node from the header
//
// Directories
// -----------
//
// A directory is a node whose data is a list of directory entries.
// These contain the
// following fields:
//
// node    - Index of the node in the romfs_disk table that is referenced by
//           this entry. This is present in every directory entry fragment.
// next    - Offset of the next name entry.
// name    - The filename associated with this link to the node.
//
// Data Storage
// ------------
//
// Each file has its data stored in a single contiguous memory block
// referenced by the offset in the node.
//
//==========================================================================

#include <pkgconf/system.h>
#include <pkgconf/hal.h>
#include <pkgconf/kernel.h>
#include <pkgconf/io_fileio.h>
#include <pkgconf/fs_rom.h>

#include <cyg/kernel/ktypes.h>         // base kernel types
#include <cyg/infra/cyg_trac.h>        // tracing macros
#include <cyg/infra/cyg_ass.h>         // assertion macros

#include <unistd.h>
#include <sys/types.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <errno.h>
#include <dirent.h>

#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <cyg/fileio/fileio.h>

#include <cyg/kernel/kapi.h>
#include <cyg/infra/diag.h>

#if defined(CYGFUN_FS_ROM_FLASH_BLOCK_DEVICE_LOOKUP)
#include <cyg/io/io.h>
#include <cyg/io/config_keys.h>
#include <cyg/io/flash.h>
#endif

//==========================================================================
// Eventually we want to eXecute In Place from the ROM in a protected
// environment, so we'll need executables to be aligned to a boundary
// suitable for MMU protection. A suitable boundary would be the 4k
// boundary in all the CPU architectures I am currently aware of.

// Forward definitions

// Filesystem operations
static int romfs_mount    ( cyg_fstab_entry *fste, cyg_mtab_entry *mte );
static int romfs_umount   ( cyg_mtab_entry *mte );
static int romfs_open     ( cyg_mtab_entry *mte, cyg_dir dir, const char *name,
                             int mode,  cyg_file *fte );
static int romfs_opendir  ( cyg_mtab_entry *mte, cyg_dir dir, const char *name,
                             cyg_file *fte );
static int romfs_chdir    ( cyg_mtab_entry *mte, cyg_dir dir, const char *name,
                             cyg_dir *dir_out );
static int romfs_stat     ( cyg_mtab_entry *mte, cyg_dir dir, const char *name,
                             struct stat *buf);
static int romfs_getinfo  ( cyg_mtab_entry *mte, cyg_dir dir, const char *name,
                             int key, void *buf, int len );
static int romfs_setinfo  ( cyg_mtab_entry *mte, cyg_dir dir, const char *name,
                             int key, void *buf, int len );

// File operations
static int romfs_fo_read      (struct CYG_FILE_TAG *fp, struct CYG_UIO_TAG *uio);
static int romfs_fo_lseek     (struct CYG_FILE_TAG *fp, off_t *pos, int whence );
static int romfs_fo_ioctl     (struct CYG_FILE_TAG *fp, CYG_ADDRWORD com,
                                CYG_ADDRWORD data);
static int romfs_fo_fsync     (struct CYG_FILE_TAG *fp, int mode );        
static int romfs_fo_close     (struct CYG_FILE_TAG *fp);
static int romfs_fo_fstat     (struct CYG_FILE_TAG *fp, struct stat *buf );
static int romfs_fo_getinfo   (struct CYG_FILE_TAG *fp, int key, void *buf, int len );
static int romfs_fo_setinfo   (struct CYG_FILE_TAG *fp, int key, void *buf, int len );

// Directory operations
static int romfs_fo_dirread      (struct CYG_FILE_TAG *fp, struct CYG_UIO_TAG *uio);
static int romfs_fo_dirlseek     (struct CYG_FILE_TAG *fp, off_t *pos, int whence );


//==========================================================================
// Filesystem table entries

// -------------------------------------------------------------------------
// Fstab entry.
// This defines the entry in the filesystem table.
// For simplicity we use _FILESYSTEM synchronization for all accesses since
// we should never block in any filesystem operations.

FSTAB_ENTRY( romfs_fste, "romfs", 0,
             CYG_SYNCMODE_FILE_FILESYSTEM|CYG_SYNCMODE_IO_FILESYSTEM,
             romfs_mount,
             romfs_umount,
             romfs_open,
             (cyg_fsop_unlink *)cyg_fileio_erofs,
             (cyg_fsop_mkdir *)cyg_fileio_erofs,
             (cyg_fsop_rmdir *)cyg_fileio_erofs,
             (cyg_fsop_rename *)cyg_fileio_erofs,
             (cyg_fsop_link *)cyg_fileio_erofs,
             romfs_opendir,
             romfs_chdir,
             romfs_stat,
             romfs_getinfo,
             romfs_setinfo);

// -------------------------------------------------------------------------
// mtab entry.
// This defines a single ROMFS loaded into ROM at the configured address
//
// MTAB_ENTRY(	rom_mte,	// structure name
// 		"/rom",		// mount point
// 		"romfs",	// FIlesystem type
// 		"",		// hardware device
//  (CYG_ADDRWORD) CYGNUM_FS_ROM_BASE_ADDRESS	// Address in ROM
//           );


// -------------------------------------------------------------------------
// File operations.
// This set of file operations are used for normal open files.

static cyg_fileops romfs_fileops =
{
    romfs_fo_read,
    (cyg_fileop_write *)cyg_fileio_erofs,
    romfs_fo_lseek,
    romfs_fo_ioctl,
    cyg_fileio_seltrue,
    romfs_fo_fsync,
    romfs_fo_close,
    romfs_fo_fstat,
    romfs_fo_getinfo,
    romfs_fo_setinfo
};

// -------------------------------------------------------------------------
// Directory file operations.
// This set of operations are used for open directories. Most entries
// point to error-returning stub functions. Only the read, lseek and
// close entries are functional.

static cyg_fileops romfs_dirops =
{
    romfs_fo_dirread,
    (cyg_fileop_write *)cyg_fileio_enosys,
    romfs_fo_dirlseek,
    (cyg_fileop_ioctl *)cyg_fileio_enosys,
    cyg_fileio_seltrue,
    (cyg_fileop_fsync *)cyg_fileio_enosys,
    romfs_fo_close,
    (cyg_fileop_fstat *)cyg_fileio_enosys,
    (cyg_fileop_getinfo *)cyg_fileio_enosys,
    (cyg_fileop_setinfo *)cyg_fileio_enosys
};

//==========================================================================
// Data typedefs
// Some forward typedefs for the main data structures.

struct romfs_disk;
typedef struct romfs_disk romfs_disk;

struct romfs_node;
typedef struct romfs_node romfs_node;

struct romfs_dirent;
typedef struct romfs_dirent romfs_dirent;

//==========================================================================
// File and directory node
// This data structure represents a file or directory. 

struct romfs_node
{
    cyg_uint32          mode;           // 0-3   node type
    cyg_ucount32        nlink;          // 4-7   number of links to this node
    cyg_uint16		uid;		// 8-9   Owner id
    cyg_uint16		gid;		// 10-11 Group id
    cyg_uint32          size;           // 12-15 size of file in bytes
    cyg_uint32          ctime;          // 16-19 creation status time
    cyg_uint32		offset;		// 20-23 offset of data from start of ROMFS
    cyg_uint32		pad[2];		// 24-31 padding to align to 32byte boundary
};

//==========================================================================
// Directory entry.
// Variable sized entry containing the name and node of a directory entry

struct romfs_dirent
{
    cyg_ucount32        node;           // Index of node in romfs_disk structure
    cyg_uint32		next;		// Offset from start of directory of
					// a) the next entry, or 
    					// b) the end of the directory data
    char                name[0];	// The name, NUL terminated
};

//==========================================================================
// ROMFS header
// This data structure contains overall information on the ROMFS

struct romfs_disk
{
    cyg_uint32		magic;		// 0-3   Marks a valid ROMFS entry
    cyg_ucount32	nodecount;	// 4-7   Count of nodes in this filesystem
    cyg_ucount32	disksize;	// 8-11  Count of bytes in this filesystem
    cyg_uint32		dev_id;		// 12-15 ID of disk (put into stat.st_dev)
    char		name[16];	// 16-31 Name - pads to 32 bytes
    romfs_node		node[0];
};

#define ROMFS_MAGIC	0x526f6d2e	// The magic signature word for a romfs
#define ROMFS_CIGAM	0x2e6d6f52	// The byte sex is wrong if you see this

//==========================================================================
// Directory search data
// Parameters for a directory search. The fields of this structure are
// updated as we follow a pathname through the directory tree.

struct romfs_dirsearch
{
    romfs_disk		*disk;		// disk structure
    romfs_node          *dir;           // directory to search
    const char          *path;          // path to follow
    romfs_node          *node;          // Node found
    const char          *name;          // last name used
    int			namelen;	// name fragment length
    cyg_bool		last;		// last name in path?
};

typedef struct romfs_dirsearch romfs_dirsearch;

//==========================================================================
// This seems to be the only string function referenced. Define as static
// here to avoid having to load the string library

static bool match( const char *a, const char *b, int len )
{
    for ( ; len > 0 && *a && *b && *a == *b ; a++, b++, len-- )
	;
    return ( len == 0 && *a == 0 );
}

		
//==========================================================================
// SIMPLE buffer management.
// Each node has a data buffer pointer and a size.

// -------------------------------------------------------------------------
// findbuffer_node()
// return a pointer to the data at the indicated file position.

static int findbuffer_node( romfs_disk *disk,	// header pointer
			    romfs_node  *node,  // node pointer
                            off_t pos,          // data position to get
                            cyg_uint8 **buffer, // returned buffer pointer
                            size_t *size)       // returned buffer size
{
    if ( pos >= node->size || node->size == 0 )
    {
        // Indicate end of data.
        *size = 0;
    } else {

	// Calculate the buffer position
	*buffer = (cyg_uint8*)disk + node->offset + pos;
	*size = node->size-pos;
    }

    return ENOERR;
}

//==========================================================================
// Directory operations


// -------------------------------------------------------------------------
// find_direntry()
// Find a directory entry for the name and return a pointer to the first
// entry fragment.

static romfs_dirent *find_direntry( romfs_disk *disk, romfs_node *dir, const char *name, int namelen )
{
    off_t pos = 0;
    int err;

    // Loop over all the entries until a match is found or we run out
    // of data.
    while( pos < dir->size )
    {
        romfs_dirent *d;
        cyg_uint8 *buf;
        size_t size;
        
	err = findbuffer_node( disk, dir, pos, &buf, &size );
	if( err != ENOERR || size == 0)
	    return NULL;

	d = (romfs_dirent *)buf;

	// Is this the directory entry we're looking for?
	if ( match( d->name, name, namelen ) )
	    return d;

	// Otherwise move on to next entry in chain
        pos = d->next;
    }

    return NULL;
}

//==========================================================================
// Directory search

// -------------------------------------------------------------------------
// init_dirsearch()
// Initialize a dirsearch object to start a search

static void init_dirsearch( romfs_dirsearch *ds,
                            romfs_disk *disk,
			    romfs_node *dir,
                            const char *name)
{
    ds->disk     = disk;
    ds->dir      = dir;
    ds->path     = name;
    ds->node     = dir;
    ds->name     = name;
    ds->namelen  = 0;
    ds->last	 = false;
}

// -------------------------------------------------------------------------
// find_entry()
// Search a single directory for the next name in a path and update the
// dirsearch object appropriately.

static int find_entry( romfs_dirsearch *ds )
{
    romfs_node *dir = ds->dir;
    const char *name = ds->path;
    const char *n = name;
    int namelen = 0;
    romfs_dirent *d;
    
    // check that we really have a directory
    if( !S_ISDIR(dir->mode) )
        return ENOTDIR;

    // Isolate the next element of the path name. 
    while( *n != '\0' && *n != '/' )
        n++, namelen++;

    // Check if this is the last path element.
    while( *n == '/') n++;
    if( *n == '\0' ) 
        ds->last = true;

    // update name in dirsearch object
    ds->name = name;
    ds->namelen = namelen;
    
    // Here we have the name and its length set up.
    // Search the directory for a matching entry

    d = find_direntry( ds->disk, dir, name, namelen );

    if( d == NULL )
        return ENOENT;

    // pass back the node we have found
    ds->node = &ds->disk->node[d->node];

    return ENOERR;

}

// -------------------------------------------------------------------------
// romfs_find()
// Main interface to directory search code. This is used in all file
// level operations to locate the object named by the pathname.

static int romfs_find( romfs_dirsearch *d )
{
    int err;

    // Short circuit empty paths
    if( *(d->path) == '\0' )
        return ENOERR;

    // iterate down directory tree until we find the object
    // we want.
    for(;;)
    {
        err = find_entry( d );

        if( err != ENOERR )
            return err;

        if( d->last )
            return ENOERR;

        // Update dirsearch object to search next directory.
        d->dir = d->node;
        d->path += d->namelen;
        while( *(d->path) == '/' ) d->path++; // skip dirname separators
    }
}

//==========================================================================
// Pathconf support
// This function provides support for pathconf() and fpathconf().

static int romfs_pathconf( romfs_node *node, struct cyg_pathconf_info *info )
{
    int err = ENOERR;
    
    switch( info->name )
    {
    case _PC_LINK_MAX:
        info->value = LINK_MAX;
        break;
        
    case _PC_MAX_CANON:
        info->value = -1;       // not supported
        err = EINVAL;
        break;
        
    case _PC_MAX_INPUT:
        info->value = -1;       // not supported
        err = EINVAL;        
        break;
        
    case _PC_NAME_MAX:
        info->value = NAME_MAX;
        break;
        
    case _PC_PATH_MAX:
        info->value = PATH_MAX;
        break;        

    case _PC_PIPE_BUF:
        info->value = -1;       // not supported
        err = EINVAL;        
        break;        

        
    case _PC_ASYNC_IO:
        info->value = -1;       // not supported
        err = EINVAL;        
        break;
        
    case _PC_CHOWN_RESTRICTED:
        info->value = -1;       // not supported
        err = EINVAL;
        break;
        
    case _PC_NO_TRUNC:
        info->value = 0;
        break;
        
    case _PC_PRIO_IO:
        info->value = 0;
        break;
        
    case _PC_SYNC_IO:
        info->value = 0;
        break;
        
    case _PC_VDISABLE:
        info->value = -1;       // not supported
        err = EINVAL;        
        break;
        
    default:
        err = EINVAL;
        break;
    }

    return err;
}

//==========================================================================
// Filesystem operations

// -------------------------------------------------------------------------
// romfs_mount()
// Process a mount request. This mainly finds root for the
// filesystem.

static int romfs_mount    ( cyg_fstab_entry *fste, cyg_mtab_entry *mte )
{
    romfs_disk *disk=NULL;
    
    if ( !mte->data ) {
	// If the image address was not in the MTE data word,
	if ( mte->devname && mte->devname[0] ) {
	    // If a device name specified, lookup flash block device.
#if defined(CYGFUN_FS_ROM_FLASH_BLOCK_DEVICE_LOOKUP)
	    if ( mte->devname[0] == '/' ) {
		Cyg_ErrNo err;
		cyg_io_handle_t t;
		cyg_io_flash_getconfig_devaddr_t d;
		int len;
		err = cyg_io_lookup(mte->devname, &t);
		if (err != ENOERR) {
		    return -err;
		}
		len = sizeof(d);
		err = cyg_io_get_config(t, CYG_IO_GET_CONFIG_FLASH_DEVADDR, &d, &len);
		if (err != ENOERR) {
		    return -err;
		}
		disk = (romfs_disk *) d.dev_addr;
	    } else
#endif
	    {
		char *addr;
		// And there's something in the 'hardware device' field,
		// then read the address from there.
		sscanf( mte->devname, "%p", &addr );
		disk = (romfs_disk *) addr;
	    }
        }
    } else {
        disk = (romfs_disk *)mte->data;
    }

    if ( !disk ) {
	// If still no address, try the FSTAB entry data word
	disk = (romfs_disk *)fste->data;
    }

    if ( !disk ) {
	// If still no address, give up...
	return ENOENT;
    }


    
    // Check the ROMFS magic number to ensure that there's really an fs.
    if ( disk->magic == ROMFS_CIGAM ) {
	// The disk image has the wrong byte sex!!!
	return EIO;
    } else if ( disk->magic != ROMFS_MAGIC || disk->nodecount == 0 ) {
	// No image found
	return ENOENT;
    }

    mte->root = (cyg_dir)&disk->node[0];
    
    mte->data = (CYG_ADDRWORD)disk;
    return ENOERR;
}

// -------------------------------------------------------------------------
// romfs_umount()
// Unmount the filesystem. This will currently always succeed.

static int romfs_umount   ( cyg_mtab_entry *mte )
{
    // Clear root pointer
    mte->root = CYG_DIR_NULL;
    
    // That's all folks.
        
    return ENOERR;
}

// -------------------------------------------------------------------------
// romfs_open()
// Open a file for reading

static int romfs_open     ( cyg_mtab_entry *mte, cyg_dir dir, const char *name,
                             int mode,  cyg_file *file )
{

    romfs_dirsearch ds;
    romfs_node *node = NULL;
    int err;

    init_dirsearch( &ds, (romfs_disk *)mte->data, (romfs_node *)dir, name );
    
    err = romfs_find( &ds );

    if( err == ENOENT )
    {
	return ENOENT;
    }
    else if( err == ENOERR )
    {
        // The node exists. If the O_CREAT and O_EXCL bits are set, we
        // must fail the open.
        
        if( (mode & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL) )
            err = EEXIST;
        else node = ds.node;
    }
    
    if( err == ENOERR && (mode & O_TRUNC ) )
    {
        // If the O_TRUNC bit is set we must fail the open

        err = EPERM;
    }

    if( err != ENOERR ) return err;

    // Check that we actually have a file here
    if( S_ISDIR(node->mode) ) return EISDIR;

    // Initialize the file object
    
    file->f_flag        |= mode & CYG_FILE_MODE_MASK;
    file->f_type        = CYG_FILE_TYPE_FILE;
    file->f_ops         = &romfs_fileops;
    file->f_offset      = 0;
    file->f_data        = (CYG_ADDRWORD)node;
    file->f_xops        = 0;

    return ENOERR;
}

// -------------------------------------------------------------------------
// romfs_opendir()
// Open a directory for reading.

static int romfs_opendir  ( cyg_mtab_entry *mte, cyg_dir dir, const char *name,
                             cyg_file *file )
{
    romfs_dirsearch ds;
    int err;

    init_dirsearch( &ds, (romfs_disk *)mte->data, (romfs_node *)dir, name );
    
    err = romfs_find( &ds );

    if( err != ENOERR ) return err;

    // check it is really a directory.
    if( !S_ISDIR(ds.node->mode) ) return ENOTDIR;

    // Initialize the file object, setting the f_ops field to a
    // special set of file ops.
    
    file->f_type        = CYG_FILE_TYPE_FILE;
    file->f_ops         = &romfs_dirops;
    file->f_offset      = 0;
    file->f_data        = (CYG_ADDRWORD)ds.node;
    file->f_xops        = 0;
        
    return ENOERR;

}

// -------------------------------------------------------------------------
// romfs_chdir()
// Change directory support.

static int romfs_chdir    ( cyg_mtab_entry *mte, cyg_dir dir, const char *name,
                             cyg_dir *dir_out )
{
    if( dir_out != NULL )
    {
        // This is a request to get a new directory pointer in
        // *dir_out.

        romfs_dirsearch ds;
        int err;
    
        init_dirsearch( &ds, (romfs_disk *)mte->data, (romfs_node *)dir, name );
    
        err = romfs_find( &ds );

        if( err != ENOERR ) return err;

        // check it is a directory
        if( !S_ISDIR(ds.node->mode) )
            return ENOTDIR;
        
        // Pass it out
        *dir_out = (cyg_dir)ds.node;
    }
    // If no output dir is required, this means that the mte and
    // dir arguments are the current cdir setting and we should
    // forget this fact. Do nothing in ROMFS.
        
    return ENOERR;
}

// -------------------------------------------------------------------------
// romfs_stat()
// Get struct stat info for named object.

static int romfs_stat     ( cyg_mtab_entry *mte, cyg_dir dir, const char *name,
                             struct stat *buf)
{
    romfs_dirsearch ds;
    int err;
    romfs_disk *disk = (romfs_disk *)mte->data;

    init_dirsearch( &ds, disk, (romfs_node *)dir, name );
    
    err = romfs_find( &ds );

    if( err != ENOERR ) return err;

    // Fill in the status
    buf->st_mode        = ds.node->mode;
    buf->st_ino         = (ino_t)(ds.node - &disk->node[0]);
    buf->st_dev         = (dev_t)disk->dev_id;
    buf->st_nlink       = ds.node->nlink;
    buf->st_uid         = ds.node->uid;
    buf->st_gid         = ds.node->gid;
    buf->st_size        = ds.node->size;
    buf->st_atime       = ds.node->ctime;
    buf->st_mtime       = ds.node->ctime;
    buf->st_ctime       = ds.node->ctime;
    
    return err;
}

// -------------------------------------------------------------------------
// romfs_getinfo()
// Getinfo. Currently only support pathconf() and file system block usage

static int romfs_getinfo  ( cyg_mtab_entry *mte, cyg_dir dir, const char *name,
                             int key, void *buf, int len )
{
    romfs_dirsearch ds;
    int err;

    init_dirsearch( &ds, (romfs_disk *)mte->data, (romfs_node *)dir, name );
    
    err = romfs_find( &ds );

    if( err != ENOERR ) return err;

    switch( key )
    {
    case FS_INFO_CONF:
        err = romfs_pathconf( ds.node, (struct cyg_pathconf_info *)buf );
        break;
#if defined(CYGSEM_FILEIO_BLOCK_USAGE)
    case FS_INFO_BLOCK_USAGE: {
      struct cyg_fs_block_usage *usage = (struct cyg_fs_block_usage *) buf;
      struct romfs_disk *disk = (struct romfs_disk*) mte->data;
      usage->total_blocks = disk->disksize;
      usage->free_blocks = 0;
      usage->block_size = 1;
      return ENOERR;
    }
#endif
    default:
        err = EINVAL;
    }
    return err;
}

// -------------------------------------------------------------------------
// romfs_setinfo()
// Setinfo. Nothing to support here at present.

static int romfs_setinfo  ( cyg_mtab_entry *mte, cyg_dir dir, const char *name,
                             int key, void *buf, int len )
{
    // No setinfo keys supported at present
    
    return EINVAL;
}


//==========================================================================
// File operations

// -------------------------------------------------------------------------
// romfs_fo_read()
// Read data from the file.

static int romfs_fo_read      (struct CYG_FILE_TAG *fp, struct CYG_UIO_TAG *uio)
{
    romfs_node *node = (romfs_node *)fp->f_data;
    int i;
    off_t pos = fp->f_offset;
    ssize_t resid = uio->uio_resid;

    // Loop over the io vectors until there are none left
    for( i = 0; i < uio->uio_iovcnt; i++ )
    {
        cyg_iovec *iov = &uio->uio_iov[i];
        char *buf = (char *)iov->iov_base;
        off_t len = iov->iov_len;

        // Loop over each vector filling it with data from the file.
        while( len > 0 && pos < node->size )
        {
            cyg_uint8 *fbuf;
            size_t bsize;
            off_t l = len;
            int err;

            // Get a pointer to the data at offset _pos_.
            err = findbuffer_node( (romfs_disk *)fp->f_mte->data, node, pos, &fbuf, &bsize );

            if( err != ENOERR )
                return err;

            // adjust size to end of file if necessary
            if( l > node->size-pos )
                l = node->size-pos;
            
            // adjust size to the amount of contiguous data we can see
            // at present.
            if( l > bsize )
                l = bsize;

            // copy data out
            memcpy( buf, fbuf, l );

            // Update working vars
            len -= l;
            buf += l;
            pos += l;
            resid -= l;
        }
    }

    // We successfully read some data
    // Update the file offset and transfer residue.
    
    uio->uio_resid = resid;
    fp->f_offset = pos;
    
    return ENOERR;
}

// -------------------------------------------------------------------------
// romfs_fo_lseek()
// Seek to a new file position.

static int romfs_fo_lseek     (struct CYG_FILE_TAG *fp, off_t *apos, int whence )
{
    romfs_node *node = (romfs_node *)fp->f_data;
    off_t pos = *apos;

    switch( whence )
    {
    case SEEK_SET:
        // Pos is already where we want to be.
        break;

    case SEEK_CUR:
        // Add pos to current offset.
        pos += fp->f_offset;
        break;

    case SEEK_END:
        // Add pos to file size.
        pos += node->size;
        break;

    default:
        return EINVAL;
    }
    
    // Check that pos is still within current file size, or at the
    // very end.
    if( pos < 0 || pos > node->size )
        return EINVAL;

    // All OK, set fp offset and return new position.
    *apos = fp->f_offset = pos;
    
    return ENOERR;
}

// -------------------------------------------------------------------------
// romfs_fo_ioctl()
// Handle ioctls. Currently none are defined.

static int romfs_fo_ioctl     (struct CYG_FILE_TAG *fp, CYG_ADDRWORD com,
                                CYG_ADDRWORD data)
{
    // No Ioctls currenly defined.

    return EINVAL;
}

// -------------------------------------------------------------------------
// romfs_fo_fsync().
// Force the file out to data storage.

static int romfs_fo_fsync     (struct CYG_FILE_TAG *fp, int mode )
{
    // Data is always permanently where it belongs, nothing to do
    // here.
  
    return ENOERR;
}

// -------------------------------------------------------------------------
// romfs_fo_close()
// Close a file. We just clear out the data pointer.

static int romfs_fo_close     (struct CYG_FILE_TAG *fp)
{
    fp->f_data = 0;     // zero data pointer
    
    return ENOERR;
}

// -------------------------------------------------------------------------
//romfs_fo_fstat()
// Get file status.

static int romfs_fo_fstat     (struct CYG_FILE_TAG *fp, struct stat *buf )
{
    romfs_node *node = (romfs_node *)fp->f_data;
    romfs_disk *disk = (romfs_disk*)(fp->f_mte->data);

    // Fill in the status
    buf->st_mode        = node->mode;
    buf->st_ino         = (ino_t)(node - &disk->node[0]);
    buf->st_dev         = disk->dev_id;
    buf->st_nlink       = node->nlink;
    buf->st_uid         = node->uid;
    buf->st_gid         = node->gid;
    buf->st_size        = node->size;
    buf->st_atime       = node->ctime;
    buf->st_mtime       = node->ctime;
    buf->st_ctime       = node->ctime;
    
    return ENOERR;
}

// -------------------------------------------------------------------------
// romfs_fo_getinfo()
// Get info. Currently only supports fpathconf().

static int romfs_fo_getinfo   (struct CYG_FILE_TAG *fp, int key, void *buf, int len )
{
    romfs_node *node = (romfs_node *)fp->f_data;    
    int err;

    switch( key )
    {
    case FS_INFO_CONF:
        err = romfs_pathconf( node, (struct cyg_pathconf_info *)buf );
        break;
        
    default:
        err = EINVAL;
    }
    return err;
}

// -------------------------------------------------------------------------
// romfs_fo_setinfo()
// Set info. Nothing supported here.

static int romfs_fo_setinfo   (struct CYG_FILE_TAG *fp, int key, void *buf, int len )
{
    // No setinfo key supported at present
    
    return ENOERR;
}


//==========================================================================
// Directory operations

// -------------------------------------------------------------------------
// romfs_fo_dirread()
// Read a single directory entry from a file.

static int romfs_fo_dirread      (struct CYG_FILE_TAG *fp, struct CYG_UIO_TAG *uio)
{
    romfs_node *dir = (romfs_node *)fp->f_data;
    off_t pos = fp->f_offset;
    int err = ENOERR;
    struct dirent *ent = (struct dirent *)uio->uio_iov[0].iov_base;
    char *nbuf = ent->d_name;
    int nlen = sizeof(ent->d_name)-1;
    off_t len = uio->uio_iov[0].iov_len;
    romfs_dirent *d = NULL;
    cyg_uint8 *buf;
    size_t size;
    int i;
        
    if( len < sizeof(struct dirent) )
        return EINVAL;    
    
    // Get the next entry
    err = findbuffer_node( (romfs_disk *)fp->f_mte->data, dir, pos, &buf, &size );
    if( err != ENOERR || size == 0 || pos >= dir->size )
	return err;

    d = (romfs_dirent *)buf;

    for ( i = 0 ; i < nlen && d->name[i] ; i++, nbuf++ )
	*nbuf = d->name[i];
#ifdef CYGPKG_FS_ROM_RET_DIRENT_DTYPE
	ent->d_type = (((romfs_disk *)fp->f_mte->data)->node[d->node]).mode;
#endif

    // A successful read. Terminate the entry name with a NUL, set the
    // residue and set the file offset to restart at the next
    // directory entry.
    
    *nbuf = '\0';
    uio->uio_resid -= sizeof(struct dirent);
    fp->f_offset = d->next;
    
    return ENOERR;
}

// -------------------------------------------------------------------------
// romfs_fo_dirlseek()
// Seek directory to start.

static int romfs_fo_dirlseek     (struct CYG_FILE_TAG *fp, off_t *pos, int whence )
{
    // Only allow SEEK_SET to zero
    
    if( whence != SEEK_SET || *pos != 0)
        return EINVAL;

    *pos = fp->f_offset = 0;
    
    return ENOERR;
}

// -------------------------------------------------------------------------
// EOF romfs.c