Chameleon

Chameleon Svn Source Tree

Root/branches/azimutz/trunkGraphicsEnablerModules/i386/util/fdisk/cmd.c

1/*
2 * Copyright (c) 2002-2005 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/*
26 * Copyright (c) 1997 Tobias Weingartner
27 * All rights reserved.
28 *
29 * Redistribution and use in source and binary forms, with or without
30 * modification, are permitted provided that the following conditions
31 * are met:
32 * 1. Redistributions of source code must retain the above copyright
33 * notice, this list of conditions and the following disclaimer.
34 * 2. Redistributions in binary form must reproduce the above copyright
35 * notice, this list of conditions and the following disclaimer in the
36 * documentation and/or other materials provided with the distribution.
37 * 3. All advertising materials mentioning features or use of this software
38 * must display the following acknowledgement:
39 * This product includes software developed by Tobias Weingartner.
40 * 4. The name of the author may not be used to endorse or promote products
41 * derived from this software without specific prior written permission.
42 *
43 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
44 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
45 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
46 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
47 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
48 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
49 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
50 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
51 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
52 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
53 */
54
55#include <stdio.h>
56#include <ctype.h>
57#include <memory.h>
58#include <stdlib.h>
59#include <unistd.h>
60#include <signal.h>
61#include <sys/fcntl.h>
62#include "disk.h"
63#include "misc.h"
64#include "user.h"
65#include "part.h"
66#include "cmd.h"
67#include "auto.h"
68
69#define MAX(a, b) ((a) >= (b) ? (a) : (b))
70
71int
72Xerase(cmd, disk, mbr, tt, offset)
73cmd_t *cmd;
74disk_t *disk;
75mbr_t *mbr;
76mbr_t *tt;
77int offset;
78{
79 bzero(mbr->part, sizeof(mbr->part));
80mbr->signature = MBR_SIGNATURE;
81return (CMD_DIRTY);
82}
83
84int
85Xreinit(cmd, disk, mbr, tt, offset)
86cmd_t *cmd;
87disk_t *disk;
88mbr_t *mbr;
89mbr_t *tt;
90int offset;
91{
92/* Copy template MBR */
93MBR_make(tt);
94MBR_parse(disk, offset, 0, mbr);
95
96MBR_init(disk, mbr);
97
98/* Tell em we did something */
99printf("In memory copy is initialized to:\n");
100printf("Offset: %d\t", offset);
101MBR_print(mbr);
102printf("Use 'write' to update disk.\n");
103
104return (CMD_DIRTY);
105}
106
107int
108Xauto(cmd, disk, mbr, tt, offset)
109cmd_t *cmd;
110disk_t *disk;
111mbr_t *mbr;
112mbr_t *tt;
113int offset;
114{
115if (cmd->args[0] == '\0') {
116 printf("usage: auto <style>\n");
117 printf(" where style is one of:\n");
118 AUTO_print_styles(stdout);
119 return (CMD_CONT);
120}
121
122 if (AUTO_init(disk, cmd->args, mbr) != AUTO_OK) {
123 return (CMD_CONT);
124}
125MBR_make(mbr);
126return (CMD_DIRTY);
127}
128
129int
130Xdisk(cmd, disk, mbr, tt, offset)
131cmd_t *cmd;
132disk_t *disk;
133mbr_t *mbr;
134mbr_t *tt;
135int offset;
136{
137int maxcyl = 1024;
138int maxhead = 256;
139int maxsec = 63;
140
141/* Print out disk info */
142DISK_printmetrics(disk);
143
144#if defined (__powerpc__) || defined (__mips__)
145maxcyl = 9999999;
146maxhead = 9999999;
147maxsec = 9999999;
148#endif
149
150/* Ask for new info */
151if (ask_yn("Change disk geometry?", 0)) {
152disk->real->cylinders = ask_num("BIOS Cylinders", ASK_DEC,
153 disk->real->cylinders, 1, maxcyl, NULL);
154disk->real->heads = ask_num("BIOS Heads", ASK_DEC,
155 disk->real->heads, 1, maxhead, NULL);
156disk->real->sectors = ask_num("BIOS Sectors", ASK_DEC,
157 disk->real->sectors, 1, maxsec, NULL);
158
159disk->real->size = disk->real->cylinders * disk->real->heads
160* disk->real->sectors;
161}
162
163return (CMD_CONT);
164}
165
166int
167Xedit(cmd, disk, mbr, tt, offset)
168cmd_t *cmd;
169disk_t *disk;
170mbr_t *mbr;
171mbr_t *tt;
172int offset;
173{
174int pn, num, ret;
175prt_t *pp;
176
177ret = CMD_CONT;
178
179if (!isdigit(cmd->args[0])) {
180printf("Invalid argument: %s <partition number>\n", cmd->cmd);
181return (ret);
182}
183pn = atoi(cmd->args) - 1;
184
185if (pn < 0 || pn > 3) {
186printf("Invalid partition number.\n");
187return (ret);
188}
189
190/* Print out current table entry */
191pp = &mbr->part[pn];
192PRT_print(0, NULL);
193PRT_print(pn, pp);
194
195#defineEDIT(p, f, v, n, m, h)\
196if ((num = ask_num(p, f, v, n, m, h)) != v)\
197ret = CMD_DIRTY;\
198v = num;
199
200/* Ask for partition type */
201EDIT("Partition id ('0' to disable) ", ASK_HEX, pp->id, 0, 0xFF, PRT_printall);
202
203/* Unused, so just zero out */
204if (pp->id == DOSPTYP_UNUSED) {
205memset(pp, 0, sizeof(*pp));
206printf("Partition %d is disabled.\n", pn + 1);
207return (ret);
208}
209
210/* Change table entry */
211if (ask_yn("Do you wish to edit in CHS mode?", 0)) {
212int maxcyl, maxhead, maxsect;
213
214/* Shorter */
215maxcyl = disk->real->cylinders - 1;
216maxhead = disk->real->heads - 1;
217maxsect = disk->real->sectors;
218
219/* Get data */
220EDIT("BIOS Starting cylinder", ASK_DEC, pp->scyl, 0, maxcyl, NULL);
221EDIT("BIOS Starting head", ASK_DEC, pp->shead, 0, maxhead, NULL);
222EDIT("BIOS Starting sector", ASK_DEC, pp->ssect, 1, maxsect, NULL);
223EDIT("BIOS Ending cylinder", ASK_DEC, pp->ecyl, 0, maxcyl, NULL);
224EDIT("BIOS Ending head", ASK_DEC, pp->ehead, 0, maxhead, NULL);
225EDIT("BIOS Ending sector", ASK_DEC, pp->esect, 1, maxsect, NULL);
226/* Fix up off/size values */
227PRT_fix_BN(disk, pp, pn);
228/* Fix up CHS values for LBA */
229PRT_fix_CHS(disk, pp, pn);
230} else {
231u_int m;
232
233if (pn == 0) {
234pp->bs = 63 + offset;
235} else {
236if (mbr->part[pn-1].id != 0) {
237pp->bs = mbr->part[pn-1].bs + mbr->part[pn-1].ns;
238}
239}
240/* Get data */
241EDIT("Partition offset", ASK_DEC, pp->bs, 0,
242 disk->real->size, NULL);
243m = MAX(pp->ns, disk->real->size - pp->bs);
244if ( m > disk->real->size - pp->bs) {
245/* dont have default value extend beyond end of disk */
246m = disk->real->size - pp->bs;
247}
248pp->ns = m;
249EDIT("Partition size", ASK_DEC, pp->ns, 1,
250 m, NULL);
251
252/* Fix up CHS values */
253PRT_fix_CHS(disk, pp, pn);
254}
255#undef EDIT
256return (ret);
257}
258
259int
260Xsetpid(cmd, disk, mbr, tt, offset)
261cmd_t *cmd;
262disk_t *disk;
263mbr_t *mbr;
264mbr_t *tt;
265int offset;
266{
267int pn, num, ret;
268prt_t *pp;
269
270ret = CMD_CONT;
271
272if (!isdigit(cmd->args[0])) {
273printf("Invalid argument: %s <partition number>\n", cmd->cmd);
274return (ret);
275}
276pn = atoi(cmd->args) - 1;
277
278if (pn < 0 || pn > 3) {
279printf("Invalid partition number.\n");
280return (ret);
281}
282
283/* Print out current table entry */
284pp = &mbr->part[pn];
285PRT_print(0, NULL);
286PRT_print(pn, pp);
287
288#defineEDIT(p, f, v, n, m, h)\
289if ((num = ask_num(p, f, v, n, m, h)) != v)\
290ret = CMD_DIRTY;\
291v = num;
292
293/* Ask for partition type */
294EDIT("Partition id ('0' to disable) ", ASK_HEX, pp->id, 0, 0xFF, PRT_printall);
295
296#undef EDIT
297return (ret);
298}
299int
300Xselect(cmd, disk, mbr, tt, offset)
301cmd_t *cmd;
302disk_t *disk;
303mbr_t *mbr;
304mbr_t *tt;
305int offset;
306{
307static int firstoff = 0;
308int off;
309int pn;
310
311if (!isdigit(cmd->args[0])) {
312printf("Invalid argument: %s <partition number>\n", cmd->cmd);
313return (CMD_CONT);
314}
315
316pn = atoi(cmd->args) - 1;
317if (pn < 0 || pn > 3) {
318printf("Invalid partition number.\n");
319return (CMD_CONT);
320}
321
322off = mbr->part[pn].bs;
323
324/* Sanity checks */
325if ((mbr->part[pn].id != DOSPTYP_EXTEND) &&
326 (mbr->part[pn].id != DOSPTYP_EXTENDL)) {
327printf("Partition %d is not an extended partition.\n", pn + 1);
328return (CMD_CONT);
329}
330
331if (firstoff == 0)
332firstoff = off;
333
334if (!off) {
335printf("Loop to offset 0! Not selected.\n");
336return (CMD_CONT);
337} else {
338printf("Selected extended partition %d\n", pn + 1);
339printf("New MBR at offset %d.\n", off);
340}
341
342/* Recursion is beautifull! */
343USER_modify(disk, tt, off, firstoff);
344return (CMD_CONT);
345}
346
347int
348Xprint(cmd, disk, mbr, tt, offset)
349cmd_t *cmd;
350disk_t *disk;
351mbr_t *mbr;
352mbr_t *tt;
353int offset;
354{
355
356DISK_printmetrics(disk);
357printf("Offset: %d\t", offset);
358MBR_print(mbr);
359
360return (CMD_CONT);
361}
362
363int
364Xwrite(cmd, disk, mbr, tt, offset)
365cmd_t *cmd;
366disk_t *disk;
367mbr_t *mbr;
368mbr_t *tt;
369int offset;
370{
371int fd;
372int shared = 0;
373
374fd = DISK_openshared(disk->name, O_RDWR, &shared);
375if(shared) {
376 if(!ask_yn("Device could not be accessed exclusively.\nA reboot will be needed for changes to take effect. OK?", 0)) {
377 close(fd);
378 printf("MBR unchanged\n");
379 return (CMD_CONT);
380 }
381}
382
383printf("Writing MBR at offset %d.\n", offset);
384
385MBR_make(mbr);
386MBR_write(disk, fd, mbr);
387close(fd);
388return (CMD_CLEAN);
389}
390
391int
392Xquit(cmd, disk, r, tt, offset)
393cmd_t *cmd;
394disk_t *disk;
395mbr_t *r;
396mbr_t *tt;
397int offset;
398{
399
400/* Nothing to do here */
401return (CMD_SAVE);
402}
403
404int
405Xabort(cmd, disk, mbr, tt, offset)
406cmd_t *cmd;
407disk_t *disk;
408mbr_t *mbr;
409mbr_t *tt;
410int offset;
411{
412exit(0);
413
414/* NOTREACHED */
415return (CMD_CONT);
416}
417
418
419int
420Xexit(cmd, disk, mbr, tt, offset)
421cmd_t *cmd;
422disk_t *disk;
423mbr_t *mbr;
424mbr_t *tt;
425int offset;
426{
427
428/* Nothing to do here */
429return (CMD_EXIT);
430}
431
432int
433Xhelp(cmd, disk, mbr, tt, offset)
434cmd_t *cmd;
435disk_t *disk;
436mbr_t *mbr;
437mbr_t *tt;
438int offset;
439{
440cmd_table_t *cmd_table = cmd->table;
441int i;
442
443/* Hmm, print out cmd_table here... */
444for (i = 0; cmd_table[i].cmd != NULL; i++)
445printf("\t%s\t\t%s\n", cmd_table[i].cmd, cmd_table[i].help);
446return (CMD_CONT);
447}
448
449int
450Xupdate(cmd, disk, mbr, tt, offset)
451cmd_t *cmd;
452disk_t *disk;
453mbr_t *mbr;
454mbr_t *tt;
455int offset;
456{
457extern char *mbr_binary;
458/* Update code */
459memcpy(mbr->code, mbr_binary, MBR_CODE_SIZE);
460printf("Machine code updated.\n");
461return (CMD_DIRTY);
462}
463
464int
465Xflag(cmd, disk, mbr, tt, offset)
466cmd_t *cmd;
467disk_t *disk;
468mbr_t *mbr;
469mbr_t *tt;
470int offset;
471{
472int i, pn = -1;
473
474/* Parse partition table entry number */
475if (!isdigit(cmd->args[0])) {
476printf("Invalid argument: %s <partition number>\n", cmd->cmd);
477return (CMD_CONT);
478}
479pn = atoi(cmd->args) - 1;
480
481if (pn < 0 || pn > 3) {
482printf("Invalid partition number.\n");
483return (CMD_CONT);
484}
485
486/* Set active flag */
487for (i = 0; i < 4; i++) {
488if (i == pn)
489mbr->part[i].flag = DOSACTIVE;
490else
491mbr->part[i].flag = 0x00;
492}
493
494printf("Partition %d marked active.\n", pn + 1);
495return (CMD_DIRTY);
496}
497
498int
499Xmanual(cmd, disk, mbr, tt, offset)
500cmd_t *cmd;
501disk_t *disk;
502mbr_t *mbr;
503mbr_t *tt;
504int offset;
505{
506system("man 8 fdisk");
507return (CMD_CONT);
508}
509

Archive Download this file

Revision: 1054