]> git.ozlabs.org Git - yaboot.git/blob - second/partition.c
amiga partition table support.
[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
44 /* We currently don't check the partition type, some users
45  * are putting crap there and still expect it to work...
46  */
47 #undef CHECK_FOR_VALID_MAC_PARTITION_TYPE
48
49 #ifdef CHECK_FOR_VALID_MAC_PARTITION_TYPE
50 static const char *valid_mac_partition_types[] = {
51      "apple_unix_svr2",
52      "linux",
53      "apple_hfs",
54      "apple_boot",
55      "apple_bootstrap",
56      NULL
57 };
58 #endif
59     
60
61 /* Local functions */
62 static unsigned long swab32(unsigned long value);
63
64 #define MAX_BLOCK_SIZE  2048
65 static unsigned char block_buffer[MAX_BLOCK_SIZE];
66
67 static void
68 add_new_partition(struct partition_t**  list, int part_number, const char *part_type,
69                   const char *part_name, unsigned long part_start, unsigned long part_size,
70                   unsigned short part_blocksize)
71 {
72      struct partition_t*        part;
73      part = (struct partition_t*)malloc(sizeof(struct partition_t));
74         
75      part->part_number = part_number;
76      strncpy(part->part_type, part_type, MAX_PART_NAME);
77      strncpy(part->part_name, part_name, MAX_PART_NAME);
78      part->part_start = part_start;
79      part->part_size = part_size;
80      part->blocksize = part_blocksize;
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      }
154 }
155
156 /* 
157  * Same function as partition_mac_lookup(), except for fdisk
158  * partitioned disks.
159  */
160 static void
161 partition_fdisk_lookup( const char *dev_name, prom_handle disk,
162                         unsigned int prom_blksize, struct partition_t** list )
163 {
164      int partition;
165
166      /* fdisk partition tables start at offset 0x1be
167       * from byte 0 of the boot drive.
168       */
169      struct fdisk_partition* part = 
170           (struct fdisk_partition *) (block_buffer + 0x1be);
171
172      for (partition=1; partition <= 4 ;partition++, part++) {
173           if (part->sys_ind == LINUX_NATIVE) {
174                add_new_partition(  list,
175                                    partition,
176                                    "Linux", /* type */
177                                    '\0', /* name */
178                                    swab32(*(unsigned int *)(part->start4)),
179                                    swab32(*(unsigned int *)(part->size4)),
180                                    512 /*blksize*/ );
181           }
182      }
183 }
184
185 /* I don't know if it's possible to handle multisession and other multitrack
186  * stuffs with the current OF disklabel package. This can still be implemented
187  * with direct calls to atapi stuffs.
188  * Currently, we enter this code for any device of block size 0x2048 who lacks
189  * a MacOS partition map signature.
190  */
191 static int
192 identify_iso_fs(ihandle device, unsigned int *iso_root_block)
193 {
194      int block;
195
196      for (block = 16; block < 100; block++) {
197           struct iso_volume_descriptor  * vdp;
198
199           if (prom_readblocks(device, block, 1, block_buffer) != 1) {
200                prom_printf("Can't read volume desc block %d\n", block);
201                break;
202           }
203                 
204           vdp = (struct iso_volume_descriptor *)block_buffer;
205             
206           /* Due to the overlapping physical location of the descriptors, 
207            * ISO CDs can match hdp->id==HS_STANDARD_ID as well. To ensure 
208            * proper identification in this case, we first check for ISO.
209            */
210           if (strncmp (vdp->id, ISO_STANDARD_ID, sizeof vdp->id) == 0) {
211                *iso_root_block = block;
212                return 1;
213           }
214      }
215         
216      return 0;
217 }
218
219 /* 
220  * Detects and read amiga partition tables.
221  */
222
223 static int
224 _amiga_checksum (unsigned int blk_size)
225 {
226         unsigned int sum;
227         int i, end;
228         unsigned int *amiga_block = (unsigned int *) block_buffer;
229
230         sum = amiga_block[0];
231         end = amiga_block[AMIGA_LENGTH];
232
233         if (end > blk_size) end = blk_size;
234
235         for (i = 1; i < end; i++) sum += amiga_block[i];
236
237         return sum;
238 }
239
240 static int
241 _amiga_find_rdb (const char *dev_name, prom_handle disk, unsigned int prom_blksize)
242 {
243         int i;
244         unsigned int *amiga_block = (unsigned int *) block_buffer;
245
246         for (i = 0; i<AMIGA_RDB_MAX; i++) {
247                 if (i != 0) {
248                         if (prom_readblocks(disk, i, 1, block_buffer) != 1) {
249                                 prom_printf("Can't read boot block %d\n", i);
250                                 break;
251                         }       
252                 }
253                 if ((amiga_block[AMIGA_ID] == AMIGA_ID_RDB) && (_amiga_checksum (prom_blksize) == 0))
254                         return 1;
255         }
256         /* Amiga partition table not found, let's reread block 0 */
257         if (prom_readblocks(disk, 0, 1, block_buffer) != 1) {
258                 prom_printf("Can't read boot blocks\n");
259                 return 0; /* TODO: something bad happened, should fail more verbosely */
260         }       
261         return 0;
262 }
263
264 static void
265 partition_amiga_lookup( const char *dev_name, prom_handle disk,
266                         unsigned int prom_blksize, struct partition_t** list )
267 {
268         int partition, part;
269         unsigned int blockspercyl;
270         unsigned int *amiga_block = (unsigned int *) block_buffer;
271         unsigned int *used = NULL;
272         unsigned int possible;
273         int checksum;
274         int i;
275
276         blockspercyl = amiga_block[AMIGA_SECT] * amiga_block[AMIGA_HEADS];
277         possible = amiga_block[AMIGA_RDBLIMIT]/32 +1;
278
279         used = (unsigned int *) malloc (sizeof (unsigned int) * (possible + 1));
280
281         for (i=0; i < possible; i++) used[i] = 0;
282
283
284         for (part = amiga_block[AMIGA_PARTITIONS], partition = 0;
285                 part != AMIGA_END;
286                 part = amiga_block[AMIGA_PART_NEXT], partition++)
287         {
288                 if (prom_readblocks(disk, part, 1, block_buffer) != 1) {
289                         prom_printf("Can't read partition block %d\n", part);
290                         break;
291                 }       
292                 checksum = _amiga_checksum (prom_blksize);
293                 if ((amiga_block[AMIGA_ID] == AMIGA_ID_PART) &&
294                         (checksum == 0) &&
295                         ((used[part/32] & (0x1 << (part % 32))) == 0))
296                 {
297                         used[part/32] |= (0x1 << (part % 32));
298                 } else {
299                         prom_printf("Amiga partition table corrupted at block %d\n", part);
300                         if (amiga_block[AMIGA_ID] != AMIGA_ID_PART)
301                                 prom_printf ("block type is not partition but %08x\n", amiga_block[AMIGA_ID]);
302                         if (checksum != 0)
303                                 prom_printf ("block checsum is bad : %d\n", checksum);
304                         if ((used[part/32] & (0x1 << (part % 32))) != 0)
305                                 prom_printf ("partition table is looping, block %d already traveled\n", part);
306                         break;
307                 }
308
309                /* We use the partition block size from the partition table.
310                 * The filesystem implmentations are responsible for mapping
311                 * to their own fs blocksize */
312                add_new_partition(
313                     list, /* partition list */
314                     partition, /* partition number */
315                     "Linux", /* type */
316                     '\0', /* name */
317                     blockspercyl * amiga_block[AMIGA_PART_LOWCYL], /* start */
318                     blockspercyl * (amiga_block[AMIGA_PART_HIGHCYL] - amiga_block[AMIGA_PART_LOWCYL] + 1), /* size */
319                     prom_blksize );
320         }
321 }
322
323 struct partition_t*
324 partitions_lookup(const char *device)
325 {
326      ihandle    disk;
327      struct mac_driver_desc *desc = (struct mac_driver_desc *)block_buffer;
328      struct partition_t* list = NULL;
329      unsigned int prom_blksize, iso_root_block;
330         
331      strncpy(block_buffer, device, 2040);
332      strcat(block_buffer, ":0");
333         
334      /* Open device */
335      disk = prom_open(block_buffer);
336      if (disk == NULL) {
337           prom_printf("Can't open device <%s>\n", block_buffer);
338           goto bail;
339      }
340      prom_blksize = prom_getblksize(disk);
341      DEBUG_F("block size of device is %d\n", prom_blksize);
342
343      if (prom_blksize <= 1)
344           prom_blksize = 512;
345      if (prom_blksize > MAX_BLOCK_SIZE) {
346           prom_printf("block_size %d not supported !\n", prom_blksize);
347           goto bail;
348      }
349         
350      /* Read boot blocs */
351      if (prom_readblocks(disk, 0, 1, block_buffer) != 1) {
352           prom_printf("Can't read boot blocks\n");
353           goto bail;
354      }  
355      if (desc->signature == MAC_DRIVER_MAGIC) {
356           /* pdisk partition format */
357           partition_mac_lookup(device, disk, prom_blksize, &list);
358      } else if ((block_buffer[510] == 0x55) && (block_buffer[511] == 0xaa)) {
359           /* fdisk partition format */
360           partition_fdisk_lookup(device, disk, prom_blksize, &list);
361      } else if (prom_blksize == 2048 && identify_iso_fs(disk, &iso_root_block)) {
362           add_new_partition(&list,
363                             0,
364                             '\0',
365                             '\0',
366                             iso_root_block,
367                             0,
368                             prom_blksize);
369           prom_printf("ISO9660 disk\n");
370      } else if (_amiga_find_rdb(device, disk, prom_blksize) != -1) {
371           /* amiga partition format */
372           partition_amiga_lookup(device, disk, prom_blksize, &list);
373      } else {
374           prom_printf("No supported partition table detected\n");
375           goto bail;
376      }
377
378 bail:
379      prom_close(disk);
380         
381      return list;
382 }
383
384 char *
385 get_part_type(char *device, int partition)
386 {
387      struct partition_t*        parts;
388      struct partition_t*        p;
389      struct partition_t*        found;
390      char *type = NULL;
391
392      if (prom_get_devtype(device) != FILE_DEVICE_BLOCK)
393           return NULL;
394
395      parts = partitions_lookup(device);
396      found = NULL;
397
398      if (!parts)
399           return '\0';
400
401      for (p = parts; p && !found; p=p->next) {
402           DEBUG_F("number: %02d, start: 0x%08lx, length: 0x%08lx, type: %s, name: %s\n",
403                   p->part_number, p->part_start, p->part_size, p->part_type, p->part_name);
404           if ((partition >= 0) && (partition == p->part_number)) {
405                type = strdup(p->part_type);
406                break;
407           }       
408      }
409      if (parts)
410           partitions_free(parts);
411      return type;
412 }
413
414 /* Freed in reverse order of allocation to help malloc'ator */
415 void
416 partitions_free(struct partition_t* list)
417 {
418      struct partition_t*        next;
419         
420      while(list) {
421           next = list->next;
422           free(list);
423           list = next;
424      }
425 }
426 unsigned long
427 swab32(unsigned long value)
428 {
429      __u32 result;
430
431      __asm__("rlwimi %0,%1,24,16,23\n\t"
432              "rlwimi %0,%1,8,8,15\n\t"
433              "rlwimi %0,%1,24,0,7"
434              : "=r" (result)
435              : "r" (value), "0" (value >> 24));
436      return result;
437 }
438
439
440 /* 
441  * Local variables:
442  * c-file-style: "k&r"
443  * c-basic-offset: 5
444  * End:
445  */