aboutsummaryrefslogtreecommitdiffstats
path: root/diskdev_cmds/fdisk.tproj/mbr.c
blob: 90c88099e5919d8de6a51946b1ffc40080a964e9 (plain) (blame)
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
/*
 * Copyright (c) 2002, 2005 Apple Computer, Inc. All rights reserved.
 *
 * @APPLE_LICENSE_HEADER_START@
 * 
 * This file contains Original Code and/or Modifications of Original Code
 * as defined in and that are subject to the Apple Public Source License
 * Version 2.0 (the 'License'). You may not use this file except in
 * compliance with the License. Please obtain a copy of the License at
 * http://www.opensource.apple.com/apsl/ and read it before using this
 * file.
 * 
 * The Original Code and all software distributed under the License are
 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
 * Please see the License for the specific language governing rights and
 * limitations under the License.
 * 
 * @APPLE_LICENSE_HEADER_END@
 */

/*
 * Copyright (c) 1997 Tobias Weingartner
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *    This product includes software developed by Tobias Weingartner.
 * 4. The name of the author may not be used to endorse or promote products
 *    derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include <err.h>
#include <util.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <ctype.h>
#include <memory.h>
#include <sys/fcntl.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>
#if 0
#include <sys/dkio.h>
#endif
#include <machine/param.h>
#include "disk.h"
#include "misc.h"
#include "mbr.h"
#include "part.h"


void
MBR_init(disk, mbr)
	disk_t *disk;
	mbr_t *mbr;
{
	/* Fix up given mbr for this disk */
	mbr->part[0].flag = 0;
	mbr->part[1].flag = 0;
	mbr->part[2].flag = 0;
#if !defined(DOSPTYP_OPENBSD)
	mbr->part[3].flag = 0;
	mbr->signature = MBR_SIGNATURE;
#else

	mbr->part[3].flag = DOSACTIVE;
	mbr->signature = DOSMBR_SIGNATURE;

	/* Use whole disk, save for first head, on first cyl. */
	mbr->part[3].id = DOSPTYP_OPENBSD;
	mbr->part[3].scyl = 0;
	mbr->part[3].shead = 1;
	mbr->part[3].ssect = 1;

	/* Go right to the end */
	mbr->part[3].ecyl = disk->real->cylinders - 1;
	mbr->part[3].ehead = disk->real->heads - 1;
	mbr->part[3].esect = disk->real->sectors;

	/* Fix up start/length fields */
	PRT_fix_BN(disk, &mbr->part[3], 3);

#if defined(__powerpc__) || defined(__mips__)
	/* Now fix up for the MS-DOS boot partition on PowerPC. */
	mbr->part[0].flag = DOSACTIVE;	/* Boot from dos part */
	mbr->part[3].flag = 0;
	mbr->part[3].ns += mbr->part[3].bs;
	mbr->part[3].bs = mbr->part[0].bs + mbr->part[0].ns;
	mbr->part[3].ns -= mbr->part[3].bs;
	PRT_fix_CHS(disk, &mbr->part[3], 3);
	if ((mbr->part[3].shead != 1) || (mbr->part[3].ssect != 1)) {
		/* align the partition on a cylinder boundary */
		mbr->part[3].shead = 0;
		mbr->part[3].ssect = 1;
		mbr->part[3].scyl += 1;
	}
	/* Fix up start/length fields */
	PRT_fix_BN(disk, &mbr->part[3], 3);
#endif
#endif
}

void
MBR_parse(disk, offset, reloff, mbr)
	disk_t *disk;
	off_t offset;
	off_t reloff;
	mbr_t *mbr;
{
	int i;
	unsigned char *mbr_buf = mbr->buf;

	memcpy(mbr->code, mbr_buf, MBR_CODE_SIZE);
	mbr->offset = offset;
	mbr->reloffset = reloff;
	mbr->signature = getshort(&mbr_buf[MBR_SIG_OFF]);

	for (i = 0; i < NDOSPART; i++)
		PRT_parse(disk, &mbr_buf[MBR_PART_OFF + MBR_PART_SIZE * i], 
		    offset, reloff, &mbr->part[i], i);
}

void
MBR_make(mbr)
	mbr_t *mbr;
{
	int i;
	unsigned char *mbr_buf = mbr->buf;

	memcpy(mbr_buf, mbr->code, MBR_CODE_SIZE);
	putshort(&mbr_buf[MBR_SIG_OFF], mbr->signature);

	for (i = 0; i < NDOSPART; i++)
		PRT_make(&mbr->part[i], mbr->offset, mbr->reloffset, 
		    &mbr_buf[MBR_PART_OFF + MBR_PART_SIZE * i]);
}

void
MBR_print(mbr)
	mbr_t *mbr;
{
	int i;

	/* Header */
	printf("Signature: 0x%X\n",
	    (int)mbr->signature);
	PRT_print(0, NULL);

	/* Entries */
	for (i = 0; i < NDOSPART; i++)
		PRT_print(i, &mbr->part[i]);
}

