]> git.proxmox.com Git - grub2.git/blob - grub-core/partmap/apple.c
Import grub2_2.02+dfsg1.orig.tar.xz
[grub2.git] / grub-core / partmap / apple.c
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 GRUB_MOD_LICENSE ("GPLv3+");
26
27 #define GRUB_APPLE_HEADER_MAGIC 0x4552
28 #define GRUB_APPLE_PART_MAGIC 0x504D
29
30 struct grub_apple_header
31 {
32 /* The magic number to identify the partition map, it should have
33 the value `0x4552'. */
34 grub_uint16_t magic;
35 grub_uint16_t blocksize;
36 };
37
38 struct grub_apple_part
39 {
40 /* The magic number to identify this as a partition, it should have
41 the value `0x504D'. */
42 grub_uint16_t magic;
43
44 /* Reserved. */
45 grub_uint16_t reserved;
46
47 /* The size of the partition map in blocks. */
48 grub_uint32_t partmap_size;
49
50 /* The first physical block of the partition. */
51 grub_uint32_t first_phys_block;
52
53 /* The amount of blocks. */
54 grub_uint32_t blockcnt;
55
56 /* The partition name. */
57 char partname[32];
58
59 /* The partition type. */
60 char parttype[32];
61
62 /* The first datablock of the partition. */
63 grub_uint32_t datablocks_first;
64
65 /* The amount datablocks. */
66 grub_uint32_t datablocks_count;
67
68 /* The status of the partition. (???) */
69 grub_uint32_t status;
70
71 /* The first block on which the bootcode can be found. */
72 grub_uint32_t bootcode_pos;
73
74 /* The size of the bootcode in bytes. */
75 grub_uint32_t bootcode_size;
76
77 /* The load address of the bootcode. */
78 grub_uint32_t bootcode_loadaddr;
79
80 /* Reserved. */
81 grub_uint32_t reserved2;
82
83 /* The entry point of the bootcode. */
84 grub_uint32_t bootcode_entrypoint;
85
86 /* Reserved. */
87 grub_uint32_t reserved3;
88
89 /* A checksum of the bootcode. */
90 grub_uint32_t bootcode_checksum;
91
92 /* The processor type. */
93 char processor[16];
94
95 /* Padding. */
96 grub_uint16_t pad[187];
97 };
98
99 static struct grub_partition_map grub_apple_partition_map;
100 \f
101
102 static grub_err_t
103 apple_partition_map_iterate (grub_disk_t disk,
104 grub_partition_iterate_hook_t hook,
105 void *hook_data)
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, hook_data))
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 \f
183 /* Partition map type. */
184 static struct grub_partition_map grub_apple_partition_map =
185 {
186 .name = "apple",
187 .iterate = apple_partition_map_iterate,
188 };
189
190 GRUB_MOD_INIT(part_apple)
191 {
192 grub_partition_map_register (&grub_apple_partition_map);
193 }
194
195 GRUB_MOD_FINI(part_apple)
196 {
197 grub_partition_map_unregister (&grub_apple_partition_map);
198 }
199