2 * partition.c - partition table support
4 * Copyright (C) 2004 Sven Luther
6 * Copyright (C) 2001, 2002 Ethan Benson
8 * Copyright (C) 1999 Benjamin Herrenschmidt
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.
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.
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.
26 * Todo: Add disklabel (or let OF do it ?). Eventually think about
27 * fixing CDROM handling by directly using the ATAPI layer.
35 #include "fdisk-part.h"
36 #include "amiga-part.h"
37 #include "partition.h"
40 #include "linux/iso_fs.h"
44 #include "byteorder.h"
46 /* We currently don't check the partition type, some users
47 * are putting crap there and still expect it to work...
49 #undef CHECK_FOR_VALID_MAC_PARTITION_TYPE
51 #ifdef CHECK_FOR_VALID_MAC_PARTITION_TYPE
52 static const char *valid_mac_partition_types[] = {
63 #define MAX_BLOCK_SIZE 2048
64 static unsigned char block_buffer[MAX_BLOCK_SIZE];
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)
71 struct partition_t* part;
72 part = (struct partition_t*)malloc(sizeof(struct partition_t));
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;
82 /* Tack this entry onto the list */
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
92 partition_mac_lookup( const char *dev_name, prom_handle disk,
93 unsigned int prom_blksize, struct partition_t** list )
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;
103 for (block=1; block < map_size + 1; block++)
105 #ifdef CHECK_FOR_VALID_MAC_PARTITION_TYPE
109 if (prom_readblocks(disk, block, 1, block_buffer) != 1) {
110 prom_printf("Can't read partition %d\n", block);
113 if (part->signature != MAC_PARTITION_MAGIC) {
115 prom_printf("Wrong partition %d signature\n", block);
120 map_size = part->map_count;
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))
133 prom_printf( "MAC: Unsupported partition #%d; type=%s\n",
139 #ifdef CHECK_FOR_VALID_MAC_PARTITION_TYPE
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 */
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 */
158 * Same function as partition_mac_lookup(), except for fdisk
162 partition_fdisk_lookup( const char *dev_name, prom_handle disk,
163 unsigned int prom_blksize, struct partition_t** list )
167 /* fdisk partition tables start at offset 0x1be
168 * from byte 0 of the boot drive.
170 struct fdisk_partition* part =
171 (struct fdisk_partition *) (block_buffer + 0x1be);
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,
179 le32_to_cpu(*(unsigned int *)part->start4),
180 le32_to_cpu(*(unsigned int *)part->size4),
182 part->sys_ind /* partition type */ );
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.
194 identify_iso_fs(ihandle device, unsigned int *iso_root_block)
198 for (block = 16; block < 100; block++) {
199 struct iso_volume_descriptor * vdp;
201 if (prom_readblocks(device, block, 1, block_buffer) != 1) {
202 prom_printf("Can't read volume desc block %d\n", block);
206 vdp = (struct iso_volume_descriptor *)block_buffer;
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.
212 if (strncmp (vdp->id, ISO_STANDARD_ID, sizeof vdp->id) == 0) {
213 *iso_root_block = block;
222 * Detects and read amiga partition tables.
226 _amiga_checksum (unsigned int blk_size)
230 unsigned int *amiga_block = (unsigned int *) block_buffer;
232 sum = amiga_block[0];
233 end = amiga_block[AMIGA_LENGTH];
235 if (end > blk_size) end = blk_size;
237 for (i = 1; i < end; i++) sum += amiga_block[i];
243 _amiga_find_rdb (const char *dev_name, prom_handle disk, unsigned int prom_blksize)
246 unsigned int *amiga_block = (unsigned int *) block_buffer;
248 for (i = 0; i<AMIGA_RDB_MAX; i++) {
250 if (prom_readblocks(disk, i, 1, block_buffer) != 1) {
251 prom_printf("Can't read boot block %d\n", i);
255 if ((amiga_block[AMIGA_ID] == AMIGA_ID_RDB) && (_amiga_checksum (prom_blksize) == 0))
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 */
267 partition_amiga_lookup( const char *dev_name, prom_handle disk,
268 unsigned int prom_blksize, struct partition_t** list )
271 unsigned int blockspercyl;
272 unsigned int *amiga_block = (unsigned int *) block_buffer;
273 unsigned int *used = NULL;
274 unsigned int possible;
278 blockspercyl = amiga_block[AMIGA_SECT] * amiga_block[AMIGA_HEADS];
279 possible = amiga_block[AMIGA_RDBLIMIT]/32 +1;
281 used = (unsigned int *) malloc (sizeof (unsigned int) * (possible + 1));
283 prom_printf("Can't allocate memory\n");
287 for (i=0; i < possible; i++) used[i] = 0;
290 for (part = amiga_block[AMIGA_PARTITIONS], partition = 1;
292 part = amiga_block[AMIGA_PART_NEXT], partition++)
294 if (prom_readblocks(disk, part, 1, block_buffer) != 1) {
295 prom_printf("Can't read partition block %d\n", part);
298 checksum = _amiga_checksum (prom_blksize);
299 if ((amiga_block[AMIGA_ID] == AMIGA_ID_PART) &&
301 ((used[part/32] & (0x1 << (part % 32))) == 0))
303 used[part/32] |= (0x1 << (part % 32));
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]);
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);
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 */
319 list, /* partition list */
320 partition, /* partition number */
323 blockspercyl * amiga_block[AMIGA_PART_LOWCYL], /* start */
324 blockspercyl * (amiga_block[AMIGA_PART_HIGHCYL] - amiga_block[AMIGA_PART_LOWCYL] + 1), /* size */
333 partitions_lookup(const char *device)
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;
340 strncpy(block_buffer, device, 2040);
341 if (_machine != _MACH_bplan)
342 strcat(block_buffer, ":0");
345 disk = prom_open(block_buffer);
347 prom_printf("Can't open device <%s>\n", block_buffer);
350 prom_blksize = prom_getblksize(disk);
351 DEBUG_F("block size of device is %d\n", prom_blksize);
353 if (prom_blksize <= 1)
355 if (prom_blksize > MAX_BLOCK_SIZE) {
356 prom_printf("block_size %d not supported !\n", prom_blksize);
360 /* Read boot blocs */
361 if (prom_readblocks(disk, 0, 1, block_buffer) != 1) {
362 prom_printf("Can't read boot blocks\n");
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,
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);
385 prom_printf("No supported partition table detected\n");
396 get_part_type(char *device, int partition)
398 struct partition_t* parts;
399 struct partition_t* p;
400 struct partition_t* found;
403 int device_kind = prom_get_devtype(device);
404 if (device_kind != FILE_DEVICE_BLOCK && device_kind != FILE_DEVICE_ISCSI)
407 parts = partitions_lookup(device);
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);
422 partitions_free(parts);
426 /* Freed in reverse order of allocation to help malloc'ator */
428 partitions_free(struct partition_t* list)
430 struct partition_t* next;
442 * c-file-style: "k&r"