2009-07-05 Pavel Roskin <proski@gnu.org>
[grub2/bean.git] / partmap / apple.c
blobfce2f2c5fe5e08c2b3bda7cb878cbd915e4bc299
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;
35 struct grub_apple_part
37 /* The magic number to identify this as a partition, it should have
38 the value `0x504D'. */
39 grub_uint16_t magic;
41 /* Reserved. */
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. */
54 char partname[32];
56 /* The partition type. */
57 char parttype[32];
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. (???) */
66 grub_uint32_t status;
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;
77 /* Reserved. */
78 grub_uint32_t reserved2;
80 /* The entry point of the bootcode. */
81 grub_uint32_t bootcode_entrypoint;
83 /* Reserved. */
84 grub_uint32_t reserved3;
86 /* A checksum of the bootcode. */
87 grub_uint32_t bootcode_checksum;
89 /* The processor type. */
90 char processor[16];
92 /* Padding. */
93 grub_uint16_t pad[187];
96 static struct grub_partition_map grub_apple_partition_map;
99 static grub_err_t
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;
108 int partno = 0;
109 unsigned pos = GRUB_DISK_SECTOR_SIZE;
111 /* Enforce raw disk access. */
112 raw = *disk;
113 raw.partition = 0;
115 part.partmap = &grub_apple_partition_map;
117 if (grub_disk_read (&raw, 0, 0, sizeof (aheader), &aheader))
118 return grub_errno;
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);
126 goto fail;
129 for (;;)
131 if (grub_disk_read (&raw, pos / GRUB_DISK_SECTOR_SIZE,
132 pos % GRUB_DISK_SECTOR_SIZE,
133 sizeof (struct grub_apple_part), &apart))
134 return grub_errno;
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);
142 break;
145 part.start = grub_be_to_cpu32 (apart.first_phys_block);
146 part.len = grub_be_to_cpu32 (apart.blockcnt);
147 part.offset = pos;
148 part.index = partno;
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))
157 return grub_errno;
159 if (grub_be_to_cpu32 (apart.first_phys_block)
160 == GRUB_DISK_SECTOR_SIZE * 2)
161 return 0;
163 pos += sizeof (struct grub_apple_part);
164 partno++;
167 if (pos != GRUB_DISK_SECTOR_SIZE)
168 return 0;
170 fail:
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;
180 int partnum = 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));
191 if (! p)
192 return 1;
194 grub_memcpy (p, partition, sizeof (*p));
195 return 1;
198 return 0;
201 /* Get the partition number. */
202 partnum = grub_strtoul (s, 0, 10) - 1;
203 if (grub_errno)
205 grub_error (GRUB_ERR_BAD_FILENAME, "invalid partition");
206 return 0;
209 if (apple_partition_map_iterate (disk, find_func))
210 goto fail;
212 return p;
214 fail:
215 grub_free (p);
216 return 0;
220 static char *
221 apple_partition_map_get_name (const grub_partition_t p)
223 char *name;
225 name = grub_malloc (13);
226 if (! name)
227 return 0;
229 grub_sprintf (name, "%d", p->index + 1);
230 return name;
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);