3 Copyright (C) 1999 Benjamin Herrenschmidt
5 Todo: Add disklabel (or let OF do it ?). Eventually think about
6 fixing CDROM handling by directly using the ATAPI layer.
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., 675 Mass Ave, Cambridge, MA 02139, USA. */
27 #include "fdisk-part.h"
28 #include "partition.h"
31 #include "linux/iso_fs.h"
33 /* We currently don't check the partition type, some users
34 * are putting crap there and still expect it to work...
36 #undef CHECK_FOR_VALID_MAC_PARTITION_TYPE
38 #ifdef CHECK_FOR_VALID_MAC_PARTITION_TYPE
39 static const char *valid_mac_partition_types[] = {
51 static unsigned long swab32(unsigned long value);
53 #define MAX_BLOCK_SIZE 2048
54 static unsigned char block_buffer[MAX_BLOCK_SIZE];
57 add_new_partition( struct partition_t** list, int part_number,
58 unsigned long part_start, unsigned long part_size,
59 unsigned short part_blocksize )
61 struct partition_t* part;
62 part = (struct partition_t*)malloc(sizeof(struct partition_t));
64 part->part_number = part_number;
65 part->part_start = part_start;
66 part->part_size = part_size;
67 part->blocksize = part_blocksize;
69 /* Tack this entry onto the list */
74 /* Note, we rely on partitions being dev-block-size aligned,
75 * I have to check if it's true. If it's not, then things will get
76 * a bit more complicated
79 partition_mac_lookup( const char *dev_name, prom_handle disk,
80 unsigned int prom_blksize, struct partition_t** list )
84 /* block_buffer contains block 0 from the partitions_lookup() stage */
85 struct mac_partition* part = (struct mac_partition *)block_buffer;
86 unsigned short ptable_block_size =
87 ((struct mac_driver_desc *)block_buffer)->block_size;
90 for (block=1; block < map_size + 1; block++)
92 #ifdef CHECK_FOR_VALID_MAC_PARTITION_TYPE
96 if (prom_readblocks(disk, block, 1, block_buffer) != 1) {
97 prom_printf("Can't read partition %d\n", block);
100 if (part->signature != MAC_PARTITION_MAGIC) {
102 prom_printf("Wrong partition %d signature\n", block);
107 map_size = part->map_count;
109 #ifdef CHECK_FOR_VALID_MAC_PARTITION_TYPE
110 /* We don't bother looking at swap partitions of any type,
111 * and the rest are the ones we know about */
112 for (ptype = valid_mac_partition_types; ptype; ptype++)
113 if (!strcmp (part->type, ptype))
120 prom_printf( "MAC: Unsupported partition #%d; type=%s\n",
126 #ifdef CHECK_FOR_VALID_MAC_PARTITION_TYPE
129 /* We use the partition block size from the partition table.
130 * The filesystem implmentations are responsible for mapping
131 * to their own fs blocksize */
133 list, /* partition list */
134 block, /* partition number */
135 part->start_block + part->data_start, /* start */
136 part->data_count, /* size */
142 * Same function as partition_mac_lookup(), except for fdisk
146 partition_fdisk_lookup( const char *dev_name, prom_handle disk,
147 unsigned int prom_blksize, struct partition_t** list )
151 /* fdisk partition tables start at offset 0x1be
152 * from byte 0 of the boot drive.
154 struct fdisk_partition* part =
155 (struct fdisk_partition *) (block_buffer + 0x1be);
157 for (partition=1; partition <= 4 ;partition++, part++) {
158 if (part->sys_ind == LINUX_NATIVE) {
159 add_new_partition( list,
161 swab32(*(unsigned int *)(part->start4)),
162 swab32(*(unsigned int *)(part->size4)),
168 /* I don't know if it's possible to handle multisession and other multitrack
169 * stuffs with the current OF disklabel package. This can still be implemented
170 * with direct calls to atapi stuffs.
171 * Currently, we enter this code for any device of block size 0x2048 who lacks
172 * a MacOS partition map signature.
175 identify_iso_fs(ihandle device, unsigned int *iso_root_block)
179 for (block = 16; block < 100; block++) {
180 struct iso_volume_descriptor * vdp;
182 if (prom_readblocks(device, block, 1, block_buffer) != 1) {
183 prom_printf("Can't read volume desc block %d\n", block);
187 vdp = (struct iso_volume_descriptor *)block_buffer;
189 /* Due to the overlapping physical location of the descriptors,
190 * ISO CDs can match hdp->id==HS_STANDARD_ID as well. To ensure
191 * proper identification in this case, we first check for ISO.
193 if (strncmp (vdp->id, ISO_STANDARD_ID, sizeof vdp->id) == 0) {
194 *iso_root_block = block;
203 partitions_lookup(const char *device)
206 struct mac_driver_desc *desc = (struct mac_driver_desc *)block_buffer;
207 struct partition_t* list = NULL;
208 unsigned int prom_blksize, iso_root_block;
210 strncpy(block_buffer, device, 2040);
211 strcat(block_buffer, ":0");
214 disk = prom_open(block_buffer);
216 prom_printf("Can't open device <%s>\n", block_buffer);
219 prom_blksize = prom_getblksize(disk);
221 prom_printf("block size of device is %d\n", prom_blksize);
223 if (prom_blksize <= 1)
225 if (prom_blksize > MAX_BLOCK_SIZE) {
226 prom_printf("block_size %d not supported !\n");
230 /* Read boot blocs */
231 if (prom_readblocks(disk, 0, 1, block_buffer) != 1) {
232 prom_printf("Can't read boot blocs\n");
235 if (desc->signature == MAC_DRIVER_MAGIC) {
236 /* pdisk partition format */
237 partition_mac_lookup(device, disk, prom_blksize, &list);
238 } else if ((block_buffer[510] == 0x55) && (block_buffer[511] == 0xaa)) {
239 /* fdisk partition format */
240 partition_fdisk_lookup(device, disk, prom_blksize, &list);
241 } else if (prom_blksize == 2048 && identify_iso_fs(disk, &iso_root_block)) {
242 add_new_partition( &list,
247 prom_printf("ISO9660 disk\n");
249 prom_printf("Not a macintosh-formatted disk !\n");
259 /* Freed in reverse order of allocation to help malloc'ator */
261 partitions_free(struct partition_t* list)
263 struct partition_t* next;
272 swab32(unsigned long value)
276 __asm__("rlwimi %0,%1,24,16,23\n\t"
277 "rlwimi %0,%1,8,8,15\n\t"
278 "rlwimi %0,%1,24,0,7"
280 : "r" (value), "0" (value >> 24));