UbixOS  2.0
fat.c
Go to the documentation of this file.
1 /*****************************************************************************************
2  Copyright (c) 2002-2004 The UbixOS Project
3  All rights reserved.
4 
5  Redistribution and use in source and binary forms, with or without modification, are
6  permitted provided that the following conditions are met:
7 
8  Redistributions of source code must retain the above copyright notice, this list of
9  conditions, the following disclaimer and the list of authors. Redistributions in binary
10  form must reproduce the above copyright notice, this list of conditions, the following
11  disclaimer and the list of authors in the documentation and/or other materials provided
12  with the distribution. Neither the name of the UbixOS Project nor the names of its
13  contributors may be used to endorse or promote products derived from this software
14  without specific prior written permission.
15 
16  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
17  EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
19  THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
21  OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
23  TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 
26  $Id: ubixfs.c 79 2016-01-11 16:21:27Z reddawg $
27 
28  *****************************************************************************************/
29 
30 #include <vfs/vfs.h>
31 #include <ubixos/sched.h>
32 #include <ubixos/kpanic.h>
33 #include <ubixos/exec.h>
34 #include <lib/kmalloc.h>
35 #include <string.h>
36 #include <lib/kprintf.h>
37 #include <assert.h>
38 
39 #include "fat_filelib.h"
40 #include "fat_access.h"
41 
43 struct vfs_mountPoint *_mp;
44 
45 int media_read(unsigned long sector, unsigned char *buffer, unsigned long sector_count) {
46  _mp->device->devInfo->read(_mp->device->devInfo->info, buffer, sector, sector_count);
47 
48  return 1;
49 }
50 
51 int media_write(unsigned long sector, unsigned char *buffer, unsigned long sector_count) {
52  _mp->device->devInfo->write(_mp->device->devInfo->info, buffer, sector, sector_count);
53 
54  return 1;
55 }
56 
57 int fat_initialize(struct vfs_mountPoint *mp) {
58  FL_FILE *file;
59  _mp = mp;
60  // Attach media access functions to library
62  kprintf("ERROR: Media attach failed\n");
63  return (0);
64  }
65 
66  // List root directory
67  //fl_listdirectory("/bin/");
68 
69  // Delete File
70  //if (fl_remove("/file.bin") < 0)
71  // kprintf("ERROR: Delete file failed\n");
72 
73  // Create File
74  file = fl_fopen("/file.bin", "w");
75  unsigned char data[] = {
76  'a',
77  '\n',
78  'b',
79  '\n',
80  'c',
81  '\n' };
82  if (file) {
83  // Write some data
84  if (fl_fwrite(data, 1, sizeof(data), file) != sizeof(data))
85  kprintf("ERROR: Write file failed\n");
86  }
87  else
88  kprintf("ERROR: Create file failed\n");
89 
90  // Close file
91  fl_fclose(file);
92 
93  return (1);
94 }
95 
96 int read_fat(fileDescriptor_t *fd, char *data, off_t offset, long size) {
97  FL_FILE *_file = (FL_FILE*) fd->res;
98 
99  if (size == 0) {
100  kprintf("ZERO: %s", _file->filename);
101  return (-1);
102  }
103 
104  //kprintf("[Offset(%s): %ld:%ld]", _file->filename, offset, size);
105  if (fl_fseek(_file, offset & 0xFFFFFFFF, 0) != 0)
106  kprintf("SEEK FAILED!");
107 
108  size = fl_fread(data, size, 1, _file);
109  if (size >= 0)
110  fd->offset = offset + size;
111 
112  /* Return */
113  return (size);
114 }
115 
116 int write_fat(fileDescriptor_t *fd, char *data, off_t offset, long size) {
117  FL_FILE *_file = (FL_FILE*) fd->res;
118 
119  kprintf("Writing: %i[%i]\n", size, offset);
120  // XXX this is not supposed to happen fl_fseek(_file, offset, 0);
121 
122  if (fl_fwrite(data, 1, size, _file) != size)
123  kprintf("ERROR: Write file failed\n");
124 
125  kprintf("Wrote: %i\n", size);
126 
127  /* Return */
128  return (size);
129 }
130 
131 int open_fat(const char *file, fileDescriptor_t *fd) {
132  FL_FILE *_file = 0x0;
133 
134  assert(fd);
135  assert(fd->mp);
136  assert(fd->mp->device);
137  assert(fd->mp->device->devInfo);
138  assert(fd->mp->device->devInfo->read);
139  assert(file);
140 
141  //kprintf("File: %s, ", file);
142  //kprintf("Mode: 0x%X\n", fd->mode);
143 
144  if ((fd->mode & 0x1) == 0x1) {
145  _file = fl_fopen(file, "r");
146  }
147  else if ((fd->mode & 0x2) == 0x2) {
148  if ((fd->mode & 0x8) == 0x8) {
149  _file = fl_fopen(file, "a");
150  }
151  else {
152  _file = fl_fopen(file, "w");
153  //kprintf("open for writing");
154  }
155  }
156  else
157  kprintf("Invalid Mode?");
158 
159  if (!_file) {
160  return (0x0);
161  }
162  else {
163  fd->offset = 0;
164  fd->res = _file;
165  fd->perms = 0x1;
166  fd->size = _file->filelength;
167  fd->ino = _file->startcluster;
168  }
169 
170  return (0x1);
171 }
172 
173 int unlink_fat() {
174  return (0);
175 }
176 
177 int mkdir_fat() {
178  return (0);
179 }
180 
181 int fat_init() {
182  // Initialise File IO Library
183  fl_init();
184 
185  /* Set up our file system structure */
186  struct fileSystem ubixFileSystem = {
187  NULL, /* prev */
188  NULL, /* next */
189  (void*) fat_initialize, /* vfsInitFS */
190  (void*) read_fat, /* vfsRead */
191  (void*) write_fat, /* vfsWrite */
192  (void*) open_fat, /* vfsOpenFile */
193  (void*) unlink_fat, /* vfsUnlink */
194  (void*) mkdir_fat, /* vfsMakeDir */
195  NULL, /* vfsRemDir */
196  NULL, /* vfsSync */
197  0xFA /* vfsType */
198  }; /* ubixFileSystem */
199 
200  /* Add UbixFS */
201  if (vfsRegisterFS(ubixFileSystem) != 0x0) {
202  kpanic("Unable To Enable UbixFS");
203  return (0x1);
204  }
205 
206  /* Return */
207  return (0x0);
208 }
mkdir_fat
int mkdir_fat()
Definition: fat.c:176
media_write
int media_write(unsigned long sector, unsigned char *buffer, unsigned long sector_count)
Definition: fat.c:50
file
fileDescriptor * file
Definition: tcpdump.c:45
fat_access.h
sFL_FILE::startcluster
uint32 startcluster
Definition: fat_filelib.h:41
fl_fclose
void fl_fclose(void *f)
Definition: fat_filelib.c:856
buffer
char * buffer
Definition: shell.c:47
fat_initialize
int fat_initialize(struct vfs_mountPoint *mp)
Definition: fat.c:56
vfs.h
fileDescriptor::res
void * res
Definition: file.h:82
string.h
fileDescriptor::offset
off_t offset
Definition: file.h:69
fileDescriptor::mode
uint16_t mode
Definition: file.h:68
fileDescriptor
Definition: file.h:62
file
Definition: descrip.h:67
assert
#define assert(e)
Definition: assert.h:64
assert.h
FAT_INIT_OK
#define FAT_INIT_OK
Definition: fat_access.h:10
fl_fwrite
int fl_fwrite(const void *data, int size, int count, void *f)
Definition: fat_filelib.c:1162
_fd
fileDescriptor_t * _fd
Definition: fat.c:41
fat_filelib.h
exec.h
fileSystem
filesSystem Structure
Definition: vfs.h:59
sched.h
kpanic
void kpanic(const char *fmt,...)
print panic message and halt system
Definition: kpanic.c:41
fl_init
void fl_init(void)
Definition: fat_filelib.c:584
open_fat
int open_fat(const char *file, fileDescriptor_t *fd)
Definition: fat.c:130
media_read
int media_read(unsigned long sector, unsigned char *buffer, unsigned long sector_count)
Definition: fat.c:44
fileDescriptor::mp
struct vfs_mountPoint * mp
Definition: file.h:65
vfsRegisterFS
int vfsRegisterFS(struct fileSystem newFS)
register a file system
Definition: vfs.c:79
vfs_mountPoint
Definition: mount.h:66
kpanic.h
kprintf.h
write_fat
int write_fat(fileDescriptor_t *fd, char *data, off_t offset, long size)
Definition: fat.c:115
unlink_fat
int unlink_fat()
Definition: fat.c:172
fileDescriptor::ino
uint32_t ino
Definition: file.h:66
device_interface::write
int(* write)(void *, void *, uInt32, uInt32)
Definition: device.h:53
fat_init
int fat_init()
Definition: fat.c:180
device_interface::read
int(* read)(void *, void *, uInt32, uInt32)
Definition: device.h:52
fl_fread
int fl_fread(void *buffer, int size, int length, void *f)
Definition: fat_filelib.c:938
fileDescriptor::perms
uint32_t perms
Definition: file.h:76
fl_fopen
void * fl_fopen(const char *path, const char *mode)
Definition: fat_filelib.c:638
sFL_FILE
Definition: fat_filelib.h:38
device_interface::info
void * info
Definition: device.h:51
sFL_FILE::filename
char filename[260]
Definition: fat_filelib.h:46
vfs_mountPoint::device
struct device_node * device
Definition: mount.h:70
_mp
struct vfs_mountPoint * _mp
Definition: fat.c:42
off_t
__int64_t off_t
Definition: types.h:119
device_node::devInfo
struct device_interface * devInfo
Definition: device.h:37
fl_fseek
int fl_fseek(void *f, long offset, int origin)
Definition: fat_filelib.c:1034
kprintf
int kprintf(const char *,...)
Definition: kprintf.c:259
fl_attach_media
int fl_attach_media(fn_diskio_read rd, fn_diskio_write wr)
Definition: fat_filelib.c:606
fileDescriptor::size
uint32_t size
Definition: file.h:70
sFL_FILE::filelength
uint32 filelength
Definition: fat_filelib.h:43
kmalloc.h
read_fat
int read_fat(fileDescriptor_t *fd, char *data, off_t offset, long size)
Definition: fat.c:95
NULL
#define NULL
Definition: fat_string.h:17