1 //==========================================================================
5 // Disk device driver for the synthetic target
7 //==========================================================================
8 //####ECOSGPLCOPYRIGHTBEGIN####
9 // -------------------------------------------
10 // This file is part of eCos, the Embedded Configurable Operating System.
11 // Copyright (C) 2003 Savin Zlobec.
13 // eCos is free software; you can redistribute it and/or modify it under
14 // the terms of the GNU General Public License as published by the Free
15 // Software Foundation; either version 2 or (at your option) any later version.
17 // eCos is distributed in the hope that it will be useful, but WITHOUT ANY
18 // WARRANTY; without even the implied warranty of MERCHANTABILITY or
19 // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22 // You should have received a copy of the GNU General Public License along
23 // with eCos; if not, write to the Free Software Foundation, Inc.,
24 // 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
26 // As a special exception, if other files instantiate templates or use macros
27 // or inline functions from this file, or you compile this file and link it
28 // with other works to produce a work based on this file, this file does not
29 // by itself cause the resulting work to be covered by the GNU General Public
30 // License. However the source code for this file must still be made available
31 // in accordance with section (3) of the GNU General Public License.
33 // This exception does not invalidate any other reasons why a work based on
34 // this file might be covered by the GNU General Public License.
36 // -------------------------------------------
37 //####ECOSGPLCOPYRIGHTEND####
38 //==========================================================================
39 //#####DESCRIPTIONBEGIN####
45 //####DESCRIPTIONEND####
47 //==========================================================================
49 #include <pkgconf/devs_disk_ecosynth.h>
51 #include <cyg/infra/cyg_type.h>
52 #include <cyg/infra/cyg_ass.h>
53 #include <cyg/infra/diag.h>
54 #include <cyg/hal/hal_arch.h>
55 #include <cyg/hal/hal_io.h>
56 #include <cyg/hal/drv_api.h>
57 #include <cyg/io/io.h>
58 #include <cyg/io/devtab.h>
59 #include <cyg/io/disk.h>
61 #include <stdio.h> // sprintf
63 // ----------------------------------------------------------------------------
67 // ----------------------------------------------------------------------------
71 cyg_uint32 cylinders_num;
73 cyg_uint32 sectors_num;
79 // ----------------------------------------------------------------------------
81 static cyg_bool synth_disk_init(struct cyg_devtab_entry *tab);
83 static Cyg_ErrNo synth_disk_read(disk_channel *chan,
86 cyg_uint32 block_num);
88 static Cyg_ErrNo synth_disk_write(disk_channel *chan,
91 cyg_uint32 block_num);
93 static Cyg_ErrNo synth_disk_get_config(disk_channel *chan,
98 static Cyg_ErrNo synth_disk_set_config(disk_channel *chan,
103 static Cyg_ErrNo synth_disk_lookup(struct cyg_devtab_entry **tab,
104 struct cyg_devtab_entry *sub_tab,
107 static DISK_FUNS(synth_disk_funs,
110 synth_disk_get_config,
111 synth_disk_set_config
114 // ----------------------------------------------------------------------------
116 #define SYNTH_DISK_INSTANCE(_number_,_mbr_supp_, _cyl_,_hpt_,_spt_) \
117 static synth_disk_info_t synth_disk_info##_number_ = { \
119 cylinders_num: _cyl_, \
121 sectors_num: _spt_, \
122 size: CYGNUM_IO_DISK_ECOSYNTH_DISK##_number_##_SIZE, \
124 filename: CYGDAT_IO_DISK_ECOSYNTH_DISK##_number_##_FILENAME \
126 DISK_CHANNEL(synth_disk_channel##_number_, \
128 synth_disk_info##_number_, \
132 BLOCK_DEVTAB_ENTRY(synth_disk_io##_number_, \
133 CYGDAT_IO_DISK_ECOSYNTH_DISK##_number_##_NAME, \
135 &cyg_io_disk_devio, \
138 &synth_disk_channel##_number_ \
141 // ----------------------------------------------------------------------------
143 #ifdef CYGVAR_DEVS_DISK_ECOSYNTH_DISK0
144 # ifndef CYGIMP_IO_DISK_ECOSYNTH_DISK0_MBR
145 SYNTH_DISK_INSTANCE(0, false, 0, 0, 0);
147 SYNTH_DISK_INSTANCE(0, true, CYGIMP_IO_DISK_ECOSYNTH_DISK0_CYLINDERS,
148 CYGIMP_IO_DISK_ECOSYNTH_DISK0_HEADS,
149 CYGIMP_IO_DISK_ECOSYNTH_DISK0_SECTORS);
153 // ----------------------------------------------------------------------------
156 synth_disk_init(struct cyg_devtab_entry *tab)
158 disk_channel *chan = (disk_channel *) tab->priv;
159 synth_disk_info_t *synth_info = (synth_disk_info_t *) chan->dev_priv;
166 diag_printf("synth disk %d init size=%d\n",
167 synth_info->num, synth_info->size);
170 synth_info->filefd = cyg_hal_sys_open(synth_info->filename,
172 CYG_HAL_SYS_S_IRWXU|CYG_HAL_SYS_S_IRWXG|CYG_HAL_SYS_S_IRWXO);
174 if (-ENOENT == synth_info->filefd)
176 synth_info->filefd = cyg_hal_sys_open(synth_info->filename,
177 CYG_HAL_SYS_O_RDWR|CYG_HAL_SYS_O_CREAT, 0644);
179 if (synth_info->filefd >= 0)
181 unsigned char b = 0x00;
184 for (i = 0; i < synth_info->size; i++)
185 cyg_hal_sys_write(synth_info->filefd, &b, 1);
189 if (synth_info->filefd < 0)
191 CYG_ASSERT(false, "Can't open/create disk image file");
197 cyg_disk_identify_t ident;
199 ident.serial[0] = '\0';
200 ident.firmware_rev[0] = '\0';
201 ident.model_num[0] = '\0';
202 ident.lba_sectors_num = synth_info->size / 512;
203 ident.cylinders_num = synth_info->cylinders_num;
204 ident.heads_num = synth_info->heads_num;
205 ident.sectors_num = synth_info->sectors_num;
207 if (!(chan->callbacks->disk_init)(tab))
209 if (ENOERR != (chan->callbacks->disk_connected)(tab, &ident))
216 synth_disk_lookup(struct cyg_devtab_entry **tab,
217 struct cyg_devtab_entry *sub_tab,
220 disk_channel *chan = (disk_channel *) (*tab)->priv;
221 return (chan->callbacks->disk_lookup(tab, sub_tab, name));
225 synth_disk_read(disk_channel *chan,
228 cyg_uint32 block_num)
230 synth_disk_info_t *synth_info = (synth_disk_info_t *)chan->dev_priv;
233 diag_printf("synth disk read block %d\n", block_num);
236 if (synth_info->filefd >= 0)
238 cyg_hal_sys_lseek(synth_info->filefd,
239 block_num * chan->info->block_size,
240 CYG_HAL_SYS_SEEK_SET);
241 cyg_hal_sys_read(synth_info->filefd, buf, len);
248 synth_disk_write(disk_channel *chan,
251 cyg_uint32 block_num)
253 synth_disk_info_t *synth_info = (synth_disk_info_t *)chan->dev_priv;
256 diag_printf("synth disk write block %d\n", block_num);
259 if (synth_info->filefd >= 0)
261 cyg_hal_sys_lseek(synth_info->filefd,
262 block_num * chan->info->block_size,
263 CYG_HAL_SYS_SEEK_SET);
264 cyg_hal_sys_write(synth_info->filefd, buf, len);
265 // cyg_hal_sys_fdatasync(synth_info->filefd);
272 synth_disk_get_config(disk_channel *chan,
279 diag_printf("synth disk get config\n");
286 synth_disk_set_config(disk_channel *chan,
293 diag_printf("synth disk set config\n");
299 // ----------------------------------------------------------------------------