Indentation fix, cleanup.
[AROS.git] / arch / all-pc / boot / grub2-aros / grub-core / partmap / apple.c
blobc3ead0fcf4f7e942fe55d6d59fde1a75c18a1896
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.
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 GRUB_MOD_LICENSE ("GPLv3+");
27 #define GRUB_APPLE_HEADER_MAGIC 0x4552
28 #define GRUB_APPLE_PART_MAGIC 0x504D
30 struct grub_apple_header
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;
38 struct grub_apple_part
40 /* The magic number to identify this as a partition, it should have
41 the value `0x504D'. */
42 grub_uint16_t magic;
44 /* Reserved. */
45 grub_uint16_t reserved;
47 /* The size of the partition map in blocks. */
48 grub_uint32_t partmap_size;
50 /* The first physical block of the partition. */
51 grub_uint32_t first_phys_block;
53 /* The amount of blocks. */
54 grub_uint32_t blockcnt;
56 /* The partition name. */
57 char partname[32];
59 /* The partition type. */
60 char parttype[32];
62 /* The first datablock of the partition. */
63 grub_uint32_t datablocks_first;
65 /* The amount datablocks. */
66 grub_uint32_t datablocks_count;
68 /* The status of the partition. (???) */
69 grub_uint32_t status;
71 /* The first block on which the bootcode can be found. */
72 grub_uint32_t bootcode_pos;
74 /* The size of the bootcode in bytes. */
75 grub_uint32_t bootcode_size;
77 /* The load address of the bootcode. */
78 grub_uint32_t bootcode_loadaddr;
80 /* Reserved. */
81 grub_uint32_t reserved2;
83 /* The entry point of the bootcode. */
84 grub_uint32_t bootcode_entrypoint;
86 /* Reserved. */
87 grub_uint32_t reserved3;
89 /* A checksum of the bootcode. */
90 grub_uint32_t bootcode_checksum;
92 /* The processor type. */
93 char processor[16];
95 /* Padding. */
96 grub_uint16_t pad[187];
99 static struct grub_partition_map grub_apple_partition_map;
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)
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;
113 part.partmap = &grub_apple_partition_map;
115 if (grub_disk_read (disk, 0, 0, sizeof (aheader), &aheader))
116 return grub_errno;
118 if (grub_be_to_cpu16 (aheader.magic) != GRUB_APPLE_HEADER_MAGIC)
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;
127 pos = grub_be_to_cpu16 (aheader.blocksize);
131 part.offset = pos / GRUB_DISK_SECTOR_SIZE;
132 part.index = pos % GRUB_DISK_SECTOR_SIZE;
134 if (grub_disk_read (disk, part.offset, part.index,
135 sizeof (struct grub_apple_part), &apart))
136 return grub_errno;
138 if (grub_be_to_cpu16 (apart.magic) != GRUB_APPLE_PART_MAGIC)
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;
147 if (partnum == 0)
148 partnum = grub_be_to_cpu32 (apart.partmap_size);
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;
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, hook_data))
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 /* Partition map type. */
184 static struct grub_partition_map grub_apple_partition_map =
186 .name = "apple",
187 .iterate = apple_partition_map_iterate,
190 GRUB_MOD_INIT(part_apple)
192 grub_partition_map_register (&grub_apple_partition_map);
195 GRUB_MOD_FINI(part_apple)
197 grub_partition_map_unregister (&grub_apple_partition_map);