int
MBR_read(disk, fd, where, mbr)
	disk_t *disk;
	int fd;
	off_t where;
	mbr_t *mbr;
{
	off_t off;
	int len;
	int size;
	unsigned char *buf = mbr->buf;

	size = disk->real->sector_size;
	where *= size;
	off = lseek(fd, where, SEEK_SET);
	if (off != where)
		return (off);
	len = read(fd, buf, size);
	if (len != size)
		return (len);
	return (0);
}

int
MBR_write(disk, fd, mbr)
	disk_t *disk;
	int fd;
	mbr_t *mbr;
{
	off_t off;
	int len;
	int size;
	unsigned char *buf = mbr->buf;
	off_t where;

	size = disk->real->sector_size;
	where = mbr->offset * size;
	off = lseek(fd, where, SEEK_SET);
	if (off != where)
		return (off);
	len = write(fd, buf, size);
	if (len != size)
		return (len);
#if defined(DIOCRLDINFO)
	(void) ioctl(fd, DIOCRLDINFO, 0);
#endif
	return (0);
}

void 
MBR_pcopy(disk, mbr)
	disk_t *disk;
	mbr_t *mbr;
{
	/* 
	 * Copy partition table from the disk indicated
	 * to the supplied mbr structure 
	 */

	int i, fd, offset = 0, reloff = 0;
	mbr_t *mbrd;
	
	mbrd = MBR_alloc(NULL);
	fd = DISK_open(disk->name, O_RDONLY);
	MBR_read(disk, fd, offset, mbrd);
	DISK_close(fd);
	MBR_parse(disk, offset, reloff, mbrd);
	for (i = 0; i < NDOSPART; i++) {
		PRT_parse(disk, &mbrd->buf[MBR_PART_OFF +
					 MBR_PART_SIZE * i], 
			  offset, reloff, &mbr->part[i], i);
		PRT_print(i, &mbr->part[i]);
	}
	MBR_free(mbrd);
}


static int
parse_number(char *str, int default_val, int base) {
	if (str != NULL && *str != '\0') {
	  default_val = strtol(str, NULL, base);
	}
	return default_val;
}

static inline int
null_arg(char *arg) {
  if (arg == NULL || *arg == 0)
    return 1;
  else
    return 0;
}

/* Parse a partition spec into a partition structure.
 * Spec is of the form:
 *   <start>,<size>,<id>,<bootable>[,<c,h,s>,<c,h,s>]
 * We require passing in the disk and mbr so we can
 * set reasonable defaults for values, e.g. "the whole disk"
 * or "starting after the last partition."
 */
#define N_ARGS 10
static int
MBR_parse_one_spec(char *line, disk_t *disk, mbr_t *mbr, int pn)
{
  int i;
  char *args[N_ARGS];
  prt_t *part = &mbr->part[pn];
  int next_start, next_size;

  /* There are up to 10 arguments. */
  for (i=0; i<N_ARGS; i++) {
    char *arg;
    while (isspace(*line))
      line++;
    arg = strsep(&line, ",\n");
    if (arg == NULL || line == NULL) {
      break;
    }
    args[i] = arg;
  }
  for (; i<N_ARGS; i++) {
    args[i] = NULL;
  }
  /* Set reasonable defaults. */
  if (pn == 0) {
    next_start = 0;
  } else {
    next_start = mbr->part[pn-1].bs + mbr->part[pn-1].ns;
  }
  next_size = disk->real->size;
  for(i=0; i<pn; i++) {
    next_size -= mbr->part[i].ns;
  }

  part->id = parse_number(args[2], 0xA8, 16);
  if (!null_arg(args[3]) && *args[3] == '*') {
    part->flag = 0x80;
  } else {
    part->flag = 0;
  }
  /* If you specify the start or end sector,
     you have to give both. */
  if ((null_arg(args[0]) && !null_arg(args[1])) ||
       (!null_arg(args[0]) && null_arg(args[1]))) {
    errx(1, "You must specify both start and size, or neither");
    return -1;
  }

  /* If you specify one of the CHS args,
     you have to give them all. */
  if (!null_arg(args[4])) {
    for (i=5; i<10; i++) {
      if (null_arg(args[i])) {
	errx(1, "Either all CHS arguments must be specified, or none");
	return -1;
      }
    }

    part->scyl = parse_number(args[4], 0, 10);
    part->shead = parse_number(args[5], 0, 10);
    part->ssect = parse_number(args[6], 0, 10);	  
    part->scyl = parse_number(args[7], 0, 10);
    part->shead = parse_number(args[8], 0, 10);
    part->ssect = parse_number(args[9], 0, 10);	  
    if (null_arg(args[0])) {
      PRT_fix_BN(disk, part, pn);
    }
  } else {
    /* See if they gave no CHS and no start/end */
    if (null_arg(args[0])) {
      errx(1, "You must specify either start sector and size or CHS");
      return -1;
    }
  }
  if (!null_arg(args[0])) {
    part->bs = parse_number(args[0], next_start, 10);
    part->ns = parse_number(args[1], next_size, 10);
    PRT_fix_CHS(disk, part, pn);
  }
  return 0;
}


