1
/* apple.c - Read macintosh partition tables. */
3
* GRUB -- GRand Unified Bootloader
4
* Copyright (C) 2002,2004,2005,2006,2007,2008,2009 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
int partno = 0, partnum = 0;
111
part.partmap = &grub_apple_partition_map;
113
if (grub_disk_read (disk, 0, 0, sizeof (aheader), &aheader))
116
if (grub_be_to_cpu16 (aheader.magic) != GRUB_APPLE_HEADER_MAGIC)
118
grub_dprintf ("partition",
119
"bad magic (found 0x%x; wanted 0x%x\n",
120
grub_be_to_cpu16 (aheader.magic),
121
GRUB_APPLE_HEADER_MAGIC);
125
pos = grub_be_to_cpu16 (aheader.blocksize);
129
part.offset = pos / GRUB_DISK_SECTOR_SIZE;
130
part.index = pos % GRUB_DISK_SECTOR_SIZE;
132
if (grub_disk_read (disk, part.offset, part.index,
133
sizeof (struct grub_apple_part), &apart))
136
if (grub_be_to_cpu16 (apart.magic) != GRUB_APPLE_PART_MAGIC)
138
grub_dprintf ("partition",
139
"partition %d: bad magic (found 0x%x; wanted 0x%x\n",
140
partno, grub_be_to_cpu16 (apart.magic),
141
GRUB_APPLE_PART_MAGIC);
146
partnum = grub_be_to_cpu32 (apart.partmap_size);
148
part.start = ((grub_disk_addr_t) grub_be_to_cpu32 (apart.first_phys_block)
149
* grub_be_to_cpu16 (aheader.blocksize))
150
/ GRUB_DISK_SECTOR_SIZE;
151
part.len = ((grub_disk_addr_t) grub_be_to_cpu32 (apart.blockcnt)
152
* grub_be_to_cpu16 (aheader.blocksize))
153
/ GRUB_DISK_SECTOR_SIZE;
156
part.number = partno;
158
grub_dprintf ("partition",
159
"partition %d: name %s, type %s, start 0x%x, len 0x%x\n",
160
partno, apart.partname, apart.parttype,
161
grub_be_to_cpu32 (apart.first_phys_block),
162
grub_be_to_cpu32 (apart.blockcnt));
164
if (hook (disk, &part))
167
pos += grub_be_to_cpu16 (aheader.blocksize);
170
while (partno < partnum);
176
return grub_error (GRUB_ERR_BAD_PART_TABLE,
177
"Apple partition map not found");
181
/* Partition map type. */
182
static struct grub_partition_map grub_apple_partition_map =
185
.iterate = apple_partition_map_iterate,
188
GRUB_MOD_INIT(part_apple)
190
grub_partition_map_register (&grub_apple_partition_map);
193
GRUB_MOD_FINI(part_apple)
195
grub_partition_map_unregister (&grub_apple_partition_map);