]> git.ozlabs.org Git - yaboot.git/blob - second/fs_of.c
Use ino_size if available
[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         0x1000000
48 #define LOAD_BUFFER_SIZE        0x1800000
49
50 static int of_open(struct boot_file_t* file,
51                    struct partition_t* part, struct boot_fspec_t* fspec);
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,
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 unsigned int of_net_ino_size(struct boot_file_t* file);
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      of_net_ino_size,
81 };
82
83 static int
84 of_open(struct boot_file_t* file,
85         struct partition_t* part, struct boot_fspec_t* fspec)
86 {
87      static char        buffer[1024];
88      char               *filename;
89      char               *p;
90
91      DEBUG_ENTER;
92      DEBUG_OPEN;
93
94      strncpy(buffer, fspec->dev, 768);
95      strcat(buffer, ":");
96      if (part) {
97           if (part->sys_ind == LINUX_RAID || part->sys_ind == LINUX_NATIVE) {
98                DEBUG_F("skipping because partition is tagged %08x\n",
99                        part->sys_ind  );
100                DEBUG_LEAVE(FILE_ERR_BAD_FSYS);
101                return FILE_ERR_BAD_FSYS;
102           }
103           char pn[3];
104           sprintf(pn, "%02d", part->part_number);
105           strcat(buffer, pn);
106      }
107      if (fspec->file && strlen(fspec->file)) {
108           if (part)
109                strcat(buffer, ",");
110           filename = strdup(fspec->file);
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,
138             struct partition_t* part, struct boot_fspec_t* fspec)
139 {
140      static char        buffer[1024];
141      char               *filename = NULL;
142      char               *p;
143      int                new_tftp;
144
145      DEBUG_ENTER;
146      DEBUG_OPEN;
147
148      if (fspec->file && strlen(fspec->file)) {
149           filename = strdup(fspec->file);
150           for (p = filename; *p; p++)
151                if (*p == '/')
152                     *p = '\\';
153      }
154
155      DEBUG_F("siaddr <%s>; filename <%s>; ciaddr <%s>; giaddr <%s>;"
156              " ipv6 <%d>\n",
157              fspec->siaddr, filename, fspec->ciaddr, fspec->giaddr,
158              fspec->is_ipv6);
159
160      strncpy(buffer, fspec->dev, 768);
161      /* If we didn't get a ':' include one */
162      if (fspec->dev[strlen(fspec->dev)-1] != ':')
163           strcat(buffer, ":");
164
165      /* If /packages/cas exists the we have a "new skool" tftp.
166       * This means that siaddr is the tftp server and that we can add
167       * {tftp,bootp}_retrys, subnet mask and tftp block size to the load
168       * method */
169      new_tftp = (prom_finddevice("/packages/cas") != PROM_INVALID_HANDLE);
170      DEBUG_F("Using %s tftp style\n", (new_tftp? "new": "old"));
171
172      if (new_tftp) {
173           strcat(buffer, fspec->siaddr);
174           strcat(buffer, ",");
175
176           if (fspec->is_ipv6 && (strstr(filename, "filename=") == NULL))
177                strcat(buffer, "filename=");
178
179           strcat(buffer, filename);
180           strcat(buffer, ",");
181           strcat(buffer, fspec->ciaddr);
182           strcat(buffer, ",");
183           strcat(buffer, fspec->giaddr);
184           strcat(buffer, ",");
185           strcat(buffer, fspec->bootp_retries);
186           strcat(buffer, ",");
187           strcat(buffer, fspec->tftp_retries);
188           strcat(buffer, ",");
189           strcat(buffer, fspec->subnetmask);
190           strcat(buffer, ",");
191           strcat(buffer, fspec->addl_params);
192      } else {
193           strcat(buffer, ",");
194           strcat(buffer, filename);
195      }
196
197      DEBUG_F("Opening: \"%s\"\n", buffer);
198
199      file->of_device = prom_open(buffer);
200
201      DEBUG_F("file->of_device = %p\n", file->of_device);
202
203      file->pos = 0;
204      if ((file->of_device == PROM_INVALID_HANDLE) || (file->of_device == 0))
205      {
206           DEBUG_LEAVE(FILE_ERR_BAD_FSYS);
207           return FILE_ERR_BAD_FSYS;
208      }
209
210
211      file->buffer = prom_claim_chunk((void *)LOAD_BUFFER_POS,
212                                      LOAD_BUFFER_SIZE, 0);
213      if (file->buffer == (void *)-1) {
214           prom_printf("Can't claim memory for TFTP download\n");
215           prom_close(file->of_device);
216           DEBUG_LEAVE(FILE_IOERR);
217           return FILE_IOERR;
218      }
219      memset(file->buffer, 0, LOAD_BUFFER_SIZE);
220
221      DEBUG_F("TFP...\n");
222
223      file->len = prom_loadmethod(file->of_device, file->buffer);
224
225      DEBUG_F("result: %Ld\n", file->len);
226
227      DEBUG_LEAVE(FILE_ERR_OK);
228      return FILE_ERR_OK;
229 }
230
231 static int
232 of_read(struct boot_file_t* file, unsigned int size, void* buffer)
233 {
234      unsigned int count;
235
236      count = prom_read(file->of_device, buffer, size);
237      file->pos += count;
238      return count;
239 }
240
241 static int
242 of_net_read(struct boot_file_t* file, unsigned int size, void* buffer)
243 {
244      unsigned int count, av;
245
246      av = file->len - file->pos;
247      count = size > av ? av : size;
248      memcpy(buffer, file->buffer + file->pos, count);
249      file->pos += count;
250      return count;
251 }
252
253 static int
254 of_seek(struct boot_file_t* file, unsigned int newpos)
255 {
256      if (prom_seek(file->of_device, newpos)) {
257           file->pos = newpos;
258           return FILE_ERR_OK;
259      }
260
261      return FILE_CANT_SEEK;
262 }
263
264 static int
265 of_net_seek(struct boot_file_t* file, unsigned int newpos)
266 {
267      file->pos = (newpos > file->len) ? file->len : newpos;
268      return FILE_ERR_OK;
269 }
270
271 static int
272 of_close(struct boot_file_t* file)
273 {
274
275      DEBUG_ENTER;
276      DEBUG_F("<@%p>\n", file->of_device);
277
278      if (file->buffer) {
279           prom_release(file->buffer, LOAD_BUFFER_SIZE);
280      }
281      prom_close(file->of_device);
282      DEBUG_F("of_close called\n");
283
284      DEBUG_LEAVE(0);
285      return 0;
286 }
287
288 static unsigned int
289 of_net_ino_size(struct boot_file_t* file)
290 {
291         return file->len;
292 }
293
294 /*
295  * Local variables:
296  * c-file-style: "k&r"
297  * c-basic-offset: 5
298  * End:
299  */