typedef struct _mbr_chain {
  mbr_t mbr;
  struct _mbr_chain *next;
} mbr_chain_t;

/* Parse some number of MBR spec lines.
 * Spec is of the form:
 *   <start>,<size>,<id>,<bootable>[,<c,h,s>,<c,h,s>]
 * 
 */
mbr_t *
MBR_parse_spec(FILE *f, disk_t *disk)
{
  int lineno;
  int offset, firstoffset;
  mbr_t *mbr, *head, *prev_mbr;

  head = mbr = prev_mbr = NULL;
  firstoffset = 0;
  do {

    offset = 0;
    for (lineno = 0; lineno < NDOSPART && !feof(f); lineno++) {
        char line[256];
        char *str;
	prt_t *part;

	do {
	    str = fgets(line, 256, f);
	} while ((str != NULL) && (*str == '\0'));
        if (str == NULL) {
            break;
        }

	if (mbr == NULL) {
	    mbr = MBR_alloc(prev_mbr);
	    if (head == NULL)
		head = mbr;
	}

	if (MBR_parse_one_spec(line, disk, mbr, lineno)) {
	  /* MBR_parse_one_spec printed the error message. */
	  return NULL;
	}
	part = &mbr->part[lineno];
	if ((part->id == DOSPTYP_EXTEND) || (part->id == DOSPTYP_EXTENDL)) {
	  offset = part->bs;
	  if (firstoffset == 0) firstoffset = offset;
	}
    }
    /* If fewer lines than partitions, zero out the rest of the partitions */
    if (mbr != NULL) {
	for (; lineno < NDOSPART; lineno++) {
	    bzero(&mbr->part[lineno], sizeof(prt_t));
	}
    }
    prev_mbr = mbr;
    mbr = NULL;
  } while (offset >= 0 && !feof(f));

  return head;
}

void
MBR_dump(mbr_t *mbr)
{
  int i;
  prt_t *part;

  for (i=0; i<NDOSPART; i++) {
    part = &mbr->part[i];
    printf("%d,%d,0x%02X,%c,%u,%u,%u,%u,%u,%u\n",
	   part->bs,
	   part->ns,
	   part->id,
	   (part->flag == 0x80) ? '*' : '-',
	   part->scyl,
	   part->shead,
	   part->ssect,
	   part->ecyl,
	   part->ehead,
	   part->esect);
  }
}

mbr_t *
MBR_alloc(mbr_t *parent)
{
  mbr_t *mbr = (mbr_t *)malloc(sizeof(mbr_t));
  bzero(mbr, sizeof(mbr_t));
  if (parent) {
    parent->next = mbr;
  }
  mbr->signature = MBR_SIGNATURE;
  return mbr;
}

void
MBR_free(mbr_t *mbr)
{
  mbr_t *tmp;
  while (mbr) {
    tmp = mbr->next;
    free(mbr);
    mbr = tmp;
  }
}

/* Read and parse all the partition tables on the disk,
 * including extended partitions.
 */
mbr_t *
MBR_read_all(disk_t *disk)
{
  mbr_t *mbr = NULL, *head = NULL;
  int i, fd, offset, firstoff;

  fd = DISK_open(disk->name, O_RDONLY);
  firstoff = offset = 0;
  do {
    mbr = MBR_alloc(mbr);
    if (head == NULL) {
      head = mbr;
    }
    MBR_read(disk, fd, offset, mbr);
    MBR_parse(disk, offset, firstoff, mbr);
    if (mbr->signature != MBR_SIGNATURE) {
      /* The MBR signature is invalid. */
      break;
    }    
    offset = 0;
    for (i=0; i<NDOSPART; i++) {
      prt_t *part = &mbr->part[i];
      if ((part->id == DOSPTYP_EXTEND) || (part->id == DOSPTYP_EXTENDL)) {
	offset = part->bs;
	if (firstoff == 0) {
	  firstoff = offset;
	}
      }
    }
  } while (offset > 0);
  DISK_close(fd);

  return head;
}


int
MBR_write_all(disk_t *disk, mbr_t *mbr)
{
  int result = 0;
  int fd;

  fd = DISK_open(disk->name, O_RDWR);
  while (mbr) {
    MBR_make(mbr);
    result = MBR_write(disk, fd, mbr);
    if (result)
      break;
    mbr = mbr->next;
  }
  DISK_close(fd);
  return result;
}

void
MBR_print_all(mbr_t *mbr) {
  while (mbr) {
    MBR_print(mbr);
    mbr = mbr->next;
  }
}

void
MBR_dump_all(mbr_t *mbr) {
  while (mbr) {
    MBR_dump(mbr);
    mbr = mbr->next;
  }
}

void
MBR_clear(mbr_t *mbr) {
  int i;
  if (mbr->next) {
    MBR_free(mbr->next);
    mbr->next = NULL;
  }
  for (i=0; i<4; i++) {
    bzero(&mbr->part[i], sizeof(mbr->part[i]));
  }
  bzero(&mbr->buf, sizeof(mbr->buf));
}