1 | /** |
---|
2 | * @file |
---|
3 | * |
---|
4 | * @brief Block Device Partition Management |
---|
5 | * @ingroup rtems_bdpart |
---|
6 | */ |
---|
7 | |
---|
8 | /* |
---|
9 | * Copyright (c) 2009, 2012 embedded brains GmbH. All rights reserved. |
---|
10 | * |
---|
11 | * embedded brains GmbH |
---|
12 | * Obere Lagerstr. 30 |
---|
13 | * 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.org/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 | #include <unistd.h> |
---|
30 | |
---|
31 | #include <rtems.h> |
---|
32 | #include <rtems/bdpart.h> |
---|
33 | |
---|
34 | static char *create_logical_disk_name( const char *disk_name, char **marker) |
---|
35 | { |
---|
36 | size_t disk_name_size = strlen( disk_name); |
---|
37 | char *logical_disk_name = malloc( disk_name_size + RTEMS_BDPART_NUMBER_SIZE); |
---|
38 | |
---|
39 | if (logical_disk_name != NULL) { |
---|
40 | strncpy( logical_disk_name, disk_name, disk_name_size); |
---|
41 | *marker = logical_disk_name + disk_name_size; |
---|
42 | } |
---|
43 | |
---|
44 | return logical_disk_name; |
---|
45 | } |
---|
46 | |
---|
47 | static rtems_status_code update_logical_disk_name( |
---|
48 | char *logical_disk_marker, |
---|
49 | size_t i |
---|
50 | ) |
---|
51 | { |
---|
52 | rtems_status_code sc = RTEMS_SUCCESSFUL; |
---|
53 | int rv = 0; |
---|
54 | |
---|
55 | rv = snprintf( logical_disk_marker, RTEMS_BDPART_NUMBER_SIZE, "%zu", i + 1); |
---|
56 | if (rv >= RTEMS_BDPART_NUMBER_SIZE) { |
---|
57 | sc = RTEMS_INVALID_NAME; |
---|
58 | } |
---|
59 | |
---|
60 | return sc; |
---|
61 | } |
---|
62 | |
---|
63 | rtems_status_code rtems_bdpart_register( |
---|
64 | const char *disk_name, |
---|
65 | const rtems_bdpart_partition *pt, |
---|
66 | size_t count |
---|
67 | ) |
---|
68 | { |
---|
69 | rtems_status_code sc = RTEMS_SUCCESSFUL; |
---|
70 | rtems_status_code esc = RTEMS_SUCCESSFUL; |
---|
71 | rtems_blkdev_bnum disk_end = 0; |
---|
72 | char *logical_disk_name = NULL; |
---|
73 | char *logical_disk_marker = NULL; |
---|
74 | size_t i = 0; |
---|
75 | int fd = -1; |
---|
76 | rtems_disk_device *dd = NULL; |
---|
77 | |
---|
78 | /* Get disk data */ |
---|
79 | sc = rtems_bdpart_get_disk_data( disk_name, &fd, &dd, &disk_end); |
---|
80 | if (sc != RTEMS_SUCCESSFUL) { |
---|
81 | return sc; |
---|
82 | } |
---|
83 | |
---|
84 | /* Create logical disk name */ |
---|
85 | logical_disk_name = create_logical_disk_name( |
---|
86 | disk_name, |
---|
87 | &logical_disk_marker |
---|
88 | ); |
---|
89 | if (logical_disk_name == NULL) { |
---|
90 | esc = sc; |
---|
91 | goto cleanup; |
---|
92 | } |
---|
93 | |
---|
94 | /* Create a logical disk for each partition */ |
---|
95 | for (i = 0; i < count; ++i) { |
---|
96 | const rtems_bdpart_partition *p = pt + i; |
---|
97 | |
---|
98 | /* Set partition number for logical disk name */ |
---|
99 | sc = update_logical_disk_name( logical_disk_marker, i); |
---|
100 | if (sc != RTEMS_SUCCESSFUL) { |
---|
101 | esc = sc; |
---|
102 | goto cleanup; |
---|
103 | } |
---|
104 | |
---|
105 | /* Create logical disk */ |
---|
106 | sc = rtems_blkdev_create_partition( |
---|
107 | logical_disk_name, |
---|
108 | disk_name, |
---|
109 | p->begin, |
---|
110 | p->end - p->begin |
---|
111 | ); |
---|
112 | if (sc != RTEMS_SUCCESSFUL) { |
---|
113 | esc = sc; |
---|
114 | goto cleanup; |
---|
115 | } |
---|
116 | } |
---|
117 | |
---|
118 | cleanup: |
---|
119 | |
---|
120 | free( logical_disk_name); |
---|
121 | close( fd); |
---|
122 | |
---|
123 | return esc; |
---|
124 | } |
---|
125 | |
---|
126 | rtems_status_code rtems_bdpart_register_from_disk( const char *disk_name) |
---|
127 | { |
---|
128 | rtems_status_code sc = RTEMS_SUCCESSFUL; |
---|
129 | rtems_bdpart_format format; |
---|
130 | rtems_bdpart_partition pt [RTEMS_BDPART_PARTITION_NUMBER_HINT]; |
---|
131 | size_t count = RTEMS_BDPART_PARTITION_NUMBER_HINT; |
---|
132 | |
---|
133 | /* Read partitions */ |
---|
134 | sc = rtems_bdpart_read( disk_name, &format, pt, &count); |
---|
135 | if (sc != RTEMS_SUCCESSFUL) { |
---|
136 | return sc; |
---|
137 | } |
---|
138 | |
---|
139 | /* Register partitions */ |
---|
140 | return rtems_bdpart_register( disk_name, pt, count); |
---|
141 | } |
---|
142 | |
---|
143 | rtems_status_code rtems_bdpart_unregister( |
---|
144 | const char *disk_name, |
---|
145 | const rtems_bdpart_partition *pt RTEMS_UNUSED, |
---|
146 | size_t count |
---|
147 | ) |
---|
148 | { |
---|
149 | rtems_status_code sc = RTEMS_SUCCESSFUL; |
---|
150 | rtems_status_code esc = RTEMS_SUCCESSFUL; |
---|
151 | rtems_blkdev_bnum disk_end = 0; |
---|
152 | char *logical_disk_name = NULL; |
---|
153 | char *logical_disk_marker = NULL; |
---|
154 | size_t i = 0; |
---|
155 | int fd = -1; |
---|
156 | rtems_disk_device *dd = NULL; |
---|
157 | |
---|
158 | /* Get disk data */ |
---|
159 | sc = rtems_bdpart_get_disk_data( disk_name, &fd, &dd, &disk_end); |
---|
160 | if (sc != RTEMS_SUCCESSFUL) { |
---|
161 | return sc; |
---|
162 | } |
---|
163 | |
---|
164 | /* Create logical disk name */ |
---|
165 | logical_disk_name = create_logical_disk_name( |
---|
166 | disk_name, |
---|
167 | &logical_disk_marker |
---|
168 | ); |
---|
169 | if (logical_disk_name == NULL) { |
---|
170 | esc = sc; |
---|
171 | goto cleanup; |
---|
172 | } |
---|
173 | |
---|
174 | /* Delete the logical disk for each partition */ |
---|
175 | for (i = 0; i < count; ++i) { |
---|
176 | int rv = 0; |
---|
177 | |
---|
178 | /* Set partition number for logical disk name */ |
---|
179 | sc = update_logical_disk_name( logical_disk_marker, i); |
---|
180 | if (sc != RTEMS_SUCCESSFUL) { |
---|
181 | esc = sc; |
---|
182 | goto cleanup; |
---|
183 | } |
---|
184 | |
---|
185 | /* Delete logical disk */ |
---|
186 | rv = unlink( logical_disk_name); |
---|
187 | if (rv != 0) { |
---|
188 | esc = sc; |
---|
189 | goto cleanup; |
---|
190 | } |
---|
191 | } |
---|
192 | |
---|
193 | cleanup: |
---|
194 | |
---|
195 | free( logical_disk_name); |
---|
196 | close( fd); |
---|
197 | |
---|
198 | return esc; |
---|
199 | } |
---|