blob: 39d3eae20d9d845bd9b6cc373f7285b018a55994 [file] [log] [blame]
wdenk2262cfe2002-11-18 00:14:45 +00001/*
2 * (C) Copyright 2002
Albert ARIBAUDfa82f872011-08-04 18:45:45 +02003 * Stäubli Faverges - <www.staubli.com>
wdenk2262cfe2002-11-18 00:14:45 +00004 * Pierre AUBERT p.aubert@staubli.com
5 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02006 * SPDX-License-Identifier: GPL-2.0+
wdenk2262cfe2002-11-18 00:14:45 +00007 */
8
9#include <common.h>
10#include <config.h>
11#include <malloc.h>
12
wdenk2262cfe2002-11-18 00:14:45 +000013#include "dos.h"
14#include "fdos.h"
15
16
17/*-----------------------------------------------------------------------------
wdenk8bde7f72003-06-27 21:31:46 +000018 * fill_fs -- Read info on file system
wdenk2262cfe2002-11-18 00:14:45 +000019 *-----------------------------------------------------------------------------
20 */
21static int fill_fs (BootSector_t *boot, Fs_t *fs)
22{
wdenk8bde7f72003-06-27 21:31:46 +000023
wdenk2262cfe2002-11-18 00:14:45 +000024 fs -> fat_start = __le16_to_cpu (boot -> nrsvsect);
25 fs -> fat_len = __le16_to_cpu (boot -> fatlen);
26 fs -> nb_fat = boot -> nfat;
wdenk8bde7f72003-06-27 21:31:46 +000027
wdenk2262cfe2002-11-18 00:14:45 +000028 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/*-----------------------------------------------------------------------------
wdenk8bde7f72003-06-27 21:31:46 +000037 * fs_init --
wdenk2262cfe2002-11-18 00:14:45 +000038 *-----------------------------------------------------------------------------
39 */
40int fs_init (Fs_t *fs)
41{
42 BootSector_t *boot;
43
44 /* Initialize physical device */
45 if (dev_open () < 0) {
wdenk8bde7f72003-06-27 21:31:46 +000046 PRINTF ("Unable to initialize the fdc\n");
47 return (-1);
wdenk2262cfe2002-11-18 00:14:45 +000048 }
49 init_subdir ();
wdenk8bde7f72003-06-27 21:31:46 +000050
wdenk2262cfe2002-11-18 00:14:45 +000051 /* Allocate space for read the boot sector */
52 if ((boot = (BootSector_t *)malloc (sizeof (BootSector_t))) == NULL) {
wdenk8bde7f72003-06-27 21:31:46 +000053 PRINTF ("Unable to allocate space for boot sector\n");
54 return (-1);
wdenk2262cfe2002-11-18 00:14:45 +000055 }
wdenk8bde7f72003-06-27 21:31:46 +000056
wdenk2262cfe2002-11-18 00:14:45 +000057 /* read boot sector */
58 if (dev_read (boot, 0, 1)){
wdenk8bde7f72003-06-27 21:31:46 +000059 PRINTF ("Error during boot sector read\n");
60 free (boot);
61 return (-1);
wdenk2262cfe2002-11-18 00:14:45 +000062 }
63
64 /* we verify it'a a DOS diskette */
65 if (boot -> jump [0] != JUMP_0_1 && boot -> jump [0] != JUMP_0_2) {
wdenk8bde7f72003-06-27 21:31:46 +000066 PRINTF ("Not a DOS diskette\n");
67 free (boot);
68 return (-1);
wdenk2262cfe2002-11-18 00:14:45 +000069 }
70
71 if (boot -> descr < MEDIA_STD) {
wdenk8bde7f72003-06-27 21:31:46 +000072 /* We handle only recent medias (type F0) */
73 PRINTF ("unrecognized diskette type\n");
74 free (boot);
75 return (-1);
wdenk2262cfe2002-11-18 00:14:45 +000076 }
77
78 if (check_dev (boot, fs) < 0) {
wdenk8bde7f72003-06-27 21:31:46 +000079 PRINTF ("Bad diskette\n");
80 free (boot);
81 return (-1);
wdenk2262cfe2002-11-18 00:14:45 +000082 }
wdenk2262cfe2002-11-18 00:14:45 +000083
wdenk8bde7f72003-06-27 21:31:46 +000084 if (fill_fs (boot, fs) < 0) {
85 free (boot);
86
87 return (-1);
wdenk2262cfe2002-11-18 00:14:45 +000088 }
89
90 /* Read FAT */
91 if (read_fat (boot, fs) < 0) {
wdenk8bde7f72003-06-27 21:31:46 +000092 free (boot);
93 return (-1);
wdenk2262cfe2002-11-18 00:14:45 +000094 }
95
96 free (boot);
97 return (0);
98}