UbixOS V2  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[] = { 1, 2, 3, 4 };
76  if (file) {
77  // Write some data
78  if (fl_fwrite(data, 1, sizeof(data), file) != sizeof(data))
79  kprintf("ERROR: Write file failed\n");
80  }
81  else
82  kprintf("ERROR: Create file failed\n");
83 
84  // Close file
85  fl_fclose(file);
86  /*
87  fl_listdirectory("/");
88 
89  // List root directory
90  fl_listdirectory("/");
91 
92  size_t size;
93 
94  file = fl_fopen("/shell", "r");
95  size = fl_fread(data, 1, sizeof(data), file);
96  kprintf("READ: %i", size);
97  fl_fclose(file);
98  */
99  return (1);
100 }
101 
102 int read_fat(fileDescriptor_t *fd, char *data, uInt32 offset, long size) {
103  FL_FILE *_file = (FL_FILE*) fd->res;
104 
105  //kprintf("Reading: %i[%i]\n", size, offset);
106  if (fl_fseek(_file, offset, 0) != 0)
107  kprintf("SEEK FAILED!");
108 
109  size = fl_fread(data, size, 1, _file);
110 
111  //kprintf("[Read:%i]", size);
112 
113  /* Return */
114  return (size);
115 }
116 
117 int write_fat(fileDescriptor_t *fd, char *data, uInt32 offset, long size) {
118  FL_FILE *_file = (FL_FILE*) fd->res;
119 
120  kprintf("Writing: %i[%i]\n", size, offset);
121  // XXX this is not supposed to happen fl_fseek(_file, offset, 0);
122 
123  if (fl_fwrite(data, 1, size, _file) != size)
124  kprintf("ERROR: Write file failed\n");
125 
126  kprintf("Wrote: %i\n", size);
127 
128  /* Return */
129  return (size);
130 }
131 
132 int open_fat(const char *file, fileDescriptor_t *fd) {
133  FL_FILE *_file = 0x0;
134 
135  assert(fd);
136  assert(fd->mp);
137  assert(fd->mp->device);
138  assert(fd->mp->device->devInfo);
139  assert(fd->mp->device->devInfo->read);
140  assert(file);
141 
142  kprintf("File: %s, ", file);
143  kprintf("Mode: 0x%X\n", fd->mode);
144 
145  if ((fd->mode & 0x1) == 0x1)
146  _file = fl_fopen(file, "r");
147  else if ((fd->mode & 0x2) == 0x2) {
148  if ((fd->mode & 0x8) == 0x8)
149  _file = fl_fopen(file, "a");
150  else
151  _file = fl_fopen(file, "w");
152  }
153  else
154  kprintf("Invalid Mode?");
155 
156  if (!_file) {
157  //kprintf("ERROR[%s:%i]: Open file: [%s] failed\n", __FILE__, __LINE__, file);
158  return (0x0);
159  }
160  else {
161  fd->offset = 0;
162  fd->res = _file;
163  fd->perms = 0x1;
164  fd->size = _file->filelength;
165  fd->ino = _file->startcluster;
166  //kprintf("Size: %i\n", fd->size);
167  }
168 
169  return (0x1);
170 }
171 
172 int unlink_fat() {
173  return (0);
174 }
175 
176 int mkdir_fat() {
177  return (0);
178 }
179 
180 int fat_init() {
181  // Initialise File IO Library
182  fl_init();
183 
184  /* Set up our file system structure */
185  struct fileSystem ubixFileSystem = {
186  NULL, /* prev */
187  NULL, /* next */
188  (void*) fat_initialize, /* vfsInitFS */
189  (void*) read_fat, /* vfsRead */
190  (void*) write_fat, /* vfsWrite */
191  (void*) open_fat, /* vfsOpenFile */
192  (void*) unlink_fat, /* vfsUnlink */
193  (void*) mkdir_fat, /* vfsMakeDir */
194  NULL, /* vfsRemDir */
195  NULL, /* vfsSync */
196  0xFA /* vfsType */
197  }; /* ubixFileSystem */
198 
199  /* Add UbixFS */
200  if (vfsRegisterFS(ubixFileSystem) != 0x0) {
201  kpanic("Unable To Enable UbixFS");
202  return (0x1);
203  }
204 
205  /* Return */
206  return (0x0);
207 }
mkdir_fat
int mkdir_fat()
Definition: fat.c:175
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
uInt32
unsigned long int uInt32
Definition: objgfx30.h:49
vfs.h
fileDescriptor::res
void * res
Definition: file.h:82
string.h
fl_remove
int fl_remove(const char *filename)
Definition: fat_filelib.c:1314
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
write_fat
int write_fat(fileDescriptor_t *fd, char *data, uInt32 offset, long size)
Definition: fat.c:116
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:131
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
unlink_fat
int unlink_fat()
Definition: fat.c:171
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:179
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
vfs_mountPoint::device
struct device_node * device
Definition: mount.h:70
_mp
struct vfs_mountPoint * _mp
Definition: fat.c:42
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, uInt32 offset, long size)
Definition: fat.c:101
NULL
#define NULL
Definition: fat_string.h:17