1 /* This file is part of the program psim.
3 Copyright (C) 1994-1997, Andrew Cagney <cagney@highland.com.au>
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.
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.
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.
22 #ifndef _PK_DISKLABEL_C_
23 #define _PK_DISKLABEL_C_
25 #ifndef STATIC_INLINE_PK_DISKLABEL
26 #define STATIC_INLINE_PK_DISKLABEL STATIC_INLINE
29 #include "device_table.h"
41 disk-label - all knowing disk I/O package
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.
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.
56 /* taken from bfd/ppcboot.c by Michael Meissner */
58 /* PPCbug location structure */
59 typedef struct ppcboot_location {
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;
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 */
90 typedef struct _disklabel {
91 device_instance *parent;
92 device_instance *raw_disk;
94 unsigned_word sector_begin;
95 unsigned_word sector_length;
100 sector2uw(unsigned8 s[4])
110 disklabel_delete(device_instance *instance)
112 disklabel *label = device_instance_data(instance);
113 device_instance_delete(label->raw_disk);
119 disklabel_read(device_instance *instance,
123 disklabel *label = device_instance_data(instance);
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)
130 nr_read = device_instance_read(label->raw_disk, buf, len);
132 label->pos += nr_read;
137 disklabel_write(device_instance *instance,
141 disklabel *label = device_instance_data(instance);
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)
148 nr_written = device_instance_write(label->raw_disk, buf, len);
150 label->pos += nr_written;
155 disklabel_seek(device_instance *instance,
156 unsigned_word pos_hi,
157 unsigned_word pos_lo)
159 disklabel *label = device_instance_data(instance);
160 if (pos_lo >= label->sector_length || pos_hi != 0)
167 static const device_instance_callbacks package_disklabel_callbacks = {
174 /* Reconize different types of boot block */
177 block0_is_bpb(const unsigned8 block[])
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)
184 /* must have LE 0xAA55 signature at offset 510 */
185 if (block[511] != 0xAA && block[510] != 0x55)
187 /* valid 16 bit LE bytes per sector - 256, 512, 1024 */
189 || (block[12] != 1 && block[12] != 2 && block[12] != 4))
191 /* nr fats is 1 or 2 */
192 if (block[16] != 1 && block[16] != 2)
198 /* Verify that the device contains an ISO-9660 File system */
201 is_iso9660(device_instance *raw_disk)
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)
207 if (device_instance_read(raw_disk, block, sizeof(block)) != sizeof(block))
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.
224 Return 1..4: valid, value returned is the first active partition
225 Return -1: no active partition */
228 block0_is_fdisk(const unsigned8 block[])
230 const int partition_type_fields[] = { 0, 0x1c2, 0x1d2, 0x1e2, 0x1f2 };
231 const int partition_active_fields[] = { 0, 0x1be, 0x1ce, 0x1de, 0xee };
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)
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;
243 switch (partition_type) {
248 type = "FAT 12 File system";
251 type = "FAT 16 File system";
255 type = "rejected - extended/chained partition not supported";
259 type = "Single program image";
265 type = "ISO 9660 File system";
268 type = "rejected - unknown type";
272 PTRACE(disklabel, ("partition %d of type 0x%02x - %s%s\n",
276 is_active && active != 0 ? " (active)" : ""));
277 if (partition_type != 0 && is_active && active < 0)
284 /* Verify that block0 corresponds to a MAC disk */
287 block0_is_mac_disk(const unsigned8 block[])
289 /* ref PowerPC Microprocessor CHRP bindings 1.2b - page 47 */
290 /* signature - BEx4552 at offset 0 */
291 if (block[0] != 0x45 || block[1] != 0x52)
297 /* Open a logical disk/file */
300 pk_disklabel_create_instance(device_instance *raw_disk,
306 /* parse the arguments */
312 partition = strtoul((char*)args, &filename, 0);
313 if (filename == args)
314 partition = -1; /* not specified */
315 if (*filename == ',')
317 if (*filename == '\0')
318 filename = NULL; /* easier */
321 if (partition == 0) {
322 /* select the raw disk */
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");
336 /* select the active partition */
337 if (block0_is_bpb(boot_block)) {
338 device_error(device_instance_device(raw_disk), "Unimplemented active BPB");
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)",
345 else if (is_iso9660(raw_disk)) {
346 device_error(device_instance_device(raw_disk), "Unimplemented active ISO9660");
348 else if (block0_is_mac_disk(boot_block)) {
349 device_error(device_instance_device(raw_disk), "Unimplemented active MAC DISK");
352 device_error(device_instance_device(raw_disk), "Unreconized bootblock");
356 /* select the specified disk partition */
357 if (block0_is_bpb(boot_block)) {
358 device_error(device_instance_device(raw_disk), "Unimplemented BPB");
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;
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;
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",
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,
384 &package_disklabel_callbacks);
386 else if (block0_is_mac_disk(boot_block)) {
387 device_error(device_instance_device(raw_disk), "Unimplemented MAC DISK");
390 device_error(device_instance_device(raw_disk),
391 "Unreconized bootblock");
400 #endif /* _PK_DISKLABEL_C_ */