1 /* apple.c - Read macintosh partition tables. */
3 * GRUB -- GRand Unified Bootloader
4 * Copyright (C) 2002,2004,2005,2006,2007,2008 Free Software Foundation, Inc.
6 * GRUB is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * GRUB is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
20 #include <grub/disk.h>
21 #include <grub/misc.h>
23 #include <grub/partition.h>
25 #define GRUB_APPLE_HEADER_MAGIC 0x4552
26 #define GRUB_APPLE_PART_MAGIC 0x504D
28 struct grub_apple_header
30 /* The magic number to identify the partition map, it should have
31 the value `0x4552'. */
33 grub_uint16_t blocksize
;
36 struct grub_apple_part
38 /* The magic number to identify this as a partition, it should have
39 the value `0x504D'. */
43 grub_uint16_t reserved
;
45 /* The size of the partition map in blocks. */
46 grub_uint32_t partmap_size
;
48 /* The first physical block of the partition. */
49 grub_uint32_t first_phys_block
;
51 /* The amount of blocks. */
52 grub_uint32_t blockcnt
;
54 /* The partition name. */
57 /* The partition type. */
60 /* The first datablock of the partition. */
61 grub_uint32_t datablocks_first
;
63 /* The amount datablocks. */
64 grub_uint32_t datablocks_count
;
66 /* The status of the partition. (???) */
69 /* The first block on which the bootcode can be found. */
70 grub_uint32_t bootcode_pos
;
72 /* The size of the bootcode in bytes. */
73 grub_uint32_t bootcode_size
;
75 /* The load address of the bootcode. */
76 grub_uint32_t bootcode_loadaddr
;
79 grub_uint32_t reserved2
;
81 /* The entry point of the bootcode. */
82 grub_uint32_t bootcode_entrypoint
;
85 grub_uint32_t reserved3
;
87 /* A checksum of the bootcode. */
88 grub_uint32_t bootcode_checksum
;
90 /* The processor type. */
94 grub_uint16_t pad
[187];
97 static struct grub_partition_map grub_apple_partition_map
;
101 apple_partition_map_iterate (grub_disk_t disk
,
102 int (*hook
) (grub_disk_t disk
,
103 const grub_partition_t partition
))
105 struct grub_partition part
;
106 struct grub_apple_header aheader
;
107 struct grub_apple_part apart
;
108 struct grub_disk raw
;
109 int partno
= 0, partnum
= 0;
112 /* Enforce raw disk access. */
116 part
.partmap
= &grub_apple_partition_map
;
118 if (grub_disk_read (&raw
, 0, 0, sizeof (aheader
), &aheader
))
121 if (grub_be_to_cpu16 (aheader
.magic
) != GRUB_APPLE_HEADER_MAGIC
)
123 grub_dprintf ("partition",
124 "bad magic (found 0x%x; wanted 0x%x\n",
125 grub_be_to_cpu16 (aheader
.magic
),
126 GRUB_APPLE_HEADER_MAGIC
);
130 pos
= grub_be_to_cpu16 (aheader
.blocksize
);
134 if (grub_disk_read (&raw
, pos
/ GRUB_DISK_SECTOR_SIZE
,
135 pos
% GRUB_DISK_SECTOR_SIZE
,
136 sizeof (struct grub_apple_part
), &apart
))
139 if (grub_be_to_cpu16 (apart
.magic
) != GRUB_APPLE_PART_MAGIC
)
141 grub_dprintf ("partition",
142 "partition %d: bad magic (found 0x%x; wanted 0x%x\n",
143 partno
, grub_be_to_cpu16 (apart
.magic
),
144 GRUB_APPLE_PART_MAGIC
);
149 partnum
= grub_be_to_cpu32 (apart
.partmap_size
);
151 part
.start
= ((grub_disk_addr_t
) grub_be_to_cpu32 (apart
.first_phys_block
)
152 * grub_be_to_cpu16 (aheader
.blocksize
))
153 / GRUB_DISK_SECTOR_SIZE
;
154 part
.len
= ((grub_disk_addr_t
) grub_be_to_cpu32 (apart
.blockcnt
)
155 * grub_be_to_cpu16 (aheader
.blocksize
))
156 / GRUB_DISK_SECTOR_SIZE
;
160 grub_dprintf ("partition",
161 "partition %d: name %s, type %s, start 0x%x, len 0x%x\n",
162 partno
, apart
.partname
, apart
.parttype
,
163 grub_be_to_cpu32 (apart
.first_phys_block
),
164 grub_be_to_cpu32 (apart
.blockcnt
));
166 if (hook (disk
, &part
))
169 pos
+= grub_be_to_cpu16 (aheader
.blocksize
);
172 while (partno
< partnum
);
178 return grub_error (GRUB_ERR_BAD_PART_TABLE
,
179 "Apple partition map not found.");
183 static grub_partition_t
184 apple_partition_map_probe (grub_disk_t disk
, const char *str
)
186 grub_partition_t p
= 0;
188 char *s
= (char *) str
;
190 auto int find_func (grub_disk_t d
, const grub_partition_t partition
);
192 int find_func (grub_disk_t d
__attribute__ ((unused
)),
193 const grub_partition_t partition
)
195 if (partnum
== partition
->index
)
197 p
= (grub_partition_t
) grub_malloc (sizeof (*p
));
201 grub_memcpy (p
, partition
, sizeof (*p
));
208 /* Get the partition number. */
209 partnum
= grub_strtoul (s
, 0, 10) - 1;
212 grub_error (GRUB_ERR_BAD_FILENAME
, "invalid partition");
216 if (apple_partition_map_iterate (disk
, find_func
))
228 apple_partition_map_get_name (const grub_partition_t p
)
232 name
= grub_malloc (13);
236 grub_sprintf (name
, "%d", p
->index
+ 1);
241 /* Partition map type. */
242 static struct grub_partition_map grub_apple_partition_map
=
244 .name
= "part_apple",
245 .iterate
= apple_partition_map_iterate
,
246 .probe
= apple_partition_map_probe
,
247 .get_name
= apple_partition_map_get_name
250 GRUB_MOD_INIT(apple_partition_map
)
252 grub_partition_map_register (&grub_apple_partition_map
);
255 GRUB_MOD_FINI(apple_partition_map
)
257 grub_partition_map_unregister (&grub_apple_partition_map
);