2 * partition.c - partition table support
4 * Copyright (C) 2001, 2002 Ethan Benson
6 * Copyright (C) 1999 Benjamin Herrenschmidt
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 * Todo: Add disklabel (or let OF do it ?). Eventually think about
25 * fixing CDROM handling by directly using the ATAPI layer.
33 #include "fdisk-part.h"
34 #include "partition.h"
37 #include "linux/iso_fs.h"
41 /* We currently don't check the partition type, some users
42 * are putting crap there and still expect it to work...
44 #undef CHECK_FOR_VALID_MAC_PARTITION_TYPE
46 #ifdef CHECK_FOR_VALID_MAC_PARTITION_TYPE
47 static const char *valid_mac_partition_types[] = {
59 static unsigned long swab32(unsigned long value);
61 #define MAX_BLOCK_SIZE 2048
62 static unsigned char block_buffer[MAX_BLOCK_SIZE];
65 add_new_partition(struct partition_t** list, int part_number, const char *part_type,
66 const char *part_name, unsigned long part_start, unsigned long part_size,
67 unsigned short part_blocksize)
69 struct partition_t* part;
70 part = (struct partition_t*)malloc(sizeof(struct partition_t));
72 part->part_number = part_number;
73 strncpy(part->part_type, part_type, MAX_PART_NAME);
74 strncpy(part->part_name, part_name, MAX_PART_NAME);
75 part->part_start = part_start;
76 part->part_size = part_size;
77 part->blocksize = part_blocksize;
79 /* Tack this entry onto the list */
84 /* Note, we rely on partitions being dev-block-size aligned,
85 * I have to check if it's true. If it's not, then things will get
86 * a bit more complicated
89 partition_mac_lookup( const char *dev_name, prom_handle disk,
90 unsigned int prom_blksize, struct partition_t** list )
94 /* block_buffer contains block 0 from the partitions_lookup() stage */
95 struct mac_partition* part = (struct mac_partition *)block_buffer;
96 unsigned short ptable_block_size =
97 ((struct mac_driver_desc *)block_buffer)->block_size;
100 for (block=1; block < map_size + 1; block++)
102 #ifdef CHECK_FOR_VALID_MAC_PARTITION_TYPE
106 if (prom_readblocks(disk, block, 1, block_buffer) != 1) {
107 prom_printf("Can't read partition %d\n", block);
110 if (part->signature != MAC_PARTITION_MAGIC) {
112 prom_printf("Wrong partition %d signature\n", block);
117 map_size = part->map_count;
119 #ifdef CHECK_FOR_VALID_MAC_PARTITION_TYPE
120 /* We don't bother looking at swap partitions of any type,
121 * and the rest are the ones we know about */
122 for (ptype = valid_mac_partition_types; ptype; ptype++)
123 if (!strcmp (part->type, ptype))
130 prom_printf( "MAC: Unsupported partition #%d; type=%s\n",
136 #ifdef CHECK_FOR_VALID_MAC_PARTITION_TYPE
139 /* We use the partition block size from the partition table.
140 * The filesystem implmentations are responsible for mapping
141 * to their own fs blocksize */
143 list, /* partition list */
144 block, /* partition number */
145 part->type, /* type */
146 part->name, /* name */
147 part->start_block + part->data_start, /* start */
148 part->data_count, /* size */
154 * Same function as partition_mac_lookup(), except for fdisk
158 partition_fdisk_lookup( const char *dev_name, prom_handle disk,
159 unsigned int prom_blksize, struct partition_t** list )
163 /* fdisk partition tables start at offset 0x1be
164 * from byte 0 of the boot drive.
166 struct fdisk_partition* part =
167 (struct fdisk_partition *) (block_buffer + 0x1be);
169 for (partition=1; partition <= 4 ;partition++, part++) {
170 if (part->sys_ind == LINUX_NATIVE) {
171 add_new_partition( list,
175 swab32(*(unsigned int *)(part->start4)),
176 swab32(*(unsigned int *)(part->size4)),
182 /* I don't know if it's possible to handle multisession and other multitrack
183 * stuffs with the current OF disklabel package. This can still be implemented
184 * with direct calls to atapi stuffs.
185 * Currently, we enter this code for any device of block size 0x2048 who lacks
186 * a MacOS partition map signature.
189 identify_iso_fs(ihandle device, unsigned int *iso_root_block)
193 for (block = 16; block < 100; block++) {
194 struct iso_volume_descriptor * vdp;
196 if (prom_readblocks(device, block, 1, block_buffer) != 1) {
197 prom_printf("Can't read volume desc block %d\n", block);
201 vdp = (struct iso_volume_descriptor *)block_buffer;
203 /* Due to the overlapping physical location of the descriptors,
204 * ISO CDs can match hdp->id==HS_STANDARD_ID as well. To ensure
205 * proper identification in this case, we first check for ISO.
207 if (strncmp (vdp->id, ISO_STANDARD_ID, sizeof vdp->id) == 0) {
208 *iso_root_block = block;
217 partitions_lookup(const char *device)
220 struct mac_driver_desc *desc = (struct mac_driver_desc *)block_buffer;
221 struct partition_t* list = NULL;
222 unsigned int prom_blksize, iso_root_block;
224 strncpy(block_buffer, device, 2040);
225 strcat(block_buffer, ":0");
228 disk = prom_open(block_buffer);
230 prom_printf("Can't open device <%s>\n", block_buffer);
233 prom_blksize = prom_getblksize(disk);
234 DEBUG_F("block size of device is %d\n", prom_blksize);
236 if (prom_blksize <= 1)
238 if (prom_blksize > MAX_BLOCK_SIZE) {
239 prom_printf("block_size %d not supported !\n", prom_blksize);
243 /* Read boot blocs */
244 if (prom_readblocks(disk, 0, 1, block_buffer) != 1) {
245 prom_printf("Can't read boot blocks\n");
248 if (desc->signature == MAC_DRIVER_MAGIC) {
249 /* pdisk partition format */
250 partition_mac_lookup(device, disk, prom_blksize, &list);
251 } else if ((block_buffer[510] == 0x55) && (block_buffer[511] == 0xaa)) {
252 /* fdisk partition format */
253 partition_fdisk_lookup(device, disk, prom_blksize, &list);
254 } else if (prom_blksize == 2048 && identify_iso_fs(disk, &iso_root_block)) {
255 add_new_partition(&list,
262 prom_printf("ISO9660 disk\n");
264 prom_printf("No supported partition table detected\n");
275 get_part_type(char *device, int partition)
277 struct partition_t* parts;
278 struct partition_t* p;
279 struct partition_t* found;
282 if (prom_get_devtype(device) != FILE_DEVICE_BLOCK)
285 parts = partitions_lookup(device);
291 for (p = parts; p && !found; p=p->next) {
292 DEBUG_F("number: %02d, start: 0x%08lx, length: 0x%08lx, type: %s, name: %s\n",
293 p->part_number, p->part_start, p->part_size, p->part_type, p->part_name);
294 if ((partition >= 0) && (partition == p->part_number)) {
295 type = strdup(p->part_type);
300 partitions_free(parts);
304 /* Freed in reverse order of allocation to help malloc'ator */
306 partitions_free(struct partition_t* list)
308 struct partition_t* next;
317 swab32(unsigned long value)
321 __asm__("rlwimi %0,%1,24,16,23\n\t"
322 "rlwimi %0,%1,8,8,15\n\t"
323 "rlwimi %0,%1,24,0,7"
325 : "r" (value), "0" (value >> 24));
332 * c-file-style: "k&r"