gcc-4.6.2: Update with patch for gengtype.c
[AROS.git] / rom / dosboot / bootscan.c
blobe83bc643d924d9c5b931fc1e114d055ac361bac5
1 /*
2 Copyright © 1995-2011, The AROS Development Team. All rights reserved.
3 $Id$
5 Desc: Discover all mountable partitions
6 Lang: english
7 */
9 #include <string.h>
10 #include <stdlib.h>
12 #include <aros/debug.h>
13 #include <exec/alerts.h>
14 #include <aros/asmcall.h>
15 #include <aros/bootloader.h>
16 #include <exec/lists.h>
17 #include <exec/memory.h>
18 #include <exec/resident.h>
19 #include <exec/types.h>
20 #include <libraries/configvars.h>
21 #include <libraries/expansion.h>
22 #include <libraries/expansionbase.h>
23 #include <libraries/partition.h>
24 #include <utility/tagitem.h>
25 #include <devices/bootblock.h>
26 #include <devices/timer.h>
27 #include <dos/dosextens.h>
28 #include <resources/filesysres.h>
30 #include <proto/exec.h>
31 #include <proto/expansion.h>
32 #include <proto/partition.h>
33 #include <proto/bootloader.h>
35 #include LC_LIBDEFS_FILE
37 #include "dosboot_intern.h"
39 #define uppercase(x) ((x >= 'a' && x <= 'z') ? (x & 0xdf) : x)
41 static ULONG GetOffset(struct Library *PartitionBase, struct PartitionHandle *ph)
43 IPTR tags[3];
44 struct DosEnvec de;
45 ULONG offset = 0;
47 tags[0] = PT_DOSENVEC;
48 tags[1] = (IPTR)&de;
49 tags[2] = TAG_DONE;
50 ph = ph->root;
51 while (ph->root)
53 GetPartitionAttrs(ph, (struct TagItem *)tags);
54 offset += de.de_LowCyl * de.de_Surfaces * de.de_BlocksPerTrack;
55 ph = ph->root;
57 return offset;
60 static VOID AddPartitionVolume(struct ExpansionBase *ExpansionBase, struct Library *PartitionBase,
61 struct FileSysStartupMsg *fssm, struct PartitionHandle *table,
62 struct PartitionHandle *pn, struct ExecBase *SysBase)
64 UBYTE name[32];
65 ULONG i, blockspercyl;
66 const struct PartitionAttribute *attrs;
67 IPTR tags[7];
68 IPTR pp[4 + DE_BOOTBLOCKS + 1] = { };
69 struct DeviceNode *devnode;
70 LONG ppos;
71 TEXT *devname;
72 LONG bootable;
73 ULONG pttype = PHPTT_UNKNOWN;
74 BOOL appended, changed;
75 struct Node *fsnode;
77 D(bug("[Boot] AddPartitionVolume\n"));
78 GetPartitionTableAttrsTags(table, PTT_TYPE, &pttype, TAG_DONE);
80 attrs = QueryPartitionAttrs(table);
81 while ((attrs->attribute != TAG_DONE) && (attrs->attribute != PT_NAME))
82 attrs++; /* look for name attr */
84 if (attrs->attribute != TAG_DONE)
86 D(bug("[Boot] RDB/GPT partition\n"));
88 /* partition has a name => RDB/GPT partition */
89 tags[0] = PT_NAME;
90 tags[1] = (IPTR)name;
91 tags[2] = PT_DOSENVEC;
92 tags[3] = (IPTR)&pp[4];
93 tags[4] = PT_BOOTABLE;
94 tags[5] = (IPTR)&bootable;
95 tags[6] = TAG_DONE;
96 GetPartitionAttrs(pn, (struct TagItem *)tags);
98 D(bug("[Boot] Partition name: %s bootable: %d\n", name, bootable));
100 else
102 D(bug("[Boot] MBR/EBR partition\n"));
104 /* partition doesn't have a name => MBR/EBR partition */
105 tags[0] = PT_POSITION;
106 tags[1] = (IPTR)&ppos;
107 tags[2] = PT_DOSENVEC;
108 tags[3] = (IPTR)&pp[4];
109 tags[4] = TAG_DONE;
110 GetPartitionAttrs(pn, (struct TagItem *)tags);
113 * 'Active' is not the same as 'Bootable'. Theoretically we can set Active flag for multiple
114 * partitions, but this may screw up Microsoft system software which expects to see only one
115 * active partition.
117 bootable = TRUE;
119 /* make the name */
120 devname = AROS_BSTR_ADDR(fssm->fssm_Device);
121 for (i = 0; i < 26; i++)
123 if (*devname == '.' || *devname == '\0')
124 break;
125 name[i] = (UBYTE)uppercase(*devname);
126 devname++;
128 if ((fssm->fssm_Unit / 10))
129 name[i++] = '0' + (UBYTE)(fssm->fssm_Unit / 10);
130 name[i++] = '0' + (UBYTE)(fssm->fssm_Unit % 10);
131 name[i++] = 'P';
132 if (table->table->type == PHPTT_EBR)
133 ppos += 4;
134 if ((ppos / 10))
135 name[i++] = '0' + (UBYTE)(ppos / 10);
136 name[i++] = '0' + (UBYTE)(ppos % 10);
137 name[i] = '\0';
139 D(bug("[Boot] Partition name: %s\n", name));
142 if ((pp[4 + DE_TABLESIZE] < DE_DOSTYPE) || (pp[4 + DE_DOSTYPE] == 0))
145 * partition.library reports DosType == 0 for unknown filesystems.
146 * However dos.library will mount such DeviceNodes using rn_DefaultHandler
147 * (FFS). This is done for compatibility with 3rd party expansion ROMs.
148 * Here we ignore partitions with DosType == 0 and won't enter them into
149 * mountlist.
151 D(bug("[Boot] Unknown DosType for %s, skipping partition\n"));
152 return;
155 if (pttype != PHPTT_RDB)
158 * Only RDB partitions can store the complete DosEnvec.
159 * For other partition types partition.library puts some defaults
160 * into these fields, however they do not have anything to do with
161 * real values, which are device-dependent.
162 * However, the device itself knows them. Here we inherit these settings
163 * from the original DeviceNode which represents the whole drive.
164 * Note that we don't change DosEnvec size. If these fields are not included,
165 * it will stay this way.
166 * Copy members only if they are present in device's DosEnvec.
168 struct DosEnvec *devenv = BADDR(fssm->fssm_Environ);
170 if (devenv->de_TableSize >= DE_MAXTRANSFER)
172 pp[4 + DE_MAXTRANSFER] = devenv->de_MaxTransfer;
174 if (devenv->de_TableSize >= DE_MASK)
175 pp[4 + DE_MASK] = devenv->de_Mask;
180 * BHFormat complains if this bit is not set, and it's really wrong to have it unset.
181 * So we explicitly set it here. Pavel Fedin <pavel.fedin@mail.ru>
183 pp[4 + DE_BUFMEMTYPE] |= MEMF_PUBLIC;
185 pp[0] = (IPTR)name;
186 pp[1] = (IPTR)AROS_BSTR_ADDR(fssm->fssm_Device);
187 pp[2] = fssm->fssm_Unit;
188 pp[3] = fssm->fssm_Flags;
190 i = GetOffset(PartitionBase, pn);
191 blockspercyl = pp[4 + DE_BLKSPERTRACK] * pp[4 + DE_NUMHEADS];
192 if (i % blockspercyl != 0)
194 D(bug("[Boot] Start block of subtable not on cylinder boundary: "
195 "%ld (Blocks per Cylinder = %ld)\n", i, blockspercyl));
196 return;
198 i /= blockspercyl;
199 pp[4 + DE_LOWCYL] += i;
200 pp[4 + DE_HIGHCYL] += i;
202 /* Append .n if same device name already exists */
203 appended = FALSE;
204 changed = TRUE;
205 while (changed)
207 struct BootNode *bn;
208 changed = FALSE;
209 ForeachNode(&ExpansionBase->MountList, bn)
211 if (stricmp(AROS_BSTR_ADDR(((struct DeviceNode*)bn->bn_DeviceNode)->dn_Name), name) == 0)
213 if (!appended)
214 strcat(name, ".1");
215 else
216 name[strlen(name) - 1]++;
217 appended = TRUE;
218 changed = TRUE;
223 fsnode = FindFileSystem(table, FST_ID, pp[4 + DE_DOSTYPE], TAG_DONE);
224 if (fsnode) {
225 D(bug("[Boot] Found on-disk filesystem 0x%08x\n", pp[4 + DE_DOSTYPE]));
226 AddBootFileSystem(fsnode);
229 devnode = MakeDosNode(pp);
230 if (devnode != NULL) {
231 AddBootNode(bootable ? pp[4 + DE_BOOTPRI] : -128, ADNF_STARTPROC, devnode, NULL);
232 D(bug("[Boot] AddBootNode(%b, 0, 0x%p, NULL)\n", devnode->dn_Name, pp[4 + DE_DOSTYPE]));
233 return;
237 static BOOL CheckTables(struct ExpansionBase *ExpansionBase, struct Library *PartitionBase,
238 struct FileSysStartupMsg *fssm, struct PartitionHandle *table,
239 struct ExecBase *SysBase)
241 BOOL retval = FALSE;
242 struct PartitionHandle *ph;
244 /* Traverse partition tables recursively, and attempt to add a BootNode
245 for any non-subtable partitions found */
246 if (OpenPartitionTable(table) == 0)
248 ph = (struct PartitionHandle *)table->table->list.lh_Head;
249 while (ph->ln.ln_Succ)
251 /* Attempt to add partition to system if it isn't a subtable */
252 if (!CheckTables(ExpansionBase, PartitionBase, fssm, ph, SysBase))
253 AddPartitionVolume(ExpansionBase, PartitionBase, fssm, table,
254 ph, SysBase);
255 ph = (struct PartitionHandle *)ph->ln.ln_Succ;
257 retval = TRUE;
258 ClosePartitionTable(table);
260 return retval;
263 static VOID CheckPartitions(struct ExpansionBase *ExpansionBase, struct Library *PartitionBase, struct ExecBase *SysBase, struct BootNode *bn)
265 struct DeviceNode *dn = bn->bn_DeviceNode;
266 BOOL res = FALSE;
268 D(bug("CheckPartition('%b') handler = %x\n", dn->dn_Name, dn->dn_SegList));
270 /* If we already have filesystem handler, don't do anything */
271 if (dn->dn_SegList == BNULL)
273 struct FileSysStartupMsg *fssm = BADDR(dn->dn_Startup);
275 if (fssm && fssm->fssm_Device)
277 struct PartitionHandle *pt = OpenRootPartition(AROS_BSTR_ADDR(fssm->fssm_Device), fssm->fssm_Unit);
279 if (pt)
281 res = CheckTables(ExpansionBase, PartitionBase, fssm, pt, SysBase);
283 CloseRootPartition(pt);
288 if (!res)
289 /* If no partitions were found for the DeviceNode, put it back */
290 Enqueue(&ExpansionBase->MountList, &bn->bn_Node);
293 /* Scan all partitions manually for additional volumes that can be mounted. */
294 void dosboot_BootScan(LIBBASETYPEPTR LIBBASE)
296 APTR PartitionBase;
298 /* If we have partition.library, we can look for partitions */
299 PartitionBase = OpenLibrary("partition.library", 2);
300 if (PartitionBase)
303 * Remove the whole chain of BootNodes from the list and re-initialize it.
304 * We will insert new nodes into it, based on old ones.
305 * What is done here is safe as long as we don't move the list itself.
306 * ln_Succ of the last node in chain points to the lh_Tail of our list
307 * which always contains NULL.
309 struct BootNode *bootNode = (struct BootNode *)LIBBASE->bm_ExpansionBase->MountList.lh_Head;
311 NEWLIST(&LIBBASE->bm_ExpansionBase->MountList);
313 while (bootNode->bn_Node.ln_Succ)
315 /* Keep ln_Succ because it can be clobbered by reinsertion */
316 struct BootNode *nextNode = (struct BootNode *)bootNode->bn_Node.ln_Succ;
318 CheckPartitions(LIBBASE->bm_ExpansionBase, PartitionBase, SysBase, bootNode);
319 bootNode = nextNode;
322 CloseLibrary(PartitionBase);