1 | /***************************************************************************** |
---|
2 | * |
---|
3 | * ide_part_table.c |
---|
4 | * |
---|
5 | * The implementation of library supporting "MS-DOS-style" partition table |
---|
6 | * |
---|
7 | * |
---|
8 | * Copyright (C) 2002 OKTET Ltd., St.-Petersburg, Russia |
---|
9 | * |
---|
10 | * Author: Konstantin Abramenko <Konstantin.Abramenko@oktet.ru> |
---|
11 | * Alexander Kukuta <Alexander.Kukuta@oktet.ru> |
---|
12 | * |
---|
13 | * The license and distribution terms for this file may be |
---|
14 | * found in the file LICENSE in this distribution or at |
---|
15 | * http://www.rtems.com/license/LICENSE. |
---|
16 | * |
---|
17 | * $Id$ |
---|
18 | * |
---|
19 | *****************************************************************************/ |
---|
20 | |
---|
21 | #if HAVE_CONFIG_H |
---|
22 | #include "config.h" |
---|
23 | #endif |
---|
24 | |
---|
25 | #include <string.h> |
---|
26 | |
---|
27 | #include <rtems/ide_part_table.h> |
---|
28 | |
---|
29 | /* |
---|
30 | * get_sector -- |
---|
31 | * gets sector from the disk |
---|
32 | * |
---|
33 | * PARAMETERS: |
---|
34 | * dev - device number |
---|
35 | * sector_num - number of sector to read |
---|
36 | * sector - returned pointer to pointer to allocated |
---|
37 | * sector_data_t structure |
---|
38 | * |
---|
39 | * RETURNS: |
---|
40 | * RTEMS_SUCCESSFUL, if success; |
---|
41 | * RTEMS_NO_MEMORY, if canot allocate memory for sector data; |
---|
42 | * other error codes returned by rtems_bdbuf_read(). |
---|
43 | * |
---|
44 | * NOTES: |
---|
45 | * get_sector() operates with device via bdbuf library, |
---|
46 | * and does not support devices with sector size other than 512 bytes |
---|
47 | */ |
---|
48 | static rtems_status_code |
---|
49 | get_sector(dev_t dev, uint32_t sector_num, rtems_sector_data_t **sector) |
---|
50 | { |
---|
51 | rtems_sector_data_t *s; |
---|
52 | rtems_bdbuf_buffer *buf; |
---|
53 | rtems_status_code rc; |
---|
54 | |
---|
55 | if (sector == NULL) |
---|
56 | { |
---|
57 | return RTEMS_INTERNAL_ERROR; |
---|
58 | } |
---|
59 | |
---|
60 | s = (rtems_sector_data_t *) malloc(sizeof(rtems_sector_data_t) + RTEMS_IDE_SECTOR_SIZE); |
---|
61 | if (s == NULL) |
---|
62 | { |
---|
63 | return RTEMS_NO_MEMORY; |
---|
64 | } |
---|
65 | |
---|
66 | rc = rtems_bdbuf_read(dev, sector_num, &buf); |
---|
67 | if (rc != RTEMS_SUCCESSFUL) |
---|
68 | { |
---|
69 | free(s); |
---|
70 | return rc; |
---|
71 | } |
---|
72 | |
---|
73 | memcpy(s->data, buf->buffer, RTEMS_IDE_SECTOR_SIZE); |
---|
74 | s->sector_num = sector_num; |
---|
75 | |
---|
76 | *sector = s; |
---|
77 | |
---|
78 | rtems_bdbuf_release(buf); |
---|
79 | |
---|
80 | return RTEMS_SUCCESSFUL; |
---|
81 | } |
---|
82 | |
---|
83 | |
---|
84 | /* |
---|
85 | * msdos_signature_check -- |
---|
86 | * checks if the partition table sector has msdos signature |
---|
87 | * |
---|
88 | * PARAMETERS: |
---|
89 | * sector - sector to check |
---|
90 | * |
---|
91 | * RETURNS: |
---|
92 | * true if sector has msdos signature, false otherwise |
---|
93 | */ |
---|
94 | static bool |
---|
95 | msdos_signature_check (rtems_sector_data_t *sector) |
---|
96 | { |
---|
97 | uint8_t *p = sector->data + RTEMS_IDE_PARTITION_MSDOS_SIGNATURE_OFFSET; |
---|
98 | |
---|
99 | return ((p[0] == RTEMS_IDE_PARTITION_MSDOS_SIGNATURE_DATA1) && |
---|
100 | (p[1] == RTEMS_IDE_PARTITION_MSDOS_SIGNATURE_DATA2)); |
---|
101 | } |
---|
102 | |
---|
103 | |
---|
104 | /* |
---|
105 | * is_extended -- |
---|
106 | * checks if the partition type is extended |
---|
107 | * |
---|
108 | * PARAMETERS: |
---|
109 | * type - type of partition to check |
---|
110 | * |
---|
111 | * RETURNS: |
---|
112 | * true if partition type is extended, false otherwise |
---|
113 | */ |
---|
114 | static bool |
---|
115 | is_extended(uint8_t type) |
---|
116 | { |
---|
117 | return ((type == EXTENDED_PARTITION) || (type == LINUX_EXTENDED)); |
---|
118 | } |
---|
119 | |
---|
120 | /* |
---|
121 | * is_fat_partition -- |
---|
122 | * checks if the partition type is defined for FAT |
---|
123 | * |
---|
124 | * PARAMETERS: |
---|
125 | * type - type of partition to check |
---|
126 | * |
---|
127 | * RETURNS: |
---|
128 | * true if partition type is extended, false otherwise |
---|
129 | */ |
---|
130 | static bool |
---|
131 | is_fat_partition(uint8_t type) |
---|
132 | { |
---|
133 | static const uint8_t fat_part_types[] = { |
---|
134 | DOS_FAT12_PARTITION,DOS_FAT16_PARTITION, |
---|
135 | DOS_P32MB_PARTITION, |
---|
136 | FAT32_PARTITION ,FAT32_LBA_PARTITION, |
---|
137 | FAT16_LBA_PARTITION |
---|
138 | }; |
---|
139 | |
---|
140 | return (NULL != memchr(fat_part_types,type,sizeof(fat_part_types))); |
---|
141 | } |
---|
142 | |
---|
143 | |
---|
144 | /* |
---|
145 | * data_to_part_desc -- |
---|
146 | * parses raw partition table sector data |
---|
147 | * to partition description structure |
---|
148 | * |
---|
149 | * PARAMETERS: |
---|
150 | * data - raw partition table sector data |
---|
151 | * new_part_desc - pointer to returned partition description structure |
---|
152 | * |
---|
153 | * RETURNS: |
---|
154 | * RTEMS_SUCCESSFUL, if success; |
---|
155 | * RTEMS_NO_MEMOTY, if cannot allocate memory for part_desc_t strucure; |
---|
156 | * RTEMS_INTERNAL_ERROR, if other error occurs. |
---|
157 | */ |
---|
158 | static rtems_status_code |
---|
159 | data_to_part_desc(uint8_t *data, rtems_part_desc_t **new_part_desc) |
---|
160 | { |
---|
161 | rtems_part_desc_t *part_desc; |
---|
162 | uint32_t temp; |
---|
163 | |
---|
164 | if (new_part_desc == NULL) |
---|
165 | { |
---|
166 | return RTEMS_INTERNAL_ERROR; |
---|
167 | } |
---|
168 | |
---|
169 | *new_part_desc = NULL; |
---|
170 | |
---|
171 | if ((part_desc = calloc(1, sizeof(rtems_part_desc_t))) == NULL) |
---|
172 | { |
---|
173 | return RTEMS_NO_MEMORY; |
---|
174 | } |
---|
175 | |
---|
176 | part_desc->bootable = *(data + RTEMS_IDE_PARTITION_BOOTABLE_OFFSET); |
---|
177 | part_desc->sys_type = *(data + RTEMS_IDE_PARTITION_SYS_TYPE_OFFSET); |
---|
178 | |
---|
179 | /* read the offset start position and partition size in sectors */ |
---|
180 | |
---|
181 | /* due to incorrect data alignment one have to align data first */ |
---|
182 | memcpy(&temp, data + RTEMS_IDE_PARTITION_START_OFFSET, sizeof(uint32_t)); |
---|
183 | part_desc->start = LE_TO_CPU_U32(temp); |
---|
184 | |
---|
185 | memcpy(&temp, data + RTEMS_IDE_PARTITION_SIZE_OFFSET, sizeof(uint32_t)); |
---|
186 | part_desc->size = LE_TO_CPU_U32(temp); |
---|
187 | |
---|
188 | /* |
---|
189 | * use partitions that are |
---|
190 | * - extended |
---|
191 | * or |
---|
192 | * - FAT type and non-zero |
---|
193 | */ |
---|
194 | if (is_extended(part_desc->sys_type) || |
---|
195 | ((is_fat_partition(part_desc->sys_type)) && (part_desc->size != 0))) { |
---|
196 | *new_part_desc = part_desc; |
---|
197 | } |
---|
198 | else { |
---|
199 | /* empty partition */ |
---|
200 | free(part_desc); |
---|
201 | } |
---|
202 | return RTEMS_SUCCESSFUL; |
---|
203 | } |
---|
204 | |
---|
205 | |
---|
206 | /* |
---|
207 | * read_extended_partition -- |
---|
208 | * recursively reads extended partition sector from the device |
---|
209 | * and constructs the partition table tree |
---|
210 | * |
---|
211 | * PARAMETERS: |
---|
212 | * start - start sector of primary extended partition, used for |
---|
213 | * calculation of absolute partition sector address |
---|
214 | * ext_part - description of extended partition to process |
---|
215 | * |
---|
216 | * RETURNS: |
---|
217 | * RTEMS_SUCCESSFUL if success, |
---|
218 | * RTEMS_NO_MEMOTY if cannot allocate memory for part_desc_t strucure, |
---|
219 | * RTEMS_INTERNAL_ERROR if other error occurs. |
---|
220 | */ |
---|
221 | static rtems_status_code |
---|
222 | read_extended_partition(uint32_t start, rtems_part_desc_t *ext_part) |
---|
223 | { |
---|
224 | int i; |
---|
225 | dev_t dev; |
---|
226 | rtems_sector_data_t *sector; |
---|
227 | uint32_t here; |
---|
228 | uint8_t *data; |
---|
229 | rtems_part_desc_t *new_part_desc; |
---|
230 | rtems_status_code rc; |
---|
231 | |
---|
232 | if ((ext_part == NULL) || (ext_part->disk_desc == NULL)) |
---|
233 | { |
---|
234 | return RTEMS_INTERNAL_ERROR; |
---|
235 | } |
---|
236 | |
---|
237 | dev = ext_part->disk_desc->dev; |
---|
238 | |
---|
239 | /* get start sector of current extended partition */ |
---|
240 | here = ext_part->start; |
---|
241 | |
---|
242 | /* get first extended partition sector */ |
---|
243 | |
---|
244 | rc = get_sector(dev, here, §or); |
---|
245 | if (rc != RTEMS_SUCCESSFUL) |
---|
246 | { |
---|
247 | return rc; |
---|
248 | } |
---|
249 | |
---|
250 | if (!msdos_signature_check(sector)) |
---|
251 | { |
---|
252 | return RTEMS_INTERNAL_ERROR; |
---|
253 | } |
---|
254 | |
---|
255 | /* read and process up to 4 logical partition descriptors */ |
---|
256 | |
---|
257 | data = sector->data + RTEMS_IDE_PARTITION_TABLE_OFFSET; |
---|
258 | |
---|
259 | for (i = 0; i < RTEMS_IDE_PARTITION_MAX_SUB_PARTITION_NUMBER; i++) |
---|
260 | { |
---|
261 | /* if data_to_part_desc fails skip this partition |
---|
262 | * and parse the next one |
---|
263 | */ |
---|
264 | rc = data_to_part_desc(data, &new_part_desc); |
---|
265 | if (rc != RTEMS_SUCCESSFUL) |
---|
266 | { |
---|
267 | free(sector); |
---|
268 | return rc; |
---|
269 | } |
---|
270 | |
---|
271 | if (new_part_desc == NULL) |
---|
272 | { |
---|
273 | data += RTEMS_IDE_PARTITION_DESCRIPTOR_SIZE; |
---|
274 | continue; |
---|
275 | } |
---|
276 | |
---|
277 | ext_part->sub_part[i] = new_part_desc; |
---|
278 | new_part_desc->ext_part = ext_part; |
---|
279 | new_part_desc->disk_desc = ext_part->disk_desc; |
---|
280 | |
---|
281 | if (is_extended(new_part_desc->sys_type)) |
---|
282 | { |
---|
283 | new_part_desc->log_id = EMPTY_PARTITION; |
---|
284 | new_part_desc->start += start; |
---|
285 | read_extended_partition(start, new_part_desc); |
---|
286 | } |
---|
287 | else |
---|
288 | { |
---|
289 | rtems_disk_desc_t *disk_desc = new_part_desc->disk_desc; |
---|
290 | disk_desc->partitions[disk_desc->last_log_id] = new_part_desc; |
---|
291 | new_part_desc->log_id = ++disk_desc->last_log_id; |
---|
292 | new_part_desc->start += here; |
---|
293 | new_part_desc->end = new_part_desc->start + new_part_desc->size - 1; |
---|
294 | } |
---|
295 | data += RTEMS_IDE_PARTITION_DESCRIPTOR_SIZE; |
---|
296 | } |
---|
297 | |
---|
298 | free(sector); |
---|
299 | |
---|
300 | return RTEMS_SUCCESSFUL; |
---|
301 | } |
---|
302 | |
---|
303 | |
---|
304 | /* |
---|
305 | * read_mbr -- |
---|
306 | * reads Master Boot Record (sector 0) of physical device and |
---|
307 | * constructs disk description structure |
---|
308 | * |
---|
309 | * PARAMETERS: |
---|
310 | * disk_desc - returned disc description structure |
---|
311 | * |
---|
312 | * RETURNS: |
---|
313 | * RTEMS_SUCCESSFUL if success, |
---|
314 | * RTEMS_INTERNAL_ERROR otherwise |
---|
315 | */ |
---|
316 | static rtems_status_code |
---|
317 | read_mbr(rtems_disk_desc_t *disk_desc) |
---|
318 | { |
---|
319 | int part_num; |
---|
320 | rtems_sector_data_t *sector = NULL; |
---|
321 | rtems_part_desc_t *part_desc; |
---|
322 | uint8_t *data; |
---|
323 | rtems_status_code rc; |
---|
324 | dev_t dev = disk_desc->dev; |
---|
325 | |
---|
326 | /* get MBR sector */ |
---|
327 | rc = get_sector(dev, 0, §or); |
---|
328 | if (rc != RTEMS_SUCCESSFUL) |
---|
329 | { |
---|
330 | if (sector) |
---|
331 | free(sector); |
---|
332 | return rc; |
---|
333 | } |
---|
334 | |
---|
335 | /* check if the partition table structure is MS-DOS style */ |
---|
336 | if (!msdos_signature_check(sector)) |
---|
337 | { |
---|
338 | free(sector); |
---|
339 | return RTEMS_INTERNAL_ERROR; |
---|
340 | } |
---|
341 | |
---|
342 | /* read and process 4 primary partition descriptors */ |
---|
343 | |
---|
344 | data = sector->data + RTEMS_IDE_PARTITION_TABLE_OFFSET; |
---|
345 | |
---|
346 | for (part_num = 0; |
---|
347 | part_num < RTEMS_IDE_PARTITION_MAX_SUB_PARTITION_NUMBER; |
---|
348 | part_num++) |
---|
349 | { |
---|
350 | rc = data_to_part_desc(data, &part_desc); |
---|
351 | if (rc != RTEMS_SUCCESSFUL) |
---|
352 | { |
---|
353 | free(sector); |
---|
354 | return rc; |
---|
355 | } |
---|
356 | |
---|
357 | if (part_desc != NULL) |
---|
358 | { |
---|
359 | part_desc->log_id = part_num + 1; |
---|
360 | part_desc->disk_desc = disk_desc; |
---|
361 | part_desc->end = part_desc->start + part_desc->size - 1; |
---|
362 | disk_desc->partitions[part_num] = part_desc; |
---|
363 | } |
---|
364 | else |
---|
365 | { |
---|
366 | disk_desc->partitions[part_num] = NULL; |
---|
367 | } |
---|
368 | |
---|
369 | data += RTEMS_IDE_PARTITION_DESCRIPTOR_SIZE; |
---|
370 | } |
---|
371 | |
---|
372 | free(sector); |
---|
373 | |
---|
374 | disk_desc->last_log_id = RTEMS_IDE_PARTITION_MAX_SUB_PARTITION_NUMBER; |
---|
375 | |
---|
376 | /* There cannot be more than one extended partition, |
---|
377 | but we are to process each primary partition */ |
---|
378 | for (part_num = 0; |
---|
379 | part_num < RTEMS_IDE_PARTITION_MAX_SUB_PARTITION_NUMBER; |
---|
380 | part_num++) |
---|
381 | { |
---|
382 | part_desc = disk_desc->partitions[part_num]; |
---|
383 | if (part_desc != NULL && is_extended(part_desc->sys_type)) |
---|
384 | { |
---|
385 | read_extended_partition(part_desc->start, part_desc); |
---|
386 | } |
---|
387 | } |
---|
388 | |
---|
389 | return RTEMS_SUCCESSFUL; |
---|
390 | } |
---|
391 | |
---|
392 | |
---|
393 | /* |
---|
394 | * partition free -- |
---|
395 | * frees partition description structure |
---|
396 | * |
---|
397 | * PARAMETERS: |
---|
398 | * part_desc - returned disc description structure |
---|
399 | * |
---|
400 | * RETURNS: |
---|
401 | * N/A |
---|
402 | */ |
---|
403 | static void |
---|
404 | partition_free(rtems_part_desc_t *part_desc) |
---|
405 | { |
---|
406 | int part_num; |
---|
407 | |
---|
408 | if (part_desc == NULL) |
---|
409 | return; |
---|
410 | |
---|
411 | if (is_extended(part_desc->sys_type)) |
---|
412 | { |
---|
413 | for (part_num = 0; |
---|
414 | part_num < RTEMS_IDE_PARTITION_MAX_SUB_PARTITION_NUMBER; |
---|
415 | part_num++) |
---|
416 | { |
---|
417 | partition_free(part_desc->sub_part[part_num]); |
---|
418 | } |
---|
419 | } |
---|
420 | |
---|
421 | free(part_desc); |
---|
422 | } |
---|
423 | |
---|
424 | |
---|
425 | /* |
---|
426 | * rtems_ide_part_table_free - frees disk descriptor structure |
---|
427 | * |
---|
428 | * PARAMETERS: |
---|
429 | * disk_desc - disc descriptor structure to free |
---|
430 | * |
---|
431 | * RETURNS: |
---|
432 | * N/A |
---|
433 | */ |
---|
434 | void |
---|
435 | rtems_ide_part_table_free(rtems_disk_desc_t *disk_desc) |
---|
436 | { |
---|
437 | int part_num; |
---|
438 | |
---|
439 | for (part_num = 0; part_num < RTEMS_IDE_PARTITION_MAX_SUB_PARTITION_NUMBER; part_num++) |
---|
440 | { |
---|
441 | partition_free(disk_desc->partitions[part_num]); |
---|
442 | } |
---|
443 | |
---|
444 | free(disk_desc); |
---|
445 | } |
---|
446 | |
---|
447 | |
---|
448 | /* |
---|
449 | * rtems_ide_part_table_get - reads partition table structure from the device |
---|
450 | * and creates disk description structure |
---|
451 | * |
---|
452 | * PARAMETERS: |
---|
453 | * dev_name - path to physical device in /dev filesystem |
---|
454 | * disk_desc - returned disc description structure |
---|
455 | * |
---|
456 | * RETURNS: |
---|
457 | * RTEMS_SUCCESSFUL if success, |
---|
458 | * RTEMS_INTERNAL_ERROR otherwise |
---|
459 | */ |
---|
460 | rtems_status_code |
---|
461 | rtems_ide_part_table_get(const char *dev_name, rtems_disk_desc_t *disk_desc) |
---|
462 | { |
---|
463 | struct stat dev_stat; |
---|
464 | rtems_status_code rc; |
---|
465 | |
---|
466 | rc = stat(dev_name, &dev_stat); |
---|
467 | if (rc != RTEMS_SUCCESSFUL) |
---|
468 | { |
---|
469 | return RTEMS_INTERNAL_ERROR; |
---|
470 | } |
---|
471 | |
---|
472 | strncpy (disk_desc->dev_name, dev_name, 15); |
---|
473 | disk_desc->dev = dev_stat.st_rdev; |
---|
474 | disk_desc->sector_size = (dev_stat.st_blksize) ? dev_stat.st_blksize : |
---|
475 | RTEMS_IDE_SECTOR_SIZE; |
---|
476 | |
---|
477 | rc = read_mbr(disk_desc); |
---|
478 | |
---|
479 | return rc; |
---|
480 | } |
---|
481 | |
---|
482 | |
---|
483 | /* |
---|
484 | * rtems_ide_part_table_initialize - initializes logical devices |
---|
485 | * on the physical IDE drive |
---|
486 | * |
---|
487 | * PARAMETERS: |
---|
488 | * dev_name - path to physical device in /dev filesystem |
---|
489 | * |
---|
490 | * RETURNS: |
---|
491 | * RTEMS_SUCCESSFUL if success, |
---|
492 | * RTEMS_NO_MEMOTY if cannot have not enough memory, |
---|
493 | * RTEMS_INTERNAL_ERROR if other error occurs. |
---|
494 | */ |
---|
495 | rtems_status_code |
---|
496 | rtems_ide_part_table_initialize(char *dev_name) |
---|
497 | { |
---|
498 | int part_num; |
---|
499 | dev_t dev; |
---|
500 | rtems_disk_desc_t *disk_desc; |
---|
501 | rtems_device_major_number major; |
---|
502 | rtems_device_minor_number minor; |
---|
503 | rtems_status_code rc; |
---|
504 | rtems_part_desc_t *part_desc; |
---|
505 | |
---|
506 | /* logical device name /dev/hdxyy */ |
---|
507 | char name[RTEMS_IDE_PARTITION_DEV_NAME_LENGTH_MAX]; |
---|
508 | |
---|
509 | disk_desc = (rtems_disk_desc_t *) calloc(1, sizeof(rtems_disk_desc_t)); |
---|
510 | if (disk_desc == NULL) |
---|
511 | { |
---|
512 | return RTEMS_NO_MEMORY; |
---|
513 | } |
---|
514 | |
---|
515 | /* get partition table */ |
---|
516 | rc = rtems_ide_part_table_get(dev_name, disk_desc); |
---|
517 | if (rc != RTEMS_SUCCESSFUL) |
---|
518 | { |
---|
519 | free(disk_desc); |
---|
520 | return rc; |
---|
521 | } |
---|
522 | |
---|
523 | /* To avoid device numbers conflicts we have to use for logic disk the same |
---|
524 | * device major number as ATA device has, and minor number that equals to |
---|
525 | * sum of logic disk partition number and the minor number of physical disk |
---|
526 | */ |
---|
527 | |
---|
528 | rtems_filesystem_split_dev_t (disk_desc->dev, major, minor); |
---|
529 | |
---|
530 | /* create logical disks on the physical one */ |
---|
531 | for (part_num = 0; part_num < disk_desc->last_log_id; part_num++) |
---|
532 | { |
---|
533 | sprintf(name, "%s%d", dev_name, part_num + 1); |
---|
534 | dev = rtems_filesystem_make_dev_t(major, ++minor); |
---|
535 | |
---|
536 | part_desc = disk_desc->partitions[part_num]; |
---|
537 | if (part_desc == NULL) |
---|
538 | { |
---|
539 | continue; |
---|
540 | } |
---|
541 | |
---|
542 | rc = rtems_disk_create_log(dev, disk_desc->dev, part_desc->start, |
---|
543 | part_desc->size, name); |
---|
544 | if (rc != RTEMS_SUCCESSFUL) |
---|
545 | { |
---|
546 | fprintf(stdout,"Cannot create device %s, error code %d\n", name, rc); |
---|
547 | continue; |
---|
548 | } |
---|
549 | } |
---|
550 | |
---|
551 | rtems_ide_part_table_free(disk_desc); |
---|
552 | |
---|
553 | return RTEMS_SUCCESSFUL; |
---|
554 | } |
---|