File: pcbios.c

package info (click to toggle)
acorn-fdisk 3.0.6-13
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,068 kB
  • sloc: ansic: 5,422; makefile: 99
file content (610 lines) | stat: -rw-r--r-- 16,342 bytes parent folder | download | duplicates (5)
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
/*
 * lib/scheme/pcbios.c
 *
 * Partition backend for PC/BIOS partitioning scheme.  This consists
 * of a partition table in the first sector of the disk which contains
 * both CHS and LBA values for the partitions.  There can be up to four
 * entries in the primary partition table.  One of these can be an
 * extended partition containing up to four extra partitions.
 *
 * NOTE: We do not support extended partitions yet.
 *
 * Copyright (C) 1997,1998 Russell King
 */
#include <assert.h>
#include <stdlib.h>
#include <string.h>

#include "util/debug.h"
#include "util/error.h"
#include "util/warning.h"
#include "part/part.h"
#include "part/utils.h"
#include "scheme/pcbios.h"

#define PCBIOS_SAVE
#define PCBIOS_DEBUG

#define PCBIOS_SECTOR_SIZE	512
#define PCBIOS_BOOT_SECTOR	0
#define PCBIOS_NR_PRIMARY	4

#define is_extended(x)	((x) == ptyp_dos_extended)

/* Prototype: void pcbios_copychsin(chs_t *chs, u8 *p)
 * Purpose  : Copy a CHS value from a PC partition table to a chs_t structure
 * Params   : chs - destination for CHS values
 *          : p   - pointer to start of packed CHS values
 */
static void pcbios_copychsin(chs_t *chs, u8 *p)
{
  chs->head     = p[0];
  chs->sector   = (p[1] & 63) - 1;
  chs->cylinder = p[2] | (p[1] & 0xc0) << 2;
}

/* Prototype: void pcbios_copychsout(u8 *p, chs_t *chs)
 * Purpose  : Copy a CHS value to a PC partition table from a chs_t structure
 * Params   : p   - pointer to start of packed CHS values
 *          : chs - values to pack for CHS values
 */
static void pcbios_copychsout(u8 *p, chs_t *chs)
{
  p[0] = chs->head;
  p[1] = ((chs->sector + 1) & 63) | ((chs->cylinder >> 2) & 0xc0);
  p[2] = chs->cylinder & 0xff;
}

/* Prototype: u32 pcbios_copylogin(u8 *p)
 * Purpose  : Copy a logical start/end 32-bit value from a PC partition table
 * Params   : p - pointer to start of 32-bit value
 * Returns  : 32-bit unsigned integer
 */
static u32 pcbios_copylogin(u8 *p)
{
  return p[0] | p[1] << 8 | p[2] << 16 | p[3] << 24;
}

/* Prototype: u_int pcbios_copylogout(u8 *p, u32 l)
 * Purpose  : Copy a logical start/end 32-bit value to a PC partition table
 * Params   : p - pointer to start of 32-bit value
 *          : l - value to place
 */
static void pcbios_copylogout(u8 *p, u32 l)
{
  p[0] = l & 255;
  p[1] = (l >> 8) & 255;
  p[2] = (l >> 16) & 255;
  p[3] = (l >> 24) & 255;
}

/* Prototype: u_int pcbios_detect(part_t *part)
 * Purpose  : detect a drive partitioned with a PC/BIOS partition table
 * Params   : part - partitionable device
 * Returns  : FALSE if not detected
 */
static u_int
pcbios_detect(part_t *part)
{
  u_char sector[PCBIOS_SECTOR_SIZE];
  u_int ret = 0;

  dbg_printf("pcbios_detect()");
  dbg_level_up();

  assert(part != NULL);

  do {
    if (blkio_setblocksize(part->blkio, PCBIOS_SECTOR_SIZE) != PCBIOS_SECTOR_SIZE)
      break;

    if (blkio_read(part->blkio, sector, PCBIOS_BOOT_SECTOR, 1) != 1)
      break;

    if (sector[510] != 0x55 || sector[511] != 0xaa)
      break;

    ret = 1;
  } while (0);

  dbg_level_down();
  dbg_printf("ret%s detected", ret ? "" : " not");
  return ret;
}

