Chameleon

Chameleon Svn Source Tree

Root/branches/ErmaC/Trunk/i386/util/fdisk/auto.c

1/*
2 * Copyright (c) 2002 Apple Computer, Inc. All rights reserved.
3 *
4 *
5 * This file contains Original Code and/or Modifications of Original Code
6 * as defined in and that are subject to the Apple Public Source License
7 * Version 2.0 (the 'License'). You may not use this file except in
8 * compliance with the License. Please obtain a copy of the License at
9 * http://www.opensource.apple.com/apsl/ and read it before using this
10 * file.
11 *
12 * The Original Code and all software distributed under the License are
13 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
17 * Please see the License for the specific language governing rights and
18 * limitations under the License.
19 *
20 *
21 *
22 * Auto partitioning code.
23 */
24
25#include <stdio.h>
26#include <ctype.h>
27#include <stdlib.h>
28#include <unistd.h>
29#include <string.h>
30#include <err.h>
31#include "disk.h"
32#include "mbr.h"
33#include "auto.h"
34
35int AUTO_boothfs __P((disk_t *, mbr_t *));
36int AUTO_bootufs __P((disk_t *, mbr_t *));
37int AUTO_hfs __P((disk_t *, mbr_t *));
38int AUTO_ufs __P((disk_t *, mbr_t *));
39int AUTO_dos __P((disk_t *, mbr_t *));
40int AUTO_raid __P((disk_t *, mbr_t *));
41
42/* The default style is the first one in the list */
43struct _auto_style {
44 char *style_name;
45 int (*style_fn)(disk_t *, mbr_t *);
46 char *description;
47} style_fns[] = {
48 {"boothfs", AUTO_boothfs, "8Mb boot plus HFS+ root partition"},
49 {"bootufs", AUTO_bootufs, "8Mb boot plus UFS root partition"},
50 {"hfs", AUTO_hfs, "Entire disk as one HFS+ partition"},
51 {"ufs", AUTO_ufs, "Entire disk as one UFS partition"},
52 {"dos", AUTO_dos, "Entire disk as one DOS partition"},
53 {"raid", AUTO_raid, "Entire disk as one 0xAC partition"},
54 {0,0}
55};
56
57void
58AUTO_print_styles(FILE *f)
59{
60 struct _auto_style *fp;
61 int i;
62
63 for (i=0, fp = &style_fns[0]; fp->style_name != NULL; i++, fp++) {
64 fprintf(f, " %-10s %s%s\n", fp->style_name, fp->description, (i==0) ? " (default)" : "");
65 }
66}
67
68
69int
70AUTO_init(disk_t *disk, char *style, mbr_t *mbr)
71{
72 struct _auto_style *fp;
73
74 for (fp = &style_fns[0]; fp->style_name != NULL; fp++) {
75 /* If style is NULL, use the first (default) style */
76 if (style == NULL || strcasecmp(style, fp->style_name) == 0) {
77 return (*fp->style_fn)(disk, mbr);
78 }
79 }
80 warnx("No such auto-partition style %s", style);
81 return AUTO_ERR;
82}
83
84
85static int
86use_whole_disk(disk_t *disk, unsigned char id, mbr_t *mbr)
87{
88 MBR_clear(mbr);
89 mbr->part[0].id = id;
90 mbr->part[0].bs = 63;
91 mbr->part[0].ns = disk->real->size - 63;
92 PRT_fix_CHS(disk, &mbr->part[0], 0);
93 return AUTO_OK;
94}
95
96/* DOS style: one partition for the whole disk */
97int
98AUTO_dos(disk_t *disk, mbr_t *mbr)
99{
100 int cc;
101 cc = use_whole_disk(disk, 0x0C, mbr);
102 if (cc == AUTO_OK) {
103 mbr->part[0].flag = DOSACTIVE;
104 }
105 return cc;
106}
107
108/* HFS style: one partition for the whole disk */
109int
110AUTO_hfs(disk_t *disk, mbr_t *mbr)
111{
112 int cc;
113 cc = use_whole_disk(disk, 0xAF, mbr);
114 if (cc == AUTO_OK) {
115 mbr->part[0].flag = DOSACTIVE;
116 }
117 return cc;
118}
119
120/* UFS style: one partition for the whole disk */
121int
122AUTO_ufs(disk_t *disk, mbr_t *mbr)
123{
124 int cc;
125 cc = use_whole_disk(disk, 0xA8, mbr);
126 if (cc == AUTO_OK) {
127 mbr->part[0].flag = DOSACTIVE;
128 }
129 return cc;
130}
131
132/* One boot partition, one HFS+ root partition */
133int
134AUTO_boothfs (disk_t *disk, mbr_t *mbr)
135{
136 /* Check disk size. */
137 if (disk->real->size < 16 * 2048) {
138 errx(1, "Disk size must be greater than 16Mb");
139 return AUTO_ERR;
140 }
141
142 MBR_clear(mbr);
143
144 /* 8MB boot partition */
145 mbr->part[0].id = 0xAB;
146 mbr->part[0].bs = 63;
147 mbr->part[0].ns = 8 * 1024 * 2;
148 mbr->part[0].flag = DOSACTIVE;
149 PRT_fix_CHS(disk, &mbr->part[0], 0);
150
151 /* Rest of the disk for rooting */
152 mbr->part[1].id = 0xAF;
153 mbr->part[1].bs = (mbr->part[0].bs + mbr->part[0].ns);
154 mbr->part[1].ns = disk->real->size - mbr->part[0].ns - 63;
155 PRT_fix_CHS(disk, &mbr->part[1], 1);
156
157 return AUTO_OK;
158}
159
160/* One boot partition, one UFS root partition */
161int
162AUTO_bootufs(disk_t *disk, mbr_t *mbr)
163{
164 /* Check disk size. */
165 if (disk->real->size < 16 * 2048) {
166 errx(1, "Disk size must be greater than 16Mb");
167 return AUTO_ERR;
168 }
169
170 MBR_clear(mbr);
171
172 /* 8MB boot partition */
173 mbr->part[0].id = 0xAB;
174 mbr->part[0].bs = 63;
175 mbr->part[0].ns = 8 * 1024 * 2;
176 mbr->part[0].flag = DOSACTIVE;
177 PRT_fix_CHS(disk, &mbr->part[0], 0);
178
179 /* Rest of the disk for rooting */
180 mbr->part[1].id = 0xA8;
181 mbr->part[1].bs = (mbr->part[0].bs + mbr->part[0].ns);
182 mbr->part[1].ns = disk->real->size - mbr->part[0].ns - 63;
183 PRT_fix_CHS(disk, &mbr->part[1], 1);
184
185 return AUTO_OK;
186}
187
188/* RAID style: one 0xAC partition for the whole disk */
189int
190AUTO_raid(disk_t *disk, mbr_t *mbr)
191{
192 return use_whole_disk(disk, 0xAC, mbr);
193}
194
195

Archive Download this file

Revision: 2045