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