static u_int
pcbios_readextended(part_t *part, blk_t blkstart, u_int size)
{
  static u_char sector[PCBIOS_SECTOR_SIZE];
  u_int ret = 0;
  u_int extended_nr = 5;

  dbg_printf("pcbios_readextended(%d, %d)", blkstart, size);
  dbg_level_up();

  assert(part != NULL);

  do {
    u_char *p = sector + 0x1be;
    partinfo_i_t pinfo;
    u_int parn;

    if (blkio_read(part->blkio, sector, blkstart, 1) != 1)
      break;

    if (sector[510] != 0x55 || sector[511] != 0xaa) {
      issue_warning("The extended partition table does not contain a valid signature."
      		    "  This will be corrected when writing partition information "
      		    "to disk.");
      sector[510] = 0x55;
      sector[511] = 0xaa;
    }

    part->data.pcbios.extended_sector = sector;

    ret = 1;
    for (parn = 0; parn < PCBIOS_NR_PRIMARY; parn++, p += 16)
      if (p[4] && !is_extended(p[4])) {
        blk_t start;
        u32 size;

        pinfo.info.chs_valid = 1;
        pinfo.info.kern_part_no = extended_nr;
        pcbios_copychsin(&pinfo.info.chs_start, p + 1);
        pcbios_copychsin(&pinfo.info.chs_end, p + 5);
        pinfo.info.type	        = p[4];
        start = pcbios_copylogin(p + 8) + blkstart;
        size  = pcbios_copylogin(p + 12);
        pinfo.info.blk_start    = start;
        pinfo.info.blk_end      = start + size - 1;
        if (!part_add (part, extended_nr, &pinfo)) {
          ret = 0;
          break;
        }
        extended_nr += 1;
      }
    p = sector + 0x1be;
    for (parn = 0; parn < PCBIOS_NR_PRIMARY; parn++, p += 16)
      if (is_extended(p[4])) {
      	blkstart += pcbios_copylogin(p + 8);
      	size = pcbios_copylogin(p + 12);
      }
  } while (0);

  dbg_level_down();
  dbg_printf("ret %s", ret ? "ok" : "error");

  return ret;
}

/* Prototype: u_int pcbios_readinfo(part_t *part)
 * Purpose  : read all partition information from the drive
 * Params   : part - partitionable device
 * Returns  : FALSE on error
 */
