source: rtems/testsuites/libtests/flashdev01/init.c @ ea27ada

Last change on this file since ea27ada was ea27ada, checked in by Kinsey Moore <kinsey.moore@…>, on 06/09/23 at 16:11:58

testsuites/flashdev01: Use correct page_size type

The page size ioctl requires an int pointer to retrieve the page size.
The test currently uses a size_t which mostly works fine for systems
where size_t and int are the same size, but can leave junk data in the
upper bits when they differ in size causing an assert in the test to
fail in some cases. This updates the variable to the correct type.

  • Property mode set to 100644
File size: 5.7 KB
Line 
1/*
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
4 * Copyright (C) 2023 Aaron Nyholm
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 *    notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 *    notice, this list of conditions and the following disclaimer in the
13 *    documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
19 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25 * POSSIBILITY OF SUCH DAMAGE.
26 */
27
28#include "tmacros.h"
29
30#include "test_flashdev.h"
31
32#include <stdio.h>
33#include <stdlib.h>
34#include <fcntl.h>
35#include <sys/ioctl.h>
36
37#define TEST_NAME_LENGTH 10
38
39#define TEST_DATA_SIZE (PAGE_SIZE * PAGE_COUNT)
40#define PAGE_COUNT 16
41#define PAGE_SIZE 128
42#define WB_SIZE 1
43
44const char rtems_test_name[] = "FLASHDEV 1";
45
46static void run_test(void);
47
48static void run_test(void) {
49
50  char buff[TEST_DATA_SIZE] = {0};
51  FILE *file;
52  int fd;
53  rtems_flashdev* flash;
54  int status;
55  char* read_data;
56  rtems_flashdev_region e_args;
57  rtems_flashdev_ioctl_page_info pg_info;
58  rtems_flashdev_region region;
59  uint32_t jedec;
60  int page_count;
61  int type;
62  size_t wb_size;
63
64  /* Initalize the flash device driver and flashdev */
65  flash = test_flashdev_init();
66  rtems_test_assert(flash != NULL);
67
68  /* Register the flashdev as a device */
69  status = rtems_flashdev_register(flash, "dev/flashdev0");
70  rtems_test_assert(!status);
71
72  /* Open the flashdev */
73  file = fopen("dev/flashdev0", "r+");
74  rtems_test_assert(file != NULL);
75  fd = fileno(file);
76
77  /* Read data from flash */
78  read_data = fgets(buff, TEST_DATA_SIZE, file);
79  rtems_test_assert(read_data != NULL);
80
81  /* Fseek to start of flash */
82  status = fseek(file, 0x0, SEEK_SET);
83  rtems_test_assert(!status);
84
85  /* Write the test name to the flash */
86  status = fwrite(rtems_test_name, TEST_NAME_LENGTH, 1, file);
87  rtems_test_assert(status == 1);
88
89  /* Fseek to start of flash and read again */
90  status = fseek(file, 0x0, SEEK_SET);
91  rtems_test_assert(!status);
92  fgets(buff, TEST_DATA_SIZE, file);
93  rtems_test_assert(!strncmp(buff, rtems_test_name, TEST_NAME_LENGTH));
94
95  /* Test Erasing */
96  e_args.offset = 0x0;
97  e_args.size = PAGE_SIZE;
98  status = ioctl(fd, RTEMS_FLASHDEV_IOCTL_ERASE, &e_args);
99  rtems_test_assert(!status);
100
101  fseek(file, 0x0, SEEK_SET);
102  fgets(buff, TEST_DATA_SIZE, file);
103  rtems_test_assert(buff[0] == 0);
104
105  /* Test getting JEDEC ID */
106  status = ioctl(fd, RTEMS_FLASHDEV_IOCTL_JEDEC_ID, &jedec);
107  rtems_test_assert(!status);
108  rtems_test_assert(jedec == 0x00ABCDEF);
109
110  /* Test getting flash type */
111  status = ioctl(fd, RTEMS_FLASHDEV_IOCTL_TYPE, &type);
112  rtems_test_assert(!status);
113  rtems_test_assert(type == RTEMS_FLASHDEV_NOR);
114
115  /* Test getting page info from offset */
116  pg_info.location = PAGE_SIZE + PAGE_SIZE/2;
117
118  status = ioctl(fd, RTEMS_FLASHDEV_IOCTL_PAGEINFO_BY_OFFSET, &pg_info);
119  rtems_test_assert(!status);
120  rtems_test_assert(pg_info.page_info.offset == PAGE_SIZE);
121  rtems_test_assert(pg_info.page_info.size == PAGE_SIZE);
122
123  /* Test getting page info from index */
124  pg_info.location = 2;
125  status = ioctl(fd, RTEMS_FLASHDEV_IOCTL_PAGEINFO_BY_INDEX, &pg_info);
126  rtems_test_assert(!status);
127  rtems_test_assert(pg_info.page_info.offset == 2*PAGE_SIZE);
128  rtems_test_assert(pg_info.page_info.size == PAGE_SIZE);
129
130  /* Test getting page count */
131  status = ioctl(fd, RTEMS_FLASHDEV_IOCTL_PAGE_COUNT, &page_count);
132  rtems_test_assert(!status);
133  rtems_test_assert(page_count == PAGE_COUNT);
134
135  /* Test getting write block size */
136  status = ioctl(fd, RTEMS_FLASHDEV_IOCTL_WRITE_BLOCK_SIZE, &wb_size);
137  rtems_test_assert(!status);
138  rtems_test_assert(wb_size == WB_SIZE);
139
140  /* Test Regions */
141  region.offset = 0x400;
142  region.size = 0x200;
143  status = ioctl(fd, RTEMS_FLASHDEV_IOCTL_REGION_SET, &region);
144  rtems_test_assert(!status);
145
146  /* Test read to larger then region */
147  fseek(file, 0x0, SEEK_SET);
148  read_data = fgets(buff, 2048, file);
149  rtems_test_assert(read_data == NULL);
150
151  /* Test fseek outside of region */
152  status = fseek(file, 0x201, SEEK_SET);
153  rtems_test_assert(status);
154
155  /* Write to base unset region and check the writes location */
156  fseek(file, 0x0, SEEK_SET);
157  fwrite("HELLO WORLD", 11, 1, file);
158  ioctl(fd, RTEMS_FLASHDEV_IOCTL_REGION_UNSET, NULL);
159  fseek(file, 0x400, SEEK_SET);
160  fgets(buff, 11, file);
161  rtems_test_assert(strncmp(buff, "HELLO WORLD", 11));
162}
163
164static void Init(rtems_task_argument arg)
165{
166  TEST_BEGIN();
167
168  run_test();
169
170  TEST_END();
171  rtems_test_exit(0);
172}
173
174#define CONFIGURE_MICROSECONDS_PER_TICK 2000
175
176#define CONFIGURE_APPLICATION_DOES_NOT_NEED_CLOCK_DRIVER
177
178#define CONFIGURE_MAXIMUM_FILE_DESCRIPTORS 7
179
180#define CONFIGURE_MAXIMUM_TASKS 2
181
182#define CONFIGURE_MAXIMUM_SEMAPHORES 1
183
184#define CONFIGURE_INITIAL_EXTENSIONS RTEMS_TEST_INITIAL_EXTENSION
185
186#define CONFIGURE_RTEMS_INIT_TASKS_TABLE
187
188#define CONFIGURE_INIT
189
190#include <rtems/confdefs.h>
Note: See TracBrowser for help on using the repository browser.