2 * ----------------------------------------------------------------------------
3 * "THE BEER-WARE LICENSE" (Revision 42):
4 * <phk@login.dknet.dk> wrote this file. As long as you retain this notice you
5 * can do whatever you want with this stuff. If we meet some day, and you think
6 * this stuff is worth it, you can buy me a beer in return. Poul-Henning Kamp
7 * ----------------------------------------------------------------------------
9 * $FreeBSD: src/lib/libdisk/rules.c,v 1.16.2.4 2001/05/13 20:16:32 jkh Exp $
10 * $DragonFly: src/lib/libdisk/Attic/rules.c,v 1.2 2003/06/17 04:26:49 dillon Exp $
18 #include <sys/types.h>
19 #include <sys/diskslice.h>
20 #include <sys/disklabel.h>
24 Track_Aligned(struct disk *d, u_long offset)
28 if (offset % d->bios_sect)
34 Prev_Track_Aligned(struct disk *d, u_long offset)
38 return (offset / d->bios_sect) * d->bios_sect;
42 Next_Track_Aligned(struct disk *d, u_long offset)
46 return Prev_Track_Aligned(d, offset + d->bios_sect-1);
50 Cyl_Aligned(struct disk *d, u_long offset)
52 if (!d->bios_sect || !d->bios_hd)
54 if (offset % (d->bios_sect * d->bios_hd))
60 Prev_Cyl_Aligned(struct disk *d, u_long offset)
62 if (!d->bios_sect || !d->bios_hd)
64 return (offset / (d->bios_sect*d->bios_hd)) * d->bios_sect * d->bios_hd;
68 Next_Cyl_Aligned(struct disk *d, u_long offset)
70 if (!d->bios_sect || !d->bios_hd)
72 return Prev_Cyl_Aligned(d,offset + (d->bios_sect * d->bios_hd)-1);
77 * Chunks of type 'whole' can have max NDOSPART children.
78 * Only one of them can have the "active" flag
81 Rule_000(struct disk *d, struct chunk *c, char *msg)
92 for (c1 = c->part; c1; c1 = c1->next) {
93 if (c1->type != unused) continue;
95 if (c1->flags & CHUNK_ACTIVE)
101 sprintf(msg + strlen(msg),
102 "%d is too many children of the 'whole' chunk. Max is %d\n",
106 sprintf(msg + strlen(msg),
107 "Too many active children of 'whole'");
113 * All children of 'whole' and 'extended' must be track-aligned.
114 * Exception: the end can be unaligned if it matches the end of 'whole'
117 Rule_001(struct disk *d, struct chunk *c, char *msg)
122 if (c->type != whole && c->type != extended)
124 for (i = 0, c1 = c->part; c1; c1 = c1->next) {
125 if (c1->type == unused) continue;
126 c1->flags |= CHUNK_ALIGN;
128 if (!Cyl_Aligned(d, c1->offset))
130 if (!Track_Aligned(d, c1->offset))
132 sprintf(msg + strlen(msg),
134 "chunk '%s' [%ld..%ld] does not start on a cylinder boundary\n",
136 "chunk '%s' [%ld..%ld] does not start on a track boundary\n",
138 c1->name, c1->offset, c1->end);
139 if ((c->type == whole || c->end == c1->end)
140 || Cyl_Aligned(d, c1->end + 1))
143 sprintf(msg + strlen(msg),
144 "chunk '%s' [%ld..%ld] does not end on a cylinder boundary\n",
145 c1->name, c1->offset, c1->end);
151 * Max one 'fat' as child of 'whole'
154 Rule_002(struct disk *d, struct chunk *c, char *msg)
160 if (c->type != whole)
162 for (i = 0, c1 = c->part; c1; c1 = c1->next) {
168 sprintf(msg + strlen(msg),
169 "Max one 'fat' allowed as child of 'whole'\n");
176 * Max one extended as child of 'whole'
179 Rule_003(struct disk *d, struct chunk *c, char *msg)
185 if (c->type != whole)
187 for (i = 0, c1 = c->part; c1; c1 = c1->next) {
188 if (c1->type != extended)
193 sprintf(msg + strlen(msg),
194 "Max one 'extended' allowed as child of 'whole'\n");
201 * Max seven 'part' as children of 'freebsd'
202 * Max one CHUNK_IS_ROOT child per 'freebsd'
205 Rule_004(struct disk *d, struct chunk *c, char *msg)
210 if (c->type != freebsd)
213 for (c1 = c->part; c1; c1 = c1->next) {
214 if (c1->type != part)
216 if (c1->flags & CHUNK_IS_ROOT)
221 sprintf(msg + strlen(msg),
222 "Max seven partitions per freebsd slice\n");
225 sprintf(msg + strlen(msg),
226 "Max one root partition child per freebsd slice\n");
231 Check_Chunk(struct disk *d, struct chunk *c, char *msg)
239 Check_Chunk(d, c->part, msg);
241 Check_Chunk(d, c->next, msg);
245 CheckRules(struct disk *d)
250 Check_Chunk(d, d->chunks, msg);
257 ChunkCanBeRoot(struct chunk *c)
260 struct disk *d = c->disk;
264 for (c1 = d->chunks->part; ; ) {
265 for (; c1; c1 = c1->next)
266 if (c1->offset <= c->offset && c1->end >= c->end)
270 "Internal trouble, cannot find this chunk in the chunk-tree\n");
273 if (c1->type == freebsd)
279 if (c1->type != freebsd) {
281 "The root partition must be in a FreeBSD slice, otherwise\n");
283 "the kernel cannot be booted from it\n");