1 | /** |
---|
2 | * @file |
---|
3 | * |
---|
4 | * @ingroup rtems_bdpart |
---|
5 | * |
---|
6 | * Block device partition management. |
---|
7 | */ |
---|
8 | |
---|
9 | /* |
---|
10 | * Copyright (c) 2009, 2010 |
---|
11 | * embedded brains GmbH |
---|
12 | * Obere Lagerstr. 30 |
---|
13 | * D-82178 Puchheim |
---|
14 | * Germany |
---|
15 | * <rtems@embedded-brains.de> |
---|
16 | * |
---|
17 | * The license and distribution terms for this file may be |
---|
18 | * found in the file LICENSE in this distribution or at |
---|
19 | * http://www.rtems.com/license/LICENSE. |
---|
20 | */ |
---|
21 | |
---|
22 | #ifdef HAVE_CONFIG_H |
---|
23 | #include "config.h" |
---|
24 | #endif |
---|
25 | |
---|
26 | #include <stdio.h> |
---|
27 | #include <stdlib.h> |
---|
28 | #include <string.h> |
---|
29 | |
---|
30 | #include <rtems.h> |
---|
31 | #include <rtems/bdpart.h> |
---|
32 | #include <rtems/dosfs.h> |
---|
33 | #include <rtems/fsmount.h> |
---|
34 | |
---|
35 | rtems_status_code rtems_bdpart_mount( |
---|
36 | const char *disk_name, |
---|
37 | const rtems_bdpart_partition *pt __attribute__((unused)), |
---|
38 | size_t count, |
---|
39 | const char *mount_base |
---|
40 | ) |
---|
41 | { |
---|
42 | rtems_status_code esc = RTEMS_SUCCESSFUL; |
---|
43 | const char *disk_file_name = strrchr( disk_name, '/'); |
---|
44 | char *logical_disk_name = NULL; |
---|
45 | char *logical_disk_marker = NULL; |
---|
46 | char *mount_point = NULL; |
---|
47 | char *mount_marker = NULL; |
---|
48 | size_t disk_file_name_size = 0; |
---|
49 | size_t disk_name_size = strlen( disk_name); |
---|
50 | size_t mount_base_size = strlen( mount_base); |
---|
51 | size_t i = 0; |
---|
52 | |
---|
53 | /* Create logical disk name base */ |
---|
54 | logical_disk_name = malloc( disk_name_size + RTEMS_BDPART_NUMBER_SIZE); |
---|
55 | if (logical_disk_name == NULL) { |
---|
56 | return RTEMS_NO_MEMORY; |
---|
57 | } |
---|
58 | strncpy( logical_disk_name, disk_name, disk_name_size); |
---|
59 | |
---|
60 | /* Get disk file name */ |
---|
61 | if (disk_file_name != NULL) { |
---|
62 | disk_file_name += 1; |
---|
63 | disk_file_name_size = strlen( disk_file_name); |
---|
64 | } else { |
---|
65 | disk_file_name = disk_name; |
---|
66 | disk_file_name_size = disk_name_size; |
---|
67 | } |
---|
68 | |
---|
69 | /* Create mount point base */ |
---|
70 | mount_point = malloc( mount_base_size + 1 + disk_file_name_size + RTEMS_BDPART_NUMBER_SIZE); |
---|
71 | if (mount_point == NULL) { |
---|
72 | esc = RTEMS_NO_MEMORY; |
---|
73 | goto cleanup; |
---|
74 | } |
---|
75 | strncpy( mount_point, mount_base, mount_base_size); |
---|
76 | mount_point [mount_base_size] = '/'; |
---|
77 | strncpy( mount_point + mount_base_size + 1, disk_file_name, disk_file_name_size); |
---|
78 | |
---|
79 | /* Markers */ |
---|
80 | logical_disk_marker = logical_disk_name + disk_name_size; |
---|
81 | mount_marker = mount_point + mount_base_size + 1 + disk_file_name_size; |
---|
82 | |
---|
83 | /* Mount supported file systems for each partition */ |
---|
84 | for (i = 0; i < count; ++i) { |
---|
85 | /* Create logical disk name */ |
---|
86 | int rv = snprintf( logical_disk_marker, RTEMS_BDPART_NUMBER_SIZE, "%zu", i + 1); |
---|
87 | if (rv >= RTEMS_BDPART_NUMBER_SIZE) { |
---|
88 | esc = RTEMS_INVALID_NAME; |
---|
89 | goto cleanup; |
---|
90 | } |
---|
91 | |
---|
92 | /* Create mount point */ |
---|
93 | strncpy( mount_marker, logical_disk_marker, RTEMS_BDPART_NUMBER_SIZE); |
---|
94 | rv = rtems_fsmount_create_mount_point( mount_point); |
---|
95 | if (rv != 0) { |
---|
96 | esc = RTEMS_IO_ERROR; |
---|
97 | goto cleanup; |
---|
98 | } |
---|
99 | |
---|
100 | /* Mount */ |
---|
101 | rv = mount( |
---|
102 | NULL, |
---|
103 | &msdos_ops, |
---|
104 | 0, |
---|
105 | logical_disk_name, |
---|
106 | mount_point |
---|
107 | ); |
---|
108 | if (rv != 0) { |
---|
109 | rmdir( mount_point); |
---|
110 | } |
---|
111 | } |
---|
112 | |
---|
113 | cleanup: |
---|
114 | |
---|
115 | free( logical_disk_name); |
---|
116 | free( mount_point); |
---|
117 | |
---|
118 | return esc; |
---|
119 | } |
---|
120 | |
---|
121 | rtems_status_code rtems_bdpart_unmount( |
---|
122 | const char *disk_name, |
---|
123 | const rtems_bdpart_partition *pt __attribute__((unused)), |
---|
124 | size_t count, |
---|
125 | const char *mount_base |
---|
126 | ) |
---|
127 | { |
---|
128 | rtems_status_code esc = RTEMS_SUCCESSFUL; |
---|
129 | const char *disk_file_name = strrchr( disk_name, '/'); |
---|
130 | char *mount_point = NULL; |
---|
131 | char *mount_marker = NULL; |
---|
132 | size_t disk_file_name_size = 0; |
---|
133 | size_t disk_name_size = strlen( disk_name); |
---|
134 | size_t mount_base_size = strlen( mount_base); |
---|
135 | size_t i = 0; |
---|
136 | |
---|
137 | /* Get disk file name */ |
---|
138 | if (disk_file_name != NULL) { |
---|
139 | disk_file_name += 1; |
---|
140 | disk_file_name_size = strlen( disk_file_name); |
---|
141 | } else { |
---|
142 | disk_file_name = disk_name; |
---|
143 | disk_file_name_size = disk_name_size; |
---|
144 | } |
---|
145 | |
---|
146 | /* Create mount point base */ |
---|
147 | mount_point = malloc( mount_base_size + 1 + disk_file_name_size + RTEMS_BDPART_NUMBER_SIZE); |
---|
148 | if (mount_point == NULL) { |
---|
149 | esc = RTEMS_NO_MEMORY; |
---|
150 | goto cleanup; |
---|
151 | } |
---|
152 | strncpy( mount_point, mount_base, mount_base_size); |
---|
153 | mount_point [mount_base_size] = '/'; |
---|
154 | strncpy( mount_point + mount_base_size + 1, disk_file_name, disk_file_name_size); |
---|
155 | |
---|
156 | /* Marker */ |
---|
157 | mount_marker = mount_point + mount_base_size + 1 + disk_file_name_size; |
---|
158 | |
---|
159 | /* Mount supported file systems for each partition */ |
---|
160 | for (i = 0; i < count; ++i) { |
---|
161 | /* Create mount point */ |
---|
162 | int rv = snprintf( mount_marker, RTEMS_BDPART_NUMBER_SIZE, "%zu", i + 1); |
---|
163 | if (rv >= RTEMS_BDPART_NUMBER_SIZE) { |
---|
164 | esc = RTEMS_INVALID_NAME; |
---|
165 | goto cleanup; |
---|
166 | } |
---|
167 | |
---|
168 | /* Unmount */ |
---|
169 | rv = unmount( mount_point); |
---|
170 | if (rv == 0) { |
---|
171 | /* Remove mount point */ |
---|
172 | rv = rmdir( mount_point); |
---|
173 | if (rv != 0) { |
---|
174 | esc = RTEMS_IO_ERROR; |
---|
175 | goto cleanup; |
---|
176 | } |
---|
177 | } |
---|
178 | } |
---|
179 | |
---|
180 | cleanup: |
---|
181 | |
---|
182 | free( mount_point); |
---|
183 | |
---|
184 | return esc; |
---|
185 | } |
---|