]> git.ozlabs.org Git - yaboot.git/blob - second/partition.c
Add/Correct libgcc function prototypes
[yaboot.git] / second / partition.c
1 /*
2  *  partition.c - partition table support
3  *
4  *  Copyright (C) 2004 Sven Luther
5  *
6  *  Copyright (C) 2001, 2002 Ethan Benson
7  *
8  *  Copyright (C) 1999 Benjamin Herrenschmidt
9  *
10  *  This program is free software; you can redistribute it and/or modify
11  *  it under the terms of the GNU General Public License as published by
12  *  the Free Software Foundation; either version 2 of the License, or
13  *  (at your option) any later version.
14  *
15  *  This program is distributed in the hope that it will be useful,
16  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  *  GNU General Public License for more details.
19  *
20  *  You should have received a copy of the GNU General Public License
21  *  along with this program; if not, write to the Free Software
22  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
23  */
24
25 /*
26  *  Todo: Add disklabel (or let OF do it ?). Eventually think about
27  *        fixing CDROM handling by directly using the ATAPI layer.
28  */
29
30 #include "ctype.h"
31 #include "types.h"
32 #include "stddef.h"
33 #include "stdlib.h"
34 #include "mac-part.h"
35 #include "fdisk-part.h"
36 #include "amiga-part.h"
37 #include "partition.h"
38 #include "prom.h"
39 #include "string.h"
40 #include "linux/iso_fs.h"
41 #include "debug.h"
42 #include "errors.h"
43 #include "bootinfo.h"
44 #include "byteorder.h"
45
46 /* We currently don't check the partition type, some users
47  * are putting crap there and still expect it to work...
48  */
49 #undef CHECK_FOR_VALID_MAC_PARTITION_TYPE
50
51 #ifdef CHECK_FOR_VALID_MAC_PARTITION_TYPE
52 static const char *valid_mac_partition_types[] = {
53      "apple_unix_svr2",
54      "linux",
55      "apple_hfs",
56      "apple_boot",
57      "apple_bootstrap",
58      NULL
59 };
60 #endif
61
62
63 #define MAX_BLOCK_SIZE  2048
64 static unsigned char block_buffer[MAX_BLOCK_SIZE];
65
66 static void
67 add_new_partition(struct partition_t**  list, int part_number, const char *part_type,
68                   const char *part_name, unsigned long part_start, unsigned long part_size,
69                   unsigned short part_blocksize, int sys_ind)
70 {
71      struct partition_t*        part;
72      part = (struct partition_t*)malloc(sizeof(struct partition_t));
73
74      part->part_number = part_number;
75      strncpy(part->part_type, part_type, MAX_PART_NAME);
76      strncpy(part->part_name, part_name, MAX_PART_NAME);
77      part->part_start = part_start;
78      part->part_size = part_size;
79      part->blocksize = part_blocksize;
80      part->sys_ind = sys_ind;
81
82      /* Tack this entry onto the list */
83      part->next = *list;
84      *list = part;
85 }
86
87 /* Note, we rely on partitions being dev-block-size aligned,
88  * I have to check if it's true. If it's not, then things will get
89  * a bit more complicated
90  */
91 static void
92 partition_mac_lookup( const char *dev_name, prom_handle disk,
93                       unsigned int prom_blksize, struct partition_t** list )
94 {
95      int block, map_size;
96
97      /* block_buffer contains block 0 from the partitions_lookup() stage */
98      struct mac_partition* part = (struct mac_partition *)block_buffer;
99      unsigned short ptable_block_size =
100           ((struct mac_driver_desc *)block_buffer)->block_size;
101
102      map_size = 1;
103      for (block=1; block < map_size + 1; block++)
104      {
105 #ifdef CHECK_FOR_VALID_MAC_PARTITION_TYPE
106           int valid = 0;
107           const char *ptype;
108 #endif
109           if (prom_readblocks(disk, block, 1, block_buffer) != 1) {
110                prom_printf("Can't read partition %d\n", block);
111                break;
112           }
113           if (part->signature != MAC_PARTITION_MAGIC) {
114 #if 0
115                prom_printf("Wrong partition %d signature\n", block);
116 #endif
117                break;
118           }
119           if (block == 1)
120                map_size = part->map_count;
121
122 #ifdef CHECK_FOR_VALID_MAC_PARTITION_TYPE
123           /* We don't bother looking at swap partitions of any type,
124            * and the rest are the ones we know about */
125           for (ptype = valid_mac_partition_types; ptype; ptype++)
126                if (!strcmp (part->type, ptype))
127                {
128                     valid = 1;
129                     break;
130                }
131 #if DEBUG
132           if (!valid)
133                prom_printf( "MAC: Unsupported partition #%d; type=%s\n",
134                             block, part->type );
135 #endif
136 #endif
137
138
139 #ifdef CHECK_FOR_VALID_MAC_PARTITION_TYPE
140           if (valid)
141 #endif
142                /* We use the partition block size from the partition table.
143                 * The filesystem implmentations are responsible for mapping
144                 * to their own fs blocksize */
145                add_new_partition(
146                     list, /* partition list */
147                     block, /* partition number */
148                     part->type, /* type */
149                     part->name, /* name */
150                     part->start_block + part->data_start, /* start */
151                     part->data_count, /* size */
152                     ptable_block_size,
153                     0);
154      }
155 }
156
157 /*
158  * Same function as partition_mac_lookup(), except for fdisk
159  * partitioned disks.
160  */
161 static void
162 partition_fdisk_lookup( const char *dev_name, prom_handle disk,
163                         unsigned int prom_blksize, struct partition_t** list )
164 {
165      int partition;
166
167      /* fdisk partition tables start at offset 0x1be
168       * from byte 0 of the boot drive.
169       */
170      struct fdisk_partition* part =
171           (struct fdisk_partition *) (block_buffer + 0x1be);
172
173      for (partition=1; partition <= 4 ;partition++, part++) {
174           if (part->sys_ind == LINUX_NATIVE || part->sys_ind == LINUX_RAID) {
175                add_new_partition(  list,
176                                    partition,
177                                    "Linux", /* type */
178                                    '\0', /* name */
179                                    le32_to_cpu(*(unsigned int *)part->start4),
180                                    le32_to_cpu(*(unsigned int *)part->size4),
181                                    512 /*blksize*/,
182                                    part->sys_ind /* partition type */ );
183           }
184      }
185 }
186
187 /* I don't know if it's possible to handle multisession and other multitrack
188  * stuffs with the current OF disklabel package. This can still be implemented
189  * with direct calls to atapi stuffs.
190  * Currently, we enter this code for any device of block size 0x2048 who lacks
191  * a MacOS partition map signature.
192  */
193 static int
194 identify_iso_fs(ihandle device, unsigned int *iso_root_block)
195 {
196      int block;
197
198      for (block = 16; block < 100; block++) {
199           struct iso_volume_descriptor  * vdp;
200
201           if (prom_readblocks(device, block, 1, block_buffer) != 1) {
202                prom_printf("Can't read volume desc block %d\n", block);
203                break;
204           }
205
206           vdp = (struct iso_volume_descriptor *)block_buffer;
207
208           /* Due to the overlapping physical location of the descriptors,
209            * ISO CDs can match hdp->id==HS_STANDARD_ID as well. To ensure
210            * proper identification in this case, we first check for ISO.
211            */
212           if (strncmp (vdp->id, ISO_STANDARD_ID, sizeof vdp->id) == 0) {
213                *iso_root_block = block;
214                return 1;
215           }
216      }
217
218      return 0;
219 }
220
221 /*
222  * Detects and read amiga partition tables.
223  */
224
225 static int
226 _amiga_checksum (unsigned int blk_size)
227 {
228         unsigned int sum;
229         int i, end;
230         unsigned int *amiga_block = (unsigned int *) block_buffer;
231
232         sum = amiga_block[0];
233         end = amiga_block[AMIGA_LENGTH];
234
235         if (end > blk_size) end = blk_size;
236
237         for (i = 1; i < end; i++) sum += amiga_block[i];
238
239         return sum;
240 }
241
242 static int
243 _amiga_find_rdb (const char *dev_name, prom_handle disk, unsigned int prom_blksize)
244 {
245         int i;
246         unsigned int *amiga_block = (unsigned int *) block_buffer;
247
248         for (i = 0; i<AMIGA_RDB_MAX; i++) {
249                 if (i != 0) {
250                         if (prom_readblocks(disk, i, 1, block_buffer) != 1) {
251                                 prom_printf("Can't read boot block %d\n", i);
252                                 break;
253                         }
254                 }
255                 if ((amiga_block[AMIGA_ID] == AMIGA_ID_RDB) && (_amiga_checksum (prom_blksize) == 0))
256                         return 1;
257         }
258         /* Amiga partition table not found, let's reread block 0 */
259         if (prom_readblocks(disk, 0, 1, block_buffer) != 1) {
260                 prom_printf("Can't read boot blocks\n");
261                 return 0; /* TODO: something bad happened, should fail more verbosely */
262         }
263         return 0;
264 }
265
266 static void
267 partition_amiga_lookup( const char *dev_name, prom_handle disk,
268                         unsigned int prom_blksize, struct partition_t** list )
269 {
270         int partition, part;
271         unsigned int blockspercyl;
272         unsigned int *amiga_block = (unsigned int *) block_buffer;
273         unsigned int *used = NULL;
274         unsigned int possible;
275         int checksum;
276         int i;
277
278         blockspercyl = amiga_block[AMIGA_SECT] * amiga_block[AMIGA_HEADS];
279         possible = amiga_block[AMIGA_RDBLIMIT]/32 +1;
280
281         used = (unsigned int *) malloc (sizeof (unsigned int) * (possible + 1));
282         if (!used) {
283                prom_printf("Can't allocate memory\n");
284                return;
285         }
286
287         for (i=0; i < possible; i++) used[i] = 0;
288
289
290         for (part = amiga_block[AMIGA_PARTITIONS], partition = 1;
291                 part != AMIGA_END;
292                 part = amiga_block[AMIGA_PART_NEXT], partition++)
293         {
294                 if (prom_readblocks(disk, part, 1, block_buffer) != 1) {
295                         prom_printf("Can't read partition block %d\n", part);
296                         break;
297                 }
298                 checksum = _amiga_checksum (prom_blksize);
299                 if ((amiga_block[AMIGA_ID] == AMIGA_ID_PART) &&
300                         (checksum == 0) &&
301                         ((used[part/32] & (0x1 << (part % 32))) == 0))
302                 {
303                         used[part/32] |= (0x1 << (part % 32));
304                 } else {
305                         prom_printf("Amiga partition table corrupted at block %d\n", part);
306                         if (amiga_block[AMIGA_ID] != AMIGA_ID_PART)
307                                 prom_printf ("block type is not partition but %08x\n", amiga_block[AMIGA_ID]);
308                         if (checksum != 0)
309                                 prom_printf ("block checsum is bad : %d\n", checksum);
310                         if ((used[part/32] & (0x1 << (part % 32))) != 0)
311                                 prom_printf ("partition table is looping, block %d already traveled\n", part);
312                         break;
313                 }
314
315                /* We use the partition block size from the partition table.
316                 * The filesystem implmentations are responsible for mapping
317                 * to their own fs blocksize */
318                add_new_partition(
319                     list, /* partition list */
320                     partition, /* partition number */
321                     "Linux", /* type */
322                     '\0', /* name */
323                     blockspercyl * amiga_block[AMIGA_PART_LOWCYL], /* start */
324                     blockspercyl * (amiga_block[AMIGA_PART_HIGHCYL] - amiga_block[AMIGA_PART_LOWCYL] + 1), /* size */
325                     prom_blksize,
326                     0 );
327         }
328         if (used)
329                 free(used);
330 }
331
332 struct partition_t*
333 partitions_lookup(const char *device)
334 {
335      ihandle    disk;
336      struct mac_driver_desc *desc = (struct mac_driver_desc *)block_buffer;
337      struct partition_t* list = NULL;
338      unsigned int prom_blksize, iso_root_block;
339
340      strncpy(block_buffer, device, 2040);
341      if (_machine != _MACH_bplan)
342           strcat(block_buffer, ":0");
343
344      /* Open device */
345      disk = prom_open(block_buffer);
346      if (disk == NULL) {
347           prom_printf("Can't open device <%s>\n", block_buffer);
348           goto bail;
349      }
350      prom_blksize = prom_getblksize(disk);
351      DEBUG_F("block size of device is %d\n", prom_blksize);
352
353      if (prom_blksize <= 1)
354           prom_blksize = 512;
355      if (prom_blksize > MAX_BLOCK_SIZE) {
356           prom_printf("block_size %d not supported !\n", prom_blksize);
357           goto bail;
358      }
359
360      /* Read boot blocs */
361      if (prom_readblocks(disk, 0, 1, block_buffer) != 1) {
362           prom_printf("Can't read boot blocks\n");
363           goto bail;
364      }
365      if (desc->signature == MAC_DRIVER_MAGIC) {
366           /* pdisk partition format */
367           partition_mac_lookup(device, disk, prom_blksize, &list);
368      } else if ((block_buffer[510] == 0x55) && (block_buffer[511] == 0xaa)) {
369           /* fdisk partition format */
370           partition_fdisk_lookup(device, disk, prom_blksize, &list);
371      } else if (prom_blksize == 2048 && identify_iso_fs(disk, &iso_root_block)) {
372           add_new_partition(&list,
373                             0,
374                             '\0',
375                             '\0',
376                             iso_root_block,
377                             0,
378                             prom_blksize,
379                             0);
380           prom_printf("ISO9660 disk\n");
381      } else if (_amiga_find_rdb(device, disk, prom_blksize) != -1) {
382           /* amiga partition format */
383           partition_amiga_lookup(device, disk, prom_blksize, &list);
384      } else {
385           prom_printf("No supported partition table detected\n");
386           goto bail;
387      }
388
389 bail:
390      prom_close(disk);
391
392      return list;
393 }
394
395 char *
396 get_part_type(char *device, int partition)
397 {
398      struct partition_t*        parts;
399      struct partition_t*        p;
400      struct partition_t*        found;
401      char *type = NULL;
402
403      int device_kind = prom_get_devtype(device);
404      if (device_kind != FILE_DEVICE_BLOCK && device_kind != FILE_DEVICE_ISCSI)
405           return NULL;
406
407      parts = partitions_lookup(device);
408      found = NULL;
409
410      if (!parts)
411           return '\0';
412
413      for (p = parts; p && !found; p=p->next) {
414           DEBUG_F("number: %02d, start: 0x%08lx, length: 0x%08lx, type: %s, name: %s\n",
415                   p->part_number, p->part_start, p->part_size, p->part_type, p->part_name);
416           if ((partition >= 0) && (partition == p->part_number)) {
417                type = strdup(p->part_type);
418                break;
419           }
420      }
421      if (parts)
422           partitions_free(parts);
423      return type;
424 }
425
426 /* Freed in reverse order of allocation to help malloc'ator */
427 void
428 partitions_free(struct partition_t* list)
429 {
430      struct partition_t*        next;
431
432      while(list) {
433           next = list->next;
434           free(list);
435           list = next;
436      }
437 }
438
439
440 /*
441  * Local variables:
442  * c-file-style: "k&r"
443  * c-basic-offset: 5
444  * End:
445  */