OpenCores
URL https://opencores.org/ocsvn/openrisc_me/openrisc_me/trunk

Subversion Repositories openrisc_me

[/] [openrisc/] [trunk/] [gnu-src/] [gdb-7.1/] [sim/] [ppc/] [pk_disklabel.c] - Blame information for rev 252

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 227 jeremybenn
/*  This file is part of the program psim.
2
 
3
    Copyright (C) 1994-1997, Andrew Cagney <cagney@highland.com.au>
4
 
5
    This program is free software; you can redistribute it and/or modify
6
    it under the terms of the GNU General Public License as published by
7
    the Free Software Foundation; either version 2 of the License, or
8
    (at your option) any later version.
9
 
10
    This program is distributed in the hope that it will be useful,
11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
    GNU General Public License for more details.
14
 
15
    You should have received a copy of the GNU General Public License
16
    along with this program; if not, write to the Free Software
17
    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18
 
19
    */
20
 
21
 
22
#ifndef _PK_DISKLABEL_C_
23
#define _PK_DISKLABEL_C_
24
 
25
#ifndef STATIC_INLINE_PK_DISKLABEL
26
#define STATIC_INLINE_PK_DISKLABEL STATIC_INLINE
27
#endif
28
 
29
#include "device_table.h"
30
 
31
#include "pk.h"
32
 
33
#ifdef HAVE_STDLIB_H
34
#include <stdlib.h>
35
#endif
36
 
37
 
38
 
39
/* PACKAGE
40
 
41
   disk-label - all knowing disk I/O package
42
 
43
   DESCRIPTION
44
 
45
   The disk-label package provides a generic interface to disk
46
   devices.  It uses the arguments specified when an instance is being
47
   created to determine if the raw disk, a partition, or a file within
48
   a partition should be opened.
49
 
50
   An instance create call to disk-label could result, for instance,
51
   in the opening of a DOS file system contained within a dos
52
   partition contained within a physical disk.
53
 
54
   */
55
 
56
/* taken from bfd/ppcboot.c by Michael Meissner */
57
 
58
/* PPCbug location structure */
59
typedef struct ppcboot_location {
60
  unsigned8 ind;
61
  unsigned8 head;
62
  unsigned8 sector;
63
  unsigned8 cylinder;
64
} ppcboot_location_t;
65
 
66
/* PPCbug partition table layout */
67
typedef struct ppcboot_partition {
68
  ppcboot_location_t partition_begin;   /* partition begin */
69
  ppcboot_location_t partition_end;     /* partition end */
70
  unsigned8 sector_begin[4];            /* 32-bit start RBA (zero-based), little endian */
71
  unsigned8 sector_length[4];           /* 32-bit RBA count (one-based), little endian */
72
} ppcboot_partition_t;
73
 
74
#if 0
75
/* PPCbug boot layout.  */
76
typedef struct ppcboot_hdr {
77
  unsigned8             pc_compatibility[446];  /* x86 instruction field */
78
  ppcboot_partition_t   partition[4];           /* partition information */
79
  unsigned8             signature[2];           /* 0x55 and 0xaa */
80
  unsigned8             entry_offset[4];        /* entry point offset, little endian */
81
  unsigned8             length[4];              /* load image length, little endian */
82
  unsigned8             flags;                  /* flag field */
83
  unsigned8             os_id;                  /* OS_ID */
84
  char                  partition_name[32];     /* partition name */
85
  unsigned8             reserved1[470];         /* reserved */
86
} ppcboot_hdr_t;
87
#endif
88
 
89
 
90
typedef struct _disklabel {
91
  device_instance *parent;
92
  device_instance *raw_disk;
93
  unsigned_word pos;
94
  unsigned_word sector_begin;
95
  unsigned_word sector_length;
96
} disklabel;
97
 
98
 
99
static unsigned_word
100
sector2uw(unsigned8 s[4])
101
{
102
  return ((s[3] << 24)
103
          + (s[2] << 16)
104
          + (s[1] << 8)
105
          + (s[0] << 0));
106
}
107
 
108
 
109
static void
110
disklabel_delete(device_instance *instance)
111
{
112
  disklabel *label = device_instance_data(instance);
113
  device_instance_delete(label->raw_disk);
114
  zfree(label);
115
}
116
 
117
 
118
static int
119
disklabel_read(device_instance *instance,
120
               void *buf,
121
               unsigned_word len)
122
{
123
  disklabel *label = device_instance_data(instance);
124
  int nr_read;
125
  if (label->pos + len > label->sector_length)
126
    len = label->sector_length - label->pos;
127
  if (device_instance_seek(label->raw_disk, 0,
128
                           label->sector_begin + label->pos) < 0)
129
    return -1;
130
  nr_read = device_instance_read(label->raw_disk, buf, len);
131
  if (nr_read > 0)
132
    label->pos += nr_read;
133
  return nr_read;
134
}
135
 
