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