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