File: icside.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 (526 lines) | stat: -rw-r--r-- 13,306 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
/*
 * lib/scheme/icside.c
 *
 * 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/types.h"
#include "part/part.h"
#include "part/utils.h"

#define ICSIDE_SECTOR_SIZE	512
#define ICSIDE_PART_SECTOR	0
#define ICSIDE_NR_PARTITIONS	16

#undef  ICSIDE_DUMP
#define ICSIDE_SAVE

#define NR(x) (sizeof(x) / sizeof(x[0]))

typedef union {
  struct part {
    u32 start;
    s32 size;
  } p[ICSIDE_SECTOR_SIZE / sizeof(struct part)];
  u8 sector[ICSIDE_SECTOR_SIZE];
} icside_pe_t;

static u_int
icside_checksum(u8 *sector)
{
  u_int sum = 0x50617274;
  int i;

  for (i = 0; i < 508; i++)
    sum += sector[i];

  return sum;
}

/* Prototype: u_int icside_detect(part_t *part)
 * Purpose  : detect a drive partitioned with an ICS IDE table
 * Params   : part - partitionable device
 * Returns  : FALSE if not detected
 */
static u_int
icside_detect(part_t *part)
{
  u_int ret = 0;

  dbg_printf("icside_detect()");
  dbg_level_up();

  assert(part != NULL);

  do {
    icside_pe_t sector;
    u32 calc_checksum, disc_checksum;

    if (blkio_setblocksize(part->blkio, ICSIDE_SECTOR_SIZE) != ICSIDE_SECTOR_SIZE)
      break;

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

    calc_checksum = icside_checksum(sector.sector);
    disc_checksum = *(u32 *)&sector.sector[508];
    dbg_printf("-icside disc checksum: %08X", disc_checksum);
    dbg_printf("-icside calc checksum: %08X", calc_checksum);

    if (calc_checksum != disc_checksum) {
      dbg_memdump(sector.sector, ICSIDE_SECTOR_SIZE);
      break;
    }

    ret = 1;
  } while (0);

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

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

  dbg_printf("icside_readinfo()");
  dbg_level_up();

  assert(part != NULL);

  do {
    icside_pe_t sector;
    partinfo_i_t pinfo;
    char temp_sector[512];
    u_int idx, part_no;

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

    ret = 1;

    pinfo.info.chs_valid = 0;

    /* Scan through all available partitions, detecting filecore,
     * Linux and non-Linux partitions
     */
    for (idx = 0, part_no = 1; idx < ICSIDE_NR_PARTITIONS; idx ++, part_no ++) {
      pinfo.info.chs_valid    = 0;
      pinfo.info.blk_start    = sector.p[idx].start;
      pinfo.data.icside.idx   = idx;

      if (sector.p[idx].size > 0) {
        /* If size > 0, then we have a filecore partition */
        pinfo.info.blk_start    = sector.p[idx].start;
        pinfo.info.blk_end      = sector.p[idx].start + sector.p[idx].size - 1;
        pinfo.info.type         = ptyp_filecore;
        pinfo.info.kern_part_no = part_no;
        pinfo.data.icside.idx   = idx;

        if (!part_add(part, part_no, &pinfo)) {
          ret = 0;
          break;
        }
        /* should we check the filecore boot sector? */
      } else if (sector.p[idx].size < 0) {
        /* Otherwise if size < 0, we have some other partition */
        if (blkio_read(part->blkio, temp_sector, pinfo.info.blk_start, 1) == 1 &&
            memcmp(temp_sector, "LinuxPart", 9) == 0) {

          pinfo.info.chs_valid    = 0;
          pinfo.info.blk_start    = sector.p[idx].start;
          pinfo.info.blk_end      = sector.p[idx].start - sector.p[idx].size - 1;
          pinfo.info.kern_part_no = part_no;
          pinfo.data.icside.idx   = idx;

          switch (temp_sector[9]) {
          case 'N':
            pinfo.info.type = ptyp_linux_native;
            break;
          case 'S':
            pinfo.info.type = ptyp_linux_swap;
            break;
          default:
            break;
          }
          if (!part_add(part, part_no, &pinfo)) {
            ret = 0;
            break;
          }
        } else {
          /* Mark other partitions as unknown */
          pinfo.info.chs_valid    = 0;
          pinfo.info.blk_start    = sector.p[idx].start;
          pinfo.info.blk_end      = (sector.p[idx].start - sector.p[idx].size) - 1;
          pinfo.info.type         = ptyp_unknown;
          pinfo.info.kern_part_no = part_no;
          pinfo.data.icside.idx   = idx;

          if (!part_add(part, part_no, &pinfo)) {
            ret = 0;
            break;
          }
        }
      }
    }
  } while (0);

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

  return ret;
}

/* Prototype: u_int icside_writeinfo(part_t *part)
 * Purpose  : write all partition information back to the drive
 * Params   : part - partitionable device
 * Returns  : FALSE on error
 */
