1 /* apple.c - Read macintosh partition tables. */
2 /*
3 * GRUB -- GRand Unified Bootloader
4 * Copyright (C) 2002,2004,2005,2006,2007,2008,2009 Free Software Foundation, Inc.
5 *
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.
10 *
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.
15 *
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/>.
18 */
19
20 #include <grub/disk.h>
21 #include <grub/misc.h>
22 #include <grub/mm.h>
23 #include <grub/partition.h>
24
25 #define GRUB_APPLE_HEADER_MAGIC 0x4552
26 #define GRUB_APPLE_PART_MAGIC 0x504D
27
28 struct grub_apple_header
29 {
30 /* The magic number to identify the partition map, it should have
31 the value `0x4552'. */
32 grub_uint16_t magic;
33 grub_uint16_t blocksize;
34 };
35
36 struct grub_apple_part
37 {
38 /* The magic number to identify this as a partition, it should have
39 the value `0x504D'. */
40 grub_uint16_t magic;
41
42 /* Reserved. */
43 grub_uint16_t reserved;
44
45 /* The size of the partition map in blocks. */
46 grub_uint32_t partmap_size;
47
48 /* The first physical block of the partition. */
49 grub_uint32_t first_phys_block;
50
51 /* The amount of blocks. */
52 grub_uint32_t blockcnt;
53
54 /* The partition name. */
55 char partname[32];
56
57 /* The partition type. */
58 char parttype[32];
59
60 /* The first datablock of the partition. */
61 grub_uint32_t datablocks_first;
62
63 /* The amount datablocks. */
64 grub_uint32_t datablocks_count;
65
66 /* The status of the partition. (???) */
67 grub_uint32_t status;
68
69 /* The first block on which the bootcode can be found. */
70 grub_uint32_t bootcode_pos;
71
72 /* The size of the bootcode in bytes. */
73 grub_uint32_t bootcode_size;
74
75 /* The load address of the bootcode. */
76 grub_uint32_t bootcode_loadaddr;
77
78 /* Reserved. */
79 grub_uint32_t reserved2;
80
81 /* The entry point of the bootcode. */
82 grub_uint32_t bootcode_entrypoint;
83
84 /* Reserved. */
85 grub_uint32_t reserved3;
86
87 /* A checksum of the bootcode. */
88 grub_uint32_t bootcode_checksum;
89
90 /* The processor type. */
91 char processor[16];
92
93 /* Padding. */
94 grub_uint16_t pad[187];
95 };
96
97 struct grub_partition_map grub_apple_partition_map;
98
99
100 static grub_err_t
apple_partition_map_iterate(grub_disk_t disk,int (* hook)(grub_disk_t disk,const grub_partition_t partition,void * closure),void * closure)101 apple_partition_map_iterate (grub_disk_t disk,
102 int (*hook) (grub_disk_t disk,
103 const grub_partition_t partition,
104 void *closure),
105 void *closure)
106 {
107 struct grub_partition part;
108 struct grub_apple_header aheader;
109 struct grub_apple_part apart;
110 int partno = 0, partnum = 0;
111 unsigned pos;
112
113 part.partmap = &grub_apple_partition_map;
114
115 if (grub_disk_read (disk, 0, 0, sizeof (aheader), &aheader))
116 return grub_errno;
117
118 if (grub_be_to_cpu16 (aheader.magic) != GRUB_APPLE_HEADER_MAGIC)
119 {
120 grub_dprintf ("partition",
121 "bad magic (found 0x%x; wanted 0x%x\n",
122 grub_be_to_cpu16 (aheader.magic),
123 GRUB_APPLE_HEADER_MAGIC);
124 goto fail;
125 }
126
127 pos = grub_be_to_cpu16 (aheader.blocksize);
128
129 do
130 {
131 part.offset = pos / GRUB_DISK_SECTOR_SIZE;
132 part.index = pos % GRUB_DISK_SECTOR_SIZE;
133
134 if (grub_disk_read (disk, part.offset, part.index,
135 sizeof (struct grub_apple_part), &apart))
136 return grub_errno;
137
138 if (grub_be_to_cpu16 (apart.magic) != GRUB_APPLE_PART_MAGIC)
139 {
140 grub_dprintf ("partition",
141 "partition %d: bad magic (found 0x%x; wanted 0x%x\n",
142 partno, grub_be_to_cpu16 (apart.magic),
143 GRUB_APPLE_PART_MAGIC);
144 break;
145 }
146
147 if (partnum == 0)
148 partnum = grub_be_to_cpu32 (apart.partmap_size);
149
150 part.start = ((grub_disk_addr_t) grub_be_to_cpu32 (apart.first_phys_block)
151 * grub_be_to_cpu16 (aheader.blocksize))
152 / GRUB_DISK_SECTOR_SIZE;
153 part.len = ((grub_disk_addr_t) grub_be_to_cpu32 (apart.blockcnt)
154 * grub_be_to_cpu16 (aheader.blocksize))
155 / GRUB_DISK_SECTOR_SIZE;
156 part.offset = pos;
157 part.index = partno;
158 part.number = partno;
159
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));
165
166 if (hook (disk, &part, closure))
167 return grub_errno;
168
169 pos += grub_be_to_cpu16 (aheader.blocksize);
170 partno++;
171 }
172 while (partno < partnum);
173
174 if (partno != 0)
175 return 0;
176
177 fail:
178 return grub_error (GRUB_ERR_BAD_PART_TABLE,
179 "Apple partition map not found");
180 }
181
182
183 /* Partition map type. */
184 struct grub_partition_map grub_apple_partition_map =
185 {
186 .name = "apple",
187 .iterate = apple_partition_map_iterate,
188 };
189