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 3 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, see <http://www.gnu.org/licenses/>.
21 #ifndef _PK_DISKLABEL_C_
22 #define _PK_DISKLABEL_C_
24 #ifndef STATIC_INLINE_PK_DISKLABEL
25 #define STATIC_INLINE_PK_DISKLABEL STATIC_INLINE
28 #include "device_table.h"
40 disk-label - all knowing disk I/O package
44 The disk-label package provides a generic interface to disk
45 devices. It uses the arguments specified when an instance is being
46 created to determine if the raw disk, a partition, or a file within
47 a partition should be opened.
49 An instance create call to disk-label could result, for instance,
50 in the opening of a DOS file system contained within a dos
51 partition contained within a physical disk.
55 /* taken from bfd/ppcboot.c by Michael Meissner */
57 /* PPCbug location structure */
58 typedef struct ppcboot_location {
65 /* PPCbug partition table layout */
66 typedef struct ppcboot_partition {
67 ppcboot_location_t partition_begin; /* partition begin */
68 ppcboot_location_t partition_end; /* partition end */
69 unsigned8 sector_begin[4]; /* 32-bit start RBA (zero-based), little endian */
70 unsigned8 sector_length[4]; /* 32-bit RBA count (one-based), little endian */
71 } ppcboot_partition_t;
74 /* PPCbug boot layout. */
75 typedef struct ppcboot_hdr {
76 unsigned8 pc_compatibility[446]; /* x86 instruction field */
77 ppcboot_partition_t partition[4]; /* partition information */
78 unsigned8 signature[2]; /* 0x55 and 0xaa */
79 unsigned8 entry_offset[4]; /* entry point offset, little endian */
80 unsigned8 length[4]; /* load image length, little endian */
81 unsigned8 flags; /* flag field */
82 unsigned8 os_id; /* OS_ID */
83 char partition_name[32]; /* partition name */
84 unsigned8 reserved1[470]; /* reserved */
89 typedef struct _disklabel {
90 device_instance *parent;
91 device_instance *raw_disk;
93 unsigned_word sector_begin;
94 unsigned_word sector_length;
99 sector2uw(unsigned8 s[4])
109 disklabel_delete(device_instance *instance)
111 disklabel *label = device_instance_data(instance);
112 device_instance_delete(label->raw_disk);
118 disklabel_read(device_instance *instance,
122 disklabel *label = device_instance_data(instance);
124 if (label->pos + len > label->sector_length)
125 len = label->sector_length - label->pos;
126 if (device_instance_seek(label->raw_disk, 0,
127 label->sector_begin + label->pos) < 0)
129 nr_read = device_instance_read(label->raw_disk, buf, len);
131 label->pos += nr_read;
136 disklabel_write(device_instance *instance,
140 disklabel *label = device_instance_data(instance);
142 if (label->pos + len > label->sector_length)
143 len = label->sector_length - label->pos;
144 if (device_instance_seek(label->raw_disk, 0,
145 label->sector_begin + label->pos) < 0)
147 nr_written = device_instance_write(label->raw_disk, buf, len);
149 label->pos += nr_written;
154 disklabel_seek(device_instance *instance,
155 unsigned_word pos_hi,
156 unsigned_word pos_lo)
158 disklabel *label = device_instance_data(instance);
159 if (pos_lo >= label->sector_length || pos_hi != 0)
166 static const device_instance_callbacks package_disklabel_callbacks = {
173 /* Reconize different types of boot block */
176 block0_is_bpb(const unsigned8 block[])
178 const char ebdic_ibma[] = { 0xc9, 0xc2, 0xd4, 0xc1 };
179 /* ref PowerPC Microprocessor CHRP bindings 1.2b - page 47 */
180 /* can't start with IBMA */
181 if (memcmp(block, ebdic_ibma, sizeof(ebdic_ibma)) == 0)
183 /* must have LE 0xAA55 signature at offset 510 */
184 if (block[511] != 0xAA && block[510] != 0x55)
186 /* valid 16 bit LE bytes per sector - 256, 512, 1024 */
188 || (block[12] != 1 && block[12] != 2 && block[12] != 4))
190 /* nr fats is 1 or 2 */
191 if (block[16] != 1 && block[16] != 2)
197 /* Verify that the device contains an ISO-9660 File system */
200 is_iso9660(device_instance *raw_disk)
202 /* ref PowerPC Microprocessor CHRP bindings 1.2b - page 47 */
203 unsigned8 block[512];
204 if (device_instance_seek(raw_disk, 0, 512 * 64) < 0)
206 if (device_instance_read(raw_disk, block, sizeof(block)) != sizeof(block))
219 /* Verify that the disk block contains a valid DOS partition table.
220 While we're at it have a look around for active partitions etc.
223 Return 1..4: valid, value returned is the first active partition
224 Return -1: no active partition */
227 block0_is_fdisk(const unsigned8 block[])
229 const int partition_type_fields[] = { 0, 0x1c2, 0x1d2, 0x1e2, 0x1f2 };
230 const int partition_active_fields[] = { 0, 0x1be, 0x1ce, 0x1de, 0xee };
233 /* ref PowerPC Microprocessor CHRP bindings 1.2b - page 47 */
234 /* must have LE 0xAA55 signature at offset 510 */
235 if (block[511/*0x1ff*/] != 0xAA && block[510/*0x1fe*/] != 0x55)
237 /* must contain valid partition types */
238 for (partition = 1; partition <= 4 && active != 0; partition++) {
239 int partition_type = block[partition_type_fields[partition]];
240 int is_active = block[partition_active_fields[partition]] == 0x80;
242 switch (partition_type) {
247 type = "FAT 12 File system";
250 type = "FAT 16 File system";
254 type = "rejected - extended/chained partition not supported";
258 type = "Single program image";
264 type = "ISO 9660 File system";
267 type = "rejected - unknown type";
271 PTRACE(disklabel, ("partition %d of type 0x%02x - %s%s\n",
275 is_active && active != 0 ? " (active)" : ""));
276 if (partition_type != 0 && is_active && active < 0)
283 /* Verify that block0 corresponds to a MAC disk */
286 block0_is_mac_disk(const unsigned8 block[])
288 /* ref PowerPC Microprocessor CHRP bindings 1.2b - page 47 */
289 /* signature - BEx4552 at offset 0 */
290 if (block[0] != 0x45 || block[1] != 0x52)
296 /* Open a logical disk/file */
299 pk_disklabel_create_instance(device_instance *raw_disk,
305 /* parse the arguments */
311 partition = strtoul((char*)args, &filename, 0);
312 if (filename == args)
313 partition = -1; /* not specified */
314 if (*filename == ',')
316 if (*filename == '\0')
317 filename = NULL; /* easier */
320 if (partition == 0) {
321 /* select the raw disk */
325 unsigned8 boot_block[512];
326 /* get the boot block for examination */
327 if (device_instance_seek(raw_disk, 0, 0) < 0)
328 device_error(device_instance_device(raw_disk),
329 "Problem seeking on raw disk");
330 if (device_instance_read(raw_disk, &boot_block, sizeof(boot_block))
331 != sizeof(boot_block))
332 device_error(device_instance_device(raw_disk), "Problem reading boot block");
335 /* select the active partition */
336 if (block0_is_bpb(boot_block)) {
337 device_error(device_instance_device(raw_disk), "Unimplemented active BPB");
339 else if (block0_is_fdisk(boot_block)) {
340 int active = block0_is_fdisk(boot_block);
341 device_error(device_instance_device(raw_disk), "Unimplemented active FDISK (%d)",
344 else if (is_iso9660(raw_disk)) {
345 device_error(device_instance_device(raw_disk), "Unimplemented active ISO9660");
347 else if (block0_is_mac_disk(boot_block)) {
348 device_error(device_instance_device(raw_disk), "Unimplemented active MAC DISK");
351 device_error(device_instance_device(raw_disk), "Unreconized bootblock");
355 /* select the specified disk partition */
356 if (block0_is_bpb(boot_block)) {
357 device_error(device_instance_device(raw_disk), "Unimplemented BPB");
359 else if (block0_is_fdisk(boot_block)) {
360 /* return an instance */
361 ppcboot_partition_t *partition_table = (ppcboot_partition_t*) &boot_block[446];
362 ppcboot_partition_t *partition_entry;
365 device_error(device_instance_device(raw_disk),
366 "Only FDISK partitions 1..4 supported");
367 partition_entry = &partition_table[partition - 1];
368 label = ZALLOC(disklabel);
369 label->raw_disk = raw_disk;
371 label->sector_begin = 512 * sector2uw(partition_entry->sector_begin);
372 label->sector_length = 512 * sector2uw(partition_entry->sector_length);
373 PTRACE(disklabel, ("partition %ld, sector-begin %ld, length %ld\n",
375 (long)label->sector_begin,
376 (long)label->sector_length));
377 if (filename != NULL)
378 device_error(device_instance_device(raw_disk),
379 "FDISK file names not yet supported");
380 return device_create_instance_from(NULL, raw_disk,
383 &package_disklabel_callbacks);
385 else if (block0_is_mac_disk(boot_block)) {
386 device_error(device_instance_device(raw_disk), "Unimplemented MAC DISK");
389 device_error(device_instance_device(raw_disk),
390 "Unreconized bootblock");
399 #endif /* _PK_DISKLABEL_C_ */