3 * Copyright (c) 2006-2009 by Roland Riegel <feedback@roland-riegel.de>
5 * This file is free software; you can redistribute it and/or modify
6 * it under the terms of either the GNU General Public License version 2
7 * or the GNU Lesser General Public License version 2.1, both as
8 * published by the Free Software Foundation.
11 #include "partition.h"
12 #include "partition_config.h"
13 #include "sd-reader_config.h"
17 #if USE_DYNAMIC_MEMORY
22 * \addtogroup partition Partition table support
24 * Support for reading partition tables and access to partitions.
30 * Partition table implementation (license: GPLv2 or LGPLv2.1)
32 * \author Roland Riegel
36 * \addtogroup partition_config Configuration of partition table support
37 * Preprocessor defines to configure the partition support.
40 #if !USE_DYNAMIC_MEMORY
41 static struct partition_struct partition_handles[PARTITION_COUNT];
47 * Opens a partition by its index number and returns a partition
48 * handle which describes the opened partition.
50 * \note This function does not support extended partitions.
52 * \param[in] device_read A function pointer which is used to read from the disk.
53 * \param[in] device_read_interval A function pointer which is used to read in constant intervals from the disk.
54 * \param[in] device_write A function pointer which is used to write to the disk.
55 * \param[in] device_write_interval A function pointer which is used to write a data stream to disk.
56 * \param[in] index The index of the partition which should be opened, range 0 to 3.
57 * A negative value is allowed as well. In this case, the partition opened is
58 * not checked for existance, begins at offset zero, has a length of zero
59 * and is of an unknown type. Use this in case you want to open the whole device
60 * as a single partition (e.g. for "super floppy" use).
61 * \returns 0 on failure, a partition descriptor on success.
62 * \see partition_close
64 struct partition_struct* partition_open(device_read_t device_read, device_read_interval_t device_read_interval, device_write_t device_write, device_write_interval_t device_write_interval, int8_t index)
66 struct partition_struct* new_partition = 0;
69 if(!device_read || !device_read_interval || index >= 4)
74 /* read specified partition table index */
75 if(!device_read(0x01be + index * 0x10, buffer, sizeof(buffer)))
78 /* abort on empty partition entry */
83 /* allocate partition descriptor */
84 #if USE_DYNAMIC_MEMORY
85 new_partition = malloc(sizeof(*new_partition));
89 new_partition = partition_handles;
91 for(i = 0; i < PARTITION_COUNT; ++i)
93 if(new_partition->type == PARTITION_TYPE_FREE)
98 if(i >= PARTITION_COUNT)
102 memset(new_partition, 0, sizeof(*new_partition));
104 /* fill partition descriptor */
105 new_partition->device_read = device_read;
106 new_partition->device_read_interval = device_read_interval;
107 new_partition->device_write = device_write;
108 new_partition->device_write_interval = device_write_interval;
112 new_partition->type = buffer[4];
113 new_partition->offset = ((uint32_t) buffer[8]) |
114 ((uint32_t) buffer[9] << 8) |
115 ((uint32_t) buffer[10] << 16) |
116 ((uint32_t) buffer[11] << 24);
117 new_partition->length = ((uint32_t) buffer[12]) |
118 ((uint32_t) buffer[13] << 8) |
119 ((uint32_t) buffer[14] << 16) |
120 ((uint32_t) buffer[15] << 24);
124 new_partition->type = 0xff;
127 return new_partition;
131 * Closes a partition.
133 * This function destroys a partition descriptor which was
134 * previously obtained from a call to partition_open().
135 * When this function returns, the given descriptor will be
138 * \param[in] partition The partition descriptor to destroy.
139 * \returns 0 on failure, 1 on success.
140 * \see partition_open
142 uint8_t partition_close(struct partition_struct* partition)
147 /* destroy partition descriptor */
148 #if USE_DYNAMIC_MEMORY
151 partition->type = PARTITION_TYPE_FREE;