# add some debug.
[AROS.git] / rom / partition / partitionebr.c
blob340dc44ac488528a0157b1a5f6717fcfc1900968
1 /*
2 Copyright © 1995-2017, The AROS Development Team. All rights reserved.
3 $Id$
5 */
7 #include <exec/memory.h>
8 #include <exec/types.h>
9 #include <libraries/partition.h>
10 #include <proto/exec.h>
11 #include <proto/partition.h>
12 #include <proto/utility.h>
14 #include "partition_types.h"
15 #include "partition_support.h"
16 #include "partitionmbr.h"
17 #include "platform.h"
18 #include "debug.h"
20 struct EBRData
22 UBYTE type;
23 ULONG ebr_block_no; /* Home block of the record itself */
24 ULONG block_no; /* Start partition block */
25 ULONG block_count; /* Length of partition in blocks */
28 static LONG PartitionEBRCheckPartitionTable
30 struct Library *PartitionBase,
31 struct PartitionHandle *root
34 union {
35 struct MBR mbr;
36 UBYTE space[4096];
37 } sector;
39 struct PartitionType type;
40 struct TagItem tags[] = {{PT_TYPE, (IPTR)&type}, {TAG_DONE, 0}};
42 if ((root->de.de_SizeBlock << 2) > sizeof(sector))
43 return 0;
45 if (readBlock(PartitionBase, root, 0, &sector.mbr) == 0)
47 if (AROS_LE2WORD(sector.mbr.magic) == 0xAA55)
49 if (root->root != NULL)
51 GetPartitionAttrs(root, tags);
52 if (
53 (root->root->table->type == PHPTT_MBR) &&
54 (type.id[0] == MBRT_EXTENDED
55 || type.id[0] == MBRT_EXTENDED2)
58 return 1;
63 return 0;
66 static struct PartitionHandle *PartitionEBRNewHandle(struct Library *PartitionBase, struct PartitionHandle *root,
67 UBYTE type, ULONG block_no, ULONG block_count, ULONG ebr_block_no)
69 struct PartitionHandle *ph;
71 ph = AllocMem(sizeof(struct PartitionHandle), MEMF_PUBLIC | MEMF_CLEAR);
72 if (ph != NULL)
74 struct EBRData *data;
76 data = AllocMem(sizeof(struct EBRData), MEMF_PUBLIC);
77 if (data != NULL)
79 data->type = type;
80 data->ebr_block_no = ebr_block_no;
81 data->block_no = block_no;
82 data->block_count = block_count;
84 ph->data = data;
86 /* Initialize DosEnvec and DriveGeometry */
87 initPartitionHandle(root, ph, block_no, block_count);
89 /* Map type ID to a DOSType */
90 setDosType(&ph->de, MBR_FindDosType(data->type));
92 return ph;
94 FreeMem(ph, sizeof(struct PartitionHandle));
96 return NULL;
99 static LONG PartitionEBROpenPartitionTable
101 struct Library *PartitionBase,
102 struct PartitionHandle *root
105 LONG error = 0;
106 struct PartitionHandle *ph;
107 struct MBR *ebr;
108 UBYTE i;
109 ULONG block_no = 0;
110 BOOL atEnd = FALSE;
112 ebr = AllocMem(root->de.de_SizeBlock<<2, MEMF_PUBLIC);
113 if (ebr != NULL)
115 NEWLIST(&root->table->list);
116 for (i = 0; !atEnd && error == 0; i++)
118 if (readBlock(PartitionBase, root, block_no, ebr) == 0)
120 if (AROS_LE2WORD(ebr->magic) == 0xAA55)
122 /* Create handle for current EBR's logical partition */
124 if (AROS_LE2LONG(ebr->pcpt[0].count_sector) != 0)
126 ph = PartitionEBRNewHandle(PartitionBase, root,
127 ebr->pcpt[0].type,
128 block_no + AROS_LE2LONG(ebr->pcpt[0].first_sector),
129 AROS_LE2LONG(ebr->pcpt[0].count_sector),
130 block_no);
131 if (ph != NULL)
133 Enqueue(&root->table->list, &ph->ln);
135 else
136 error = 1;
139 /* Get location of next EBR in chain */
141 block_no = AROS_LE2LONG(ebr->pcpt[1].first_sector);
142 if (block_no == 0)
143 atEnd = TRUE;
145 else
146 error = 1;
148 else
149 error = 1;
151 FreeMem(ebr, root->de.de_SizeBlock<<2);
153 else
154 error = 1;
155 return error;
158 static void PartitionEBRFreeHandle
160 struct Library *PartitionBase,
161 struct PartitionHandle *ph
164 ClosePartitionTable(ph);
165 FreeMem(ph->data, sizeof(struct EBRData));
166 FreeMem(ph, sizeof(struct PartitionHandle));
169 static void PartitionEBRClosePartitionTable
171 struct Library *PartitionBase,
172 struct PartitionHandle *root
175 struct PartitionHandle *ph;
177 while ((ph = (struct PartitionHandle *)RemTail(&root->table->list)))
178 PartitionEBRFreeHandle(PartitionBase, ph);
181 static LONG PartitionEBRWritePartitionTable
183 struct Library *PartitionBase,
184 struct PartitionHandle *root
187 ULONG block_no, block_count;
188 struct PartitionHandle *ph, *next_ph, *tail;
189 struct EBRData *data, *next_data;
190 struct DosEnvec *de;
191 struct MBR *ebr;
192 LONG error = 0;
194 ebr = AllocMem(root->de.de_SizeBlock << 2, MEMF_PUBLIC | MEMF_CLEAR);
195 if (ebr != NULL)
197 ph = (struct PartitionHandle *)root->table->list.lh_Head;
198 tail = (struct PartitionHandle *)&root->table->list.lh_Tail;
200 if (ph == tail)
202 /* Write an empty EBR block for empty partition table */
204 ebr->magic = AROS_WORD2LE(0xAA55);
205 if (writeBlock(PartitionBase, root, 0, ebr))
206 error = 1;
209 else
211 /* First EBR must be at extended partition's first block */
212 data = (struct EBRData *)ph->data;
213 data->ebr_block_no = 0;
215 /* Write an EBR block for every partition in the list */
217 while (ph != tail)
219 data = (struct EBRData *)ph->data;
220 next_ph = (struct PartitionHandle *)ph->ln.ln_Succ;
222 /* Write table entry for current EBR's logical partition */
224 de = &ph->de;
225 block_no = de->de_LowCyl * de->de_Surfaces * de->de_BlocksPerTrack;
226 block_count = (de->de_HighCyl - de->de_LowCyl + 1) *
227 de->de_Surfaces * de->de_BlocksPerTrack;
229 PartitionMBRSetGeometry(root, &ebr->pcpt[0], block_no,
230 block_count, data->ebr_block_no);
231 ebr->pcpt[0].status = 0;
232 ebr->pcpt[0].type = data->type;
234 /* Write table entry that points to next EBR in chain */
236 if (next_ph != tail)
238 de = &root->de;
239 next_data = (struct EBRData *)next_ph->data;
240 block_no = next_data->ebr_block_no;
241 block_count = (de->de_HighCyl - de->de_LowCyl + 1) *
242 de->de_Surfaces * de->de_BlocksPerTrack - block_no;
243 ebr->pcpt[1].type = MBRT_EXTENDED;
245 else
247 block_no = 0;
248 block_count = 0;
249 ebr->pcpt[1].type = 0;
251 PartitionMBRSetGeometry(root, &ebr->pcpt[1], block_no, block_count, 0);
252 ebr->pcpt[1].status = 0;
254 /* Write EBR */
256 ebr->magic = AROS_WORD2LE(0xAA55);
257 if (writeBlock(PartitionBase, root, data->ebr_block_no, ebr))
258 error = 1;
260 ph = next_ph;
264 FreeMem(ebr, root->de.de_SizeBlock << 2);
266 else
267 error = 1;
269 return error;
272 static LONG PartitionEBRCreatePartitionTable
274 struct Library *PartitionBase,
275 struct PartitionHandle *ph
278 NEWLIST(&ph->table->list);
279 return 0;
282 static struct PartitionHandle *PartitionEBRAddPartition(struct Library *PartitionBase, struct PartitionHandle *root, const struct TagItem *taglist)
284 struct TagItem *tag;
285 ULONG block_no = 0, new_block_no, new_block_count, ebr_track_no, ebr_block_no;
286 struct PartitionHandle *ph, *new_ph, *head;
287 struct DosEnvec *de;
288 BOOL found = FALSE;
289 struct PartitionType *ptype;
290 LONG error = 0;
292 tag = FindTagItem(PT_DOSENVEC, taglist);
293 if (tag == NULL)
294 error = 1;
296 if (error == 0)
298 de = (struct DosEnvec *)tag->ti_Data;
300 tag = FindTagItem(PT_TYPE, taglist);
301 if (tag == NULL)
302 error = 1;
305 if (error == 0)
307 ptype = (struct PartitionType *)tag->ti_Data;
309 new_block_no =
310 de->de_LowCyl * de->de_Surfaces * de->de_BlocksPerTrack;
311 new_block_count = (de->de_HighCyl - de->de_LowCyl + 1) *
312 de->de_Surfaces * de->de_BlocksPerTrack;
314 /* Find the position in the chain/list of EBRs at which to insert
315 new EBR */
317 ph = (struct PartitionHandle *)root->table->list.lh_TailPred;
318 head = (struct PartitionHandle *)&root->table->list;
320 while (ph != head && !found)
322 de = &ph->de;
323 block_no = de->de_LowCyl * de->de_Surfaces * de->de_BlocksPerTrack;
324 if (block_no < new_block_no)
325 found = TRUE;
326 else
327 ph = (struct PartitionHandle *)ph->ln.ln_Pred;
330 /* Calculate appropriate location for new EBR */
332 de = &root->de;
333 ebr_track_no = (new_block_no - 1) / de->de_BlocksPerTrack;
334 ebr_block_no = ebr_track_no * de->de_BlocksPerTrack;
336 /* Create new handle and add it to list */
338 new_ph = PartitionEBRNewHandle(PartitionBase, root, ptype->id[0],
339 new_block_no, new_block_count, ebr_block_no);
340 Insert(&root->table->list, &new_ph->ln, &ph->ln);
342 return new_ph;
344 return NULL;
347 static void PartitionEBRDeletePartition
349 struct Library *PartitionBase,
350 struct PartitionHandle *ph
353 Remove(&ph->ln);
354 PartitionEBRFreeHandle(PartitionBase, ph);
357 static LONG PartitionEBRGetPartitionTableAttr(struct Library *PartitionBase,
358 struct PartitionHandle *root, const struct TagItem *tag)
360 switch (tag->ti_Tag)
362 case PTT_MAXLEADIN:
363 *((LONG *)tag->ti_Data) = root->de.de_BlocksPerTrack;
364 return TRUE;
367 return 0;
370 static LONG PartitionEBRGetPartitionAttr(struct Library *PartitionBase,
371 struct PartitionHandle *ph, const struct TagItem *tag)
373 struct EBRData *data = (struct EBRData *)ph->data;
375 switch (tag->ti_Tag)
377 case PT_TYPE:
378 PTYPE(tag->ti_Data)->id[0] = data->type;
379 PTYPE(tag->ti_Data)->id_len = 1;
380 return TRUE;
382 case PT_STARTBLOCK:
383 *((UQUAD *)tag->ti_Data) = data->block_no;
384 return TRUE;
386 case PT_ENDBLOCK:
387 *((UQUAD *)tag->ti_Data) = data->block_no + data->block_count - 1;
388 return TRUE;
391 return 0;
394 static LONG PartitionEBRSetPartitionAttrs(struct Library *PartitionBase, struct PartitionHandle *ph, const struct TagItem *taglist)
396 struct EBRData *data = (struct EBRData *)ph->data;
397 struct TagItem *tag;
399 while ((tag = NextTagItem((struct TagItem **)&taglist)))
401 switch (tag->ti_Tag)
403 case PT_DOSENVEC:
404 // TO DO: move handle to new position in list
405 CopyMem((struct DosEnvec *)tag->ti_Data, &ph->de, sizeof(struct DosEnvec));
406 break;
407 case PT_TYPE:
408 data->type = PTYPE(tag->ti_Data)->id[0]; // fix
409 /* Update DOSType according to a new type ID */
410 setDosType(&ph->de, MBR_FindDosType(data->type));
411 break;
415 return 0;
418 static const struct PartitionAttribute PartitionEBRPartitionTableAttrs[]=
420 {PTT_TYPE, PLAM_READ},
421 {PTT_RESERVED, PLAM_READ},
422 {PTT_MAXLEADIN, PLAM_READ},
423 {TAG_DONE, 0}
426 static const struct PartitionAttribute PartitionEBRPartitionAttrs[]=
428 {PT_GEOMETRY, PLAM_READ},
429 {PT_TYPE, PLAM_READ | PLAM_WRITE},
430 {PT_DOSENVEC, PLAM_READ | PLAM_WRITE},
431 {PT_POSITION, PLAM_READ},
432 {PT_LEADIN, PLAM_READ},
433 {PT_STARTBLOCK, PLAM_READ},
434 {PT_ENDBLOCK, PLAM_READ},
435 {TAG_DONE, 0}
438 static ULONG PartitionEBRDestroyPartitionTable
440 struct Library *PartitionBase,
441 struct PartitionHandle *root
444 struct PartitionHandle *ph;
447 while ((ph = (struct PartitionHandle *)RemHead(&root->table->list))
448 != NULL)
450 PartitionEBRDeletePartition(PartitionBase, ph);
452 PartitionEBRWritePartitionTable(PartitionBase, root);
454 return 0;
457 const struct PTFunctionTable PartitionEBR =
459 PHPTT_EBR,
460 "PC-EBR",
461 PartitionEBRCheckPartitionTable,
462 PartitionEBROpenPartitionTable,
463 PartitionEBRClosePartitionTable,
464 PartitionEBRWritePartitionTable,
465 PartitionEBRCreatePartitionTable,
466 PartitionEBRAddPartition,
467 PartitionEBRDeletePartition,
468 PartitionEBRGetPartitionTableAttr,
469 NULL,
470 PartitionEBRGetPartitionAttr,
471 PartitionEBRSetPartitionAttrs,
472 PartitionEBRPartitionTableAttrs,
473 PartitionEBRPartitionAttrs,
474 PartitionEBRDestroyPartitionTable,
475 NULL