static u_int
pcbios_readinfo(part_t *part)
{
  static u_char sector[PCBIOS_SECTOR_SIZE];
  u_int ret = 0;

  dbg_printf("pcbios_readinfo()");
  dbg_level_up();

  assert(part != NULL);

  do {
    partinfo_i_t pinfo;
    chs_t chs;
    blk_t start;
    u_int parn, valid;
    u_char *p;

    part->data.pcbios.pcboot_sector = sector;

    if (blkio_read(part->blkio, sector, PCBIOS_BOOT_SECTOR, 1) != 1)
      break;

    valid = sector[510] == 0x55 && sector[511] == 0xaa;

    if (!valid) {
      issue_warning("The master boot sector does not contain a valid signature."
      		    "  This will be corrected when writing partition information "
      		    " to disk.");
      sector[510] = 0x55;
      sector[511] = 0xaa;
    }

    p = part->data.pcbios.pcboot_sector + 0x1be;

#if 0
    if (valid && chs.cylinder == 0) {
    /* } chs is uninitialised here */
#else
    if (valid) {
#endif
      /*
       * Attempt to calculate the size of one track
       *  track_size = (start_log / (cyl * head) - sector
       */
      start = pcbios_copylogin(p + 8);
      pcbios_copychsin(&chs, p + 1);
      start /= chs.head ? chs.head : 1;
      start -= chs.sector;
    } else {
      geometry_t geo;

      blkio_getgeometry(part->blkio, &geo);
      
      start = geo.sectors * geo.heads;
    }

    memset (&pinfo, 0, sizeof(pinfo));
    pinfo.info.chs_valid	  = 1;
    pinfo.info.chs_start.sector	  = 0;
    pinfo.info.chs_end.sector	  = start - 1;
    pinfo.info.blk_end		  = start - 1;
    pinfo.info.type		  = ptyp_pc_table;

    if (!part_add(part, 0, &pinfo))
      break;

    /*
     * Look for all possible primary partitions
     *  - feature - if the drive is already partitioned,
     *     with non-Linux partitions, we will assert!
     */
    ret = 1;
    for (parn = 0; parn < PCBIOS_NR_PRIMARY; parn++, p += 16)
      if (p[4]) {
        blk_t start;
        u32 size;

        pinfo.info.chs_valid = 1;
        pinfo.info.kern_part_no = parn + 1;
        pcbios_copychsin(&pinfo.info.chs_start, p + 1);
        pcbios_copychsin(&pinfo.info.chs_end, p + 5);
        pinfo.info.type	        = p[4];
        start = pcbios_copylogin(p + 8);
        size  = pcbios_copylogin(p + 12);
        pinfo.info.blk_start    = start;
        pinfo.info.blk_end      = start + size - 1;
        if (!part_add (part, parn + 1, &pinfo)) {
          ret = 0;
          break;
        }
        if (is_extended(p[4]) && !pcbios_readextended(part, start, size)) {
          ret = 0;
          break;
        }
      }
  } while (0);

  dbg_level_down();
  dbg_printf("ret %s", ret ? "ok" : "error");

  return ret;
}

/* Prototype: u_int pcbios_writeinfo(part_t *part)
 * Purpose  : write all partition information back to the drive
 * Params   : part - partitionable device
 * Returns  : FALSE on error
 */
static u_int
pcbios_writeinfo(part_t *part)
{
  u_int parn;
  u_int ret = 0;
  u_char *p;

  dbg_printf("pcbios_writeinfo()");
  dbg_level_up();

  assert(part != NULL);
  assert(part->nr_partitions <= PCBIOS_NR_PRIMARY + 1);
  assert(part->data.pcbios.pcboot_sector != NULL);

  /*
   * We use the mbr sector that we loaded when we started
   * the partition process, so keeping any PC code
   */
  p = part->data.pcbios.pcboot_sector + 0x1be;
  /*
   * Write the magic number
   */
  part->data.pcbios.pcboot_sector[510] = 0x55;
  part->data.pcbios.pcboot_sector[511] = 0xaa;
  /*
   * Loop through *all* possible partitions - we zero the
   * undefined partition entries.
   */
  for (parn = 1; parn < PCBIOS_NR_PRIMARY + 1; parn++, p += 16) {
    partinfo_i_t *info;

    memset(p, 0, 16);

    if (parn < part->nr_partitions) {
      info = part->partinfo[parn];

      if (info && info->info.type != ptyp_none) {
        /*
         * We should always have 'type' as an 8-bit value
         */
        assert(info->info.type <= 0xff);
        /*
         * p[0] = boot flag... (we don't set yet)
         */
        p[4] = info->info.type;
        pcbios_copychsout(p + 1, &info->info.chs_start);
        pcbios_copychsout(p + 5, &info->info.chs_end);
        pcbios_copylogout(p + 8, info->info.blk_start);
        pcbios_copylogout(p + 12, info->info.blk_end - info->info.blk_start + 1);
      }
    }
  }

#ifdef PCBIOS_DEBUG
  dbg_printf("Boot sector:");
  dbg_memdump(part->data.pcbios.pcboot_sector, 512);
#endif
#ifdef PCBIOS_SAVE
  if (blkio_write(part->blkio, part->data.pcbios.pcboot_sector, PCBIOS_BOOT_SECTOR, 1) != 1)
    set_error("unable to save partition information");
  else
    ret = 1;
#else
  set_error("PC/BIOS partition information cannot be saved with this build");
#endif

  dbg_level_down();
  dbg_printf("ret %s", ret ? "ok" : "error");

  return ret;
}

/* Prototype: u_int pcbios_allocate(part_t *part, partinfo_t *pnew)
 * Purpose  : allocate a partition entry number and a kernel partition
 *            number for a new partition
 * Params   : part - partitionable device
 * Returns  : partition number, or PARN_ERROR on error
 */
static u_int
pcbios_allocate(part_t *part, partinfo_t *pnew)
{
  u_int parn;

  dbg_printf("pcbios_allocate()");

  assert(part != NULL);
  assert(pnew != NULL);

  /*
   * Look for a free primary partition
   */
  for (parn = 1; parn < 1 + PCBIOS_NR_PRIMARY && parn < part->nr_partitions; parn++)
    if (!part->partinfo[parn] || part->partinfo[parn]->info.type == ptyp_none)
      break;

  if (parn >= 1 + PCBIOS_NR_PRIMARY) {
    set_error("primary bios partition table is full");
    parn = PARN_ERROR;
  } else {
    pnew->kern_part_no = parn;
    pnew->type = ptyp_linux_native;
  }

  dbg_printf("ret %d", parn);

  assert(parn < 1 + PCBIOS_NR_PRIMARY || parn == PARN_ERROR);

  return parn;
}

/* Prototype: u_int pcbios_validate_change(part_t *part, u_int parn,
 *                                         const partinfo_t *pold, const partinfo_t *pnew)
 * Purpose  : validate changes made to a partition
 * Params   : part - partitionable device
 *          : parn - partition number
 *          : pold - old partition information
 *          : pnew - new partition information
 * Returns  : FALSE if we reject the change
 */
static u_int
pcbios_validate_change(part_t *part, u_int parn, const partinfo_t *pold, const partinfo_t *pnew)
{
  u_int ret = 0;

  dbg_printf("pcbios_validate_change(parn=%d)", parn);
  dbg_level_up();

  assert(part != NULL);
  assert(pold != NULL);
  assert(parn < part->nr_partitions);

  do {
    if (parn == 0) {
      set_error("unable to modify the BIOS partition table partition");
      break;
    }

    if (pnew && check_overlap(part, parn, pnew)) {
      set_error("the modified partition overlaps an existing partition");
      break;
    }

    ret = 1;
  } while (0);

  dbg_level_down();
  dbg_printf("ret %svalid", ret ? "" : "in");

  return ret;
}

/* Prototype: u_int pcbios_validate_creation(part_t *part, u_int parn,
 *                                           const partinfo_t *pold, const partinfo_t *pnew)
 * Purpose  : validate changes made to a partition
 * Params   : part - partitionable device
 *          : parn - partition number
 *          : pold - old partition information (NULL if none)
 *          : pnew - new partition information
 * Returns  : FALSE if we reject the creation
 */
static u_int
pcbios_validate_creation(part_t *part, u_int parn, const partinfo_t *pold, const partinfo_t *pnew)
{
  u_int ret = 0;

  dbg_printf("pcbios_validate_creation(parn=%d)", parn);
  dbg_level_up();

  assert(part != NULL);
  assert(parn < part->nr_partitions);

  do {
    if (pold != NULL) {
      set_error("unable to create partition in this slot");
      break;
    }

    if (pnew && check_overlap(part, parn, pnew)) {
      set_error("the new partition overlaps an existing partition");
      break;
    }

    ret = 1;
  } while (0);

  dbg_level_down();
  dbg_printf("ret %svalid", ret ? "" : "in");

  return ret;
}

/* Prototype: u_int pcbios_validate_deletion(part_t *part, u_int parn, const partinfo_t *pold)
 * Purpose  : validate a deletion of a partition
 * Params   : part - partitionable device
 *          : parn - partition number
 *          : pold - old partition information
 * Returns  : FALSE if we reject the deletion
 */
static u_int
pcbios_validate_deletion(part_t *part, u_int parn, const partinfo_t *pold)
{
  u_int ret = 0;

  dbg_printf("pcbios_validate_deletion(parn=%d)", parn);
  dbg_level_up();

  assert(part != NULL);
  assert(pold != NULL);
  assert(parn < part->nr_partitions);

  if (parn > 0)
    ret = 1;
  else
    set_error("unable to delete partition");

  dbg_level_down();
  dbg_printf("ret %svalid", ret ? "" : "in");

  return ret;
}

/* Prototype: u_int icside_validate_partno(part_t *part, u_int parn)
 * Purpose  : validate a partition number
 * Params   : part - partitionable device
 *          : parn - partition number
 * Returns  : FALSE if we reject the partition number
 */
static u_int
pcbios_validate_partno(part_t *part, u_int parn)
{
  u_int ret = 0;

  dbg_printf("pcbios_validate_partno(parn=%d)", parn);
  dbg_level_up();

  assert(part != NULL);

  if (parn > 0 && parn < 1 + PCBIOS_NR_PRIMARY)
    ret = 1;
  else
    set_error("the requested partition is invalid");

  dbg_level_down();
  dbg_printf("ret %svalid", ret ? "" : "in");

  return ret;
}

/* Prototype: ptyp_t pcbios_nexttype(part_t *part, u_int parn, ptyp_t current, int dir)
 * Purpose  : return next valid partition type for an entry
 * Params   : part    - partitionable device
 *          : parn    - partition number
 *          : current - currently selected partition type
 *          : dir     - next/previous flag
 * Returns  : next valid partition type
 */
static ptyp_t
pcbios_nexttype(part_t *part, u_int parn, ptyp_t current, int dir)
{
  static ptyp_t types[] = {
    ptyp_dos12, ptyp_xenixroot, ptyp_xenixusr, ptyp_dos16l32, ptyp_dos_extended,
    ptyp_dos16g32, ptyp_hpfs, ptyp_aix, ptyp_aixboot, ptyp_os2, ptyp_win98_extended,
    ptyp_venix, ptyp_novell, ptyp_microport, ptyp_dm6_aux3, ptyp_dm6,
    ptyp_ezdrive, ptyp_gnuhurd, ptyp_novelnet286, ptyp_novelnet386, ptyp_pcix, ptyp_old_minix,
    ptyp_minix, ptyp_linux_swap, ptyp_linux_native, ptyp_linux_extended, ptyp_amoeba,
    ptyp_amoebabbt, ptyp_bsd386, ptyp_bsdifs, ptyp_bsdiswap, ptyp_syrinx, ptyp_cpm,
    ptyp_dosaccess, ptyp_dosro, ptyp_dosscc, ptyp_bbt
  };
#define NR_TYPES (sizeof(types) / sizeof(ptyp_t))
  ptyp_t ptype = current;

  dbg_printf("pcbios_nexttype(parn=%d, current=0x%X, %s)", parn, current,
	     dir > 0 ? "next" : "previous");
  dbg_level_up();
  assert(part != NULL);
  assert(parn < part->nr_partitions);

  if (parn == 0)
    ptype = ptyp_pc_table;
  else {
    s_int i;

    for (i = 0; i < NR_TYPES; i++)
      if (types[i] == current)
        break;

    if (i < NR_TYPES && types[i] == current) {
      i += dir;

      if (i < 0)
        i = 0;

      if (i >= NR_TYPES)
        i = NR_TYPES - 1;

      ptype = types[i];
    } else
      ptype = ptyp_linux_native;
  }

  dbg_level_down();
  dbg_printf("ret 0x%X", ptype);

  return ptype;
}
scheme_t pcbios_scheme = {
  "PC/BIOS",
  pcbios_detect,
  pcbios_readinfo,
  pcbios_writeinfo,
  pcbios_allocate,
  pcbios_validate_change,
  pcbios_validate_creation,
  pcbios_validate_deletion,
  pcbios_validate_partno,
  pcbios_nexttype
};