forked from apache/nuttx-apps
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnand_sim_main.c
225 lines (176 loc) · 6.26 KB
/
nand_sim_main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
/****************************************************************************
* apps/testing/drivers/nand_sim/nand_sim_main.c
*
* SPDX-License-Identifier: Apache-2.0
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. The
* ASF licenses this file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <debug.h>
#include <stdio.h>
#include <nuttx/drivers/drivers.h>
#include <nuttx/mtd/nand.h>
#include <nuttx/mtd/nand_scheme.h>
#include <nuttx/mtd/nand_ram.h>
#include <nuttx/mtd/nand_wrapper.h>
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
#define NAND_SIM_NAME "nand"
#define NAND_SIM_PATH "/dev/" NAND_SIM_NAME
/****************************************************************************
* Private Types
****************************************************************************/
/****************************************************************************
* Private Data
****************************************************************************/
/****************************************************************************
* Public Data
****************************************************************************/
FAR struct mtd_dev_s *g_nand_sim_mtd_wrapper;
FAR struct mtd_dev_s *g_nand_sim_mtd_under;
FAR struct nand_raw_s *g_nand_mtd_raw;
/****************************************************************************
* External Functions
****************************************************************************/
/****************************************************************************
* Private Functions
****************************************************************************/
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: wrapper_init
*
* Description:
* Initializes the wrapper by allocating memory and assiging the methods.
*
* Returned Value:
* 0: Successful
* -ENOMEM: No memory left to allocate device
*
****************************************************************************/
int wrapper_init(void)
{
struct nand_dev_s *under;
struct nand_dev_s *wrapper;
g_nand_sim_mtd_wrapper = kmm_zalloc(sizeof(struct nand_wrapper_dev_s));
if (g_nand_sim_mtd_wrapper == NULL)
{
return -ENOMEM;
}
under = &((struct nand_wrapper_dev_s *)g_nand_sim_mtd_wrapper)->under;
wrapper = &((struct nand_wrapper_dev_s *)g_nand_sim_mtd_wrapper)->wrapper;
memcpy(under, g_nand_sim_mtd_under, sizeof(struct nand_dev_s));
memcpy(wrapper, g_nand_sim_mtd_under, sizeof(struct nand_dev_s));
nand_wrapper_initialize();
((struct mtd_dev_s *)wrapper)->name = NAND_SIM_NAME;
((struct mtd_dev_s *)wrapper)->erase = nand_wrapper_erase;
((struct mtd_dev_s *)wrapper)->bread = nand_wrapper_bread;
((struct mtd_dev_s *)wrapper)->bwrite = nand_wrapper_bwrite;
((struct mtd_dev_s *)wrapper)->ioctl = nand_wrapper_ioctl;
((struct mtd_dev_s *)wrapper)->isbad = nand_wrapper_isbad;
((struct mtd_dev_s *)wrapper)->markbad = nand_wrapper_markbad;
return OK;
}
/****************************************************************************
* Name: terminate
*
* Description:
* Handles the SIGTERM signal by exitting gracefully.
*
****************************************************************************/
void terminate(int sig)
{
kmm_free(g_nand_sim_mtd_under);
kmm_free(g_nand_sim_mtd_wrapper);
unregister_mtddriver(NAND_SIM_PATH);
syslog(LOG_DEBUG, "Exited!\n");
}
/****************************************************************************
* Name: nand_sim_main
*
* Description:
* Entry point of the device emulator.
*
****************************************************************************/
int main(int argc, FAR char *argv[])
{
int ret;
pid_t pid;
/* Daemon */
pid = fork();
if (pid > 0)
{
return OK;
}
if (daemon(0, 1) == -1)
{
ret = EXIT_FAILURE;
goto errout;
}
/* Signal Handlers */
signal(SIGTERM, terminate);
/* Initializers */
/* Raw NAND MTD Device */
g_nand_mtd_raw = kmm_zalloc(sizeof(struct nand_raw_s));
if (g_nand_mtd_raw == NULL)
{
ret = -ENOMEM;
goto errout_with_logs;
}
g_nand_sim_mtd_under = nand_ram_initialize(g_nand_mtd_raw);
if (g_nand_sim_mtd_under == NULL)
{
ret = -EINVAL;
goto errout_with_raw_s;
}
ret = wrapper_init();
if (ret < 0)
{
goto errout_with_mtd_under;
}
/* Under device driver is already copied to wrapper, so free. */
kmm_free(g_nand_sim_mtd_under);
ret = register_mtddriver(NAND_SIM_PATH,
g_nand_sim_mtd_wrapper,
0777, NULL);
if (ret < 0)
{
goto errout_with_mtd_wrapper;
}
printf("Driver running!\n");
/* To keep the daemon still running. All events are handled by signals */
while (1)
{
sleep(1);
}
/* Won't reach this point */
return OK;
errout_with_mtd_wrapper:
kmm_free(g_nand_sim_mtd_wrapper);
errout_with_mtd_under:
kmm_free(g_nand_sim_mtd_under);
errout_with_raw_s:
kmm_free(g_nand_mtd_raw);
errout_with_logs:
unregister_mtddriver(NAND_SIM_PATH);
errout:
return ret;
}