3 * Copyright (c) 2006-2012 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 "byteordering.h"
12 #include "partition.h"
13 #include "partition_config.h"
14 #include "sd-reader_config.h"
18 #if USE_DYNAMIC_MEMORY
23 * \addtogroup partition Partition table support
25 * Support for reading partition tables and access to partitions.
31 * Partition table implementation (license: GPLv2 or LGPLv2.1)
33 * \author Roland Riegel
37 * \addtogroup partition_config Configuration of partition table support
38 * Preprocessor defines to configure the partition support.
41 #if !USE_DYNAMIC_MEMORY
42 static struct partition_struct partition_handles[PARTITION_COUNT];
48 * Opens a partition by its index number and returns a partition
49 * handle which describes the opened partition.
51 * \note This function does not support extended partitions.
53 * \param[in] device_read A function pointer which is used to read from the disk.
54 * \param[in] device_read_interval A function pointer which is used to read in constant intervals from the disk.
55 * \param[in] device_write A function pointer which is used to write to the disk.
56 * \param[in] device_write_interval A function pointer which is used to write a data stream to disk.
57 * \param[in] index The index of the partition which should be opened, range 0 to 3.
58 * A negative value is allowed as well. In this case, the partition opened is
59 * not checked for existance, begins at offset zero, has a length of zero
60 * and is of an unknown type. Use this in case you want to open the whole device
61 * as a single partition (e.g. for "super floppy" use).
62 * \returns 0 on failure, a partition descriptor on success.
63 * \see partition_close
65 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)
67 struct partition_struct* new_partition = 0;
70 if(!device_read || !device_read_interval || index >= 4)
75 /* read specified partition table index */
76 if(!device_read(0x01be + index * 0x10, buffer, sizeof(buffer)))
79 /* abort on empty partition entry */
84 /* allocate partition descriptor */
85 #if USE_DYNAMIC_MEMORY
86 new_partition = malloc(sizeof(*new_partition));
90 new_partition = partition_handles;
92 for(i = 0; i < PARTITION_COUNT; ++i)
94 if(new_partition->type == PARTITION_TYPE_FREE)
99 if(i >= PARTITION_COUNT)
103 memset(new_partition, 0, sizeof(*new_partition));
105 /* fill partition descriptor */
106 new_partition->device_read = device_read;
107 new_partition->device_read_interval = device_read_interval;
108 new_partition->device_write = device_write;
109 new_partition->device_write_interval = device_write_interval;
113 new_partition->type = buffer[4];
114 new_partition->offset = read32(&buffer[8]);
115 new_partition->length = read32(&buffer[12]);
119 new_partition->type = 0xff;
122 return new_partition;
126 * Closes a partition.
128 * This function destroys a partition descriptor which was
129 * previously obtained from a call to partition_open().
130 * When this function returns, the given descriptor will be
133 * \param[in] partition The partition descriptor to destroy.
134 * \returns 0 on failure, 1 on success.
135 * \see partition_open
137 uint8_t partition_close(struct partition_struct* partition)
142 /* destroy partition descriptor */
143 #if USE_DYNAMIC_MEMORY
146 partition->type = PARTITION_TYPE_FREE;