removed accidental commit
[grub2/phcoder.git] / partmap / apple.c
blob16210860bd48fa823993ffac640c71d86bd56c68
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 int partno = 0;
108 unsigned pos = GRUB_DISK_SECTOR_SIZE;
110 part.partmap = &grub_apple_partition_map;
112 if (grub_disk_read (disk, 0, 0, sizeof (aheader), &aheader))
113 return grub_errno;
115 if (grub_be_to_cpu16 (aheader.magic) != GRUB_APPLE_HEADER_MAGIC)
117 grub_dprintf ("partition",
118 "bad magic (found 0x%x; wanted 0x%x\n",
119 grub_be_to_cpu16 (aheader.magic),
120 GRUB_APPLE_HEADER_MAGIC);
121 goto fail;
124 part.data = 0;
126 for (;;)
128 if (grub_disk_read (disk, pos / GRUB_DISK_SECTOR_SIZE,
129 pos % GRUB_DISK_SECTOR_SIZE,
130 sizeof (struct grub_apple_part), &apart))
131 return grub_errno;
133 if (grub_be_to_cpu16 (apart.magic) != GRUB_APPLE_PART_MAGIC)
135 grub_dprintf ("partition",
136 "partition %d: bad magic (found 0x%x; wanted 0x%x\n",
137 partno, grub_be_to_cpu16 (apart.magic),
138 GRUB_APPLE_PART_MAGIC);
139 break;
142 part.start = grub_be_to_cpu32 (apart.first_phys_block);
143 part.len = grub_be_to_cpu32 (apart.blockcnt);
144 part.offset = pos;
145 part.number = partno;
147 grub_dprintf ("partition",
148 "partition %d: name %s, type %s, start 0x%x, len 0x%x\n",
149 partno, apart.partname, apart.parttype,
150 grub_be_to_cpu32 (apart.first_phys_block),
151 grub_be_to_cpu32 (apart.blockcnt));
153 if (hook (disk, &part))
154 return grub_errno;
156 if (grub_be_to_cpu32 (apart.first_phys_block)
157 == GRUB_DISK_SECTOR_SIZE * 2)
158 return 0;
160 pos += sizeof (struct grub_apple_part);
161 partno++;
164 if (pos != GRUB_DISK_SECTOR_SIZE)
165 return 0;
167 fail:
168 return grub_error (GRUB_ERR_BAD_PART_TABLE,
169 "Apple partition map not found.");
173 /* Partition map type. */
174 static struct grub_partition_map grub_apple_partition_map =
176 .name = "apple_partition_map",
177 .iterate = apple_partition_map_iterate,
180 GRUB_MOD_INIT(apple_partition_map)
182 grub_partition_map_register (&grub_apple_partition_map);
185 GRUB_MOD_FINI(apple_partition_map)
187 grub_partition_map_unregister (&grub_apple_partition_map);