wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2002 |
Albert ARIBAUD | 60fbc8d | 2011-08-04 18:45:45 +0200 | [diff] [blame] | 3 | * Stäubli Faverges - <www.staubli.com> |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 4 | * Pierre AUBERT p.aubert@staubli.com |
| 5 | * |
Wolfgang Denk | d79de1d | 2013-07-08 09:37:19 +0200 | [diff] [blame^] | 6 | * SPDX-License-Identifier: GPL-2.0+ |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | #include <common.h> |
| 10 | #include <config.h> |
| 11 | #include <malloc.h> |
| 12 | |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 13 | #include "dos.h" |
| 14 | #include "fdos.h" |
| 15 | |
| 16 | |
| 17 | /*----------------------------------------------------------------------------- |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 18 | * fill_fs -- Read info on file system |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 19 | *----------------------------------------------------------------------------- |
| 20 | */ |
| 21 | static int fill_fs (BootSector_t *boot, Fs_t *fs) |
| 22 | { |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 23 | |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 24 | fs -> fat_start = __le16_to_cpu (boot -> nrsvsect); |
| 25 | fs -> fat_len = __le16_to_cpu (boot -> fatlen); |
| 26 | fs -> nb_fat = boot -> nfat; |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 27 | |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 28 | fs -> dir_start = fs -> fat_start + fs -> nb_fat * fs -> fat_len; |
| 29 | fs -> dir_len = __le16_to_cpu (boot -> dirents) * MDIR_SIZE / SZ_STD_SECTOR; |
| 30 | fs -> cluster_size = boot -> clsiz; |
| 31 | fs -> num_clus = (fs -> tot_sectors - fs -> dir_start - fs -> dir_len) / fs -> cluster_size; |
| 32 | |
| 33 | return (0); |
| 34 | } |
| 35 | |
| 36 | /*----------------------------------------------------------------------------- |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 37 | * fs_init -- |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 38 | *----------------------------------------------------------------------------- |
| 39 | */ |
| 40 | int fs_init (Fs_t *fs) |
| 41 | { |
| 42 | BootSector_t *boot; |
| 43 | |
| 44 | /* Initialize physical device */ |
| 45 | if (dev_open () < 0) { |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 46 | PRINTF ("Unable to initialize the fdc\n"); |
| 47 | return (-1); |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 48 | } |
| 49 | init_subdir (); |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 50 | |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 51 | /* Allocate space for read the boot sector */ |
| 52 | if ((boot = (BootSector_t *)malloc (sizeof (BootSector_t))) == NULL) { |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 53 | PRINTF ("Unable to allocate space for boot sector\n"); |
| 54 | return (-1); |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 55 | } |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 56 | |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 57 | /* read boot sector */ |
| 58 | if (dev_read (boot, 0, 1)){ |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 59 | PRINTF ("Error during boot sector read\n"); |
| 60 | free (boot); |
| 61 | return (-1); |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | /* we verify it'a a DOS diskette */ |
| 65 | if (boot -> jump [0] != JUMP_0_1 && boot -> jump [0] != JUMP_0_2) { |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 66 | PRINTF ("Not a DOS diskette\n"); |
| 67 | free (boot); |
| 68 | return (-1); |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | if (boot -> descr < MEDIA_STD) { |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 72 | /* We handle only recent medias (type F0) */ |
| 73 | PRINTF ("unrecognized diskette type\n"); |
| 74 | free (boot); |
| 75 | return (-1); |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | if (check_dev (boot, fs) < 0) { |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 79 | PRINTF ("Bad diskette\n"); |
| 80 | free (boot); |
| 81 | return (-1); |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 82 | } |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 83 | |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 84 | if (fill_fs (boot, fs) < 0) { |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 85 | free (boot); |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 86 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 87 | return (-1); |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | /* Read FAT */ |
| 91 | if (read_fat (boot, fs) < 0) { |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 92 | free (boot); |
| 93 | return (-1); |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | free (boot); |
| 97 | return (0); |
| 98 | } |