Hanna
Sourcecode Batteriemanagementsystem
partition.c
gehe zur Dokumentation dieser Datei
1 
14 /*
15  * Copyright (c) 2006-2011 by Roland Riegel <feedback@roland-riegel.de>
16  *
17  * This file is free software; you can redistribute it and/or modify
18  * it under the terms of either the GNU General Public License version 2
19  * or the GNU Lesser General Public License version 2.1, both as
20  * published by the Free Software Foundation.
21  */
22 
23 #include "byteordering.h"
24 #include "partition.h"
25 #include "partition_config.h"
26 #include "sd-reader_config.h"
27 
28 #include <string.h>
29 
30 #if USE_DYNAMIC_MEMORY
31  #include <stdlib.h>
32 #endif
33 
53 #if !USE_DYNAMIC_MEMORY
54 static struct partition_struct partition_handles[PARTITION_COUNT];
55 #endif
56 
78 {
79  struct partition_struct* new_partition = 0;
80  uint8_t buffer[0x10];
81 
82  if(!device_read || !device_read_interval || index >= 4)
83  return 0;
84 
85  if(index >= 0)
86  {
87  /* read specified partition table index */
88  if(!device_read(0x01be + index * 0x10, buffer, sizeof(buffer)))
89  return 0;
90 
91  /* abort on empty partition entry */
92  if(buffer[4] == 0x00)
93  return 0;
94  }
95 
96  /* allocate partition descriptor */
97 #if USE_DYNAMIC_MEMORY
98  new_partition = malloc(sizeof(*new_partition));
99  if(!new_partition)
100  return 0;
101 #else
102  new_partition = partition_handles;
103  uint8_t i;
104  for(i = 0; i < PARTITION_COUNT; ++i)
105  {
106  if(new_partition->type == PARTITION_TYPE_FREE)
107  break;
108 
109  ++new_partition;
110  }
111  if(i >= PARTITION_COUNT)
112  return 0;
113 #endif
114 
115  memset(new_partition, 0, sizeof(*new_partition));
116 
117  /* fill partition descriptor */
118  new_partition->device_read = device_read;
120  new_partition->device_write = device_write;
122 
123  if(index >= 0)
124  {
125  new_partition->type = buffer[4];
126  new_partition->offset = read32(&buffer[8]);
127  new_partition->length = read32(&buffer[12]);
128  }
129  else
130  {
131  new_partition->type = 0xff;
132  }
133 
134  return new_partition;
135 }
136 
149 uint8_t partition_close(struct partition_struct* partition)
150 {
151  if(!partition)
152  return 0;
153 
154  /* destroy partition descriptor */
155 #if USE_DYNAMIC_MEMORY
156  free(partition);
157 #else
158  partition->type = PARTITION_TYPE_FREE;
159 #endif
160 
161  return 1;
162 }
163 
uint8_t(* device_write_t)(offset_t offset, const uint8_t *buffer, uintptr_t length)
Definition: partition.h:131
device_read_t device_read
Definition: partition.h:173
uint8_t(* device_read_t)(offset_t offset, uint8_t *buffer, uintptr_t length)
Definition: partition.h:95
#define PARTITION_COUNT
SD-Card Reader Bibliothek von Roland Riegel. Common SD-reader configuration used by all modules (lice...
SD-Card Reader Bibliothek von Roland Riegel.
device_read_interval_t device_read_interval
Definition: partition.h:180
uint8_t(* device_read_interval_t)(offset_t offset, uint8_t *buffer, uintptr_t interval, uintptr_t length, device_read_callback_t callback, void *p)
Definition: partition.h:123
uint32_t read32(const uint8_t *p)
Definition: byteordering.c:87
uint32_t length
Definition: partition.h:209
uint32_t offset
Definition: partition.h:205
uint8_t partition_close(struct partition_struct *partition)
Definition: partition.c:149
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)
Definition: partition.c:77
SD-Card Reader Bibliothek von Roland Riegel.
device_write_interval_t device_write_interval
Definition: partition.h:194
device_write_t device_write
Definition: partition.h:187
uint8_t(* device_write_interval_t)(offset_t offset, uint8_t *buffer, uintptr_t length, device_write_callback_t callback, void *p)
Definition: partition.h:160
SD-Card Reader Bibliothek von Roland Riegel.
#define PARTITION_TYPE_FREE
Definition: partition.h:50