1995-04-29 01:55:25 +00:00
|
|
|
/*
|
|
|
|
* ----------------------------------------------------------------------------
|
|
|
|
* "THE BEER-WARE LICENSE" (Revision 42):
|
2002-03-25 13:52:45 +00:00
|
|
|
* <phk@FreeBSD.org> wrote this file. As long as you retain this notice you
|
1995-04-29 01:55:25 +00:00
|
|
|
* can do whatever you want with this stuff. If we meet some day, and you think
|
|
|
|
* this stuff is worth it, you can buy me a beer in return. Poul-Henning Kamp
|
|
|
|
* ----------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
2001-09-30 21:16:57 +00:00
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
|
|
|
|
1995-04-29 01:55:25 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/disklabel.h>
|
2002-10-08 12:13:19 +00:00
|
|
|
#ifdef PC98
|
|
|
|
#include <sys/diskpc98.h>
|
|
|
|
#else
|
2002-10-01 07:24:55 +00:00
|
|
|
#include <sys/diskmbr.h>
|
2002-10-08 12:13:19 +00:00
|
|
|
#endif
|
1995-04-29 01:55:25 +00:00
|
|
|
#include "libdisk.h"
|
|
|
|
|
1995-04-29 04:00:57 +00:00
|
|
|
int
|
2002-10-22 09:13:02 +00:00
|
|
|
Track_Aligned(const struct disk *d, u_long offset)
|
1995-04-29 04:00:57 +00:00
|
|
|
{
|
2002-11-15 13:24:29 +00:00
|
|
|
|
1995-04-29 04:00:57 +00:00
|
|
|
if (!d->bios_sect)
|
|
|
|
return 1;
|
|
|
|
if (offset % d->bios_sect)
|
|
|
|
return 0;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
u_long
|
2002-10-22 09:13:02 +00:00
|
|
|
Prev_Track_Aligned(const struct disk *d, u_long offset)
|
1995-04-29 04:00:57 +00:00
|
|
|
{
|
2002-11-15 13:24:29 +00:00
|
|
|
|
1995-04-29 04:00:57 +00:00
|
|
|
if (!d->bios_sect)
|
|
|
|
return offset;
|
|
|
|
return (offset / d->bios_sect) * d->bios_sect;
|
|
|
|
}
|
|
|
|
|
|
|
|
u_long
|
2002-10-22 09:13:02 +00:00
|
|
|
Next_Track_Aligned(const struct disk *d, u_long offset)
|
1995-04-29 04:00:57 +00:00
|
|
|
{
|
2002-11-15 13:24:29 +00:00
|
|
|
|
1995-04-29 04:00:57 +00:00
|
|
|
if (!d->bios_sect)
|
|
|
|
return offset;
|
2001-04-01 12:18:20 +00:00
|
|
|
return Prev_Track_Aligned(d, offset + d->bios_sect-1);
|
1995-04-30 06:09:29 +00:00
|
|
|
}
|
|
|
|
|
2002-10-22 09:13:02 +00:00
|
|
|
static int
|
|
|
|
Cyl_Aligned(const struct disk *d, u_long offset)
|
1995-04-30 06:09:29 +00:00
|
|
|
{
|
2002-11-15 13:24:29 +00:00
|
|
|
|
1995-04-30 06:09:29 +00:00
|
|
|
if (!d->bios_sect || !d->bios_hd)
|
|
|
|
return 1;
|
|
|
|
if (offset % (d->bios_sect * d->bios_hd))
|
|
|
|
return 0;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
u_long
|
2002-10-22 09:13:02 +00:00
|
|
|
Prev_Cyl_Aligned(const struct disk *d, u_long offset)
|
1995-04-30 06:09:29 +00:00
|
|
|
{
|
2002-11-15 13:24:29 +00:00
|
|
|
|
1995-04-30 06:09:29 +00:00
|
|
|
if (!d->bios_sect || !d->bios_hd)
|
|
|
|
return offset;
|
|
|
|
return (offset / (d->bios_sect*d->bios_hd)) * d->bios_sect * d->bios_hd;
|
|
|
|
}
|
|
|
|
|
|
|
|
u_long
|
2002-10-22 09:13:02 +00:00
|
|
|
Next_Cyl_Aligned(const struct disk *d, u_long offset)
|
1995-04-30 06:09:29 +00:00
|
|
|
{
|
2002-11-15 13:24:29 +00:00
|
|
|
|
1995-04-30 06:09:29 +00:00
|
|
|
if (!d->bios_sect || !d->bios_hd)
|
|
|
|
return offset;
|
2002-11-15 13:24:29 +00:00
|
|
|
return Prev_Cyl_Aligned(d,offset + (d->bios_sect * d->bios_hd) - 1);
|
1995-04-29 04:00:57 +00:00
|
|
|
}
|
|
|
|
|
1995-04-29 01:55:25 +00:00
|
|
|
/*
|
|
|
|
* Rule#0:
|
|
|
|
* Chunks of type 'whole' can have max NDOSPART children.
|
1995-05-04 07:00:57 +00:00
|
|
|
* Only one of them can have the "active" flag
|
1995-04-29 01:55:25 +00:00
|
|
|
*/
|
2002-10-22 09:13:02 +00:00
|
|
|
static void
|
|
|
|
Rule_000(const struct disk *d, const struct chunk *c, char *msg)
|
1995-04-29 01:55:25 +00:00
|
|
|
{
|
2000-03-29 15:10:28 +00:00
|
|
|
#ifdef PC98
|
2002-11-15 13:24:29 +00:00
|
|
|
int i = 0;
|
2000-03-29 15:10:28 +00:00
|
|
|
#else
|
2002-11-15 13:24:29 +00:00
|
|
|
int i = 0, j = 0;
|
2000-03-29 15:10:28 +00:00
|
|
|
#endif
|
1995-04-29 01:55:25 +00:00
|
|
|
struct chunk *c1;
|
|
|
|
|
|
|
|
if (c->type != whole)
|
|
|
|
return;
|
2001-04-01 12:18:20 +00:00
|
|
|
for (c1 = c->part; c1; c1 = c1->next) {
|
2002-11-15 13:24:29 +00:00
|
|
|
if (c1->type != unused)
|
|
|
|
continue;
|
2000-03-29 15:10:28 +00:00
|
|
|
#ifndef PC98
|
1995-05-04 07:00:57 +00:00
|
|
|
if (c1->flags & CHUNK_ACTIVE)
|
|
|
|
j++;
|
2000-03-29 15:10:28 +00:00
|
|
|
#endif
|
1995-04-30 06:09:29 +00:00
|
|
|
i++;
|
|
|
|
}
|
1995-05-04 07:00:57 +00:00
|
|
|
if (i > NDOSPART)
|
2001-04-01 12:18:20 +00:00
|
|
|
sprintf(msg + strlen(msg),
|
2002-11-15 13:24:29 +00:00
|
|
|
"%d is too many children of the 'whole' chunk."
|
|
|
|
" Max is %d\n", i, NDOSPART);
|
2000-03-29 15:10:28 +00:00
|
|
|
#ifndef PC98
|
1995-05-04 07:00:57 +00:00
|
|
|
if (j > 1)
|
2001-04-01 12:18:20 +00:00
|
|
|
sprintf(msg + strlen(msg),
|
2002-11-15 13:24:29 +00:00
|
|
|
"Too many active children of 'whole'");
|
2000-03-29 15:10:28 +00:00
|
|
|
#endif
|
1995-04-29 01:55:25 +00:00
|
|
|
}
|
|
|
|
|
1995-05-30 08:29:07 +00:00
|
|
|
/*
|
1995-04-29 01:55:25 +00:00
|
|
|
* Rule#1:
|
1995-05-04 07:00:57 +00:00
|
|
|
* All children of 'whole' and 'extended' must be track-aligned.
|
1995-04-29 04:00:57 +00:00
|
|
|
* Exception: the end can be unaligned if it matches the end of 'whole'
|
1995-04-29 01:55:25 +00:00
|
|
|
*/
|
2002-10-22 09:13:02 +00:00
|
|
|
static void
|
|
|
|
Rule_001(const struct disk *d, const struct chunk *c, char *msg)
|
1995-04-29 01:55:25 +00:00
|
|
|
{
|
|
|
|
struct chunk *c1;
|
|
|
|
|
1995-05-04 07:00:57 +00:00
|
|
|
if (c->type != whole && c->type != extended)
|
1995-04-29 01:55:25 +00:00
|
|
|
return;
|
2002-10-22 09:13:02 +00:00
|
|
|
for (c1 = c->part; c1; c1 = c1->next) {
|
2002-11-15 13:24:29 +00:00
|
|
|
if (c1->type == unused)
|
|
|
|
continue;
|
1995-05-04 07:00:57 +00:00
|
|
|
c1->flags |= CHUNK_ALIGN;
|
2000-03-29 15:10:28 +00:00
|
|
|
#ifdef PC98
|
2001-04-01 12:18:20 +00:00
|
|
|
if (!Cyl_Aligned(d, c1->offset))
|
2000-03-29 15:10:28 +00:00
|
|
|
#else
|
2001-04-01 12:18:20 +00:00
|
|
|
if (!Track_Aligned(d, c1->offset))
|
2000-03-29 15:10:28 +00:00
|
|
|
#endif
|
2001-04-01 12:18:20 +00:00
|
|
|
sprintf(msg + strlen(msg),
|
2000-03-29 15:10:28 +00:00
|
|
|
#ifdef PC98
|
2002-11-15 13:24:29 +00:00
|
|
|
"chunk '%s' [%ld..%ld] does not start"
|
|
|
|
" on a cylinder boundary\n",
|
2000-03-29 15:10:28 +00:00
|
|
|
#else
|
2002-11-15 13:24:29 +00:00
|
|
|
"chunk '%s' [%ld..%ld] does not start"
|
|
|
|
" on a track boundary\n",
|
2000-03-29 15:10:28 +00:00
|
|
|
#endif
|
2001-04-01 12:18:20 +00:00
|
|
|
c1->name, c1->offset, c1->end);
|
1995-05-04 07:00:57 +00:00
|
|
|
if ((c->type == whole || c->end == c1->end)
|
2001-04-01 12:18:20 +00:00
|
|
|
|| Cyl_Aligned(d, c1->end + 1))
|
1995-05-04 07:00:57 +00:00
|
|
|
;
|
|
|
|
else
|
2001-04-01 12:18:20 +00:00
|
|
|
sprintf(msg + strlen(msg),
|
2002-11-15 13:24:29 +00:00
|
|
|
"chunk '%s' [%ld..%ld] does not end"
|
|
|
|
" on a cylinder boundary\n",
|
2001-04-01 12:18:20 +00:00
|
|
|
c1->name, c1->offset, c1->end);
|
1995-04-29 01:55:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1995-05-30 08:29:07 +00:00
|
|
|
/*
|
1995-04-29 07:21:14 +00:00
|
|
|
* Rule#2:
|
|
|
|
* Max one 'fat' as child of 'whole'
|
|
|
|
*/
|
2002-10-22 09:13:02 +00:00
|
|
|
static void
|
|
|
|
Rule_002(const struct disk *d, const struct chunk *c, char *msg)
|
1995-04-29 07:21:14 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
struct chunk *c1;
|
|
|
|
|
|
|
|
if (c->type != whole)
|
|
|
|
return;
|
2001-04-01 12:18:20 +00:00
|
|
|
for (i = 0, c1 = c->part; c1; c1 = c1->next) {
|
1995-04-29 07:21:14 +00:00
|
|
|
if (c1->type != fat)
|
|
|
|
continue;
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
if (i > 1) {
|
2001-04-01 12:18:20 +00:00
|
|
|
sprintf(msg + strlen(msg),
|
2002-11-15 13:24:29 +00:00
|
|
|
"Max one 'fat' allowed as child of 'whole'\n");
|
1995-04-29 07:21:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1995-05-30 08:29:07 +00:00
|
|
|
/*
|
1995-04-29 07:21:14 +00:00
|
|
|
* Rule#3:
|
|
|
|
* Max one extended as child of 'whole'
|
|
|
|
*/
|
2002-10-22 09:13:02 +00:00
|
|
|
static void
|
|
|
|
Rule_003(const struct disk *d, const struct chunk *c, char *msg)
|
1995-04-29 07:21:14 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
struct chunk *c1;
|
|
|
|
|
|
|
|
if (c->type != whole)
|
|
|
|
return;
|
2001-04-01 12:18:20 +00:00
|
|
|
for (i = 0, c1 = c->part; c1; c1 = c1->next) {
|
1995-04-29 07:21:14 +00:00
|
|
|
if (c1->type != extended)
|
|
|
|
continue;
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
if (i > 1) {
|
2001-04-01 12:18:20 +00:00
|
|
|
sprintf(msg + strlen(msg),
|
2002-11-15 13:24:29 +00:00
|
|
|
"Max one 'extended' allowed as child of 'whole'\n");
|
1995-04-29 07:21:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1995-05-30 08:29:07 +00:00
|
|
|
/*
|
1995-04-30 06:09:29 +00:00
|
|
|
* Rule#4:
|
|
|
|
* Max seven 'part' as children of 'freebsd'
|
1995-05-04 07:00:57 +00:00
|
|
|
* Max one CHUNK_IS_ROOT child per 'freebsd'
|
1995-04-30 06:09:29 +00:00
|
|
|
*/
|
2002-10-22 09:13:02 +00:00
|
|
|
static void
|
|
|
|
Rule_004(const struct disk *d, const struct chunk *c, char *msg)
|
1995-04-30 06:09:29 +00:00
|
|
|
{
|
2002-11-15 13:24:29 +00:00
|
|
|
int i = 0, k = 0;
|
1995-04-30 06:09:29 +00:00
|
|
|
struct chunk *c1;
|
|
|
|
|
|
|
|
if (c->type != freebsd)
|
|
|
|
return;
|
1995-06-11 19:33:05 +00:00
|
|
|
|
2001-04-01 12:18:20 +00:00
|
|
|
for (c1 = c->part; c1; c1 = c1->next) {
|
1995-04-30 06:09:29 +00:00
|
|
|
if (c1->type != part)
|
|
|
|
continue;
|
2000-07-12 18:05:18 +00:00
|
|
|
if (c1->flags & CHUNK_IS_ROOT)
|
1995-05-03 06:30:59 +00:00
|
|
|
k++;
|
1995-04-30 06:09:29 +00:00
|
|
|
i++;
|
|
|
|
}
|
|
|
|
if (i > 7) {
|
2001-04-01 12:18:20 +00:00
|
|
|
sprintf(msg + strlen(msg),
|
2002-11-15 13:24:29 +00:00
|
|
|
"Max seven partitions per freebsd slice\n");
|
1995-05-03 06:30:59 +00:00
|
|
|
}
|
|
|
|
if (k > 1) {
|
2001-04-01 12:18:20 +00:00
|
|
|
sprintf(msg + strlen(msg),
|
2002-11-15 13:24:29 +00:00
|
|
|
"Max one root partition child per freebsd slice\n");
|
1995-04-30 06:09:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-10-22 09:13:02 +00:00
|
|
|
static void
|
|
|
|
Check_Chunk(const struct disk *d, const struct chunk *c, char *msg)
|
1995-04-29 01:55:25 +00:00
|
|
|
{
|
2002-10-29 12:13:36 +00:00
|
|
|
|
2002-10-31 05:51:25 +00:00
|
|
|
switch (platform) {
|
|
|
|
case p_i386:
|
2003-04-30 21:03:16 +00:00
|
|
|
case p_amd64:
|
2002-10-29 12:13:36 +00:00
|
|
|
Rule_000(d, c, msg);
|
|
|
|
Rule_001(d, c, msg);
|
|
|
|
Rule_002(d, c, msg);
|
|
|
|
Rule_003(d, c, msg);
|
|
|
|
Rule_004(d, c, msg);
|
|
|
|
if (c->part)
|
|
|
|
Check_Chunk(d, c->part, msg);
|
|
|
|
if (c->next)
|
|
|
|
Check_Chunk(d, c->next, msg);
|
2002-10-31 05:51:25 +00:00
|
|
|
break;
|
|
|
|
case p_pc98:
|
|
|
|
Rule_000(d, c, msg);
|
|
|
|
Rule_001(d, c, msg);
|
|
|
|
Rule_004(d, c, msg);
|
|
|
|
if (c->part)
|
|
|
|
Check_Chunk(d, c->part, msg);
|
|
|
|
if (c->next)
|
|
|
|
Check_Chunk(d, c->next, msg);
|
|
|
|
break;
|
2003-04-23 12:15:31 +00:00
|
|
|
default:
|
|
|
|
break;
|
2002-10-29 12:13:36 +00:00
|
|
|
}
|
1995-04-29 01:55:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
char *
|
2002-10-22 09:13:02 +00:00
|
|
|
CheckRules(const struct disk *d)
|
1995-04-29 01:55:25 +00:00
|
|
|
{
|
|
|
|
char msg[BUFSIZ];
|
|
|
|
|
|
|
|
*msg = '\0';
|
2001-04-01 12:18:20 +00:00
|
|
|
Check_Chunk(d, d->chunks, msg);
|
1995-04-29 01:55:25 +00:00
|
|
|
if (*msg)
|
|
|
|
return strdup(msg);
|
|
|
|
return 0;
|
|
|
|
}
|