1 | /*-----------------------------------------------------------------------*/
|
---|
2 | /* PFF - Low level disk control module for Win32 (C)ChaN, 2014 */
|
---|
3 | /*-----------------------------------------------------------------------*/
|
---|
4 |
|
---|
5 | #include <string.h>
|
---|
6 | #include "pffconf.h"
|
---|
7 | #include "diskio.h"
|
---|
8 |
|
---|
9 | /*--------------------------------------------------------------------------
|
---|
10 |
|
---|
11 | Module Private Functions
|
---|
12 |
|
---|
13 | ---------------------------------------------------------------------------*/
|
---|
14 |
|
---|
15 | #define MAX_DRIVES 1 /* Max number of physical drives to be used */
|
---|
16 |
|
---|
17 | #define SZ_RAMDISK 768 /* Size of RAM disk [kB] */
|
---|
18 | #define SS_RAMDISK 512 /* Sector size of RAM disk [byte] */
|
---|
19 |
|
---|
20 | #ifndef _MSC_VER
|
---|
21 | #define RamDisk ((unsigned char *)0xfff00000)
|
---|
22 | #else
|
---|
23 | #include <windows.h>
|
---|
24 | #include <tchar.h>
|
---|
25 | #include <winioctl.h>
|
---|
26 |
|
---|
27 | unsigned char RamDisk[SZ_RAMDISK * 1024];
|
---|
28 | #endif
|
---|
29 |
|
---|
30 | typedef struct {
|
---|
31 | DSTATUS status;
|
---|
32 | WORD sz_sector;
|
---|
33 | DWORD wofs;
|
---|
34 | DWORD wip;
|
---|
35 | DWORD n_sectors;
|
---|
36 | } STAT;
|
---|
37 |
|
---|
38 | static volatile
|
---|
39 | STAT Stat[MAX_DRIVES];
|
---|
40 |
|
---|
41 | static
|
---|
42 | BYTE Buffer[SS_RAMDISK];
|
---|
43 |
|
---|
44 | static
|
---|
45 | int get_status(volatile STAT *stat) {
|
---|
46 | stat->sz_sector = SS_RAMDISK;
|
---|
47 | if(stat->sz_sector < _MIN_SS || stat->sz_sector > _MAX_SS) return 0;
|
---|
48 | stat->n_sectors = SZ_RAMDISK * 1024 / SS_RAMDISK;
|
---|
49 | stat->status = 0;
|
---|
50 | return 1;
|
---|
51 | }
|
---|
52 |
|
---|
53 | /*-----------------------------------------------------------------------*/
|
---|
54 | /* Initialize Disk Drive */
|
---|
55 | /*-----------------------------------------------------------------------*/
|
---|
56 | DSTATUS disk_initialize (void)
|
---|
57 | {
|
---|
58 | DSTATUS sta;
|
---|
59 | #ifdef _MSC_VER
|
---|
60 | HANDLE h;
|
---|
61 | DWORD br;
|
---|
62 |
|
---|
63 | h = CreateFile(_T("..\\..\\..\\..\\uip\\apps\\webserver\\httpd-fs.bin"), GENERIC_READ, 0, 0, OPEN_EXISTING, 0, 0);
|
---|
64 | if (h != INVALID_HANDLE_VALUE) {
|
---|
65 | ReadFile(h, RamDisk, sizeof(RamDisk), &br, 0);
|
---|
66 | CloseHandle(h);
|
---|
67 | }
|
---|
68 | #endif
|
---|
69 | get_status(&Stat[0]);
|
---|
70 | sta = Stat[0].status;
|
---|
71 | Stat[0].wip = 0;
|
---|
72 |
|
---|
73 | return sta;
|
---|
74 | }
|
---|
75 |
|
---|
76 | /*-----------------------------------------------------------------------*/
|
---|
77 | /* Get Disk Status */
|
---|
78 | /*-----------------------------------------------------------------------*/
|
---|
79 | DSTATUS disk_get_status (void)
|
---|
80 | {
|
---|
81 | return Stat[0].status;
|
---|
82 | }
|
---|
83 |
|
---|
84 | /*-----------------------------------------------------------------------*/
|
---|
85 | /* Read Partial Sector */
|
---|
86 | /*-----------------------------------------------------------------------*/
|
---|
87 | DRESULT disk_readp (
|
---|
88 | BYTE* buff, /* Data read buffer */
|
---|
89 | DWORD sector, /* Sector number (LBA) */
|
---|
90 | UINT offset, /* Offset in the sector */
|
---|
91 | UINT count /* Byte count */
|
---|
92 | )
|
---|
93 | {
|
---|
94 | DRESULT res;
|
---|
95 | int dofs;
|
---|
96 |
|
---|
97 |
|
---|
98 | if(Stat[0].status & STA_NOINIT) return RES_NOTRDY;
|
---|
99 |
|
---|
100 | if(sector >= Stat[0].n_sectors || !count || offset + count > Stat[0].sz_sector/* || Stat[0].wip*/) {
|
---|
101 | res = RES_PARERR;
|
---|
102 | }
|
---|
103 | else {
|
---|
104 | if(buff) {
|
---|
105 | dofs = sector * Stat[0].sz_sector;
|
---|
106 | memcpy(buff, &RamDisk[dofs + offset], count);
|
---|
107 | res = RES_OK;
|
---|
108 | }
|
---|
109 | else {
|
---|
110 | res = RES_OK;
|
---|
111 | }
|
---|
112 | }
|
---|
113 |
|
---|
114 | return res;
|
---|
115 | }
|
---|
116 |
|
---|
117 | /*-----------------------------------------------------------------------*/
|
---|
118 | /* Write Partial Sector */
|
---|
119 | /*-----------------------------------------------------------------------*/
|
---|
120 | DRESULT disk_writep (
|
---|
121 | const BYTE* buff, /* Pointer to the write data */
|
---|
122 | DWORD sc /* Sector number (LBA), Number of bytes to send */
|
---|
123 | )
|
---|
124 | {
|
---|
125 | DRESULT res;
|
---|
126 | int dofs;
|
---|
127 |
|
---|
128 | if(Stat[0].status & STA_NOINIT) return RES_NOTRDY;
|
---|
129 |
|
---|
130 | if(!buff) {
|
---|
131 | if(sc) {
|
---|
132 | Stat[0].wip = sc;
|
---|
133 | Stat[0].wofs = 0;
|
---|
134 | memset(Buffer, 0, sizeof Buffer);
|
---|
135 | res = RES_OK;
|
---|
136 | }
|
---|
137 | else {
|
---|
138 | if(Stat[0].wip) {
|
---|
139 | dofs = Stat[0].wip * Stat[0].sz_sector;
|
---|
140 | memcpy(RamDisk + dofs, Buffer, Stat[0].sz_sector);
|
---|
141 | Stat[0].wip = 0;
|
---|
142 | res = RES_OK;
|
---|
143 | }
|
---|
144 | else {
|
---|
145 | res = RES_OK;
|
---|
146 | }
|
---|
147 | }
|
---|
148 | }
|
---|
149 | else {
|
---|
150 | if(Stat[0].wofs + sc > Stat[0].sz_sector) sc = Stat[0].sz_sector - Stat[0].wofs;
|
---|
151 | memcpy(&Buffer[Stat[0].wofs], buff, sc);
|
---|
152 | Stat[0].wofs += sc;
|
---|
153 | res = RES_OK;
|
---|
154 | }
|
---|
155 |
|
---|
156 | return res;
|
---|
157 | }
|
---|