wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2002 |
| 3 | * Stäubli Faverges - <www.staubli.com> |
| 4 | * Pierre AUBERT p.aubert@staubli.com |
| 5 | * |
| 6 | * See file CREDITS for list of people who contributed to this |
| 7 | * project. |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or |
| 10 | * modify it under the terms of the GNU General Public License as |
| 11 | * published by the Free Software Foundation; either version 2 of |
| 12 | * the License, or (at your option) any later version. |
| 13 | * |
| 14 | * This program is distributed in the hope that it will be useful, |
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | * GNU General Public License for more details. |
| 18 | * |
| 19 | * You should have received a copy of the GNU General Public License |
| 20 | * along with this program; if not, write to the Free Software |
| 21 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
| 22 | * MA 02111-1307 USA |
| 23 | */ |
| 24 | |
| 25 | #include <common.h> |
| 26 | #include <config.h> |
| 27 | #include <malloc.h> |
| 28 | |
| 29 | #if (CONFIG_COMMANDS & CFG_CMD_FDOS) |
| 30 | |
| 31 | #include "dos.h" |
| 32 | #include "fdos.h" |
| 33 | |
| 34 | |
| 35 | /*----------------------------------------------------------------------------- |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 36 | * fill_fs -- Read info on file system |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 37 | *----------------------------------------------------------------------------- |
| 38 | */ |
| 39 | static int fill_fs (BootSector_t *boot, Fs_t *fs) |
| 40 | { |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 41 | |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 42 | fs -> fat_start = __le16_to_cpu (boot -> nrsvsect); |
| 43 | fs -> fat_len = __le16_to_cpu (boot -> fatlen); |
| 44 | fs -> nb_fat = boot -> nfat; |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 45 | |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 46 | fs -> dir_start = fs -> fat_start + fs -> nb_fat * fs -> fat_len; |
| 47 | fs -> dir_len = __le16_to_cpu (boot -> dirents) * MDIR_SIZE / SZ_STD_SECTOR; |
| 48 | fs -> cluster_size = boot -> clsiz; |
| 49 | fs -> num_clus = (fs -> tot_sectors - fs -> dir_start - fs -> dir_len) / fs -> cluster_size; |
| 50 | |
| 51 | return (0); |
| 52 | } |
| 53 | |
| 54 | /*----------------------------------------------------------------------------- |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 55 | * fs_init -- |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 56 | *----------------------------------------------------------------------------- |
| 57 | */ |
| 58 | int fs_init (Fs_t *fs) |
| 59 | { |
| 60 | BootSector_t *boot; |
| 61 | |
| 62 | /* Initialize physical device */ |
| 63 | if (dev_open () < 0) { |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 64 | PRINTF ("Unable to initialize the fdc\n"); |
| 65 | return (-1); |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 66 | } |
| 67 | init_subdir (); |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 68 | |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 69 | /* Allocate space for read the boot sector */ |
| 70 | if ((boot = (BootSector_t *)malloc (sizeof (BootSector_t))) == NULL) { |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 71 | PRINTF ("Unable to allocate space for boot sector\n"); |
| 72 | return (-1); |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 73 | } |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 74 | |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 75 | /* read boot sector */ |
| 76 | if (dev_read (boot, 0, 1)){ |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 77 | PRINTF ("Error during boot sector read\n"); |
| 78 | free (boot); |
| 79 | return (-1); |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | /* we verify it'a a DOS diskette */ |
| 83 | if (boot -> jump [0] != JUMP_0_1 && boot -> jump [0] != JUMP_0_2) { |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 84 | PRINTF ("Not a DOS diskette\n"); |
| 85 | free (boot); |
| 86 | return (-1); |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | if (boot -> descr < MEDIA_STD) { |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 90 | /* We handle only recent medias (type F0) */ |
| 91 | PRINTF ("unrecognized diskette type\n"); |
| 92 | free (boot); |
| 93 | return (-1); |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | if (check_dev (boot, fs) < 0) { |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 97 | PRINTF ("Bad diskette\n"); |
| 98 | free (boot); |
| 99 | return (-1); |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 100 | } |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 101 | |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 102 | if (fill_fs (boot, fs) < 0) { |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 103 | free (boot); |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 104 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 105 | return (-1); |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | /* Read FAT */ |
| 109 | if (read_fat (boot, fs) < 0) { |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 110 | free (boot); |
| 111 | return (-1); |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | free (boot); |
| 115 | return (0); |
| 116 | } |
| 117 | |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 118 | #endif |