]> git.ozlabs.org Git - yaboot.git/blob - second/fs_of.c
fc88e8e49c889f7596733ded3e28d9d8af0126f0
[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_SIZE        0x1800000
48
49 static int of_open(struct boot_file_t* file,
50                    struct partition_t* part, struct boot_fspec_t* fspec);
51 static int of_read(struct boot_file_t* file, unsigned int size, void* buffer);
52 static int of_seek(struct boot_file_t* file, unsigned int newpos);
53 static int of_close(struct boot_file_t* file);
54 static int of_ino_size(struct boot_file_t* file, unsigned int *size);
55
56
57 static int of_net_open(struct boot_file_t* file,
58                        struct partition_t* part, struct boot_fspec_t* fspec);
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 static int of_net_ino_size(struct boot_file_t* file, unsigned int *size);
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      of_ino_size,
72 };
73
74 struct fs_t of_net_filesystem =
75 {
76      "built-in network",
77      of_net_open,
78      of_net_read,
79      of_net_seek,
80      of_close,
81      of_net_ino_size,
82 };
83
84 static int
85 of_open(struct boot_file_t* file,
86         struct partition_t* part, struct boot_fspec_t* fspec)
87 {
88      static char        buffer[1024];
89      char               *filename;
90      char               *p;
91
92      DEBUG_ENTER;
93      DEBUG_OPEN;
94
95      strncpy(buffer, fspec->dev, 768);
96      strcat(buffer, ":");
97      if (part) {
98           if (part->sys_ind == LINUX_RAID || part->sys_ind == LINUX_NATIVE) {
99                DEBUG_F("skipping because partition is tagged %08x\n",
100                        part->sys_ind  );
101                DEBUG_LEAVE(FILE_ERR_BAD_FSYS);
102                return FILE_ERR_BAD_FSYS;
103           }
104           char pn[3];
105           sprintf(pn, "%02d", part->part_number);
106           strcat(buffer, pn);
107      }
108      if (fspec->file && strlen(fspec->file)) {
109           if (part)
110                strcat(buffer, ",");
111           filename = strdup(fspec->file);
112           for (p = filename; *p; p++)
113                if (*p == '/')
114                     *p = '\\';
115           strcat(buffer, filename);
116           free(filename);
117      }
118
119      DEBUG_F("opening: \"%s\"\n", buffer);
120
121      file->of_device = prom_open(buffer);
122
123      DEBUG_F("file->of_device = %p\n", file->of_device);
124
125      file->pos = 0;
126      file->buffer = NULL;
127      file->devspec_cache = strdup(buffer);
128
129      if ((file->of_device == PROM_INVALID_HANDLE) || (file->of_device == 0))
130      {
131           DEBUG_LEAVE(FILE_ERR_BAD_FSYS);
132           return FILE_ERR_BAD_FSYS;
133      }
134
135      DEBUG_LEAVE(FILE_ERR_OK);
136      return FILE_ERR_OK;
137 }
138
139 static int
140 of_net_open(struct boot_file_t* file,
141             struct partition_t* part, struct boot_fspec_t* fspec)
142 {
143      static char        buffer[1024];
144      char               *filename = NULL;
145      char               *p;
146      int                new_tftp;
147      prom_handle        root;
148      ihandle            prom_net;
149
150      DEBUG_ENTER;
151      DEBUG_OPEN;
152
153      if (fspec->file && strlen(fspec->file)) {
154           filename = strdup(fspec->file);
155           for (p = filename; *p; p++)
156                if (*p == '/')
157                     *p = '\\';
158      }
159
160      DEBUG_F("siaddr <%s>; filename <%s>; ciaddr <%s>; giaddr <%s>;"
161              " ipv6 <%d>; vtag <%s>\n",
162              fspec->siaddr, filename, fspec->ciaddr, fspec->giaddr,
163              fspec->is_ipv6, fspec->vtag);
164
165      strncpy(buffer, fspec->dev, 768);
166      /* If we didn't get a ':' include one */
167      if (fspec->dev[strlen(fspec->dev)-1] != ':')
168           strcat(buffer, ":");
169
170      /* If /ibm,fw-net-compatibility exists the we have a "new skool" tftp.
171       * This means that siaddr is the tftp server and that we can add
172       * {tftp,bootp}_retrys, subnet mask and tftp block size to the load
173       * method */
174      new_tftp = 0;
175      root = prom_finddevice("/");
176      if (prom_getprop(root, "ibm,fw-net-compatibility", &prom_net, sizeof(ihandle)) > 0)
177           new_tftp = 1;
178      DEBUG_F("Using %s tftp style\n", (new_tftp? "new": "old"));
179
180      if (new_tftp) {
181           if (fspec->is_ipv6)
182                strcat(buffer, "ipv6,");
183
184           if (fspec->vtag) {
185                strcat(buffer, fspec->vtag);
186                strcat(buffer, ",");
187           }
188           strcat(buffer, fspec->siaddr);
189           strcat(buffer, ",");
190
191           if (fspec->is_ipv6 && (strstr(filename, "filename=") == NULL))
192                strcat(buffer, "filename=");
193
194           strcat(buffer, filename);
195           strcat(buffer, ",");
196           strcat(buffer, fspec->ciaddr);
197           strcat(buffer, ",");
198           strcat(buffer, fspec->giaddr);
199           strcat(buffer, ",");
200           strcat(buffer, fspec->bootp_retries);
201           strcat(buffer, ",");
202           strcat(buffer, fspec->tftp_retries);
203           strcat(buffer, ",");
204           strcat(buffer, fspec->subnetmask);
205           strcat(buffer, ",");
206           strcat(buffer, fspec->addl_params);
207      } else {
208           strcat(buffer, ",");
209           strcat(buffer, filename);
210      }
211
212      DEBUG_F("Opening: \"%s\"\n", buffer);
213
214      file->of_device = prom_open(buffer);
215
216      DEBUG_F("file->of_device = %p\n", file->of_device);
217
218      file->pos = 0;
219      if ((file->of_device == PROM_INVALID_HANDLE) || (file->of_device == 0))
220      {
221           DEBUG_LEAVE(FILE_ERR_BAD_FSYS);
222           return FILE_ERR_BAD_FSYS;
223      }
224
225
226      file->buffer = prom_claim_chunk_top(LOAD_BUFFER_SIZE, 0);
227      if (file->buffer == (void *)-1) {
228           prom_printf("Can't claim memory for TFTP download\n");
229           prom_close(file->of_device);
230           DEBUG_LEAVE(FILE_IOERR);
231           return FILE_IOERR;
232      }
233      memset(file->buffer, 0, LOAD_BUFFER_SIZE);
234
235      DEBUG_F("TFP...\n");
236
237      file->len = prom_loadmethod(file->of_device, file->buffer);
238
239      DEBUG_F("result: %Ld\n", file->len);
240
241      DEBUG_LEAVE(FILE_ERR_OK);
242      return FILE_ERR_OK;
243 }
244
245 static int
246 of_read(struct boot_file_t* file, unsigned int size, void* buffer)
247 {
248      unsigned int count;
249
250      count = prom_read(file->of_device, buffer, size);
251      file->pos += count;
252      return count;
253 }
254
255 static int
256 of_net_read(struct boot_file_t* file, unsigned int size, void* buffer)
257 {
258      unsigned int count, av;
259
260      av = file->len - file->pos;
261      count = size > av ? av : size;
262      memcpy(buffer, file->buffer + file->pos, count);
263      file->pos += count;
264      return count;
265 }
266
267 static int
268 of_seek(struct boot_file_t* file, unsigned int newpos)
269 {
270      if (prom_seek(file->of_device, newpos)) {
271           file->pos = newpos;
272           return FILE_ERR_OK;
273      }
274
275      return FILE_CANT_SEEK;
276 }
277
278 static int
279 of_net_seek(struct boot_file_t* file, unsigned int newpos)
280 {
281      file->pos = (newpos > file->len) ? file->len : newpos;
282      return FILE_ERR_OK;
283 }
284
285 static int
286 of_close(struct boot_file_t* file)
287 {
288
289      DEBUG_ENTER;
290      DEBUG_F("<@%p>\n", file->of_device);
291
292      if (file->buffer) {
293           prom_release(file->buffer, LOAD_BUFFER_SIZE);
294      }
295      prom_close(file->of_device);
296      DEBUG_F("of_close called\n");
297
298      DEBUG_LEAVE(0);
299      return 0;
300 }
301
302 static int
303 of_ino_size(struct boot_file_t* file, unsigned int *size)
304 {
305      static char buffer[1<<20];
306      int read_count = 0;
307
308      if (file->len == 0) {
309           DEBUG_F("Estimating size of: %p\n", file->of_device);
310           while (prom_read(file->of_device, (void *)&buffer, sizeof(buffer))
311                  != 0) {
312                read_count++;
313                DEBUG_F("read_count == %d\n", read_count);
314           }
315           file->pos = 0;
316           file->len = read_count * sizeof(buffer);
317           /* sigh:
318            * prom_seek(file->of_device, file->pos);
319            * doen't work */
320           prom_close(file->of_device);
321           DEBUG_F("Re-Opening: \"%s\"\n", file->devspec_cache);
322           file->of_device = prom_open(file->devspec_cache);
323           DEBUG_F("file->of_device = %p\n", file->of_device);
324           if (file->of_device == 0) {
325                file->len = 0;
326                return FILE_IOERR;
327           }
328      }
329
330      DEBUG_F("Estimated size is: %Lu(%d)\n", (unsigned long long)file->len,
331              read_count);
332      *size = file->len;
333      return FILE_ERR_OK;
334 }
335
336 static int
337 of_net_ino_size(struct boot_file_t* file, unsigned int *size)
338 {
339         *size = file->len;
340         return FILE_ERR_OK;
341 }
342
343 /*
344  * Local variables:
345  * c-file-style: "k&r"
346  * c-basic-offset: 5
347  * End:
348  */