-
Notifications
You must be signed in to change notification settings - Fork 2
/
hfdisk.c
650 lines (579 loc) · 13.8 KB
/
hfdisk.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
//
// hfdisk - an editor for Apple format partition tables
//
// Written by Eryk Vershen ([email protected])
//
// Still under development (as of 20 Dec 1996)
//
/*
* Copyright 1996,1997 by Apple Computer, Inc.
* All Rights Reserved
*
* Permission to use, copy, modify, and distribute this software and
* its documentation for any purpose and without fee is hereby granted,
* provided that the above copyright notice appears in all copies and
* that both the copyright notice and this permission notice appear in
* supporting documentation.
*
* APPLE COMPUTER DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE
* INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE.
*
* IN NO EVENT SHALL APPLE COMPUTER BE LIABLE FOR ANY SPECIAL, INDIRECT, OR
* CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
* LOSS OF USE, DATA OR PROFITS, WHETHER IN ACTION OF CONTRACT,
* NEGLIGENCE, OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
* WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <stdio.h>
#include <getopt.h>
#include <stddef.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <inttypes.h>
#include <sys/ioctl.h>
#include "hfdisk.h"
#include "io.h"
#include "errors.h"
#include "partition_map.h"
#include "dump.h"
#include "version.h"
//
// Defines
//
#define ARGV_CHUNK 5
//
// Types
//
//
// Global Constants
//
enum getopt_values {
kLongOption = 0,
kBadOption = '?',
kOptionArg = 1000,
kListOption = 1001
};
const NAMES plist[] = {
{"Drvr", "Apple_Driver"},
{"Dr43", "Apple_Driver43"},
{"Free", "Apple_Free"},
{" HFS", "Apple_HFS"},
{" MFS", "Apple_MFS"},
{"PDOS", "Apple_PRODOS"},
{"junk", "Apple_Scratch"},
{"unix", "Apple_UNIX_SVR2"},
{" map", "Apple_partition_map"},
{0, 0}
};
//
// Global Variables
//
int lflag;
char *lfile;
int vflag;
int hflag;
int dflag;
int rflag;
//
// Forward declarations
//
void do_add_intel_partition(partition_map_header *map);
void do_change_map_size(partition_map_header *map);
void do_create_partition(partition_map_header *map, int get_type);
void do_create_bootstrap_partition(partition_map_header *map);
void do_delete_partition(partition_map_header *map);
int do_expert(partition_map_header *map);
void do_reorder(partition_map_header *map);
void do_write_partition_map(partition_map_header *map);
void edit(char *name);
int get_base_argument(long *number, partition_map_header *map);
int get_size_argument(uint32_t base, long *number, partition_map_header *map);
int get_options(int argc, char **argv);
void print_notes();
//
// Routines
//
int
main(int argc, char **argv)
{
int name_index;
int err=0;
if (sizeof(DPME) != PBLOCK_SIZE) {
fatal(-1, "Size of partion map entry (%d) "
"is not equal to block size (%d)\n",
sizeof(DPME), PBLOCK_SIZE);
}
if (sizeof(Block0) != PBLOCK_SIZE) {
fatal(-1, "Size of block zero structure (%d) "
"is not equal to block size (%d)\n",
sizeof(Block0), PBLOCK_SIZE);
}
name_index = get_options(argc, argv);
if (vflag) {
printf("version " VERSION " (" RELEASE_DATE ")\n");
}
if (hflag) {
do_help();
} else if (lflag) {
if (lfile != NULL) {
dump(lfile);
} else if (name_index < argc) {
while (name_index < argc) {
dump(argv[name_index++]);
}
} else {
list_all_disks();
}
} else if (name_index < argc) {
while (name_index < argc) {
edit(argv[name_index++]);
}
} else if (!vflag) {
usage("no device argument");
do_help();
err=-EINVAL; // debatable
}
exit(err);
}
int
get_options(int argc, char **argv)
{
int c;
static struct option long_options[] =
{
// name has_arg &flag val
{"help", no_argument, 0, 'h'},
{"list", optional_argument, 0, kListOption},
{"version", no_argument, 0, 'v'},
{"debug", no_argument, 0, 'd'},
{"readonly", no_argument, 0, 'r'},
{0, 0, 0, 0}
};
int option_index = 0;
extern int optind;
extern char *optarg;
int flag = 0;
init_program_name(argv);
lflag = 0;
lfile = NULL;
vflag = 0;
hflag = 0;
dflag = 0;
rflag = 0;
optind = 0; // reset option scanner logic
while ((c = getopt_long(argc, argv, "hlvdr", long_options,
&option_index)) >= 0) {
switch (c) {
case kLongOption:
// option_index would be used here
break;
case 'h':
hflag = 1;
break;
case kListOption:
if (optarg != NULL) {
lfile = optarg;
}
// fall through
case 'l':
lflag = 1;
break;
case 'v':
vflag = 1;
break;
case 'd':
dflag = 1;
break;
case 'r':
rflag = 1;
break;
case kBadOption:
default:
flag = 1;
break;
}
}
if (flag) {
usage("bad arguments");
}
return optind;
}
//
// Edit the file
//
void
edit(char *name)
{
partition_map_header *map;
int command;
int first = 1;
int order;
int get_type;
int valid_file;
map = open_partition_map(name, &valid_file);
if (!valid_file) {
return;
}
printf("%s\n", name);
while (get_command("Command (? for help): ", first, &command)) {
first = 0;
order = 1;
get_type = 0;
switch (command) {
case '?':
print_notes();
case 'H':
case 'h':
printf("Commands are:\n");
printf(" h help\n");
printf(" p print the partition table\n");
printf(" P (print ordered by base address)\n");
printf(" i initialize partition map\n");
printf(" s change size of partition map\n");
printf(" b create new 800K bootstrap partition\n");
printf(" c create new Linux partition\n");
printf(" C (create with type also specified)\n");
printf(" d delete a partition\n");
printf(" r reorder partition entry in map\n");
if (!rflag) {
printf(" w write the partition table\n");
}
printf(" q quit editing (don't save changes)\n");
if (dflag) {
printf(" x extra extensions for experts\n");
}
break;
case 'P':
order = 0;
// fall through
case 'p':
dump_partition_map(map, order);
break;
case 'Q':
case 'q':
goto finis;
break;
case 'I':
case 'i':
map = init_partition_map(name, map);
break;
case 'B':
case 'b':
do_create_bootstrap_partition(map);
break;
case 'C':
get_type = 1;
// fall through
case 'c':
do_create_partition(map, get_type);
break;
case 'D':
case 'd':
do_delete_partition(map);
break;
case 'R':
case 'r':
do_reorder(map);
break;
case 'S':
case 's':
do_change_map_size(map);
break;
case 'X':
case 'x':
if (!dflag) {
goto do_error;
} else if (do_expert(map)) {
goto finis;
}
break;
case 'W':
case 'w':
if (!rflag) {
do_write_partition_map(map);
break;
}
default:
do_error:
bad_input("No such command (%c)", command);
break;
}
}
finis:
close_partition_map(map);
}
void
do_create_partition(partition_map_header *map, int get_type)
{
long base;
long length;
char *name = NULL;
char *type_name = NULL;
if (map == NULL) {
bad_input("No partition map exists");
return;
}
if (!rflag && map->writeable == 0) {
printf("The map is not writeable.\n");
}
// XXX add help feature (i.e. '?' in any argument routine prints help string)
if (get_base_argument(&base, map) == 0) {
return;
}
if (get_size_argument(base, &length, map) == 0) {
return;
}
if (get_string_argument("Name of partition: ", &name, 1) == 0) {
bad_input("Bad name");
return;
}
if (get_type == 0) {
add_partition_to_map(name, kUnixType, base, length, map);
} else {
while (1) {
if (get_string_argument("Type of partition (L for known types): ", &type_name, 1) == 0) {
bad_input("Bad type");
return;
} else if (strncmp(type_name, kFreeType, DPISTRLEN) == 0) {
bad_input("Can't create a partition with the Free type");
} else if (strncmp(type_name, kMapType, DPISTRLEN) == 0) {
bad_input("Can't create a partition with the Map type");
} else if (strncmp(type_name, "L", DPISTRLEN) == 0) {
int i = 0;
while (plist[i].full) {
printf("%s\n", plist[i].full);
++i;
}
printf("\n");
}
else
{
break;
}
}
add_partition_to_map(name, type_name, base, length, map);
}
}
void
do_create_bootstrap_partition(partition_map_header *map)
{
long base;
if (map == NULL) {
bad_input("No partition map exists");
return;
}
if (!rflag && map->writeable == 0) {
printf("The map is not writeable.\n");
}
// XXX add help feature (i.e. '?' in any argument routine prints help string)
if (get_base_argument(&base, map) == 0) {
return;
}
// create 800K type Apple_Bootstrap partition named `bootstrap'
add_partition_to_map(kBootstrapName, kBootstrapType, base, 1600, map);
}
int
get_base_argument(long *number, partition_map_header *map)
{
int result = 0;
uint32_t defaultFirstBlock = find_free_space(map);
char prompt[32];
sprintf(prompt, "First block [%"PRIu32"]: ", defaultFirstBlock);
if (get_number_argument(prompt, number, defaultFirstBlock) == 0) {
bad_input("Bad block number");
} else {
result = 1;
}
return result;
}
int
get_size_argument(uint32_t base, long *number, partition_map_header *map)
{
int result = 0;
uint32_t defaultSize = 20480; // 10MB
// Work out how much free-space is available.
partition_map* part = find_entry_by_sector(base, map);
if (part)
{
size_t partEnd =
part->data->dpme_pblock_start +
part->data->dpme_pblocks;
defaultSize = partEnd - base;
}
char prompt[80];
sprintf(
prompt,
"Length (in blocks, kB (k), MB (M) or GB (G)) [%"PRIu32"]: ",
defaultSize);
if (get_number_argument(prompt, number, defaultSize) == 0) {
bad_input("Bad length");
} else {
result = 1;
}
return result;
}
void
do_delete_partition(partition_map_header *map)
{
partition_map * cur;
long index;
if (map == NULL) {
bad_input("No partition map exists");
return;
}
if (!rflag && map->writeable == 0) {
printf("The map is not writeable.\n");
}
if (get_number_argument("Partition number: ", &index, kDefault) == 0) {
bad_input("Bad partition number");
return;
}
// find partition and delete it
cur = find_entry_by_disk_address(index, map);
if (cur == NULL) {
printf("No such partition\n");
} else {
delete_partition_from_map(cur);
}
}
void
do_reorder(partition_map_header *map)
{
long old_index;
long index;
if (map == NULL) {
bad_input("No partition map exists");
return;
}
if (!rflag && map->writeable == 0) {
printf("The map is not writeable.\n");
}
if (get_number_argument("Partition number: ", &old_index, kDefault) == 0) {
bad_input("Bad partition number");
return;
}
if (get_number_argument("New number: ", &index, kDefault) == 0) {
bad_input("Bad partition number");
return;
}
move_entry_in_map(old_index, index, map);
}
void
do_write_partition_map(partition_map_header *map)
{
if (map == NULL) {
bad_input("No partition map exists");
return;
}
if (map->changed == 0) {
bad_input("The map has not been changed.");
return;
}
if (map->writeable == 0) {
bad_input("The map is not writeable.");
return;
}
// printf("Writing the map destroys what was there before. ");
printf("IMPORTANT: You are about to write a changed partition map to disk. \n");
printf("For any partition you changed the start or size of, writing out \n");
printf("the map causes all data on that partition to be LOST FOREVER. \n");
printf("Make sure you have a backup of any data on such partitions you \n");
printf("want to keep before answering 'yes' to the question below! \n\n");
if (get_okay("Write partition map? [N/y]: ") != 1) {
return;
}
write_partition_map(map);
printf("\nPartition map written to disk. If any partitions on this disk \n");
printf("were still in use by the system (see messages above), you will need \n");
printf("to reboot in order to utilize the new partition map.\n\n");
// exit(0);
}
int
do_expert(partition_map_header *map)
{
int command;
int first = 0;
int quit = 0;
while (get_command("Expert command (? for help): ", first, &command)) {
first = 0;
switch (command) {
case '?':
print_notes();
case 'H':
case 'h':
printf("Commands are:\n");
printf(" h print help\n");
printf(" x return to main menu\n");
printf(" p print the partition table\n");
if (dflag) {
printf(" P (show data structures - debugging)\n");
}
printf(" s change size of partition map\n");
if (!rflag) {
printf(" w write the partition table\n");
}
printf(" q quit without saving changes\n");
break;
case 'X':
case 'x':
goto finis;
break;
case 'Q':
case 'q':
quit = 1;
goto finis;
break;
case 'S':
case 's':
do_change_map_size(map);
break;
case 'P':
if (dflag) {
show_data_structures(map);
break;
}
// fall through
case 'p':
dump_partition_map(map, 1);
break;
case 'W':
case 'w':
if (!rflag) {
do_write_partition_map(map);
break;
}
default:
bad_input("No such command (%c)", command);
break;
}
}
finis:
return quit;
}
void
do_change_map_size(partition_map_header *map)
{
long size;
if (map == NULL) {
bad_input("No partition map exists");
return;
}
if (!rflag && map->writeable == 0) {
printf("The map is not writeable.\n");
}
if (get_number_argument("New size: ", &size, kDefault) == 0) {
bad_input("Bad size");
return;
}
resize_map(size, map);
}
void
print_notes()
{
printf("Notes:\n");
printf(" Base and length fields are blocks, which are 512 bytes long.\n");
printf(" The name of a partition is descriptive text.\n");
printf("\n");
}