00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #include "partition.h"
00019 #include "partition_config.h"
00020 #include "config.h"
00021
00022 #include <string.h>
00023
00024 #if USE_DYNAMIC_MEMORY
00025 #include <stdlib.h>
00026 #endif
00027
00047 #if !USE_DYNAMIC_MEMORY
00048 static struct partition_struct partition_handles[PARTITION_COUNT];
00049 #endif
00050
00070 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)
00071 {
00072 struct partition_struct* new_partition = 0;
00073 uint8_t buffer[0x10];
00074
00075 if(!device_read || !device_read_interval || index >= 4)
00076 return 0;
00077
00078 if(index >= 0)
00079 {
00080
00081 if(!device_read(0x01be + index * 0x10, buffer, sizeof(buffer)))
00082 return 0;
00083
00084
00085 if(buffer[4] == 0x00)
00086 return 0;
00087 }
00088
00089
00090 #if USE_DYNAMIC_MEMORY
00091 new_partition = malloc(sizeof(*new_partition));
00092 if(!new_partition)
00093 return 0;
00094 #else
00095 new_partition = partition_handles;
00096 uint8_t i;
00097 for(i = 0; i < PARTITION_COUNT; ++i)
00098 {
00099 if(new_partition->type == PARTITION_TYPE_FREE)
00100 break;
00101
00102 ++new_partition;
00103 }
00104 if(i >= PARTITION_COUNT)
00105 return 0;
00106 #endif
00107
00108 memset(new_partition, 0, sizeof(*new_partition));
00109
00110
00111 new_partition->device_read = device_read;
00112 new_partition->device_read_interval = device_read_interval;
00113 new_partition->device_write = device_write;
00114 new_partition->device_write_interval = device_write_interval;
00115
00116 if(index >= 0)
00117 {
00118 new_partition->type = buffer[4];
00119 new_partition->offset = ((uint32_t) buffer[8]) |
00120 ((uint32_t) buffer[9] << 8) |
00121 ((uint32_t) buffer[10] << 16) |
00122 ((uint32_t) buffer[11] << 24);
00123 new_partition->length = ((uint32_t) buffer[12]) |
00124 ((uint32_t) buffer[13] << 8) |
00125 ((uint32_t) buffer[14] << 16) |
00126 ((uint32_t) buffer[15] << 24);
00127 }
00128 else
00129 {
00130 new_partition->type = 0xff;
00131 }
00132
00133 return new_partition;
00134 }
00135
00148 uint8_t partition_close(struct partition_struct* partition)
00149 {
00150 if(!partition)
00151 return 0;
00152
00153
00154 #if USE_DYNAMIC_MEMORY
00155 free(partition);
00156 #else
00157 partition->type = PARTITION_TYPE_FREE;
00158 #endif
00159
00160 return 1;
00161 }
00162