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 $
17 #include <sys/types.h>
18 #include <sys/diskslice.h>
19 #include <sys/disklabel.h>
23 Track_Aligned(struct disk *d, u_long offset)
27 if (offset % d->bios_sect)
33 Prev_Track_Aligned(struct disk *d, u_long offset)
37 return (offset / d->bios_sect) * d->bios_sect;
41 Next_Track_Aligned(struct disk *d, u_long offset)
45 return Prev_Track_Aligned(d, offset + d->bios_sect-1);
49 Cyl_Aligned(struct disk *d, u_long offset)
51 if (!d->bios_sect || !d->bios_hd)
53 if (offset % (d->bios_sect * d->bios_hd))
59 Prev_Cyl_Aligned(struct disk *d, u_long offset)
61 if (!d->bios_sect || !d->bios_hd)
63 return (offset / (d->bios_sect*d->bios_hd)) * d->bios_sect * d->bios_hd;
67 Next_Cyl_Aligned(struct disk *d, u_long offset)
69 if (!d->bios_sect || !d->bios_hd)
71 return Prev_Cyl_Aligned(d,offset + (d->bios_sect * d->bios_hd)-1);
76 * Chunks of type 'whole' can have max NDOSPART children.
77 * Only one of them can have the "active" flag
80 Rule_000(struct disk *d, struct chunk *c, char *msg)
91 for (c1 = c->part; c1; c1 = c1->next) {
92 if (c1->type != unused) continue;
94 if (c1->flags & CHUNK_ACTIVE)
100 sprintf(msg + strlen(msg),
101 "%d is too many children of the 'whole' chunk. Max is %d\n",
105 sprintf(msg + strlen(msg),
106 "Too many active children of 'whole'");
112 * All children of 'whole' and 'extended' must be track-aligned.
113 * Exception: the end can be unaligned if it matches the end of 'whole'
116 Rule_001(struct disk *d, struct chunk *c, char *msg)
121 if (c->type != whole && c->type != extended)
123 for (i = 0, c1 = c->part; c1; c1 = c1->next) {
124 if (c1->type == unused) continue;
125 c1->flags |= CHUNK_ALIGN;
127 if (!Cyl_Aligned(d, c1->offset))
129 if (!Track_Aligned(d, c1->offset))
131 sprintf(msg + strlen(msg),
133 "chunk '%s' [%ld..%ld] does not start on a cylinder boundary\n",
135 "chunk '%s' [%ld..%ld] does not start on a track boundary\n",
137 c1->name, c1->offset, c1->end);
138 if ((c->type == whole || c->end == c1->end)
139 || Cyl_Aligned(d, c1->end + 1))
142 sprintf(msg + strlen(msg),
143 "chunk '%s' [%ld..%ld] does not end on a cylinder boundary\n",
144 c1->name, c1->offset, c1->end);
150 * Max one 'fat' as child of 'whole'
153 Rule_002(struct disk *d, struct chunk *c, char *msg)
159 if (c->type != whole)
161 for (i = 0, c1 = c->part; c1; c1 = c1->next) {
167 sprintf(msg + strlen(msg),
168 "Max one 'fat' allowed as child of 'whole'\n");
175 * Max one extended as child of 'whole'
178 Rule_003(struct disk *d, struct chunk *c, char *msg)
184 if (c->type != whole)
186 for (i = 0, c1 = c->part; c1; c1 = c1->next) {
187 if (c1->type != extended)
192 sprintf(msg + strlen(msg),
193 "Max one 'extended' allowed as child of 'whole'\n");
200 * Max seven 'part' as children of 'freebsd'
201 * Max one CHUNK_IS_ROOT child per 'freebsd'
204 Rule_004(struct disk *d, struct chunk *c, char *msg)
209 if (c->type != freebsd)
212 for (c1 = c->part; c1; c1 = c1->next) {
213 if (c1->type != part)
215 if (c1->flags & CHUNK_IS_ROOT)
220 sprintf(msg + strlen(msg),
221 "Max seven partitions per freebsd slice\n");
224 sprintf(msg + strlen(msg),
225 "Max one root partition child per freebsd slice\n");
230 Check_Chunk(struct disk *d, struct chunk *c, char *msg)
238 Check_Chunk(d, c->part, msg);
240 Check_Chunk(d, c->next, msg);
244 CheckRules(struct disk *d)
249 Check_Chunk(d, d->chunks, msg);
256 ChunkCanBeRoot(struct chunk *c)
259 struct disk *d = c->disk;
263 for (c1 = d->chunks->part; ; ) {
264 for (; c1; c1 = c1->next)
265 if (c1->offset <= c->offset && c1->end >= c->end)
269 "Internal trouble, cannot find this chunk in the chunk-tree\n");
272 if (c1->type == freebsd)
278 if (c1->type != freebsd) {
280 "The root partition must be in a FreeBSD slice, otherwise\n");
282 "the kernel cannot be booted from it\n");