1
/* apple.c - Read macintosh partition tables. */
3
* GRUB -- GRand Unified Bootloader
4
* Copyright (C) 2002, 2004, 2005 Free Software Foundation, Inc.
6
* This program 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 2 of the License, or
9
* (at your option) any later version.
11
* This program 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 this program; if not, write to the Free Software
18
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21
#include <grub/disk.h>
22
#include <grub/misc.h>
24
#include <grub/partition.h>
26
#define GRUB_APPLE_PART_MAGIC 0x504D
28
struct grub_apple_part
30
/* The magic number to idenify this as a partition, it should have
31
the value `0x504D'. */
35
grub_uint16_t reserved;
37
/* The size of the partition map in blocks. */
38
grub_uint32_t partmap_size;
40
/* The first physical block of the partition. */
41
grub_uint32_t first_phys_block;
43
/* The amount of blocks. */
44
grub_uint32_t blockcnt;
46
/* The partition name. */
49
/* The partition type. */
52
/* The first datablock of the partition. */
53
grub_uint32_t datablocks_first;
55
/* The amount datablocks. */
56
grub_uint32_t datablocks_count;
58
/* The status of the partition. (???) */
61
/* The first block on which the bootcode can be found. */
62
grub_uint32_t bootcode_pos;
64
/* The size of the bootcode in bytes. */
65
grub_uint32_t bootcode_size;
67
/* The load address of the bootcode. */
68
grub_uint32_t bootcode_loadaddr;
71
grub_uint32_t reserved2;
73
/* The entrypoint of the bootcode. */
74
grub_uint32_t bootcode_entrypoint;
77
grub_uint32_t reserved3;
79
/* A checksum of the bootcode. */
80
grub_uint32_t bootcode_checksum;
82
/* The processor type. */
86
grub_uint16_t pad[187];
89
static struct grub_partition_map grub_apple_partition_map;
92
static grub_dl_t my_mod;
97
apple_partition_map_iterate (grub_disk_t disk,
98
int (*hook) (grub_disk_t disk,
99
const grub_partition_t partition))
101
struct grub_partition part;
102
struct grub_apple_part apart;
103
struct grub_disk raw;
105
int pos = GRUB_DISK_SECTOR_SIZE;
107
/* Enforce raw disk access. */
111
part.partmap = &grub_apple_partition_map;
115
if (grub_disk_read (&raw, pos / GRUB_DISK_SECTOR_SIZE,
116
pos % GRUB_DISK_SECTOR_SIZE,
117
sizeof (struct grub_apple_part), (char *) &apart))
120
if (apart.magic != GRUB_APPLE_PART_MAGIC)
122
grub_dprintf ("partition",
123
"partition %d: bad magic (found 0x%x; wanted 0x%x\n",
124
partno, apart.magic, GRUB_APPLE_PART_MAGIC);
128
part.start = apart.first_phys_block;
129
part.len = apart.blockcnt;
133
grub_dprintf ("partition",
134
"partition %d: name %s, type %s, start 0x%x, len 0x%x\n",
135
partno, apart.partname, apart.parttype,
136
apart.first_phys_block, apart.blockcnt);
138
if (hook (disk, &part))
141
if (apart.first_phys_block == GRUB_DISK_SECTOR_SIZE * 2)
144
pos += sizeof (struct grub_apple_part);
148
if (pos == GRUB_DISK_SECTOR_SIZE)
149
return grub_error (GRUB_ERR_BAD_PART_TABLE,
150
"Apple partition map not found.");
156
static grub_partition_t
157
apple_partition_map_probe (grub_disk_t disk, const char *str)
159
grub_partition_t p = 0;
161
char *s = (char *) str;
163
auto int find_func (grub_disk_t d, const grub_partition_t partition);
165
int find_func (grub_disk_t d __attribute__ ((unused)),
166
const grub_partition_t partition)
168
if (partnum == partition->index)
170
p = (grub_partition_t) grub_malloc (sizeof (*p));
174
grub_memcpy (p, partition, sizeof (*p));
181
/* Get the partition number. */
182
partnum = grub_strtoul (s, 0, 10);
185
grub_error (GRUB_ERR_BAD_FILENAME, "invalid partition");
189
if (apple_partition_map_iterate (disk, find_func))
201
apple_partition_map_get_name (const grub_partition_t p)
205
name = grub_malloc (13);
209
grub_sprintf (name, "%d", p->index);
214
/* Partition map type. */
215
static struct grub_partition_map grub_apple_partition_map =
217
.name = "apple_partition_map",
218
.iterate = apple_partition_map_iterate,
219
.probe = apple_partition_map_probe,
220
.get_name = apple_partition_map_get_name
223
GRUB_MOD_INIT(apple_partition_map)
225
grub_partition_map_register (&grub_apple_partition_map);
231
GRUB_MOD_FINI(apple_partition_map)
233
grub_partition_map_unregister (&grub_apple_partition_map);