The DOS type wasn't kept up-to-date in the local DosEnvec structure.
[tangerine.git] / workbench / tools / HDToolBox / partitions.c
blob4e6803099cd77a8e53e0b373d559638ab891ada4
1 /*
2 Copyright © 1995-2002, The AROS Development Team. All rights reserved.
3 $Id$
4 */
7 #include <stdio.h>
8 #include <strings.h>
9 #include <proto/exec.h>
10 #include <proto/gadtools.h>
11 #include <proto/intuition.h>
12 #include <proto/partition.h>
14 #include <devices/trackdisk.h>
15 #include <exec/memory.h>
16 #include <utility/tagitem.h>
18 #include "partitions.h"
19 #include "hdtoolbox_support.h"
20 #include "platform.h"
21 #include "prefs.h"
23 #include "debug.h"
25 void setPartitionName(struct HDTBPartition *pnode) {
27 if (pnode->pos != -1)
28 sprintf(pnode->listnode.ln.ln_Name, "Partition %ld", pnode->pos);
29 else
31 sprintf
33 pnode->listnode.ln.ln_Name,
34 "%ld-%ld",
35 pnode->de.de_LowCyl,
36 pnode->de.de_HighCyl
41 struct HDTBPartition *newPartition
43 struct ListNode *parent,
44 struct HDTBPartition *partition
47 struct HDTBPartition *pn;
49 pn = AllocMem(sizeof(struct HDTBPartition), MEMF_PUBLIC | MEMF_CLEAR);
50 if (pn)
52 pn->listnode.ln.ln_Name = AllocVec(100, MEMF_PUBLIC | MEMF_CLEAR);
53 if (pn->listnode.ln.ln_Name)
55 if (InitListNode(&pn->listnode, parent))
57 pn->listnode.ln.ln_Type = LNT_Partition;
58 pn->root = partition;
59 return pn;
61 FreeVec(pn->listnode.ln.ln_Name);
63 FreeMem(pn, sizeof(struct HDTBPartition));
65 return NULL;
68 void findPartitions(struct ListNode *parent, struct HDTBPartition *partition) {
69 struct PartitionHandle *ph;
70 struct HDTBPartition *pn;
71 LONG flag;
73 for
75 ph = (struct PartitionHandle *)partition->ph->table->list.lh_Head;
76 ph->ln.ln_Succ;
77 ph = (struct PartitionHandle *)ph->ln.ln_Succ
80 pn = newPartition(parent, partition);
81 if (pn != NULL)
83 pn->ph = ph;
84 GetPartitionAttrsA
86 pn->ph,
87 PT_GEOMETRY, &pn->dg,
88 PT_DOSENVEC, &pn->de,
89 TAG_DONE
91 if (getAttrInfo(pn->root->table->pattrlist, PTA_TYPE) & PLAM_READ)
92 GetPartitionAttrsA(pn->ph, PT_TYPE, &pn->type, TAG_DONE);
93 else
95 pn->type.id[0] = 0;
96 pn->type.id_len = 0;
98 if (getAttrInfo(pn->root->table->pattrlist, PTA_POSITION) & PLAM_READ)
99 GetPartitionAttrsA(pn->ph, PT_POSITION, &pn->pos, TAG_DONE);
100 else
101 pn->pos = -1L;
102 if (getAttrInfo(pn->root->table->pattrlist, PTA_NAME) & PLAM_READ)
103 GetPartitionAttrsA(pn->ph, PT_NAME, pn->listnode.ln.ln_Name, TAG_DONE);
104 else
105 setPartitionName(pn);
106 if (getAttrInfo(pn->root->table->pattrlist, PTA_BOOTABLE) & PLAM_READ)
108 GetPartitionAttrsA(pn->ph, PT_BOOTABLE, &flag, TAG_DONE);
109 if (flag)
110 pn->flags |= PNF_BOOTABLE;
112 if (getAttrInfo(pn->root->table->pattrlist, PTA_AUTOMOUNT) & PLAM_READ)
114 GetPartitionAttrsA(pn->ph, PT_AUTOMOUNT, &flag, TAG_DONE);
115 if (flag)
116 pn->flags |= PNF_AUTOMOUNT;
118 if (getAttrInfo(pn->root->table->pattrlist, PTA_ACTIVE) & PLAM_READ)
120 GetPartitionAttrsA(pn->ph, PT_ACTIVE, &flag, TAG_DONE);
121 if (flag)
122 pn->flags |= PNF_ACTIVE;
124 if (pn->root->table->max_partitions)
126 pn->listnode.ln.ln_Pri=pn->root->table->max_partitions-1-pn->pos;
127 Enqueue(&parent->list, &pn->listnode.ln);
129 else
130 AddTail(&parent->list, &pn->listnode.ln);
131 if (findPartitionTable(pn))
133 findPartitions(&pn->listnode, pn);
134 pn->listnode.flags |= LNF_Listable;
140 void freePartitionNode(struct HDTBPartition *node) {
142 if (node->table)
144 freePartitionList(&node->listnode.list);
145 freePartitionTable(node);
147 UninitListNode(&node->listnode);
148 FreeVec(node->listnode.ln.ln_Name);
149 FreeMem(node, sizeof(struct HDTBPartition));
152 void freePartitionList(struct List *list) {
153 struct HDTBPartition *node;
154 struct HDTBPartition *next;
156 node = (struct HDTBPartition *)list->lh_Head;
157 while (node->listnode.ln.ln_Succ)
159 next = (struct HDTBPartition *)node->listnode.ln.ln_Succ;
160 if (node->listnode.ln.ln_Type != LNT_Parent)
162 Remove(&node->listnode.ln);
163 freePartitionNode(node);
165 node = next;
169 BOOL validValue
171 struct HDTBPartition *table,
172 struct HDTBPartition *current,
173 ULONG value
176 struct HDTBPartition *pn;
177 ULONG spc;
179 if (value<table->table->reserved)
180 return FALSE;
181 spc = table->dg.dg_Heads*table->dg.dg_TrackSectors;
182 if (value>=(table->dg.dg_Cylinders*spc))
183 return FALSE;
184 pn = (struct HDTBPartition *)table->listnode.list.lh_Head;
185 while (pn->listnode.ln.ln_Succ)
187 /* dont't check currently processed partition */
188 if (current != pn)
190 spc = pn->de.de_Surfaces*pn->de.de_BlocksPerTrack;
191 if (
192 (value >= (pn->de.de_LowCyl*spc)) &&
193 (value < (((pn->de.de_HighCyl+1)*spc)-1))
195 return FALSE;
197 pn = (struct HDTBPartition *)pn->listnode.ln.ln_Succ;
199 return TRUE;
202 struct PartitionHandle *AddPartitionA
204 struct PartitionHandle *root,
205 LONG tag,
210 return AddPartition(root, (struct TagItem *)&tag);
213 struct HDTBPartition *addPartition
215 struct HDTBPartition *table,
216 struct DosEnvec *de
219 struct HDTBPartition *partition;
220 struct HDTBPartition *pn;
221 struct TableTypeNode *ttn;
223 partition = newPartition(&table->listnode, table);
224 if (partition)
226 if (table->table->max_partitions)
228 /* find first free position */
229 ULONG count = 0;
230 pn = (struct HDTBPartition *)table->listnode.list.lh_Head;
231 while (pn->listnode.ln.ln_Succ)
233 if (pn->listnode.ln.ln_Type != LNT_Parent)
235 if (count!=pn->pos)
237 partition->pos = count;
238 break;
240 count++;
242 pn = (struct HDTBPartition *)pn->listnode.ln.ln_Succ;
244 if (pn->listnode.ln.ln_Succ == NULL)
245 partition->pos = count;
246 partition->listnode.ln.ln_Pri = table->table->max_partitions-1-partition->pos;
247 Enqueue(&table->listnode.list, &partition->listnode.ln);
249 else
250 AddTail(&table->listnode.list, &partition->listnode.ln);
251 de->de_TableSize = DE_DOSTYPE;
252 de->de_MaxTransfer = 0xFFFFFF;
253 de->de_Mask = 0xFFFFFFFE;
254 de->de_Reserved = 2;
255 de->de_BufMemType = MEMF_ANY;
256 CopyMem(de, &partition->de, sizeof(struct DosEnvec));
257 ttn = findTableTypeNode(table->table->type);
258 CopyMem(&ttn->defaulttype, &partition->type, sizeof(struct PartitionType));
259 if (getAttrInfo(table->table->pattrlist, PTA_NAME) & PLAM_WRITE)
260 strcpy(partition->listnode.ln.ln_Name, "DH0");
261 else
262 setPartitionName(partition);
263 partition->ph = AddPartitionA
265 table->ph,
266 PT_DOSENVEC, de,
267 PT_TYPE, &partition->type,
268 PT_NAME, partition->listnode.ln.ln_Name,
269 PT_POSITION, partition->pos,
270 TAG_DONE
272 if (partition->ph)
274 /* we did not set GEOMETRY so partitionlib did it */
275 GetPartitionAttrsA
277 partition->ph,
278 PT_GEOMETRY, &partition->dg,
279 TAG_DONE
281 /* Update DOS type in local DOSEnvec */
282 GetPartitionAttrsA(partition->ph, PT_DOSENVEC, &partition->de, TAG_DONE);
283 return partition;
285 freePartitionNode(partition);
287 return NULL;