1 /*
2  *   Creation Date: <2001/05/06 22:27:09 samuel>
3  *   Time-stamp: <2003/12/12 02:24:56 samuel>
4  *
5  *	<fs.c>
6  *
7  *     	I/O API used by the filesystem code
8  *
9  *   Copyright (C) 2001, 2002, 2003 Samuel Rydh (samuel@ibrium.se)
10  *
11  *   This program is free software; you can redistribute it and/or
12  *   modify it under the terms of the GNU General Public License
13  *   as published by the Free Software Foundation
14  *
15  */
16 
17 #include "config.h"
18 #include "libopenbios/bindings.h"
19 #include "fs/fs.h"
20 #include "libc/diskio.h"
21 #include "os.h"
22 #include "hfs_mdb.h"
23 
24 /************************************************************************/
25 /*	functionsions used by the various filesystems			*/
26 /************************************************************************/
27 
28 char *
get_hfs_vol_name(int fd,char * buf,int size)29 get_hfs_vol_name( int fd, char *buf, int size )
30 {
31 	char sect[512];
32 	hfs_mdb_t *mdb = (hfs_mdb_t*)&sect;
33 
34 	seek_io( fd, 0x400 );
35 	read_io( fd, sect, sizeof(sect) );
36 	if( hfs_get_ushort(mdb->drSigWord) == HFS_SIGNATURE ) {
37 		unsigned int n = mdb->drVN[0];
38 		if( n >= size )
39 			n = size - 1;
40 		memcpy( buf, &mdb->drVN[1], n );
41 		buf[n] = 0;
42 	} else if( hfs_get_ushort(mdb->drSigWord) == HFS_PLUS_SIGNATURE ) {
43 		strncpy( buf, "Unembedded HFS+", size );
44 	} else {
45 		strncpy( buf, "Error", size );
46 	}
47 	return buf;
48 }
49 
50 unsigned long
os_read(int fd,void * buf,unsigned long len,int blksize_bits)51 os_read( int fd, void *buf, unsigned long len, int blksize_bits )
52 {
53 	/* printk("os_read %d\n", (int)len); */
54 
55 	int cnt = read_io( fd, buf, len << blksize_bits );
56 	return (cnt > 0)? (cnt >> blksize_bits) : cnt;
57 }
58 
59 unsigned long
os_seek(int fd,unsigned long blknum,int blksize_bits)60 os_seek( int fd, unsigned long blknum, int blksize_bits )
61 {
62 	/* printk("os_seek %d\n", blknum ); */
63 	long long offs = (long long)blknum << blksize_bits;
64 
65 	/* offset == -1 means seek to EOF */
66 	if( (int)blknum == -1 )
67 		offs = -1;
68 
69 	if( seek_io(fd, offs) ) {
70 		/* printk("os_seek failure\n"); */
71 		return (unsigned long)-1;
72 	}
73 
74 	if( (int)blknum == -1 ) {
75 		if( (offs=tell(fd)) < 0 )
76 			return -1;
77 		blknum = offs >> blksize_bits;
78 	}
79 	return blknum;
80 }
81 
82 void
os_seek_offset(int fd,long long offset)83 os_seek_offset( int fd, long long offset )
84 {
85 	seek_io(fd, offset);
86 }
87 
88 int
os_same(int fd1,int fd2)89 os_same( int fd1, int fd2 )
90 {
91 	return fd1 == fd2;
92 }
93