17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  *  GRUB  --  GRand Unified Bootloader
37c478bd9Sstevel@tonic-gate  *  Copyright (C) 2001   Free Software Foundation, Inc.
47c478bd9Sstevel@tonic-gate  *
57c478bd9Sstevel@tonic-gate  *  This program is free software; you can redistribute it and/or modify
67c478bd9Sstevel@tonic-gate  *  it under the terms of the GNU General Public License as published by
77c478bd9Sstevel@tonic-gate  *  the Free Software Foundation; either version 2 of the License, or
87c478bd9Sstevel@tonic-gate  *  (at your option) any later version.
97c478bd9Sstevel@tonic-gate  *
107c478bd9Sstevel@tonic-gate  *  This program is distributed in the hope that it will be useful,
117c478bd9Sstevel@tonic-gate  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
127c478bd9Sstevel@tonic-gate  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
137c478bd9Sstevel@tonic-gate  *  GNU General Public License for more details.
147c478bd9Sstevel@tonic-gate  *
157c478bd9Sstevel@tonic-gate  *  You should have received a copy of the GNU General Public License
167c478bd9Sstevel@tonic-gate  *  along with this program; if not, write to the Free Software
177c478bd9Sstevel@tonic-gate  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
187c478bd9Sstevel@tonic-gate  */
197c478bd9Sstevel@tonic-gate 
207c478bd9Sstevel@tonic-gate #ifdef FSYS_VSTAFS
217c478bd9Sstevel@tonic-gate 
227c478bd9Sstevel@tonic-gate #include "shared.h"
237c478bd9Sstevel@tonic-gate #include "filesys.h"
247c478bd9Sstevel@tonic-gate #include "vstafs.h"
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate static void get_file_info (int sector);
287c478bd9Sstevel@tonic-gate static struct dir_entry *vstafs_readdir (long sector);
297c478bd9Sstevel@tonic-gate static struct dir_entry *vstafs_nextdir (void);
307c478bd9Sstevel@tonic-gate 
317c478bd9Sstevel@tonic-gate 
327c478bd9Sstevel@tonic-gate #define FIRST_SECTOR	((struct first_sector *) FSYS_BUF)
337c478bd9Sstevel@tonic-gate #define FILE_INFO	((struct fs_file *) (int) FIRST_SECTOR + 8192)
347c478bd9Sstevel@tonic-gate #define DIRECTORY_BUF	((struct dir_entry *) (int) FILE_INFO + 512)
357c478bd9Sstevel@tonic-gate 
367c478bd9Sstevel@tonic-gate #define ROOT_SECTOR	1
377c478bd9Sstevel@tonic-gate 
387c478bd9Sstevel@tonic-gate /*
397c478bd9Sstevel@tonic-gate  * In f_sector we store the sector number in which the information about
407c478bd9Sstevel@tonic-gate  * the found file is.
417c478bd9Sstevel@tonic-gate  */
427c478bd9Sstevel@tonic-gate extern int filepos;
437c478bd9Sstevel@tonic-gate static int f_sector;
447c478bd9Sstevel@tonic-gate 
457c478bd9Sstevel@tonic-gate int
vstafs_mount(void)467c478bd9Sstevel@tonic-gate vstafs_mount (void)
477c478bd9Sstevel@tonic-gate {
487c478bd9Sstevel@tonic-gate   int retval = 1;
497c478bd9Sstevel@tonic-gate 
507c478bd9Sstevel@tonic-gate   if( (((current_drive & 0x80) || (current_slice != 0))
517c478bd9Sstevel@tonic-gate        && current_slice != PC_SLICE_TYPE_VSTAFS)
527c478bd9Sstevel@tonic-gate       ||  ! devread (0, 0, BLOCK_SIZE, (char *) FSYS_BUF)
537c478bd9Sstevel@tonic-gate       ||  FIRST_SECTOR->fs_magic != 0xDEADFACE)
547c478bd9Sstevel@tonic-gate     retval = 0;
557c478bd9Sstevel@tonic-gate 
567c478bd9Sstevel@tonic-gate   return retval;
577c478bd9Sstevel@tonic-gate }
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate static void
get_file_info(int sector)607c478bd9Sstevel@tonic-gate get_file_info (int sector)
617c478bd9Sstevel@tonic-gate {
627c478bd9Sstevel@tonic-gate   devread (sector, 0, BLOCK_SIZE, (char *) FILE_INFO);
637c478bd9Sstevel@tonic-gate }
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate static int curr_ext, current_direntry, current_blockpos;
667c478bd9Sstevel@tonic-gate static struct alloc *a;
677c478bd9Sstevel@tonic-gate 
687c478bd9Sstevel@tonic-gate static struct dir_entry *
vstafs_readdir(long sector)697c478bd9Sstevel@tonic-gate vstafs_readdir (long sector)
707c478bd9Sstevel@tonic-gate {
717c478bd9Sstevel@tonic-gate   /*
727c478bd9Sstevel@tonic-gate    * Get some information from the current directory
737c478bd9Sstevel@tonic-gate    */
747c478bd9Sstevel@tonic-gate   get_file_info (sector);
757c478bd9Sstevel@tonic-gate   if (FILE_INFO->type != 2)
767c478bd9Sstevel@tonic-gate     {
777c478bd9Sstevel@tonic-gate       errnum = ERR_FILE_NOT_FOUND;
787c478bd9Sstevel@tonic-gate       return 0;
797c478bd9Sstevel@tonic-gate     }
807c478bd9Sstevel@tonic-gate 
817c478bd9Sstevel@tonic-gate   a = FILE_INFO->blocks;
827c478bd9Sstevel@tonic-gate   curr_ext = 0;
837c478bd9Sstevel@tonic-gate   devread (a[curr_ext].a_start, 0, 512, (char *) DIRECTORY_BUF);
847c478bd9Sstevel@tonic-gate   current_direntry = 11;
857c478bd9Sstevel@tonic-gate   current_blockpos = 0;
867c478bd9Sstevel@tonic-gate 
877c478bd9Sstevel@tonic-gate   return &DIRECTORY_BUF[10];
887c478bd9Sstevel@tonic-gate }
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate static struct dir_entry *
vstafs_nextdir(void)917c478bd9Sstevel@tonic-gate vstafs_nextdir (void)
927c478bd9Sstevel@tonic-gate {
937c478bd9Sstevel@tonic-gate   if (current_direntry > 15)
947c478bd9Sstevel@tonic-gate     {
957c478bd9Sstevel@tonic-gate       current_direntry = 0;
967c478bd9Sstevel@tonic-gate       if (++current_blockpos > (a[curr_ext].a_len - 1))
977c478bd9Sstevel@tonic-gate 	{
987c478bd9Sstevel@tonic-gate 	  current_blockpos = 0;
997c478bd9Sstevel@tonic-gate 	  curr_ext++;
1007c478bd9Sstevel@tonic-gate 	}
1017c478bd9Sstevel@tonic-gate 
1027c478bd9Sstevel@tonic-gate       if (curr_ext < FILE_INFO->extents)
1037c478bd9Sstevel@tonic-gate 	{
1047c478bd9Sstevel@tonic-gate 	  devread (a[curr_ext].a_start + current_blockpos, 0,
1057c478bd9Sstevel@tonic-gate 		   512, (char *) DIRECTORY_BUF);
1067c478bd9Sstevel@tonic-gate 	}
1077c478bd9Sstevel@tonic-gate       else
1087c478bd9Sstevel@tonic-gate 	{
1097c478bd9Sstevel@tonic-gate 	  /* errnum =ERR_FILE_NOT_FOUND; */
1107c478bd9Sstevel@tonic-gate 	  return 0;
1117c478bd9Sstevel@tonic-gate 	}
1127c478bd9Sstevel@tonic-gate     }
1137c478bd9Sstevel@tonic-gate 
1147c478bd9Sstevel@tonic-gate   return &DIRECTORY_BUF[current_direntry++];
1157c478bd9Sstevel@tonic-gate }
1167c478bd9Sstevel@tonic-gate 
1177c478bd9Sstevel@tonic-gate int
vstafs_dir(char * dirname)1187c478bd9Sstevel@tonic-gate vstafs_dir (char *dirname)
1197c478bd9Sstevel@tonic-gate {
1207c478bd9Sstevel@tonic-gate   char *fn, ch;
1217c478bd9Sstevel@tonic-gate   struct dir_entry *d;
1227c478bd9Sstevel@tonic-gate   /* int l, i, s; */
1237c478bd9Sstevel@tonic-gate 
1247c478bd9Sstevel@tonic-gate   /*
1257c478bd9Sstevel@tonic-gate    * Read in the entries of the current directory.
1267c478bd9Sstevel@tonic-gate    */
1277c478bd9Sstevel@tonic-gate   f_sector = ROOT_SECTOR;
1287c478bd9Sstevel@tonic-gate   do
1297c478bd9Sstevel@tonic-gate     {
1307c478bd9Sstevel@tonic-gate       if (! (d = vstafs_readdir (f_sector)))
1317c478bd9Sstevel@tonic-gate 	{
1327c478bd9Sstevel@tonic-gate 	  return 0;
1337c478bd9Sstevel@tonic-gate 	}
1347c478bd9Sstevel@tonic-gate 
1357c478bd9Sstevel@tonic-gate       /*
1367c478bd9Sstevel@tonic-gate        * Find the file in the path
1377c478bd9Sstevel@tonic-gate        */
1387c478bd9Sstevel@tonic-gate       while (*dirname == '/') dirname++;
1397c478bd9Sstevel@tonic-gate       fn = dirname;
1407c478bd9Sstevel@tonic-gate       while ((ch = *fn) && ch != '/' && ! isspace (ch)) fn++;
1417c478bd9Sstevel@tonic-gate       *fn = 0;
1427c478bd9Sstevel@tonic-gate 
1437c478bd9Sstevel@tonic-gate       do
1447c478bd9Sstevel@tonic-gate 	{
1457c478bd9Sstevel@tonic-gate 	  if (d->name[0] == 0 || d->name[0] & 0x80)
1467c478bd9Sstevel@tonic-gate 	    continue;
1477c478bd9Sstevel@tonic-gate 
1487c478bd9Sstevel@tonic-gate #ifndef STAGE1_5
1497c478bd9Sstevel@tonic-gate 	  if (print_possibilities && ch != '/'
1507c478bd9Sstevel@tonic-gate 	      && (! *dirname || strcmp (dirname, d->name) <= 0))
1517c478bd9Sstevel@tonic-gate 	    {
1527c478bd9Sstevel@tonic-gate 	      if (print_possibilities > 0)
1537c478bd9Sstevel@tonic-gate 		print_possibilities = -print_possibilities;
1547c478bd9Sstevel@tonic-gate 
1557c478bd9Sstevel@tonic-gate 	      printf ("  %s", d->name);
1567c478bd9Sstevel@tonic-gate 	    }
1577c478bd9Sstevel@tonic-gate #endif
1587c478bd9Sstevel@tonic-gate 	  if (! grub_strcmp (dirname, d->name))
1597c478bd9Sstevel@tonic-gate 	    {
1607c478bd9Sstevel@tonic-gate 	      f_sector = d->start;
1617c478bd9Sstevel@tonic-gate 	      get_file_info (f_sector);
1627c478bd9Sstevel@tonic-gate 	      filemax = FILE_INFO->len;
1637c478bd9Sstevel@tonic-gate 	      break;
1647c478bd9Sstevel@tonic-gate 	    }
1657c478bd9Sstevel@tonic-gate 	}
1667c478bd9Sstevel@tonic-gate       while ((d =vstafs_nextdir ()));
1677c478bd9Sstevel@tonic-gate 
1687c478bd9Sstevel@tonic-gate       *(dirname = fn) = ch;
1697c478bd9Sstevel@tonic-gate       if (! d)
1707c478bd9Sstevel@tonic-gate 	{
1717c478bd9Sstevel@tonic-gate 	  if (print_possibilities < 0)
1727c478bd9Sstevel@tonic-gate 	    {
1737c478bd9Sstevel@tonic-gate 	      putchar ('\n');
1747c478bd9Sstevel@tonic-gate 	      return 1;
1757c478bd9Sstevel@tonic-gate 	    }
1767c478bd9Sstevel@tonic-gate 
1777c478bd9Sstevel@tonic-gate 	  errnum = ERR_FILE_NOT_FOUND;
1787c478bd9Sstevel@tonic-gate 	  return 0;
1797c478bd9Sstevel@tonic-gate 	}
1807c478bd9Sstevel@tonic-gate     }
1817c478bd9Sstevel@tonic-gate   while (*dirname && ! isspace (ch));
1827c478bd9Sstevel@tonic-gate 
1837c478bd9Sstevel@tonic-gate   return 1;
1847c478bd9Sstevel@tonic-gate }
1857c478bd9Sstevel@tonic-gate 
1867c478bd9Sstevel@tonic-gate int
vstafs_read(char * addr,int len)1877c478bd9Sstevel@tonic-gate vstafs_read (char *addr, int len)
1887c478bd9Sstevel@tonic-gate {
1897c478bd9Sstevel@tonic-gate   struct alloc *a;
1907c478bd9Sstevel@tonic-gate   int size, ret = 0, offset, curr_len = 0;
1917c478bd9Sstevel@tonic-gate   int curr_ext;
1927c478bd9Sstevel@tonic-gate   char extent;
1937c478bd9Sstevel@tonic-gate   int ext_size;
1947c478bd9Sstevel@tonic-gate   char *curr_pos;
1957c478bd9Sstevel@tonic-gate 
1967c478bd9Sstevel@tonic-gate   get_file_info (f_sector);
1977c478bd9Sstevel@tonic-gate   size = FILE_INFO->len-VSTAFS_START_DATA;
1987c478bd9Sstevel@tonic-gate   a = FILE_INFO->blocks;
1997c478bd9Sstevel@tonic-gate 
2007c478bd9Sstevel@tonic-gate   if (filepos > 0)
2017c478bd9Sstevel@tonic-gate     {
2027c478bd9Sstevel@tonic-gate       if (filepos < a[0].a_len * 512 - VSTAFS_START_DATA)
2037c478bd9Sstevel@tonic-gate 	{
2047c478bd9Sstevel@tonic-gate 	  offset = filepos + VSTAFS_START_DATA;
2057c478bd9Sstevel@tonic-gate 	  extent = 0;
2067c478bd9Sstevel@tonic-gate 	  curr_len = a[0].a_len * 512 - offset - filepos;
2077c478bd9Sstevel@tonic-gate 	}
2087c478bd9Sstevel@tonic-gate       else
2097c478bd9Sstevel@tonic-gate 	{
2107c478bd9Sstevel@tonic-gate 	  ext_size = a[0].a_len * 512 - VSTAFS_START_DATA;
2117c478bd9Sstevel@tonic-gate 	  offset = filepos - ext_size;
2127c478bd9Sstevel@tonic-gate 	  extent = 1;
2137c478bd9Sstevel@tonic-gate 	  do
2147c478bd9Sstevel@tonic-gate 	    {
2157c478bd9Sstevel@tonic-gate 	      curr_len -= ext_size;
2167c478bd9Sstevel@tonic-gate 	      offset -= ext_size;
2177c478bd9Sstevel@tonic-gate 	      ext_size = a[extent+1].a_len * 512;
2187c478bd9Sstevel@tonic-gate 	    }
2197c478bd9Sstevel@tonic-gate 	  while (extent < FILE_INFO->extents && offset>ext_size);
2207c478bd9Sstevel@tonic-gate 	}
2217c478bd9Sstevel@tonic-gate     }
2227c478bd9Sstevel@tonic-gate   else
2237c478bd9Sstevel@tonic-gate     {
2247c478bd9Sstevel@tonic-gate       offset = VSTAFS_START_DATA;
2257c478bd9Sstevel@tonic-gate       extent = 0;
2267c478bd9Sstevel@tonic-gate       curr_len = a[0].a_len * 512 - offset;
2277c478bd9Sstevel@tonic-gate     }
2287c478bd9Sstevel@tonic-gate 
2297c478bd9Sstevel@tonic-gate   curr_pos = addr;
2307c478bd9Sstevel@tonic-gate   if (curr_len > len)
2317c478bd9Sstevel@tonic-gate     curr_len = len;
2327c478bd9Sstevel@tonic-gate 
2337c478bd9Sstevel@tonic-gate   for (curr_ext=extent;
2347c478bd9Sstevel@tonic-gate        curr_ext < FILE_INFO->extents;
2357c478bd9Sstevel@tonic-gate        curr_len = a[curr_ext].a_len * 512, curr_pos += curr_len, curr_ext++)
2367c478bd9Sstevel@tonic-gate     {
2377c478bd9Sstevel@tonic-gate       ret += curr_len;
2387c478bd9Sstevel@tonic-gate       size -= curr_len;
2397c478bd9Sstevel@tonic-gate       if (size < 0)
2407c478bd9Sstevel@tonic-gate 	{
2417c478bd9Sstevel@tonic-gate 	  ret += size;
2427c478bd9Sstevel@tonic-gate 	  curr_len += size;
2437c478bd9Sstevel@tonic-gate 	}
2447c478bd9Sstevel@tonic-gate 
2457c478bd9Sstevel@tonic-gate       devread (a[curr_ext].a_start,offset, curr_len, curr_pos);
2467c478bd9Sstevel@tonic-gate       offset = 0;
2477c478bd9Sstevel@tonic-gate     }
2487c478bd9Sstevel@tonic-gate 
2497c478bd9Sstevel@tonic-gate   return ret;
2507c478bd9Sstevel@tonic-gate }
2517c478bd9Sstevel@tonic-gate 
2527c478bd9Sstevel@tonic-gate #endif /* FSYS_VSTAFS */
253