Chameleon

Chameleon Svn Source Tree

Root/branches/cparm/i386/util/fdisk/auto.c

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

Archive Download this file

Revision: 2118