136
static int
137
disklabel_write(device_instance *instance,
138
                const void *buf,
139
                unsigned_word len)
140
{
141
  disklabel *label = device_instance_data(instance);
142
  int nr_written;
143
  if (label->pos + len > label->sector_length)
144
    len = label->sector_length - label->pos;
145
  if (device_instance_seek(label->raw_disk, 0,
146
                           label->sector_begin + label->pos) < 0)
147
    return -1;
148
  nr_written = device_instance_write(label->raw_disk, buf, len);
149
  if (nr_written > 0)
150
    label->pos += nr_written;
151
  return nr_written;
152
}
153
 
154
static int
155
disklabel_seek(device_instance *instance,
156
               unsigned_word pos_hi,
157
               unsigned_word pos_lo)
158
{
159
  disklabel *label = device_instance_data(instance);
160
  if (pos_lo >= label->sector_length || pos_hi != 0)
161
    return -1;
162
  label->pos = pos_lo;
163
  return 0;
164
}
165
 
166
 
167
static const device_instance_callbacks package_disklabel_callbacks = {
168
  disklabel_delete,
169
  disklabel_read,
170
  disklabel_write,
171
  disklabel_seek,
172
};
173
 
174
/* Reconize different types of boot block */
175
 
176
static int
177
block0_is_bpb(const unsigned8 block[])
178
{
179
  const char ebdic_ibma[] = { 0xc9, 0xc2, 0xd4, 0xc1 };
180
  /* ref PowerPC Microprocessor CHRP bindings 1.2b - page 47 */
181
  /* can't start with IBMA */
182
  if (memcmp(block, ebdic_ibma, sizeof(ebdic_ibma)) == 0)
183
    return 0;
184
  /* must have LE 0xAA55 signature at offset 510 */
185
  if (block[511] != 0xAA && block[510] != 0x55)
186
    return 0;
187
  /* valid 16 bit LE bytes per sector - 256, 512, 1024 */
188
  if (block[11] != 0
189
      || (block[12] != 1 && block[12] != 2 && block[12] != 4))
190
    return 0;
191
  /* nr fats is 1 or 2 */
192
  if (block[16] != 1 && block[16] != 2)
193
    return 0;
194
  return 1;
195
}
196
 
197
 
198
/* Verify that the device contains an ISO-9660 File system */
199
 
200
static int
201
is_iso9660(device_instance *raw_disk)
202
{
203
  /* ref PowerPC Microprocessor CHRP bindings 1.2b - page 47 */
204
  unsigned8 block[512];
205
  if (device_instance_seek(raw_disk, 0, 512 * 64) < 0)
206
    return 0;
207
  if (device_instance_read(raw_disk, block, sizeof(block)) != sizeof(block))
208
    return 0;
209
  if (block[0] == 0x01
210
      && block[1] == 'C'
211
      && block[2] == 'D'
212
      && block[3] == '0'
213
      && block[4] == '0'
214
      && block[5] == '1')
215
    return 1;
216
  return 0;
217
}
218
 
219
 
220
/* Verify that the disk block contains a valid DOS partition table.
221
   While we're at it have a look around for active partitions etc.
222
 
223
   Return 0: invalid
224
   Return 1..4: valid, value returned is the first active partition
225
   Return -1: no active partition */
226
 
227
static int
228
block0_is_fdisk(const unsigned8 block[])
229
{
230
  const int partition_type_fields[] = { 0, 0x1c2, 0x1d2, 0x1e2, 0x1f2 };
231
  const int partition_active_fields[] = { 0, 0x1be, 0x1ce, 0x1de, 0xee };
232
  int partition;
233
  int active = -1;
234
  /* ref PowerPC Microprocessor CHRP bindings 1.2b - page 47 */
235
  /* must have LE 0xAA55 signature at offset 510 */
236
  if (block[511/*0x1ff*/] != 0xAA && block[510/*0x1fe*/] != 0x55)
237
    return 0;
238
  /* must contain valid partition types */
239
  for (partition = 1; partition <= 4 && active != 0; partition++) {
240
    int partition_type = block[partition_type_fields[partition]];
241
    int is_active = block[partition_active_fields[partition]] == 0x80;
242
    const char *type;
243
    switch (partition_type) {
244
    case 0x00:
245
      type = "UNUSED";
246
      break;
247
    case 0x01:
248
      type = "FAT 12 File system";
249
      break;
250
    case 0x04:
251
      type = "FAT 16 File system";
252
      break;
253
    case 0x05:
254
    case 0x06:
255
      type = "rejected - extended/chained partition not supported";
256
      active = 0;
257
      break;
258
    case 0x41:
259
      type = "Single program image";
260
      break;
261
    case 0x82:
262
      type = "Solaris?";
263
      break;
264
    case 0x96:
265
      type = "ISO 9660 File system";
266
      break;
267
    default:
268
      type = "rejected - unknown type";
269
      active = 0;
270
      break;
271
    }
272
    PTRACE(disklabel, ("partition %d of type 0x%02x - %s%s\n",
273
                       partition,
274
                       partition_type,
275
                       type,
276
                       is_active && active != 0 ? " (active)" : ""));
277
    if (partition_type != 0 && is_active && active < 0)
278
      active = partition;
279
  }
280
  return active;
281
}
282
 
