-
Notifications
You must be signed in to change notification settings - Fork 10
/
nvramdisk.c
195 lines (167 loc) · 3.98 KB
/
nvramdisk.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
/*
* nvramdisk.c
*
* RTEMS Project (http://www.rtems.org/)
*
* Copyright 2007 Chris Johns ([email protected])
*/
/*
** File-system block driver test program.
*/
#include "rki_config.h"
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <rtems.h>
#include <rtems/bdbuf.h>
#include <rtems/blkdev.h>
#include <rtems/diskdevs.h>
#include <rtems/dosfs.h>
#include <rtems/error.h>
#include <rtems/flashdisk.h>
#include <rtems/fsmount.h>
#include <rtems/shell.h>
#include <rtems/nvdisk-sram.h>
/*
** Let the IO system allocation the next available major number.
*/
#define RTEMS_DRIVER_AUTO_MAJOR (0)
/*
** Variable to hold the addresses of the memory pools for
** the NVRAM disk. The memory for the disk is allocated in the early
** int from the heap so it will be available as one chunk.
*/
uint32_t rki_nvdisk_base;
/*
** The NV Device descriptor. For this test it is just DRAM.
*/
rtems_nvdisk_device_desc rtems_nv_heap_device_descriptor[] =
{
{
flags: 0,
base: 0, /* The memory address is assigned at runtime */
size: RKI_NVRAMDISK_SIZE,
nv_ops: &rtems_nvdisk_sram_handlers
},
};
/**
* The NV Disk configuration.
*/
const rtems_nvdisk_config rtems_nvdisk_configuration[] =
{
{
block_size: 512,
device_count: 1,
devices: &rtems_nv_heap_device_descriptor[0],
flags: 0,
info_level: 0
},
};
/*
** The number of NV Disk configurations.
*/
uint32_t rtems_nvdisk_configuration_size = 1;
/*
* Create the NV Disk Driver entry.
*/
rtems_driver_address_table rtems_nvdisk_io_ops = {
initialization_entry: rtems_nvdisk_initialize,
open_entry: rtems_blkdev_generic_open,
close_entry: rtems_blkdev_generic_close,
read_entry: rtems_blkdev_generic_read,
write_entry: rtems_blkdev_generic_write,
control_entry: rtems_blkdev_generic_ioctl
};
#ifdef RKI_INCLUDE_NVRAMDISK
/*
** Allocate the disk RAM
*/
int allocate_disk_ram(void)
{
rki_nvdisk_base = (uint32_t) malloc (RKI_NVRAMDISK_SIZE);
if (!rki_nvdisk_base)
{
printf ("error: no memory for NV disk\n");
return 1;
}
else
{
printf("Allocated memory for NVRAM Disk\n");
}
return(0);
}
int shell_nvram_erase (int argc, char* argv[])
{
const char* driver = NULL;
int arg;
int fd;
for (arg = 1; arg < argc; arg++)
{
if (argv[arg][0] == '-')
{
printf ("error: invalid option: %s\n", argv[arg]);
return 1;
}
else
{
if (!driver)
driver = argv[arg];
else
{
printf ("error: only one driver name allowed: %s\n", argv[arg]);
return 1;
}
}
}
printf ("erase nvram disk: %s\n", driver);
fd = open (driver, O_WRONLY, 0);
if (fd < 0)
{
printf ("error: nvram driver open failed: %s\n", strerror (errno));
return 1;
}
if (ioctl (fd, RTEMS_NVDISK_IOCTL_ERASE_DISK) < 0)
{
printf ("error: nvram driver erase failed: %s\n", strerror (errno));
return 1;
}
close (fd);
printf ("flash nvram erased successful\n");
return 0;
}
int setup_nvdisk (void)
{
rtems_device_major_number major;
rtems_status_code sc;
/*
** check to see if the RAM is allocated before initializing the driver
*/
if ( rki_nvdisk_base == 0 )
{
printf("Cannot setup NVRAM disk, memory was not allocated\n");
return(1);
}
/*
** Assign the RAM allocated
*/
rtems_nv_heap_device_descriptor[0].base = rki_nvdisk_base;
/*
** Register the NVRAM Disk driver.
*/
printf ("Register NV Disk Driver: ");
sc = rtems_io_register_driver (RTEMS_DRIVER_AUTO_MAJOR,
&rtems_nvdisk_io_ops,
&major);
if (sc != RTEMS_SUCCESSFUL)
{
printf ("error: nvdisk driver not initialised: %s\n",
rtems_status_text (sc));
return 1;
}
rtems_shell_add_cmd ("nverase", "misc", "nverase driver", shell_nvram_erase);
printf ("successful\n");
return 0;
}
#endif