2 * Creation Date: <2003/12/07 19:36:00 samuel>
3 * Time-stamp: <2004/01/07 19:28:43 samuel>
9 * Copyright (C) 2003, 2004 Samuel Rydh (samuel@ibrium.se)
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License
18 #include "libopenbios/bindings.h"
19 #include "libc/diskio.h"
21 //#define CONFIG_DEBUG_DISKIO
22 #ifdef CONFIG_DEBUG_DISKIO
23 #define DPRINTF(fmt, args...) \
24 do { printk(fmt , ##args); } while (0)
26 #define DPRINTF(fmt, args...)
44 static priv_fd_t
*file_descriptors
[MAX_FD
];
47 lookup_xt( ihandle_t ih
, const char *method
, xt_t
*xt
)
51 *xt
= find_ih_method( method
, ih
);
56 open_ih( ihandle_t ih
)
58 xt_t read_xt
=0, seek_xt
=0;
62 if( !ih
|| lookup_xt(ih
, "read", &read_xt
) )
64 if( lookup_xt(ih
, "seek", &seek_xt
) )
67 for (fd
=0; fd
<MAX_FD
; fd
++)
68 if(file_descriptors
[fd
]==NULL
)
73 fdp
= malloc( sizeof(*fdp
) );
74 /* Better clear the fd, as it
75 * contains valuable information
77 memset(fdp
, 0, sizeof(*fdp
));
79 fdp
->read_xt
= read_xt
;
80 fdp
->seek_xt
= seek_xt
;
83 file_descriptors
[fd
]=fdp
;
84 DPRINTF("%s(0x%lx) = %d\n", __func__
, (unsigned long)ih
, fd
);
89 open_io( const char *spec
)
92 ihandle_t ih
= open_dev( spec
);
95 DPRINTF("%s(%s)\n", __func__
, spec
);
99 if( (fd
=open_ih(ih
)) == -1 ) {
104 fdp
= file_descriptors
[fd
];
111 reopen( int fd
, const char *filename
)
113 priv_fd_t
*fdp
= file_descriptors
[fd
];
116 if( lookup_xt(fdp
->ih
, "reopen", &fdp
->reopen_xt
) )
119 push_str( filename
);
120 call_package( fdp
->reopen_xt
, fdp
->ih
);
121 ret
= (POP() == (ucell
)-1)? 0 : -1;
123 DPRINTF("%s(%d, %s) = %d\n", __func__
, fd
, filename
, ret
);
128 reopen_nwrom( int fd
)
130 priv_fd_t
*fdp
= file_descriptors
[fd
];
132 DPRINTF("%s(%d)\n", __func__
, fd
);
133 if( lookup_xt(fdp
->ih
, "open-nwrom", &fdp
->open_nwrom_xt
) )
135 call_package( fdp
->open_nwrom_xt
, fdp
->ih
);
136 return (POP() == (ucell
)-1)? 0 : -1;
140 get_ih_from_fd( int fd
)
142 priv_fd_t
*fdp
= file_descriptors
[fd
];
147 get_file_path( int fd
)
149 priv_fd_t
*fdp
= file_descriptors
[fd
];
150 if( lookup_xt(fdp
->ih
, "get-path", &fdp
->get_path_xt
) )
152 call_package( fdp
->get_path_xt
, fdp
->ih
);
153 return (char*)cell2pointer(POP());
157 get_volume_name( int fd
)
159 priv_fd_t
*fdp
= file_descriptors
[fd
];
160 if( lookup_xt(fdp
->ih
, "volume-name", &fdp
->volume_name_xt
) )
162 call_package( fdp
->volume_name_xt
, fdp
->ih
);
163 return (char*)cell2pointer(POP());
169 priv_fd_t
*fdp
= file_descriptors
[fd
];
170 if( lookup_xt(fdp
->ih
, "get-fstype", &fdp
->get_fstype_xt
) )
172 call_package( fdp
->get_fstype_xt
, fdp
->ih
);
173 return (char*)cell2pointer(POP());
177 read_io( int fd
, void *buf
, size_t cnt
)
182 DPRINTF("%s(%d, %p, %u)\n", __func__
, fd
, buf
, cnt
);
184 fdp
= file_descriptors
[fd
];
186 PUSH( pointer2cell(buf
) );
188 call_package( fdp
->read_xt
, fdp
->ih
);
201 seek_io( int fd
, long long offs
)
205 DPRINTF("%s(%d, %lld)\n", __func__
, fd
, offs
);
207 fdp
= file_descriptors
[fd
];
210 call_package( fdp
->seek_xt
, fdp
->ih
);
211 return ((((cell
)POP()) >= 0)? 0 : -1);
220 priv_fd_t
*fdp
= file_descriptors
[fd
];
223 if( lookup_xt(fdp
->ih
, "tell", &fdp
->tell_xt
) )
225 call_package( fdp
->tell_xt
, fdp
->ih
);
227 DPRINTF("%s(%d) = %lld\n", __func__
, fd
, offs
);
236 DPRINTF("%s(%d)\n", __func__
, fd
);
238 fdp
= file_descriptors
[fd
];
241 close_dev( fdp
->ih
);
244 file_descriptors
[fd
]=NULL
;