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'. */
35 struct grub_apple_part
37 /* The magic number to identify this as a partition, it should have
38 the value `0x504D'. */
42 grub_uint16_t reserved
;
44 /* The size of the partition map in blocks. */
45 grub_uint32_t partmap_size
;
47 /* The first physical block of the partition. */
48 grub_uint32_t first_phys_block
;
50 /* The amount of blocks. */
51 grub_uint32_t blockcnt
;
53 /* The partition name. */
56 /* The partition type. */
59 /* The first datablock of the partition. */
60 grub_uint32_t datablocks_first
;
62 /* The amount datablocks. */
63 grub_uint32_t datablocks_count
;
65 /* The status of the partition. (???) */
68 /* The first block on which the bootcode can be found. */
69 grub_uint32_t bootcode_pos
;
71 /* The size of the bootcode in bytes. */
72 grub_uint32_t bootcode_size
;
74 /* The load address of the bootcode. */
75 grub_uint32_t bootcode_loadaddr
;
78 grub_uint32_t reserved2
;
80 /* The entry point of the bootcode. */
81 grub_uint32_t bootcode_entrypoint
;
84 grub_uint32_t reserved3
;
86 /* A checksum of the bootcode. */
87 grub_uint32_t bootcode_checksum
;
89 /* The processor type. */
93 grub_uint16_t pad
[187];
96 static struct grub_partition_map grub_apple_partition_map
;
100 apple_partition_map_iterate (grub_disk_t disk
,
101 int (*hook
) (grub_disk_t disk
,
102 const grub_partition_t partition
))
104 struct grub_partition part
;
105 struct grub_apple_header aheader
;
106 struct grub_apple_part apart
;
107 struct grub_disk raw
;
109 unsigned pos
= GRUB_DISK_SECTOR_SIZE
;
111 /* Enforce raw disk access. */
115 part
.partmap
= &grub_apple_partition_map
;
117 if (grub_disk_read (&raw
, 0, 0, sizeof (aheader
), &aheader
))
120 if (grub_be_to_cpu16 (aheader
.magic
) != GRUB_APPLE_HEADER_MAGIC
)
122 grub_dprintf ("partition",
123 "bad magic (found 0x%x; wanted 0x%x\n",
124 grub_be_to_cpu16 (aheader
.magic
),
125 GRUB_APPLE_HEADER_MAGIC
);
131 if (grub_disk_read (&raw
, pos
/ GRUB_DISK_SECTOR_SIZE
,
132 pos
% GRUB_DISK_SECTOR_SIZE
,
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
);
145 part
.start
= grub_be_to_cpu32 (apart
.first_phys_block
);
146 part
.len
= grub_be_to_cpu32 (apart
.blockcnt
);
150 grub_dprintf ("partition",
151 "partition %d: name %s, type %s, start 0x%x, len 0x%x\n",
152 partno
, apart
.partname
, apart
.parttype
,
153 grub_be_to_cpu32 (apart
.first_phys_block
),
154 grub_be_to_cpu32 (apart
.blockcnt
));
156 if (hook (disk
, &part
))
159 if (grub_be_to_cpu32 (apart
.first_phys_block
)
160 == GRUB_DISK_SECTOR_SIZE
* 2)
163 pos
+= sizeof (struct grub_apple_part
);
167 if (pos
!= GRUB_DISK_SECTOR_SIZE
)
171 return grub_error (GRUB_ERR_BAD_PART_TABLE
,
172 "Apple partition map not found.");
176 static grub_partition_t
177 apple_partition_map_probe (grub_disk_t disk
, const char *str
)
179 grub_partition_t p
= 0;
181 char *s
= (char *) str
;
183 auto int find_func (grub_disk_t d
, const grub_partition_t partition
);
185 int find_func (grub_disk_t d
__attribute__ ((unused
)),
186 const grub_partition_t partition
)
188 if (partnum
== partition
->index
)
190 p
= (grub_partition_t
) grub_malloc (sizeof (*p
));
194 grub_memcpy (p
, partition
, sizeof (*p
));
201 /* Get the partition number. */
202 partnum
= grub_strtoul (s
, 0, 10) - 1;
205 grub_error (GRUB_ERR_BAD_FILENAME
, "invalid partition");
209 if (apple_partition_map_iterate (disk
, find_func
))
221 apple_partition_map_get_name (const grub_partition_t p
)
225 name
= grub_malloc (13);
229 grub_sprintf (name
, "%d", p
->index
+ 1);
234 /* Partition map type. */
235 static struct grub_partition_map grub_apple_partition_map
=
237 .name
= "apple_partition_map",
238 .iterate
= apple_partition_map_iterate
,
239 .probe
= apple_partition_map_probe
,
240 .get_name
= apple_partition_map_get_name
243 GRUB_MOD_INIT(apple_partition_map
)
245 grub_partition_map_register (&grub_apple_partition_map
);
248 GRUB_MOD_FINI(apple_partition_map
)
250 grub_partition_map_unregister (&grub_apple_partition_map
);