2009-11-21 Samuel Thibault <samuel.thibault@ens-lyon.org>
[grub2.git] / partmap / apple.c
blob4dea55a321ce4c16862492745581b24b95e78f23
1 /* apple.c - Read macintosh partition tables. */
2 /*
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>
22 #include <grub/mm.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'. */
32 grub_uint16_t magic;
33 grub_uint16_t blocksize;
36 struct grub_apple_part
38 /* The magic number to identify this as a partition, it should have
39 the value `0x504D'. */
40 grub_uint16_t magic;
42 /* Reserved. */
43 grub_uint16_t reserved;
45 /* The size of the partition map in blocks. */
46 grub_uint32_t partmap_size;
48 /* The first physical block of the partition. */
49 grub_uint32_t first_phys_block;
51 /* The amount of blocks. */
52 grub_uint32_t blockcnt;
54 /* The partition name. */
55 char partname[32];
57 /* The partition type. */
58 char parttype[32];
60 /* The first datablock of the partition. */
61 grub_uint32_t datablocks_first;
63 /* The amount datablocks. */
64 grub_uint32_t datablocks_count;
66 /* The status of the partition. (???) */
67 grub_uint32_t status;
69 /* The first block on which the bootcode can be found. */
70 grub_uint32_t bootcode_pos;
72 /* The size of the bootcode in bytes. */
73 grub_uint32_t bootcode_size;
75 /* The load address of the bootcode. */
76 grub_uint32_t bootcode_loadaddr;
78 /* Reserved. */
79 grub_uint32_t reserved2;
81 /* The entry point of the bootcode. */
82 grub_uint32_t bootcode_entrypoint;
84 /* Reserved. */
85 grub_uint32_t reserved3;
87 /* A checksum of the bootcode. */
88 grub_uint32_t bootcode_checksum;
90 /* The processor type. */
91 char processor[16];
93 /* Padding. */
94 grub_uint16_t pad[187];
97 static struct grub_partition_map grub_apple_partition_map;
100 static grub_err_t
101 apple_partition_map_iterate (grub_disk_t disk,
102 int (*hook) (grub_disk_t disk,
103 const grub_partition_t partition))
105 struct grub_partition part;
106 struct grub_apple_header aheader;
107 struct grub_apple_part apart;
108 struct grub_disk raw;
109 int partno = 0, partnum = 0;
110 unsigned pos;
112 /* Enforce raw disk access. */
113 raw = *disk;
114 raw.partition = 0;
116 part.partmap = &grub_apple_partition_map;
118 if (grub_disk_read (&raw, 0, 0, sizeof (aheader), &aheader))
119 return grub_errno;
121 if (grub_be_to_cpu16 (aheader.magic) != GRUB_APPLE_HEADER_MAGIC)
123 grub_dprintf ("partition",
124 "bad magic (found 0x%x; wanted 0x%x\n",
125 grub_be_to_cpu16 (aheader.magic),
126 GRUB_APPLE_HEADER_MAGIC);
127 goto fail;
130 pos = grub_be_to_cpu16 (aheader.blocksize);
134 if (grub_disk_read (&raw, pos / GRUB_DISK_SECTOR_SIZE,
135 pos % GRUB_DISK_SECTOR_SIZE,
136 sizeof (struct grub_apple_part), &apart))
137 return grub_errno;
139 if (grub_be_to_cpu16 (apart.magic) != GRUB_APPLE_PART_MAGIC)
141 grub_dprintf ("partition",
142 "partition %d: bad magic (found 0x%x; wanted 0x%x\n",
143 partno, grub_be_to_cpu16 (apart.magic),
144 GRUB_APPLE_PART_MAGIC);
145 break;
148 if (partnum == 0)
149 partnum = grub_be_to_cpu32 (apart.partmap_size);
151 part.start = ((grub_disk_addr_t) grub_be_to_cpu32 (apart.first_phys_block)
152 * grub_be_to_cpu16 (aheader.blocksize))
153 / GRUB_DISK_SECTOR_SIZE;
154 part.len = ((grub_disk_addr_t) grub_be_to_cpu32 (apart.blockcnt)
155 * grub_be_to_cpu16 (aheader.blocksize))
156 / GRUB_DISK_SECTOR_SIZE;
157 part.offset = pos;
158 part.index = partno;
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));
166 if (hook (disk, &part))
167 return grub_errno;
169 pos += grub_be_to_cpu16 (aheader.blocksize);
170 partno++;
172 while (partno < partnum);
174 if (partno != 0)
175 return 0;
177 fail:
178 return grub_error (GRUB_ERR_BAD_PART_TABLE,
179 "Apple partition map not found.");
183 static grub_partition_t
184 apple_partition_map_probe (grub_disk_t disk, const char *str)
186 grub_partition_t p = 0;
187 int partnum = 0;
188 char *s = (char *) str;
190 auto int find_func (grub_disk_t d, const grub_partition_t partition);
192 int find_func (grub_disk_t d __attribute__ ((unused)),
193 const grub_partition_t partition)
195 if (partnum == partition->index)
197 p = (grub_partition_t) grub_malloc (sizeof (*p));
198 if (! p)
199 return 1;
201 grub_memcpy (p, partition, sizeof (*p));
202 return 1;
205 return 0;
208 /* Get the partition number. */
209 partnum = grub_strtoul (s, 0, 10) - 1;
210 if (grub_errno)
212 grub_error (GRUB_ERR_BAD_FILENAME, "invalid partition");
213 return 0;
216 if (apple_partition_map_iterate (disk, find_func))
217 goto fail;
219 return p;
221 fail:
222 grub_free (p);
223 return 0;
227 static char *
228 apple_partition_map_get_name (const grub_partition_t p)
230 char *name;
232 name = grub_malloc (13);
233 if (! name)
234 return 0;
236 grub_sprintf (name, "%d", p->index + 1);
237 return name;
241 /* Partition map type. */
242 static struct grub_partition_map grub_apple_partition_map =
244 .name = "part_apple",
245 .iterate = apple_partition_map_iterate,
246 .probe = apple_partition_map_probe,
247 .get_name = apple_partition_map_get_name
250 GRUB_MOD_INIT(apple_partition_map)
252 grub_partition_map_register (&grub_apple_partition_map);
255 GRUB_MOD_FINI(apple_partition_map)
257 grub_partition_map_unregister (&grub_apple_partition_map);