]> git.ozlabs.org Git - yaboot.git/blob - second/fs_of.c
7a22117b2e6f41176f7feaf583e95e3d87c091f2
[yaboot.git] / second / fs_of.c
1 /*
2  *  fs_of.c - an implementation for OpenFirmware supported filesystems
3  *
4  *  Copyright (C) 2001, 2002 Ethan Benson
5  *
6  *  Copyright (C) 1999 Benjamin Herrenschmidt
7  *
8  *  This program is free software; you can redistribute it and/or modify
9  *  it under the terms of the GNU General Public License as published by
10  *  the Free Software Foundation; either version 2 of the License, or
11  *  (at your option) any later version.
12  *
13  *  This program is distributed in the hope that it will be useful,
14  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *  GNU General Public License for more details.
17  *
18  *  You should have received a copy of the GNU General Public License
19  *  along with this program; if not, write to the Free Software
20  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
21  */
22
23 /* 
24  * BrokenFirmware cannot "read" from the network. We use tftp "load"
25  * method for network boot for now, we may provide our own NFS
26  * implementation in a later version. That means that we allocate a
27  * huge block of memory for the entire file before loading it. We use
28  * the location where the kernel puts RTAS, it's not used by the
29  * bootloader and if freed when the kernel is booted.  This will have
30  * to be changed if we plan to instanciate RTAS in the bootloader
31  * itself
32  */
33
34 #include "ctype.h"
35 #include "types.h"
36 #include "stddef.h"
37 #include "stdlib.h"
38 #include "file.h"
39 #include "prom.h"
40 #include "string.h"
41 #include "partition.h"
42 #include "fdisk-part.h"
43 #include "fs.h"
44 #include "errors.h"
45 #include "debug.h"
46
47 #define LOAD_BUFFER_POS         0x600000
48 /* this cannot be safely increased any further */
49 #define LOAD_BUFFER_SIZE        0x600000
50
51 static int of_open(struct boot_file_t* file, const char* dev_name,
52                    struct partition_t* part, const char* file_name);
53 static int of_read(struct boot_file_t* file, unsigned int size, void* buffer);
54 static int of_seek(struct boot_file_t* file, unsigned int newpos);
55 static int of_close(struct boot_file_t* file);
56
57
58 static int of_net_open(struct boot_file_t* file, const char* dev_name,
59                        struct partition_t* part, const char* file_name);
60 static int of_net_read(struct boot_file_t* file, unsigned int size, void* buffer);
61 static int of_net_seek(struct boot_file_t* file, unsigned int newpos);
62
63
64 struct fs_t of_filesystem =
65 {
66      "built-in",
67      of_open,
68      of_read,
69      of_seek,
70      of_close
71 };
72
73 struct fs_t of_net_filesystem =
74 {
75      "built-in network",
76      of_net_open,
77      of_net_read,
78      of_net_seek,
79      of_close
80 };
81
82 static int
83 of_open(struct boot_file_t* file, const char* dev_name,
84         struct partition_t* part, const char* file_name)
85 {
86      static char        buffer[1024];
87      char               *filename;
88      char               *p;
89         
90      DEBUG_ENTER;
91      DEBUG_OPEN;
92
93      if (part->sys_ind == LINUX_RAID)
94      {
95           DEBUG_F("skipping because partition is marked LINUX_RAID\n");
96           DEBUG_LEAVE(FILE_ERR_BAD_FSYS);
97           return FILE_ERR_BAD_FSYS;
98      }
99
100      strncpy(buffer, dev_name, 768);
101      strcat(buffer, ":");
102      if (part) {
103           char pn[3];
104           sprintf(pn, "%02d", part->part_number);
105           strcat(buffer, pn);
106      }
107      if (file_name && strlen(file_name)) {
108           if (part)
109                strcat(buffer, ",");
110           filename = strdup(file_name);
111           for (p = filename; *p; p++)
112                if (*p == '/') 
113                     *p = '\\';
114           strcat(buffer, filename);
115           free(filename);
116      }
117
118      DEBUG_F("opening: \"%s\"\n", buffer);
119
120      file->of_device = prom_open(buffer);
121
122      DEBUG_F("file->of_device = %p\n", file->of_device);
123
124      file->pos = 0;
125      file->buffer = NULL;
126      if ((file->of_device == PROM_INVALID_HANDLE) || (file->of_device == 0))
127      {
128           DEBUG_LEAVE(FILE_ERR_BAD_FSYS);
129           return FILE_ERR_BAD_FSYS;
130      }
131         
132      DEBUG_LEAVE(FILE_ERR_OK);
133      return FILE_ERR_OK;
134 }
135
136 static int
137 of_net_open(struct boot_file_t* file, const char* dev_name,
138             struct partition_t* part, const char* file_name)
139 {
140      static char        buffer[1024];
141      char               *filename;
142      char               *p;
143
144      DEBUG_ENTER;
145      DEBUG_OPEN;
146
147      strncpy(buffer, dev_name, 768);
148      if (file_name && strlen(file_name)) {
149           strcat(buffer, ",");
150           filename = strdup(file_name);
151           for (p = filename; *p; p++)
152                if (*p == '/') 
153                     *p = '\\';
154           strcat(buffer, filename);
155           free(filename);
156      }
157                         
158      DEBUG_F("Opening: \"%s\"\n", buffer);
159
160      file->of_device = prom_open(buffer);
161
162      DEBUG_F("file->of_device = %p\n", file->of_device);
163
164      file->pos = 0;
165      if ((file->of_device == PROM_INVALID_HANDLE) || (file->of_device == 0))
166      {
167           DEBUG_LEAVE(FILE_ERR_BAD_FSYS);
168           return FILE_ERR_BAD_FSYS;
169      }
170         
171      file->buffer = prom_claim((void *)LOAD_BUFFER_POS, LOAD_BUFFER_SIZE, 0);
172      if (file->buffer == (void *)-1) {
173           prom_printf("Can't claim memory for TFTP download\n");
174           prom_close(file->of_device);
175           DEBUG_LEAVE(FILE_IOERR);
176           return FILE_IOERR;
177      }
178      memset(file->buffer, 0, LOAD_BUFFER_SIZE);
179
180      DEBUG_F("TFP...\n");
181
182      file->len = prom_loadmethod(file->of_device, file->buffer);
183         
184      DEBUG_F("result: %Ld\n", file->len);
185         
186      DEBUG_LEAVE(FILE_ERR_OK);
187      return FILE_ERR_OK;
188 }
189
190 static int
191 of_read(struct boot_file_t* file, unsigned int size, void* buffer)
192 {
193      unsigned int count;
194         
195      count = prom_read(file->of_device, buffer, size);
196      file->pos += count;
197      return count;
198 }
199
200 static int
201 of_net_read(struct boot_file_t* file, unsigned int size, void* buffer)
202 {
203      unsigned int count, av;
204         
205      av = file->len - file->pos;
206      count = size > av ? av : size; 
207      memcpy(buffer, file->buffer + file->pos, count);
208      file->pos += count;
209      return count;
210 }
211
212 static int
213 of_seek(struct boot_file_t* file, unsigned int newpos)
214 {
215      if (prom_seek(file->of_device, newpos)) {
216           file->pos = newpos;
217           return FILE_ERR_OK;
218      }
219                 
220      return FILE_CANT_SEEK;
221 }
222
223 static int
224 of_net_seek(struct boot_file_t* file, unsigned int newpos)
225 {
226      file->pos = (newpos > file->len) ? file->len : newpos;
227      return FILE_ERR_OK;
228 }
229
230 static int
231 of_close(struct boot_file_t* file)
232 {
233
234      DEBUG_ENTER;
235      DEBUG_F("<@%p>\n", file->of_device);
236
237      if (file->buffer) {
238           prom_release(file->buffer, LOAD_BUFFER_SIZE);
239      }
240      prom_close(file->of_device);
241      DEBUG_F("of_close called\n");
242
243      DEBUG_LEAVE(0);    
244      return 0;
245 }
246
247 /* 
248  * Local variables:
249  * c-file-style: "k&r"
250  * c-basic-offset: 5
251  * End:
252  */