]> git.ozlabs.org Git - yaboot.git/blob - second/fs_of.c
Increase tftp buffer to ~6MB
[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 "fs.h"
43 #include "errors.h"
44 #include "debug.h"
45
46 #define LOAD_BUFFER_POS         0x600000
47 /* this cannot be safely increased any further */
48 #define LOAD_BUFFER_SIZE        0x600000
49
50 static int of_open(struct boot_file_t* file, const char* dev_name,
51                    struct partition_t* part, const char* file_name);
52 static int of_read(struct boot_file_t* file, unsigned int size, void* buffer);
53 static int of_seek(struct boot_file_t* file, unsigned int newpos);
54 static int of_close(struct boot_file_t* file);
55
56
57 static int of_net_open(struct boot_file_t* file, const char* dev_name,
58                        struct partition_t* part, const char* file_name);
59 static int of_net_read(struct boot_file_t* file, unsigned int size, void* buffer);
60 static int of_net_seek(struct boot_file_t* file, unsigned int newpos);
61
62
63 struct fs_t of_filesystem =
64 {
65      "built-in",
66      of_open,
67      of_read,
68      of_seek,
69      of_close
70 };
71
72 struct fs_t of_net_filesystem =
73 {
74      "built-in network",
75      of_net_open,
76      of_net_read,
77      of_net_seek,
78      of_close
79 };
80
81 static int
82 of_open(struct boot_file_t* file, const char* dev_name,
83         struct partition_t* part, const char* file_name)
84 {
85      static char        buffer[1024];
86      char               *filename;
87      char               *p;
88         
89      DEBUG_ENTER;
90      DEBUG_OPEN;
91
92      strncpy(buffer, dev_name, 768);
93      strcat(buffer, ":");
94      if (part) {
95           char pn[3];
96           sprintf(pn, "%02d", part->part_number);
97           strcat(buffer, pn);
98      }
99      if (file_name && strlen(file_name)) {
100           if (part)
101                strcat(buffer, ",");
102           filename = strdup(file_name);
103           for (p = filename; *p; p++)
104                if (*p == '/') 
105                     *p = '\\';
106           strcat(buffer, filename);
107           free(filename);
108      }
109
110      DEBUG_F("opening: \"%s\"\n", buffer);
111
112      file->of_device = prom_open(buffer);
113
114      DEBUG_F("file->of_device = %p\n", file->of_device);
115
116      file->pos = 0;
117      file->buffer = NULL;
118      if ((file->of_device == PROM_INVALID_HANDLE) || (file->of_device == 0))
119      {
120           DEBUG_LEAVE(FILE_ERR_BAD_FSYS);
121           return FILE_ERR_BAD_FSYS;
122      }
123         
124      DEBUG_LEAVE(FILE_ERR_OK);
125      return FILE_ERR_OK;
126 }
127
128 static int
129 of_net_open(struct boot_file_t* file, const char* dev_name,
130             struct partition_t* part, const char* file_name)
131 {
132      static char        buffer[1024];
133      char               *filename;
134      char               *p;
135
136      DEBUG_ENTER;
137      DEBUG_OPEN;
138
139      strncpy(buffer, dev_name, 768);
140      if (file_name && strlen(file_name)) {
141           strcat(buffer, ",");
142           filename = strdup(file_name);
143           for (p = filename; *p; p++)
144                if (*p == '/') 
145                     *p = '\\';
146           strcat(buffer, filename);
147           free(filename);
148      }
149                         
150      DEBUG_F("Opening: \"%s\"\n", buffer);
151
152      file->of_device = prom_open(buffer);
153
154      DEBUG_F("file->of_device = %p\n", file->of_device);
155
156      file->pos = 0;
157      if ((file->of_device == PROM_INVALID_HANDLE) || (file->of_device == 0))
158      {
159           DEBUG_LEAVE(FILE_ERR_BAD_FSYS);
160           return FILE_ERR_BAD_FSYS;
161      }
162         
163      file->buffer = prom_claim((void *)LOAD_BUFFER_POS, LOAD_BUFFER_SIZE, 0);
164      if (file->buffer == (void *)-1) {
165           prom_printf("Can't claim memory for TFTP download\n");
166           prom_close(file->of_device);
167           DEBUG_LEAVE(FILE_IOERR);
168           return FILE_IOERR;
169      }
170      memset(file->buffer, 0, LOAD_BUFFER_SIZE);
171
172      DEBUG_F("TFP...\n");
173
174      file->len = prom_loadmethod(file->of_device, file->buffer);
175         
176      DEBUG_F("result: %Ld\n", file->len);
177         
178      DEBUG_LEAVE(FILE_ERR_OK);
179      return FILE_ERR_OK;
180 }
181
182 static int
183 of_read(struct boot_file_t* file, unsigned int size, void* buffer)
184 {
185      unsigned int count;
186         
187      count = prom_read(file->of_device, buffer, size);
188      file->pos += count;
189      return count;
190 }
191
192 static int
193 of_net_read(struct boot_file_t* file, unsigned int size, void* buffer)
194 {
195      unsigned int count, av;
196         
197      av = file->len - file->pos;
198      count = size > av ? av : size; 
199      memcpy(buffer, file->buffer + file->pos, count);
200      file->pos += count;
201      return count;
202 }
203
204 static int
205 of_seek(struct boot_file_t* file, unsigned int newpos)
206 {
207      if (prom_seek(file->of_device, newpos)) {
208           file->pos = newpos;
209           return FILE_ERR_OK;
210      }
211                 
212      return FILE_CANT_SEEK;
213 }
214
215 static int
216 of_net_seek(struct boot_file_t* file, unsigned int newpos)
217 {
218      file->pos = (newpos > file->len) ? file->len : newpos;
219      return FILE_ERR_OK;
220 }
221
222 static int
223 of_close(struct boot_file_t* file)
224 {
225
226      DEBUG_ENTER;
227      DEBUG_F("<@%p>\n", file->of_device);
228
229      if (file->buffer) {
230           prom_release(file->buffer, LOAD_BUFFER_SIZE);
231      }
232      prom_close(file->of_device);
233      DEBUG_F("of_close called\n");
234
235      DEBUG_LEAVE(0);    
236      return 0;
237 }
238
239 /* 
240  * Local variables:
241  * c-file-style: "k&r"
242  * c-basic-offset: 5
243  * End:
244  */