blob: 39d3eae20d9d845bd9b6cc373f7285b018a55994 [file] [log] [blame]
wdenk591dda52002-11-18 00:14:45 +00001/*
2 * (C) Copyright 2002
Albert ARIBAUD60fbc8d2011-08-04 18:45:45 +02003 * Stäubli Faverges - <www.staubli.com>
wdenk591dda52002-11-18 00:14:45 +00004 * Pierre AUBERT p.aubert@staubli.com
5 *
Wolfgang Denkd79de1d2013-07-08 09:37:19 +02006 * SPDX-License-Identifier: GPL-2.0+
wdenk591dda52002-11-18 00:14:45 +00007 */
8
9#include <common.h>
10#include <config.h>
11#include <malloc.h>
12
wdenk591dda52002-11-18 00:14:45 +000013#include "dos.h"
14#include "fdos.h"
15
16
17/*-----------------------------------------------------------------------------
wdenk57b2d802003-06-27 21:31:46 +000018 * fill_fs -- Read info on file system
wdenk591dda52002-11-18 00:14:45 +000019 *-----------------------------------------------------------------------------
20 */
21static int fill_fs (BootSector_t *boot, Fs_t *fs)
22{
wdenk57b2d802003-06-27 21:31:46 +000023
wdenk591dda52002-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;
wdenk57b2d802003-06-27 21:31:46 +000027
wdenk591dda52002-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/*-----------------------------------------------------------------------------
wdenk57b2d802003-06-27 21:31:46 +000037 * fs_init --
wdenk591dda52002-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) {
wdenk57b2d802003-06-27 21:31:46 +000046 PRINTF ("Unable to initialize the fdc\n");
47 return (-1);
wdenk591dda52002-11-18 00:14:45 +000048 }
49 init_subdir ();
wdenk57b2d802003-06-27 21:31:46 +000050
wdenk591dda52002-11-18 00:14:45 +000051 /* Allocate space for read the boot sector */
52 if ((boot = (BootSector_t *)malloc (sizeof (BootSector_t))) == NULL) {
wdenk57b2d802003-06-27 21:31:46 +000053 PRINTF ("Unable to allocate space for boot sector\n");
54 return (-1);
wdenk591dda52002-11-18 00:14:45 +000055 }
wdenk57b2d802003-06-27 21:31:46 +000056
wdenk591dda52002-11-18 00:14:45 +000057 /* read boot sector */
58 if (dev_read (boot, 0, 1)){
wdenk57b2d802003-06-27 21:31:46 +000059 PRINTF ("Error during boot sector read\n");
60 free (boot);
61 return (-1);
wdenk591dda52002-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) {
wdenk57b2d802003-06-27 21:31:46 +000066 PRINTF ("Not a DOS diskette\n");
67 free (boot);
68 return (-1);
wdenk591dda52002-11-18 00:14:45 +000069 }
70
71 if (boot -> descr < MEDIA_STD) {
wdenk57b2d802003-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);
wdenk591dda52002-11-18 00:14:45 +000076 }
77
78 if (check_dev (boot, fs) < 0) {
wdenk57b2d802003-06-27 21:31:46 +000079 PRINTF ("Bad diskette\n");
80 free (boot);
81 return (-1);
wdenk591dda52002-11-18 00:14:45 +000082 }
wdenk57b2d802003-06-27 21:31:46 +000083
wdenk591dda52002-11-18 00:14:45 +000084 if (fill_fs (boot, fs) < 0) {
wdenk57b2d802003-06-27 21:31:46 +000085 free (boot);
wdenk591dda52002-11-18 00:14:45 +000086
wdenk57b2d802003-06-27 21:31:46 +000087 return (-1);
wdenk591dda52002-11-18 00:14:45 +000088 }
89
90 /* Read FAT */
91 if (read_fat (boot, fs) < 0) {
wdenk57b2d802003-06-27 21:31:46 +000092 free (boot);
93 return (-1);
wdenk591dda52002-11-18 00:14:45 +000094 }
95
96 free (boot);
97 return (0);
98}