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 entrypoint 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
;
99 static grub_dl_t my_mod
;
104 apple_partition_map_iterate (grub_disk_t disk
,
105 int (*hook
) (grub_disk_t disk
,
106 const grub_partition_t partition
))
108 struct grub_partition part
;
109 struct grub_apple_header aheader
;
110 struct grub_apple_part apart
;
111 struct grub_disk raw
;
113 unsigned pos
= GRUB_DISK_SECTOR_SIZE
;
115 /* Enforce raw disk access. */
119 part
.partmap
= &grub_apple_partition_map
;
121 if (grub_disk_read (&raw
, 0, 0, sizeof (aheader
), (char *) &aheader
))
124 if (grub_be_to_cpu16 (aheader
.magic
) != GRUB_APPLE_HEADER_MAGIC
)
126 grub_dprintf ("partition",
127 "bad magic (found 0x%x; wanted 0x%x\n",
128 grub_be_to_cpu16 (aheader
.magic
),
129 GRUB_APPLE_HEADER_MAGIC
);
135 if (grub_disk_read (&raw
, pos
/ GRUB_DISK_SECTOR_SIZE
,
136 pos
% GRUB_DISK_SECTOR_SIZE
,
137 sizeof (struct grub_apple_part
), (char *) &apart
))
140 if (grub_be_to_cpu16 (apart
.magic
) != GRUB_APPLE_PART_MAGIC
)
142 grub_dprintf ("partition",
143 "partition %d: bad magic (found 0x%x; wanted 0x%x\n",
144 partno
, grub_be_to_cpu16 (apart
.magic
),
145 GRUB_APPLE_PART_MAGIC
);
149 part
.start
= grub_be_to_cpu32 (apart
.first_phys_block
);
150 part
.len
= grub_be_to_cpu32 (apart
.blockcnt
);
154 grub_dprintf ("partition",
155 "partition %d: name %s, type %s, start 0x%x, len 0x%x\n",
156 partno
, apart
.partname
, apart
.parttype
,
157 grub_be_to_cpu32 (apart
.first_phys_block
),
158 grub_be_to_cpu32 (apart
.blockcnt
));
160 if (hook (disk
, &part
))
163 if (grub_be_to_cpu32 (apart
.first_phys_block
)
164 == GRUB_DISK_SECTOR_SIZE
* 2)
167 pos
+= sizeof (struct grub_apple_part
);
171 if (pos
!= GRUB_DISK_SECTOR_SIZE
)
175 return grub_error (GRUB_ERR_BAD_PART_TABLE
,
176 "Apple partition map not found.");
180 static grub_partition_t
181 apple_partition_map_probe (grub_disk_t disk
, const char *str
)
183 grub_partition_t p
= 0;
185 char *s
= (char *) str
;
187 auto int find_func (grub_disk_t d
, const grub_partition_t partition
);
189 int find_func (grub_disk_t d
__attribute__ ((unused
)),
190 const grub_partition_t partition
)
192 if (partnum
== partition
->index
)
194 p
= (grub_partition_t
) grub_malloc (sizeof (*p
));
198 grub_memcpy (p
, partition
, sizeof (*p
));
205 /* Get the partition number. */
206 partnum
= grub_strtoul (s
, 0, 10) - 1;
209 grub_error (GRUB_ERR_BAD_FILENAME
, "invalid partition");
213 if (apple_partition_map_iterate (disk
, find_func
))
225 apple_partition_map_get_name (const grub_partition_t p
)
229 name
= grub_malloc (13);
233 grub_sprintf (name
, "%d", p
->index
+ 1);
238 /* Partition map type. */
239 static struct grub_partition_map grub_apple_partition_map
=
241 .name
= "apple_partition_map",
242 .iterate
= apple_partition_map_iterate
,
243 .probe
= apple_partition_map_probe
,
244 .get_name
= apple_partition_map_get_name
247 GRUB_MOD_INIT(apple_partition_map
)
249 grub_partition_map_register (&grub_apple_partition_map
);
255 GRUB_MOD_FINI(apple_partition_map
)
257 grub_partition_map_unregister (&grub_apple_partition_map
);