283
 
284
/* Verify that block0 corresponds to a MAC disk */
285
 
286
static int
287
block0_is_mac_disk(const unsigned8 block[])
288
{
289
  /* ref PowerPC Microprocessor CHRP bindings 1.2b - page 47 */
290
  /* signature - BEx4552 at offset 0 */
291
  if (block[0] != 0x45 || block[1] != 0x52)
292
    return 0;
293
  return 1;
294
}
295
 
296
 
297
/* Open a logical disk/file */
298
 
299
device_instance *
300
pk_disklabel_create_instance(device_instance *raw_disk,
301
                             const char *args)
302
{
303
  int partition;
304
  char *filename;
305
 
306
  /* parse the arguments */
307
  if (args == NULL) {
308
    partition = 0;
309
    filename = NULL;
310
  }
311
  else {
312
    partition = strtoul((char*)args, &filename, 0);
313
    if (filename == args)
314
      partition = -1; /* not specified */
315
    if (*filename == ',')
316
      filename++;
317
    if (*filename == '\0')
318
      filename = NULL; /* easier */
319
  }
320
 
321
  if (partition == 0) {
322
    /* select the raw disk */
323
    return raw_disk;
324
  }
325
  else {
326
    unsigned8 boot_block[512];
327
    /* get the boot block for examination */
328
    if (device_instance_seek(raw_disk, 0, 0) < 0)
329
      device_error(device_instance_device(raw_disk),
330
                   "Problem seeking on raw disk");
331
    if (device_instance_read(raw_disk, &boot_block, sizeof(boot_block))
332
        != sizeof(boot_block))
333
      device_error(device_instance_device(raw_disk), "Problem reading boot block");
334
 
335
    if (partition < 0) {
336
      /* select the active partition */
337
      if (block0_is_bpb(boot_block)) {
338
        device_error(device_instance_device(raw_disk), "Unimplemented active BPB");
339
      }
340
      else if (block0_is_fdisk(boot_block)) {
341
        int active = block0_is_fdisk(boot_block);
342
        device_error(device_instance_device(raw_disk), "Unimplemented active FDISK (%d)",
343
                     active);
344
      }
345
      else if (is_iso9660(raw_disk)) {
346
        device_error(device_instance_device(raw_disk), "Unimplemented active ISO9660");
347
      }
348
      else if (block0_is_mac_disk(boot_block)) {
349
        device_error(device_instance_device(raw_disk), "Unimplemented active MAC DISK");
350
      }
351
      else {
352
        device_error(device_instance_device(raw_disk), "Unreconized bootblock");
353
      }
354
    }
355
    else {
356
      /* select the specified disk partition */
357
      if (block0_is_bpb(boot_block)) {
358
        device_error(device_instance_device(raw_disk), "Unimplemented BPB");
359
      }
360
      else if (block0_is_fdisk(boot_block)) {
361
        /* return an instance */
362
        ppcboot_partition_t *partition_table = (ppcboot_partition_t*) &boot_block[446];
363
        ppcboot_partition_t *partition_entry;
364
        disklabel *label;
365
        if (partition > 4)
366
          device_error(device_instance_device(raw_disk),
367
                       "Only FDISK partitions 1..4 supported");
368
        partition_entry = &partition_table[partition - 1];
369
        label = ZALLOC(disklabel);
370
        label->raw_disk = raw_disk;
371
        label->pos = 0;
372
        label->sector_begin = 512 * sector2uw(partition_entry->sector_begin);
373
        label->sector_length = 512 * sector2uw(partition_entry->sector_length);
374
        PTRACE(disklabel, ("partition %ld, sector-begin %ld, length %ld\n",
375
                           (long)partition,
376
                           (long)label->sector_begin,
377
                           (long)label->sector_length));
378
        if (filename != NULL)
379
          device_error(device_instance_device(raw_disk),
380
                       "FDISK file names not yet supported");
381
        return device_create_instance_from(NULL, raw_disk,
382
                                           label,
383
                                           NULL, args,
384
                                           &package_disklabel_callbacks);
385
      }
386
      else if (block0_is_mac_disk(boot_block)) {
387
        device_error(device_instance_device(raw_disk), "Unimplemented MAC DISK");
388
      }
389
      else {
390
        device_error(device_instance_device(raw_disk),
391
                     "Unreconized bootblock");
392
      }
393
    }
394
  }
395
 
396
  return NULL;
397
}
398
 
399
 
400
#endif /* _PK_DISKLABEL_C_ */
401
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.