static u_int
icside_writeinfo(part_t *part)
{
  icside_pe_t sector;
  u_int part_no, ret = 0;
  unsigned long csum;

  dbg_printf("icside_writeinfo()");
  dbg_level_up();

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

  memset(sector.sector, 0, sizeof(sector));

  for (part_no = 1; part_no < part->nr_partitions; part_no++)
    if (part->partinfo[part_no]) {
      partinfo_i_t *info = part->partinfo[part_no];
      u_int idx = part_no - 1;

      /*
       * Linux table partitions are just placeholders - ignore them
       */
      switch (info->info.type) {
      case ptyp_none:
        break;

      case ptyp_unknown:
        sector.p[idx].start = info->info.blk_start;
        sector.p[idx].size  = -((info->info.blk_end - info->info.blk_start) + 1);
        break;

      case ptyp_linux_native:
      case ptyp_linux_swap:
        sector.p[idx].start = info->info.blk_start;
        sector.p[idx].size  = -((info->info.blk_end - info->info.blk_start) + 1);
        break;

      case ptyp_filecore:
        sector.p[idx].start = info->info.blk_start;
        sector.p[idx].size  = (info->info.blk_end - info->info.blk_start) + 1;
        break;

      default:
        assert(0);
      }
    }
  /* XXX possible endianness issue */
  csum = icside_checksum(sector.sector);
  memcpy(&sector.sector[508], &csum, sizeof(csum));

#ifdef ICSIDE_DUMP
  dbg_memdump(sector.sector, ICSIDE_SECTOR_SIZE);
#endif
#ifdef ICSIDE_SAVE
  ret = 1;
  if (blkio_write(part->blkio, sector.sector, ICSIDE_PART_SECTOR, 1) != 1)
    ret = 0;
#endif

  for (part_no = 1; part_no < part->nr_partitions; part_no++)
    if (part->partinfo[part_no]) {
      partinfo_i_t *info = part->partinfo[part_no];
      char *id = NULL;

      switch (info->info.type) {
      case ptyp_linux_native:
        id = "LinuxPartN";
        break;

      case ptyp_linux_swap:
        id = "LinuxPartS";
        break;

      default:
        break;
      }

      if (id) {
        memset(sector.sector, 0, ICSIDE_SECTOR_SIZE);
        memcpy(sector.sector, id, strlen(id));

        dbg_printf("write to 0x%lX", info->info.blk_start);
#ifdef ICSIDE_DUMP
        dbg_memdump(sector.sector, ICSIDE_SECTOR_SIZE);
#endif
#ifdef ICSIDE_SAVE
        if (blkio_write(part->blkio, sector.sector, info->info.blk_start, 1) != 1)
          ret = 0;
#endif
      }
    }

#ifndef ICSIDE_SAVE
  set_error("ICS-style partition information cannot be saved with this build");
#endif

  dbg_level_down();
  dbg_printf("ret=%d", ret);

  return ret;
}

/* Prototype: u_int icside_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
icside_allocate(part_t *part, partinfo_t *pnew)
{
  u_int parn;

  dbg_printf("icside_allcate()");

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

  for (parn = 1; parn < part->nr_partitions; parn++)
    if (!part->partinfo[parn])
      break;

  if (parn >= ICSIDE_NR_PARTITIONS) {
    set_error("too many partitions already created");
    parn = PARN_ERROR;
  } else {
    pnew->kern_part_no = parn;
    pnew->type = ptyp_linux_native;
  }

  dbg_printf("ret %d", parn);
  return parn;
}

/* Prototype: u_int icside_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
icside_validate_change(part_t *part, u_int parn, const partinfo_t *pold, const partinfo_t *pnew)
{
  u_int ret = 0;

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

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

  do {
    if (pold->type == ptyp_filecore) {
      set_error("you cannot edit a filecore partition");
      break;
    }

    if (pnew) {
      if (pnew->type == ptyp_filecore) {
        set_error("you cannot create a filecore partition");
        break;
      }

      if (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 icside_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
icside_validate_creation(part_t *part, u_int parn, const partinfo_t *pold, const partinfo_t *pnew)
{
  u_int ret = 0;

  dbg_printf("icside_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) {
      if (pnew->type == ptyp_filecore) {
        set_error("you cannot create a filecore partition");
        break;
      }

      if (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 icside_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
icside_validate_deletion(part_t *part, u_int parn, const partinfo_t *pold)
{
  u_int ret = 0;

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

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

  ret = 1;

  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
icside_validate_partno(part_t *part, u_int parn)
{
  u_int ret = 0;

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

  assert(part != NULL);

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

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

  return ret;
}

/* Prototype: ptyp_t icside_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
icside_nexttype(part_t *part, u_int parn, ptyp_t current, int dir)
{
  ptyp_t types[] = { ptyp_filecore, ptyp_linux_native, ptyp_linux_swap, ptyp_unknown };
  ptyp_t ptype;

  dbg_printf("icside_nexttype(parn=%d, current=0x%X, %s)", parn, current,
  	     dir > 0 ? "next" : "previous");
  dbg_level_up();

  assert(parn < part->nr_partitions);

  if (parn == 1)
    ptype = ptyp_filecore;
  else {
    int i;

    for (i = 0; i < NR(types); i++)
      if (types[i] == current)
        break;
    i += dir;
    if (i < 0)
      i = 0;
    else if (i >= NR(types))
      i = NR(types) - 1;
    ptype = types[i];
  }

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

  return ptype;
}

scheme_t icside_scheme = {
  "ICSIDEFS",
  icside_detect,
  icside_readinfo,
  icside_writeinfo,
  icside_allocate,
  icside_validate_change,
  icside_validate_creation,
  icside_validate_deletion,
  icside_validate_partno,
  icside_nexttype
};