1 /* This file is part of the program psim.
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 block_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)
197 /* is_iso9660() later ... */
200 block_is_fdisk(const unsigned8 block[])
202 const int partition_type_fields[] = { 0x1c2, 0x1d2, 0x1e2, 0x1f2, 0 };
204 /* ref PowerPC Microprocessor CHRP bindings 1.2b - page 47 */
205 /* must have LE 0xAA55 signature at offset 510 */
206 if (block[511] != 0xAA && block[510] != 0x55)
208 /* must contain valid partition types */
210 partition_type_fields[partition] != 0;
212 switch (block[partition_type_fields[partition]]) {
213 case 4: case 0x41: case 0x96:
216 PTRACE(disklabel, ("extended partition types not supported\n"));
219 PTRACE(disklabel, ("warning - partition %d of type 0x82 - Solaris?\n", partition));
222 PTRACE(disklabel, ("warning - partition %d of type 1 - DOS12\n", partition));
227 PTRACE(disklabel, ("rejecting partition %d of type 0x%x\n", partition,
228 block[partition_type_fields[partition]]));
236 block_is_mac_disk(const unsigned8 block[])
238 /* ref PowerPC Microprocessor CHRP bindings 1.2b - page 47 */
239 /* signature - BEx4552 at offset 0 */
240 if (block[0] != 0x45 || block[1] != 0x52)
246 /* Open a logical disk/file */
249 pk_disklabel_create_instance(device_instance *raw_disk,
254 unsigned8 boot_block[512];
256 /* parse the arguments */
262 partition = strtoul((char*)args, &filename, 0);
263 if (filename == args)
264 partition = -1; /* not specified */
265 if (*filename == ',')
267 if (*filename == '\0')
268 filename = NULL; /* easier */
271 if (device_instance_seek(raw_disk, 0, 0) < 0)
272 device_error(device_instance_device(raw_disk),
273 "Problem seeking on raw disk\n");
276 /* select the active partition */
277 if (device_instance_read(raw_disk, &boot_block, sizeof(boot_block))
278 != sizeof(boot_block))
280 device_error(device_instance_device(raw_disk),
281 "selection of the active partition unimplemented\n");
282 return (device_instance *)0; /* silence warnings */
284 else if (partition == 0) {
285 /* select the raw disk */
289 /* select the specified disk partition */
290 /* read in the boot record */
291 if (device_instance_read(raw_disk, &boot_block, sizeof(boot_block))
292 != sizeof(boot_block))
294 if (block_is_bpb(boot_block)) {
295 device_error(device_instance_device(raw_disk), "Unimplemented\n");
297 else if (block_is_fdisk(boot_block)) {
298 /* return an instance */
299 ppcboot_partition_t *partition_table = (ppcboot_partition_t*) &boot_block[446];
300 ppcboot_partition_t *partition_entry;
303 device_error(device_instance_device(raw_disk),
304 "Invalid fdisk partition number %d\n", partition);
305 partition_entry = &partition_table[partition-1];
306 label = ZALLOC(disklabel);
307 label->raw_disk = raw_disk;
309 label->sector_begin = 512 * sector2uw(partition_entry->sector_begin);
310 label->sector_length = 512 * sector2uw(partition_entry->sector_length);
311 PTRACE(disklabel, ("partition %ld, sector-begin %ld, length %ld\n",
312 (long)partition, (long)label->sector_begin, (long)label->sector_length));
313 if (filename != NULL)
314 device_error(device_instance_device(raw_disk),
315 "File names not yet supported\n");
316 return device_create_instance_from(NULL, raw_disk,
319 &package_disklabel_callbacks);
321 else if (block_is_mac_disk(boot_block)) {
322 device_error(device_instance_device(raw_disk), "Unimplemented\n");
325 device_error(device_instance_device(raw_disk),
326 "Unreconized bootblock\n");
334 #endif /* _PK_DISKLABEL_C_ */