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"
46 #define LOAD_BUFFER_POS 0x600000
47 #define LOAD_BUFFER_SIZE 0x400000
49 static int of_open(struct boot_file_t* file, const char* dev_name,
50 struct partition_t* part, const char* file_name);
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);
56 static int of_net_open(struct boot_file_t* file, const char* dev_name,
57 struct partition_t* part, const char* file_name);
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);
62 struct fs_t of_filesystem =
71 struct fs_t of_net_filesystem =
81 of_open(struct boot_file_t* file, const char* dev_name,
82 struct partition_t* part, const char* file_name)
84 static char buffer[1024];
91 strncpy(buffer, dev_name, 768);
95 sprintf(pn, "%02d", part->part_number);
98 if (file_name && strlen(file_name)) {
101 filename = strdup(file_name);
102 for (p = filename; *p; p++)
105 strcat(buffer, filename);
109 DEBUG_F("opening: \"%s\"\n", buffer);
111 file->of_device = prom_open(buffer);
113 DEBUG_F("file->of_device = %p\n", file->of_device);
117 if ((file->of_device == PROM_INVALID_HANDLE) || (file->of_device == 0))
119 DEBUG_LEAVE(FILE_ERR_BAD_FSYS);
120 return FILE_ERR_BAD_FSYS;
123 DEBUG_LEAVE(FILE_ERR_OK);
128 of_net_open(struct boot_file_t* file, const char* dev_name,
129 struct partition_t* part, const char* file_name)
131 static char buffer[1024];
138 strncpy(buffer, dev_name, 768);
139 if (file_name && strlen(file_name)) {
141 filename = strdup(file_name);
142 for (p = filename; *p; p++)
145 strcat(buffer, filename);
149 DEBUG_F("Opening: \"%s\"\n", buffer);
151 file->of_device = prom_open(buffer);
153 DEBUG_F("file->of_device = %p\n", file->of_device);
156 if ((file->of_device == PROM_INVALID_HANDLE) || (file->of_device == 0))
158 DEBUG_LEAVE(FILE_ERR_BAD_FSYS);
159 return FILE_ERR_BAD_FSYS;
162 file->buffer = prom_claim((void *)LOAD_BUFFER_POS, LOAD_BUFFER_SIZE, 0);
163 if (file->buffer == (void *)-1) {
164 prom_printf("Can't claim memory for TFTP download\n");
165 prom_close(file->of_device);
166 DEBUG_LEAVE(FILE_IOERR);
169 memset(file->buffer, 0, LOAD_BUFFER_SIZE);
173 file->len = prom_loadmethod(file->of_device, file->buffer);
175 DEBUG_F("result: %Ld\n", file->len);
177 DEBUG_LEAVE(FILE_ERR_OK);
182 of_read(struct boot_file_t* file, unsigned int size, void* buffer)
186 count = prom_read(file->of_device, buffer, size);
192 of_net_read(struct boot_file_t* file, unsigned int size, void* buffer)
194 unsigned int count, av;
196 av = file->len - file->pos;
197 count = size > av ? av : size;
198 memcpy(buffer, file->buffer + file->pos, count);
204 of_seek(struct boot_file_t* file, unsigned int newpos)
206 if (prom_seek(file->of_device, newpos)) {
211 return FILE_CANT_SEEK;
215 of_net_seek(struct boot_file_t* file, unsigned int newpos)
217 file->pos = (newpos > file->len) ? file->len : newpos;
222 of_close(struct boot_file_t* file)
226 DEBUG_F("<@%p>\n", file->of_device);
229 prom_release(file->buffer, LOAD_BUFFER_SIZE);
231 prom_close(file->of_device);
232 DEBUG_F("of_close called\n");
240 * c-file-style: "k&r"