Updated PCI IDs to latest snapshot.
[tangerine.git] / arch / common / boot / grub2 / partmap / apple.c
blobd6ddc0b3fd0f04bf4217b64ef9bd3437bc68ce5c
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 entrypoint 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;
98 #ifndef GRUB_UTIL
99 static grub_dl_t my_mod;
100 #endif
103 static grub_err_t
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;
112 int partno = 0;
113 unsigned pos = GRUB_DISK_SECTOR_SIZE;
115 /* Enforce raw disk access. */
116 raw = *disk;
117 raw.partition = 0;
119 part.partmap = &grub_apple_partition_map;
121 if (grub_disk_read (&raw, 0, 0, sizeof (aheader), (char *) &aheader))
122 return grub_errno;
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);
130 goto fail;
133 for (;;)
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))
138 return grub_errno;
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);
146 break;
149 part.start = grub_be_to_cpu32 (apart.first_phys_block);
150 part.len = grub_be_to_cpu32 (apart.blockcnt);
151 part.offset = pos;
152 part.index = partno;
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))
161 return grub_errno;
163 if (grub_be_to_cpu32 (apart.first_phys_block)
164 == GRUB_DISK_SECTOR_SIZE * 2)
165 return 0;
167 pos += sizeof (struct grub_apple_part);
168 partno++;
171 if (pos != GRUB_DISK_SECTOR_SIZE)
172 return 0;
174 fail:
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;
184 int partnum = 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));
195 if (! p)
196 return 1;
198 grub_memcpy (p, partition, sizeof (*p));
199 return 1;
202 return 0;
205 /* Get the partition number. */
206 partnum = grub_strtoul (s, 0, 10) - 1;
207 if (grub_errno)
209 grub_error (GRUB_ERR_BAD_FILENAME, "invalid partition");
210 return 0;
213 if (apple_partition_map_iterate (disk, find_func))
214 goto fail;
216 return p;
218 fail:
219 grub_free (p);
220 return 0;
224 static char *
225 apple_partition_map_get_name (const grub_partition_t p)
227 char *name;
229 name = grub_malloc (13);
230 if (! name)
231 return 0;
233 grub_sprintf (name, "%d", p->index + 1);
234 return name;
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);
250 #ifndef GRUB_UTIL
251 my_mod = mod;
252 #endif
255 GRUB_MOD_FINI(apple_partition_map)
257 grub_partition_map_unregister (&grub_apple_partition_map);