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.3 2003/11/10 06:14:40 dillon Exp $
18 #include <sys/types.h>
19 #include <sys/diskslice.h>
20 #include <sys/disklabel.h>
21 #include <sys/diskmbr.h>
25 Track_Aligned(struct disk *d, u_long offset)
29 if (offset % d->bios_sect)
35 Prev_Track_Aligned(struct disk *d, u_long offset)
39 return (offset / d->bios_sect) * d->bios_sect;
43 Next_Track_Aligned(struct disk *d, u_long offset)
47 return Prev_Track_Aligned(d, offset + d->bios_sect-1);
51 Cyl_Aligned(struct disk *d, u_long offset)
53 if (!d->bios_sect || !d->bios_hd)
55 if (offset % (d->bios_sect * d->bios_hd))
61 Prev_Cyl_Aligned(struct disk *d, u_long offset)
63 if (!d->bios_sect || !d->bios_hd)
65 return (offset / (d->bios_sect*d->bios_hd)) * d->bios_sect * d->bios_hd;
69 Next_Cyl_Aligned(struct disk *d, u_long offset)
71 if (!d->bios_sect || !d->bios_hd)
73 return Prev_Cyl_Aligned(d,offset + (d->bios_sect * d->bios_hd)-1);
78 * Chunks of type 'whole' can have max NDOSPART children.
79 * Only one of them can have the "active" flag
82 Rule_000(struct disk *d, struct chunk *c, char *msg)
93 for (c1 = c->part; c1; c1 = c1->next) {
94 if (c1->type != unused) continue;
96 if (c1->flags & CHUNK_ACTIVE)
102 sprintf(msg + strlen(msg),
103 "%d is too many children of the 'whole' chunk. Max is %d\n",
107 sprintf(msg + strlen(msg),
108 "Too many active children of 'whole'");
114 * All children of 'whole' and 'extended' must be track-aligned.
115 * Exception: the end can be unaligned if it matches the end of 'whole'
118 Rule_001(struct disk *d, struct chunk *c, char *msg)
123 if (c->type != whole && c->type != extended)
125 for (i = 0, c1 = c->part; c1; c1 = c1->next) {
126 if (c1->type == unused) continue;
127 c1->flags |= CHUNK_ALIGN;
129 if (!Cyl_Aligned(d, c1->offset))
131 if (!Track_Aligned(d, c1->offset))
133 sprintf(msg + strlen(msg),
135 "chunk '%s' [%ld..%ld] does not start on a cylinder boundary\n",
137 "chunk '%s' [%ld..%ld] does not start on a track boundary\n",
139 c1->name, c1->offset, c1->end);
140 if ((c->type == whole || c->end == c1->end)
141 || Cyl_Aligned(d, c1->end + 1))
144 sprintf(msg + strlen(msg),
145 "chunk '%s' [%ld..%ld] does not end on a cylinder boundary\n",
146 c1->name, c1->offset, c1->end);
152 * Max one 'fat' as child of 'whole'
155 Rule_002(struct disk *d, struct chunk *c, char *msg)
161 if (c->type != whole)
163 for (i = 0, c1 = c->part; c1; c1 = c1->next) {
169 sprintf(msg + strlen(msg),
170 "Max one 'fat' allowed as child of 'whole'\n");
177 * Max one extended as child of 'whole'
180 Rule_003(struct disk *d, struct chunk *c, char *msg)
186 if (c->type != whole)
188 for (i = 0, c1 = c->part; c1; c1 = c1->next) {
189 if (c1->type != extended)
194 sprintf(msg + strlen(msg),
195 "Max one 'extended' allowed as child of 'whole'\n");
202 * Max seven 'part' as children of 'freebsd'
203 * Max one CHUNK_IS_ROOT child per 'freebsd'
206 Rule_004(struct disk *d, struct chunk *c, char *msg)
211 if (c->type != freebsd)
214 for (c1 = c->part; c1; c1 = c1->next) {
215 if (c1->type != part)
217 if (c1->flags & CHUNK_IS_ROOT)
222 sprintf(msg + strlen(msg),
223 "Max seven partitions per freebsd slice\n");
226 sprintf(msg + strlen(msg),
227 "Max one root partition child per freebsd slice\n");
232 Check_Chunk(struct disk *d, struct chunk *c, char *msg)
240 Check_Chunk(d, c->part, msg);
242 Check_Chunk(d, c->next, msg);
246 CheckRules(struct disk *d)
251 Check_Chunk(d, d->chunks, msg);
258 ChunkCanBeRoot(struct chunk *c)
261 struct disk *d = c->disk;
265 for (c1 = d->chunks->part; ; ) {
266 for (; c1; c1 = c1->next)
267 if (c1->offset <= c->offset && c1->end >= c->end)
271 "Internal trouble, cannot find this chunk in the chunk-tree\n");
274 if (c1->type == freebsd)
280 if (c1->type != freebsd) {
282 "The root partition must be in a FreeBSD slice, otherwise\n");
284 "the kernel cannot be booted from it\n");