ppc64: Don't set Kp bit on SLB
[openbios/afaerber.git] / libc / diskio.c
blob23f38ebb25209d7208c25989b35d1003e3c0be10
1 /*
2 * Creation Date: <2003/12/07 19:36:00 samuel>
3 * Time-stamp: <2004/01/07 19:28:43 samuel>
5 * <diskio.c>
7 * I/O wrappers
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
13 * version 2
17 #include "config.h"
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)
25 #else
26 #define DPRINTF(fmt, args...)
27 #endif
29 typedef struct {
30 ihandle_t ih;
31 int do_close;
32 xt_t read_xt;
33 xt_t seek_xt;
35 xt_t reopen_xt;
36 xt_t tell_xt;
37 xt_t get_path_xt;
38 xt_t get_fstype_xt;
39 xt_t open_nwrom_xt;
40 xt_t volume_name_xt;
41 } priv_fd_t;
43 #define MAX_FD 32
44 static priv_fd_t *file_descriptors[MAX_FD];
46 static int
47 lookup_xt( ihandle_t ih, const char *method, xt_t *xt )
49 if( *xt )
50 return 0;
51 *xt = find_ih_method( method, ih );
52 return (*xt) ? 0:1;
55 int
56 open_ih( ihandle_t ih )
58 xt_t read_xt=0, seek_xt=0;
59 priv_fd_t *fdp;
60 int fd;
62 if( !ih || lookup_xt(ih, "read", &read_xt) )
63 return -1;
64 if( lookup_xt(ih, "seek", &seek_xt) )
65 return -1;
67 for (fd=0; fd<MAX_FD; fd++)
68 if(file_descriptors[fd]==NULL)
69 break;
70 if(fd==MAX_FD)
71 return -1;
73 fdp = malloc( sizeof(*fdp) );
74 /* Better clear the fd, as it
75 * contains valuable information
77 memset(fdp, 0, sizeof(*fdp));
78 fdp->ih = ih;
79 fdp->read_xt = read_xt;
80 fdp->seek_xt = seek_xt;
81 fdp->do_close = 0;
83 file_descriptors[fd]=fdp;
84 DPRINTF("%s(0x%lx) = %d\n", __func__, (unsigned long)ih, fd);
85 return fd;
88 int
89 open_io( const char *spec )
91 int fd;
92 ihandle_t ih = open_dev( spec );
93 priv_fd_t *fdp;
95 DPRINTF("%s(%s)\n", __func__, spec);
96 if( !ih )
97 return -1;
99 if( (fd=open_ih(ih)) == -1 ) {
100 close_dev( ih );
101 return -1;
104 fdp = file_descriptors[fd];
105 fdp->do_close = 1;
107 return fd;
111 reopen( int fd, const char *filename )
113 priv_fd_t *fdp = file_descriptors[fd];
114 int ret;
116 if( lookup_xt(fdp->ih, "reopen", &fdp->reopen_xt) )
117 return -1;
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);
124 return 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) )
134 return -1;
135 call_package( fdp->open_nwrom_xt, fdp->ih );
136 return (POP() == (ucell)-1)? 0 : -1;
139 ihandle_t
140 get_ih_from_fd( int fd )
142 priv_fd_t *fdp = file_descriptors[fd];
143 return fdp->ih;
146 const char *
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) )
151 return NULL;
152 call_package( fdp->get_path_xt, fdp->ih );
153 return (char*)cell2pointer(POP());
156 const char *
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) )
161 return NULL;
162 call_package( fdp->volume_name_xt, fdp->ih );
163 return (char*)cell2pointer(POP());
166 const char *
167 get_fstype( int fd )
169 priv_fd_t *fdp = file_descriptors[fd];
170 if( lookup_xt(fdp->ih, "get-fstype", &fdp->get_fstype_xt) )
171 return NULL;
172 call_package( fdp->get_fstype_xt, fdp->ih );
173 return (char*)cell2pointer(POP());
177 read_io( int fd, void *buf, size_t cnt )
179 priv_fd_t *fdp;
180 ucell ret;
182 DPRINTF("%s(%d, %p, %u)\n", __func__, fd, buf, cnt);
183 if (fd != -1) {
184 fdp = file_descriptors[fd];
186 PUSH( pointer2cell(buf) );
187 PUSH( cnt );
188 call_package( fdp->read_xt, fdp->ih );
189 ret = POP();
191 if( !ret && cnt )
192 ret = -1;
193 } else {
194 ret = -1;
197 return ret;
201 seek_io( int fd, long long offs )
203 priv_fd_t *fdp;
205 DPRINTF("%s(%d, %lld)\n", __func__, fd, offs);
206 if (fd != -1) {
207 fdp = file_descriptors[fd];
209 DPUSH( offs );
210 call_package( fdp->seek_xt, fdp->ih );
211 return ((((cell)POP()) >= 0)? 0 : -1);
212 } else {
213 return -1;
217 long long
218 tell( int fd )
220 priv_fd_t *fdp = file_descriptors[fd];
221 long long offs;
223 if( lookup_xt(fdp->ih, "tell", &fdp->tell_xt) )
224 return -1;
225 call_package( fdp->tell_xt, fdp->ih );
226 offs = DPOP();
227 DPRINTF("%s(%d) = %lld\n", __func__, fd, offs);
228 return offs;
232 close_io( int fd )
234 priv_fd_t *fdp;
236 DPRINTF("%s(%d)\n", __func__, fd);
237 if (fd != -1) {
238 fdp = file_descriptors[fd];
240 if( fdp->do_close )
241 close_dev( fdp->ih );
242 free( fdp );
244 file_descriptors[fd]=NULL;
247 return 0;