- Don't access list nodes after their memory has been freed.
[AROS.git] / workbench / tools / HDToolBox / harddisks.c
blobe69fd97dddb4ef7f70de302ac644db89a02d81ce
1 /*
2 Copyright © 1995-2012, The AROS Development Team. All rights reserved.
3 $Id$
4 */
6 #include <proto/exec.h>
7 #include <proto/partition.h>
9 #include <devices/scsidisk.h>
10 #include <exec/io.h>
11 #include <exec/memory.h>
13 #include <stdio.h>
15 #define DEBUG 0
16 #include "debug.h"
18 #include "harddisks.h"
19 #include "hdtoolbox_support.h"
20 #include "platform.h"
22 extern struct GUIGadgets gadgets;
24 void w2strcpy(STRPTR name, UWORD *wstr, ULONG len)
26 STRPTR p = name;
28 while (len)
30 *((UWORD *)p) = AROS_BE2WORD(*wstr);
31 p += sizeof(UWORD);
32 len -= 2;
33 wstr++;
35 p -= 2;
36 while (p >= name && (*p == 0 || *p == ' '))
37 *p-- = 0;
40 BOOL identify(struct IOStdReq *ioreq, STRPTR name)
42 struct SCSICmd scsicmd = {0};
43 UWORD data[256];
44 UBYTE cmd = 0xEC; /* identify */
46 D(bug("[HDToolBox] identify('%s')\n", name));
48 scsicmd.scsi_Data = data;
49 scsicmd.scsi_Length = 512;
50 scsicmd.scsi_Command = &cmd;
51 scsicmd.scsi_CmdLength = 1;
52 ioreq->io_Command = HD_SCSICMD;
53 ioreq->io_Data = &scsicmd;
54 ioreq->io_Length = sizeof(struct SCSICmd);
56 if (DoIO((struct IORequest *)ioreq))
57 return FALSE;
59 w2strcpy(name, &data[27], 40);
60 return TRUE;
63 void findHDs(struct HDTBDevice *parent)
65 struct IOStdReq *ioreq;
66 struct MsgPort *mp;
67 struct HDNode *node;
68 int i;
70 D(bug("[HDToolBox] findHDs()\n"));
72 mp = CreateMsgPort();
73 if (mp)
75 ioreq = (struct IOStdReq *)CreateIORequest(mp, sizeof(struct IOStdReq));
76 if (ioreq)
78 int maxunits = 8;
79 if (parent->maxunits > maxunits)
80 maxunits = parent->maxunits;
82 for (i=0;i<maxunits;i++)
84 node = AllocMem(sizeof(struct HDNode), MEMF_PUBLIC | MEMF_CLEAR);
85 if (node)
87 node->root_partition.listnode.ln.ln_Name = AllocVec(100, MEMF_PUBLIC | MEMF_CLEAR);
88 if (node->root_partition.listnode.ln.ln_Name)
90 if (InitListNode(&node->root_partition.listnode, (struct ListNode *)parent))
92 node->root_partition.listnode.ln.ln_Type = LNT_Harddisk;
93 node->unit = i;
94 if (OpenDevice(parent->listnode.ln.ln_Name, i, (struct IORequest *)ioreq, 0) == 0)
96 D(bug("[HDToolBox] findHDs: Opened %s:%d\n",parent->listnode.ln.ln_Name, i));
97 if (!identify(ioreq, node->root_partition.listnode.ln.ln_Name))
98 sprintf(node->root_partition.listnode.ln.ln_Name, "Unit %d", i);
99 CloseDevice((struct IORequest *)ioreq);
100 node->root_partition.ph = OpenRootPartition(parent->listnode.ln.ln_Name, node->unit);
101 if (node->root_partition.ph)
103 D(bug("[HDToolBox] - appending ROOT partition %p to list %p\n", &node->root_partition.listnode.ln, &parent->listnode.list));
104 D(bug("[HDToolBox] - first entry at %p\n", node->root_partition.listnode.list.lh_Head));
105 AddTail(&parent->listnode.list, &node->root_partition.listnode.ln);
106 if (findPartitionTable(&node->root_partition))
108 D(bug("[HDToolBox] - partition table found. searching for partitions\n"));
109 findPartitions(&node->root_partition.listnode, &node->root_partition);
110 node->root_partition.listnode.flags |= LNF_Listable;
112 else
114 D(bug("[HDToolBox] - partition table not found.\n"));
116 GetPartitionAttrsA
118 node->root_partition.ph,
119 PT_GEOMETRY, &node->root_partition.dg,
120 PT_DOSENVEC, &node->root_partition.de,
121 TAG_DONE
123 continue;
126 UninitListNode(&node->root_partition.listnode);
128 FreeVec(node->root_partition.listnode.ln.ln_Name);
130 FreeMem(node, sizeof(struct HDNode));
133 DeleteIORequest((struct IORequest *)ioreq);
135 DeleteMsgPort(mp);
137 D(bug("[HDToolBox] findHDs() successful\n"));
140 void freeHDList(struct List *list)
142 struct HDNode *node;
143 struct HDNode *next;
145 D(bug("[HDToolBox] freeHDList(%p)\n", list));
147 node = (struct HDNode *)list->lh_Head;
148 while (node->root_partition.listnode.ln.ln_Succ)
150 next = (struct HDNode *)node->root_partition.listnode.ln.ln_Succ;
151 if (node->root_partition.listnode.ln.ln_Type != LNT_Parent)
153 Remove(&node->root_partition.listnode.ln);
154 if (node->root_partition.table)
156 freePartitionList(&node->root_partition.listnode.list);
157 freePartitionTable(&node->root_partition);
159 CloseRootPartition(node->root_partition.ph);
160 UninitListNode(&node->root_partition.listnode);
161 FreeVec(node->root_partition.listnode.ln.ln_Name);
162 FreeMem(node, sizeof(struct HDNode));
164 node = next;