2 * fs_of.c - an implementation for OpenFirmware supported filesystems
4 * Copyright (C) 2001, 2002 Ethan Benson
6 * Copyright (C) 1999 Benjamin Herrenschmidt
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.
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.
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.
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
41 #include "partition.h"
42 #include "fdisk-part.h"
47 #define LOAD_BUFFER_SIZE 0x1800000
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);
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);
64 struct fs_t of_filesystem =
74 struct fs_t of_net_filesystem =
85 of_open(struct boot_file_t* file,
86 struct partition_t* part, struct boot_fspec_t* fspec)
88 static char buffer[1024];
95 strncpy(buffer, fspec->dev, 768);
98 if (part->sys_ind == LINUX_RAID || part->sys_ind == LINUX_NATIVE) {
99 DEBUG_F("skipping because partition is tagged %08x\n",
101 DEBUG_LEAVE(FILE_ERR_BAD_FSYS);
102 return FILE_ERR_BAD_FSYS;
105 sprintf(pn, "%02d", part->part_number);
108 if (fspec->file && strlen(fspec->file)) {
111 filename = strdup(fspec->file);
112 for (p = filename; *p; p++)
115 strcat(buffer, filename);
119 DEBUG_F("opening: \"%s\"\n", buffer);
121 file->of_device = prom_open(buffer);
123 DEBUG_F("file->of_device = %p\n", file->of_device);
127 file->devspec_cache = strdup(buffer);
129 if ((file->of_device == PROM_INVALID_HANDLE) || (file->of_device == 0))
131 DEBUG_LEAVE(FILE_ERR_BAD_FSYS);
132 return FILE_ERR_BAD_FSYS;
135 DEBUG_LEAVE(FILE_ERR_OK);
140 of_net_open(struct boot_file_t* file,
141 struct partition_t* part, struct boot_fspec_t* fspec)
143 static char buffer[1024];
144 char *filename = NULL;
151 if (fspec->file && strlen(fspec->file)) {
152 filename = strdup(fspec->file);
153 for (p = filename; *p; p++)
158 DEBUG_F("siaddr <%s>; filename <%s>; ciaddr <%s>; giaddr <%s>;"
159 " ipv6 <%d>; vtag <%s>\n",
160 fspec->siaddr, filename, fspec->ciaddr, fspec->giaddr,
161 fspec->is_ipv6, fspec->vtag);
163 strncpy(buffer, fspec->dev, 768);
164 /* If we didn't get a ':' include one */
165 if (fspec->dev[strlen(fspec->dev)-1] != ':')
168 /* If /packages/cas exists the we have a "new skool" tftp.
169 * This means that siaddr is the tftp server and that we can add
170 * {tftp,bootp}_retrys, subnet mask and tftp block size to the load
172 new_tftp = (prom_finddevice("/packages/cas") != PROM_INVALID_HANDLE);
173 DEBUG_F("Using %s tftp style\n", (new_tftp? "new": "old"));
177 strcat(buffer, "ipv6,");
180 strcat(buffer, fspec->vtag);
183 strcat(buffer, fspec->siaddr);
186 if (fspec->is_ipv6 && (strstr(filename, "filename=") == NULL))
187 strcat(buffer, "filename=");
189 strcat(buffer, filename);
191 strcat(buffer, fspec->ciaddr);
193 strcat(buffer, fspec->giaddr);
195 strcat(buffer, fspec->bootp_retries);
197 strcat(buffer, fspec->tftp_retries);
199 strcat(buffer, fspec->subnetmask);
201 strcat(buffer, fspec->addl_params);
204 strcat(buffer, filename);
207 DEBUG_F("Opening: \"%s\"\n", buffer);
209 file->of_device = prom_open(buffer);
211 DEBUG_F("file->of_device = %p\n", file->of_device);
214 if ((file->of_device == PROM_INVALID_HANDLE) || (file->of_device == 0))
216 DEBUG_LEAVE(FILE_ERR_BAD_FSYS);
217 return FILE_ERR_BAD_FSYS;
221 file->buffer = prom_claim_chunk_top(LOAD_BUFFER_SIZE, 0);
222 if (file->buffer == (void *)-1) {
223 prom_printf("Can't claim memory for TFTP download\n");
224 prom_close(file->of_device);
225 DEBUG_LEAVE(FILE_IOERR);
228 memset(file->buffer, 0, LOAD_BUFFER_SIZE);
232 file->len = prom_loadmethod(file->of_device, file->buffer);
234 DEBUG_F("result: %Ld\n", file->len);
236 DEBUG_LEAVE(FILE_ERR_OK);
241 of_read(struct boot_file_t* file, unsigned int size, void* buffer)
245 count = prom_read(file->of_device, buffer, size);
251 of_net_read(struct boot_file_t* file, unsigned int size, void* buffer)
253 unsigned int count, av;
255 av = file->len - file->pos;
256 count = size > av ? av : size;
257 memcpy(buffer, file->buffer + file->pos, count);
263 of_seek(struct boot_file_t* file, unsigned int newpos)
265 if (prom_seek(file->of_device, newpos)) {
270 return FILE_CANT_SEEK;
274 of_net_seek(struct boot_file_t* file, unsigned int newpos)
276 file->pos = (newpos > file->len) ? file->len : newpos;
281 of_close(struct boot_file_t* file)
285 DEBUG_F("<@%p>\n", file->of_device);
288 prom_release(file->buffer, LOAD_BUFFER_SIZE);
290 prom_close(file->of_device);
291 DEBUG_F("of_close called\n");
298 of_ino_size(struct boot_file_t* file, unsigned int *size)
300 static char buffer[1<<20];
303 if (file->len == 0) {
304 DEBUG_F("Estimating size of: %p\n", file->of_device);
305 while (prom_read(file->of_device, (void *)&buffer, sizeof(buffer))
308 DEBUG_F("read_count == %d\n", read_count);
311 file->len = read_count * sizeof(buffer);
313 * prom_seek(file->of_device, file->pos);
315 prom_close(file->of_device);
316 DEBUG_F("Re-Opening: \"%s\"\n", file->devspec_cache);
317 file->of_device = prom_open(file->devspec_cache);
318 DEBUG_F("file->of_device = %p\n", file->of_device);
319 if (file->of_device == 0) {
325 DEBUG_F("Estimated size is: %Lu(%d)\n", (unsigned long long)file->len,
332 of_net_ino_size(struct boot_file_t* file, unsigned int *size)
340 * c-file-style: "k&r"