This commit was generated by cvs2svn to track changes on a CVS vendor
[external/binutils.git] / sim / ppc / pk_disklabel.c
1 /*  This file is part of the program psim.
2     
3     Copyright (C) 1994-1996, 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 block_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 /* is_iso9660() later ... */
198
199 static int
200 block_is_fdisk(const unsigned8 block[])
201 {
202   const int partition_type_fields[] = { 0x1c2, 0x1d2, 0x1e2, 0x1f2, 0 };
203   int partition;
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)
207     return 0;
208   /* must contain valid partition types */
209   for (partition = 0;
210        partition_type_fields[partition] != 0;
211        partition++) {
212     switch (block[partition_type_fields[partition]]) {
213     case 4: case 0x41: case 0x96:
214       break;
215     case 5: case 6:
216       PTRACE(disklabel, ("extended partition types not supported\n"));
217       return 0;
218     case 0x82:
219       PTRACE(disklabel, ("warning - partition %d of type 0x82 - Solaris?\n", partition));
220       break;
221     case 0x1:
222       PTRACE(disklabel, ("warning - partition %d of type 1 - DOS12\n", partition));
223       break;
224     case 0:
225       break;
226     default:
227       PTRACE(disklabel, ("rejecting partition %d of type 0x%x\n", partition,
228                          block[partition_type_fields[partition]]));
229       return 0;
230     }
231   }
232   return 1;
233 }
234
235 static int
236 block_is_mac_disk(const unsigned8 block[])
237 {
238   /* ref PowerPC Microprocessor CHRP bindings 1.2b - page 47 */
239   /* signature - BEx4552 at offset 0 */
240   if (block[0] != 0x45 || block[1] != 0x52)
241     return 0;
242   return 1;
243 }
244
245
246 /* Open a logical disk/file */
247
248 device_instance *
249 pk_disklabel_create_instance(device_instance *raw_disk,
250                              const char *args)
251 {
252   int partition;
253   char *filename;
254   unsigned8 boot_block[512];
255
256   /* parse the arguments */
257   if (args == NULL) {
258     partition = 0;
259     filename = NULL;
260   }
261   else {
262     partition = strtoul((char*)args, &filename, 0);
263     if (filename == args)
264       partition = -1; /* not specified */
265     if (*filename == ',')
266       filename++;
267     if (*filename == '\0')
268       filename = NULL; /* easier */
269   }
270
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");
274
275   if (partition < 0) {
276     /* select the active partition */
277     if (device_instance_read(raw_disk, &boot_block, sizeof(boot_block))
278         != sizeof(boot_block))
279       return raw_disk;
280     device_error(device_instance_device(raw_disk),
281                  "selection of the active partition unimplemented\n");
282     return (device_instance *)0;        /* silence warnings */
283   }
284   else if (partition == 0) {
285     /* select the raw disk */
286     return raw_disk;
287   }
288   else {
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))
293       return raw_disk;
294     if (block_is_bpb(boot_block)) {
295       device_error(device_instance_device(raw_disk), "Unimplemented\n");
296     }
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;
301       disklabel *label;
302       if (partition > 3)
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;
308       label->pos = 0;
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,
317                                          label,
318                                          NULL, args,
319                                          &package_disklabel_callbacks);
320     }
321     else if (block_is_mac_disk(boot_block)) {
322       device_error(device_instance_device(raw_disk), "Unimplemented\n");
323     }
324     else {
325       device_error(device_instance_device(raw_disk),
326                    "Unreconized bootblock\n");
327     }
328     return raw_disk;
329   }
330
331 }
332
333
334 #endif /* _PK_DISKLABEL_C_ */
335