00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 #include "partition.h"
00012 #include "partition_config.h"
00013 #include "sd-reader_config.h"
00014
00015 #include <string.h>
00016
00017 #if USE_DYNAMIC_MEMORY
00018 #include <stdlib.h>
00019 #endif
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040 #if !USE_DYNAMIC_MEMORY
00041 static struct partition_struct partition_handles[PARTITION_COUNT];
00042 #endif
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064 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)
00065 {
00066 struct partition_struct* new_partition = 0;
00067 uint8_t buffer[0x10];
00068
00069 if(!device_read || !device_read_interval || index >= 4)
00070 return 0;
00071
00072 if(index >= 0)
00073 {
00074
00075 if(!device_read(0x01be + index * 0x10, buffer, sizeof(buffer)))
00076 return 0;
00077
00078
00079 if(buffer[4] == 0x00)
00080 return 0;
00081 }
00082
00083
00084 #if USE_DYNAMIC_MEMORY
00085 new_partition = malloc(sizeof(*new_partition));
00086 if(!new_partition)
00087 return 0;
00088 #else
00089 new_partition = partition_handles;
00090 uint8_t i;
00091 for(i = 0; i < PARTITION_COUNT; ++i)
00092 {
00093 if(new_partition->type == PARTITION_TYPE_FREE)
00094 break;
00095
00096 ++new_partition;
00097 }
00098 if(i >= PARTITION_COUNT)
00099 return 0;
00100 #endif
00101
00102 memset(new_partition, 0, sizeof(*new_partition));
00103
00104
00105 new_partition->device_read = device_read;
00106 new_partition->device_read_interval = device_read_interval;
00107 new_partition->device_write = device_write;
00108 new_partition->device_write_interval = device_write_interval;
00109
00110 if(index >= 0)
00111 {
00112 new_partition->type = buffer[4];
00113 new_partition->offset = ((uint32_t) buffer[8]) |
00114 ((uint32_t) buffer[9] << 8) |
00115 ((uint32_t) buffer[10] << 16) |
00116 ((uint32_t) buffer[11] << 24);
00117 new_partition->length = ((uint32_t) buffer[12]) |
00118 ((uint32_t) buffer[13] << 8) |
00119 ((uint32_t) buffer[14] << 16) |
00120 ((uint32_t) buffer[15] << 24);
00121 }
00122 else
00123 {
00124 new_partition->type = 0xff;
00125 }
00126
00127 return new_partition;
00128 }
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142 uint8_t partition_close(struct partition_struct* partition)
00143 {
00144 if(!partition)
00145 return 0;
00146
00147
00148 #if USE_DYNAMIC_MEMORY
00149 free(partition);
00150 #else
00151 partition->type = PARTITION_TYPE_FREE;
00152 #endif
00153
00154 return 1;
00155 }
00156
00157
00158
00159
00160