Chameleon Applications

Chameleon Applications Svn Source Tree

Root/branches/iFabio/Chameleon/i386/util/fdisk/cmd.c

Source at commit 302 created 12 years 10 months ago.
By ifabio, Last mqin trunk change. update the buildpkg: now the fdisk440 will be included into the pkg from the sym/i386 folder. Add a dmg folder into pakage folder.
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#define MAX(a, b) ((a) >= (b) ? (a) : (b))
69
70int
71Xerase(cmd, disk, mbr, tt, offset)
72cmd_t *cmd;
73disk_t *disk;
74mbr_t *mbr;
75mbr_t *tt;
76int offset;
77{
78 bzero(mbr->part, sizeof(mbr->part));
79mbr->signature = MBR_SIGNATURE;
80return (CMD_DIRTY);
81}
82
83int
84Xreinit(cmd, disk, mbr, tt, offset)
85cmd_t *cmd;
86disk_t *disk;
87mbr_t *mbr;
88mbr_t *tt;
89int offset;
90{
91/* Copy template MBR */
92MBR_make(tt);
93MBR_parse(disk, offset, 0, mbr);
94
95MBR_init(disk, mbr);
96
97/* Tell em we did something */
98printf("In memory copy is initialized to:\n");
99printf("Offset: %d\t", offset);
100MBR_print(mbr);
101printf("Use 'write' to update disk.\n");
102
103return (CMD_DIRTY);
104}
105
106int
107Xauto(cmd, disk, mbr, tt, offset)
108cmd_t *cmd;
109disk_t *disk;
110mbr_t *mbr;
111mbr_t *tt;
112int offset;
113{
114if (cmd->args[0] == '\0') {
115 printf("usage: auto <style>\n");
116 printf(" where style is one of:\n");
117 AUTO_print_styles(stdout);
118 return (CMD_CONT);
119}
120
121 if (AUTO_init(disk, cmd->args, mbr) != AUTO_OK) {
122 return (CMD_CONT);
123}
124MBR_make(mbr);
125return (CMD_DIRTY);
126}
127
128int
129Xdisk(cmd, disk, mbr, tt, offset)
130cmd_t *cmd;
131disk_t *disk;
132mbr_t *mbr;
133mbr_t *tt;
134int offset;
135{
136int maxcyl = 1024;
137int maxhead = 256;
138int maxsec = 63;
139
140/* Print out disk info */
141DISK_printmetrics(disk);
142
143#if defined (__powerpc__) || defined (__mips__)
144maxcyl = 9999999;
145maxhead = 9999999;
146maxsec = 9999999;
147#endif
148
149/* Ask for new info */
150if (ask_yn("Change disk geometry?", 0)) {
151disk->real->cylinders = ask_num("BIOS Cylinders", ASK_DEC,
152 disk->real->cylinders, 1, maxcyl, NULL);
153disk->real->heads = ask_num("BIOS Heads", ASK_DEC,
154 disk->real->heads, 1, maxhead, NULL);
155disk->real->sectors = ask_num("BIOS Sectors", ASK_DEC,
156 disk->real->sectors, 1, maxsec, NULL);
157
158disk->real->size = disk->real->cylinders * disk->real->heads
159* disk->real->sectors;
160}
161
162return (CMD_CONT);
163}
164
165int
166Xedit(cmd, disk, mbr, tt, offset)
167cmd_t *cmd;
168disk_t *disk;
169mbr_t *mbr;
170mbr_t *tt;
171int offset;
172{
173int pn, num, ret;
174prt_t *pp;
175
176ret = CMD_CONT;
177
178if (!isdigit(cmd->args[0])) {
179printf("Invalid argument: %s <partition number>\n", cmd->cmd);
180return (ret);
181}
182pn = atoi(cmd->args) - 1;
183
184if (pn < 0 || pn > 3) {
185printf("Invalid partition number.\n");
186return (ret);
187}
188
189/* Print out current table entry */
190pp = &mbr->part[pn];
191PRT_print(0, NULL);
192PRT_print(pn, pp);
193
194#defineEDIT(p, f, v, n, m, h)\
195if ((num = ask_num(p, f, v, n, m, h)) != v)\
196ret = CMD_DIRTY;\
197v = num;
198
199/* Ask for partition type */
200EDIT("Partition id ('0' to disable) ", ASK_HEX, pp->id, 0, 0xFF, PRT_printall);
201
202/* Unused, so just zero out */
203if (pp->id == DOSPTYP_UNUSED) {
204memset(pp, 0, sizeof(*pp));
205printf("Partition %d is disabled.\n", pn + 1);
206return (ret);
207}
208
209/* Change table entry */
210if (ask_yn("Do you wish to edit in CHS mode?", 0)) {
211int maxcyl, maxhead, maxsect;
212
213/* Shorter */
214maxcyl = disk->real->cylinders - 1;
215maxhead = disk->real->heads - 1;
216maxsect = disk->real->sectors;
217
218/* Get data */
219EDIT("BIOS Starting cylinder", ASK_DEC, pp->scyl, 0, maxcyl, NULL);
220EDIT("BIOS Starting head", ASK_DEC, pp->shead, 0, maxhead, NULL);
221EDIT("BIOS Starting sector", ASK_DEC, pp->ssect, 1, maxsect, NULL);
222EDIT("BIOS Ending cylinder", ASK_DEC, pp->ecyl, 0, maxcyl, NULL);
223EDIT("BIOS Ending head", ASK_DEC, pp->ehead, 0, maxhead, NULL);
224EDIT("BIOS Ending sector", ASK_DEC, pp->esect, 1, maxsect, NULL);
225/* Fix up off/size values */
226PRT_fix_BN(disk, pp, pn);
227/* Fix up CHS values for LBA */
228PRT_fix_CHS(disk, pp, pn);
229} else {
230u_int m;
231
232if (pn == 0) {
233pp->bs = 63 + offset;
234} else {
235if (mbr->part[pn-1].id != 0) {
236pp->bs = mbr->part[pn-1].bs + mbr->part[pn-1].ns;
237}
238}
239/* Get data */
240EDIT("Partition offset", ASK_DEC, pp->bs, 0,
241 disk->real->size, NULL);
242m = MAX(pp->ns, disk->real->size - pp->bs);
243if ( m > disk->real->size - pp->bs) {
244/* dont have default value extend beyond end of disk */
245m = disk->real->size - pp->bs;
246}
247pp->ns = m;
248EDIT("Partition size", ASK_DEC, pp->ns, 1,
249 m, NULL);
250
251/* Fix up CHS values */
252PRT_fix_CHS(disk, pp, pn);
253}
254#undef EDIT
255return (ret);
256}
257
258int
259Xsetpid(cmd, disk, mbr, tt, offset)
260cmd_t *cmd;
261disk_t *disk;
262mbr_t *mbr;
263mbr_t *tt;
264int offset;
265{
266int pn, num, ret;
267prt_t *pp;
268
269ret = CMD_CONT;
270
271if (!isdigit(cmd->args[0])) {
272printf("Invalid argument: %s <partition number>\n", cmd->cmd);
273return (ret);
274}
275pn = atoi(cmd->args) - 1;
276
277if (pn < 0 || pn > 3) {
278printf("Invalid partition number.\n");
279return (ret);
280}
281
282/* Print out current table entry */
283pp = &mbr->part[pn];
284PRT_print(0, NULL);
285PRT_print(pn, pp);
286
287#defineEDIT(p, f, v, n, m, h)\
288if ((num = ask_num(p, f, v, n, m, h)) != v)\
289ret = CMD_DIRTY;\
290v = num;
291
292/* Ask for partition type */
293EDIT("Partition id ('0' to disable) ", ASK_HEX, pp->id, 0, 0xFF, PRT_printall);
294
295#undef EDIT
296return (ret);
297}
298int
299Xselect(cmd, disk, mbr, tt, offset)
300cmd_t *cmd;
301disk_t *disk;
302mbr_t *mbr;
303mbr_t *tt;
304int offset;
305{
306static int firstoff = 0;
307int off;
308int pn;
309
310if (!isdigit(cmd->args[0])) {
311printf("Invalid argument: %s <partition number>\n", cmd->cmd);
312return (CMD_CONT);
313}
314
315pn = atoi(cmd->args) - 1;
316if (pn < 0 || pn > 3) {
317printf("Invalid partition number.\n");
318return (CMD_CONT);
319}
320
321off = mbr->part[pn].bs;
322
323/* Sanity checks */
324if ((mbr->part[pn].id != DOSPTYP_EXTEND) &&
325 (mbr->part[pn].id != DOSPTYP_EXTENDL)) {
326printf("Partition %d is not an extended partition.\n", pn + 1);
327return (CMD_CONT);
328}
329
330if (firstoff == 0)
331firstoff = off;
332
333if (!off) {
334printf("Loop to offset 0! Not selected.\n");
335return (CMD_CONT);
336} else {
337printf("Selected extended partition %d\n", pn + 1);
338printf("New MBR at offset %d.\n", off);
339}
340
341/* Recursion is beautifull! */
342USER_modify(disk, tt, off, firstoff);
343return (CMD_CONT);
344}
345
346int
347Xprint(cmd, disk, mbr, tt, offset)
348cmd_t *cmd;
349disk_t *disk;
350mbr_t *mbr;
351mbr_t *tt;
352int offset;
353{
354
355DISK_printmetrics(disk);
356printf("Offset: %d\t", offset);
357MBR_print(mbr);
358
359return (CMD_CONT);
360}
361
362int
363Xwrite(cmd, disk, mbr, tt, offset)
364cmd_t *cmd;
365disk_t *disk;
366mbr_t *mbr;
367mbr_t *tt;
368int offset;
369{
370int fd;
371int shared = 0;
372
373fd = DISK_openshared(disk->name, O_RDWR, &shared);
374if(shared) {
375 if(!ask_yn("Device could not be accessed exclusively.\nA reboot will be needed for changes to take effect. OK?", 0)) {
376 close(fd);
377 printf("MBR unchanged\n");
378 return (CMD_CONT);
379 }
380}
381
382printf("Writing MBR at offset %d.\n", offset);
383
384MBR_make(mbr);
385MBR_write(disk, fd, mbr);
386close(fd);
387return (CMD_CLEAN);
388}
389
390int
391Xquit(cmd, disk, r, tt, offset)
392cmd_t *cmd;
393disk_t *disk;
394mbr_t *r;
395mbr_t *tt;
396int offset;
397{
398
399/* Nothing to do here */
400return (CMD_SAVE);
401}
402
403int
404Xabort(cmd, disk, mbr, tt, offset)
405cmd_t *cmd;
406disk_t *disk;
407mbr_t *mbr;
408mbr_t *tt;
409int offset;
410{
411exit(0);
412
413/* NOTREACHED */
414return (CMD_CONT);
415}
416
417
418int
419Xexit(cmd, disk, mbr, tt, offset)
420cmd_t *cmd;
421disk_t *disk;
422mbr_t *mbr;
423mbr_t *tt;
424int offset;
425{
426
427/* Nothing to do here */
428return (CMD_EXIT);
429}
430
431int
432Xhelp(cmd, disk, mbr, tt, offset)
433cmd_t *cmd;
434disk_t *disk;
435mbr_t *mbr;
436mbr_t *tt;
437int offset;
438{
439cmd_table_t *cmd_table = cmd->table;
440int i;
441
442/* Hmm, print out cmd_table here... */
443for (i = 0; cmd_table[i].cmd != NULL; i++)
444printf("\t%s\t\t%s\n", cmd_table[i].cmd, cmd_table[i].help);
445return (CMD_CONT);
446}
447
448int
449Xupdate(cmd, disk, mbr, tt, offset)
450cmd_t *cmd;
451disk_t *disk;
452mbr_t *mbr;
453mbr_t *tt;
454int offset;
455{
456extern char *mbr_binary;
457/* Update code */
458memcpy(mbr->code, mbr_binary, MBR_CODE_SIZE);
459printf("Machine code updated.\n");
460return (CMD_DIRTY);
461}
462
463int
464Xflag(cmd, disk, mbr, tt, offset)
465cmd_t *cmd;
466disk_t *disk;
467mbr_t *mbr;
468mbr_t *tt;
469int offset;
470{
471int i, pn = -1;
472
473/* Parse partition table entry number */
474if (!isdigit(cmd->args[0])) {
475printf("Invalid argument: %s <partition number>\n", cmd->cmd);
476return (CMD_CONT);
477}
478pn = atoi(cmd->args) - 1;
479
480if (pn < 0 || pn > 3) {
481printf("Invalid partition number.\n");
482return (CMD_CONT);
483}
484
485/* Set active flag */
486for (i = 0; i < 4; i++) {
487if (i == pn)
488mbr->part[i].flag = DOSACTIVE;
489else
490mbr->part[i].flag = 0x00;
491}
492
493printf("Partition %d marked active.\n", pn + 1);
494return (CMD_DIRTY);
495}
496
497int
498Xmanual(cmd, disk, mbr, tt, offset)
499cmd_t *cmd;
500disk_t *disk;
501mbr_t *mbr;
502mbr_t *tt;
503int offset;
504{
505system("man 8 fdisk");
506return (CMD_CONT);
507}
508

Archive Download this file

Revision: 302