3 Copyright (C) 1999 Benjamin Herrenschmidt
7 Copyright (C) 1996 Maurizio Plaza
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 Note: This version is way too slow due to the way we use bmap. This
25 should be replaced by an iterator.
34 #include "partition.h"
40 #define MAX_READ_RANGE 256
44 #include "linux/ext2_fs.h"
45 #include "ext2fs/ext2fs.h"
47 static int ext2_open( struct boot_file_t* file,
49 struct partition_t* part,
50 const char* file_name);
51 static int ext2_read( struct boot_file_t* file,
54 static int ext2_seek( struct boot_file_t* file,
56 static int ext2_close( struct boot_file_t* file);
58 struct fs_t ext2_filesystem =
67 /* IO manager structure for the ext2 library */
69 static errcode_t linux_open (const char *name, int flags, io_channel * channel);
70 static errcode_t linux_close (io_channel channel);
71 static errcode_t linux_set_blksize (io_channel channel, int blksize);
72 static errcode_t linux_read_blk (io_channel channel, unsigned long block, int count, void *data);
73 static errcode_t linux_write_blk (io_channel channel, unsigned long block, int count, const void *data);
74 static errcode_t linux_flush (io_channel channel);
76 static struct struct_io_manager struct_linux_manager =
78 EXT2_ET_MAGIC_IO_MANAGER,
88 static io_manager linux_io_manager = &struct_linux_manager;
90 /* Currently, we have a mess between what is in the file structure
91 * and what is stored globally here. I'll clean this up later
93 static int opened = 0; /* We can't open twice ! */
94 static unsigned int bs; /* Blocksize */
95 static unsigned long long doff; /* Byte offset where partition starts */
96 static ino_t root,cwd;
97 static ext2_filsys fs = 0;
98 static struct boot_file_t* cur_file;
99 static char *block_buffer = NULL;
102 static unsigned long read_range_start;
103 static unsigned long read_range_count;
104 static unsigned long read_last_logical;
105 static unsigned long read_total;
106 static unsigned long read_max;
107 static struct boot_file_t* read_cur_file;
108 static errcode_t read_result;
109 static char* read_buffer;
111 static int read_dump_range(void);
112 static int read_iterator(ext2_filsys fs, blk_t *blocknr, int lg_block, void *private);
113 #else /* FAST_VERSION */
114 static struct ext2_inode cur_inode;
115 #endif /* FAST_VERSION */
117 void com_err (const char *a, long i, const char *fmt,...)
119 prom_printf ((char *) fmt);
123 ext2_open( struct boot_file_t* file,
124 const char* dev_name,
125 struct partition_t* part,
126 const char* file_name)
129 int error = FILE_ERR_NOTFOUND;
130 static char buffer[1024];
137 prom_printf("ext2_open() : fs busy\n");
138 DEBUG_LEAVE(FILE_ERR_FSBUSY);
139 return FILE_ERR_FSBUSY;
141 if (file->device_kind != FILE_DEVICE_BLOCK) {
142 prom_printf("Can't open ext2 filesystem on non-block device\n");
143 DEBUG_LEAVE(FILE_ERR_BADDEV);
144 return FILE_ERR_BADDEV;
149 /* We don't care too much about the device block size since we run
150 * thru the deblocker. We may have to change that is we plan to be
151 * compatible with older versions of OF
156 doff = (unsigned long long)(part->part_start) * part->blocksize;
160 DEBUG_F("partition offset: %Lu\n", doff);
162 /* Open the OF device for the entire disk */
163 strncpy(buffer, dev_name, 1020);
164 strcat(buffer, ":0");
166 DEBUG_F("<%s>\n", buffer);
168 file->of_device = prom_open(buffer);
170 DEBUG_F("file->of_device = %p\n", file->of_device);
172 if (file->of_device == PROM_INVALID_HANDLE) {
174 DEBUG_F("Can't open device %p\n", file->of_device);
175 DEBUG_LEAVE(FILE_IOERR);
180 /* Open the ext2 filesystem */
181 result = ext2fs_open (buffer, EXT2_FLAG_RW, 0, 0, linux_io_manager, &fs);
184 if(result == EXT2_ET_BAD_MAGIC)
186 DEBUG_F( "ext2fs_open returned bad magic loading file %p\n",
191 DEBUG_F( "ext2fs_open error #%d while loading file %s\n",
194 error = FILE_ERR_BAD_FSYS;
198 /* Allocate the block buffer */
199 block_buffer = malloc(fs->blocksize * 2);
202 DEBUG_F("ext2fs: can't alloc block buffer (%d bytes)\n", fs->blocksize * 2);
207 /* Lookup file by pathname */
208 root = cwd = EXT2_ROOT_INO;
209 result = ext2fs_namei_follow(fs, root, cwd, file_name, &file->inode);
212 DEBUG_F("ext2fs_namei error #%d while loading file %s\n", result, file_name);
213 if (result == EXT2_ET_SYMLINK_LOOP)
214 error = FILE_ERR_SYMLINK_LOOP;
215 else if (result == EXT2_ET_FILE_NOT_FOUND)
216 error = FILE_ERR_NOTFOUND;
223 result = ext2fs_follow_link(fs, root, cwd, file->inode, &file->inode);
226 DEBUG_F("ext2fs_follow_link error #%d while loading file %s\n", result, file_name);
227 error = FILE_ERR_NOTFOUND;
233 result = ext2fs_read_inode(fs, file->inode, &cur_inode);
236 DEBUG_F("ext2fs_read_inode error #%d while loading file %s\n", result, file_name);
237 if (result == EXT2_ET_FILE_TOO_BIG)
238 error = FILE_ERR_LENGTH;
239 else if (result == EXT2_ET_LLSEEK_FAILED)
240 error = FILE_CANT_SEEK;
241 else if (result == EXT2_ET_FILE_NOT_FOUND)
242 error = FILE_ERR_NOTFOUND;
247 #endif /* FAST_VERSION */
257 prom_close(file->of_device);
263 DEBUG_LEAVE_F(error);
267 DEBUG_LEAVE(FILE_ERR_OK);
274 read_dump_range(void)
276 int count = read_range_count;
280 DEBUG_F(" dumping range: start: 0x%x count: 0x%x\n",
281 read_range_count, read_range_start);
283 /* Check if we need to handle a special case for the last block */
284 if ((count * bs) > read_max)
288 read_result = io_channel_read_blk(fs->io, read_range_start, count, read_buffer);
294 read_cur_file->pos += size;
295 read_range_count -= count;
296 read_range_start += count;
297 read_last_logical += count;
299 /* Handle remaining block */
300 if (read_max && read_range_count) {
301 read_result = io_channel_read_blk(fs->io, read_range_start, 1, block_buffer);
304 memcpy(read_buffer, block_buffer, read_max);
305 read_cur_file->pos += read_max;
306 read_total += read_max;
309 read_range_count = read_range_start = 0;
311 return (read_max == 0) ? BLOCK_ABORT : 0;
315 read_iterator(ext2_filsys fs, blk_t *blocknr, int lg_block, void *private)
318 DEBUG_F("read_it: p_bloc: 0x%x, l_bloc: 0x%x, f_pos: 0x%x, rng_pos: 0x%x ",
319 *blocknr, lg_block, read_cur_file->pos, read_last_logical);
323 DEBUG_F(" <skip lg>\n");
328 /* If we have not reached the start block yet, we skip */
329 if (lg_block < read_cur_file->pos / bs) {
331 DEBUG_F(" <skip pos>\n");
336 /* If block is contiguous to current range, just extend range,
337 * exit if we pass the remaining bytes count to read
339 if (read_range_start && read_range_count < MAX_READ_RANGE
340 && (*blocknr == read_range_start + read_range_count)
341 && (lg_block == read_last_logical + read_range_count)) {
343 DEBUG_F(" block in range\n");
346 return ((read_range_count * bs) >= read_max) ? BLOCK_ABORT : 0;
349 /* Range doesn't match. Dump existing range */
350 if (read_range_start) {
352 DEBUG_F(" calling dump range \n");
354 if (read_dump_range())
358 /* Here we handle holes in the file */
359 if (lg_block && lg_block != read_last_logical) {
362 DEBUG_F(" hole from lg_bloc 0x%x\n", read_last_logical);
364 if (read_cur_file->pos % bs) {
365 int offset = read_cur_file->pos % bs;
366 int size = bs - offset;
369 memset(read_buffer, 0, size);
373 read_cur_file->pos += size;
378 nzero = (lg_block - read_last_logical) * bs;
380 if (nzero > read_max)
382 memset(read_buffer, 0, nzero);
385 read_buffer += nzero;
386 read_cur_file->pos += nzero;
390 read_last_logical = lg_block;
393 /* If we are not aligned, handle that case */
394 if (read_cur_file->pos % bs) {
395 int offset = read_cur_file->pos % bs;
396 int size = bs - offset;
398 DEBUG_F(" handle unaligned start\n");
400 read_result = io_channel_read_blk(fs->io, *blocknr, 1, block_buffer);
405 memcpy(read_buffer, block_buffer + offset, size);
406 read_cur_file->pos += size;
410 read_last_logical = lg_block + 1;
411 return (read_max == 0) ? BLOCK_ABORT : 0;
414 /* If there is still a physical block to add, then create a new range */
417 DEBUG_F(" new range\n");
419 read_range_start = *blocknr;
420 read_range_count = 1;
421 return (bs >= read_max) ? BLOCK_ABORT : 0;
430 #endif /* FAST_VERSION */
433 ext2_read( struct boot_file_t* file,
444 DEBUG_F("ext_read() from pos 0x%Lx, size: 0x%ux\n", file->pos, size);
447 read_cur_file = file;
448 read_range_start = 0;
449 read_range_count = 0;
450 read_last_logical = file->pos / bs;
453 read_buffer = (unsigned char*)buffer;
456 retval = ext2fs_block_iterate(fs, file->inode, 0, 0, read_iterator, 0);
457 if (retval == BLOCK_ABORT)
458 retval = read_result;
459 if (!retval && read_range_start) {
461 DEBUG_F("on exit: range_start is 0x%x, calling dump...\n",
465 retval = read_result;
468 prom_printf ("ext2: i/o error %ld in read\n", (long) retval);
472 #else /* FAST_VERSION */
474 unsigned int read = 0;
480 DEBUG_F("ext_read() from pos 0x%x, size: 0x%x\n", file->pos, size);
484 blk_t fblock = file->pos / bs;
486 unsigned int blkorig, s, b;
489 status = ext2fs_bmap(fs, file->inode, &cur_inode,
490 block_buffer, 0, fblock, &pblock);
493 DEBUG_F("ext2fs_bmap(fblock:%d) return: %d\n", fblock, status);
496 blkorig = fblock * bs;
497 b = file->pos - blkorig;
498 s = ((bs - b) > size) ? size : (bs - b);
500 unsigned long long pos =
501 ((unsigned long long)pblock) * (unsigned long long)bs;
503 prom_lseek(file->of_device, pos);
504 status = prom_read(file->of_device, block_buffer, bs);
506 prom_printf("ext2: io error in read, ex: %d, got: %d\n",
511 memset(block_buffer, 0, bs);
513 memcpy(buffer, block_buffer + b, s);
520 #endif /* FAST_VERSION */
524 ext2_seek( struct boot_file_t* file,
528 return FILE_CANT_SEEK;
535 ext2_close( struct boot_file_t* file)
548 prom_close(file->of_device);
549 DEBUG_F("ext2_close called\n");
556 static errcode_t linux_open (const char *name, int flags, io_channel * channel)
562 return EXT2_ET_BAD_DEVICE_NAME;
563 io = (io_channel) malloc (sizeof (struct struct_io_channel));
565 return EXT2_ET_BAD_DEVICE_NAME;
566 memset (io, 0, sizeof (struct struct_io_channel));
567 io->magic = EXT2_ET_MAGIC_IO_CHANNEL;
568 io->manager = linux_io_manager;
569 io->name = (char *) malloc (strlen (name) + 1);
570 strcpy (io->name, name);
579 static errcode_t linux_close (io_channel channel)
585 static errcode_t linux_set_blksize (io_channel channel, int blksize)
587 channel->block_size = bs = blksize;
590 block_buffer = malloc(bs * 2);
595 static errcode_t linux_read_blk (io_channel channel, unsigned long block, int count, void *data)
598 unsigned long long tempb;
603 tempb = (((unsigned long long) block) *
604 ((unsigned long long)bs)) + (unsigned long long)doff;
605 size = (count < 0) ? -count : count * bs;
606 prom_lseek(cur_file->of_device, tempb);
607 if (prom_read(cur_file->of_device, data, size) != size) {
608 DEBUG_F("\nRead error on block %ld\n", block);
609 return EXT2_ET_SHORT_READ;
614 static errcode_t linux_write_blk (io_channel channel, unsigned long block, int count, const void *data)
619 static errcode_t linux_flush (io_channel channel)