1 | /* |
---|
2 | * Copyright (c) 2012 embedded brains GmbH. All rights reserved. |
---|
3 | * |
---|
4 | * embedded brains GmbH |
---|
5 | * Obere Lagerstr. 30 |
---|
6 | * 82178 Puchheim |
---|
7 | * Germany |
---|
8 | * <rtems@embedded-brains.de> |
---|
9 | * |
---|
10 | * The license and distribution terms for this file may be |
---|
11 | * found in the file LICENSE in this distribution or at |
---|
12 | * http://www.rtems.org/license/LICENSE. |
---|
13 | */ |
---|
14 | |
---|
15 | #ifdef HAVE_CONFIG_H |
---|
16 | #include "config.h" |
---|
17 | #endif |
---|
18 | |
---|
19 | #include "tmacros.h" |
---|
20 | |
---|
21 | #include <sys/stat.h> |
---|
22 | #include <errno.h> |
---|
23 | #include <string.h> |
---|
24 | #include <stdint.h> |
---|
25 | #include <unistd.h> |
---|
26 | #include <fcntl.h> |
---|
27 | |
---|
28 | #include <rtems/flashdisk.h> |
---|
29 | #include <rtems/libio.h> |
---|
30 | #include <rtems/blkdev.h> |
---|
31 | #include <rtems/rtems-rfs-format.h> |
---|
32 | |
---|
33 | #include "test-file-system.h" |
---|
34 | |
---|
35 | const char rtems_test_name[] = "FLASHDISK 1"; |
---|
36 | |
---|
37 | /* forward declarations to avoid warnings */ |
---|
38 | static rtems_task Init(rtems_task_argument argument); |
---|
39 | |
---|
40 | #define FLASHDISK_CONFIG_COUNT 1 |
---|
41 | |
---|
42 | #define FLASHDISK_DEVICE_COUNT 1 |
---|
43 | |
---|
44 | #define FLASHDISK_SEGMENT_COUNT 4U |
---|
45 | |
---|
46 | #define FLASHDISK_SEGMENT_SIZE (16 * 1024) |
---|
47 | |
---|
48 | #define FLASHDISK_BLOCK_SIZE 512U |
---|
49 | |
---|
50 | #define FLASHDISK_BLOCKS_PER_SEGMENT \ |
---|
51 | (FLASHDISK_SEGMENT_SIZE / FLASHDISK_BLOCK_SIZE) |
---|
52 | |
---|
53 | #define FLASHDISK_SIZE \ |
---|
54 | (FLASHDISK_SEGMENT_COUNT * FLASHDISK_SEGMENT_SIZE) |
---|
55 | |
---|
56 | static const rtems_rfs_format_config rfs_config; |
---|
57 | |
---|
58 | static const char device [] = "/dev/fdda"; |
---|
59 | |
---|
60 | static const char mnt [] = "/mnt"; |
---|
61 | |
---|
62 | static const char file [] = "/mnt/file"; |
---|
63 | |
---|
64 | static uint8_t flashdisk_data [FLASHDISK_SIZE]; |
---|
65 | |
---|
66 | static void flashdisk_print_status(const char *disk_path) |
---|
67 | { |
---|
68 | int rv; |
---|
69 | int fd = open(disk_path, O_RDWR); |
---|
70 | rtems_test_assert(fd >= 0); |
---|
71 | |
---|
72 | rv = ioctl(fd, RTEMS_FDISK_IOCTL_PRINT_STATUS); |
---|
73 | rtems_test_assert(rv == 0); |
---|
74 | |
---|
75 | rv = close(fd); |
---|
76 | rtems_test_assert(rv == 0); |
---|
77 | } |
---|
78 | |
---|
79 | static int test_rfs_mount_handler( |
---|
80 | const char *disk_path, |
---|
81 | const char *mount_path, |
---|
82 | void *arg |
---|
83 | ) |
---|
84 | { |
---|
85 | return mount_and_make_target_path( |
---|
86 | disk_path, |
---|
87 | mount_path, |
---|
88 | RTEMS_FILESYSTEM_TYPE_RFS, |
---|
89 | RTEMS_FILESYSTEM_READ_WRITE, |
---|
90 | NULL |
---|
91 | ); |
---|
92 | } |
---|
93 | |
---|
94 | static int test_rfs_format_handler( |
---|
95 | const char *disk_path, |
---|
96 | void *arg |
---|
97 | ) |
---|
98 | { |
---|
99 | flashdisk_print_status(disk_path); |
---|
100 | |
---|
101 | rtems_test_assert(0); |
---|
102 | |
---|
103 | errno = EIO; |
---|
104 | |
---|
105 | return -1; |
---|
106 | } |
---|
107 | |
---|
108 | static const test_file_system_handler test_rfs_handler = { |
---|
109 | .mount = test_rfs_mount_handler, |
---|
110 | .format = test_rfs_format_handler |
---|
111 | }; |
---|
112 | |
---|
113 | static void test(void) |
---|
114 | { |
---|
115 | int rv; |
---|
116 | const void *data = NULL; |
---|
117 | |
---|
118 | rv = mkdir(mnt, S_IRWXU | S_IRWXG | S_IRWXO); |
---|
119 | rtems_test_assert(rv == 0); |
---|
120 | |
---|
121 | rv = rtems_rfs_format(device, &rfs_config); |
---|
122 | rtems_test_assert(rv == 0); |
---|
123 | |
---|
124 | rv = mount( |
---|
125 | device, |
---|
126 | mnt, |
---|
127 | RTEMS_FILESYSTEM_TYPE_RFS, |
---|
128 | RTEMS_FILESYSTEM_READ_WRITE, |
---|
129 | data |
---|
130 | ); |
---|
131 | rtems_test_assert(rv == 0); |
---|
132 | |
---|
133 | rv = mknod(file, S_IFREG | S_IRWXU | S_IRWXG | S_IRWXO, 0); |
---|
134 | rtems_test_assert(rv == 0); |
---|
135 | |
---|
136 | rv = unmount(mnt); |
---|
137 | rtems_test_assert(rv == 0); |
---|
138 | |
---|
139 | test_file_system_with_handler( |
---|
140 | 0, |
---|
141 | device, |
---|
142 | mnt, |
---|
143 | &test_rfs_handler, |
---|
144 | NULL |
---|
145 | ); |
---|
146 | |
---|
147 | flashdisk_print_status(device); |
---|
148 | } |
---|
149 | |
---|
150 | static void Init(rtems_task_argument arg) |
---|
151 | { |
---|
152 | TEST_BEGIN(); |
---|
153 | |
---|
154 | test(); |
---|
155 | |
---|
156 | TEST_END(); |
---|
157 | |
---|
158 | rtems_test_exit(0); |
---|
159 | } |
---|
160 | |
---|
161 | static void erase_device(void) |
---|
162 | { |
---|
163 | memset(&flashdisk_data [0], 0xff, FLASHDISK_SIZE); |
---|
164 | } |
---|
165 | |
---|
166 | static rtems_device_driver flashdisk_initialize( |
---|
167 | rtems_device_major_number major, |
---|
168 | rtems_device_minor_number minor, |
---|
169 | void *arg |
---|
170 | ) |
---|
171 | { |
---|
172 | erase_device(); |
---|
173 | |
---|
174 | return rtems_fdisk_initialize(major, minor, arg); |
---|
175 | } |
---|
176 | |
---|
177 | static uint8_t *get_data_pointer( |
---|
178 | const rtems_fdisk_segment_desc *sd, |
---|
179 | uint32_t segment, |
---|
180 | uint32_t offset |
---|
181 | ) |
---|
182 | { |
---|
183 | offset += sd->offset + (segment - sd->segment) * sd->size; |
---|
184 | |
---|
185 | return &flashdisk_data [offset]; |
---|
186 | } |
---|
187 | |
---|
188 | static int flashdisk_read( |
---|
189 | const rtems_fdisk_segment_desc *sd, |
---|
190 | uint32_t device, |
---|
191 | uint32_t segment, |
---|
192 | uint32_t offset, |
---|
193 | void *buffer, |
---|
194 | uint32_t size |
---|
195 | ) |
---|
196 | { |
---|
197 | int eno = 0; |
---|
198 | const uint8_t *data = get_data_pointer(sd, segment, offset); |
---|
199 | |
---|
200 | memcpy(buffer, data, size); |
---|
201 | |
---|
202 | return eno; |
---|
203 | } |
---|
204 | |
---|
205 | static int flashdisk_write( |
---|
206 | const rtems_fdisk_segment_desc *sd, |
---|
207 | uint32_t device, |
---|
208 | uint32_t segment, |
---|
209 | uint32_t offset, |
---|
210 | const void *buffer, |
---|
211 | uint32_t size |
---|
212 | ) |
---|
213 | { |
---|
214 | int eno = 0; |
---|
215 | uint8_t *data = get_data_pointer(sd, segment, offset); |
---|
216 | |
---|
217 | memcpy(data, buffer, size); |
---|
218 | |
---|
219 | return eno; |
---|
220 | } |
---|
221 | |
---|
222 | static int flashdisk_blank( |
---|
223 | const rtems_fdisk_segment_desc *sd, |
---|
224 | uint32_t device, |
---|
225 | uint32_t segment, |
---|
226 | uint32_t offset, |
---|
227 | uint32_t size |
---|
228 | ) |
---|
229 | { |
---|
230 | int eno = 0; |
---|
231 | const uint8_t *current = get_data_pointer(sd, segment, offset); |
---|
232 | const uint8_t *end = current + size; |
---|
233 | |
---|
234 | while (eno == 0 && current != end) { |
---|
235 | if (*current != 0xff) { |
---|
236 | eno = EIO; |
---|
237 | } |
---|
238 | ++current; |
---|
239 | } |
---|
240 | |
---|
241 | return eno; |
---|
242 | } |
---|
243 | |
---|
244 | static int flashdisk_verify( |
---|
245 | const rtems_fdisk_segment_desc *sd, |
---|
246 | uint32_t device, |
---|
247 | uint32_t segment, |
---|
248 | uint32_t offset, |
---|
249 | const void *buffer, |
---|
250 | uint32_t size |
---|
251 | ) |
---|
252 | { |
---|
253 | int eno = 0; |
---|
254 | uint8_t *data = get_data_pointer(sd, segment, offset); |
---|
255 | |
---|
256 | if (memcmp(data, buffer, size) != 0) { |
---|
257 | eno = EIO; |
---|
258 | } |
---|
259 | |
---|
260 | return eno; |
---|
261 | } |
---|
262 | |
---|
263 | static int flashdisk_erase( |
---|
264 | const rtems_fdisk_segment_desc *sd, |
---|
265 | uint32_t device, |
---|
266 | uint32_t segment |
---|
267 | ) |
---|
268 | { |
---|
269 | int eno = 0; |
---|
270 | uint8_t *data = get_data_pointer(sd, segment, 0); |
---|
271 | |
---|
272 | memset(data, 0xff, sd->size); |
---|
273 | |
---|
274 | return eno; |
---|
275 | } |
---|
276 | |
---|
277 | static int flashdisk_erase_device( |
---|
278 | const rtems_fdisk_device_desc *sd, |
---|
279 | uint32_t device |
---|
280 | ) |
---|
281 | { |
---|
282 | int eno = 0; |
---|
283 | |
---|
284 | erase_device(); |
---|
285 | |
---|
286 | return eno; |
---|
287 | } |
---|
288 | |
---|
289 | static const rtems_fdisk_segment_desc flashdisk_segment_desc = { |
---|
290 | .count = FLASHDISK_SEGMENT_COUNT, |
---|
291 | .segment = 0, |
---|
292 | .offset = 0, |
---|
293 | .size = FLASHDISK_SEGMENT_SIZE |
---|
294 | }; |
---|
295 | |
---|
296 | static const rtems_fdisk_driver_handlers flashdisk_ops = { |
---|
297 | .read = flashdisk_read, |
---|
298 | .write = flashdisk_write, |
---|
299 | .blank = flashdisk_blank, |
---|
300 | .verify = flashdisk_verify, |
---|
301 | .erase = flashdisk_erase, |
---|
302 | .erase_device = flashdisk_erase_device |
---|
303 | }; |
---|
304 | |
---|
305 | static const rtems_fdisk_device_desc flashdisk_device = { |
---|
306 | .segment_count = 1, |
---|
307 | .segments = &flashdisk_segment_desc, |
---|
308 | .flash_ops = &flashdisk_ops |
---|
309 | }; |
---|
310 | |
---|
311 | const rtems_flashdisk_config |
---|
312 | rtems_flashdisk_configuration [FLASHDISK_CONFIG_COUNT] = { |
---|
313 | { |
---|
314 | .block_size = FLASHDISK_BLOCK_SIZE, |
---|
315 | .device_count = FLASHDISK_DEVICE_COUNT, |
---|
316 | .devices = &flashdisk_device, |
---|
317 | .flags = RTEMS_FDISK_CHECK_PAGES |
---|
318 | | RTEMS_FDISK_BLANK_CHECK_BEFORE_WRITE, |
---|
319 | .unavail_blocks = FLASHDISK_BLOCKS_PER_SEGMENT, |
---|
320 | .compact_segs = 2, |
---|
321 | .avail_compact_segs = 1, |
---|
322 | .info_level = 0 |
---|
323 | } |
---|
324 | }; |
---|
325 | |
---|
326 | uint32_t rtems_flashdisk_configuration_size = FLASHDISK_CONFIG_COUNT; |
---|
327 | |
---|
328 | #define FLASHDISK_DRIVER { .initialization_entry = flashdisk_initialize } |
---|
329 | |
---|
330 | #define CONFIGURE_APPLICATION_NEEDS_CLOCK_DRIVER |
---|
331 | #define CONFIGURE_APPLICATION_NEEDS_SIMPLE_CONSOLE_DRIVER |
---|
332 | #define CONFIGURE_APPLICATION_EXTRA_DRIVERS FLASHDISK_DRIVER |
---|
333 | #define CONFIGURE_APPLICATION_NEEDS_LIBBLOCK |
---|
334 | |
---|
335 | #define CONFIGURE_MAXIMUM_FILE_DESCRIPTORS 6 |
---|
336 | |
---|
337 | #define CONFIGURE_FILESYSTEM_RFS |
---|
338 | |
---|
339 | #define CONFIGURE_MAXIMUM_TASKS 2 |
---|
340 | #define CONFIGURE_MAXIMUM_SEMAPHORES 1 |
---|
341 | |
---|
342 | #define CONFIGURE_INITIAL_EXTENSIONS RTEMS_TEST_INITIAL_EXTENSION |
---|
343 | |
---|
344 | #define CONFIGURE_RTEMS_INIT_TASKS_TABLE |
---|
345 | |
---|
346 | #define CONFIGURE_INIT_TASK_STACK_SIZE (32U * 1024U) |
---|
347 | |
---|
348 | #define CONFIGURE_INIT_TASK_ATTRIBUTES RTEMS_FLOATING_POINT |
---|
349 | |
---|
350 | #define CONFIGURE_INIT |
---|
351 | |
---|
352 | #include <rtems/confdefs.h> |
---|