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/fsmount.h> |
---|
33 | |
---|
34 | rtems_status_code rtems_bdpart_mount( |
---|
35 | const char *disk_name, |
---|
36 | const rtems_bdpart_partition *pt __attribute__((unused)), |
---|
37 | size_t count, |
---|
38 | const char *mount_base |
---|
39 | ) |
---|
40 | { |
---|
41 | rtems_status_code esc = RTEMS_SUCCESSFUL; |
---|
42 | const char *disk_file_name = strrchr( disk_name, '/'); |
---|
43 | char *logical_disk_name = NULL; |
---|
44 | char *logical_disk_marker = NULL; |
---|
45 | char *mount_point = NULL; |
---|
46 | char *mount_marker = NULL; |
---|
47 | size_t disk_file_name_size = 0; |
---|
48 | size_t disk_name_size = strlen( disk_name); |
---|
49 | size_t mount_base_size = strlen( mount_base); |
---|
50 | size_t i = 0; |
---|
51 | |
---|
52 | /* Create logical disk name base */ |
---|
53 | logical_disk_name = malloc( disk_name_size + RTEMS_BDPART_NUMBER_SIZE); |
---|
54 | if (logical_disk_name == NULL) { |
---|
55 | return RTEMS_NO_MEMORY; |
---|
56 | } |
---|
57 | strncpy( logical_disk_name, disk_name, disk_name_size); |
---|
58 | |
---|
59 | /* Get disk file name */ |
---|
60 | if (disk_file_name != NULL) { |
---|
61 | disk_file_name += 1; |
---|
62 | disk_file_name_size = strlen( disk_file_name); |
---|
63 | } else { |
---|
64 | disk_file_name = disk_name; |
---|
65 | disk_file_name_size = disk_name_size; |
---|
66 | } |
---|
67 | |
---|
68 | /* Create mount point base */ |
---|
69 | mount_point = malloc( mount_base_size + 1 + disk_file_name_size + RTEMS_BDPART_NUMBER_SIZE); |
---|
70 | if (mount_point == NULL) { |
---|
71 | esc = RTEMS_NO_MEMORY; |
---|
72 | goto cleanup; |
---|
73 | } |
---|
74 | strncpy( mount_point, mount_base, mount_base_size); |
---|
75 | mount_point [mount_base_size] = '/'; |
---|
76 | strncpy( mount_point + mount_base_size + 1, disk_file_name, disk_file_name_size); |
---|
77 | |
---|
78 | /* Markers */ |
---|
79 | logical_disk_marker = logical_disk_name + disk_name_size; |
---|
80 | mount_marker = mount_point + mount_base_size + 1 + disk_file_name_size; |
---|
81 | |
---|
82 | /* Mount supported file systems for each partition */ |
---|
83 | for (i = 0; i < count; ++i) { |
---|
84 | /* Create logical disk name */ |
---|
85 | int rv = snprintf( logical_disk_marker, RTEMS_BDPART_NUMBER_SIZE, "%zu", i + 1); |
---|
86 | if (rv >= RTEMS_BDPART_NUMBER_SIZE) { |
---|
87 | esc = RTEMS_INVALID_NAME; |
---|
88 | goto cleanup; |
---|
89 | } |
---|
90 | |
---|
91 | /* Create mount point */ |
---|
92 | strncpy( mount_marker, logical_disk_marker, RTEMS_BDPART_NUMBER_SIZE); |
---|
93 | rv = rtems_fsmount_create_mount_point( mount_point); |
---|
94 | if (rv != 0) { |
---|
95 | esc = RTEMS_IO_ERROR; |
---|
96 | goto cleanup; |
---|
97 | } |
---|
98 | |
---|
99 | /* Mount */ |
---|
100 | rv = mount( |
---|
101 | logical_disk_name, |
---|
102 | mount_point, |
---|
103 | "msdos", |
---|
104 | 0, |
---|
105 | NULL |
---|
106 | ); |
---|
107 | if (rv != 0) { |
---|
108 | rmdir( mount_point); |
---|
109 | } |
---|
110 | } |
---|
111 | |
---|
112 | cleanup: |
---|
113 | |
---|
114 | free( logical_disk_name); |
---|
115 | free( mount_point); |
---|
116 | |
---|
117 | return esc; |
---|
118 | } |
---|
119 | |
---|
120 | rtems_status_code rtems_bdpart_unmount( |
---|
121 | const char *disk_name, |
---|
122 | const rtems_bdpart_partition *pt __attribute__((unused)), |
---|
123 | size_t count, |
---|
124 | const char *mount_base |
---|
125 | ) |
---|
126 | { |
---|
127 | rtems_status_code esc = RTEMS_SUCCESSFUL; |
---|
128 | const char *disk_file_name = strrchr( disk_name, '/'); |
---|
129 | char *mount_point = NULL; |
---|
130 | char *mount_marker = NULL; |
---|
131 | size_t disk_file_name_size = 0; |
---|
132 | size_t disk_name_size = strlen( disk_name); |
---|
133 | size_t mount_base_size = strlen( mount_base); |
---|
134 | size_t i = 0; |
---|
135 | |
---|
136 | /* Get disk file name */ |
---|
137 | if (disk_file_name != NULL) { |
---|
138 | disk_file_name += 1; |
---|
139 | disk_file_name_size = strlen( disk_file_name); |
---|
140 | } else { |
---|
141 | disk_file_name = disk_name; |
---|
142 | disk_file_name_size = disk_name_size; |
---|
143 | } |
---|
144 | |
---|
145 | /* Create mount point base */ |
---|
146 | mount_point = malloc( mount_base_size + 1 + disk_file_name_size + RTEMS_BDPART_NUMBER_SIZE); |
---|
147 | if (mount_point == NULL) { |
---|
148 | esc = RTEMS_NO_MEMORY; |
---|
149 | goto cleanup; |
---|
150 | } |
---|
151 | strncpy( mount_point, mount_base, mount_base_size); |
---|
152 | mount_point [mount_base_size] = '/'; |
---|
153 | strncpy( mount_point + mount_base_size + 1, disk_file_name, disk_file_name_size); |
---|
154 | |
---|
155 | /* Marker */ |
---|
156 | mount_marker = mount_point + mount_base_size + 1 + disk_file_name_size; |
---|
157 | |
---|
158 | /* Mount supported file systems for each partition */ |
---|
159 | for (i = 0; i < count; ++i) { |
---|
160 | /* Create mount point */ |
---|
161 | int rv = snprintf( mount_marker, RTEMS_BDPART_NUMBER_SIZE, "%zu", i + 1); |
---|
162 | if (rv >= RTEMS_BDPART_NUMBER_SIZE) { |
---|
163 | esc = RTEMS_INVALID_NAME; |
---|
164 | goto cleanup; |
---|
165 | } |
---|
166 | |
---|
167 | /* Unmount */ |
---|
168 | rv = unmount( mount_point); |
---|
169 | if (rv == 0) { |
---|
170 | /* Remove mount point */ |
---|
171 | rv = rmdir( mount_point); |
---|
172 | if (rv != 0) { |
---|
173 | esc = RTEMS_IO_ERROR; |
---|
174 | goto cleanup; |
---|
175 | } |
---|
176 | } |
---|
177 | } |
---|
178 | |
---|
179 | cleanup: |
---|
180 | |
---|
181 | free( mount_point); |
---|
182 | |
---|
183 | return esc; |
---|
184 | } |
---|