Zero is a normal mask value, not a wildcard.
[AROS.git] / rom / dosboot / bootscan.c
blobb6e3dad573d0aba894075b3a530c13cb85f05e87
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;
77 * TODO: Try to locate RDB filesystem for this volume and make it bootable.
78 * Use FindFileSystem() and AddBootFileSystem() for this.
81 D(bug("[Boot] AddPartitionVolume\n"));
82 GetPartitionTableAttrsTags(table, PTT_TYPE, &pttype, TAG_DONE);
84 attrs = QueryPartitionAttrs(table);
85 while ((attrs->attribute != TAG_DONE) && (attrs->attribute != PT_NAME))
86 attrs++; /* look for name attr */
88 if (attrs->attribute != TAG_DONE)
90 D(bug("[Boot] RDB/GPT partition\n"));
92 /* partition has a name => RDB/GPT partition */
93 tags[0] = PT_NAME;
94 tags[1] = (IPTR)name;
95 tags[2] = PT_DOSENVEC;
96 tags[3] = (IPTR)&pp[4];
97 tags[4] = PT_BOOTABLE;
98 tags[5] = (IPTR)&bootable;
99 tags[6] = TAG_DONE;
100 GetPartitionAttrs(pn, (struct TagItem *)tags);
102 D(bug("[Boot] Partition name: %s bootable: %d\n", name, bootable));
104 else
106 D(bug("[Boot] MBR/EBR partition\n"));
108 /* partition doesn't have a name => MBR/EBR partition */
109 tags[0] = PT_POSITION;
110 tags[1] = (IPTR)&ppos;
111 tags[2] = PT_DOSENVEC;
112 tags[3] = (IPTR)&pp[4];
113 tags[4] = TAG_DONE;
114 GetPartitionAttrs(pn, (struct TagItem *)tags);
117 * 'Active' is not the same as 'Bootable'. Theoretically we can set Active flag for multiple
118 * partitions, but this may screw up Microsoft system software which expects to see only one
119 * active partition.
121 bootable = TRUE;
123 /* make the name */
124 devname = AROS_BSTR_ADDR(fssm->fssm_Device);
125 for (i = 0; i < 26; i++)
127 if (*devname == '.' || *devname == '\0')
128 break;
129 name[i] = (UBYTE)uppercase(*devname);
130 devname++;
132 if ((fssm->fssm_Unit / 10))
133 name[i++] = '0' + (UBYTE)(fssm->fssm_Unit / 10);
134 name[i++] = '0' + (UBYTE)(fssm->fssm_Unit % 10);
135 name[i++] = 'P';
136 if (table->table->type == PHPTT_EBR)
137 ppos += 4;
138 if ((ppos / 10))
139 name[i++] = '0' + (UBYTE)(ppos / 10);
140 name[i++] = '0' + (UBYTE)(ppos % 10);
141 name[i] = '\0';
143 D(bug("[Boot] Partition name: %s\n", name));
146 if ((pp[4 + DE_TABLESIZE] < DE_DOSTYPE) || (pp[4 + DE_DOSTYPE] == 0))
149 * partition.library reports DosType == 0 for unknown filesystems.
150 * However dos.library will mount such DeviceNodes using rn_DefaultHandler
151 * (FFS). This is done for compatibility with 3rd party expansion ROMs.
152 * Here we ignore partitions with DosType == 0 and won't enter them into
153 * mountlist.
155 D(bug("[Boot] Unknown DosType for %s, skipping partition\n"));
156 return;
159 if (pttype != PHPTT_RDB)
162 * Only RDB partitions can store the complete DosEnvec.
163 * For other partition types partition.library puts some defaults
164 * into these fields, however they do not have anything to do with
165 * real values, which are device-dependent.
166 * However, the device itself knows them. Here we inherit these settings
167 * from the original DeviceNode which represents the whole drive.
168 * Note that we don't change DosEnvec size. If these fields are not included,
169 * it will stay this way.
170 * Copy members only if they are present in device's DosEnvec.
172 struct DosEnvec *devenv = BADDR(fssm->fssm_Environ);
174 if (devenv->de_TableSize >= DE_MAXTRANSFER)
176 pp[4 + DE_MAXTRANSFER] = devenv->de_MaxTransfer;
178 if (devenv->de_TableSize >= DE_MASK)
179 pp[4 + DE_MASK] = devenv->de_Mask;
184 * BHFormat complains if this bit is not set, and it's really wrong to have it unset.
185 * So we explicitly set it here. Pavel Fedin <pavel.fedin@mail.ru>
187 pp[4 + DE_BUFMEMTYPE] |= MEMF_PUBLIC;
189 pp[0] = (IPTR)name;
190 pp[1] = (IPTR)AROS_BSTR_ADDR(fssm->fssm_Device);
191 pp[2] = fssm->fssm_Unit;
192 pp[3] = fssm->fssm_Flags;
194 i = GetOffset(PartitionBase, pn);
195 blockspercyl = pp[4 + DE_BLKSPERTRACK] * pp[4 + DE_NUMHEADS];
196 if (i % blockspercyl != 0)
198 D(bug("[Boot] Start block of subtable not on cylinder boundary: "
199 "%ld (Blocks per Cylinder = %ld)\n", i, blockspercyl));
200 return;
202 i /= blockspercyl;
203 pp[4 + DE_LOWCYL] += i;
204 pp[4 + DE_HIGHCYL] += i;
206 /* Append .n if same device name already exists */
207 appended = FALSE;
208 changed = TRUE;
209 while (changed)
211 struct BootNode *bn;
212 changed = FALSE;
213 ForeachNode(&ExpansionBase->MountList, bn)
215 if (stricmp(AROS_BSTR_ADDR(((struct DeviceNode*)bn->bn_DeviceNode)->dn_Name), name) == 0)
217 if (!appended)
218 strcat(name, ".1");
219 else
220 name[strlen(name) - 1]++;
221 appended = TRUE;
222 changed = TRUE;
227 devnode = MakeDosNode(pp);
228 if (devnode != NULL) {
229 AddBootNode(bootable ? pp[4 + DE_BOOTPRI] : -128, ADNF_STARTPROC, devnode, NULL);
230 D(bug("[Boot] AddBootNode(%b, 0, 0x%p, NULL)\n", devnode->dn_Name, pp[4 + DE_DOSTYPE]));
231 return;
235 static BOOL CheckTables(struct ExpansionBase *ExpansionBase, struct Library *PartitionBase,
236 struct FileSysStartupMsg *fssm, struct PartitionHandle *table,
237 struct ExecBase *SysBase)
239 BOOL retval = FALSE;
240 struct PartitionHandle *ph;
242 /* Traverse partition tables recursively, and attempt to add a BootNode
243 for any non-subtable partitions found */
244 if (OpenPartitionTable(table) == 0)
246 ph = (struct PartitionHandle *)table->table->list.lh_Head;
247 while (ph->ln.ln_Succ)
249 /* Attempt to add partition to system if it isn't a subtable */
250 if (!CheckTables(ExpansionBase, PartitionBase, fssm, ph, SysBase))
251 AddPartitionVolume(ExpansionBase, PartitionBase, fssm, table,
252 ph, SysBase);
253 ph = (struct PartitionHandle *)ph->ln.ln_Succ;
255 retval = TRUE;
256 ClosePartitionTable(table);
258 return retval;
261 static VOID CheckPartitions(struct ExpansionBase *ExpansionBase, struct Library *PartitionBase, struct ExecBase *SysBase, struct BootNode *bn)
263 struct DeviceNode *dn = bn->bn_DeviceNode;
264 BOOL res = FALSE;
266 D(bug("CheckPartition('%b') handler = %x\n", dn->dn_Name, dn->dn_SegList));
268 /* If we already have filesystem handler, don't do anything */
269 if (dn->dn_SegList == BNULL)
271 struct FileSysStartupMsg *fssm = BADDR(dn->dn_Startup);
273 if (fssm && fssm->fssm_Device)
275 struct PartitionHandle *pt = OpenRootPartition(AROS_BSTR_ADDR(fssm->fssm_Device), fssm->fssm_Unit);
277 if (pt)
279 res = CheckTables(ExpansionBase, PartitionBase, fssm, pt, SysBase);
281 CloseRootPartition(pt);
286 if (!res)
287 /* If no partitions were found for the DeviceNode, put it back */
288 Enqueue(&ExpansionBase->MountList, &bn->bn_Node);
291 /* Scan all partitions manually for additional volumes that can be mounted. */
292 void dosboot_BootScan(LIBBASETYPEPTR LIBBASE)
294 APTR PartitionBase;
296 /* If we have partition.library, we can look for partitions */
297 PartitionBase = OpenLibrary("partition.library", 2);
298 if (PartitionBase)
301 * Remove the whole chain of BootNodes from the list and re-initialize it.
302 * We will insert new nodes into it, based on old ones.
303 * What is done here is safe as long as we don't move the list itself.
304 * ln_Succ of the last node in chain points to the lh_Tail of our list
305 * which always contains NULL.
307 struct BootNode *bootNode = (struct BootNode *)LIBBASE->bm_ExpansionBase->MountList.lh_Head;
309 NEWLIST(&LIBBASE->bm_ExpansionBase->MountList);
311 while (bootNode->bn_Node.ln_Succ)
313 /* Keep ln_Succ because it can be clobbered by reinsertion */
314 struct BootNode *nextNode = (struct BootNode *)bootNode->bn_Node.ln_Succ;
316 CheckPartitions(LIBBASE->bm_ExpansionBase, PartitionBase, SysBase, bootNode);
317 bootNode = nextNode;
320 CloseLibrary(PartitionBase);