2 * ----------------------------------------------------------------------------
3 * "THE BEER-WARE LICENSE" (Revision 42):
4 * <phk@FreeBSD.org> 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 * ----------------------------------------------------------------------------
10 #include <sys/cdefs.h>
11 __FBSDID("$FreeBSD$");
20 #include <sys/param.h>
21 #include <sys/disklabel.h>
23 #include <sys/diskpc98.h>
25 #include <sys/diskmbr.h>
27 #include <sys/mount.h>
29 #include <sys/sysctl.h>
36 Fixup_FreeBSD_Names(struct chunk *c)
38 struct chunk *c1, *c3;
41 if (!strcmp(c->name, "X"))
44 /* reset all names to "X" */
45 for (c1 = c->part; c1; c1 = c1->next) {
47 c1->name = malloc(12);
53 /* Allocate the first swap-partition we find */
54 for (c1 = c->part; c1; c1 = c1->next) {
55 if (c1->type == unused)
57 if (c1->subtype != FS_SWAP)
59 sprintf(c1->name, "%s%c", c->name, SWAP_PART + 'a');
63 /* Allocate the first root-partition we find */
64 for (c1 = c->part; c1; c1 = c1->next) {
65 if (c1->type == unused)
67 if (!(c1->flags & CHUNK_IS_ROOT))
69 sprintf(c1->name, "%s%c", c->name, 0 + 'a');
73 /* Try to give them the same as they had before */
74 for (c1 = c->part; c1; c1 = c1->next) {
75 if (strcmp(c1->name, "X"))
77 for (c3 = c->part; c3 ; c3 = c3->next)
78 if (c1 != c3 && !strcmp(c3->name, c1->oname))
80 strcpy(c1->name, c1->oname);
85 /* Allocate the rest sequentially */
86 for (c1 = c->part; c1; c1 = c1->next) {
87 const char order[] = "defghab";
89 if (c1->type == unused)
91 if (strcmp("X", c1->name))
94 for (j = 0; j < strlen(order); j++) {
95 sprintf(c1->name, "%s%c", c->name, order[j]);
96 for (c3 = c->part; c3 ; c3 = c3->next)
97 if (c1 != c3 && !strcmp(c3->name, c1->name))
101 strcpy(c1->name, "X");
105 for (c1 = c->part; c1; c1 = c1->next) {
114 Fixup_Extended_Names(struct chunk *c)
119 for (c1 = c->part; c1; c1 = c1->next) {
120 if (c1->type == unused)
123 c1->name = malloc(12);
126 sprintf(c1->name, "%ss%d", c->disk->chunks->name, j++);
127 if (c1->type == freebsd)
128 if (Fixup_FreeBSD_Names(c1) != 0)
137 Fixup_Apple_Names(struct chunk *c)
141 for (c1 = c->part; c1; c1 = c1->next) {
142 if (c1->type == unused)
145 c1->name = strdup(c->name);
154 Fixup_Names(struct disk *d)
156 struct chunk *c1, *c2;
157 #if defined(__i386__) || defined(__ia64__) || defined(__amd64__)
163 for (c2 = c1->part; c2 ; c2 = c2->next) {
164 if (c2->type == unused)
166 if (strcmp(c2->name, "X"))
168 #if defined(__i386__) || defined(__ia64__) || defined(__amd64__)
169 c2->oname = malloc(12);
177 for (j = 1; j <= max; j++) {
179 sprintf(c2->oname, "%s%c%d", c1->name,
180 (c1->type == whole) ? 'p' : 's', j);
182 sprintf(c2->oname, "%ss%d", c1->name, j);
184 for (c3 = c1->part; c3; c3 = c3->next)
185 if (c3 != c2 && !strcmp(c3->name, c2->oname))
188 c2->name = c2->oname;
198 c2->name = strdup(c1->name);
201 for (c2 = c1->part; c2; c2 = c2->next) {
202 if (c2->type == freebsd)
203 Fixup_FreeBSD_Names(c2);
205 else if (c2->type == apple)
206 Fixup_Apple_Names(c2);
209 else if (c2->type == extended)
210 Fixup_Extended_Names(c2);
217 Create_Chunk(struct disk *d, daddr_t offset, daddr_t size, chunk_e type,
218 int subtype, u_long flags, const char *sname)
223 if (!(flags & CHUNK_FORCE_ALL)) {
226 /* Never use the first cylinder */
228 offset += (d->bios_sect * d->bios_hd);
229 size -= (d->bios_sect * d->bios_hd);
232 /* Never use the first track */
234 offset += d->bios_sect;
235 size -= d->bios_sect;
239 /* Always end on cylinder boundary */
240 l = (offset + size) % (d->bios_sect * d->bios_hd);
245 i = Add_Chunk(d, offset, size, "X", type, subtype, flags, sname);
251 Create_Chunk_DWIM(struct disk *d, struct chunk *parent, daddr_t size,
252 chunk_e type, int subtype, u_long flags)
261 if ((parent->type == freebsd && type == part && parent->part == NULL)
262 || (parent->type == apple && type == part && parent->part == NULL)) {
266 c1->disk = parent->disk;
267 c1->offset = parent->offset;
268 c1->size = parent->size;
269 c1->end = parent->offset + parent->size - 1;
271 if (parent->sname != NULL)
272 c1->sname = strdup(parent->sname);
273 c1->name = strdup("-");
277 for (c1 = parent->part; c1; c1 = c1->next) {
278 if (c1->type != unused)
287 i = Add_Chunk(d, offset, size, "X", type, subtype, flags, "-");
291 for (c1 = parent->part; c1; c1 = c1->next)
292 if (c1->offset == offset)
294 /* barfout(1, "Serious internal trouble"); */